summaryrefslogtreecommitdiffstats
blob: 45df3be87b3eadccb560e0eaf03c2c6f9896d88d (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
#include "Swiften/Server/ServerFromClientSession.h"

#include <boost/bind.hpp>

#include "Swiften/Elements/ProtocolHeader.h"
#include "Swiften/Server/UserRegistry.h"
#include "Swiften/Network/Connection.h"
#include "Swiften/StreamStack/StreamStack.h"
#include "Swiften/StreamStack/ConnectionLayer.h"
#include "Swiften/StreamStack/XMPPLayer.h"
#include "Swiften/Elements/StreamFeatures.h"
#include "Swiften/Elements/ResourceBind.h"
#include "Swiften/Elements/StartSession.h"
#include "Swiften/Elements/IQ.h"
#include "Swiften/Elements/AuthSuccess.h"
#include "Swiften/Elements/AuthFailure.h"
#include "Swiften/Elements/AuthRequest.h"
#include "Swiften/SASL/PLAINMessage.h"

namespace Swift {

ServerFromClientSession::ServerFromClientSession(
		const String& id,
		boost::shared_ptr<Connection> connection, 
		PayloadParserFactoryCollection* payloadParserFactories, 
		PayloadSerializerCollection* payloadSerializers,
		UserRegistry* userRegistry) : 
			id_(id),
			connection_(connection), 
			payloadParserFactories_(payloadParserFactories), 
			payloadSerializers_(payloadSerializers),
			userRegistry_(userRegistry),
			authenticated_(false),
			initialized_(false) {
	xmppLayer_ = boost::shared_ptr<XMPPLayer>(new XMPPLayer(payloadParserFactories_, payloadSerializers_));
	connectionLayer_ = boost::shared_ptr<ConnectionLayer>(new ConnectionLayer(connection_));
	streamStack_ = new StreamStack(xmppLayer_, connectionLayer_);
}

ServerFromClientSession::~ServerFromClientSession() {
	delete streamStack_;
}

void ServerFromClientSession::start() {
	xmppLayer_->onStreamStart.connect(
			boost::bind(&ServerFromClientSession::handleStreamStart, this, _1));
	xmppLayer_->onElement.connect(
			boost::bind(&ServerFromClientSession::handleElement, this, _1));
	//xmppLayer_->onError.connect(
	//		boost::bind(&ServerFromClientSession::setError, this, XMLError));
	xmppLayer_->onDataRead.connect(
			boost::bind(boost::ref(onDataRead), _1));
	xmppLayer_->onWriteData.connect(
			boost::bind(boost::ref(onDataWritten), _1));
	connection_->onDisconnected.connect(boost::bind(&ServerFromClientSession::handleDisconnected, shared_from_this(), _1));
}

void ServerFromClientSession::handleElement(boost::shared_ptr<Element> element) {
	if (initialized_) {
		if (boost::shared_ptr<Stanza> stanza = boost::dynamic_pointer_cast<Stanza>(element)) {
			onStanzaReceived(stanza);
		}
		else {
			std::cerr << "Received unexpected element" << std::endl;
		}
	}
	else {
		if (AuthRequest* authRequest = dynamic_cast<AuthRequest*>(element.get())) {
			if (authRequest->getMechanism() != "PLAIN") {
				xmppLayer_->writeElement(boost::shared_ptr<AuthFailure>(new AuthFailure));
				onSessionFinished();
			}
			else {
				PLAINMessage plainMessage(authRequest->getMessage());
				if (userRegistry_->isValidUserPassword(JID(plainMessage.getAuthenticationID(), domain_.getDomain()), plainMessage.getPassword())) {
					xmppLayer_->writeElement(boost::shared_ptr<AuthSuccess>(new AuthSuccess()));
					user_ = plainMessage.getAuthenticationID();
					authenticated_ = true;
					xmppLayer_->resetParser();
				}
				else {
					xmppLayer_->writeElement(boost::shared_ptr<AuthFailure>(new AuthFailure));
					onSessionFinished();
				}
			}
		}
		else if (IQ* iq = dynamic_cast<IQ*>(element.get())) {
			if (boost::shared_ptr<ResourceBind> resourceBind = iq->getPayload<ResourceBind>()) {
				jid_ = JID(user_, domain_.getDomain(), resourceBind->getResource());
				boost::shared_ptr<ResourceBind> resultResourceBind(new ResourceBind());
				resultResourceBind->setJID(jid_);
				xmppLayer_->writeElement(IQ::createResult(JID(), iq->getID(), resultResourceBind));
			}
			else if (iq->getPayload<StartSession>()) {
				initialized_ = true;
				xmppLayer_->writeElement(IQ::createResult(jid_, iq->getID()));
				onSessionStarted();
			}
		}
	}
}

void ServerFromClientSession::handleStreamStart(const ProtocolHeader& incomingHeader) {
	domain_ = JID("", incomingHeader.getTo());
	ProtocolHeader header;
	header.setFrom(incomingHeader.getTo());
	header.setID(id_);
	xmppLayer_->writeHeader(header);

	boost::shared_ptr<StreamFeatures> features(new StreamFeatures());
	if (!authenticated_) {
		features->addAuthenticationMechanism("PLAIN");
	}
	else {
		features->setHasResourceBind();
		features->setHasSession();
	}
	xmppLayer_->writeElement(features);
}

void ServerFromClientSession::sendStanza(boost::shared_ptr<Stanza> stanza) {
	xmppLayer_->writeElement(stanza);
}

void ServerFromClientSession::handleDisconnected(const boost::optional<Connection::Error>&) {
	onSessionFinished();
}


}