From 79cc6dc63de8109ab0778412171794b014929962 Mon Sep 17 00:00:00 2001 From: HanzZ Date: Tue, 29 Mar 2011 21:59:14 +0200 Subject: [PATCH] [service] server_mode config --- examples/server_connect/sample.cfg | 3 +- include/transport/transport.h | 11 ++++- spectrum/src/sample.cfg | 3 +- src/config.cpp | 1 + src/transport.cpp | 72 +++++++++++++++++++++++------- src/user.cpp | 4 +- src/usermanager.cpp | 12 ++++- src/userregistration.cpp | 2 +- 8 files changed, 84 insertions(+), 24 deletions(-) diff --git a/examples/server_connect/sample.cfg b/examples/server_connect/sample.cfg index 2c3fda02..5db19bbe 100644 --- a/examples/server_connect/sample.cfg +++ b/examples/server_connect/sample.cfg @@ -2,4 +2,5 @@ jid = icq.localhost password = secret server = 127.0.0.1 -port = 8888 +port = 5222 +server_mode = 1 diff --git a/include/transport/transport.h b/include/transport/transport.h index 2f59dd76..aab6785e 100644 --- a/include/transport/transport.h +++ b/include/transport/transport.h @@ -22,6 +22,7 @@ #include #include "Swiften/Swiften.h" +#include "Swiften/Server/Server.h" #include "Swiften/Disco/GetDiscoInfoRequest.h" #include "Swiften/Disco/EntityCapsManager.h" #include "Swiften/Disco/CapsManager.h" @@ -29,6 +30,7 @@ #include "Swiften/Presence/PresenceOracle.h" #include "Swiften/Network/BoostTimerFactory.h" #include "Swiften/Network/BoostIOServiceThread.h" +#include "Swiften/Server/UserRegistry.h" #include #include "transport/config.h" @@ -68,13 +70,16 @@ namespace Transport { /// Returns Swift::Component associated with this Transport::Component. /// You can use it to send presences and other stanzas. /// \return Swift::Component associated with this Transport::Component - Swift::Component *getComponent(); + Swift::StanzaChannel *getStanzaChannel(); /// Returns Swift::PresenceOracle associated with this Transport::Component. /// You can use it to check current resource connected for particular user. /// \return Swift::PresenceOracle associated with this Transport::Component Swift::PresenceOracle *getPresenceOracle(); + bool inServerMode() { return m_server != NULL; } + const std::string &getUserRegistryPassword(const std::string &barejid); + /// Connects the Jabber server. /// \see Component() void connect(); @@ -134,12 +139,16 @@ namespace Transport { Swift::BoostNetworkFactories *m_factories; Swift::Component *m_component; + Swift::Server *m_server; Swift::Timer::ref m_reconnectTimer; Swift::BoostIOServiceThread m_boostIOServiceThread; 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; StorageBackend *m_storageBackend; DiscoInfoResponder *m_discoInfoResponder; DiscoItemsResponder *m_discoItemsResponder; diff --git a/spectrum/src/sample.cfg b/spectrum/src/sample.cfg index 6de70472..5daa303e 100644 --- a/spectrum/src/sample.cfg +++ b/spectrum/src/sample.cfg @@ -2,8 +2,9 @@ jid = icq.localhost password = secret server = 127.0.0.1 -port = 8888 +port = 5222 protocol=prpl-jabber +server_mode=1 [database] database = test.sql diff --git a/src/config.cpp b/src/config.cpp index 2ad0a32b..0c6603c5 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -37,6 +37,7 @@ bool Config::load(const std::string &configfile, boost::program_options::options ("service.port", value()->default_value(0), "Port the server is listening on") ("service.protocol", value()->default_value(""), "Protocol") ("service.allowed_servers", value()->default_value(""), "Only users from these servers can connect") + ("service.server_mode", value()->default_value(false), "True if Spectrum should behave as server") ("registration.enable_public_registration", value()->default_value(true), "True if users should be able to register.") ("registration.language", value()->default_value("en"), "Default language for registration form") ("registration.instructions", value()->default_value(""), "Instructions showed to user in registration form") diff --git a/src/transport.cpp b/src/transport.cpp index a1d8a37f..bcc4a525 100644 --- a/src/transport.cpp +++ b/src/transport.cpp @@ -29,7 +29,22 @@ using namespace boost; namespace Transport { + +class MyUserRegistry : public Swift::UserRegistry { + public: + MyUserRegistry() {} + ~MyUserRegistry() {} + bool isValidUserPassword(const JID& user, const std::string& password) const { + users[user.toBare().toString()] = password; + return true; + } + mutable std::map users; +}; + Component::Component(Swift::EventLoop *loop, Config *config) { + m_component = NULL; + m_userRegistry = NULL; + m_server = NULL; m_reconnectCount = 0; m_config = config; @@ -40,27 +55,38 @@ Component::Component(Swift::EventLoop *loop, Config *config) { m_reconnectTimer = m_factories->getTimerFactory()->createTimer(1000); m_reconnectTimer->onTick.connect(bind(&Component::connect, this)); - m_component = new Swift::Component(loop, m_factories, m_jid, CONFIG_STRING(m_config, "service.password")); - m_component->setSoftwareVersion("", ""); - m_component->onConnected.connect(bind(&Component::handleConnected, this)); - m_component->onError.connect(bind(&Component::handleConnectionError, this, _1)); - m_component->onDataRead.connect(bind(&Component::handleDataRead, this, _1)); - m_component->onDataWritten.connect(bind(&Component::handleDataWritten, this, _1)); - m_component->onPresenceReceived.connect(bind(&Component::handlePresenceReceived, this, _1)); -// m_component->onMessageReceived.connect(bind(&Component::handleMessageReceived, this, _1)); + if (CONFIG_BOOL(m_config, "service.server_mode")) { + m_userRegistry = new MyUserRegistry(); + m_server = new Swift::Server(loop, m_factories, m_userRegistry, m_jid, CONFIG_INT(m_config, "service.port")); + m_server->start(); + m_stanzaChannel = m_server->getStanzaChannel(); + m_iqRouter = m_server->getIQRouter(); + } + else { + m_component = new Swift::Component(loop, m_factories, m_jid, CONFIG_STRING(m_config, "service.password")); + m_component->setSoftwareVersion("", ""); + m_component->onConnected.connect(bind(&Component::handleConnected, this)); + m_component->onError.connect(bind(&Component::handleConnectionError, this, _1)); + m_component->onDataRead.connect(bind(&Component::handleDataRead, this, _1)); + m_component->onDataWritten.connect(bind(&Component::handleDataWritten, this, _1)); + m_component->onPresenceReceived.connect(bind(&Component::handlePresenceReceived, this, _1)); +// m_component->onMessageReceived.connect(bind(&Component::handleMessageReceived, this, _1)); + m_stanzaChannel = m_component->getStanzaChannel(); + m_iqRouter = m_component->getIQRouter(); + } m_capsMemoryStorage = new CapsMemoryStorage(); - m_capsManager = new CapsManager(m_capsMemoryStorage, m_component->getStanzaChannel(), m_component->getIQRouter()); - m_entityCapsManager = new EntityCapsManager(m_capsManager, m_component->getStanzaChannel()); + m_capsManager = new CapsManager(m_capsMemoryStorage, m_stanzaChannel, m_iqRouter); + m_entityCapsManager = new EntityCapsManager(m_capsManager, m_stanzaChannel); m_entityCapsManager->onCapsChanged.connect(boost::bind(&Component::handleCapsChanged, this, _1)); - m_presenceOracle = new PresenceOracle(m_component->getStanzaChannel()); + m_presenceOracle = new PresenceOracle(m_stanzaChannel); m_presenceOracle->onPresenceChange.connect(bind(&Component::handlePresence, this, _1)); - m_discoInfoResponder = new DiscoInfoResponder(m_component->getIQRouter()); + m_discoInfoResponder = new DiscoInfoResponder(m_iqRouter); m_discoInfoResponder->start(); - m_discoItemsResponder = new DiscoItemsResponder(m_component->getIQRouter()); + m_discoItemsResponder = new DiscoItemsResponder(m_iqRouter); m_discoItemsResponder->start(); // // m_registerHandler = new SpectrumRegisterHandler(m_component); @@ -74,12 +100,22 @@ Component::~Component() { delete m_capsMemoryStorage; // delete m_discoInfoResponder; // delete m_registerHandler; - delete m_component; + if (m_component) + delete m_component; + if (m_server) + delete m_server; + if (m_userRegistry) + delete m_userRegistry; delete m_factories; } -Swift::Component *Component::getComponent() { - return m_component; +const std::string &Component::getUserRegistryPassword(const std::string &barejid) { + MyUserRegistry *registry = dynamic_cast(m_userRegistry); + return registry->users[barejid]; +} + +Swift::StanzaChannel *Component::getStanzaChannel() { + return m_stanzaChannel; } Swift::PresenceOracle *Component::getPresenceOracle() { @@ -96,6 +132,8 @@ void Component::setBuddyFeatures(std::list &features) { } void Component::connect() { + if (!m_component) + return; m_reconnectCount++; m_component->connect(CONFIG_STRING(m_config, "service.server"), CONFIG_INT(m_config, "service.port")); m_reconnectTimer->stop(); @@ -170,7 +208,7 @@ void Component::handlePresence(Swift::Presence::ref presence) { std::cout << "has capsInfo " << haveFeatures << "\n"; } // else { -// GetDiscoInfoRequest::ref discoInfoRequest = GetDiscoInfoRequest::create(presence->getFrom(), m_component->getIQRouter()); +// GetDiscoInfoRequest::ref discoInfoRequest = GetDiscoInfoRequest::create(presence->getFrom(), m_iqRouter); // discoInfoRequest->onResponse.connect(boost::bind(&Component::handleDiscoInfoResponse, this, _1, _2, presence->getFrom())); // discoInfoRequest->send(); // } diff --git a/src/user.cpp b/src/user.cpp index 25a84255..daddfcd7 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -78,14 +78,14 @@ void User::handlePresence(Swift::Presence::ref presence) { if (highest) { highest->setTo(presence->getFrom().toBare()); highest->setFrom(m_component->getJID()); - m_component->getComponent()->sendPresence(highest); + m_component->getStanzaChannel()->sendPresence(highest); } else { Swift::Presence::ref response = Swift::Presence::create(); response->setTo(m_jid.toBare()); response->setFrom(m_component->getJID()); response->setType(Swift::Presence::Unavailable); - m_component->getComponent()->sendPresence(response); + m_component->getStanzaChannel()->sendPresence(response); } } diff --git a/src/usermanager.cpp b/src/usermanager.cpp index 1369c2ee..038863fa 100644 --- a/src/usermanager.cpp +++ b/src/usermanager.cpp @@ -80,7 +80,7 @@ void UserManager::handlePresence(Swift::Presence::ref presence) { response->setTo(presence->getFrom()); response->setFrom(presence->getTo()); response->setType(Swift::Presence::Unavailable); - m_component->getComponent()->sendPresence(response); + m_component->getStanzaChannel()->sendPresence(response); UserInfo res; bool registered = m_storageBackend->getUser(userkey, res); @@ -93,6 +93,16 @@ void UserManager::handlePresence(Swift::Presence::ref presence) { UserInfo res; bool registered = m_storageBackend->getUser(userkey, res); + if (!registered && m_component->inServerMode()) { + res.password = m_component->getUserRegistryPassword(userkey); + res.uin = presence->getFrom().getNode(); + if (res.uin.find_last_of("%") != std::string::npos) { + res.uin.replace(res.uin.find_last_of("%"), 1, "@"); + } + registered = true; + m_storageBackend->setUser(res); + } + if (!registered) { // TODO: logging return; diff --git a/src/userregistration.cpp b/src/userregistration.cpp index 92e527c7..a104c6f8 100644 --- a/src/userregistration.cpp +++ b/src/userregistration.cpp @@ -30,7 +30,7 @@ using namespace Swift; namespace Transport { -UserRegistration::UserRegistration(Component *component, UserManager *userManager, StorageBackend *storageBackend) : Swift::GetResponder(component->m_component->getIQRouter()), Swift::SetResponder(component->m_component->getIQRouter()) { +UserRegistration::UserRegistration(Component *component, UserManager *userManager, StorageBackend *storageBackend) : Swift::GetResponder(component->m_iqRouter), Swift::SetResponder(component->m_iqRouter) { m_component = component; m_config = m_component->m_config; m_storageBackend = storageBackend;