void ServerStanzaChannel::send(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Stanza> stanza) { JID to = stanza->getTo(); assert(to.isValid()); if (!stanza->getFrom().isValid()) { stanza->setFrom(m_jid); } // For a full JID, first try to route to a session with the full JID if (!to.isBare()) { std::list<SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<ServerFromClientSession> >::const_iterator i = std::find_if(sessions[stanza->getTo().toBare().toString()].begin(), sessions[stanza->getTo().toBare().toString()].end(), HasJID(to)); if (i != sessions[stanza->getTo().toBare().toString()].end()) { (*i)->sendElement(stanza); return; } } // Look for candidate sessions to = to.toBare(); std::vector<SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<ServerFromClientSession> > candidateSessions; for (std::list<SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<ServerFromClientSession> >::const_iterator i = sessions[stanza->getTo().toBare().toString()].begin(); i != sessions[stanza->getTo().toBare().toString()].end(); ++i) { if ((*i)->getRemoteJID().equals(to, JID::WithoutResource)) { candidateSessions.push_back(*i); (*i)->sendElement(stanza); } } if (candidateSessions.empty()) { 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::handleElement(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Element> element, const SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<ServerFromClientSession>& session) { SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Stanza> stanza = SWIFTEN_SHRPTR_NAMESPACE::dynamic_pointer_cast<Stanza>(element); if (!stanza) { return; } stanza->setFrom(session->getRemoteJID()); if (!stanza->getFrom().isValid()) return; SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Message> message = SWIFTEN_SHRPTR_NAMESPACE::dynamic_pointer_cast<Message>(stanza); if (message) { onMessageReceived(message); return; } SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Presence> presence = SWIFTEN_SHRPTR_NAMESPACE::dynamic_pointer_cast<Presence>(stanza); if (presence) { onPresenceReceived(presence); return; } SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<IQ> iq = SWIFTEN_SHRPTR_NAMESPACE::dynamic_pointer_cast<IQ>(stanza); if (iq) { onIQReceived(iq); return; } }
SMSNetworkPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin() { this->config = config; this->storageBackend = storagebackend; m_factories = new Swift::BoostNetworkFactories(loop); m_conn = m_factories->getConnectionFactory()->createConnection(); m_conn->onDataRead.connect(boost::bind(&SMSNetworkPlugin::_handleDataRead, this, _1)); m_conn->connect(Swift::HostAddressPort(SWIFT_HOSTADDRESS(host), port)); // m_conn->onConnectFinished.connect(boost::bind(&FrotzNetworkPlugin::_handleConnected, this, _1)); // m_conn->onDisconnected.connect(boost::bind(&FrotzNetworkPlugin::handleDisconnected, this)); LOG4CXX_INFO(logger, "Starting the plugin."); m_timer = m_factories->getTimerFactory()->createTimer(5000); m_timer->onTick.connect(boost::bind(&SMSNetworkPlugin::handleSMSDir, this)); m_timer->start(); // We're reusing our database model here. Buddies of user with JID INTERNAL_USER are there // to match received GSM messages from number N with the XMPP users who sent message to number N. // BuddyName = GSM number // Alias = XMPP user JID to which the messages from this number is sent to. // TODO: This should be per Modem!!! UserInfo info; info.jid = INTERNAL_USER; info.password = ""; storageBackend->setUser(info); storageBackend->getUser(INTERNAL_USER, info); m_internalUser = info.id; }
void ServerStanzaChannel::handleDataRead(const SafeByteArray &data, const SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<ServerFromClientSession> &session) { if (safeByteArrayToString(data).find("</stream:stream>") != std::string::npos) { Swift::Presence::ref presence = Swift::Presence::create(); presence->setFrom(session->getRemoteJID()); presence->setType(Swift::Presence::Unavailable); onPresenceReceived(presence); } }
FrotzNetworkPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() { this->config = config; m_factories = new Swift::BoostNetworkFactories(loop); m_conn = m_factories->getConnectionFactory()->createConnection(); m_conn->onDataRead.connect(boost::bind(&FrotzNetworkPlugin::_handleDataRead, this, _1)); m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(host), port)); // m_conn->onConnectFinished.connect(boost::bind(&FrotzNetworkPlugin::_handleConnected, this, _1)); // m_conn->onDisconnected.connect(boost::bind(&FrotzNetworkPlugin::handleDisconnected, this)); }
void ServerStanzaChannel::handleSessionFinished(const boost::optional<Session::SessionError>&, const SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<ServerFromClientSession>& session) { removeSession(session); // if (!session->initiatedFinish()) { Swift::Presence::ref presence = Swift::Presence::create(); presence->setFrom(session->getRemoteJID()); presence->setType(Swift::Presence::Unavailable); onPresenceReceived(presence); // } }
bool DiscoItemsResponder::handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::DiscoItems> info) { LOG4CXX_INFO(logger, "get request received with node " << info->getNode()); if (info->getNode() == "http://jabber.org/protocol/commands") { sendResponse(from, id, m_commands); } else if (to.getNode().empty() && info->getNode().empty()) { XMPPUser *user = static_cast<XMPPUser *>(m_userManager->getUser(from.toBare().toString())); if (!user) { sendResponse(from, id, m_rooms); return true; } SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<DiscoItems> rooms = SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<DiscoItems>(new DiscoItems()); BOOST_FOREACH(const DiscoItems::Item &item, m_rooms->getItems()) { rooms->addItem(item); } BOOST_FOREACH(const DiscoItems::Item &item, user->getRoomList()->getItems()) { rooms->addItem(item); } sendResponse(from, id, rooms); }
void ServerStanzaChannel::removeSession(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<ServerFromClientSession> session) { session->onSessionFinished.disconnect(boost::bind(&ServerStanzaChannel::handleSessionFinished, this, _1, session)); session->onElementReceived.disconnect(boost::bind(&ServerStanzaChannel::handleElement, this, _1, session)); session->onDataRead.disconnect(boost::bind(&ServerStanzaChannel::handleDataRead, this, _1, session)); std::list<SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<ServerFromClientSession> > &lst = sessions[session->getRemoteJID().toBare().toString()]; lst.erase(std::remove(lst.begin(), lst.end(), session), lst.end()); }
void _handleDataRead(SWIFTEN_SHRPTR_NAMESPACE::shared_ptr<Swift::SafeByteArray> data) { std::string d(data->begin(), data->end()); handleDataRead(d); }
void sendData(const std::string &string) { m_conn->write(Swift::createSafeByteArray(string)); }