From cffd2917a378b47be00b589ff606a65d37be52ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sun, 19 Jun 2011 22:06:30 +0000 Subject: [PATCH] * Style cleanup. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@42259 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../outbound_protocols/smtp/smtp.cpp | 203 ++++++++---------- 1 file changed, 88 insertions(+), 115 deletions(-) diff --git a/src/add-ons/mail_daemon/outbound_protocols/smtp/smtp.cpp b/src/add-ons/mail_daemon/outbound_protocols/smtp/smtp.cpp index b355e68445..6f9d24b3a4 100644 --- a/src/add-ons/mail_daemon/outbound_protocols/smtp/smtp.cpp +++ b/src/add-ons/mail_daemon/outbound_protocols/smtp/smtp.cpp @@ -6,37 +6,40 @@ * Distributed under the terms of the MIT License. */ -//! implementation of the SMTP protocol -#include -#include -#include -#include -#include -#include -#include -#include +//! Implementation of the SMTP protocol -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "smtp.h" #include -#include "smtp.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + #ifdef USE_SSL # include #else @@ -65,18 +68,14 @@ using namespace std; ** written by Martin Schaaf */ void -MD5Hmac(unsigned char *digest, - const unsigned char* text, int text_len, - const unsigned char* key, int key_len) +MD5Hmac(unsigned char *digest, const unsigned char* text, int text_len, + const unsigned char* key, int key_len) { MD5_CTX context; - unsigned char k_ipad[64]; /* inner padding - - * key XORd with ipad - */ - unsigned char k_opad[64]; /* outer padding - - * key XORd with opad - */ - /* unsigned char tk[16]; */ + unsigned char k_ipad[64]; + // inner padding - key XORd with ipad + unsigned char k_opad[64]; + // outer padding - key XORd with opad int i; /* start out by storing key in pads */ @@ -106,7 +105,6 @@ MD5Hmac(unsigned char *digest, * and text is the data being protected */ - /* XOR key with ipad and opad values */ for (i = 0; i < 64; i++) { k_ipad[i] ^= 0x36; @@ -134,9 +132,8 @@ MD5Hmac(unsigned char *digest, void -MD5HexHmac(char *hexdigest, - const unsigned char* text, int text_len, - const unsigned char* key, int key_len) +MD5HexHmac(char *hexdigest, const unsigned char* text, int text_len, + const unsigned char* key, int key_len) { unsigned char digest[16]; int i; @@ -317,9 +314,7 @@ SMTPProtocol::Disconnect() } -// Process EMail to be sent - - +//! Process EMail to be sent status_t SMTPProtocol::SendMessages(const std::vector& mails, size_t totalBytes) @@ -351,8 +346,7 @@ SMTPProtocol::SendMessages(const std::vector& mails, } -// Opens connection to server - +//! Opens connection to server status_t SMTPProtocol::Open(const char *address, int port, bool esmtp) { @@ -381,28 +375,20 @@ SMTPProtocol::Open(const char *address, int port, bool esmtp) if (hostIP == 0) return EHOSTUNREACH; -#ifndef HAIKU_TARGET_PLATFORM_BEOS fSocket = socket(AF_INET, SOCK_STREAM, 0); -#else - fSocket = socket(AF_INET, 2, 0); -#endif - if (fSocket >= 0) { - struct sockaddr_in saAddr; - memset(&saAddr, 0, sizeof(saAddr)); - saAddr.sin_family = AF_INET; - saAddr.sin_port = htons(port); - saAddr.sin_addr.s_addr = hostIP; - int result = connect(fSocket, (struct sockaddr *) &saAddr, sizeof(saAddr)); - if (result < 0) { -#ifndef HAIKU_TARGET_PLATFORM_BEOS - close(fSocket); -#else - closesocket(fSocket); -#endif - fSocket = -1; - return errno; - } - } else { + if (fSocket < 0) + return errno; + + struct sockaddr_in saAddr; + memset(&saAddr, 0, sizeof(saAddr)); + saAddr.sin_family = AF_INET; + saAddr.sin_port = htons(port); + saAddr.sin_addr.s_addr = hostIP; + int result = connect(fSocket, (struct sockaddr *)&saAddr, + sizeof(saAddr)); + if (result < 0) { + close(fSocket); + fSocket = -1; return errno; } @@ -428,17 +414,12 @@ SMTPProtocol::Open(const char *address, int port, bool esmtp) error << ". (SSL connection error)"; ShowError(error.String()); SSL_CTX_free(ctx); - #ifndef HAIKU_TARGET_PLATFORM_BEOS - close(fSocket); - #else - closesocket(fSocket); - #endif - fSocket = -1; + close(fSocket); + fSocket = -1; return B_ERROR; } } - - #endif +#endif // USE_SSL BString line; ReceiveResponse(line); @@ -460,28 +441,23 @@ SMTPProtocol::Open(const char *address, int port, bool esmtp) return B_ERROR; } - - - #ifdef USE_SSL +#ifdef USE_SSL // Check for STARTTLS - if(use_STARTTLS) - { + if (use_STARTTLS) { const char *res = fLog.String(); char *p; - + SSL_library_init(); RAND_seed(this,sizeof(SMTPProtocol)); ::sprintf(cmd, "STARTTLS"CRLF); - - if ((p = ::strstr(res, "STARTTLS")) != NULL) - { + + if ((p = ::strstr(res, "STARTTLS")) != NULL) { // Server advertises STARTTLS support - if(SendCommand(cmd) != B_OK) - { + if (SendCommand(cmd) != B_OK) { delete[] cmd; return B_ERROR; } - + // We should start TLS negotiation use_ssl = true; ctx = SSL_CTX_new(TLSv1_method()); @@ -492,25 +468,23 @@ SMTPProtocol::Open(const char *address, int port, bool esmtp) SSL_set_connect_state(ssl); if(SSL_do_handshake(ssl) != 1) return B_ERROR; - + // Should send EHLO command again if(!esmtp) ::sprintf(cmd, "HELO %s"CRLF, localhost); else ::sprintf(cmd, "EHLO %s"CRLF, localhost); - - if(SendCommand(cmd) != B_OK) - { + + if (SendCommand(cmd) != B_OK) { delete[] cmd; return B_ERROR; } } - } - #endif - +#endif // USE_SSL + delete[] cmd; - + // Check auth type if (esmtp) { const char *res = fLog.String(); @@ -805,8 +779,6 @@ SMTPProtocol::Close() } -/** Send mail */ - status_t SMTPProtocol::Send(const char* to, const char* from, BPositionIO *message) { @@ -845,14 +817,14 @@ SMTPProtocol::Send(const char* to, const char* from, BPositionIO *message) // SMTP server will remove the periods. Of course, the POP server may then // add some of its own, but the POP client should take care of them. - ssize_t amountRead; - ssize_t amountToRead; - ssize_t amountUnread; - ssize_t bufferLen = 0; - const int bufferMax = 2000; - bool foundCRLFPeriod; - int i; - bool messageEndedWithCRLF = false; + ssize_t amountRead; + ssize_t amountToRead; + ssize_t amountUnread; + ssize_t bufferLen = 0; + const int bufferMax = 2000; + bool foundCRLFPeriod; + int i; + bool messageEndedWithCRLF = false; message->Seek(0, SEEK_END); amountUnread = message->Position(); @@ -883,15 +855,15 @@ SMTPProtocol::Send(const char* to, const char* from, BPositionIO *message) if (data[i] == '\r' && data[i+1] == '\n' && data[i+2] == '.') { foundCRLFPeriod = true; // Send data up to the CRLF, and include the period too. - #ifdef USE_SSL - if (use_ssl) { - if (SSL_write(ssl,data,i + 3) < 0) { - amountUnread = 0; // Stop when an error happens. - bufferLen = 0; - break; - } - } else - #endif +#ifdef USE_SSL + if (use_ssl) { + if (SSL_write(ssl,data,i + 3) < 0) { + amountUnread = 0; // Stop when an error happens. + bufferLen = 0; + break; + } + } else +#endif if (send (fSocket,data, i + 3,0) < 0) { amountUnread = 0; // Stop when an error happens. bufferLen = 0; @@ -954,8 +926,7 @@ SMTPProtocol::Send(const char* to, const char* from, BPositionIO *message) } -// Receives response from server. - +//! Receives response from server. int32 SMTPProtocol::ReceiveResponse(BString &out) { @@ -1064,7 +1035,9 @@ SMTPProtocol::SendCommand(const char *cmd) } -// Instantiate hook +// #pragma mark - + + OutboundProtocol* instantiate_outbound_protocol(BMailAccountSettings* settings) {