]> Pileus Git - ~andy/fetchmail/blob - smtp.c
Enclose string dumps in doublequotes to work with visbuf properly.
[~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 <config.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include "fetchmail.h"
16 #include "socket.h"
17 #include "smtp.h"
18
19 struct opt
20 {
21     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 int SMTP_helo(int sock,char *host)
36 /* send a "HELO" message to the SMTP listener */
37 {
38   int ok;
39
40   SockPrintf(sock,"HELO %s\r\n", host);
41   if (outlevel == O_VERBOSE)
42       error(0, 0, "SMTP> HELO %s", host);
43   ok = SMTP_ok(sock);
44   return ok;
45 }
46
47 int SMTP_ehlo(int sock, char *host, int *opt)
48 /* send a "EHLO" message to the SMTP listener, return extension status bits */
49 {
50   struct opt *hp;
51
52   SockPrintf(sock,"EHLO %s\r\n", host);
53   if (outlevel == O_VERBOSE)
54       error(0, 0, "SMTP> EHLO %s", host);
55   
56   *opt = 0;
57   while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
58   {
59       int  n = strlen(smtp_response);
60
61       if (smtp_response[strlen(smtp_response)-1] == '\n')
62           smtp_response[strlen(smtp_response)-1] = '\0';
63       if (smtp_response[strlen(smtp_response)-1] == '\r')
64           smtp_response[strlen(smtp_response)-1] = '\0';
65       if (n < 4)
66           return SM_ERROR;
67       smtp_response[n] = '\0';
68       if (outlevel == O_VERBOSE)
69           error(0, 0, "SMTP< %s", smtp_response);
70       for (hp = extensions; hp->name; hp++)
71           if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name)))
72               *opt |= hp->value;
73       if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
74           return SM_OK;
75       else if (smtp_response[3] != '-')
76           return SM_ERROR;
77   }
78   return SM_UNRECOVERABLE;
79 }
80
81 int SMTP_from(int sock, char *from, char *opts)
82 /* send a "MAIL FROM:" message to the SMTP listener */
83 {
84     int ok;
85     char buf[MSGBUFSIZE];
86
87     if (strchr(from, '<'))
88         sprintf(buf, "MAIL FROM: %s", from);
89     else
90         sprintf(buf, "MAIL FROM:<%s>", from);
91     if (opts)
92         strcat(buf, opts);
93     SockPrintf(sock,"%s\r\n", buf);
94     if (outlevel == O_VERBOSE)
95         error(0, 0, "SMTP> %s", buf);
96     ok = SMTP_ok(sock);
97     return ok;
98 }
99
100 int SMTP_rcpt(int sock, char *to)
101 /* send a "RCPT TO:" message to the SMTP listener */
102 {
103   int ok;
104
105   SockPrintf(sock,"RCPT TO:<%s>\r\n", to);
106   if (outlevel == O_VERBOSE)
107       error(0, 0, "SMTP> RCPT TO:<%s>", to);
108   ok = SMTP_ok(sock);
109   return ok;
110 }
111
112 int SMTP_data(int sock)
113 /* send a "DATA" message to the SMTP listener */
114 {
115   int ok;
116
117   SockPrintf(sock,"DATA\r\n");
118   if (outlevel == O_VERBOSE)
119       error(0, 0, "SMTP> DATA");
120   ok = SMTP_ok(sock);
121   return ok;
122 }
123
124 int SMTP_rset(int sock)
125 /* send a "RSET" message to the SMTP listener */
126 {
127   int ok;
128
129   SockPrintf(sock,"RSET\r\n");
130   if (outlevel == O_VERBOSE)
131       error(0, 0, "SMTP> RSET");
132   ok = SMTP_ok(sock);
133   return ok;
134 }
135
136 int SMTP_quit(int sock)
137 /* send a "QUIT" message to the SMTP listener */
138 {
139   int ok;
140
141   SockPrintf(sock,"QUIT\r\n");
142   if (outlevel == O_VERBOSE)
143       error(0, 0, "SMTP> QUIT");
144   ok = SMTP_ok(sock);
145   return ok;
146 }
147
148 int SMTP_eom(int sock)
149 /* send a message data terminator to the SMTP listener */
150 {
151   int ok;
152
153   SockPrintf(sock,".\r\n");
154   if (outlevel == O_VERBOSE)
155       error(0, 0, "SMTP>. (EOM)");
156   ok = SMTP_ok(sock);
157   return ok;
158 }
159
160 int SMTP_ok(int sock)
161 /* returns status of SMTP connection */
162 {
163     while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
164     {
165         int  n = strlen(smtp_response);
166
167         if (smtp_response[strlen(smtp_response)-1] == '\n')
168             smtp_response[strlen(smtp_response)-1] = '\0';
169         if (smtp_response[strlen(smtp_response)-1] == '\r')
170             smtp_response[strlen(smtp_response)-1] = '\r';
171         if (n < 4)
172             return SM_ERROR;
173         smtp_response[n] = '\0';
174         if (outlevel == O_VERBOSE)
175             error(0, 0, "SMTP< %s", smtp_response);
176         if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
177             return SM_OK;
178         else if (smtp_response[3] != '-')
179             return SM_ERROR;
180     }
181     return SM_UNRECOVERABLE;
182 }
183
184 /* smtp.c ends here */