Files
@ 12d117f54ffe
Branch filter:
Location: libtransport.git/src/transport.cpp
12d117f54ffe
12.2 KiB
text/x-c++hdr
Working setUser for postgres
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | /**
* libtransport -- C++ library for easy XMPP Transports development
*
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
*
* 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/transport.h"
#include <boost/bind.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include "transport/storagebackend.h"
#include "transport/factory.h"
#include "transport/userregistry.h"
#include "discoinforesponder.h"
#include "discoitemsresponder.h"
#include "storageparser.h"
#include "Swiften/TLS/OpenSSL/OpenSSLServerContext.h"
#include "Swiften/TLS/PKCS12Certificate.h"
#include "Swiften/TLS/OpenSSL/OpenSSLServerContextFactory.h"
#include "Swiften/Parser/PayloadParsers/AttentionParser.h"
#include "Swiften/Serializer/PayloadSerializers/AttentionSerializer.h"
#include "Swiften/Parser/PayloadParsers/XHTMLIMParser.h"
#include "Swiften/Serializer/PayloadSerializers/XHTMLIMSerializer.h"
#include "Swiften/Parser/PayloadParsers/StatsParser.h"
#include "Swiften/Serializer/PayloadSerializers/StatsSerializer.h"
#include "Swiften/Serializer/PayloadSerializers/SpectrumErrorSerializer.h"
#include "transport/BlockParser.h"
#include "transport/BlockSerializer.h"
#include "Swiften/Parser/PayloadParsers/InvisibleParser.h"
#include "Swiften/Serializer/PayloadSerializers/InvisibleSerializer.h"
#include "log4cxx/logger.h"
#include "log4cxx/consoleappender.h"
#include "log4cxx/patternlayout.h"
#include "log4cxx/propertyconfigurator.h"
#include "Swiften/Swiften.h"
using namespace Swift;
using namespace boost;
using namespace log4cxx;
namespace Transport {
static LoggerPtr logger = Logger::getLogger("Component");
static LoggerPtr logger_xml = Logger::getLogger("Component.XML");
Component::Component(Swift::EventLoop *loop, Swift::NetworkFactories *factories, Config *config, Factory *factory, Transport::UserRegistry *userRegistry) {
m_component = NULL;
m_userRegistry = NULL;
m_server = NULL;
m_reconnectCount = 0;
m_config = config;
m_factory = factory;
m_loop = loop;
m_userRegistry = userRegistry;
m_jid = Swift::JID(CONFIG_STRING(m_config, "service.jid"));
m_factories = factories;
m_reconnectTimer = m_factories->getTimerFactory()->createTimer(3000);
m_reconnectTimer->onTick.connect(bind(&Component::start, this));
if (CONFIG_BOOL(m_config, "service.server_mode")) {
LOG4CXX_INFO(logger, "Creating component in server mode on port " << CONFIG_INT(m_config, "service.port"));
m_server = new Swift::Server(loop, m_factories, m_userRegistry, m_jid, CONFIG_INT(m_config, "service.port"));
if (!CONFIG_STRING(m_config, "service.cert").empty()) {
LOG4CXX_INFO(logger, "Using PKCS#12 certificate " << CONFIG_STRING(m_config, "service.cert"));
LOG4CXX_INFO(logger, "SSLv23_server_method used.");
TLSServerContextFactory *f = new OpenSSLServerContextFactory();
m_server->addTLSEncryption(f, PKCS12Certificate(CONFIG_STRING(m_config, "service.cert"), createSafeByteArray(CONFIG_STRING(m_config, "service.cert_password"))));
}
else {
LOG4CXX_WARN(logger, "No PKCS#12 certificate used. TLS is disabled.");
}
// m_server->start();
m_stanzaChannel = m_server->getStanzaChannel();
m_iqRouter = m_server->getIQRouter();
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<StorageParser>("private", "jabber:iq:private"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::AttentionParser>("attention", "urn:xmpp:attention:0"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::XHTMLIMParser>("html", "http://jabber.org/protocol/xhtml-im"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Transport::BlockParser>("block", "urn:xmpp:block:0"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::InvisibleParser>("invisible", "urn:xmpp:invisible:0"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::StatsParser>("query", "http://jabber.org/protocol/stats"));
m_server->addPayloadSerializer(new Swift::AttentionSerializer());
m_server->addPayloadSerializer(new Swift::XHTMLIMSerializer());
m_server->addPayloadSerializer(new Transport::BlockSerializer());
m_server->addPayloadSerializer(new Swift::InvisibleSerializer());
m_server->addPayloadSerializer(new Swift::StatsSerializer());
m_server->addPayloadSerializer(new Swift::SpectrumErrorSerializer());
m_server->onDataRead.connect(boost::bind(&Component::handleDataRead, this, _1));
m_server->onDataWritten.connect(boost::bind(&Component::handleDataWritten, this, _1));
}
else {
LOG4CXX_INFO(logger, "Creating component in gateway mode");
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(boost::bind(&Component::handleConnectionError, this, _1));
m_component->onDataRead.connect(boost::bind(&Component::handleDataRead, this, _1));
m_component->onDataWritten.connect(boost::bind(&Component::handleDataWritten, this, _1));
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<StorageParser>("private", "jabber:iq:private"));
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::AttentionParser>("attention", "urn:xmpp:attention:0"));
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::XHTMLIMParser>("html", "http://jabber.org/protocol/xhtml-im"));
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Transport::BlockParser>("block", "urn:xmpp:block:0"));
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::InvisibleParser>("invisible", "urn:xmpp:invisible:0"));
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::StatsParser>("query", "http://jabber.org/protocol/stats"));
m_component->addPayloadSerializer(new Swift::AttentionSerializer());
m_component->addPayloadSerializer(new Swift::XHTMLIMSerializer());
m_component->addPayloadSerializer(new Transport::BlockSerializer());
m_component->addPayloadSerializer(new Swift::InvisibleSerializer());
m_component->addPayloadSerializer(new Swift::StatsSerializer());
m_component->addPayloadSerializer(new Swift::SpectrumErrorSerializer());
m_stanzaChannel = m_component->getStanzaChannel();
m_iqRouter = m_component->getIQRouter();
}
m_capsMemoryStorage = new CapsMemoryStorage();
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 Transport::PresenceOracle(m_stanzaChannel);
m_presenceOracle->onPresenceChange.connect(bind(&Component::handlePresence, this, _1));
m_discoInfoResponder = new DiscoInfoResponder(m_iqRouter, m_config);
m_discoInfoResponder->start();
m_discoItemsResponder = new DiscoItemsResponder(m_iqRouter);
m_discoItemsResponder->start();
//
// m_registerHandler = new SpectrumRegisterHandler(m_component);
// m_registerHandler->start();
}
Component::~Component() {
delete m_presenceOracle;
delete m_entityCapsManager;
delete m_capsManager;
delete m_capsMemoryStorage;
delete m_discoInfoResponder;
delete m_discoItemsResponder;
if (m_component)
delete m_component;
if (m_server) {
m_server->stop();
delete m_server;
}
}
Swift::StanzaChannel *Component::getStanzaChannel() {
return m_stanzaChannel;
}
Transport::PresenceOracle *Component::getPresenceOracle() {
return m_presenceOracle;
}
void Component::setTransportFeatures(std::list<std::string> &features) {
m_discoInfoResponder->setTransportFeatures(features);
}
Swift::CapsInfo &Component::getBuddyCapsInfo() {
return m_discoInfoResponder->getBuddyCapsInfo();
}
void Component::setBuddyFeatures(std::list<std::string> &features) {
// TODO: handle caps change
m_discoInfoResponder->setBuddyFeatures(features);
}
void Component::start() {
if (m_component && !m_component->isAvailable()) {
LOG4CXX_INFO(logger, "Connecting XMPP server " << CONFIG_STRING(m_config, "service.server") << " port " << CONFIG_INT(m_config, "service.port"));
m_reconnectCount++;
m_component->connect(CONFIG_STRING(m_config, "service.server"), CONFIG_INT(m_config, "service.port"));
m_reconnectTimer->stop();
}
else if (m_server) {
LOG4CXX_INFO(logger, "Starting component in server mode on port " << CONFIG_INT(m_config, "service.port"));
m_server->start();
// We're connected right here, because we're in server mode...
handleConnected();
}
}
void Component::stop() {
if (m_component) {
m_reconnectCount = 0;
// TODO: Call this once swiften will fix assert(!session_);
// m_component->disconnect();
m_reconnectTimer->stop();
}
else if (m_server) {
LOG4CXX_INFO(logger, "Stopping component in server mode on port " << CONFIG_INT(m_config, "service.port"));
m_server->stop();
}
}
void Component::handleConnected() {
onConnected();
m_reconnectCount = 0;
}
void Component::handleConnectionError(const ComponentError &error) {
onConnectionError(error);
// if (m_reconnectCount == 2)
// Component::instance()->userManager()->removeAllUsers();
std::string str = "Unknown error";
switch (error.getType()) {
case ComponentError::UnknownError: str = "Unknown error"; break;
case ComponentError::ConnectionError: str = "Connection error"; break;
case ComponentError::ConnectionReadError: str = "Connection read error"; break;
case ComponentError::ConnectionWriteError: str = "Connection write error"; break;
case ComponentError::XMLError: str = "XML Error"; break;
case ComponentError::AuthenticationFailedError: str = "Authentication failed error"; break;
case ComponentError::UnexpectedElementError: str = "Unexpected element error"; break;
}
LOG4CXX_INFO(logger, "Disconnected from XMPP server. Error: " << str);
m_reconnectTimer->start();
}
void Component::handleDataRead(const Swift::SafeByteArray &data) {
std::string d = safeByteArrayToString(data);
if (!boost::starts_with(d, "<auth")) {
LOG4CXX_INFO(logger_xml, "XML IN " << d);
}
}
void Component::handleDataWritten(const Swift::SafeByteArray &data) {
LOG4CXX_INFO(logger_xml, "XML OUT " << safeByteArrayToString(data));
}
void Component::handlePresence(Swift::Presence::ref presence) {
bool isMUC = presence->getPayload<MUCPayload>() != NULL || *presence->getTo().getNode().c_str() == '#';
// filter out login/logout presence spam
if (!presence->getTo().getNode().empty() && isMUC == false)
return;
// filter out bad presences
if (!presence->getFrom().isValid()) {
return;
}
// check if we have this client's capabilities and ask for them
if (presence->getType() != Swift::Presence::Unavailable) {
boost::shared_ptr<CapsInfo> capsInfo = presence->getPayload<CapsInfo>();
if (capsInfo && capsInfo->getHash() == "sha-1") {
/*haveFeatures = */m_entityCapsManager->getCaps(presence->getFrom()) != DiscoInfo::ref();
}
#ifdef SUPPORT_LEGACY_CAPS
else {
GetDiscoInfoRequest::ref discoInfoRequest = GetDiscoInfoRequest::create(presence->getFrom(), m_iqRouter);
discoInfoRequest->onResponse.connect(boost::bind(&Component::handleDiscoInfoResponse, this, _1, _2, presence->getFrom()));
discoInfoRequest->send();
}
#endif
}
onUserPresenceReceived(presence);
}
void Component::handleDiscoInfoResponse(boost::shared_ptr<Swift::DiscoInfo> info, Swift::ErrorPayload::ref error, const Swift::JID& jid) {
#ifdef SUPPORT_LEGACY_CAPS
onUserDiscoInfoReceived(jid, info);
#endif
}
void Component::handleCapsChanged(const Swift::JID& jid) {
onUserDiscoInfoReceived(jid, m_entityCapsManager->getCaps(jid));
}
}
|