aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/abstract/MCErrorMessage.cpp2
-rw-r--r--src/core/abstract/MCMessageConstants.h1
-rw-r--r--src/core/provider/MCAccountValidator.cpp425
-rw-r--r--src/core/provider/MCAccountValidator.h107
-rw-r--r--src/core/provider/MCMXRecordResolverOperation.cpp71
-rw-r--r--src/core/provider/MCMXRecordResolverOperation.h41
-rw-r--r--src/core/provider/MCProvider.h1
7 files changed, 648 insertions, 0 deletions
diff --git a/src/core/abstract/MCErrorMessage.cpp b/src/core/abstract/MCErrorMessage.cpp
index c7205d42..15de9405 100644
--- a/src/core/abstract/MCErrorMessage.cpp
+++ b/src/core/abstract/MCErrorMessage.cpp
@@ -44,6 +44,8 @@ static const char * localizedDescriptionTable[] = {
"A recipient is required to send message", /** MCOErrorNoRecipient */
"An error occured while performing a No-Op operation.", /** MCOErrorNoop */
"An application specific password is required", /** MCOErrorGmailApplicationSpecificPasswordRequired */
+ "An error when requesting date", /** MCOErrorServerDate */
+ "No valid server found", /** MCOErrorNoValidServerFound */
};
String * mailcore::errorMessageWithErrorCode(ErrorCode errorCode)
diff --git a/src/core/abstract/MCMessageConstants.h b/src/core/abstract/MCMessageConstants.h
index 1c489583..c948b7a2 100644
--- a/src/core/abstract/MCMessageConstants.h
+++ b/src/core/abstract/MCMessageConstants.h
@@ -250,6 +250,7 @@ namespace mailcore {
ErrorNoop,
ErrorGmailApplicationSpecificPasswordRequired, // 40
ErrorServerDate,
+ ErrorNoValidServerFound,
};
enum PartType {
diff --git a/src/core/provider/MCAccountValidator.cpp b/src/core/provider/MCAccountValidator.cpp
new file mode 100644
index 00000000..8a290228
--- /dev/null
+++ b/src/core/provider/MCAccountValidator.cpp
@@ -0,0 +1,425 @@
+//
+// MCAccountValidator.cpp
+// mailcore2
+//
+// Created by Christopher Hockley on 22/01/15.
+// Copyright (c) 2015 MailCore. All rights reserved.
+//
+
+#include "MCAccountValidator.h"
+#include "MCMailProvider.h"
+#include "MCMailProvidersManager.h"
+#include "MCIMAPAsyncSession.h"
+#include "MCPOPAsyncSession.h"
+#include "MCSMTPAsyncSession.h"
+#include "MCNetService.h"
+#include "MCAddress.h"
+#include "MCIMAPOperation.h"
+#include "MCPOPOperation.h"
+#include "MCSMTPOperation.h"
+#include "MCMXRecordResolverOperation.h"
+
+using namespace mailcore;
+
+/* this is the service being tested */
+
+enum {
+ SERVICE_IMAP, /* IMAP service */
+ SERVICE_POP, /* POP service */
+ SERVICE_SMTP, /* SMTP service */
+};
+
+void AccountValidator::init()
+{
+ mEmail = NULL;
+ mUsername = NULL;
+ mPassword = NULL;
+ mOAuth2Token = NULL;
+
+ mImapServices = new Array();
+ mSmtpServices = new Array();
+ mPopServices = new Array();
+
+ mIdentifier = NULL;
+ mImapServer = NULL;
+ mPopServer = NULL;
+ mSmtpServer = NULL;
+ mImapError = ErrorNone;
+ mPopError = ErrorNone;
+ mSmtpError = ErrorNone;
+
+ mCurrentServiceIndex = 0;
+ mCurrentServiceTested = 0;
+
+ mProvider = NULL;
+
+ mOperation = NULL;
+ mQueue = NULL;
+ mResolveMX = NULL;
+
+ mImapSession = NULL;
+ mPopSession = NULL;
+ mSmtpSession = NULL;
+}
+
+AccountValidator::AccountValidator()
+{
+ init();
+}
+
+AccountValidator::~AccountValidator()
+{
+ MC_SAFE_RELEASE(mEmail);
+ MC_SAFE_RELEASE(mUsername);
+ MC_SAFE_RELEASE(mPassword);
+ MC_SAFE_RELEASE(mOAuth2Token);
+ MC_SAFE_RELEASE(mImapServices);
+ MC_SAFE_RELEASE(mSmtpServices);
+ MC_SAFE_RELEASE(mPopServices);
+ MC_SAFE_RELEASE(mIdentifier);
+ MC_SAFE_RELEASE(mProvider);
+ MC_SAFE_RELEASE(mOperation);
+ MC_SAFE_RELEASE(mQueue);
+ MC_SAFE_RELEASE(mResolveMX);
+ MC_SAFE_RELEASE(mImapSession);
+ MC_SAFE_RELEASE(mPopSession);
+ MC_SAFE_RELEASE(mSmtpSession);
+}
+
+void AccountValidator::start()
+{
+ if (mEmail == NULL) {
+ if (mUsername == NULL) {
+ return;
+ }
+ else {
+ mEmail = mUsername;
+ }
+ }
+ else if (mUsername == NULL){
+ mUsername = mEmail;
+ }
+
+ mProvider = MailProvidersManager::sharedManager()->providerForEmail(mUsername);
+
+ if (mProvider == NULL) {
+ resolveMX();
+ }
+ else{
+ startCheckingHosts();
+ }
+}
+
+void AccountValidator::cancel()
+{
+ if(mOperation != NULL)
+ mOperation->cancel();
+
+ if(mResolveMX != NULL)
+ mResolveMX->cancel();
+
+ if (mQueue != NULL)
+ mQueue->cancelAllOperations();
+
+ MC_SAFE_RELEASE(mOperation);
+ MC_SAFE_RELEASE(mResolveMX);
+ MC_SAFE_RELEASE(mQueue);
+ MC_SAFE_RELEASE(mImapSession);
+ MC_SAFE_RELEASE(mPopSession);
+ MC_SAFE_RELEASE(mSmtpSession);
+
+ Operation::cancel();
+}
+
+void AccountValidator::operationFinished(Operation * op)
+{
+ if (op == mResolveMX) {
+ resolveMXDone();
+ }
+ else {
+ checkNextHostDone();
+ }
+}
+
+void AccountValidator::resolveMX()
+{
+ Array * components;
+ String * domain;
+
+ components = mUsername->componentsSeparatedByString(MCSTR("@"));
+ if (components->count() >= 2) {
+ domain = (String *) components->lastObject();
+ mResolveMX = new MXRecordResolverOperation();
+ mResolveMX->setHostname(domain);
+ mResolveMX->setCallback((OperationCallback *)this);
+
+ mQueue = new OperationQueue();
+ mQueue->addOperation(mResolveMX);
+ }
+ else {
+ mImapError = ErrorNoValidServerFound;
+ mPopError = ErrorNoValidServerFound;
+ mSmtpError = ErrorNoValidServerFound;
+
+ callback()->operationFinished(this);
+ }
+}
+
+void AccountValidator::resolveMXDone()
+{
+ Array * mxRecords = mResolveMX->mxRecords();
+
+ mc_foreacharray(String, mxRecord, mxRecords) {
+ MailProvider *provider = MailProvidersManager::sharedManager()->providerForMX(mxRecord);
+ if (provider){
+ mProvider = provider;
+ break;
+ }
+ }
+
+ startCheckingHosts();
+}
+
+void AccountValidator::startCheckingHosts()
+{
+ if (mProvider != NULL) {
+ mIdentifier = mProvider->identifier();
+
+ if (mImapServices->count() == 0 and mProvider->imapServices()->count() > 0)
+ mImapServices = mProvider->imapServices();
+
+ if (mPopServices->count() == 0 and mProvider->popServices()->count() > 0)
+ mPopServices = mProvider->popServices();
+
+ if (mSmtpServices->count() == 0 and mProvider->smtpServices()->count() > 0)
+ mSmtpServices = mProvider->smtpServices();
+ }
+
+ if (mImapServices->count() == 0)
+ mImapError = ErrorNoValidServerFound;
+
+ if (mPopServices->count() == 0)
+ mPopError = ErrorNoValidServerFound;
+
+ if (mSmtpServices->count() == 0)
+ mSmtpError = ErrorNoValidServerFound;
+
+ checkNextHost();
+}
+
+/**
+ Each service(IMAP/POP/SMTP) is tested one after the other.
+ For each service we test each server details (NetService),
+ Until either:
+ we find on that works and returns ErrorNone in checkNextHostDone().
+ we have gone trough the Array of NetService for that service and checkNextHost() is then called for the next service.
+ */
+void AccountValidator::checkNextHost()
+{
+ if (mCurrentServiceTested == SERVICE_IMAP) {
+ if (mCurrentServiceIndex < mImapServices->count()) {
+ mImapSession = new IMAPAsyncSession();
+ mImapSession->setUsername(mUsername);
+ mImapSession->setPassword(mPassword);
+
+ mImapServer = (NetService *) mImapServices->objectAtIndex(mCurrentServiceIndex);
+ mImapSession->setHostname(mImapServer->hostname());
+ mImapSession->setPort(mImapServer->port());
+ mImapSession->setConnectionType(mImapServer->connectionType());
+
+ mOperation = (IMAPOperation *)mImapSession->checkAccountOperation();
+ mOperation->retain();
+ mOperation->setCallback(this);
+ mOperation->start();
+ }
+ else {
+ mCurrentServiceTested ++;
+ mCurrentServiceIndex = 0;
+ checkNextHost();
+ }
+ }
+ else if (mCurrentServiceTested == SERVICE_POP){
+ if (mCurrentServiceIndex < mPopServices->count()) {
+ mPopSession = new POPAsyncSession();
+ mPopSession->setUsername(mUsername);
+ mPopSession->setPassword(mPassword);
+
+ mPopServer = (NetService *) mPopServices->objectAtIndex(mCurrentServiceIndex);
+ mPopSession->setHostname(mPopServer->hostname());
+ mPopSession->setPort(mPopServer->port());
+ mPopSession->setConnectionType(mPopServer->connectionType());
+
+ mOperation = mPopSession->checkAccountOperation();
+ mOperation->retain();
+ mOperation->setCallback(this);
+ mOperation->start();
+ }
+ else {
+ mCurrentServiceTested ++;
+ mCurrentServiceIndex = 0;
+ checkNextHost();
+ }
+ }
+ else if (mCurrentServiceTested == SERVICE_SMTP){
+ if (mCurrentServiceIndex < mSmtpServices->count()) {
+ mSmtpSession = new SMTPAsyncSession();
+ mSmtpSession->setUsername(mUsername);
+ mSmtpSession->setPassword(mPassword);
+
+ mSmtpServer = (NetService *) mSmtpServices->objectAtIndex(mCurrentServiceIndex);
+ mSmtpSession->setHostname(mSmtpServer->hostname());
+ mSmtpSession->setPort(mSmtpServer->port());
+ mSmtpSession->setConnectionType(mSmtpServer->connectionType());
+
+ mOperation = mSmtpSession->checkAccountOperation(Address::addressWithMailbox(mEmail));
+ mOperation->retain();
+ mOperation->setCallback(this);
+ mOperation->start();
+ }
+ else {
+ mCurrentServiceTested ++;
+ mCurrentServiceIndex = 0;
+ checkNextHost();
+ }
+ }
+ else {
+ callback()->operationFinished(this);
+ }
+}
+
+void AccountValidator::checkNextHostDone()
+{
+ ErrorCode error = ErrorNone;
+
+ if (mCurrentServiceTested == SERVICE_IMAP) {
+ mImapError = ((IMAPOperation *)mOperation)->error();
+ error = mImapError;
+ MC_SAFE_RELEASE(mImapSession);
+ }
+ else if (mCurrentServiceTested == SERVICE_POP) {
+ mPopError = ((POPOperation *)mOperation)->error();
+ error = mPopError;
+ MC_SAFE_RELEASE(mPopSession);
+ }
+ else if (mCurrentServiceTested == SERVICE_SMTP) {
+ mSmtpError = ((SMTPOperation *)mOperation)->error();
+ error = mSmtpError;
+ MC_SAFE_RELEASE(mSmtpSession);
+ }
+
+ MC_SAFE_RELEASE(mOperation);
+
+ if (error == ErrorNone) {
+ mCurrentServiceTested ++;
+ }
+ else {
+ mCurrentServiceIndex ++;
+ }
+
+ checkNextHost();
+}
+
+void AccountValidator::setEmail(String * email)
+{
+ MC_SAFE_REPLACE_COPY(String, mEmail, email);
+}
+
+String * AccountValidator::email()
+{
+ return mEmail;
+}
+
+void AccountValidator::setUsername(String * username)
+{
+ MC_SAFE_REPLACE_COPY(String, mUsername, username);
+}
+
+String * AccountValidator::username()
+{
+ return mUsername;
+}
+
+void AccountValidator::setPassword(String * password)
+{
+ MC_SAFE_REPLACE_COPY(String, mPassword, password);
+}
+
+String * AccountValidator::password()
+{
+ return mPassword;
+}
+
+void AccountValidator::setOAuth2Token(String * OAuth2Token)
+{
+ MC_SAFE_REPLACE_COPY(String, mOAuth2Token, OAuth2Token);
+}
+
+String * AccountValidator::OAuth2Token()
+{
+ return mOAuth2Token;
+}
+
+void AccountValidator::setImapServices(Array * imapServices)
+{
+ MC_SAFE_REPLACE_COPY(Array, mImapServices, imapServices);
+}
+
+Array * AccountValidator::imapServices()
+{
+ return mImapServices;
+}
+
+void AccountValidator::setSmtpServices(Array * smtpServices)
+{
+ MC_SAFE_REPLACE_COPY(Array, mSmtpServices, smtpServices);
+}
+
+Array * AccountValidator::smtpServices()
+{
+ return mSmtpServices;
+}
+
+void AccountValidator::setPopServices(Array * popServices)
+{
+ MC_SAFE_REPLACE_COPY(Array, mPopServices, popServices);
+}
+
+Array * AccountValidator::popServices()
+{
+ return mPopServices;
+}
+
+String * AccountValidator::identifier()
+{
+ return mIdentifier;
+}
+
+NetService * AccountValidator::imapServer()
+{
+ return mImapServer;
+}
+
+NetService * AccountValidator::smtpServer()
+{
+ return mSmtpServer;
+}
+
+NetService * AccountValidator::popServer()
+{
+ return mPopServer;
+}
+
+ErrorCode AccountValidator::imapError()
+{
+ return mImapError;
+}
+
+ErrorCode AccountValidator::popError()
+{
+ return mPopError;
+}
+
+ErrorCode AccountValidator::smtpError()
+{
+ return mSmtpError;
+}
diff --git a/src/core/provider/MCAccountValidator.h b/src/core/provider/MCAccountValidator.h
new file mode 100644
index 00000000..74447ac3
--- /dev/null
+++ b/src/core/provider/MCAccountValidator.h
@@ -0,0 +1,107 @@
+//
+// MCAccountValidator.h
+// mailcore2
+//
+// Created by Christopher Hockley on 22/01/15.
+// Copyright (c) 2015 MailCore. All rights reserved.
+//
+
+#ifndef MAILCORE_MCACCOUNTVALIDATOR_H
+
+#define MAILCORE_MCACCOUNTVALIDATOR_H
+
+#include <MailCore/MCBaseTypes.h>
+
+#ifdef __cplusplus
+
+namespace mailcore {
+
+ class NetService;
+ class MailProvider;
+ class MXRecordResolverOperation;
+ class IMAPAsyncSession;
+ class POPAsyncSession;
+ class SMTPAsyncSession;
+
+ class MAILCORE_EXPORT AccountValidator : public Operation, public OperationCallback {
+ public:
+ AccountValidator();
+ virtual ~AccountValidator();
+
+ virtual void setEmail(String * email);
+ virtual String * email(); /* for SMTP */
+ virtual void setUsername(String * username);
+ virtual String * username();
+ virtual void setPassword(String * password);
+ virtual String * password();
+ virtual void setOAuth2Token(String * OAuth2Token);
+ virtual String * OAuth2Token();
+
+ virtual void setImapServices(Array * imapServices);
+ virtual Array * /* NetService */ imapServices();
+ virtual void setSmtpServices(Array * smtpServices);
+ virtual Array * /* NetService */ smtpServices();
+ virtual void setPopServices(Array * popServices);
+ virtual Array * /* NetService */ popServices();
+
+ // result
+ virtual String * identifier();
+ virtual NetService * imapServer();
+ virtual NetService * popServer();
+ virtual NetService * smtpServer();
+ virtual ErrorCode imapError();
+ virtual ErrorCode popError();
+ virtual ErrorCode smtpError();
+
+ virtual void start();
+ virtual void cancel();
+
+ private:
+ String * mEmail; /* for SMTP */
+ String * mUsername;
+ String * mPassword;
+ String * mOAuth2Token;
+
+ Array * /* NetService */ mImapServices;
+ Array * /* NetService */ mSmtpServices;
+ Array * /* NetService */ mPopServices;
+
+ // result
+ String * mIdentifier;
+ NetService * mImapServer;
+ NetService * mPopServer;
+ NetService * mSmtpServer;
+ ErrorCode mImapError;
+ ErrorCode mPopError;
+ ErrorCode mSmtpError;
+
+ MailProvider * mProvider;
+
+ void resolveMX();
+ void resolveMXDone();
+
+ void startCheckingHosts();
+ void checkNextHost();
+ void checkNextHostDone();
+
+ //indexs for services being tested
+ int mCurrentServiceIndex;
+ int mCurrentServiceTested;
+
+ void init();
+
+ Operation * mOperation;
+ virtual void operationFinished(Operation * op);
+
+ OperationQueue * mQueue;
+ MXRecordResolverOperation * mResolveMX;
+
+ IMAPAsyncSession * mImapSession;
+ POPAsyncSession * mPopSession;
+ SMTPAsyncSession * mSmtpSession;
+ };
+}
+
+#endif
+
+#endif
diff --git a/src/core/provider/MCMXRecordResolverOperation.cpp b/src/core/provider/MCMXRecordResolverOperation.cpp
new file mode 100644
index 00000000..aa2d5448
--- /dev/null
+++ b/src/core/provider/MCMXRecordResolverOperation.cpp
@@ -0,0 +1,71 @@
+//
+// MCFetchAsyncMXRecord.cpp
+// mailcore2
+//
+// Created by Christopher Hockley on 29/01/15.
+// Copyright (c) 2015 MailCore. All rights reserved.
+//
+
+#include "MCMXRecordResolverOperation.h"
+
+#include <arpa/inet.h>
+#include <resolv.h>
+
+using namespace mailcore;
+
+MXRecordResolverOperation::MXRecordResolverOperation()
+{
+ mHostname = NULL;
+ mMXRecords = NULL;
+}
+
+MXRecordResolverOperation::~MXRecordResolverOperation()
+{
+ MC_SAFE_RELEASE(mMXRecords);
+ MC_SAFE_RELEASE(mHostname);
+}
+
+void MXRecordResolverOperation::setHostname(String * hostname)
+{
+ MC_SAFE_REPLACE_COPY(String, mHostname, hostname);
+}
+
+String * MXRecordResolverOperation::hostname()
+{
+ return mHostname;
+}
+
+Array * MXRecordResolverOperation::mxRecords()
+{
+ return mMXRecords;
+}
+
+void MXRecordResolverOperation::main()
+{
+ mMXRecords = new Array();
+ unsigned char response[NS_PACKETSZ];
+ ns_msg handle;
+ ns_rr rr;
+ int len;
+ char dispbuf[4096];
+
+ if (((len = res_search(MCUTF8(mHostname), ns_c_in, ns_t_mx, response, sizeof(response))) >= 0) and
+ (ns_initparse(response, len, &handle) >= 0) and
+ (ns_msg_count(handle, ns_s_an) >= 0)) {
+
+ for (int ns_index = 0; ns_index < len; ns_index++) {
+ if (ns_parserr(&handle, ns_s_an, ns_index, &rr)) {
+ /* WARN: ns_parserr failed */
+ continue;
+ }
+ ns_sprintrr (&handle, &rr, NULL, NULL, dispbuf, sizeof (dispbuf));
+ if (ns_rr_class(rr) == ns_c_in and ns_rr_type(rr) == ns_t_mx) {
+ char mxname[4096];
+ dn_expand(ns_msg_base(handle), ns_msg_base(handle) + ns_msg_size(handle), ns_rr_rdata(rr) + NS_INT16SZ, mxname, sizeof(mxname));
+ String * str = String::stringWithUTF8Characters(mxname);
+ mMXRecords->addObject(str);
+ }
+ }
+ }
+}
+
diff --git a/src/core/provider/MCMXRecordResolverOperation.h b/src/core/provider/MCMXRecordResolverOperation.h
new file mode 100644
index 00000000..7d8a6fc8
--- /dev/null
+++ b/src/core/provider/MCMXRecordResolverOperation.h
@@ -0,0 +1,41 @@
+//
+// ResolveProviderUsingMXRecordAsync.h
+// mailcore2
+//
+// Created by Christopher Hockley on 29/01/15.
+// Copyright (c) 2015 MailCore. All rights reserved.
+//
+
+#ifndef MAILCORE_MCFETCHASYNCMXRECORD_H
+
+#define MAILCORE_MCFETCHASYNCMXRECORD_H
+
+#include <MailCore/MCBaseTypes.h>
+#include <MailCore/MCMessageConstants.h>
+
+#ifdef __cplusplus
+
+namespace mailcore {
+
+ class MAILCORE_EXPORT MXRecordResolverOperation : public Operation {
+ public:
+ MXRecordResolverOperation();
+ virtual ~MXRecordResolverOperation();
+
+ virtual void setHostname(String * hostname);
+ virtual String * hostname();
+
+ virtual Array * mxRecords();
+
+ public: // subclass behavior
+ virtual void main();
+
+ private:
+ Array * mMXRecords;
+ String * mHostname;
+ };
+}
+
+#endif
+
+#endif
diff --git a/src/core/provider/MCProvider.h b/src/core/provider/MCProvider.h
index b13bb263..ad8189b2 100644
--- a/src/core/provider/MCProvider.h
+++ b/src/core/provider/MCProvider.h
@@ -13,5 +13,6 @@
#include <MailCore/MCMailProvidersManager.h>
#include <MailCore/MCMailProvider.h>
#include <MailCore/MCNetService.h>
+#include <MailCore/MCAccountValidator.h>
#endif