--- parser3/src/classes/smtp/Attic/smtp.C 2001/04/07 13:48:37 1.1 +++ parser3/src/classes/smtp/Attic/smtp.C 2003/11/20 16:34:24 1.23 @@ -1,21 +1,20 @@ /** @file Parser: SMTP sender. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - - Author: Alexander Petrosyan (http://design.ru/paf) - - $Id: smtp.C,v 1.1 2001/04/07 13:48:37 paf Exp $ - + Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) Parts of the code here is based upon an early gensock and blat */ -#include "smtp.h" +static const char * const IDENT_SMTP_C="$Date: 2003/11/20 16:34:24 $"; + #include "pa_exception.h" +#include "smtp.h" + +//#define DEBUG_SHOW -SMTP::SMTP(Pool& pool, const String& aorigin_string) : Pooled(pool), - origin_string(aorigin_string) { +SMTP::SMTP() { the_socket = 0; in_index = 0; out_index = 0; @@ -29,20 +28,25 @@ SMTP::SMTP(Pool& pool, const String& aor last_winsock_error = 0; } +SMTP::~SMTP() { + free(in_buffer); + free(out_buffer); +} + // --------------------------------------------------------------------------- void SMTP:: -ConnectToHost(char *hostname, char *service) +ConnectToHost(const char* hostname, const char* service) { struct sockaddr_in sa_in; - int our_port; + u_short our_port; if( !ResolveService(service, &our_port) ) { if( !ResolveHostname(hostname, &sa_in) ) { sa_in.sin_family = AF_INET; - sa_in.sin_port = our_port; + sa_in.sin_port = (unsigned short)our_port; if( !GetAndSetTheSocket(&the_socket) ) { @@ -58,8 +62,8 @@ ConnectToHost(char *hostname, char *serv CloseConnect(); - THROW(0, 0, - &origin_string, + throw Exception("smtp.connect", + 0, "connect to %s:%s failed", hostname, service); } @@ -71,7 +75,6 @@ GetBuffer(int wait) { int retval; int bytes_read = 0; - unsigned long ready_to_read = 0; // Use select to see if data is waiting... FD_ZERO(&fds); @@ -89,7 +92,7 @@ GetBuffer(int wait) if( SOCKET_ERROR == (retval = select(0, &fds, NULL, NULL, &timeout)) ) { - char what_error[256]; + //char what_error[256]; int error_code = WSAGetLastError(); if( error_code == WSAEINPROGRESS && wait ) @@ -97,11 +100,13 @@ GetBuffer(int wait) return WAIT_A_BIT; } + /* wsprintf(what_error, "GetBuffer() unexpected error from select: %d", error_code); ShowError(what_error); + */ } // if we don't want to wait @@ -125,7 +130,7 @@ GetBuffer(int wait) if( SOCKET_ERROR == bytes_read ) { - char what_error[256]; + //char what_error[256]; int ws_error = WSAGetLastError(); switch( ws_error ) @@ -143,10 +148,12 @@ GetBuffer(int wait) return WAIT_A_BIT; default: - wsprintf(what_error, + /*wsprintf(what_error, "GetBuffer() unexpected error: %d", ws_error); ShowError(what_error); + */ + break; } } @@ -208,7 +215,7 @@ get_line( void ) //--------------------------------------------------------------------------- // returns 0 if all is OK void SMTP:: -SendLine(const char *data, unsigned long length) +SendLine(const char* data, unsigned long length) { int num_sent; @@ -220,8 +227,8 @@ SendLine(const char *data, unsigned long while( length > 0 ) { if( SOCKET_ERROR == select(0, NULL, &fds, NULL, &timeout) ) - THROW(0, 0, - &origin_string, + throw Exception("smtp.execute", + 0, "connection::put_data() unexpected error from select: %d", WSAGetLastError()); @@ -245,8 +252,8 @@ SendLine(const char *data, unsigned long break; default: - THROW(0, 0, - &origin_string, + throw Exception("smtp.execute", + 0, "connection::put_data() unexpected error from send(): %d", ws_error); } @@ -262,11 +269,8 @@ SendLine(const char *data, unsigned long //--------------------------------------------------------------------------- // returns 0 if all is OK void SMTP:: -SendBuffer(const char *data, unsigned long length) +SendBuffer(const char* data, unsigned long length) { - DWORD retval = 0; - unsigned int sorta_sent = 0; - while( length ) { if( (out_index + length) < SOCKET_BUFFER_SIZE ) @@ -314,13 +318,13 @@ CloseConnect() //---------------------------------------------------------------------- void SMTP:: -SendSmtpError(const char * message) +SendSmtpError(const char* message) { SendLine("QUIT\r\n", 6); CloseConnect(); - THROW(0, 0, - &origin_string, + throw Exception("smtp.execute", + 0, "failed: %s", message); } @@ -329,10 +333,9 @@ SendSmtpError(const char * message) // returns 20, 21, 22, 23, 24, 25 if SendBuffer() fails // returns 26 if FlushBuffer() fails void SMTP:: -transform_and_send_edit_data(const char * editptr ) +transform_and_send_edit_data(const char* editptr ) { const char *index; - const char *header_end; char previous_char = 'x'; unsigned int send_len; BOOL done = 0; @@ -340,8 +343,6 @@ transform_and_send_edit_data(const char send_len = lstrlen(editptr); index = editptr; - header_end = strstr(editptr, "\r\n\r\n"); - while( !done ) { // room for extra char for double dot on end case @@ -351,22 +352,17 @@ transform_and_send_edit_data(const char { case '.': if( previous_char == '\n' ) - { - /* send _two_ dots... */ - SendBuffer(index, 1); - } + SendBuffer(index, 1); // send _two_ dots... SendBuffer(index, 1); break; - case '\r': - // watch for soft-breaks in the header, and ignore them - if( index < header_end && (strncmp(index, "\r\r\n", 3) == 0) ) - index += 2; - else - if( previous_char != '\r' ) - SendBuffer(index, 1); - // soft line-break (see EM_FMTLINES), skip extra CR */ + case '\n': // x\n -> \r\n + if( previous_char != '\r' ) { + SendBuffer("\r", 1); + SendBuffer(index, 1); + } break; + default: SendBuffer(index, 1); break; @@ -395,7 +391,7 @@ transform_and_send_edit_data(const char // returns 16 if any get_line()'s fail // returns 20, 21, 22, 23, 24, 25, 26 if transform_and_send_edit_data() fails void SMTP:: -send_data(const char * message) +send_data(const char* message) { transform_and_send_edit_data(message); if( 250 != get_line() ) @@ -406,13 +402,13 @@ send_data(const char * message) // returns 0 if all is OK // returns 50, 51, 52 if fails void SMTP:: -open_socket( char *server, char *service ) +open_socket( const char* server, const char* service ) { ConnectToHost(server, service); if( gethostname(my_hostname, sizeof(my_hostname)) ) - THROW(0, 0, - &origin_string, + throw Exception("smtp.connect", + 0, "lookup of '%s' failed", my_hostname); } @@ -421,7 +417,7 @@ open_socket( char *server, char *service // returns 50, 51, 52 if open_socket() fails // returns 10, 11, 12, 13, 14, 15 if any get_line()'s fail void SMTP:: -prepare_message(char *from, char *to, char *server, char *service) +prepare_message(char *from, char *to, const char* server, const char* service) { char out_data[MAXOUTLINE]; char *ptr; @@ -462,8 +458,8 @@ prepare_message(char *from, char *to, ch SendLine(out_data, lstrlen(out_data) ); if( 250 != get_line() ) - THROW(0, 0, - &origin_string, + throw Exception("smtp.execute", + 0, "The mail server doesn't like the name %s. Have you set the 'To: ' field correctly?", ptr); @@ -499,12 +495,6 @@ static char *rsplit(char *string, char d } return NULL; } -static char *extractEmail(char *email) { - lsplit(email, '>'); lsplit(email, '\x0D');lsplit(email, '\x0A'); - char *next=rsplit(email, '<'); - if(next) email=next; - return email; -} //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- @@ -512,9 +502,13 @@ static char *extractEmail(char *email) { // returns 1 if MakeSmtpHeader() fails // returns 10, 11, 12, 13, 14, 15, 50, 51, 52 if prepare_message() fails void SMTP:: -Send(char *server, char *service, const char *msg, char *from, char *to) +Send(const char* server, const char* service, const char* msg, char *from, char *to) { - prepare_message( extractEmail(from), extractEmail(to), server, service); +#ifdef DEBUG_SHOW + throw Exception("paf.debug",0,"from=%s|to=%s|msg=%s", from,to,msg); +#endif + + prepare_message( from, to, server, service); send_data(msg);