diff options
author | Hoa V. DINH <dinh.viet.hoa@gmail.com> | 2013-06-26 01:37:52 -0700 |
---|---|---|
committer | Hoa V. DINH <dinh.viet.hoa@gmail.com> | 2013-06-26 01:38:02 -0700 |
commit | 49eaf450344d73d815124bc343e92402dab5b7ab (patch) | |
tree | a7ceb39b05d7538ed85462ce459766cba508bce3 /src | |
parent | fe9037f4248d50c35679cea5534d6ee87e71ede6 (diff) |
Implement IMAP logger. Work in progress on POP and SMTP loggers.
Diffstat (limited to 'src')
-rw-r--r-- | src/async/imap/MCIMAPAsyncConnection.cc | 55 | ||||
-rw-r--r-- | src/async/imap/MCIMAPAsyncConnection.h | 9 | ||||
-rw-r--r-- | src/async/imap/MCIMAPAsyncSession.cc | 16 | ||||
-rw-r--r-- | src/async/imap/MCIMAPAsyncSession.h | 5 | ||||
-rw-r--r-- | src/core/basetypes/MCConnectionLogger.h | 16 | ||||
-rw-r--r-- | src/core/basetypes/MCConnectionLoggerUtils.cc | 19 | ||||
-rw-r--r-- | src/core/imap/MCIMAPSession.cc | 8 | ||||
-rw-r--r-- | src/core/pop/MCPOPSession.cc | 5 | ||||
-rw-r--r-- | src/core/smtp/MCSMTPSession.cc | 7 |
9 files changed, 110 insertions, 30 deletions
diff --git a/src/async/imap/MCIMAPAsyncConnection.cc b/src/async/imap/MCIMAPAsyncConnection.cc index 2891d439..5ea68794 100644 --- a/src/async/imap/MCIMAPAsyncConnection.cc +++ b/src/async/imap/MCIMAPAsyncConnection.cc @@ -33,6 +33,7 @@ #include "MCOperationQueueCallback.h" #include "MCIMAPDisconnectOperation.h" #include "MCIMAPAsyncSession.h" +#include "MCConnectionLogger.h" using namespace mailcore; @@ -58,6 +59,24 @@ namespace mailcore { private: IMAPAsyncConnection * mConnection; }; + + class IMAPConnectionLogger : public Object, public ConnectionLogger { + public: + IMAPConnectionLogger(IMAPAsyncConnection * connection) { + mConnection = connection; + } + + virtual ~IMAPConnectionLogger() { + } + + virtual void log(ConnectionLogType logType, Data * buffer) + { + mConnection->logConnection(logType, buffer); + } + + private: + IMAPAsyncConnection * mConnection; + }; } IMAPAsyncConnection::IMAPAsyncConnection() @@ -70,11 +89,16 @@ IMAPAsyncConnection::IMAPAsyncConnection() mQueueCallback = new IMAPOperationQueueCallback(this); mQueue->setCallback(mQueueCallback); mOwner = NULL; + mConnectionLogger = NULL; + pthread_mutex_init(&mConnectionLoggerLock, NULL); + mInternalLogger = new IMAPConnectionLogger(this); } IMAPAsyncConnection::~IMAPAsyncConnection() { cancelDelayedPerformMethod((Object::Method) &IMAPAsyncConnection::tryAutomaticDisconnectAfterDelay, NULL); + pthread_mutex_destroy(&mConnectionLoggerLock); + MC_SAFE_RELEASE(mInternalLogger); MC_SAFE_RELEASE(mQueueCallback); MC_SAFE_RELEASE(mLastFolder); MC_SAFE_RELEASE(mDefaultNamespace); @@ -526,4 +550,35 @@ IMAPAsyncSession * IMAPAsyncConnection::owner() return mOwner; } +void IMAPAsyncConnection::setConnectionLogger(ConnectionLogger * logger) +{ + pthread_mutex_lock(&mConnectionLoggerLock); + mConnectionLogger = logger; + if (mConnectionLogger != NULL) { + mSession->setConnectionLogger(mInternalLogger); + } + else { + mSession->setConnectionLogger(NULL); + } + pthread_mutex_unlock(&mConnectionLoggerLock); +} + +ConnectionLogger * IMAPAsyncConnection::connectionLogger() +{ + ConnectionLogger * result; + + pthread_mutex_lock(&mConnectionLoggerLock); + result = mConnectionLogger; + pthread_mutex_unlock(&mConnectionLoggerLock); + + return result; +} +void IMAPAsyncConnection::logConnection(ConnectionLogType logType, Data * buffer) +{ + pthread_mutex_lock(&mConnectionLoggerLock); + if (mConnectionLogger != NULL) { + mConnectionLogger->log(mConnectionLogger->context(), this, logType, buffer); + } + pthread_mutex_unlock(&mConnectionLoggerLock); +} diff --git a/src/async/imap/MCIMAPAsyncConnection.h b/src/async/imap/MCIMAPAsyncConnection.h index d6ef6a91..48d47ad9 100644 --- a/src/async/imap/MCIMAPAsyncConnection.h +++ b/src/async/imap/MCIMAPAsyncConnection.h @@ -27,6 +27,7 @@ namespace mailcore { class IMAPCapabilityOperation; class IMAPOperationQueueCallback; class IMAPAsyncSession; + class IMAPConnectionLogger; class IMAPAsyncConnection : public Object { public: @@ -66,6 +67,9 @@ namespace mailcore { virtual void setDefaultNamespace(IMAPNamespace * ns); virtual IMAPNamespace * defaultNamespace(); + virtual void setConnectionLogger(ConnectionLogger * logger); + virtual ConnectionLogger * connectionLogger(); + virtual IMAPFolderInfoOperation * folderInfoOperation(String * folder); virtual IMAPFolderStatusOperation * folderStatusOperation(String * folder); @@ -120,6 +124,9 @@ namespace mailcore { String * mLastFolder; IMAPOperationQueueCallback * mQueueCallback; IMAPAsyncSession * mOwner; + ConnectionLogger * mConnectionLogger; + IMAPConnectionLogger * mInternalLogger; + pthread_mutex_t mConnectionLoggerLock; virtual void tryAutomaticDisconnectAfterDelay(void * context); @@ -138,6 +145,8 @@ namespace mailcore { virtual void setOwner(IMAPAsyncSession * owner); virtual IMAPAsyncSession * owner(); + + virtual void logConnection(ConnectionLogType logType, Data * buffer); }; } diff --git a/src/async/imap/MCIMAPAsyncSession.cc b/src/async/imap/MCIMAPAsyncSession.cc index 7c085e4a..9272a034 100644 --- a/src/async/imap/MCIMAPAsyncSession.cc +++ b/src/async/imap/MCIMAPAsyncSession.cc @@ -11,6 +11,7 @@ #include "MCIMAPAsyncConnection.h" #include "MCIMAPNamespace.h" #include "MCOperationQueueCallback.h" +#include "MCConnectionLogger.h" #define DEFAULT_MAX_CONNECTIONS 3 @@ -33,6 +34,7 @@ IMAPAsyncSession::IMAPAsyncSession() mDelimiter = 0; mDefaultNamespace = NULL; mTimeout = 30.; + mConnectionLogger = NULL; } IMAPAsyncSession::~IMAPAsyncSession() @@ -174,6 +176,7 @@ unsigned int IMAPAsyncSession::maximumConnections() IMAPAsyncConnection * IMAPAsyncSession::session() { IMAPAsyncConnection * session = new IMAPAsyncConnection(); + session->setConnectionLogger(mConnectionLogger); session->setOwner(this); session->autorelease(); @@ -429,3 +432,16 @@ IMAPCapabilityOperation * IMAPAsyncSession::capabilityOperation() return session->capabilityOperation(); } +void IMAPAsyncSession::setConnectionLogger(ConnectionLogger * logger) +{ + mConnectionLogger = logger; + for(unsigned int i = 0 ; i < mSessions->count() ; i ++) { + IMAPAsyncConnection * currentSession = (IMAPAsyncConnection *) mSessions->objectAtIndex(i); + currentSession->setConnectionLogger(logger); + } +} + +ConnectionLogger * IMAPAsyncSession::connectionLogger() +{ + return mConnectionLogger; +} diff --git a/src/async/imap/MCIMAPAsyncSession.h b/src/async/imap/MCIMAPAsyncSession.h index 390fdab8..eae6ea15 100644 --- a/src/async/imap/MCIMAPAsyncSession.h +++ b/src/async/imap/MCIMAPAsyncSession.h @@ -33,6 +33,7 @@ namespace mailcore { class IMAPIdentityOperation; class IMAPAsyncConnection; class IMAPCapabilityOperation; + class ConnectionLogger; class IMAPAsyncSession : public Object { public: @@ -78,6 +79,9 @@ namespace mailcore { virtual void setMaximumConnections(unsigned int maxConnections); virtual unsigned int maximumConnections(); + virtual void setConnectionLogger(ConnectionLogger * logger); + virtual ConnectionLogger * connectionLogger(); + virtual IMAPFolderInfoOperation * folderInfoOperation(String * folder); virtual IMAPFolderStatusOperation * folderStatusOperation(String * folder); @@ -141,6 +145,7 @@ namespace mailcore { time_t mTimeout; bool mAllowsFolderConcurrentAccessEnabled; unsigned int mMaximumConnections; + ConnectionLogger * mConnectionLogger; virtual IMAPAsyncConnection * sessionForFolder(String * folder, bool urgent = false); virtual IMAPAsyncConnection * session(); diff --git a/src/core/basetypes/MCConnectionLogger.h b/src/core/basetypes/MCConnectionLogger.h index 8083d12d..371e6152 100644 --- a/src/core/basetypes/MCConnectionLogger.h +++ b/src/core/basetypes/MCConnectionLogger.h @@ -9,6 +9,8 @@ #ifndef __MAILCORE_CONNECTION_LOGGER_H_ #define __MAILCORE_CONNECTION_LOGGER_H_ +#include <stdlib.h> + #ifdef __cplusplus namespace mailcore { @@ -17,20 +19,24 @@ namespace mailcore { class String; enum ConnectionLogType { - ConnectionLogTypeGeneric, + // Received data ConnectionLogTypeReceived, + // Sent data ConnectionLogTypeSent, + // Sent private data ConnectionLogTypeSentPrivate, - ConnectionLogTypeErrorGeneric, + // Parse error + ConnectionLogTypeErrorParse, + // Error while receiving data - log() is called with a NULL buffer. ConnectionLogTypeErrorReceived, + // Error while sending data - log() is called with a NULL buffer. ConnectionLogTypeErrorSent, - ConnectionLogTypeErrorSentPrivate, }; class ConnectionLogger { public: - virtual void logBuffer(ConnectionLogType logType, Data * buffer) {} - virtual void logString(ConnectionLogType logType, String * log) {} + virtual void log(void * context, void * sender, ConnectionLogType logType, Data * buffer) {} + virtual void * context() { return NULL; } }; } diff --git a/src/core/basetypes/MCConnectionLoggerUtils.cc b/src/core/basetypes/MCConnectionLoggerUtils.cc index 669c8449..234713e7 100644 --- a/src/core/basetypes/MCConnectionLoggerUtils.cc +++ b/src/core/basetypes/MCConnectionLoggerUtils.cc @@ -14,21 +14,12 @@ mailcore::ConnectionLogType mailcore::getConnectionType(int log_type) { - ConnectionLogType type = ConnectionLogTypeGeneric; + ConnectionLogType type = (ConnectionLogType) -1; bool isBuffer = false; switch (log_type) { - case MAILSTREAM_LOG_TYPE_INFO_GENERIC: - type = ConnectionLogTypeGeneric; - break; - case MAILSTREAM_LOG_TYPE_INFO_RECEIVED: - type = ConnectionLogTypeReceived; - break; - case MAILSTREAM_LOG_TYPE_INFO_SENT: - type = ConnectionLogTypeSent; - break; - case MAILSTREAM_LOG_TYPE_ERROR_GENERIC: - type = ConnectionLogTypeErrorGeneric; + case MAILSTREAM_LOG_TYPE_ERROR_PARSE: + type = ConnectionLogTypeErrorParse; isBuffer = true; break; case MAILSTREAM_LOG_TYPE_ERROR_RECEIVED: @@ -60,9 +51,7 @@ bool mailcore::isBufferFromLogType(int log_type) bool isBuffer = false; switch (log_type) { - case MAILSTREAM_LOG_TYPE_ERROR_GENERIC: - case MAILSTREAM_LOG_TYPE_ERROR_RECEIVED: - case MAILSTREAM_LOG_TYPE_ERROR_SENT: + case MAILSTREAM_LOG_TYPE_ERROR_PARSE: case MAILSTREAM_LOG_TYPE_DATA_RECEIVED: case MAILSTREAM_LOG_TYPE_DATA_SENT: case MAILSTREAM_LOG_TYPE_DATA_SENT_PRIVATE: diff --git a/src/core/imap/MCIMAPSession.cc b/src/core/imap/MCIMAPSession.cc index 0b194ff7..9bd89f69 100644 --- a/src/core/imap/MCIMAPSession.cc +++ b/src/core/imap/MCIMAPSession.cc @@ -498,15 +498,17 @@ static void logger(mailimap * imap, int log_type, const char * buffer, size_t si return; ConnectionLogType type = getConnectionType(log_type); + if ((int) type == -1) + return; + bool isBuffer = isBufferFromLogType(log_type); if (isBuffer) { Data * data = Data::dataWithBytes(buffer, (unsigned int) size); - session->connectionLogger()->logBuffer(type, data); + session->connectionLogger()->log(session->connectionLogger()->context(), session, type, data); } else { - Data * data = Data::dataWithBytes(buffer, (unsigned int) size); - session->connectionLogger()->logString(type, String::stringWithData(data)); + session->connectionLogger()->log(session->connectionLogger()->context(), session, type, NULL); } } diff --git a/src/core/pop/MCPOPSession.cc b/src/core/pop/MCPOPSession.cc index fe15c500..9e771027 100644 --- a/src/core/pop/MCPOPSession.cc +++ b/src/core/pop/MCPOPSession.cc @@ -160,11 +160,10 @@ static void logger(mailpop3 * pop3, int log_type, const char * buffer, size_t si if (isBuffer) { Data * data = Data::dataWithBytes(buffer, (unsigned int) size); - session->connectionLogger()->logBuffer(type, data); + session->connectionLogger()->log(session->connectionLogger()->context(), session, type, data); } else { - Data * data = Data::dataWithBytes(buffer, (unsigned int) size); - session->connectionLogger()->logString(type, String::stringWithData(data)); + session->connectionLogger()->log(session->connectionLogger()->context(), session, type, NULL); } } diff --git a/src/core/smtp/MCSMTPSession.cc b/src/core/smtp/MCSMTPSession.cc index e74a540a..da2e19e5 100644 --- a/src/core/smtp/MCSMTPSession.cc +++ b/src/core/smtp/MCSMTPSession.cc @@ -164,7 +164,7 @@ void SMTPSession::bodyProgress(unsigned int current, unsigned int maximum) } -static void logger(mailsmtp * pop3, int log_type, const char * buffer, size_t size, void * context) +static void logger(mailsmtp * smtp, int log_type, const char * buffer, size_t size, void * context) { SMTPSession * session = (SMTPSession *) context; @@ -176,11 +176,10 @@ static void logger(mailsmtp * pop3, int log_type, const char * buffer, size_t si if (isBuffer) { Data * data = Data::dataWithBytes(buffer, (unsigned int) size); - session->connectionLogger()->logBuffer(type, data); + session->connectionLogger()->log(session->connectionLogger()->context(), session, type, data); } else { - Data * data = Data::dataWithBytes(buffer, (unsigned int) size); - session->connectionLogger()->logString(type, String::stringWithData(data)); + session->connectionLogger()->log(session->connectionLogger()->context(), session, type, NULL); } } |