]> Pileus Git - ~andy/fetchmail/blob - smtp.c
Return of the dancing progress dots.
[~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 1997 Eric S. Raymond
8  * For license terms, see the file COPYING in this directory.
9  */
10
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include "fetchmail.h"
15 #include "socket.h"
16 #include "smtp.h"
17 #include "config.h"
18
19 struct opt
20 {
21     const char *name;
22     int value;
23 };
24
25 static struct opt extensions[] =
26 {
27     {"8BITMIME",        ESMTP_8BITMIME},
28     {"SIZE",            ESMTP_SIZE},
29     {"ETRN",            ESMTP_ETRN},
30     {(char *)NULL, 0},
31 };
32
33 char smtp_response[MSGBUFSIZE];
34
35 static char smtp_mode = 'S';
36
37 void SMTP_setmode(char sl)
38 /* set whether we are speaking SMTP or LMTP */
39 {
40     smtp_mode = sl;
41 }
42
43 int SMTP_helo(int sock,const char *host)
44 /* send a "HELO" message to the SMTP listener */
45 {
46   int ok;
47
48   SockPrintf(sock,"HELO %s\r\n", host);
49   if (outlevel >= O_MONITOR)
50       report(stdout, 0, "SMTP> HELO %s\n", host);
51   ok = SMTP_ok(sock);
52   return ok;
53 }
54
55 int SMTP_ehlo(int sock, const char *host, int *opt)
56 /* send a "EHLO" message to the SMTP listener, return extension status bits */
57 {
58   struct opt *hp;
59
60   SockPrintf(sock,"%cHLO %s\r\n", (smtp_mode == 'S') ? 'E' : smtp_mode, host);
61   if (outlevel >= O_MONITOR)
62       report(stdout, 0, "%cMTP> %cHLO %s\n", 
63             smtp_mode, (smtp_mode == 'S') ? 'E' : smtp_mode, host);
64   
65   *opt = 0;
66   while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
67   {
68       int  n = strlen(smtp_response);
69
70       if (smtp_response[strlen(smtp_response)-1] == '\n')
71           smtp_response[strlen(smtp_response)-1] = '\0';
72       if (smtp_response[strlen(smtp_response)-1] == '\r')
73           smtp_response[strlen(smtp_response)-1] = '\0';
74       if (n < 4)
75           return SM_ERROR;
76       smtp_response[n] = '\0';
77       if (outlevel >= O_MONITOR)
78           report(stdout, 0, "SMTP< %s\n", smtp_response);
79       for (hp = extensions; hp->name; hp++)
80           if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name)))
81               *opt |= hp->value;
82       if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
83           return SM_OK;
84       else if (smtp_response[3] != '-')
85           return SM_ERROR;
86   }
87   return SM_UNRECOVERABLE;
88 }
89
90 int SMTP_from(int sock, const char *from, const char *opts)
91 /* send a "MAIL FROM:" message to the SMTP listener */
92 {
93     int ok;
94     char buf[MSGBUFSIZE];
95
96     if (strchr(from, '<'))
97         sprintf(buf, "MAIL FROM: %s", from);
98     else
99         sprintf(buf, "MAIL FROM:<%s>", from);
100     if (opts)
101         strcat(buf, opts);
102     SockPrintf(sock,"%s\r\n", buf);
103     if (outlevel >= O_MONITOR)
104         report(stdout, 0, "%cMTP> %s\n", smtp_mode, buf);
105     ok = SMTP_ok(sock);
106     return ok;
107 }
108
109 int SMTP_rcpt(int sock, const char *to)
110 /* send a "RCPT TO:" message to the SMTP listener */
111 {
112   int ok;
113
114   SockPrintf(sock,"RCPT TO:<%s>\r\n", to);
115   if (outlevel >= O_MONITOR)
116       report(stdout, 0, "%cMTP> RCPT TO:<%s>\n", smtp_mode, to);
117   ok = SMTP_ok(sock);
118   return ok;
119 }
120
121 int SMTP_data(int sock)
122 /* send a "DATA" message to the SMTP listener */
123 {
124   int ok;
125
126   SockPrintf(sock,"DATA\r\n");
127   if (outlevel >= O_MONITOR)
128       report(stdout, 0, "%cMTP> DATA\n", smtp_mode);
129   ok = SMTP_ok(sock);
130   return ok;
131 }
132
133 int SMTP_rset(int sock)
134 /* send a "RSET" message to the SMTP listener */
135 {
136   int ok;
137
138   SockPrintf(sock,"RSET\r\n");
139   if (outlevel >= O_MONITOR)
140       report(stdout, 0, "%cMTP> RSET\n", smtp_mode);
141   ok = SMTP_ok(sock);
142   return ok;
143 }
144
145 int SMTP_quit(int sock)
146 /* send a "QUIT" message to the SMTP listener */
147 {
148   int ok;
149
150   SockPrintf(sock,"QUIT\r\n");
151   if (outlevel >= O_MONITOR)
152       report(stdout, 0, "%cMTP> QUIT\n", smtp_mode);
153   ok = SMTP_ok(sock);
154   return ok;
155 }
156
157 int SMTP_eom(int sock)
158 /* send a message data terminator to the SMTP listener */
159 {
160   int ok;
161
162   SockPrintf(sock,".\r\n");
163   if (outlevel >= O_MONITOR)
164       report(stdout, 0, "%cMTP>. (EOM)\n", smtp_mode);
165
166   /* 
167    * When doing LMTP, must process many of these at the outer level. 
168    */
169   if (smtp_mode == 'S')
170       ok = SMTP_ok(sock);
171   else
172       ok = SM_OK;
173
174   return ok;
175 }
176
177 int SMTP_ok(int sock)
178 /* returns status of SMTP connection */
179 {
180     while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
181     {
182         int  n = strlen(smtp_response);
183
184         if (smtp_response[strlen(smtp_response)-1] == '\n')
185             smtp_response[strlen(smtp_response)-1] = '\0';
186         if (smtp_response[strlen(smtp_response)-1] == '\r')
187             smtp_response[strlen(smtp_response)-1] = '\0';
188         if (n < 4)
189             return SM_ERROR;
190         smtp_response[n] = '\0';
191         if (outlevel >= O_MONITOR)
192             report(stdout, 0, "%cMTP< %s\n", smtp_mode, smtp_response);
193         if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
194             return SM_OK;
195         else if (smtp_response[3] != '-')
196             return SM_ERROR;
197     }
198     return SM_UNRECOVERABLE;
199 }
200
201 /* smtp.c ends here */