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