From e4374684684264f4264e85c4b12b02e4bcdcdb34 Mon Sep 17 00:00:00 2001 From: Christopher Hockley Date: Mon, 26 Jan 2015 11:03:06 +0100 Subject: AccountValidator Added AccountValidator to used to validate an email provider and it's associated metadata and credentials. --- build-mac/mailcore2.xcodeproj/project.pbxproj | 24 ++ src/core/provider/MCAccountValidator.cpp | 384 ++++++++++++++++++++++++++ src/core/provider/MCAccountValidator.h | 96 +++++++ src/core/provider/MCProvider.h | 1 + src/objc/provider/MCOAccountValidator.h | 50 ++++ src/objc/provider/MCOAccountValidator.mm | 154 +++++++++++ src/objc/provider/MCOProvider.h | 2 +- 7 files changed, 710 insertions(+), 1 deletion(-) create mode 100644 src/core/provider/MCAccountValidator.cpp create mode 100644 src/core/provider/MCAccountValidator.h create mode 100644 src/objc/provider/MCOAccountValidator.h create mode 100644 src/objc/provider/MCOAccountValidator.mm diff --git a/build-mac/mailcore2.xcodeproj/project.pbxproj b/build-mac/mailcore2.xcodeproj/project.pbxproj index 7e5b36b9..0657c2d7 100755 --- a/build-mac/mailcore2.xcodeproj/project.pbxproj +++ b/build-mac/mailcore2.xcodeproj/project.pbxproj @@ -7,6 +7,14 @@ objects = { /* Begin PBXBuildFile section */ + 27478E801A764698004AE621 /* MCAccountValidator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27478E7E1A764698004AE621 /* MCAccountValidator.cpp */; }; + 27478E811A764699004AE621 /* MCAccountValidator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27478E7E1A764698004AE621 /* MCAccountValidator.cpp */; }; + 27478E841A7646F8004AE621 /* MCOAccountValidator.mm in Sources */ = {isa = PBXBuildFile; fileRef = 27478E831A7646F8004AE621 /* MCOAccountValidator.mm */; }; + 27478E851A7646F8004AE621 /* MCOAccountValidator.mm in Sources */ = {isa = PBXBuildFile; fileRef = 27478E831A7646F8004AE621 /* MCOAccountValidator.mm */; }; + 27478E861A76475F004AE621 /* MCOAccountValidator.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 27478E821A7646F8004AE621 /* MCOAccountValidator.h */; }; + 27478E871A76475F004AE621 /* MCAccountValidator.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 27478E7F1A764698004AE621 /* MCAccountValidator.h */; }; + 27478E881A7647AC004AE621 /* MCOAccountValidator.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 27478E821A7646F8004AE621 /* MCOAccountValidator.h */; }; + 27478E891A7647AC004AE621 /* MCAccountValidator.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 27478E7F1A764698004AE621 /* MCAccountValidator.h */; }; 27780C2E19CF9CD100C77E44 /* MCOFramework.mm in Sources */ = {isa = PBXBuildFile; fileRef = C6BD28A9170BDB6B00A91AC1 /* MCOFramework.mm */; }; 27780C3219CF9CD100C77E44 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C6ED6D1917A1919500A4A14C /* Security.framework */; }; 27780C3619CF9CD100C77E44 /* providers.json in Resources */ = {isa = PBXBuildFile; fileRef = 84AF9E7D172DBAF600E60AA3 /* providers.json */; }; @@ -992,6 +1000,8 @@ dstPath = include/MailCore; dstSubfolderSpec = 16; files = ( + 27478E861A76475F004AE621 /* MCOAccountValidator.h in CopyFiles */, + 27478E871A76475F004AE621 /* MCAccountValidator.h in CopyFiles */, C673EBF01A46B45300A53F7F /* MCIMAPFolderInfo.h in CopyFiles */, C6181D3319FE0F8D0088FBBC /* MCIMAPFetchParsedContentOperation.h in CopyFiles */, C6181D3219FE0F8A0088FBBC /* MCOIMAPFetchParsedContentOperation.h in CopyFiles */, @@ -1221,6 +1231,8 @@ dstPath = include/MailCore; dstSubfolderSpec = 16; files = ( + 27478E881A7647AC004AE621 /* MCOAccountValidator.h in CopyFiles */, + 27478E891A7647AC004AE621 /* MCAccountValidator.h in CopyFiles */, C673EBEF1A46B44E00A53F7F /* MCIMAPFolderInfo.h in CopyFiles */, 8199FBF719FAF3AF0040BBC3 /* MCOIMAPFetchParsedContentOperation.h in CopyFiles */, 8199FBF819FAF3AF0040BBC3 /* MCIMAPFetchParsedContentOperation.h in CopyFiles */, @@ -1438,6 +1450,10 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 27478E7E1A764698004AE621 /* MCAccountValidator.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MCAccountValidator.cpp; sourceTree = ""; }; + 27478E7F1A764698004AE621 /* MCAccountValidator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MCAccountValidator.h; sourceTree = ""; }; + 27478E821A7646F8004AE621 /* MCOAccountValidator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MCOAccountValidator.h; sourceTree = ""; }; + 27478E831A7646F8004AE621 /* MCOAccountValidator.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MCOAccountValidator.mm; sourceTree = ""; }; 27780C3A19CF9CD100C77E44 /* MailCore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MailCore.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 27780C3C19CF9D9800C77E44 /* CFNetwork.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CFNetwork.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.0.sdk/System/Library/Frameworks/CFNetwork.framework; sourceTree = DEVELOPER_DIR; }; 27780D3419CFA19500C77E44 /* libstdc++.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = "libstdc++.dylib"; path = "Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.0.sdk/usr/lib/libstdc++.dylib"; sourceTree = DEVELOPER_DIR; }; @@ -2143,6 +2159,8 @@ 84AF9E72172DACC900E60AA3 /* provider */ = { isa = PBXGroup; children = ( + 27478E7E1A764698004AE621 /* MCAccountValidator.cpp */, + 27478E7F1A764698004AE621 /* MCAccountValidator.h */, C6CF62BF175324F0006398B9 /* MCMailProvider.cpp */, C6CF62C0175324F0006398B9 /* MCMailProvider.h */, C6CF62C1175324F0006398B9 /* MCMailProvidersManager.cpp */, @@ -2157,6 +2175,8 @@ 84AF9E84172DBC1000E60AA3 /* provider */ = { isa = PBXGroup; children = ( + 27478E821A7646F8004AE621 /* MCOAccountValidator.h */, + 27478E831A7646F8004AE621 /* MCOAccountValidator.mm */, C6CF62B2175324CE006398B9 /* MCOMailProvider.h */, C6CF62B3175324CE006398B9 /* MCOMailProvider.mm */, C6CF62B4175324CE006398B9 /* MCOMailProvidersManager.h */, @@ -3436,6 +3456,7 @@ C68B2AF717797389005E61EF /* MCConnectionLoggerUtils.cpp in Sources */, C64EA737169E847800778456 /* MCPOPMessageInfo.cpp in Sources */, C64EA73A169E847800778456 /* MCPOPSession.cpp in Sources */, + 27478E801A764698004AE621 /* MCAccountValidator.cpp in Sources */, 84D7375C199BFDCA005124E5 /* MCNNTPListNewsgroupsOperation.cpp in Sources */, C64EA73C169E847800778456 /* MCAttachment.cpp in Sources */, C64EA73E169E847800778456 /* MCMessageBuilder.cpp in Sources */, @@ -3534,6 +3555,7 @@ C64BB23F16EDAAE1000DB34C /* MCOAbstractPart.mm in Sources */, C64BB24216EDAAF4000DB34C /* MCOAddress.mm in Sources */, C64BB24516EDAAFE000DB34C /* MCOMessageHeader.mm in Sources */, + 27478E841A7646F8004AE621 /* MCOAccountValidator.mm in Sources */, C64BB25216FC284A000DB34C /* NSValue+MCO.mm in Sources */, C64BB25A16FD3BCB000DB34C /* MCOOperation.mm in Sources */, C64BB25E16FD4377000DB34C /* MCOAttachment.mm in Sources */, @@ -3704,6 +3726,7 @@ C6BA2BB61705F4E6003F0E9E /* MCIMAPSession.cpp in Sources */, C68B2AF817797389005E61EF /* MCConnectionLoggerUtils.cpp in Sources */, C6BA2BB71705F4E6003F0E9E /* MCPOPMessageInfo.cpp in Sources */, + 27478E811A764699004AE621 /* MCAccountValidator.cpp in Sources */, C6BA2BB81705F4E6003F0E9E /* MCPOPSession.cpp in Sources */, 84D7375D199BFDCA005124E5 /* MCNNTPListNewsgroupsOperation.cpp in Sources */, C6BA2BB91705F4E6003F0E9E /* MCAttachment.cpp in Sources */, @@ -3802,6 +3825,7 @@ C6BA2BF21705F4E6003F0E9E /* MCOAbstractMultipart.mm in Sources */, C6BA2BF31705F4E6003F0E9E /* MCOAbstractPart.mm in Sources */, C6BA2BF41705F4E6003F0E9E /* MCOAddress.mm in Sources */, + 27478E851A7646F8004AE621 /* MCOAccountValidator.mm in Sources */, C6BA2BF51705F4E6003F0E9E /* MCOMessageHeader.mm in Sources */, C6BA2BF61705F4E6003F0E9E /* NSValue+MCO.mm in Sources */, C6BA2BF71705F4E6003F0E9E /* MCOOperation.mm in Sources */, diff --git a/src/core/provider/MCAccountValidator.cpp b/src/core/provider/MCAccountValidator.cpp new file mode 100644 index 00000000..efbbfab0 --- /dev/null +++ b/src/core/provider/MCAccountValidator.cpp @@ -0,0 +1,384 @@ +// +// 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 +#include + +using namespace mailcore; + +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; +} + +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); +} + +void AccountValidator::start() +{ + if (setup()) test(); + else mCallback->operationFinished(this); +} + +void AccountValidator::operationFinished(Operation * op) +{ + opCompleted(); +} + +bool AccountValidator::setup() +{ + if (mEmail == NULL) { + if (mUsername == NULL) return false; + else mEmail = mUsername; + }else if (mUsername == NULL){ + mUsername = mEmail; + } + + MailProvider *provider = MailProvidersManager::sharedManager()->providerForEmail(mUsername); + + if (provider == NULL) { + Array * components; + String * domain; + + components = mUsername->componentsSeparatedByString(MCSTR("@")); + if (components->count() < 2) + return false; + + domain = (String *) components->lastObject(); + provider = VTMxRecordForHostname(domain); + } + + if (provider != NULL) { + mIdentifier = provider->identifier(); + + //If no custom Services have been set look for some in the provider + + if (mImapServices->count() == 0) { + if (provider->imapServices()->count() > 0) { + mImapServices = provider->imapServices(); + } else { + //mImapError = NewError? No Info found or provided; + } + } + + if (mPopServices->count() == 0) { + if (provider->popServices()->count() > 0) { + mPopServices = provider->popServices(); + } else { + //mPopError = NewError? No Info found or provided + } + } + + if (mSmtpServices->count() == 0) { + if (provider->smtpServices()->count() > 0) { + mSmtpServices = provider->smtpServices(); + } else { + //mSmtpError = NewError? No Info found or provided; + } + } + } + + return (mImapServices->count() > 0 || mPopServices->count() > 0 || mSmtpServices->count() > 0); +} + +void AccountValidator::opCompleted() +{ + if (mCurrentServiceTested == 0) { + mImapError = ((IMAPOperation *)mOperation)->error(); + (mImapError == ErrorNone) ? mCurrentServiceTested ++ : mCurrentServiceIndex ++; + } else if (mCurrentServiceTested == 1) { + mPopError = ((POPOperation *)mOperation)->error(); + (mPopError == ErrorNone) ? mCurrentServiceTested ++ : mCurrentServiceIndex ++; + } else if (mCurrentServiceTested == 2) { + mSmtpError = ((SMTPOperation *)mOperation)->error(); + (mSmtpError == ErrorNone) ? mCurrentServiceTested ++ : mCurrentServiceIndex ++; + } + + test(); +} + +/** + 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. + */ +void AccountValidator::test() +{ + if (mCurrentServiceTested == 0) { + if (mCurrentServiceIndex < mImapServices->count()) { + IMAPAsyncSession *imapSession = new IMAPAsyncSession(); + imapSession->setUsername(mUsername); + imapSession->setPassword(mPassword); + + mImapServer = (NetService *) mImapServices->objectAtIndex(mCurrentServiceIndex); + imapSession->setHostname(mImapServer->hostname()); + imapSession->setPort(mImapServer->port()); + imapSession->setConnectionType(mImapServer->connectionType()); + + mOperation = (IMAPOperation *)imapSession->checkAccountOperation(); + mOperation->setCallback(this); + mOperation->start(); + + } else { + mCurrentServiceTested = 1; + mCurrentServiceIndex = 0; + test(); + } + }else if (mCurrentServiceTested == 1){ + if (mCurrentServiceIndex < mPopServices->count()) { + POPAsyncSession *popSession = new POPAsyncSession(); + popSession->setUsername(mUsername); + popSession->setPassword(mPassword); + + mPopServer = (NetService *) mPopServices->objectAtIndex(mCurrentServiceIndex); + popSession->setHostname(mPopServer->hostname()); + popSession->setPort(mPopServer->port()); + popSession->setConnectionType(mPopServer->connectionType()); + + mOperation = (POPOperation *)popSession->checkAccountOperation(); + mOperation->setCallback(this); + mOperation->start(); + } else { + mCurrentServiceTested = 2; + mCurrentServiceIndex = 0; + test(); + } + }else if (mCurrentServiceTested == 2){ + if (mCurrentServiceIndex < mSmtpServices->count()) { + SMTPAsyncSession *smtpSession = new SMTPAsyncSession(); + smtpSession->setUsername(mUsername); + smtpSession->setPassword(mPassword); + + mSmtpServer = (NetService *) mSmtpServices->objectAtIndex(mCurrentServiceIndex); + smtpSession->setHostname(mSmtpServer->hostname()); + smtpSession->setPort(mSmtpServer->port()); + smtpSession->setConnectionType(mSmtpServer->connectionType()); + + mOperation = (SMTPOperation *)smtpSession->checkAccountOperation(Address::addressWithMailbox(mEmail)); + mOperation->setCallback(this); + mOperation->start(); + + } else { + mCallback->operationFinished(this); + } + } else { + mCallback->operationFinished(this); + } +} + +MailProvider* AccountValidator::VTMxRecordForHostname(String *hostname) +{ + unsigned char response[NS_PACKETSZ]; + ns_msg handle; + ns_rr rr; + int len; + char dispbuf[4096]; + + if ((len = res_search(MCUTF8(hostname), ns_c_in, ns_t_mx, response, sizeof(response))) < 0) { + /* WARN: res_search failed */ + return nil; + } + + if (ns_initparse(response, len, &handle) < 0) { + return nil; + } + + len = ns_msg_count(handle, ns_s_an); + if (len < 0) + return nil; + + CFBundleRef mainBundle = CFBundleGetMainBundle(); + CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, CFSTR("providers"), CFSTR("json"), NULL); + CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle); + CFStringEncoding encodingMethod = CFStringGetSystemEncoding(); + const char *path = CFStringGetCStringPtr(imagePath, encodingMethod); + + String * sPath = String::stringWithUTF8Characters(path); + + MailProvidersManager::sharedManager()->registerProvidersWithFilename(sPath); + + 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); + MailProvider *provider = MailProvidersManager::sharedManager()->providerForMX(str); + if (provider) + return provider; + } + } + + return nil; +} + + +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; +} + +void AccountValidator::setCallback(OperationCallback * callback) +{ + mCallback = callback; +} + +OperationCallback * AccountValidator::Callback() +{ + return mCallback; +} diff --git a/src/core/provider/MCAccountValidator.h b/src/core/provider/MCAccountValidator.h new file mode 100644 index 00000000..27508996 --- /dev/null +++ b/src/core/provider/MCAccountValidator.h @@ -0,0 +1,96 @@ +// +// 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 + +#ifdef __cplusplus + +namespace mailcore { + + class NetService; + class MailProvider; + class IMAPOperation; + + 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 setCallback(OperationCallback * callback); + virtual OperationCallback * Callback(); + + 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; + + bool setup(); + void test(); + MailProvider * VTMxRecordForHostname(String *hostname); + + //indexs for services being tested + int mCurrentServiceIndex; + int mCurrentServiceTested; + + void init(); + + OperationCallback * mCallback; + Operation * mOperation; + void opCompleted(); + virtual void operationFinished(Operation * op); + }; +} + +#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 #include #include +#include #endif diff --git a/src/objc/provider/MCOAccountValidator.h b/src/objc/provider/MCOAccountValidator.h new file mode 100644 index 00000000..0cdc25aa --- /dev/null +++ b/src/objc/provider/MCOAccountValidator.h @@ -0,0 +1,50 @@ +// +// MCOAccountValidator.h +// mailcore2 +// +// Created by Christopher Hockley on 20/01/15. +// Copyright (c) 2015 MailCore. All rights reserved. +// + +#import +#import + +/** + This class is used to validate an email provider and it's associated + metadata and credentials. + + An app might want to use this during setup to limit the number of settings + a user has to input. + */ + +@class MCONetService; + +@interface MCOAccountValidator : MCOOperation + +@property (nonatomic, copy) NSString * email; /* for SMTP */ +@property (nonatomic, copy) NSString * username; +@property (nonatomic, copy) NSString * password; +@property (nonatomic, copy) NSString * OAuth2Token; + +@property (nonatomic, retain) NSArray * /* MCONetService */ imapServers; +@property (nonatomic, retain) NSArray * /* MCONetService */ popServers; +@property (nonatomic, retain) NSArray * /* MCONetService */ smtpServers; + +// result +@property (nonatomic, retain, readonly) NSString * identifier; +@property (nonatomic, retain, readonly) MCONetService * imapServer; +@property (nonatomic, retain, readonly) MCONetService * popServer; +@property (nonatomic, retain, readonly) MCONetService * smtpServer; +@property (nonatomic, retain, readonly) NSError * imapError; +@property (nonatomic, retain, readonly) NSError * popError; +@property (nonatomic, retain, readonly) NSError * smtpError; + +/** + Starts the asynchronous account validation operation. + + @param completionBlock Called when the operation is finished. + */ +- (void) start:(void (^)(void))completionBlock; + + +@end diff --git a/src/objc/provider/MCOAccountValidator.mm b/src/objc/provider/MCOAccountValidator.mm new file mode 100644 index 00000000..172fd8a6 --- /dev/null +++ b/src/objc/provider/MCOAccountValidator.mm @@ -0,0 +1,154 @@ +// +// MCOAccountValidator.m +// mailcore2 +// +// Created by Christopher Hockley on 20/01/15. +// Copyright (c) 2015 MailCore. All rights reserved. +// + +#import "MCOAccountValidator.h" +#include "MCAccountValidator.h" +#include "MCNetService.h" + +#import "MCOOperation+Private.h" +#import "MCOUtils.h" +#import "MCOperationCallback.h" +#import "MCONetService.h" + +typedef void (^CompletionType)(void); + +@interface MCOAccountValidator () + +@property (nonatomic, retain) NSError * imapError; +@property (nonatomic, retain) NSError * popError; +@property (nonatomic, retain) NSError * smtpError; + +- (void) operationCompleted; + +@end + +class MCOValidatorOperationCallback: public mailcore::Object, public mailcore::OperationCallback { +public: + MCOValidatorOperationCallback(MCOAccountValidator * op) + { + mOperation = op; + } + + void operationFinished(mailcore::Operation * op) + { + [mOperation operationCompleted]; + } + +private: + MCOAccountValidator * mOperation; +}; + +@implementation MCOAccountValidator{ + CompletionType _completionBlock; + mailcore::AccountValidator * _validator; + MCOValidatorOperationCallback * _imapCallback; +} + +#define nativeType mailcore::AccountValidator + ++ (void) load +{ + MCORegisterClass(self, &typeid(nativeType)); +} + +- (mailcore::Object *) mco_mcObject +{ + return _validator; +} + ++ (NSObject *) mco_objectWithMCObject:(mailcore::Object *)object +{ + mailcore::AccountValidator * validator = (mailcore::AccountValidator *) object; + return [[[self alloc] initWithMCValidator:validator] autorelease]; +} + +MCO_OBJC_SYNTHESIZE_STRING(setEmail, email) +MCO_OBJC_SYNTHESIZE_STRING(setUsername, username) +MCO_OBJC_SYNTHESIZE_STRING(setPassword, password) +MCO_OBJC_SYNTHESIZE_STRING(setOAuth2Token, OAuth2Token) + +- (id) init +{ + mailcore::AccountValidator * msg = new mailcore::AccountValidator(); + self = [self initWithMCValidator:msg]; + msg->release(); + return self; +} + +- (id) initWithMCValidator:(mailcore::AccountValidator *)validator +{ + self = [super initWithMCOperation:validator]; + + _validator = validator; + _imapCallback = new MCOValidatorOperationCallback(self); + _validator->setCallback(_imapCallback); + _validator->retain(); + + return self; +} + +- (void) start:(void (^)(void))completionBlock +{ + _completionBlock = [completionBlock copy]; + + [self start]; +} + +- (void) cancel +{ + [_completionBlock release]; + _completionBlock = nil; + [super cancel]; +} + +- (void) operationCompleted +{ + if (_completionBlock == NULL) + return; + + _completionBlock(); + [_completionBlock release]; + _completionBlock = nil; +} + +- (NSString *) identifier +{ + return MCO_OBJC_BRIDGE_GET(identifier); +} + +- (MCONetService *) imapServer +{ + return MCO_OBJC_BRIDGE_GET(imapServer); +} + +- (MCONetService *) popServer +{ + return MCO_OBJC_BRIDGE_GET(popServer); +} + +- (MCONetService *) smtpServer +{ + return MCO_OBJC_BRIDGE_GET(smtpServer); +} + +- (NSError *) imapError +{ + return [NSError mco_errorWithErrorCode:_validator->imapError()]; +} + +- (NSError *) popError +{ + return [NSError mco_errorWithErrorCode:_validator->popError()]; +} + +- (NSError *) smtpError +{ + return [NSError mco_errorWithErrorCode:_validator->smtpError()]; +} + +@end diff --git a/src/objc/provider/MCOProvider.h b/src/objc/provider/MCOProvider.h index 09f2cb05..3c7bf7ce 100644 --- a/src/objc/provider/MCOProvider.h +++ b/src/objc/provider/MCOProvider.h @@ -9,9 +9,9 @@ #ifndef _MAILCORE__MCOPROVIDER_h #define _MAILCORE__MCOPROVIDER_h - #import #import #import +#import #endif -- cgit v1.2.3