]> Pileus Git - ~andy/fetchmail/blob - smtp.c
cee1841bbc365505ffa2f308e3289b38701b9fc7
[~andy/fetchmail] / smtp.c
1 /*
2  * smtp.c -- code for speaking SMTP to a listener port
3  *
4  * Concept due to Harry Hochheiser.  Implementation by ESR.  Cleanup and
5  * strict RFC821 compliance by Cameron MacPherson.
6  *
7  * Copyright 1996 Eric S. Raymond
8  * All rights reserved.
9  * For license terms, see the file COPYING in this directory.
10  */
11
12 #include <stdio.h>
13 #include <config.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include "fetchmail.h"
17 #include "smtp.h"
18
19 int smtp_response;      /* numeric value of SMTP response code */
20
21 int SMTP_helo(FILE *sockfp,char *host)
22 /* send a "HELO" message to the SMTP listener */
23 {
24   int ok;
25
26   fprintf(sockfp,"HELO %s\r\n", host);
27   if (outlevel == O_VERBOSE)
28       error(0, 0, "SMTP> HELO %s", host);
29   ok = SMTP_ok(sockfp);
30   return ok;
31 }
32
33 int SMTP_from(FILE *sockfp, char *from)
34 /* send a "MAIL FROM:" message to the SMTP listener */
35 {
36   int ok;
37
38   fprintf(sockfp,"MAIL FROM:<%s>\r\n", from);
39   if (outlevel == O_VERBOSE)
40       error(0, 0, "SMTP> MAIL FROM:<%s>", from);
41   ok = SMTP_ok(sockfp);
42   return ok;
43 }
44
45 int SMTP_rcpt(FILE *sockfp, char *to)
46 /* send a "RCPT TO:" message to the SMTP listener */
47 {
48   int ok;
49
50   fprintf(sockfp,"RCPT TO:<%s>\r\n", to);
51   if (outlevel == O_VERBOSE)
52       error(0, 0, "SMTP> RCPT TO:<%s>", to);
53   ok = SMTP_ok(sockfp);
54   return ok;
55 }
56
57 int SMTP_data(FILE *sockfp)
58 /* send a "DATA" message to the SMTP listener */
59 {
60   int ok;
61
62   fprintf(sockfp,"DATA\r\n");
63   if (outlevel == O_VERBOSE)
64       error(0, 0, "SMTP> DATA");
65   ok = SMTP_ok(sockfp);
66   return ok;
67 }
68
69 int SMTP_quit(FILE *sockfp)
70 /* send a "QUIT" message to the SMTP listener */
71 {
72   int ok;
73
74   fprintf(sockfp,"QUIT\r\n");
75   if (outlevel == O_VERBOSE)
76       error(0, 0, "SMTP> QUIT");
77   ok = SMTP_ok(sockfp);
78   return ok;
79 }
80
81 int SMTP_eom(FILE *sockfp)
82 /* send a message data terminator to the SMTP listener */
83 {
84   int ok;
85
86   fprintf(sockfp,".\r\n");
87   if (outlevel == O_VERBOSE)
88       error(0, 0, "SMTP>. (EOM)");
89   ok = SMTP_ok(sockfp);
90   return ok;
91 }
92
93 int SMTP_ok(FILE *sockfp)
94 /* returns status of SMTP connection */
95 {
96     char buf[SMTPBUFSIZE], *ip;
97   
98     while ((ip = fgets(buf, sizeof(buf)-1, sockfp)))
99     {
100         int  n = strlen(ip);
101
102         if (buf[strlen(buf)-1] == '\n')
103             buf[strlen(buf)-1] = '\0';
104         if (buf[strlen(buf)-1] == '\r')
105             buf[strlen(buf)-1] = '\r';
106         if (n < 4)
107             return SM_ERROR;
108         buf[n] = '\0';
109         if (outlevel == O_VERBOSE)
110             error(0, 0, "SMTP< %s", buf);
111         smtp_response = atoi(buf);
112         if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') && buf[3] == ' ')
113             return SM_OK;
114         else if (buf[3] != '-')
115             return SM_ERROR;
116     }
117     return SM_UNRECOVERABLE;
118 }
119
120 /* smtp.c ends here */