Changeset - c7f2b6298073
[Not reviewed]
include/Swiften/Server/Server.cpp
Show inline comments
 
@@ -125,30 +125,30 @@ void Server::handleDataRead(const SafeByteArray& data) {
 
	onDataRead(data);
 
}
 

	
 
void Server::handleDataWritten(const SafeByteArray& data) {
 
	onDataWritten(data);
 
}
 

	
 
void Server::handleSessionStarted(boost::shared_ptr<ServerFromClientSession> session) {
 
	dynamic_cast<ServerStanzaChannel *>(stanzaChannel_)->addSession(session);
 
}
 

	
 
void Server::handleSessionFinished(boost::shared_ptr<ServerFromClientSession> session) {
 
	if (!session->getRemoteJID().isValid()) {
 
		Swift::Presence::ref presence = Swift::Presence::create();
 
		presence->setFrom(session->getBareJID());
 
		presence->setType(Swift::Presence::Unavailable);
 
		dynamic_cast<ServerStanzaChannel *>(stanzaChannel_)->onPresenceReceived(presence);
 
	}
 
// 	if (!session->getRemoteJID().isValid()) {
 
// 		Swift::Presence::ref presence = Swift::Presence::create();
 
// 		presence->setFrom(session->getBareJID());
 
// 		presence->setType(Swift::Presence::Unavailable);
 
// 		dynamic_cast<ServerStanzaChannel *>(stanzaChannel_)->onPresenceReceived(presence);
 
// 	}
 
	serverFromClientSessions.erase(std::remove(serverFromClientSessions.begin(), serverFromClientSessions.end(), session), serverFromClientSessions.end());
 
	std::cout << "FINISH SESSION2 " << serverFromClientSessions.size() << "\n";
 
	session->onSessionStarted.disconnect(
 
			boost::bind(&Server::handleSessionStarted, this, session));
 
	session->onSessionFinished.disconnect(
 
			boost::bind(&Server::handleSessionFinished, this, session));
 
}
 

	
 
void Server::addTLSEncryption(TLSServerContextFactory* tlsContextFactory, const PKCS12Certificate& cert) {
 
	tlsFactory = tlsContextFactory;
 
	this->cert = cert;
 
}
include/Swiften/Server/ServerFromClientSession.cpp
Show inline comments
 
@@ -36,82 +36,60 @@ ServerFromClientSession::ServerFromClientSession(
 
		UserRegistry* userRegistry) : 
 
			Session(connection, payloadParserFactories, payloadSerializers),
 
			id_(id),
 
			userRegistry_(userRegistry),
 
			authenticated_(false),
 
			initialized(false),
 
			allowSASLEXTERNAL(false),
 
			tlsLayer(0),
 
			tlsConnected(false) {
 
}
 

	
 
ServerFromClientSession::~ServerFromClientSession() {
 
	std::cout << "DESTRUCTOR;\n";
 
	userRegistry_->onPasswordValid.disconnect(boost::bind(&ServerFromClientSession::handlePasswordValid, this, _1));
 
	userRegistry_->onPasswordInvalid.disconnect(boost::bind(&ServerFromClientSession::handlePasswordInvalid, this, _1));
 
	if (tlsLayer) {
 
		delete tlsLayer;
 
	}
 
}
 

	
 
void ServerFromClientSession::handlePasswordValid(const std::string &user) {
 
	if (user != JID(user_, getLocalJID().getDomain()).toString())
 
		return;
 
void ServerFromClientSession::handlePasswordValid() {
 
	if (!isInitialized()) {
 
		userRegistry_->onPasswordValid.disconnect(boost::bind(&ServerFromClientSession::handlePasswordValid, this, _1));
 
		userRegistry_->onPasswordInvalid.disconnect(boost::bind(&ServerFromClientSession::handlePasswordInvalid, this, _1));
 
		getXMPPLayer()->writeElement(boost::shared_ptr<AuthSuccess>(new AuthSuccess()));
 
		authenticated_ = true;
 
		getXMPPLayer()->resetParser();
 
	}
 
}
 

	
 
void ServerFromClientSession::handlePasswordInvalid(const std::string &user) {
 
	if (user != JID(user_, getLocalJID().getDomain()).toString() || authenticated_)
 
		return;
 
void ServerFromClientSession::handlePasswordInvalid() {
 
	if (!isInitialized()) {
 
		userRegistry_->onPasswordValid.disconnect(boost::bind(&ServerFromClientSession::handlePasswordValid, this, _1));
 
		userRegistry_->onPasswordInvalid.disconnect(boost::bind(&ServerFromClientSession::handlePasswordInvalid, this, _1));
 
		getXMPPLayer()->writeElement(boost::shared_ptr<AuthFailure>(new AuthFailure));
 
		finishSession(AuthenticationFailedError);
 
	}
 
}
 

	
 
void ServerFromClientSession::handleElement(boost::shared_ptr<Element> element) {
 
	if (isInitialized()) {
 
		onElementReceived(element);
 
	}
 
	else {
 
		if (AuthRequest* authRequest = dynamic_cast<AuthRequest*>(element.get())) {
 
			if (authRequest->getMechanism() == "PLAIN" || (allowSASLEXTERNAL && authRequest->getMechanism() == "EXTERNAL")) {
 
				if (authRequest->getMechanism() == "EXTERNAL") {
 
						getXMPPLayer()->writeElement(boost::shared_ptr<AuthSuccess>(new AuthSuccess()));
 
						authenticated_ = true;
 
						getXMPPLayer()->resetParser();
 
				}
 
				else {
 
					PLAINMessage plainMessage(authRequest->getMessage() ? *authRequest->getMessage() : createSafeByteArray(""));
 
					user_ = plainMessage.getAuthenticationID();
 
					userRegistry_->onPasswordInvalid(JID(plainMessage.getAuthenticationID(), getLocalJID().getDomain()).toBare().toString());
 
					userRegistry_->onPasswordValid.connect(boost::bind(&ServerFromClientSession::handlePasswordValid, this, _1));
 
					userRegistry_->onPasswordInvalid.connect(boost::bind(&ServerFromClientSession::handlePasswordInvalid, this, _1));
 
					if (userRegistry_->isValidUserPassword(JID(plainMessage.getAuthenticationID(), getLocalJID().getDomain()), plainMessage.getPassword())) {
 
						// we're waiting for usermanager signal now
 
// 						authenticated_ = true;
 
// 						getXMPPLayer()->resetParser();
 
					}
 
					else {
 
						getXMPPLayer()->writeElement(boost::shared_ptr<AuthFailure>(new AuthFailure));
 
						finishSession(AuthenticationFailedError);
 
					}
 
					userRegistry_->isValidUserPassword(JID(plainMessage.getAuthenticationID(), getLocalJID().getDomain()), this, plainMessage.getPassword());
 
				}
 
			}
 
			else {
 
				getXMPPLayer()->writeElement(boost::shared_ptr<AuthFailure>(new AuthFailure));
 
				finishSession(NoSupportedAuthMechanismsError);
 
			}
 
		}
 
		else if (dynamic_cast<StartTLSRequest*>(element.get()) != NULL) {
 
			getXMPPLayer()->writeElement(boost::shared_ptr<TLSProceed>(new TLSProceed));
 
			getStreamStack()->addLayer(tlsLayer);
 
			tlsLayer->connect();
 
			getXMPPLayer()->resetParser();
 
@@ -161,24 +139,28 @@ void ServerFromClientSession::handleStreamStart(const ProtocolHeader& incomingHe
 
	getXMPPLayer()->writeElement(features);
 
}
 

	
 
void ServerFromClientSession::setInitialized() {
 
	initialized = true;
 
	onSessionStarted();
 
}
 

	
 
void ServerFromClientSession::setAllowSASLEXTERNAL() {
 
	allowSASLEXTERNAL = true;
 
}
 

	
 
void ServerFromClientSession::handleSessionFinished(const boost::optional<SessionError>&) {
 
	userRegistry_->stopLogin(JID(user_, getLocalJID().getDomain()), this);
 
}
 

	
 
void ServerFromClientSession::addTLSEncryption(TLSServerContextFactory* tlsContextFactory, const PKCS12Certificate& cert) {
 
	tlsLayer = new TLSServerLayer(tlsContextFactory);
 
	if (!tlsLayer->setServerCertificate(cert)) {
 
// 		std::cout << "error\n";
 
		// TODO:
 
// 		onClosed(boost::shared_ptr<Error>(new Error(Error::InvalidTLSCertificateError)));
 
	}
 
	else {
 
		tlsLayer->onError.connect(boost::bind(&ServerFromClientSession::handleTLSError, this));
 
		tlsLayer->onConnected.connect(boost::bind(&ServerFromClientSession::handleTLSConnected, this));
 
// 		getStreamStack()->addLayer(tlsLayer);
 
// 		tlsLayer->onError.connect(boost::bind(&BasicSessionStream::handleTLSError, this));
include/Swiften/Server/ServerFromClientSession.h
Show inline comments
 
@@ -44,29 +44,31 @@ namespace Swift {
 
			boost::signal<void ()> onSessionStarted;
 
			void setAllowSASLEXTERNAL();
 
			const std::string &getUser() {
 
				return user_;
 
			}
 

	
 
			void addTLSEncryption(TLSServerContextFactory* tlsContextFactory, const PKCS12Certificate& cert);
 

	
 
			Swift::JID getBareJID() {
 
				return Swift::JID(user_, getLocalJID().getDomain());
 
			}
 

	
 
			void handlePasswordValid();
 
			void handlePasswordInvalid();
 

	
 
		private:
 
			void handleElement(boost::shared_ptr<Element>);
 
			void handleStreamStart(const ProtocolHeader& header);
 
			void handlePasswordValid(const std::string &user);
 
			void handlePasswordInvalid(const std::string &user);
 
			void handleSessionFinished(const boost::optional<SessionError>&);
 

	
 
			void setInitialized();
 
			bool isInitialized() const { 
 
				return initialized; 
 
			}
 

	
 
			void handleTLSError() { }
 
			void handleTLSConnected() { tlsConnected = true; }
 

	
 
		private:
 
			std::string id_;
 
			UserRegistry* userRegistry_;
include/Swiften/Server/ServerStanzaChannel.cpp
Show inline comments
 
@@ -20,62 +20,73 @@ namespace {
 
// 	};
 

	
 
	struct HasJID {
 
		HasJID(const JID& jid) : jid(jid) {}
 
		bool operator()(const boost::shared_ptr<ServerFromClientSession> session) const {
 
			return session->getRemoteJID().equals(jid, JID::WithResource);
 
		}
 
		JID jid;
 
	};
 
}
 

	
 
void ServerStanzaChannel::addSession(boost::shared_ptr<ServerFromClientSession> session) {
 
	std::cout << "ADDING SESSION\n";
 
	sessions[session->getRemoteJID().toBare().toString()].push_back(session);
 
	session->onSessionFinished.connect(boost::bind(&ServerStanzaChannel::handleSessionFinished, this, _1, session));
 
	session->onElementReceived.connect(boost::bind(&ServerStanzaChannel::handleElement, this, _1, session));
 
}
 

	
 
void ServerStanzaChannel::removeSession(boost::shared_ptr<ServerFromClientSession> session) {
 
	session->onSessionFinished.disconnect(boost::bind(&ServerStanzaChannel::handleSessionFinished, this, _1, session));
 
	session->onElementReceived.disconnect(boost::bind(&ServerStanzaChannel::handleElement, this, _1, session));
 
	std::list<boost::shared_ptr<ServerFromClientSession> > &lst = sessions[session->getRemoteJID().toBare().toString()];
 
	lst.erase(std::remove(lst.begin(), lst.end(), session), lst.end());
 
}
 

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

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

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

	
 
void ServerStanzaChannel::finishSession(const JID& to, boost::shared_ptr<Element> element) {
 
void ServerStanzaChannel::finishSession(const JID& to, boost::shared_ptr<Element> element, bool last) {
 
	std::vector<boost::shared_ptr<ServerFromClientSession> > candidateSessions;
 
	for (std::list<boost::shared_ptr<ServerFromClientSession> >::const_iterator i = sessions[to.toBare().toString()].begin(); i != sessions[to.toBare().toString()].end(); ++i) {
 
		if (element) {
 
			(*i)->sendElement(element);
 
		}
 
		candidateSessions.push_back(*i);
 
	}
 

	
 
	for (std::vector<boost::shared_ptr<ServerFromClientSession> >::const_iterator i = candidateSessions.begin(); i != candidateSessions.end(); ++i) {
 
		
 
		removeSession(*i);
 
		if (element) {
 
			(*i)->sendElement(element);
 
		}
 

	
 
		if (last && (*i)->getRemoteJID().isValid()) {
 
			Swift::Presence::ref presence = Swift::Presence::create();
 
			presence->setFrom((*i)->getRemoteJID());
 
			presence->setType(Swift::Presence::Unavailable);
 
			onPresenceReceived(presence);
 
		}
 

	
 
		(*i)->finishSession();
 
		sessions[to.toBare().toString()].remove(*i);
 
		std::cout << "FINISH SESSION " << sessions[to.toBare().toString()].size() << "\n";
 
		if (last) {
 
			break;
 
		}
 
	}
 
}
 

	
 
std::string ServerStanzaChannel::getNewIQID() {
 
	return idGenerator.generateID();
 
}
 

	
 
void ServerStanzaChannel::send(boost::shared_ptr<Stanza> stanza) {
 
	JID to = stanza->getTo();
 
	assert(to.isValid());
 

	
 
	// For a full JID, first try to route to a session with the full JID
 
@@ -100,28 +111,30 @@ void ServerStanzaChannel::send(boost::shared_ptr<Stanza> stanza) {
 
		return;
 
	}
 

	
 
	// Find the session with the highest priority
 
// 	std::vector<ServerSession*>::const_iterator i = std::max_element(sessions.begin(), sessions.end(), PriorityLessThan());
 
// 	(*i)->sendStanza(stanza);
 
	return;
 
}
 

	
 
void ServerStanzaChannel::handleSessionFinished(const boost::optional<Session::SessionError>&, const boost::shared_ptr<ServerFromClientSession>& session) {
 
	removeSession(session);
 

	
 
	Swift::Presence::ref presence = Swift::Presence::create();
 
	presence->setFrom(session->getRemoteJID());
 
	presence->setType(Swift::Presence::Unavailable);
 
	onPresenceReceived(presence);
 
// 	if (!session->initiatedFinish()) {
 
		Swift::Presence::ref presence = Swift::Presence::create();
 
		presence->setFrom(session->getRemoteJID());
 
		presence->setType(Swift::Presence::Unavailable);
 
		onPresenceReceived(presence);
 
// 	}
 
}
 

	
 
void ServerStanzaChannel::handleElement(boost::shared_ptr<Element> element, const boost::shared_ptr<ServerFromClientSession>& session) {
 
	boost::shared_ptr<Stanza> stanza = boost::dynamic_pointer_cast<Stanza>(element);
 
	if (!stanza) {
 
		return;
 
	}
 

	
 
	stanza->setFrom(session->getRemoteJID());
 

	
 
	if (!stanza->getFrom().isValid())
 
		return;
include/Swiften/Server/ServerStanzaChannel.h
Show inline comments
 
@@ -17,25 +17,25 @@
 

	
 
namespace Swift {
 
	class Error;
 
	class ServerStanzaChannel : public StanzaChannel {
 
		public:
 
			void addSession(boost::shared_ptr<ServerFromClientSession> session);
 
			void removeSession(boost::shared_ptr<ServerFromClientSession> session);
 

	
 
			void sendIQ(boost::shared_ptr<IQ> iq);
 
			void sendMessage(boost::shared_ptr<Message> message);
 
			void sendPresence(boost::shared_ptr<Presence> presence);
 

	
 
			void finishSession(const JID& to, boost::shared_ptr<Element> element);
 
			void finishSession(const JID& to, boost::shared_ptr<Element> element, bool last = false);
 

	
 
			bool getStreamManagementEnabled() const {
 
				return false;
 
			}
 

	
 
			bool isAvailable() const {
 
				return true;
 
			}
 

	
 
		private:
 
			std::string getNewIQID();
 
			void send(boost::shared_ptr<Stanza> stanza);
include/Swiften/Server/SimpleUserRegistry.cpp
Show inline comments
 
@@ -2,22 +2,29 @@
 
 * Copyright (c) 2010 Remko Tronçon
 
 * Licensed under the GNU General Public License v3.
 
 * See Documentation/Licenses/GPLv3.txt for more information.
 
 */
 

	
 
#include <Swiften/Server/SimpleUserRegistry.h>
 

	
 
namespace Swift {
 

	
 
SimpleUserRegistry::SimpleUserRegistry() {
 
}
 

	
 
bool SimpleUserRegistry::isValidUserPassword(const JID& user, const SafeByteArray& password) {
 
void SimpleUserRegistry::isValidUserPassword(const JID& user, ServerFromClientSession *session, const SafeByteArray& password) {
 
	std::map<JID,SafeByteArray>::const_iterator i = users.find(user);
 
	return i != users.end() ? i->second == password : false;
 

	
 
	
 
	if (i != users.end() && i->second == password) {
 
		session->handlePasswordValid();
 
	}
 
	else {
 
		session->handlePasswordInvalid();
 
	}
 
}
 

	
 
void SimpleUserRegistry::addUser(const JID& user, const std::string& password) {
 
	users.insert(std::make_pair(user, createSafeByteArray(password)));
 
}
 

	
 
}
include/Swiften/Server/SimpleUserRegistry.h
Show inline comments
 
@@ -10,19 +10,19 @@
 

	
 
#include <Swiften/JID/JID.h>
 
#include <string>
 
#include <Swiften/Server/UserRegistry.h>
 

	
 
namespace Swift {
 
	
 

	
 
	class SimpleUserRegistry : public UserRegistry {
 
		public:
 
			SimpleUserRegistry();
 

	
 
			virtual bool isValidUserPassword(const JID& user, const SafeByteArray& password);
 
			virtual void isValidUserPassword(const JID& user, ServerFromClientSession *session, const SafeByteArray& password);
 
			void addUser(const JID& user, const std::string& password);
 

	
 
		private:
 
			std::map<JID, SafeByteArray> users;
 
	};
 
}
include/Swiften/Server/UserRegistry.h
Show inline comments
 
/*
 
 * Copyright (c) 2010 Remko Tronçon
 
 * Licensed under the GNU General Public License v3.
 
 * See Documentation/Licenses/GPLv3.txt for more information.
 
 */
 

	
 
#pragma once
 

	
 
#include <string>
 
#include <Swiften/Base/SafeByteArray.h>
 
#include <boost/signal.hpp>
 
#include "Swiften/Server/ServerFromClientSession.h"
 

	
 
namespace Swift {
 
	class JID;
 

	
 
	class UserRegistry {
 
		public:
 
			virtual ~UserRegistry();
 

	
 
			virtual bool isValidUserPassword(const JID& user, const SafeByteArray& password) = 0;
 

	
 
			virtual void stopLogin(const JID &/*user*/) {};
 

	
 
			boost::signal<void (const std::string &user)> onPasswordValid;
 
			boost::signal<void (const std::string &user)> onPasswordInvalid;
 
			virtual void isValidUserPassword(const JID& user, ServerFromClientSession *session, const SafeByteArray& password) = 0;
 

	
 
			virtual void stopLogin(const JID &/*user*/, ServerFromClientSession *) {};
 
	};
 
}
include/transport/transport.h
Show inline comments
 
@@ -171,25 +171,25 @@ namespace Transport {
 
			void handleCapsChanged(const Swift::JID& jid);
 

	
 
			Swift::BoostNetworkFactories *m_factories;
 
			Swift::Component *m_component;
 
			Swift::Server *m_server;
 
			Swift::Timer::ref m_reconnectTimer;
 
			Swift::EntityCapsManager *m_entityCapsManager;
 
			Swift::CapsManager *m_capsManager;
 
			Swift::CapsMemoryStorage *m_capsMemoryStorage;
 
			Swift::PresenceOracle *m_presenceOracle;
 
			Swift::StanzaChannel *m_stanzaChannel;
 
			Swift::IQRouter *m_iqRouter;
 
			Swift::UserRegistry *m_userRegistry;
 
			Transport::UserRegistry *m_userRegistry;
 
			StorageBackend *m_storageBackend;
 
 			DiscoInfoResponder *m_discoInfoResponder;
 
			DiscoItemsResponder *m_discoItemsResponder;
 
			int m_reconnectCount;
 
			Config* m_config;
 
			std::string m_protocol;
 
			Swift::JID m_jid;
 
			Factory *m_factory;
 
			Swift::EventLoop *m_loop;
 

	
 
		friend class User;
 
		friend class UserRegistration;
include/transport/usermanager.h
Show inline comments
 
@@ -73,32 +73,33 @@ class UserManager {
 
		/// \param user removed User class
 
		boost::signal<void (User *user)> onUserDestroyed;
 

	
 
		bool isUserConnected(const std::string &barejid) const {
 
			return m_users.find(barejid) != m_users.end();
 
		}
 

	
 
		UserRegistry *getUserRegistry() {
 
			return m_userRegistry;
 
		}
 

	
 
		void connectUser(const Swift::JID &user);
 
		void disconnectUser(const Swift::JID &user);
 

	
 
	private:
 
		void handlePresence(Swift::Presence::ref presence);
 
		void handleMessageReceived(Swift::Message::ref message);
 
		void handleGeneralPresenceReceived(Swift::Presence::ref presence);
 
		void handleProbePresence(Swift::Presence::ref presence);
 
		void handleSubscription(Swift::Presence::ref presence);
 
		void handleRemoveTimeout(const std::string jid, bool reconnect);
 
		void handleRemoveTimeout(const std::string jid, User *user, bool reconnect);
 
// 		void handleDiscoInfoResponse(boost::shared_ptr<Swift::DiscoInfo> info, Swift::ErrorPayload::ref error, const Swift::JID& jid);
 
		void addUser(User *user);
 

	
 
		long m_onlineBuddies;
 
		User *m_cachedUser;
 
		std::map<std::string, User *> m_users;
 
		Component *m_component;
 
		StorageBackend *m_storageBackend;
 
		StorageResponder *m_storageResponder;
 
		UserRegistry *m_userRegistry;
 
		Swift::Timer::ref m_removeTimer;
 
		friend class RosterResponder;
include/transport/userregistry.h
Show inline comments
 
@@ -23,41 +23,94 @@
 
#include <string>
 
#include <map>
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Server/UserRegistry.h"
 
#include "transport/config.h"
 

	
 
namespace Transport {
 
	
 
class UserRegistry : public Swift::UserRegistry {
 
	public:
 
		UserRegistry(Config *cfg) {config = cfg;}
 
		~UserRegistry() {}
 
		bool isValidUserPassword(const Swift::JID& user, const Swift::SafeByteArray& password) {
 
		void isValidUserPassword(const Swift::JID& user, Swift::ServerFromClientSession *session, const Swift::SafeByteArray& password) {
 
			if (!CONFIG_STRING(config, "service.admin_username").empty() && user.getNode() == CONFIG_STRING(config, "service.admin_username")) {
 
				if (Swift::safeByteArrayToString(password) == CONFIG_STRING(config, "service.admin_password")) {
 
					onPasswordValid(user);
 
					session->handlePasswordValid();
 
				}
 
				else {
 
					onPasswordInvalid(user);
 
					session->handlePasswordInvalid();
 
				}
 
				return true;
 
				return;
 
			}
 

	
 
			users[user.toBare().toString()] = Swift::safeByteArrayToString(password);
 
			std::string key = user.toBare().toString();
 

	
 
			// Users try to connect twice
 
			if (users.find(key) != users.end()) {
 
				// Kill the first session if the second password is same
 
				if (Swift::safeByteArrayToString(password) == users[key].password) {
 
					Swift::ServerFromClientSession *tmp = users[key].session;
 
					users[key].session = session;
 
					tmp->handlePasswordInvalid();
 
				}
 
				else {
 
					session->handlePasswordInvalid();
 
					std::cout << "invalid " << session << "\n";
 
					return;
 
				}
 
			}
 
			std::cout << "adding " << session << "\n";
 
			users[key].password = Swift::safeByteArrayToString(password);
 
			users[key].session = session;
 
			onConnectUser(user);
 

	
 
			return true;
 
			return;
 
		}
 

	
 
		void stopLogin(const Swift::JID& user, Swift::ServerFromClientSession *session) {
 
			std::cout << "stopping " << session << "\n";
 
			std::string key = user.toBare().toString();
 
			if (users.find(key) != users.end()) {
 
				if (users[key].session == session) {
 
					std::cout << "DISCONNECT USER\n";
 
					onDisconnectUser(user);
 
					users.erase(key);
 
				}
 
			}
 
		}
 

	
 
		void onPasswordValid(const Swift::JID &user) {
 
			std::string key = user.toBare().toString();
 
			if (users.find(key) != users.end()) {
 
				users[key].session->handlePasswordValid();
 
				users.erase(key);
 
			}
 
		}
 

	
 
		void onPasswordInvalid(const Swift::JID &user) {
 
			std::string key = user.toBare().toString();
 
			if (users.find(key) != users.end()) {
 
				users[key].session->handlePasswordInvalid();
 
				users.erase(key);
 
			}
 
		}
 

	
 
		const std::string &getUserPassword(const std::string &barejid) {
 
			return users[barejid];
 
			return users[barejid].password;
 
		}
 

	
 
		boost::signal<void (const Swift::JID &user)> onConnectUser;
 
		boost::signal<void (const Swift::JID &user)> onDisconnectUser;
 

	
 

	
 
	private:
 
		typedef struct {
 
			std::string password;
 
			Swift::ServerFromClientSession *session;
 
		} Sess;
 

	
 
		mutable std::map<std::string, std::string> users;
 
		mutable std::map<std::string, Sess> users;
 
		mutable Config *config;
 
};
 

	
 
}
src/user.cpp
Show inline comments
 
@@ -21,24 +21,27 @@
 
#include "transport/user.h"
 
#include "transport/transport.h"
 
#include "transport/storagebackend.h"
 
#include "transport/rostermanager.h"
 
#include "transport/usermanager.h"
 
#include "transport/conversationmanager.h"
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Server/ServerStanzaChannel.h"
 
#include "Swiften/Elements/StreamError.h"
 
#include "Swiften/Elements/MUCPayload.h"
 
#include "log4cxx/logger.h"
 
#include <boost/foreach.hpp>
 
#include <execinfo.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 

	
 
using namespace log4cxx;
 
using namespace boost;
 

	
 
#define foreach         BOOST_FOREACH
 

	
 
namespace Transport {
 

	
 
static LoggerPtr logger = Logger::getLogger("User");
 

	
 
User::User(const Swift::JID &jid, UserInfo &userInfo, Component *component, UserManager *userManager) {
 
	m_jid = jid;
 
@@ -90,26 +93,46 @@ Swift::JID User::getJIDWithFeature(const std::string &feature) {
 
			continue;
 

	
 
		if (discoInfo->hasFeature(feature)) {
 
			LOG4CXX_INFO(logger, m_jid.toString() << ": Found JID with " << feature << " feature: " << presence->getFrom().toString());
 
			return presence->getFrom();
 
		}
 
	}
 

	
 
	LOG4CXX_INFO(logger, m_jid.toString() << ": No JID with " << feature << " feature");
 
	return jid;
 
}
 

	
 
static void
 
print_trace (void)
 
{
 
void *array[80];
 
size_t size;
 
char **strings;
 
size_t i;
 

	
 
size = backtrace (array, 80);
 
strings = backtrace_symbols (array, size);
 

	
 
printf ("Obtained %zd stack frames.\n", size);
 

	
 
for (i = 0; i < size; i++)
 
	printf ("%s\n", strings[i]);
 

	
 
free (strings);
 
}
 

	
 
void User::handlePresence(Swift::Presence::ref presence) {
 
	std::cout << "PRESENCE " << presence->getFrom().toString() << "\n";
 
// 	print_trace();
 
	if (!m_connected) {
 
		// we are not connected to legacy network, so we should do it when disco#info arrive :)
 
		if (m_readyForConnect == false) {
 
			
 
			// Forward status message to legacy network, but only if it's sent from active resource
 
// 					if (m_activeResource == presence->getFrom().getResource().getUTF8String()) {
 
// 						forwardStatus(presenceShow, stanzaStatus);
 
// 					}
 
			boost::shared_ptr<Swift::CapsInfo> capsInfo = presence->getPayload<Swift::CapsInfo>();
 
			if (capsInfo && capsInfo->getHash() == "sha-1") {
 
				if (m_entityCapsManager->getCaps(presence->getFrom()) != Swift::DiscoInfo::ref()) {
 
					LOG4CXX_INFO(logger, m_jid.toString() << ": Ready to be connected to legacy network");
src/usermanager.cpp
Show inline comments
 
@@ -46,24 +46,25 @@ UserManager::UserManager(Component *component, UserRegistry *userRegistry, Stora
 
	m_userRegistry = userRegistry;
 

	
 
	if (m_storageBackend) {
 
		m_storageResponder = new StorageResponder(component->getIQRouter(), m_storageBackend, this);
 
		m_storageResponder->start();
 
	}
 

	
 
	component->onUserPresenceReceived.connect(bind(&UserManager::handlePresence, this, _1));
 
	m_component->getStanzaChannel()->onMessageReceived.connect(bind(&UserManager::handleMessageReceived, this, _1));
 
	m_component->getStanzaChannel()->onPresenceReceived.connect(bind(&UserManager::handleGeneralPresenceReceived, this, _1));
 

	
 
	m_userRegistry->onConnectUser.connect(bind(&UserManager::connectUser, this, _1));
 
	m_userRegistry->onDisconnectUser.connect(bind(&UserManager::disconnectUser, this, _1));
 
// 	component->onDiscoInfoResponse.connect(bind(&UserManager::handleDiscoInfoResponse, this, _1, _2, _3));
 

	
 
	m_removeTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(1);
 
}
 

	
 
UserManager::~UserManager(){
 
	if (m_storageResponder) {
 
		m_storageResponder->stop();
 
		delete m_storageResponder;
 
	}
 
}
 

	
 
@@ -178,34 +179,38 @@ void UserManager::handlePresence(Swift::Presence::ref presence) {
 

	
 
	// Unavailable MUC presence should not trigger whole account disconnection, so block it here.
 
	bool isMUC = presence->getPayload<Swift::MUCPayload>() != NULL || *presence->getTo().getNode().c_str() == '#';
 
	if (isMUC)
 
		return;
 

	
 
	// Unavailable presence could remove this user, because he could be unavailable
 
	if (presence->getType() == Swift::Presence::Unavailable) {
 
		if (user) {
 
			Swift::Presence::ref highest = m_component->getPresenceOracle()->getHighestPriorityPresence(presence->getFrom().toBare());
 
			// There's no presence for this user, so disconnect
 
			if (!highest || (highest && highest->getType() == Swift::Presence::Unavailable)) {
 
				m_removeTimer->onTick.connect(boost::bind(&UserManager::handleRemoveTimeout, this, user->getJID().toBare().toString(), false));
 
				m_removeTimer->onTick.connect(boost::bind(&UserManager::handleRemoveTimeout, this, user->getJID().toBare().toString(), user, false));
 
				m_removeTimer->start();
 
			}
 
		}
 
	}
 
}
 

	
 
void UserManager::handleRemoveTimeout(const std::string jid, bool reconnect) {
 
	m_removeTimer->onTick.disconnect(boost::bind(&UserManager::handleRemoveTimeout, this, jid, reconnect));
 
void UserManager::handleRemoveTimeout(const std::string jid, User *u, bool reconnect) {
 
	m_removeTimer->onTick.disconnect(boost::bind(&UserManager::handleRemoveTimeout, this, jid, u, reconnect));
 
	User *user = getUser(jid);
 
	if (user != u) {
 
		return;
 
	}
 

	
 
	if (user) {
 
		// Reconnect means that we're disconnecting this User instance from legacy network backend,
 
		// but we're going to connect it again in this call. Currently it's used only when
 
		// "service.more_resources==false".
 
		if (reconnect) {
 
			// Send message about reason
 
			boost::shared_ptr<Swift::Message> msg(new Swift::Message());
 
			msg->setBody("You have signed on from another location.");
 
			msg->setTo(user->getJID().toBare());
 
			msg->setFrom(m_component->getJID());
 
			m_component->getStanzaChannel()->sendMessage(msg);
 

	
 
@@ -301,33 +306,55 @@ void UserManager::handleSubscription(Swift::Presence::ref presence) {
 
		response->setType(Swift::Presence::Unsubscribed);
 
		m_component->getStanzaChannel()->sendPresence(response);
 
	}
 
// 	else {
 
// // 		Log(presence->getFrom().toString().getUTF8String(), "Subscribe presence received, but this user is not logged in");
 
// 	}
 
}
 

	
 
void UserManager::connectUser(const Swift::JID &user) {
 
	// Called by UserRegistry in server mode when user connects the server and wants
 
	// to connect legacy network
 
	if (m_users.find(user.toBare().toString()) != m_users.end()) {
 
		if (CONFIG_BOOL(m_component->getConfig(), "service.more_resources")) {
 
			m_userRegistry->onPasswordValid(user);
 
		}
 
		else {
 
			// Reconnect the user if more resources per one legacy network account are not allowed
 
			m_removeTimer->onTick.connect(boost::bind(&UserManager::handleRemoveTimeout, this, user.toBare().toString(), true));
 
			m_removeTimer->start();
 
		if (m_users[user.toBare().toString()]->isConnected()) {
 
			if (CONFIG_BOOL(m_component->getConfig(), "service.more_resources")) {
 
				m_userRegistry->onPasswordValid(user);
 
			}
 
			else {
 
				boost::shared_ptr<Swift::Message> msg(new Swift::Message());
 
				msg->setBody("You have signed on from another location.");
 
				msg->setTo(user);
 
				msg->setFrom(m_component->getJID());
 
				m_component->getStanzaChannel()->sendMessage(msg);
 
				m_userRegistry->onPasswordValid(user);
 
				m_component->onUserPresenceReceived.disconnect(bind(&UserManager::handlePresence, this, _1));
 
				dynamic_cast<Swift::ServerStanzaChannel *>(m_component->getStanzaChannel())->finishSession(user, boost::shared_ptr<Swift::Element>(new Swift::StreamError()), true);
 
				m_component->onUserPresenceReceived.connect(bind(&UserManager::handlePresence, this, _1));
 
			}
 
		}
 
// 		}
 
// 		else {
 
// 			// Reconnect the user if more resources per one legacy network account are not allowed
 
// 			m_removeTimer->onTick.connect(boost::bind(&UserManager::handleRemoveTimeout, this, user.toBare().toString(), true));
 
// 			m_removeTimer->start();
 
// 		}
 
	}
 
	else {
 
		// simulate initial available presence to start connecting this user.
 
		Swift::Presence::ref response = Swift::Presence::create();
 
		response->setTo(m_component->getJID());
 
		response->setFrom(user);
 
		response->setType(Swift::Presence::Available);
 
		m_component->onUserPresenceReceived(response);
 
		dynamic_cast<Swift::ServerStanzaChannel *>(m_component->getStanzaChannel())->onPresenceReceived(response);
 
	}
 
}
 

	
 

	
 
void UserManager::disconnectUser(const Swift::JID &user) {
 
	Swift::Presence::ref response = Swift::Presence::create();
 
	response->setTo(m_component->getJID());
 
	response->setFrom(user);
 
	response->setType(Swift::Presence::Unavailable);
 
	dynamic_cast<Swift::ServerStanzaChannel *>(m_component->getStanzaChannel())->onPresenceReceived(response);
 
}
 

	
 
}
0 comments (0 inline, 0 general)