diff options
author | Hoa V. DINH <dinh.viet.hoa@gmail.com> | 2013-08-21 22:00:08 -0700 |
---|---|---|
committer | Hoa V. DINH <dinh.viet.hoa@gmail.com> | 2013-08-21 22:00:08 -0700 |
commit | c89bf888c725d8997c76375be52d2ec1819a767a (patch) | |
tree | ff328471acadb8c056614df6e86ad7fe320b93f0 /src/core/smtp | |
parent | 75810bbb5cade21363fff5096ee36ff4ead9d117 (diff) |
Check certificate can be disabled. Fixed #324: reconnect SMTP properly after it's been disconnected.
Diffstat (limited to 'src/core/smtp')
-rw-r--r-- | src/core/smtp/MCSMTPSession.cc | 165 | ||||
-rw-r--r-- | src/core/smtp/MCSMTPSession.h | 1 |
2 files changed, 95 insertions, 71 deletions
diff --git a/src/core/smtp/MCSMTPSession.cc b/src/core/smtp/MCSMTPSession.cc index 6421d138..78eb4113 100644 --- a/src/core/smtp/MCSMTPSession.cc +++ b/src/core/smtp/MCSMTPSession.cc @@ -14,9 +14,9 @@ using namespace mailcore; enum { - STATE_DISCONNECTED, - STATE_CONNECTED, - STATE_LOGGEDIN, + STATE_DISCONNECTED, + STATE_CONNECTED, + STATE_LOGGEDIN, }; void SMTPSession::init() @@ -31,6 +31,7 @@ void SMTPSession::init() mTimeout = 30; mCheckCertificateEnabled = true; mUseHeloIPEnabled = false; + mShouldDisconnect = false; mSmtp = NULL; mProgressCallback = NULL; @@ -147,6 +148,8 @@ bool SMTPSession::isCheckCertificateEnabled() bool SMTPSession::checkCertificate() { + if (!isCheckCertificateEnabled()) + return true; return mailcore::checkCertificate(mSmtp->stream, hostname()); } @@ -212,6 +215,17 @@ void SMTPSession::unsetup() void SMTPSession::connectIfNeeded(ErrorCode * pError) { + if (mSmtp != NULL) { + // In case pipelining is available, libetpan will disconnect the session. + if (mSmtp->stream == NULL) { + mShouldDisconnect = true; + } + } + if (mShouldDisconnect) { + disconnect(); + mShouldDisconnect = false; + } + if (mState == STATE_DISCONNECTED) { connect(pError); } @@ -222,114 +236,114 @@ void SMTPSession::connectIfNeeded(ErrorCode * pError) void SMTPSession::connect(ErrorCode * pError) { - int r; - + int r; + setup(); switch (mConnectionType) { - case ConnectionTypeStartTLS: - MCLog("connect %s %u", MCUTF8(hostname()), (unsigned int) port()); - r = mailsmtp_socket_connect(mSmtp, MCUTF8(hostname()), port()); - if (r != MAILSMTP_NO_ERROR) { + case ConnectionTypeStartTLS: + MCLog("connect %s %u", MCUTF8(hostname()), (unsigned int) port()); + r = mailsmtp_socket_connect(mSmtp, MCUTF8(hostname()), port()); + if (r != MAILSMTP_NO_ERROR) { * pError = ErrorConnection; - return; - } - - MCLog("init"); + goto close; + } + + MCLog("init"); if (useHeloIPEnabled()) { r = mailsmtp_init_with_ip(mSmtp, 1); } else { r = mailsmtp_init(mSmtp); } - if (r == MAILSMTP_ERROR_STREAM) { + if (r == MAILSMTP_ERROR_STREAM) { * pError = ErrorConnection; - return; - } - else if (r != MAILSMTP_NO_ERROR) { + goto close; + } + else if (r != MAILSMTP_NO_ERROR) { * pError = ErrorConnection; - return; - } - - MCLog("start TLS"); - r = mailsmtp_socket_starttls(mSmtp); - if (r != MAILSMTP_NO_ERROR) { + goto close; + } + + MCLog("start TLS"); + r = mailsmtp_socket_starttls(mSmtp); + if (r != MAILSMTP_NO_ERROR) { * pError = ErrorStartTLSNotAvailable; - return; - } - MCLog("done"); - if (!checkCertificate()) { + goto close; + } + MCLog("done"); + if (!checkCertificate()) { * pError = ErrorCertificate; - return; + goto close; } - - MCLog("init after starttls"); + + MCLog("init after starttls"); if (useHeloIPEnabled()) { r = mailsmtp_init_with_ip(mSmtp, 1); } else { r = mailsmtp_init(mSmtp); } - if (r == MAILSMTP_ERROR_STREAM) { + if (r == MAILSMTP_ERROR_STREAM) { * pError = ErrorConnection; - return; - } - else if (r != MAILSMTP_NO_ERROR) { + goto close; + } + else if (r != MAILSMTP_NO_ERROR) { * pError = ErrorConnection; - return; - } + goto close; + } + + break; - break; - - case ConnectionTypeTLS: - r = mailsmtp_ssl_connect(mSmtp, MCUTF8(mHostname), port()); - if (r != MAILSMTP_NO_ERROR) { + case ConnectionTypeTLS: + r = mailsmtp_ssl_connect(mSmtp, MCUTF8(mHostname), port()); + if (r != MAILSMTP_NO_ERROR) { * pError = ErrorConnection; - return; - } - if (!checkCertificate()) { + goto close; + } + if (!checkCertificate()) { * pError = ErrorCertificate; - return; + goto close; } - - MCLog("init"); + + MCLog("init"); if (useHeloIPEnabled()) { r = mailsmtp_init_with_ip(mSmtp, 1); } else { r = mailsmtp_init(mSmtp); } - if (r == MAILSMTP_ERROR_STREAM) { + if (r == MAILSMTP_ERROR_STREAM) { * pError = ErrorConnection; - return; - } - else if (r != MAILSMTP_NO_ERROR) { + goto close; + } + else if (r != MAILSMTP_NO_ERROR) { * pError = ErrorConnection; - return; - } - - break; - - default: - r = mailsmtp_socket_connect(mSmtp, MCUTF8(hostname()), port()); - if (r != MAILIMAP_NO_ERROR) { + goto close; + } + + break; + + default: + r = mailsmtp_socket_connect(mSmtp, MCUTF8(hostname()), port()); + if (r != MAILIMAP_NO_ERROR) { * pError = ErrorConnection; - return; - } - - MCLog("init"); + goto close; + } + + MCLog("init"); if (useHeloIPEnabled()) { r = mailsmtp_init_with_ip(mSmtp, 1); } else { r = mailsmtp_init(mSmtp); } - if (r == MAILSMTP_ERROR_STREAM) { + if (r == MAILSMTP_ERROR_STREAM) { * pError = ErrorConnection; - return; - } - else if (r != MAILSMTP_NO_ERROR) { + goto close; + } + else if (r != MAILSMTP_NO_ERROR) { * pError = ErrorConnection; - return; - } - - break; + goto close; + } + + break; } mailstream_low * low; @@ -348,6 +362,10 @@ void SMTPSession::connect(ErrorCode * pError) mState = STATE_CONNECTED; * pError = ErrorNone; + return; + +close: + unsetup(); } void SMTPSession::disconnect() @@ -510,6 +528,7 @@ void SMTPSession::login(ErrorCode * pError) } if (r == MAILSMTP_ERROR_STREAM) { * pError = ErrorConnection; + mShouldDisconnect = true; return; } else if (r != MAILSMTP_NO_ERROR) { @@ -532,6 +551,7 @@ void SMTPSession::checkAccount(Address * from, ErrorCode * pError) r = mailsmtp_mail(mSmtp, MCUTF8(from->mailbox())); if (r == MAILSMTP_ERROR_STREAM) { * pError = ErrorConnection; + mShouldDisconnect = true; return; } else if (r != MAILSMTP_NO_ERROR) { @@ -542,6 +562,7 @@ void SMTPSession::checkAccount(Address * from, ErrorCode * pError) r = mailsmtp_rcpt(mSmtp, "email@invalid.com"); if (r == MAILSMTP_ERROR_STREAM) { * pError = ErrorConnection; + mShouldDisconnect = true; return; } else if (r != MAILSMTP_NO_ERROR) { @@ -589,6 +610,7 @@ void SMTPSession::sendMessage(Address * from, Array * recipients, Data * message r = mailesmtp_send(mSmtp, MCUTF8(from->mailbox()), 0, NULL, address_list, messageData->bytes(), messageData->length()); + mailsmtp_quit(mSmtp); } esmtp_address_list_free(address_list); @@ -603,6 +625,7 @@ void SMTPSession::sendMessage(Address * from, Array * recipients, Data * message if ((r == MAILSMTP_ERROR_STREAM) || (r == MAILSMTP_ERROR_CONNECTION_REFUSED)) { * pError = ErrorConnection; + mShouldDisconnect = true; goto err; } else if (r == MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION) { diff --git a/src/core/smtp/MCSMTPSession.h b/src/core/smtp/MCSMTPSession.h index 1b4c9690..28f94ada 100644 --- a/src/core/smtp/MCSMTPSession.h +++ b/src/core/smtp/MCSMTPSession.h @@ -75,6 +75,7 @@ namespace mailcore { time_t mTimeout; bool mCheckCertificateEnabled; bool mUseHeloIPEnabled; + bool mShouldDisconnect; mailsmtp * mSmtp; SMTPProgressCallback * mProgressCallback; |