aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/provider/MCAccountValidator.cpp
blob: efbbfab086bd72c45f27372dc7fcd0f593329c3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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 <arpa/inet.h>
#include <resolv.h>

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;
}