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 ***********************************************************************/
16 #include <sys/types.h>
20 #include "popclient.h"
23 /*********************************************************************
25 description: Send a "HELO" message to the SMTP server.
28 socket TCP/IP socket for connection to SMTP
29 return value: Result of SMTP_OK: based on codes in popclient.h.
31 *********************************************************************/
33 int SMTP_helo(int socket,char *host)
36 char buf[SMTPBUFSIZE];
38 sprintf(buf,"HELO %s\r\n",host);
39 SockPrintf(socket,"%s",buf);
40 ok = SMTP_ok(socket,buf);
45 /*********************************************************************
47 description: Send a "MAIL FROM:" message to the SMTP server.
50 socket TCP/IP socket for connection to SMTP
51 from user name/host of originator
53 Note: these args are likely to change, as we get fancier about
56 return value: Result of SMTP_ok: based on codes in popclient.h.
58 *********************************************************************/
59 int SMTP_from(int socket,char *from)
61 char buf[SMTPBUFSIZE]; /* it's as good as size as any... */
63 SockPrintf(socket, "MAIL FROM %s\n", from);
64 ok = SMTP_ok(socket,buf);
70 /*********************************************************************
72 description: Send a "RCPT TO:" message to the SMTP server.
75 socket TCP/IP socket for connection to SMTP
76 touser: user name of recipient
77 tohost: host name of recipient
79 return value: Result of SMTP_OK: based on codes in popclient.h.
81 *********************************************************************/
82 int SMTP_rcpt(int socket,char *to)
84 char buf[SMTPBUFSIZE]; /* it's as good as size as any... */
87 SockPrintf(socket, "RCPT TO: %s\n", to);
88 ok = SMTP_ok(socket,buf);
94 /*********************************************************************
96 description: Send a "DATA" message to the SMTP server.
99 socket TCP/IP socket for connection to SMTP
101 *********************************************************************/
102 int SMTP_data(int socket)
104 SockPrintf(socket,"DATA\n");
107 /*********************************************************************
109 description: Send a message data termination to the SMTP server.
112 socket TCP/IP socket for connection to SMTP
113 return value: Result of SMTP_OK: based on codes in popclient.h.
115 *********************************************************************/
117 int SMTP_eom(int socket)
121 SockPuts(socket,".");
122 ok = SMTP_ok(socket,NULL);
126 /*********************************************************************
128 description: Send an "RSET" message to the SMTP server.
131 socket TCP/IP socket for connection to SMTP
133 *********************************************************************/
134 void SMTP_rset(int socket)
136 SockPrintf(socket,"RSET\n");
139 /*********************************************************************
141 description: Returns the status of the smtp connection
143 socket TCP/IP socket for connection to SMTP
144 return value: based on codes in popclient.h.
145 Do the dirty work of seeing what the status is..
146 *********************************************************************/
147 static int SMTP_check(int socket,char *argbuf)
150 char buf[SMTPBUFSIZE];
152 if (SMTP_Gets(socket, buf, sizeof(buf)) > 0) {
155 if (buf[0] == '1' || buf[0] == '2' || buf[0] == '3')
161 ok = SM_UNRECOVERABLE;
165 /*********************************************************************
167 description: Returns the statsus of the smtp connection
169 socket TCP/IP socket for connection to SMTP
170 return value: based on codes in popclient.h.
171 *********************************************************************/
172 int SMTP_ok(int socket,char *argbuf)
175 char buf[SMTPBUFSIZE];
177 /* I can tell that the SMTP server connection is ok if I can read a
178 status message that starts with "1xx" ,"2xx" or "3xx".
179 Therefore, it can't be ok if there's no data waiting to be read
181 Tried to deal with this with a call to SockDataWaiting, but
186 ok = SMTP_check(socket,argbuf);
187 if (ok == SM_ERROR) /* if we got an error, */
190 ok = SMTP_check(socket,argbuf); /* how does it look now ? */
192 ok = SM_ERROR; /* It's just a simple error, for*/
193 /* the current message */
195 ok = SM_UNRECOVERABLE; /* if it still says error, we're */
201 /*********************************************************************
203 description: Gets a line from the SMTP connection
205 socket TCP/IP socket for connection to SMTP
206 return value: number of bytes read.
207 *********************************************************************/
208 int SMTP_Gets(int socket,char *buf,int sz)
210 return read(socket,buf,sz);