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},
32 #endif /* ODMR_ENABLE */
36 char smtp_response[MSGBUFSIZE];
38 static char smtp_mode = 'S';
40 void SMTP_setmode(char sl)
41 /* set whether we are speaking SMTP or LMTP */
46 int SMTP_helo(int sock,const char *host)
47 /* send a "HELO" message to the SMTP listener */
51 SockPrintf(sock,"HELO %s\r\n", host);
52 if (outlevel >= O_MONITOR)
53 report(stdout, "SMTP> HELO %s\n", host);
58 int SMTP_ehlo(int sock, const char *host, int *opt)
59 /* send a "EHLO" message to the SMTP listener, return extension status bits */
63 SockPrintf(sock,"%cHLO %s\r\n", (smtp_mode == 'S') ? 'E' : smtp_mode, host);
64 if (outlevel >= O_MONITOR)
65 report(stdout, "%cMTP> %cHLO %s\n",
66 smtp_mode, (smtp_mode == 'S') ? 'E' : smtp_mode, host);
69 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
71 int n = strlen(smtp_response);
73 if (smtp_response[strlen(smtp_response)-1] == '\n')
74 smtp_response[strlen(smtp_response)-1] = '\0';
75 if (smtp_response[strlen(smtp_response)-1] == '\r')
76 smtp_response[strlen(smtp_response)-1] = '\0';
79 smtp_response[n] = '\0';
80 if (outlevel >= O_MONITOR)
81 report(stdout, "SMTP< %s\n", smtp_response);
82 for (hp = extensions; hp->name; hp++)
83 if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name)))
85 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
87 else if (smtp_response[3] != '-')
90 return SM_UNRECOVERABLE;
93 int SMTP_from(int sock, const char *from, const char *opts)
94 /* send a "MAIL FROM:" message to the SMTP listener */
99 if (strchr(from, '<'))
101 snprintf(buf, sizeof(buf),
104 #endif /* HAVE_SNPRINTF */
105 "MAIL FROM: %s", from);
108 snprintf(buf, sizeof(buf),
111 #endif /* HAVE_SNPRINTF */
112 "MAIL FROM:<%s>", from);
115 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%s", opts);
118 #endif /* HAVE_SNPRINTF */
119 SockPrintf(sock,"%s\r\n", buf);
120 if (outlevel >= O_MONITOR)
121 report(stdout, "%cMTP> %s\n", smtp_mode, buf);
126 int SMTP_rcpt(int sock, const char *to)
127 /* send a "RCPT TO:" message to the SMTP listener */
131 SockPrintf(sock,"RCPT TO:<%s>\r\n", to);
132 if (outlevel >= O_MONITOR)
133 report(stdout, "%cMTP> RCPT TO:<%s>\n", smtp_mode, to);
138 int SMTP_data(int sock)
139 /* send a "DATA" message to the SMTP listener */
143 SockPrintf(sock,"DATA\r\n");
144 if (outlevel >= O_MONITOR)
145 report(stdout, "%cMTP> DATA\n", smtp_mode);
150 int SMTP_rset(int sock)
151 /* send a "RSET" message to the SMTP listener */
155 SockPrintf(sock,"RSET\r\n");
156 if (outlevel >= O_MONITOR)
157 report(stdout, "%cMTP> RSET\n", smtp_mode);
162 int SMTP_quit(int sock)
163 /* send a "QUIT" message to the SMTP listener */
167 SockPrintf(sock,"QUIT\r\n");
168 if (outlevel >= O_MONITOR)
169 report(stdout, "%cMTP> QUIT\n", smtp_mode);
174 int SMTP_eom(int sock)
175 /* send a message data terminator to the SMTP listener */
179 SockPrintf(sock,".\r\n");
180 if (outlevel >= O_MONITOR)
181 report(stdout, "%cMTP>. (EOM)\n", smtp_mode);
184 * When doing LMTP, must process many of these at the outer level.
186 if (smtp_mode == 'S')
194 int SMTP_ok(int sock)
195 /* returns status of SMTP connection */
197 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
199 int n = strlen(smtp_response);
201 if (smtp_response[strlen(smtp_response)-1] == '\n')
202 smtp_response[strlen(smtp_response)-1] = '\0';
203 if (smtp_response[strlen(smtp_response)-1] == '\r')
204 smtp_response[strlen(smtp_response)-1] = '\0';
207 smtp_response[n] = '\0';
208 if (outlevel >= O_MONITOR)
209 report(stdout, "%cMTP< %s\n", smtp_mode, smtp_response);
210 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
212 else if (smtp_response[3] != '-')
215 return SM_UNRECOVERABLE;
218 /* smtp.c ends here */