From 26a01b8efa0aa88f491dc54098f542700d00229c Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Fri, 4 Dec 2015 20:21:36 +0100 Subject: [PATCH] Slack: allow registering an account and chatting with normal contacts. --- include/transport/HTTPRequestQueue.h | 9 +- include/transport/NetworkPlugin.h | 2 +- spectrum/src/frontends/slack/SlackAPI.cpp | 73 +++++++++------ spectrum/src/frontends/slack/SlackAPI.h | 2 + spectrum/src/frontends/slack/SlackRTM.cpp | 3 +- spectrum/src/frontends/slack/SlackRTM.h | 2 +- .../frontends/slack/SlackRosterManager.cpp | 41 ++++++++- .../src/frontends/slack/SlackRosterManager.h | 7 +- spectrum/src/frontends/slack/SlackSession.cpp | 90 +++++++++++++++---- spectrum/src/frontends/slack/SlackSession.h | 10 ++- spectrum/src/frontends/slack/SlackUser.cpp | 1 + src/Config.cpp | 2 +- src/HTTPRequestQueue.cpp | 15 +++- src/Transport.cpp | 2 +- src/WebSocketClient.cpp | 10 ++- 15 files changed, 215 insertions(+), 54 deletions(-) diff --git a/include/transport/HTTPRequestQueue.h b/include/transport/HTTPRequestQueue.h index a5677dc9..34e325cd 100644 --- a/include/transport/HTTPRequestQueue.h +++ b/include/transport/HTTPRequestQueue.h @@ -9,13 +9,16 @@ #include #include "rapidjson/document.h" +#include "Swiften/Network/Timer.h" + namespace Transport { class HTTPRequest; +class Component; class HTTPRequestQueue { public: - HTTPRequestQueue(int delayBetweenRequests = 1); + HTTPRequestQueue(Component *component, int delayBetweenRequests = 1); virtual ~HTTPRequestQueue(); @@ -23,10 +26,14 @@ class HTTPRequestQueue { void sendNextRequest(); + private: + void handleRequestFinished(); + private: int m_delay; std::queue m_queue; bool m_processing; + Swift::Timer::ref m_queueTimer; }; } diff --git a/include/transport/NetworkPlugin.h b/include/transport/NetworkPlugin.h index 1b1c6f08..c9493a26 100644 --- a/include/transport/NetworkPlugin.h +++ b/include/transport/NetworkPlugin.h @@ -40,7 +40,7 @@ class NetworkPlugin { class PluginConfig { public: - PluginConfig() : m_needPassword(true), m_needRegistration(false), m_supportMUC(false), m_rawXML(false), + PluginConfig() : m_needPassword(true), m_needRegistration(true), m_supportMUC(false), m_rawXML(false), m_disableJIDEscaping(false) {} virtual ~PluginConfig() {} diff --git a/spectrum/src/frontends/slack/SlackAPI.cpp b/spectrum/src/frontends/slack/SlackAPI.cpp index 8977faf2..33946d5e 100644 --- a/spectrum/src/frontends/slack/SlackAPI.cpp +++ b/spectrum/src/frontends/slack/SlackAPI.cpp @@ -36,7 +36,29 @@ namespace Transport { DEFINE_LOGGER(logger, "SlackAPI"); -SlackAPI::SlackAPI(Component *component, const std::string &token) { +#define GET_ARRAY(FROM, NAME) rapidjson::Value &NAME = FROM[#NAME]; \ + if (!NAME.IsArray()) { \ + LOG4CXX_ERROR(logger, "No '" << #NAME << "' object in the reply."); \ + return; \ + } + +#define STORE_STRING(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \ + if (!NAME##_tmp.IsString()) { \ + LOG4CXX_ERROR(logger, "No '" << #NAME << "' string in the reply."); \ + LOG4CXX_ERROR(logger, data); \ + return; \ + } \ + std::string NAME = NAME##_tmp.GetString(); + +#define STORE_BOOL(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \ + if (!NAME##_tmp.IsBool()) { \ + LOG4CXX_ERROR(logger, "No '" << #NAME << "' string in the reply."); \ + LOG4CXX_ERROR(logger, data); \ + return; \ + } \ + bool NAME = NAME##_tmp.GetBool(); + +SlackAPI::SlackAPI(Component *component, const std::string &token) : HTTPRequestQueue(component) { m_component = component; m_token = token; } @@ -60,6 +82,29 @@ void SlackAPI::sendMessage(const std::string &from, const std::string &to, const queueRequest(req); } +void SlackAPI::deleteMessage(const std::string &channel, const std::string &ts) { + LOG4CXX_INFO(logger, "Deleting message " << channel << " " << ts); + std::string url = "https://slack.com/api/chat.delete?"; + url += "&channel=" + Util::urlencode(channel); + url += "&ts=" + Util::urlencode(ts); + url += "&token=" + Util::urlencode(m_token); + + HTTPRequest *req = new HTTPRequest(THREAD_POOL(m_component), HTTPRequest::Get, url, + boost::bind(&SlackAPI::handleSendMessage, this, _1, _2, _3, _4)); + queueRequest(req); +} + +void SlackAPI::setPurpose(const std::string &channel, const std::string &purpose) { + std::string url = "https://slack.com/api/channels.setPurpose?"; + url += "&channel=" + Util::urlencode(channel); + url += "&purpose=" + Util::urlencode(purpose); + url += "&token=" + Util::urlencode(m_token); + + HTTPRequest *req = new HTTPRequest(THREAD_POOL(m_component), HTTPRequest::Get, url, + boost::bind(&SlackAPI::handleSendMessage, this, _1, _2, _3, _4)); + queueRequest(req); +} + std::string SlackAPI::getChannelId(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) { if (!ok) { LOG4CXX_ERROR(logger, req->getError()); @@ -137,28 +182,6 @@ void SlackAPI::usersList(HTTPRequest::Callback callback) { queueRequest(req); } -#define GET_ARRAY(FROM, NAME) rapidjson::Value &NAME = FROM[#NAME]; \ - if (!NAME.IsArray()) { \ - LOG4CXX_ERROR(logger, "No '" << #NAME << "' object in the reply."); \ - return; \ - } - -#define STORE_STRING(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \ - if (!NAME##_tmp.IsString()) { \ - LOG4CXX_ERROR(logger, "No '" << #NAME << "' string in the reply."); \ - LOG4CXX_ERROR(logger, data); \ - return; \ - } \ - std::string NAME = NAME##_tmp.GetString(); - -#define STORE_BOOL(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \ - if (!NAME##_tmp.IsBool()) { \ - LOG4CXX_ERROR(logger, "No '" << #NAME << "' string in the reply."); \ - LOG4CXX_ERROR(logger, data); \ - return; \ - } \ - bool NAME = NAME##_tmp.GetBool(); - void SlackAPI::getSlackChannelInfo(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::map &ret) { if (!ok) { LOG4CXX_ERROR(logger, req->getError()); @@ -182,11 +205,11 @@ void SlackAPI::getSlackChannelInfo(HTTPRequest *req, bool ok, rapidjson::Documen rapidjson::Value &members = channels[i]["members"]; for (int y = 0; members.IsArray() && y < members.Size(); y++) { - if (!members[i].IsString()) { + if (!members[y].IsString()) { continue; } - info.members.push_back(members[i].GetString()); + info.members.push_back(members[y].GetString()); } ret[info.name] = info; diff --git a/spectrum/src/frontends/slack/SlackAPI.h b/spectrum/src/frontends/slack/SlackAPI.h index d7cf3600..97c5e7f7 100644 --- a/spectrum/src/frontends/slack/SlackAPI.h +++ b/spectrum/src/frontends/slack/SlackAPI.h @@ -79,7 +79,9 @@ class SlackAPI : public HTTPRequestQueue { void imOpen(const std::string &uid, HTTPRequest::Callback callback); std::string getChannelId(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data); + void deleteMessage(const std::string &channel, const std::string &ts); void sendMessage(const std::string &from, const std::string &to, const std::string &text); + void setPurpose(const std::string &channel, const std::string &purpose); static void getSlackChannelInfo(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::map &channels); static void getSlackImInfo(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::map &ims); diff --git a/spectrum/src/frontends/slack/SlackRTM.cpp b/spectrum/src/frontends/slack/SlackRTM.cpp index 2070fe3f..8f344faf 100644 --- a/spectrum/src/frontends/slack/SlackRTM.cpp +++ b/spectrum/src/frontends/slack/SlackRTM.cpp @@ -89,7 +89,8 @@ void SlackRTM::handlePayloadReceived(const std::string &payload) { STORE_STRING(d, channel); STORE_STRING(d, user); STORE_STRING(d, text); - onMessageReceived(channel, user, text); + STORE_STRING(d, ts); + onMessageReceived(channel, user, text, ts); } } diff --git a/spectrum/src/frontends/slack/SlackRTM.h b/spectrum/src/frontends/slack/SlackRTM.h index 8224749a..d2bdacb3 100644 --- a/spectrum/src/frontends/slack/SlackRTM.h +++ b/spectrum/src/frontends/slack/SlackRTM.h @@ -80,7 +80,7 @@ class SlackRTM { return m_api; } - boost::signal onMessageReceived; + boost::signal onMessageReceived; private: void handlePayloadReceived(const std::string &payload); diff --git a/spectrum/src/frontends/slack/SlackRosterManager.cpp b/spectrum/src/frontends/slack/SlackRosterManager.cpp index 0540697d..6e20d1a5 100644 --- a/spectrum/src/frontends/slack/SlackRosterManager.cpp +++ b/spectrum/src/frontends/slack/SlackRosterManager.cpp @@ -21,6 +21,7 @@ #include "SlackRosterManager.h" #include "SlackFrontend.h" #include "SlackUser.h" +#include "SlackSession.h" #include "transport/Buddy.h" #include "transport/User.h" @@ -40,11 +41,49 @@ DEFINE_LOGGER(logger, "SlackRosterManager"); SlackRosterManager::SlackRosterManager(User *user, Component *component) : RosterManager(user, component){ m_user = user; - m_transport = component; + m_component = component; + + m_onlineBuddiesTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(20000); + m_onlineBuddiesTimer->onTick.connect(boost::bind(&SlackRosterManager::sendOnlineBuddies, this)); + m_onlineBuddiesTimer->start(); } SlackRosterManager::~SlackRosterManager() { + m_onlineBuddiesTimer->stop(); +} +void SlackRosterManager::sendOnlineBuddies() { + std::string onlineBuddies = "Online users: "; + Swift::StatusShow s; + std::string statusMessage; + + const RosterManager::BuddiesMap &roster = getBuddies(); + for(RosterManager::BuddiesMap::const_iterator bt = roster.begin(); bt != roster.end(); bt++) { + Buddy *b = (*bt).second; + if (!b) { + continue; + } + + if (!(b->getStatus(s, statusMessage))) { + continue; + } + + if (s.getType() == Swift::StatusShow::None) { + continue; + } + + onlineBuddies += b->getAlias() + ", "; + } + + if (m_onlineBuddies != onlineBuddies) { + m_onlineBuddies = onlineBuddies; + SlackSession *session = static_cast(m_user)->getSession(); + if (session) { + session->setPurpose(m_onlineBuddies); + } + } + + m_onlineBuddiesTimer->start(); } void SlackRosterManager::doRemoveBuddy(Buddy *buddy) { diff --git a/spectrum/src/frontends/slack/SlackRosterManager.h b/spectrum/src/frontends/slack/SlackRosterManager.h index fb79e797..f9a5d66b 100644 --- a/spectrum/src/frontends/slack/SlackRosterManager.h +++ b/spectrum/src/frontends/slack/SlackRosterManager.h @@ -26,6 +26,8 @@ #include #include +#include "Swiften/Network/Timer.h" + namespace Transport { class Buddy; @@ -44,11 +46,14 @@ class SlackRosterManager : public RosterManager { virtual void doAddBuddy(Buddy *buddy); virtual void doUpdateBuddy(Buddy *buddy); + void sendOnlineBuddies(); private: RosterStorage *m_rosterStorage; User *m_user; - Component *m_transport; + Component *m_component; + Swift::Timer::ref m_onlineBuddiesTimer; + std::string m_onlineBuddies; }; } diff --git a/spectrum/src/frontends/slack/SlackSession.cpp b/spectrum/src/frontends/slack/SlackSession.cpp index 200793d9..b8465cb5 100644 --- a/spectrum/src/frontends/slack/SlackSession.cpp +++ b/spectrum/src/frontends/slack/SlackSession.cpp @@ -22,6 +22,7 @@ #include "SlackFrontend.h" #include "SlackUser.h" #include "SlackRTM.h" +#include "SlackRosterManager.h" #include "transport/Transport.h" #include "transport/HTTPRequest.h" @@ -43,13 +44,13 @@ namespace Transport { DEFINE_LOGGER(logger, "SlackSession"); -SlackSession::SlackSession(Component *component, StorageBackend *storageBackend, UserInfo uinfo) : m_uinfo(uinfo) { +SlackSession::SlackSession(Component *component, StorageBackend *storageBackend, UserInfo uinfo) : m_uinfo(uinfo), m_user(NULL) { m_component = component; m_storageBackend = storageBackend; m_rtm = new SlackRTM(component, storageBackend, uinfo); m_rtm->onRTMStarted.connect(boost::bind(&SlackSession::handleRTMStarted, this)); - m_rtm->onMessageReceived.connect(boost::bind(&SlackSession::handleMessageReceived, this, _1, _2, _3, false)); + m_rtm->onMessageReceived.connect(boost::bind(&SlackSession::handleMessageReceived, this, _1, _2, _3, _4, false)); } @@ -62,16 +63,34 @@ void SlackSession::sendMessage(boost::shared_ptr message) { return; } - std::string &channel = m_jid2channel[message->getFrom().toBare().toString()]; + std::string from = message->getFrom().getResource(); + std::string channel = m_jid2channel[message->getFrom().toBare().toString()]; + LOG4CXX_INFO(logger, "JID is " << message->getFrom().toBare().toString()); if (channel.empty()) { if (m_slackChannel.empty()) { LOG4CXX_ERROR(logger, m_uinfo.jid << ": Received message for unknown channel from " << message->getFrom().toBare().toString()); return; } channel = m_slackChannel; + from = Buddy::JIDToLegacyName(message->getFrom()); + + Buddy *b; + if (m_user && (b = m_user->getRosterManager()->getBuddy(from)) != NULL) { + from = b->getAlias() + " (" + from + ")"; + } } - m_rtm->getAPI()->sendMessage(message->getFrom().getResource(), channel, message->getBody()); + LOG4CXX_INFO(logger, "FROM " << from); + m_rtm->getAPI()->sendMessage(from, channel, message->getBody()); +} + +void SlackSession::setPurpose(const std::string &purpose) { + if (m_slackChannel.empty()) { + return; + } + + LOG4CXX_INFO(logger, "Setting channel purppose: " << m_slackChannel << " " << purpose); + m_rtm->getAPI()->setPurpose(m_slackChannel, purpose); } void SlackSession::handleJoinMessage(const std::string &message, std::vector &args, bool quiet) { @@ -81,10 +100,15 @@ void SlackSession::handleJoinMessage(const std::string &message, std::vectorsetUser(m_uinfo); - std::string to = legacyRoom + "%" + legacyServer + "@" + m_component->getJID().toString(); + if (!CONFIG_BOOL_DEFAULTED(m_component->getConfig(), "registration.needRegistration", true)) { + m_uinfo.uin = name; + m_storageBackend->setUser(m_uinfo); + } +// else { +// to = legacyRoom + "\\40" + legacyServer + "@" + m_component->getJID().toString(); +// } + m_jid2channel[to] = slackChannel; m_channel2jid[slackChannel] = to; @@ -173,7 +197,7 @@ void SlackSession::handleRegisterMessage(const std::string &message, std::vector } } -void SlackSession::handleMessageReceived(const std::string &channel, const std::string &user, const std::string &message, bool quiet) { +void SlackSession::handleMessageReceived(const std::string &channel, const std::string &user, const std::string &message, const std::string &ts, bool quiet) { if (m_ownerChannel != channel) { std::string to = m_channel2jid[channel]; if (!to.empty()) { @@ -185,7 +209,41 @@ void SlackSession::handleMessageReceived(const std::string &channel, const std:: m_component->getFrontend()->onMessageReceived(msg); } else { + // When changing the purpose, we do not want to spam to room with the info, + // so remove the purpose message. +// if (message.find("set the channel purpose") != std::string::npos) { +// m_rtm->getAPI()->deleteMessage(channel, ts); +// } // TODO: MAP `user` to JID somehow and send the message to proper JID. + // So far send to all online contacts + + if (!m_user || !m_user->getRosterManager()) { + return; + } + + Swift::StatusShow s; + std::string statusMessage; + const RosterManager::BuddiesMap &roster = m_user->getRosterManager()->getBuddies(); + for(RosterManager::BuddiesMap::const_iterator bt = roster.begin(); bt != roster.end(); bt++) { + Buddy *b = (*bt).second; + if (!b) { + continue; + } + + if (!(b->getStatus(s, statusMessage))) { + continue; + } + + if (s.getType() == Swift::StatusShow::None) { + continue; + } + + boost::shared_ptr msg(new Swift::Message()); + msg->setTo(b->getJID()); + msg->setFrom(Swift::JID("", m_uinfo.jid, "default")); + msg->setBody("<" + user + "> " + message); + m_component->getFrontend()->onMessageReceived(msg); + } } return; } @@ -240,16 +298,16 @@ void SlackSession::handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document & int type = (int) TYPE_STRING; m_storageBackend->getUserSetting(m_uinfo.id, "rooms", type, rooms); - if (CONFIG_BOOL_DEFAULTED(m_component->getConfig(), "registration.needRegistration", false)) { + if (!CONFIG_BOOL_DEFAULTED(m_component->getConfig(), "registration.needRegistration", true)) { if (rooms.empty()) { std::string msg; msg = "Hi, it seems you have enabled Spectrum 2 transport for your Team. As a Team owner, you should now configure it:\\n"; msg += "1. At first, create new channel in which you want this Spectrum 2 transport to send the messages, or choose the existing one.\\n"; msg += "2. Invite this Spectrum 2 bot into this channel.\\n"; msg += "3. Configure the transportation between 3rd-party network and this channel by executing following command in this chat:\\n"; - msg += "```.spectrum2 register 3rdPartyAccount 3rdPartyPassword #SlackChannel```\\n"; - msg += "For example to join XMPP account test@xmpp.tld and transport it into #slack_channel, the command would look like this:\\n"; - msg += "```.spectrum2 register test@xmpp.tld mypassword #slack_channel```\\n"; + msg += "```.spectrum2 join.room NameOfYourBotIn3rdPartyNetwork #3rdPartyRoom hostname_of_3rd_party_server #SlackChannel```\\n"; + msg += "For example to join #test123 channel on Freenode IRC server as MyBot and transport it into #slack_channel, the command would look like this:\\n"; + msg += "```.spectrum2 join.room MyBot #test123 adams.freenode.net #slack_channel```\\n"; msg += "To get full list of available commands, executa `.spectrum2 help`\\n"; m_rtm->sendMessage(m_ownerChannel, msg); } @@ -261,9 +319,9 @@ void SlackSession::handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document & msg += "1. At first, create new channel in which you want this Spectrum 2 transport to send the messages, or choose the existing one.\\n"; msg += "2. Invite this Spectrum 2 bot into this channel.\\n"; msg += "3. Configure the transportation between 3rd-party network and this channel by executing following command in this chat:\\n"; - msg += "```.spectrum2 join.room NameOfYourBotIn3rdPartyNetwork #3rdPartyRoom hostname_of_3rd_party_server #SlackChannel```\\n"; - msg += "For example to join #test123 channel on Freenode IRC server as MyBot and transport it into #slack_channel, the command would look like this:\\n"; - msg += "```.spectrum2 join.room MyBot #test123 adams.freenode.net #slack_channel```\\n"; + msg += "```.spectrum2 register 3rdPartyAccount 3rdPartyPassword #SlackChannel```\\n"; + msg += "For example to join XMPP account test@xmpp.tld and transport it into #slack_channel, the command would look like this:\\n"; + msg += "```.spectrum2 register test@xmpp.tld mypassword #slack_channel```\\n"; msg += "To get full list of available commands, executa `.spectrum2 help`\\n"; m_rtm->sendMessage(m_ownerChannel, msg); } @@ -288,7 +346,7 @@ void SlackSession::handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document & BOOST_FOREACH(const std::string &command, commands) { if (command.size() > 5) { LOG4CXX_INFO(logger, m_uinfo.jid << ": Sending command from storage: " << command); - handleMessageReceived(m_ownerChannel, "owner", command, true); + handleMessageReceived(m_ownerChannel, "owner", command, "", true); } } } diff --git a/spectrum/src/frontends/slack/SlackSession.h b/spectrum/src/frontends/slack/SlackSession.h index 351eb5e1..e9fa3405 100644 --- a/spectrum/src/frontends/slack/SlackSession.h +++ b/spectrum/src/frontends/slack/SlackSession.h @@ -38,6 +38,7 @@ class StorageBackend; class HTTPRequest; class SlackRTM; class SlackAPI; +class User; class SlackSession { public: @@ -49,9 +50,15 @@ class SlackSession { void sendMessage(boost::shared_ptr message); + void setPurpose(const std::string &purpose); + + void setUser(User *user) { + m_user = user; + } + private: void handleRTMStarted(); - void handleMessageReceived(const std::string &channel, const std::string &user, const std::string &message, bool quiet); + void handleMessageReceived(const std::string &channel, const std::string &user, const std::string &message, const std::string &ts, bool quiet); void handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data); void handleJoinMessage(const std::string &message, std::vector &args, bool quiet = false); @@ -68,6 +75,7 @@ class SlackSession { std::map m_jid2channel; std::map m_channel2jid; std::string m_slackChannel; + User *m_user; }; } diff --git a/spectrum/src/frontends/slack/SlackUser.cpp b/spectrum/src/frontends/slack/SlackUser.cpp index 0ff46c6b..13216ce3 100644 --- a/spectrum/src/frontends/slack/SlackUser.cpp +++ b/spectrum/src/frontends/slack/SlackUser.cpp @@ -45,6 +45,7 @@ SlackUser::SlackUser(const Swift::JID &jid, UserInfo &userInfo, Component *compo m_userInfo = userInfo; m_session = static_cast(userManager)->moveTempSession(m_jid.toString()); + m_session->setUser(this); } SlackUser::~SlackUser(){ diff --git a/src/Config.cpp b/src/Config.cpp index 60ed2a71..ee513e0c 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -313,7 +313,7 @@ void Config::updateBackendConfig(const std::string &backendConfig) { options_description opts("Backend options"); opts.add_options() ("registration.needPassword", value()->default_value(true), "") - ("registration.needRegistration", value()->default_value(false), "") + ("registration.needRegistration", value()->default_value(true), "") ("registration.extraField", value >()->multitoken(), "") ("features.receipts", value()->default_value(false), "") ("features.muc", value()->default_value(false), "") diff --git a/src/HTTPRequestQueue.cpp b/src/HTTPRequestQueue.cpp index f886525e..2484e311 100644 --- a/src/HTTPRequestQueue.cpp +++ b/src/HTTPRequestQueue.cpp @@ -1,22 +1,31 @@ #include "transport/HTTPRequestQueue.h" #include "transport/HTTPRequest.h" +#include "transport/Transport.h" namespace Transport { DEFINE_LOGGER(logger, "HTTPRequestQueue") -HTTPRequestQueue::HTTPRequestQueue(int delay) { +HTTPRequestQueue::HTTPRequestQueue(Component *component, int delay) { m_delay = delay; m_processing = false; + + m_queueTimer = component->getNetworkFactories()->getTimerFactory()->createTimer(500); + m_queueTimer->onTick.connect(boost::bind(&HTTPRequestQueue::sendNextRequest, this)); } HTTPRequestQueue::~HTTPRequestQueue() { - + m_queueTimer->stop(); +} + +void HTTPRequestQueue::handleRequestFinished() { + m_queueTimer->start(); } void HTTPRequestQueue::sendNextRequest() { if (m_queue.empty()) { m_processing = false; + m_queueTimer->stop(); return; } @@ -26,7 +35,7 @@ void HTTPRequestQueue::sendNextRequest() { HTTPRequest *req = m_queue.front(); m_queue.pop(); - req->onRequestFinished.connect(boost::bind(&HTTPRequestQueue::sendNextRequest, this)); + req->onRequestFinished.connect(boost::bind(&HTTPRequestQueue::handleRequestFinished, this)); req->execute(); } diff --git a/src/Transport.cpp b/src/Transport.cpp index 59d8fb22..5e8e70f5 100644 --- a/src/Transport.cpp +++ b/src/Transport.cpp @@ -66,7 +66,7 @@ Transport::PresenceOracle *Component::getPresenceOracle() { } bool Component::inServerMode() { - return CONFIG_BOOL_DEFAULTED(m_config, "service.server_mode", true); + return CONFIG_BOOL_DEFAULTED(m_config, "service.server_mode", false); } void Component::start() { diff --git a/src/WebSocketClient.cpp b/src/WebSocketClient.cpp index d67e3184..4b001fdf 100644 --- a/src/WebSocketClient.cpp +++ b/src/WebSocketClient.cpp @@ -23,6 +23,8 @@ #include "transport/Util.h" #include "transport/Logging.h" +#include "Swiften/StringCodecs/Hexify.h" + #include #include #include @@ -131,10 +133,16 @@ void WebSocketClient::handleDataRead(boost::shared_ptr dat while (m_buffer.size() > 0) { if (m_buffer.size() >= 2) { + LOG4CXX_INFO(logger, "BUFFER: '" << m_buffer << "'"); + LOG4CXX_INFO(logger, "BUFFER: '" << Swift::Hexify::hexify(Swift::createByteArray(m_buffer)) << "'"); uint8_t opcode = *((uint8_t *) &m_buffer[0]) & 0xf; - uint8_t size7 = *((uint8_t *) &m_buffer[1]); + uint8_t size7 = *((uint8_t *) &m_buffer[1]) & 127; + bool mask = *((uint8_t *) &m_buffer[1]) & 128; uint16_t size16 = 0; int header_size = 2; + LOG4CXX_INFO(logger, "OPCODE: " << (int) opcode); + LOG4CXX_INFO(logger, "SIZE7: " << (int) size7); + LOG4CXX_INFO(logger, "MASK: " << (int) mask); if (size7 == 126) { if (m_buffer.size() >= 4) { size16 = *((uint16_t *) &m_buffer[2]);