summaryrefslogtreecommitdiffstats
blob: e5bbf9d3aeee01ee243974299118ec05cad6059a (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
#include "Swiften/Client/Client.h"

#include <boost/bind.hpp>

#include "Swiften/Client/Session.h"
#include "Swiften/StreamStack/PlatformTLSLayerFactory.h"
#include "Swiften/Network/BoostConnectionFactory.h"
#include "Swiften/TLS/PKCS12Certificate.h"

namespace Swift {

Client::Client(const JID& jid, const String& password) :
		IQRouter(this), jid_(jid), password_(password), session_(0) {
	connectionFactory_ = new BoostConnectionFactory();
	tlsLayerFactory_ = new PlatformTLSLayerFactory();
}

Client::~Client() {
	delete session_;
	delete tlsLayerFactory_;
  delete connectionFactory_;
}

void Client::connect() {
	delete session_;
	session_ = new Session(jid_, connectionFactory_, tlsLayerFactory_, &payloadParserFactories_, &payloadSerializers_);
	if (!certificate_.isEmpty()) {
		session_->setCertificate(PKCS12Certificate(certificate_, password_));
	}
	session_->onSessionStarted.connect(boost::bind(boost::ref(onConnected)));
	session_->onError.connect(boost::bind(&Client::handleSessionError, this, _1));
	session_->onNeedCredentials.connect(boost::bind(&Client::handleNeedCredentials, this));
	session_->onDataRead.connect(boost::bind(&Client::handleDataRead, this, _1));
	session_->onDataWritten.connect(boost::bind(&Client::handleDataWritten, this, _1));
	session_->onElementReceived.connect(boost::bind(&Client::handleElement, this, _1));
	session_->start();
}

void Client::disconnect() {
	if (session_) {
		session_->stop();
	}
}

void Client::send(boost::shared_ptr<Stanza> stanza) {
	session_->sendElement(stanza);
}

void Client::sendIQ(boost::shared_ptr<IQ> iq) {
	send(iq);
}

void Client::sendMessage(boost::shared_ptr<Message> message) {
	send(message);
}

void Client::sendPresence(boost::shared_ptr<Presence> presence) {
	send(presence);
}

String Client::getNewIQID() {
	return idGenerator_.generateID();
}

void Client::handleElement(boost::shared_ptr<Element> element) {
	boost::shared_ptr<Message> message = boost::dynamic_pointer_cast<Message>(element);
	if (message) {
		onMessageReceived(message);
		return;
	}

	boost::shared_ptr<Presence> presence = boost::dynamic_pointer_cast<Presence>(element);
	if (presence) {
		onPresenceReceived(presence);
		return;
	}

	boost::shared_ptr<IQ> iq = boost::dynamic_pointer_cast<IQ>(element);
	if (iq) {
		onIQReceived(iq);
		return;
	}
}

void Client::setCertificate(const String& certificate) {
	certificate_ = certificate;
}

void Client::handleSessionError(Session::SessionError error) {
	ClientError clientError;
	switch (error) {
		case Session::NoError: 
			assert(false);
			break;
		case Session::DomainNameResolveError:
			clientError = ClientError(ClientError::DomainNameResolveError);
			break;
		case Session::ConnectionError:
			clientError = ClientError(ClientError::ConnectionError);
			break;
		case Session::ConnectionReadError:
			clientError = ClientError(ClientError::ConnectionReadError);
			break;
		case Session::XMLError:
			clientError = ClientError(ClientError::XMLError);
			break;
		case Session::AuthenticationFailedError:
			clientError = ClientError(ClientError::AuthenticationFailedError);
			break;
		case Session::NoSupportedAuthMechanismsError:
			clientError = ClientError(ClientError::NoSupportedAuthMechanismsError);
			break;
		case Session::UnexpectedElementError:
			clientError = ClientError(ClientError::UnexpectedElementError);
			break;
		case Session::ResourceBindError:
			clientError = ClientError(ClientError::ResourceBindError);
			break;
		case Session::SessionStartError:
			clientError = ClientError(ClientError::SessionStartError);
			break;
		case Session::TLSError:
			clientError = ClientError(ClientError::TLSError);
			break;
		case Session::ClientCertificateLoadError:
			clientError = ClientError(ClientError::ClientCertificateLoadError);
			break;
		case Session::ClientCertificateError:
			clientError = ClientError(ClientError::ClientCertificateError);
			break;
	}
	onError(clientError);
}

void Client::handleNeedCredentials() {
	session_->sendCredentials(password_);
}

void Client::handleDataRead(const ByteArray& data) {
	onDataRead(String(data.getData(), data.getSize()));
}

void Client::handleDataWritten(const ByteArray& data) {
	onDataWritten(String(data.getData(), data.getSize()));
}

}