From 320738eda85a90c77c4072bd320f6cfd3e6416f8 Mon Sep 17 00:00:00 2001 From: HanzZ Date: Sun, 26 Feb 2012 14:03:48 +0100 Subject: [PATCH] Gateway interaction --- ChangeLog | 1 + .../GatewayPayloadSerializer.cpp | 2 +- include/transport/gatewayresponder.h | 43 +++++++++++++ include/transport/usermanager.h | 4 ++ spectrum/src/main.cpp | 4 ++ src/gatewayresponder.cpp | 63 +++++++++++++++++++ src/transport.cpp | 6 ++ 7 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 include/transport/gatewayresponder.h create mode 100644 src/gatewayresponder.cpp diff --git a/ChangeLog b/ChangeLog index 97aaa33c..bc4c7435 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ Version 2.0.0-beta (X-X-X): General: * Added PostreSQL support (thanks to Jadestorm). + * Added XEP-0100 (Gateway interaction) support. * Send presences only "from" bare JID (fixed bug with buddies appearing twice in the roster and potential unregistering issues). * Fixed potential MySQL/SQLite3 deadlocks. diff --git a/include/Swiften/Serializer/PayloadSerializers/GatewayPayloadSerializer.cpp b/include/Swiften/Serializer/PayloadSerializers/GatewayPayloadSerializer.cpp index 8a129376..6b7cd9e7 100644 --- a/include/Swiften/Serializer/PayloadSerializers/GatewayPayloadSerializer.cpp +++ b/include/Swiften/Serializer/PayloadSerializers/GatewayPayloadSerializer.cpp @@ -20,7 +20,7 @@ GatewayPayloadSerializer::GatewayPayloadSerializer() std::string GatewayPayloadSerializer::serializePayload(boost::shared_ptr payload) const { XMLElement query("query", "jabber:iq:gateway"); - if (!payload->getJID().isValid()) { + if (payload->getJID().isValid()) { boost::shared_ptr jid(new XMLElement("jid", "", payload->getJID().toBare().toString())); query.addNode(jid); } diff --git a/include/transport/gatewayresponder.h b/include/transport/gatewayresponder.h new file mode 100644 index 00000000..1acf4403 --- /dev/null +++ b/include/transport/gatewayresponder.h @@ -0,0 +1,43 @@ +/** + * libtransport -- C++ library for easy XMPP Transports development + * + * Copyright (C) 2011, Jan Kaluza + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#pragma once + +#include +#include "Swiften/Swiften.h" +#include "Swiften/Queries/Responder.h" +#include "Swiften/Elements/GatewayPayload.h" + +namespace Transport { + +class UserManager; + +class GatewayResponder : public Swift::Responder { + public: + GatewayResponder(Swift::IQRouter *router, UserManager *userManager); + ~GatewayResponder(); + + private: + virtual bool handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr payload); + virtual bool handleSetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr payload); + UserManager *m_userManager; +}; + +} \ No newline at end of file diff --git a/include/transport/usermanager.h b/include/transport/usermanager.h index 290c4235..5ea47d5c 100644 --- a/include/transport/usermanager.h +++ b/include/transport/usermanager.h @@ -104,6 +104,10 @@ class UserManager : public Swift::EntityCapsProvider { return m_userRegistry; } + Component *getComponent() { + return m_component; + } + /// Connects user manually. /// \param user JID of user. void connectUser(const Swift::JID &user); diff --git a/spectrum/src/main.cpp b/spectrum/src/main.cpp index c57573ea..d05bdeb8 100644 --- a/spectrum/src/main.cpp +++ b/spectrum/src/main.cpp @@ -12,6 +12,7 @@ #include "transport/statsresponder.h" #include "transport/usersreconnecter.h" #include "transport/util.h" +#include "transport/gatewayresponder.h" #include "Swiften/EventLoop/SimpleEventLoop.h" #include #include @@ -421,6 +422,9 @@ int main(int argc, char **argv) StatsResponder statsResponder(&transport, &userManager, &plugin, storageBackend); statsResponder.start(); + GatewayResponder gatewayResponder(transport.getIQRouter(), &userManager); + gatewayResponder.start(); + eventLoop_ = &eventLoop; eventLoop.run(); diff --git a/src/gatewayresponder.cpp b/src/gatewayresponder.cpp new file mode 100644 index 00000000..903c069a --- /dev/null +++ b/src/gatewayresponder.cpp @@ -0,0 +1,63 @@ +/** + * XMPP - libpurple transport + * + * Copyright (C) 2009, Jan Kaluza + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#include "transport/gatewayresponder.h" + +#include +#include +#include "Swiften/Queries/IQRouter.h" +#include "Swiften/Elements/RawXMLPayload.h" +#include "Swiften/Swiften.h" +#include "transport/usermanager.h" +#include "transport/user.h" +#include "transport/transport.h" +#include "log4cxx/logger.h" + +using namespace log4cxx; + +using namespace Swift; +using namespace boost; + +namespace Transport { + +static LoggerPtr logger = Logger::getLogger("GatewayResponder"); + +GatewayResponder::GatewayResponder(Swift::IQRouter *router, UserManager *userManager) : Swift::Responder(router) { + m_userManager = userManager; +} + +GatewayResponder::~GatewayResponder() { +} + +bool GatewayResponder::handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr payload) { + sendResponse(from, id, boost::shared_ptr(new GatewayPayload(Swift::JID(), "Enter legacy network contact ID.", "Contact ID"))); + return true; +} + +bool GatewayResponder::handleSetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr payload) { + std::string prompt = payload->getPrompt(); + std::string escaped = Swift::JID::getEscapedNode(prompt); + std::string jid = escaped + "@" + m_userManager->getComponent()->getJID().toBare().toString(); + + sendResponse(from, id, boost::shared_ptr(new GatewayPayload(jid))); + return true; +} + +} diff --git a/src/transport.cpp b/src/transport.cpp index 0d1d06ea..8eab7bf7 100644 --- a/src/transport.cpp +++ b/src/transport.cpp @@ -36,6 +36,8 @@ #include "Swiften/Serializer/PayloadSerializers/XHTMLIMSerializer.h" #include "Swiften/Parser/PayloadParsers/StatsParser.h" #include "Swiften/Serializer/PayloadSerializers/StatsSerializer.h" +#include "Swiften/Parser/PayloadParsers/GatewayPayloadParser.h" +#include "Swiften/Serializer/PayloadSerializers/GatewayPayloadSerializer.h" #include "Swiften/Serializer/PayloadSerializers/SpectrumErrorSerializer.h" #include "transport/BlockParser.h" #include "transport/BlockSerializer.h" @@ -95,6 +97,7 @@ Component::Component(Swift::EventLoop *loop, Swift::NetworkFactories *factories, m_server->addPayloadParserFactory(new GenericPayloadParserFactory("block", "urn:xmpp:block:0")); m_server->addPayloadParserFactory(new GenericPayloadParserFactory("invisible", "urn:xmpp:invisible:0")); m_server->addPayloadParserFactory(new GenericPayloadParserFactory("query", "http://jabber.org/protocol/stats")); + m_server->addPayloadParserFactory(new GenericPayloadParserFactory("query", "jabber:iq:gateway")); m_server->addPayloadSerializer(new Swift::AttentionSerializer()); m_server->addPayloadSerializer(new Swift::XHTMLIMSerializer()); @@ -102,6 +105,7 @@ Component::Component(Swift::EventLoop *loop, Swift::NetworkFactories *factories, m_server->addPayloadSerializer(new Swift::InvisibleSerializer()); m_server->addPayloadSerializer(new Swift::StatsSerializer()); m_server->addPayloadSerializer(new Swift::SpectrumErrorSerializer()); + m_server->addPayloadSerializer(new Swift::GatewayPayloadSerializer()); m_server->onDataRead.connect(boost::bind(&Component::handleDataRead, this, _1)); m_server->onDataWritten.connect(boost::bind(&Component::handleDataWritten, this, _1)); @@ -121,6 +125,7 @@ Component::Component(Swift::EventLoop *loop, Swift::NetworkFactories *factories, m_component->addPayloadParserFactory(new GenericPayloadParserFactory("block", "urn:xmpp:block:0")); m_component->addPayloadParserFactory(new GenericPayloadParserFactory("invisible", "urn:xmpp:invisible:0")); m_component->addPayloadParserFactory(new GenericPayloadParserFactory("query", "http://jabber.org/protocol/stats")); + m_component->addPayloadParserFactory(new GenericPayloadParserFactory("query", "jabber:iq:gateway")); m_component->addPayloadSerializer(new Swift::AttentionSerializer()); m_component->addPayloadSerializer(new Swift::XHTMLIMSerializer()); @@ -128,6 +133,7 @@ Component::Component(Swift::EventLoop *loop, Swift::NetworkFactories *factories, m_component->addPayloadSerializer(new Swift::InvisibleSerializer()); m_component->addPayloadSerializer(new Swift::StatsSerializer()); m_component->addPayloadSerializer(new Swift::SpectrumErrorSerializer()); + m_component->addPayloadSerializer(new Swift::GatewayPayloadSerializer()); m_stanzaChannel = m_component->getStanzaChannel(); m_iqRouter = m_component->getIQRouter();