1 /* Copyright 1996 Eric S. Raymond
3 * For license terms, see the file COPYING in this directory.
6 /***********************************************************************
9 programmer: Harry Hochheiser
10 description: Handling of SMTP connections, and processing of mail
11 to be forwarded via SMTP connections.
13 ***********************************************************************/
17 #include <sys/types.h>
21 #include "fetchmail.h"
24 /*********************************************************************
26 description: Send a "HELO" message to the SMTP server.
29 socket TCP/IP socket for connection to SMTP
30 return value: Result of SMTP_OK: based on codes in fetchmail.h.
32 *********************************************************************/
34 int SMTP_helo(int socket,char *host)
37 char buf[SMTPBUFSIZE+1];
39 sprintf(buf,"HELO %s",host);
40 SockPuts(socket, buf);
41 if (outlevel == O_VERBOSE)
42 fprintf(stderr, "SMTP> %s\n", buf);
43 ok = SMTP_ok(socket,buf);
48 /*********************************************************************
50 description: Send a "MAIL FROM:" message to the SMTP server.
53 socket TCP/IP socket for connection to SMTP
54 from user name/host of originator
56 Note: these args are likely to change, as we get fancier about
59 return value: Result of SMTP_ok: based on codes in fetchmail.h.
61 *********************************************************************/
62 int SMTP_from(int socket, char *from)
64 char buf[SMTPBUFSIZE+1]; /* it's as good as size as any... */
66 SockPrintf(socket, "MAIL FROM: %s\n", from);
67 if (outlevel == O_VERBOSE)
68 fprintf(stderr, "SMTP> MAIL FROM: %s\n", from);
69 ok = SMTP_ok(socket,buf);
75 /*********************************************************************
77 description: Send a "RCPT TO:" message to the SMTP server.
80 socket TCP/IP socket for connection to SMTP
81 touser: user name of recipient
82 tohost: host name of recipient
84 return value: Result of SMTP_OK: based on codes in fetchmail.h.
86 *********************************************************************/
87 int SMTP_rcpt(int socket,char *to)
89 char buf[SMTPBUFSIZE+1]; /* it's as good as size as any... */
92 SockPrintf(socket, "RCPT TO: %s\n", to);
93 if (outlevel == O_VERBOSE)
94 fprintf(stderr, "SMTP> RCPT TO: %s\n", to);
95 ok = SMTP_ok(socket,buf);
101 /*********************************************************************
103 description: Send a "DATA" message to the SMTP server.
106 socket TCP/IP socket for connection to SMTP
108 *********************************************************************/
109 int SMTP_data(int socket)
113 SockPrintf(socket,"DATA\n");
114 if (outlevel == O_VERBOSE)
115 fprintf(stderr, "SMTP> DATA\n");
116 ok = SMTP_ok(socket, NULL);
121 /*********************************************************************
123 description: Send a message data termination to the SMTP server.
126 socket TCP/IP socket for connection to SMTP
127 return value: Result of SMTP_OK: based on codes in fetchmail.h.
129 *********************************************************************/
131 int SMTP_eom(int socket)
135 SockPuts(socket,".");
136 if (outlevel == O_VERBOSE)
137 fprintf(stderr, "SMTP> (EOM)\n");
138 ok = SMTP_ok(socket,NULL);
142 /*********************************************************************
144 description: Send an "RSET" message to the SMTP server.
147 socket TCP/IP socket for connection to SMTP
149 *********************************************************************/
150 void SMTP_rset(int socket)
152 SockPrintf(socket,"RSET\n");
153 if (outlevel == O_VERBOSE)
154 fprintf(stderr, "SMTP> RSET\n");
157 /*********************************************************************
159 description: Returns the status of the smtp connection
161 socket TCP/IP socket for connection to SMTP
162 return value: based on codes in fetchmail.h.
163 Do the dirty work of seeing what the status is..
164 *********************************************************************/
165 static int SMTP_check(int socket,char *argbuf)
168 char buf[SMTPBUFSIZE];
170 if ((ok = SMTP_Gets(socket, buf, sizeof(buf)-1)) > 0) {
172 if (outlevel == O_VERBOSE)
173 fprintf(stderr, "SMTP< %s", buf);
176 if (buf[0] == '1' || buf[0] == '2' || buf[0] == '3')
182 ok = SM_UNRECOVERABLE;
186 /*********************************************************************
188 description: Returns the statsus of the smtp connection
190 socket TCP/IP socket for connection to SMTP
191 return value: based on codes in fetchmail.h.
192 *********************************************************************/
193 int SMTP_ok(int socket,char *argbuf)
196 char buf[SMTPBUFSIZE+1];
198 /* I can tell that the SMTP server connection is ok if I can read a
199 status message that starts with "1xx" ,"2xx" or "3xx".
200 Therefore, it can't be ok if there's no data waiting to be read
202 Tried to deal with this with a call to SockDataWaiting, but
207 ok = SMTP_check(socket,argbuf);
208 if (ok == SM_ERROR) /* if we got an error, */
211 ok = SMTP_check(socket,argbuf); /* how does it look now ? */
213 ok = SM_ERROR; /* It's just a simple error, for*/
214 /* the current message */
216 ok = SM_UNRECOVERABLE; /* if it still says error, we're */
222 /*********************************************************************
224 description: Gets a line from the SMTP connection
226 socket TCP/IP socket for connection to SMTP
227 return value: number of bytes read.
228 *********************************************************************/
229 int SMTP_Gets(int socket,char *buf,int sz)
231 return read(socket,buf,sz);