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, '<'))
100 sprintf(buf, "MAIL FROM: %s", from);
102 sprintf(buf, "MAIL FROM:<%s>", from);
105 SockPrintf(sock,"%s\r\n", buf);
106 if (outlevel >= O_MONITOR)
107 report(stdout, "%cMTP> %s\n", smtp_mode, buf);
112 int SMTP_rcpt(int sock, const char *to)
113 /* send a "RCPT TO:" message to the SMTP listener */
117 SockPrintf(sock,"RCPT TO:<%s>\r\n", to);
118 if (outlevel >= O_MONITOR)
119 report(stdout, "%cMTP> RCPT TO:<%s>\n", smtp_mode, to);
124 int SMTP_data(int sock)
125 /* send a "DATA" message to the SMTP listener */
129 SockPrintf(sock,"DATA\r\n");
130 if (outlevel >= O_MONITOR)
131 report(stdout, "%cMTP> DATA\n", smtp_mode);
136 int SMTP_rset(int sock)
137 /* send a "RSET" message to the SMTP listener */
141 SockPrintf(sock,"RSET\r\n");
142 if (outlevel >= O_MONITOR)
143 report(stdout, "%cMTP> RSET\n", smtp_mode);
148 int SMTP_quit(int sock)
149 /* send a "QUIT" message to the SMTP listener */
153 SockPrintf(sock,"QUIT\r\n");
154 if (outlevel >= O_MONITOR)
155 report(stdout, "%cMTP> QUIT\n", smtp_mode);
160 int SMTP_eom(int sock)
161 /* send a message data terminator to the SMTP listener */
165 SockPrintf(sock,".\r\n");
166 if (outlevel >= O_MONITOR)
167 report(stdout, "%cMTP>. (EOM)\n", smtp_mode);
170 * When doing LMTP, must process many of these at the outer level.
172 if (smtp_mode == 'S')
180 int SMTP_ok(int sock)
181 /* returns status of SMTP connection */
183 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
185 int n = strlen(smtp_response);
187 if (smtp_response[strlen(smtp_response)-1] == '\n')
188 smtp_response[strlen(smtp_response)-1] = '\0';
189 if (smtp_response[strlen(smtp_response)-1] == '\r')
190 smtp_response[strlen(smtp_response)-1] = '\0';
193 smtp_response[n] = '\0';
194 if (outlevel >= O_MONITOR)
195 report(stdout, "%cMTP< %s\n", smtp_mode, smtp_response);
196 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
198 else if (smtp_response[3] != '-')
201 return SM_UNRECOVERABLE;
204 /* smtp.c ends here */