aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/provider/MCNetService.cc
blob: 53ae7475e2d0cb0296cc9da76dca566d66f17789 (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
//
//  MCNetService.cpp
//  mailcore2
//
//  Created by Robert Widmann on 4/28/13.
//  Copyright (c) 2013 MailCore. All rights reserved.
//

#include "MCNetService.h"

using namespace mailcore;

void NetService::init()
{
    mHostname = NULL;
    mPort = 0;
    mConnectionType = ConnectionTypeClear;
}

NetService::NetService()
{
	init();
}

NetService::~NetService()
{
	MC_SAFE_RELEASE(mHostname);
}

NetService * NetService::serviceWithInfo(HashMap * info)
{
    NetService * service = new NetService();
    service->fillWithInfo(info);
    service->autorelease();
    return service;
}

void NetService::fillWithInfo(HashMap * info)
{
	bool ssl = false;
    bool starttls = false;
	
	setHostname((String *) info->objectForKey(MCSTR("hostname")));
    if (info->objectForKey(MCSTR("port")) != NULL) {
        setPort(((Value *) info->objectForKey(MCSTR("port")))->intValue());
    }
    if (info->objectForKey(MCSTR("ssl")) != NULL) {
        ssl = ((Value *) info->objectForKey(MCSTR("ssl")))->boolValue();
    }
    if (info->objectForKey(MCSTR("starttls")) != NULL) {
        starttls = ((Value *) info->objectForKey(MCSTR("starttls")))->boolValue();
    }
    if (ssl) {
        mConnectionType = ConnectionTypeTLS;
    }
    else if (starttls) {
        mConnectionType = ConnectionTypeStartTLS;
    }
    else {
        mConnectionType = ConnectionTypeClear;
    }
}

void NetService::setHostname(String *hostname)
{
    MC_SAFE_REPLACE_COPY(String, mHostname, hostname);
}

String * NetService::hostname()
{
	return mHostname;
}

void NetService::setPort(unsigned int port)
{
	mPort = port;
}

unsigned int NetService::port()
{
	return mPort;
}

void NetService::setConnectionType(ConnectionType connectionType)
{
	mConnectionType = connectionType;
}

ConnectionType NetService::connectionType()
{
	return mConnectionType;
}

String * NetService::normalizedHostnameWithEmail(String * email)
{
	Array *components = email->componentsSeparatedByString(MCSTR("@"));
	String *hostname = (String *) mHostname->copy();
	if (components->count() != 0) {
		hostname->replaceOccurrencesOfString(MCSTR("{domain}"), (String *) components->lastObject());
		return hostname;
	}
	return mHostname;
}

HashMap * NetService::info()
{
	HashMap * result;
    
    result = new HashMap();
    if (mHostname != NULL) {
		result->setObjectForKey(MCSTR("hostname"), mHostname);
    }
    if (mPort != 0) {
		result->setObjectForKey(MCSTR("port"), Value::valueWithIntValue(mPort));
    }
    switch (mConnectionType) {
        case ConnectionTypeTLS:
			result->setObjectForKey(MCSTR("ssl"), Value::valueWithBoolValue(true));
            break;
        case ConnectionTypeStartTLS:
			result->setObjectForKey(MCSTR("starttls"), Value::valueWithBoolValue(true));
            break;
        default:
            break;
    }
    
    return result;
}