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