|
|
1.1 paf 1: /** @file
2: Parser: SMTP sender.
3:
1.8 paf 4: Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.9 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6:
7: Parts of the code here is based upon an early gensock and blat
8: */
1.12 paf 9:
1.19 ! paf 10: static const char* IDENT_SMTP_C="$Date: 2002/12/15 13:53:45 $";
1.1 paf 11:
1.7 paf 12: #include "pa_exception.h"
1.1 paf 13: #include "smtp.h"
14:
1.17 paf 15: //#define DEBUG_SHOW
16:
1.1 paf 17: SMTP::SMTP(Pool& pool, const String& aorigin_string) : Pooled(pool),
18: origin_string(aorigin_string) {
19: the_socket = 0;
20: in_index = 0;
21: out_index = 0;
22: in_buffer = 0;
23: in_buffer_total = 0;
24: out_buffer_total = 0;
25:
26: in_buffer = (char *)malloc(SOCKET_BUFFER_SIZE);
27: out_buffer = (char *)malloc(SOCKET_BUFFER_SIZE);
28:
29: last_winsock_error = 0;
30: }
31:
32:
33: // ---------------------------------------------------------------------------
34: void SMTP::
1.2 paf 35: ConnectToHost(const char *hostname, const char *service)
1.1 paf 36: {
37: struct sockaddr_in sa_in;
38: int our_port;
39:
40: if( !ResolveService(service, &our_port) )
41: {
42: if( !ResolveHostname(hostname, &sa_in) )
43: {
44: sa_in.sin_family = AF_INET;
45: sa_in.sin_port = our_port;
46:
47: if( !GetAndSetTheSocket(&the_socket) )
48: {
49: if( !GetConnection(the_socket, &sa_in) )
50: {
51: MiscSocketSetup(the_socket, &fds, &timeout);
52:
53: return;
54: }
55: }
56: }
57: }
58:
59: CloseConnect();
60:
1.10 paf 61: throw Exception("smtp.connect",
1.1 paf 62: &origin_string,
63: "connect to %s:%s failed",
64: hostname, service);
65: }
66:
67: //---------------------------------------------------------------------------
68: // returns 0 if all is OK
69: int SMTP::
70: GetBuffer(int wait)
71: {
72: int retval;
73: int bytes_read = 0;
74: unsigned long ready_to_read = 0;
75:
76: // Use select to see if data is waiting...
77: FD_ZERO(&fds);
78: FD_SET(the_socket, &fds);
79:
80: // if wait is set, we are polling, return immediately
81: if( wait )
82: {
83: timeout.tv_sec = 0;
84: }
85: else
86: {
87: timeout.tv_sec = 30;
88: }
89:
90: if( SOCKET_ERROR == (retval = select(0, &fds, NULL, NULL, &timeout)) )
91: {
1.2 paf 92: //char what_error[256];
1.1 paf 93: int error_code = WSAGetLastError();
94:
95: if( error_code == WSAEINPROGRESS && wait )
96: {
97: return WAIT_A_BIT;
98: }
99:
1.2 paf 100: /*
1.1 paf 101: wsprintf(what_error,
102: "GetBuffer() unexpected error from select: %d",
103: error_code);
104:
105: ShowError(what_error);
1.2 paf 106: */
1.1 paf 107: }
108:
109: // if we don't want to wait
110: if( !retval && wait )
111: {
112: return WAIT_A_BIT;
113: }
114:
115: // we have data waiting...
116: bytes_read = recv(the_socket,
117: in_buffer,
118: SOCKET_BUFFER_SIZE,
119: 0);
120:
121: // just in case.
122: if( 0 == bytes_read )
123: {
124: // connection terminated (semi-) gracefully by the other side
125: return WSAENOTCONN;
126: }
127:
128: if( SOCKET_ERROR == bytes_read )
129: {
1.2 paf 130: //char what_error[256];
1.1 paf 131: int ws_error = WSAGetLastError();
132:
133: switch( ws_error )
134: {
135: // all these indicate loss of connection (are there more?)
136: case WSAENOTCONN:
137: case WSAENETDOWN:
138: case WSAENETUNREACH:
139: case WSAENETRESET:
140: case WSAECONNABORTED:
141: case WSAECONNRESET:
142: return WSAENOTCONN;
143:
144: case WSAEWOULDBLOCK:
145: return WAIT_A_BIT;
146:
147: default:
1.2 paf 148: /*wsprintf(what_error,
1.1 paf 149: "GetBuffer() unexpected error: %d",
150: ws_error);
151: ShowError(what_error);
1.2 paf 152: */
153: break;
1.1 paf 154: }
155: }
156:
157: // reset buffer indices.
158: in_buffer_total = bytes_read;
159: in_index = 0;
160:
161: return 0;
162: }
163:
164: //---------------------------------------------------------------------------
165: // returns 0 if all is OK
166: int SMTP::
167: GetChar(int wait, char *ch)
168: {
169: int retval = 0;
170:
171: if( in_index >= in_buffer_total )
172: {
173: if( 0 != (retval = GetBuffer(wait)) )
174: return retval;
175: }
176: *ch = in_buffer[in_index++];
177:
178: return 0;
179: }
180:
181: //----------------------------------------------------------------------
182: int SMTP::
183: get_line( void )
184: {
185: char ch = '.';
186: char in_data[MAXOUTLINE];
187: char *index;
188:
189: index = in_data;
190:
191: while( ch != '\n' )
192: {
193: if( 0 != GetChar(0, &ch) )
194: {
195: return -1;
196: }
197: else
198: {
199: *index = ch;
200: index++;
201: }
202: }
203:
204: if( in_data[3] == '-' )
205: return get_line();
206: else {
207: char *error_pos;
208: return strtol(in_data, &error_pos, 0);
209: }
210: }
211:
212: //---------------------------------------------------------------------------
213: // returns 0 if all is OK
214: void SMTP::
215: SendLine(const char *data, unsigned long length)
216: {
217: int num_sent;
218:
219: FD_ZERO(&fds);
220: FD_SET(the_socket, &fds);
221:
222: timeout.tv_sec = 30;
223:
224: while( length > 0 )
225: {
226: if( SOCKET_ERROR == select(0, NULL, &fds, NULL, &timeout) )
1.10 paf 227: throw Exception("smtp.execute",
1.1 paf 228: &origin_string,
229: "connection::put_data() unexpected error from select: %d",
230: WSAGetLastError());
231:
232: num_sent = send(the_socket,
233: data,
234: length > 1024 ? 1024 : (int)length,
235: 0);
236:
237: if( SOCKET_ERROR == num_sent )
238: {
239: int ws_error = WSAGetLastError();
240:
241: switch( ws_error )
242: {
243: // this is the only error we really expect to see.
244: case WSAENOTCONN:
245: return;
246:
247: // seems that we can still get a block
248: case WSAEWOULDBLOCK:
249: break;
250:
251: default:
1.10 paf 252: throw Exception("smtp.execute",
1.1 paf 253: &origin_string,
254: "connection::put_data() unexpected error from send(): %d",
255: ws_error);
256: }
257: }
258: else
259: {
260: length -= num_sent;
261: data += num_sent;
262: }
263: }
264: }
265:
266: //---------------------------------------------------------------------------
267: // returns 0 if all is OK
268: void SMTP::
269: SendBuffer(const char *data, unsigned long length)
270: {
271: DWORD retval = 0;
272: unsigned int sorta_sent = 0;
273:
274: while( length )
275: {
276: if( (out_index + length) < SOCKET_BUFFER_SIZE )
277: {
278: // we won't overflow, simply copy into the buffer
279: memcpy(out_buffer + out_index, data, length);
280: out_index += length;
281: length = 0;
282: }
283: else
284: {
285: unsigned int orphaned_chunk = SOCKET_BUFFER_SIZE - out_index;
286:
287: // we will overflow, handle it
288: memcpy(out_buffer + out_index, data, orphaned_chunk);
289:
290: // send this buffer...
291: SendLine(out_buffer, SOCKET_BUFFER_SIZE);
292:
293: length -= orphaned_chunk;
294: out_index = 0;
295: data += orphaned_chunk;
296: }
297: }
298: }
299:
300: //---------------------------------------------------------------------------
301: // returns 0 if all is OK
302: void SMTP::
303: FlushBuffer()
304: {
305: SendLine(out_buffer, out_index);
306: out_index = 0;
307: }
308:
309: //---------------------------------------------------------------------------
310: BOOL SMTP::
311: CloseConnect()
312: {
313: if( closesocket(the_socket) == SOCKET_ERROR )
314: return FALSE;
315:
316: return TRUE;
317: }
318:
319: //----------------------------------------------------------------------
320: void SMTP::
321: SendSmtpError(const char * message)
322: {
323: SendLine("QUIT\r\n", 6);
324: CloseConnect();
325:
1.10 paf 326: throw Exception("smtp.execute",
1.1 paf 327: &origin_string,
328: "failed: %s", message);
329: }
330:
331: //----------------------------------------------------------------------
332: // returns 0 if all is OK
333: // returns 20, 21, 22, 23, 24, 25 if SendBuffer() fails
334: // returns 26 if FlushBuffer() fails
335: void SMTP::
336: transform_and_send_edit_data(const char * editptr )
337: {
338: const char *index;
339: char previous_char = 'x';
340: unsigned int send_len;
341: BOOL done = 0;
342:
343: send_len = lstrlen(editptr);
344: index = editptr;
345:
346: while( !done )
347: {
348: // room for extra char for double dot on end case
349: while( (unsigned int)(index - editptr) < send_len )
350: {
351: switch( *index )
352: {
353: case '.':
354: if( previous_char == '\n' )
1.14 paf 355: SendBuffer(index, 1); // send _two_ dots...
1.1 paf 356: SendBuffer(index, 1);
357: break;
358:
1.14 paf 359: case '\n': // x\n -> \r\n
360: if( previous_char != '\r' ) {
361: SendBuffer("\r", 1);
362: SendBuffer(index, 1);
363: }
1.1 paf 364: break;
1.14 paf 365:
1.1 paf 366: default:
367: SendBuffer(index, 1);
368: break;
369: }
370: previous_char = *index;
371: index++;
372: }
373:
374: if( (unsigned int)(index - editptr) == send_len )
375: done = 1;
376: }
377:
378: // this handles the case where the user doesn't end the last
379: // line with a <return>
380:
381: if( editptr[send_len-1] != '\n' )
382: SendBuffer("\r\n.\r\n", 5);
383: else
384: SendBuffer(".\r\n", 3);
385: /* now make sure it's all sent... */
386: FlushBuffer();
387: }
388:
389: //----------------------------------------------------------------------
390: // returns 0 if all is OK
391: // returns 16 if any get_line()'s fail
392: // returns 20, 21, 22, 23, 24, 25, 26 if transform_and_send_edit_data() fails
393: void SMTP::
394: send_data(const char * message)
395: {
396: transform_and_send_edit_data(message);
397: if( 250 != get_line() )
398: SendSmtpError("Message not accepted by server");
399: }
400:
401: //----------------------------------------------------------------------
402: // returns 0 if all is OK
403: // returns 50, 51, 52 if fails
404: void SMTP::
1.2 paf 405: open_socket( const char *server, const char *service )
1.1 paf 406: {
407: ConnectToHost(server, service);
408:
409: if( gethostname(my_hostname, sizeof(my_hostname)) )
1.10 paf 410: throw Exception("smtp.connect",
1.1 paf 411: &origin_string,
412: "lookup of '%s' failed", my_hostname);
413: }
414:
415: //----------------------------------------------------------------------
416: // returns 0 if all is OK
417: // returns 50, 51, 52 if open_socket() fails
418: // returns 10, 11, 12, 13, 14, 15 if any get_line()'s fail
419: void SMTP::
1.2 paf 420: prepare_message(char *from, char *to, const char *server, const char *service)
1.1 paf 421: {
422: char out_data[MAXOUTLINE];
423: char *ptr;
424: int len;
425: int startLen;
426:
427: open_socket(server, service);
428:
429: if( 220 != get_line() )
430: SendSmtpError("SMTP server error");
431:
432: wsprintf(out_data, "HELO %s\r\n", my_hostname );
433: SendLine(out_data, lstrlen(out_data) );
434:
435: if( 250 != get_line() )
436: SendSmtpError("SMTP server error");
437:
438: wsprintf(out_data, "MAIL From: <%s>\r\n", from);
439: SendLine(out_data, lstrlen(out_data) );
440:
441: if( 250 != get_line() )
442: SendSmtpError("The mail server doesn't like the sender name, have you set your mail address correctly?");
443:
444: // do a series of RCPT lines for each name in address line
445: for( ptr = to; *ptr; ptr += len + 1 )
446: {
447: // if there's only one token left, then len will = startLen,
448: // and we'll iterate once only
449: startLen = lstrlen(ptr);
450: if( (len = strcspn(ptr, " ,\n\t\r")) != startLen )
451: {
452: ptr[len] = '\0'; // replace delim with NULL char
453: while( strchr (" ,\n\t\r", ptr[len+1]) ) // eat white space
454: ptr[len++] = '\0';
455: }
456:
457: wsprintf(out_data, "RCPT To: <%s>\r\n", ptr);
458: SendLine(out_data, lstrlen(out_data) );
459:
460: if( 250 != get_line() )
1.10 paf 461: throw Exception("smtp.execute",
1.1 paf 462: &origin_string,
463: "The mail server doesn't like the name %s. Have you set the 'To: ' field correctly?",
464: ptr);
465:
466: if( len == startLen ) // last token, we're done
467: break;
468: }
469:
470: wsprintf(out_data, "DATA\r\n");
471: SendLine(out_data, lstrlen(out_data));
472:
473: if( 354 != get_line() )
474: SendSmtpError("Mail server error accepting message data");
475: }
476:
477:
478: static char *lsplit(char *string, char delim) {
479: if(string) {
480: char *v=strchr(string, delim);
481: if(v) {
482: *v=0;
483: return v+1;
484: }
485: }
486: return 0;
487: }
488: static char *rsplit(char *string, char delim) {
489: if(string) {
490: char *v=strrchr(string, delim);
491: if(v) {
492: *v=0;
493: return v+1;
494: }
495: }
496: return NULL;
497: }
498:
499: //----------------------------------------------------------------------------
500: //----------------------------------------------------------------------------
501: // returns 0 if all is OK
502: // returns 1 if MakeSmtpHeader() fails
503: // returns 10, 11, 12, 13, 14, 15, 50, 51, 52 if prepare_message() fails
504: void SMTP::
1.2 paf 505: Send(const char *server, const char *service, const char *msg, char *from, char *to)
1.1 paf 506: {
1.17 paf 507: #ifdef DEBUG_SHOW
1.18 paf 508: throw Exception("paf.debug",0,"from=%s|to=%s|msg=%s", from,to,msg);
1.17 paf 509: #endif
1.15 paf 510:
1.11 paf 511: prepare_message( from, to, server, service);
1.1 paf 512:
513: send_data(msg);
514:
515: SendLine("QUIT\r\n", 6 );
516: CloseConnect();
517: }