2 * smtp.c -- code for speaking SMTP to a listener port
4 * Concept due to Harry Hochheiser. Implementation by ESR. Cleanup and
5 * strict RFC821 compliance by Cameron MacPherson.
7 * Copyright 1997 Eric S. Raymond
8 * For license terms, see the file COPYING in this directory.
14 #include "fetchmail.h"
25 static struct opt extensions[] =
27 {"8BITMIME", ESMTP_8BITMIME},
33 char smtp_response[MSGBUFSIZE];
35 static char smtp_mode = 'S';
37 void SMTP_setmode(char sl)
38 /* set whether we are speaking SMTP or LMTP */
43 int SMTP_helo(int sock,const char *host)
44 /* send a "HELO" message to the SMTP listener */
48 SockPrintf(sock,"HELO %s\r\n", host);
49 if (outlevel >= O_MONITOR)
50 report(stdout, 0, "SMTP> HELO %s\n", host);
55 int SMTP_ehlo(int sock, const char *host, int *opt)
56 /* send a "EHLO" message to the SMTP listener, return extension status bits */
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);
66 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
68 int n = strlen(smtp_response);
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';
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)))
82 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
84 else if (smtp_response[3] != '-')
87 return SM_UNRECOVERABLE;
90 int SMTP_from(int sock, const char *from, const char *opts)
91 /* send a "MAIL FROM:" message to the SMTP listener */
96 if (strchr(from, '<'))
97 sprintf(buf, "MAIL FROM: %s", from);
99 sprintf(buf, "MAIL FROM:<%s>", from);
102 SockPrintf(sock,"%s\r\n", buf);
103 if (outlevel >= O_MONITOR)
104 report(stdout, 0, "%cMTP> %s\n", smtp_mode, buf);
109 int SMTP_rcpt(int sock, const char *to)
110 /* send a "RCPT TO:" message to the SMTP listener */
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);
121 int SMTP_data(int sock)
122 /* send a "DATA" message to the SMTP listener */
126 SockPrintf(sock,"DATA\r\n");
127 if (outlevel >= O_MONITOR)
128 report(stdout, 0, "%cMTP> DATA\n", smtp_mode);
133 int SMTP_rset(int sock)
134 /* send a "RSET" message to the SMTP listener */
138 SockPrintf(sock,"RSET\r\n");
139 if (outlevel >= O_MONITOR)
140 report(stdout, 0, "%cMTP> RSET\n", smtp_mode);
145 int SMTP_quit(int sock)
146 /* send a "QUIT" message to the SMTP listener */
150 SockPrintf(sock,"QUIT\r\n");
151 if (outlevel >= O_MONITOR)
152 report(stdout, 0, "%cMTP> QUIT\n", smtp_mode);
157 int SMTP_eom(int sock)
158 /* send a message data terminator to the SMTP listener */
162 SockPrintf(sock,".\r\n");
163 if (outlevel >= O_MONITOR)
164 report(stdout, 0, "%cMTP>. (EOM)\n", smtp_mode);
167 * When doing LMTP, must process many of these at the outer level.
169 if (smtp_mode == 'S')
177 int SMTP_ok(int sock)
178 /* returns status of SMTP connection */
180 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
182 int n = strlen(smtp_response);
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';
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] == ' ')
195 else if (smtp_response[3] != '-')
198 return SM_UNRECOVERABLE;
201 /* smtp.c ends here */