aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/provider/MCAccountValidator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/provider/MCAccountValidator.cpp')
-rw-r--r--src/core/provider/MCAccountValidator.cpp245
1 files changed, 127 insertions, 118 deletions
diff --git a/src/core/provider/MCAccountValidator.cpp b/src/core/provider/MCAccountValidator.cpp
index 9b6e234b..84385e09 100644
--- a/src/core/provider/MCAccountValidator.cpp
+++ b/src/core/provider/MCAccountValidator.cpp
@@ -17,8 +17,7 @@
#include "MCIMAPOperation.h"
#include "MCPOPOperation.h"
#include "MCSMTPOperation.h"
-#include "MCResolveProviderUsingMXRecord.h"
-#include "MCOperationQueueCallback.h"
+#include "MCMXRecordResolverOperation.h"
using namespace mailcore;
@@ -30,35 +29,6 @@ enum {
SERVICE_SMTP, /* SMTP service */
};
-namespace mailcore {
-
- class ValidatorOperationQueueCallback : public Object, public OperationQueueCallback {
- public:
- ValidatorOperationQueueCallback(AccountValidator * validator) {
- mValidator = validator;
- }
-
- virtual ~ValidatorOperationQueueCallback() {
- }
-
- virtual void queueStartRunning() {
- if (mValidator->operationQueueCallback() != NULL) {
- mValidator->operationQueueCallback()->queueStartRunning();
- }
- }
-
- virtual void queueStoppedRunning() {
- if (mValidator->operationQueueCallback() != NULL) {
- mValidator->operationQueueCallback()->queueStoppedRunning();
- }
-
- mValidator->setup();
- }
- private:
- AccountValidator * mValidator;
- };
-}
-
void AccountValidator::init()
{
mEmail = NULL;
@@ -83,10 +53,13 @@ void AccountValidator::init()
mProvider = NULL;
- mQueue = new OperationQueue();
- mQueueCallback = new ValidatorOperationQueueCallback(this);
- mQueue->setCallback(mQueueCallback);
- mOperationQueueCallback = NULL;
+ mOperation = NULL;
+ mQueue = NULL;
+ mResolveMX = NULL;
+
+ mImapSession = NULL;
+ mPopSession = NULL;
+ mSmtpSession = NULL;
}
AccountValidator::AccountValidator()
@@ -105,6 +78,12 @@ AccountValidator::~AccountValidator()
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()
@@ -124,39 +103,82 @@ void AccountValidator::start()
mProvider = MailProvidersManager::sharedManager()->providerForEmail(mUsername);
if (mProvider == NULL) {
- Array * components;
- String * domain;
-
- components = mUsername->componentsSeparatedByString(MCSTR("@"));
- if (components->count() >= 2) {
- domain = (String *) components->lastObject();
- mResolveMX = new ResolveProviderUsingMXRecord();
- mResolveMX->setHostname(domain);
- mQueue->addOperation(mResolveMX);
- }
+ resolveMX();
}
else{
- setup();
+ startCheckingHosts();
}
}
void AccountValidator::cancel()
{
- mOperation->cancel();
+ if(mOperation != NULL)
+ mOperation->cancel();
+
+ if(mResolveMX != NULL)
+ mResolveMX->cancel();
+
+ if (mQueue != NULL)
+ mQueue->cancelAllOperations();
+
+ MC_SAFE_RELEASE(mImapSession);
+ MC_SAFE_RELEASE(mPopSession);
+ MC_SAFE_RELEASE(mSmtpSession);
+
Operation::cancel();
}
void AccountValidator::operationFinished(Operation * op)
{
- opCompleted();
+ 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);
+ }
}
-void AccountValidator::setup()
+void AccountValidator::resolveMXDone()
{
- if (mResolveMX != NULL) {
- mProvider = mResolveMX->provider();
+ Array * mxRecords = mResolveMX->mxRecords();
+
+ CFURLRef imageURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("providers"), CFSTR("json"), NULL);
+ CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle);
+ const char *path = CFStringGetCStringPtr(imagePath, CFStringGetSystemEncoding());
+ String * sPath = String::stringWithUTF8Characters(path);
+
+ MailProvidersManager::sharedManager()->registerProvidersWithFilename(sPath);
+
+ 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();
@@ -179,65 +201,41 @@ void AccountValidator::setup()
if (mSmtpServices->count() == 0)
mSmtpError = ErrorNoValidServerFound;
- test();
-}
-
-void AccountValidator::opCompleted()
-{
- ErrorCode error = ErrorNone;
-
- if (mCurrentServiceTested == SERVICE_IMAP) {
- mImapError = ((IMAPOperation *)mOperation)->error();
- error = mImapError;
- }
- else if (mCurrentServiceTested == SERVICE_POP) {
- mPopError = ((POPOperation *)mOperation)->error();
- error = mPopError;
- }
- else if (mCurrentServiceTested == SERVICE_SMTP) {
- mSmtpError = ((SMTPOperation *)mOperation)->error();
- error = mSmtpError;
- }
-
- if (error == ErrorNone) {
- mCurrentServiceTested ++;
- }
- else {
- mCurrentServiceIndex ++;
- }
-
- test();
+ 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 opCompleted().
- we have gone trough the Array of NetService for that service and test() is recalled for the next service.
+ 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::test()
+void AccountValidator::checkNextHost()
{
if (mCurrentServiceTested == SERVICE_IMAP) {
if (mCurrentServiceIndex < mImapServices->count()) {
- IMAPAsyncSession *imapSession = new IMAPAsyncSession();
- imapSession->setUsername(mUsername);
- imapSession->setPassword(mPassword);
-
+
+ if (mImapSession == NULL) {
+ mImapSession = new IMAPAsyncSession();
+ mImapSession->setUsername(mUsername);
+ mImapSession->setPassword(mPassword);
+ }
+
mImapServer = (NetService *) mImapServices->objectAtIndex(mCurrentServiceIndex);
- imapSession->setHostname(mImapServer->hostname());
- imapSession->setPort(mImapServer->port());
- imapSession->setConnectionType(mImapServer->connectionType());
+ mImapSession->setHostname(mImapServer->hostname());
+ mImapSession->setPort(mImapServer->port());
+ mImapSession->setConnectionType(mImapServer->connectionType());
- mOperation = (IMAPOperation *)imapSession->checkAccountOperation();
- mOperation->setCallback(this);
+ mOperation = (IMAPOperation *)mImapSession->checkAccountOperation();
+ mOperation->setCallback((OperationCallback *)this);
mOperation->start();
}
else {
- mCurrentServiceTested = SERVICE_POP;
+ mCurrentServiceTested ++;
mCurrentServiceIndex = 0;
- test();
+ checkNextHost();
}
}
else if (mCurrentServiceTested == SERVICE_POP){
@@ -252,13 +250,13 @@ void AccountValidator::test()
popSession->setConnectionType(mPopServer->connectionType());
mOperation = (POPOperation *)popSession->checkAccountOperation();
- mOperation->setCallback(this);
+ mOperation->setCallback((OperationCallback *)this);
mOperation->start();
}
else {
- mCurrentServiceTested = SERVICE_SMTP;
+ mCurrentServiceTested ++;
mCurrentServiceIndex = 0;
- test();
+ checkNextHost();
}
}
else if (mCurrentServiceTested == SERVICE_SMTP){
@@ -273,19 +271,50 @@ void AccountValidator::test()
smtpSession->setConnectionType(mSmtpServer->connectionType());
mOperation = (SMTPOperation *)smtpSession->checkAccountOperation(Address::addressWithMailbox(mEmail));
- mOperation->setCallback(this);
+ mOperation->setCallback((OperationCallback *)this);
mOperation->start();
}
else {
- callback()->operationFinished(this);
+ mCurrentServiceTested ++;
+ mCurrentServiceIndex = 0;
+ checkNextHost();
}
}
else {
+ MC_SAFE_RELEASE(mPopSession);
+ MC_SAFE_RELEASE(mSmtpSession);
callback()->operationFinished(this);
}
}
+void AccountValidator::checkNextHostDone()
+{
+ ErrorCode error = ErrorNone;
+
+ if (mCurrentServiceTested == SERVICE_IMAP) {
+ mImapError = ((IMAPOperation *)mOperation)->error();
+ error = mImapError;
+ }
+ else if (mCurrentServiceTested == SERVICE_POP) {
+ mPopError = ((POPOperation *)mOperation)->error();
+ error = mPopError;
+ }
+ else if (mCurrentServiceTested == SERVICE_SMTP) {
+ mSmtpError = ((SMTPOperation *)mOperation)->error();
+ error = mSmtpError;
+ }
+
+ if (error == ErrorNone) {
+ mCurrentServiceTested ++;
+ }
+ else {
+ mCurrentServiceIndex ++;
+ }
+
+ checkNextHost();
+}
+
void AccountValidator::setEmail(String * email)
{
MC_SAFE_REPLACE_COPY(String, mEmail, email);
@@ -390,23 +419,3 @@ ErrorCode AccountValidator::smtpError()
{
return mSmtpError;
}
-
-void POPAsyncSession::setOperationQueueCallback(OperationQueueCallback * callback)
-{
- mOperationQueueCallback = callback;
-}
-
-OperationQueueCallback * POPAsyncSession::operationQueueCallback()
-{
- return mOperationQueueCallback;
-}
-
-bool POPAsyncSession::isOperationQueueRunning()
-{
- return mQueue->count() > 0;
-}
-
-void POPAsyncSession::cancelAllOperations()
-{
- mQueue->cancelAllOperations();
-}