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