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 "popclient.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 popclient.h.
32 *********************************************************************/
34 int SMTP_helo(int socket,char *host)
37 char buf[SMTPBUFSIZE];
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 popclient.h.
61 *********************************************************************/
62 int SMTP_from(int socket, char *from)
64 char buf[SMTPBUFSIZE]; /* 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 popclient.h.
86 *********************************************************************/
87 int SMTP_rcpt(int socket,char *to)
89 char buf[SMTPBUFSIZE]; /* 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 popclient.h.
129 *********************************************************************/
131 int SMTP_eom(int socket)
135 SockPuts(socket,".");
136 if (outlevel == O_VERBOSE)
137 fprintf(stderr, "SMTP> .\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 popclient.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))) > 0) {
171 if (outlevel == O_VERBOSE)
174 fprintf(stderr, "SMTP< %s", buf);
178 if (buf[0] == '1' || buf[0] == '2' || buf[0] == '3')
184 ok = SM_UNRECOVERABLE;
188 /*********************************************************************
190 description: Returns the statsus of the smtp connection
192 socket TCP/IP socket for connection to SMTP
193 return value: based on codes in popclient.h.
194 *********************************************************************/
195 int SMTP_ok(int socket,char *argbuf)
198 char buf[SMTPBUFSIZE];
200 /* I can tell that the SMTP server connection is ok if I can read a
201 status message that starts with "1xx" ,"2xx" or "3xx".
202 Therefore, it can't be ok if there's no data waiting to be read
204 Tried to deal with this with a call to SockDataWaiting, but
209 ok = SMTP_check(socket,argbuf);
210 if (ok == SM_ERROR) /* if we got an error, */
213 ok = SMTP_check(socket,argbuf); /* how does it look now ? */
215 ok = SM_ERROR; /* It's just a simple error, for*/
216 /* the current message */
218 ok = SM_UNRECOVERABLE; /* if it still says error, we're */
224 /*********************************************************************
226 description: Gets a line from the SMTP connection
228 socket TCP/IP socket for connection to SMTP
229 return value: number of bytes read.
230 *********************************************************************/
231 int SMTP_Gets(int socket,char *buf,int sz)
233 return read(socket,buf,sz);