aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objc/smtp/MCOSMTPOperation.mm
blob: 3bf2af08beb0d3c5b7ab0671090c4a76198aa38a (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
//
//  MCOSMTPCheckAccountOperation.m
//  mailcore2
//
//  Created by DINH Viêt Hoà on 3/29/13.
//  Copyright (c) 2013 MailCore. All rights reserved.
//

#import "MCOSMTPOperation.h"

#include "MCAsyncSMTP.h"

#import "MCOUtils.h"
#import "MCOOperation+Private.h"
#import "MCOSMTPSession.h"

typedef void (^CompletionType)(NSError *error);

@implementation MCOSMTPOperation {
    CompletionType _completionBlock;
    MCOSMTPSession * _session;
}

#define nativeType mailcore::SMTPOperation

- (void) dealloc
{
    [_session release];
    [_completionBlock release];
    [super dealloc];
}

- (void)start:(void (^)(NSError *error))completionBlock {
    _completionBlock = [completionBlock copy];
    [self start];
}

- (void)operationCompleted {
    if (_completionBlock == NULL)
        return;
    
    NSError * error = [self _errorFromNativeOperation];
    _completionBlock(error);
    [_completionBlock release];
    _completionBlock = NULL;
}

- (void) setSession:(MCOSMTPSession *)session
{
    [_session release];
    _session = [session retain];
}

- (MCOSMTPSession *) session
{
    return _session;
}

- (NSError *) _errorFromNativeOperation
{
    nativeType * op = MCO_NATIVE_INSTANCE;
    NSError * error = [NSError mco_errorWithErrorCode:op->error()];
    if (error != nil) {
        if (op->lastSMTPResponse() != NULL || op->lastSMTPResponseCode() != 0) {
            NSMutableDictionary * userInfo = [[error userInfo] mutableCopy];
            if (op->lastSMTPResponse() != NULL) {
                userInfo[MCOSMTPResponseKey] = MCO_TO_OBJC(op->lastSMTPResponse());
            }
            if (op->lastSMTPResponseCode() != 0) {
                userInfo[MCOSMTPResponseCodeKey] = @(op->lastSMTPResponseCode());
            }
            error = [NSError errorWithDomain:[error domain] code:[error code] userInfo:userInfo];
            [userInfo release];
        }
    }
    return error;
}

@end