Files
@ 58e21039ff25
Branch filter:
Location: libtransport.git/spectrum/src/frontends/slack/SlackRTM.cpp - annotation
58e21039ff25
6.5 KiB
text/x-c++hdr
Slack: Parse channel_joined and channel_created RTM messages
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 | 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 b55393bf9fb6 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 3fb55b243e0e 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 3fb55b243e0e d06d6f74149a 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 b55393bf9fb6 5adb3d1f9733 5adb3d1f9733 3fb55b243e0e 737aa148dada b55393bf9fb6 3fb55b243e0e 3fb55b243e0e 6cfc7e743431 6cfc7e743431 3fb55b243e0e 3fb55b243e0e 5adb3d1f9733 3fb55b243e0e 3fb55b243e0e 0a13c5f0ceb3 f6949a020ad3 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 4b7baa691020 4b7baa691020 4b7baa691020 4b7baa691020 4b7baa691020 4b7baa691020 4b7baa691020 4b7baa691020 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e f823618439d9 f823618439d9 f823618439d9 f823618439d9 f823618439d9 f823618439d9 cf2ba1053384 cf2ba1053384 cf2ba1053384 cf2ba1053384 cf2ba1053384 cf2ba1053384 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 5adb3d1f9733 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 26a01b8efa0a f823618439d9 f823618439d9 cf2ba1053384 cf2ba1053384 cf2ba1053384 cf2ba1053384 cf2ba1053384 cf2ba1053384 cf2ba1053384 cf2ba1053384 cf2ba1053384 cf2ba1053384 f823618439d9 f823618439d9 f823618439d9 f823618439d9 f823618439d9 f823618439d9 f823618439d9 f823618439d9 2ce9fd2e16d5 2ce9fd2e16d5 2ce9fd2e16d5 f823618439d9 f823618439d9 f823618439d9 f823618439d9 f823618439d9 3fb55b243e0e 58e21039ff25 58e21039ff25 58e21039ff25 58e21039ff25 2ce9fd2e16d5 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e d06d6f74149a d06d6f74149a d06d6f74149a d06d6f74149a 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 3fb55b243e0e 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 b55393bf9fb6 24d902983a17 52347fa379b0 52347fa379b0 52347fa379b0 52347fa379b0 52347fa379b0 52347fa379b0 52347fa379b0 b55393bf9fb6 52347fa379b0 b55393bf9fb6 b55393bf9fb6 b55393bf9fb6 3fb55b243e0e 5adb3d1f9733 5adb3d1f9733 3fb55b243e0e 5adb3d1f9733 f6949a020ad3 3fb55b243e0e f6949a020ad3 6cfc7e743431 5adb3d1f9733 6cfc7e743431 737aa148dada 737aa148dada 737aa148dada 737aa148dada 737aa148dada 737aa148dada 737aa148dada 737aa148dada 737aa148dada 737aa148dada 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 | /**
* XMPP - libpurple transport
*
* Copyright (C) 2009, Jan Kaluza <hanzz@soc.pidgin.im>
*
* 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 "SlackRTM.h"
#include "SlackFrontend.h"
#include "SlackUser.h"
#include "SlackIdManager.h"
#include "transport/Transport.h"
#include "transport/HTTPRequest.h"
#include "transport/Util.h"
#include "transport/WebSocketClient.h"
#include <boost/foreach.hpp>
#include <boost/make_shared.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <map>
#include <iterator>
namespace Transport {
DEFINE_LOGGER(logger, "SlackRTM");
SlackRTM::SlackRTM(Component *component, StorageBackend *storageBackend, SlackIdManager *idManager, UserInfo uinfo) : m_uinfo(uinfo) {
m_component = component;
m_storageBackend = storageBackend;
m_counter = 0;
m_started = false;
m_idManager = idManager;
m_client = new WebSocketClient(component);
m_client->onPayloadReceived.connect(boost::bind(&SlackRTM::handlePayloadReceived, this, _1));
m_client->onWebSocketConnected.connect(boost::bind(&SlackRTM::handleWebSocketConnected, this));
m_pingTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(20000);
m_pingTimer->onTick.connect(boost::bind(&SlackRTM::sendPing, this));
int type = (int) TYPE_STRING;
m_storageBackend->getUserSetting(m_uinfo.id, "bot_token", type, m_token);
m_api = new SlackAPI(component, m_idManager, m_token, m_uinfo.jid);
}
SlackRTM::~SlackRTM() {
delete m_client;
delete m_api;
m_pingTimer->stop();
}
void SlackRTM::start() {
std::string url = "https://slack.com/api/rtm.start?";
url += "token=" + Util::urlencode(m_token);
HTTPRequest *req = new HTTPRequest(THREAD_POOL(m_component), HTTPRequest::Get, url, boost::bind(&SlackRTM::handleRTMStart, this, _1, _2, _3, _4));
req->execute();
}
#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, payload); \
return; \
} \
std::string NAME = NAME##_tmp.GetString();
#define STORE_STRING_OPTIONAL(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \
std::string NAME; \
if (NAME##_tmp.IsString()) { \
NAME = NAME##_tmp.GetString(); \
}
#define GET_OBJECT(FROM, NAME) rapidjson::Value &NAME = FROM[#NAME]; \
if (!NAME.IsObject()) { \
LOG4CXX_ERROR(logger, "No '" << #NAME << "' object in the reply."); \
return; \
}
void SlackRTM::handlePayloadReceived(const std::string &payload) {
rapidjson::Document d;
if (d.Parse<0>(payload.c_str()).HasParseError()) {
LOG4CXX_ERROR(logger, "Error while parsing JSON");
LOG4CXX_ERROR(logger, payload);
return;
}
STORE_STRING(d, type);
if (type == "message") {
STORE_STRING(d, channel);
STORE_STRING(d, text);
STORE_STRING(d, ts);
STORE_STRING_OPTIONAL(d, subtype);
rapidjson::Value &attachments = d["attachments"];
if (attachments.IsArray()) {
for (int i = 0; i < attachments.Size(); i++) {
STORE_STRING_OPTIONAL(attachments[i], fallback);
if (!fallback.empty()) {
text += fallback;
}
}
}
if (subtype == "bot_message") {
STORE_STRING(d, bot_id);
onMessageReceived(channel, bot_id, text, ts);
}
else if (subtype == "me_message") {
text = "/me " + text;
STORE_STRING(d, user);
onMessageReceived(channel, user, text, ts);
}
else if (subtype == "channel_join") {
}
else {
STORE_STRING(d, user);
onMessageReceived(channel, user, text, ts);
}
}
else if (type == "channel_joined"
|| type == "channel_created") {
std::map<std::string, SlackChannelInfo> &channels = m_idManager->getChannels();
SlackAPI::getSlackChannelInfo(NULL, true, d, payload, channels);
}
}
void SlackRTM::sendMessage(const std::string &channel, const std::string &message) {
m_counter++;
std::string m = message;
boost::replace_all(m, "\"", "\\\"");
std::string msg = "{\"id\": " + boost::lexical_cast<std::string>(m_counter) + ", \"type\": \"message\", \"channel\":\"" + channel + "\", \"text\":\"" + m + "\"}";
m_client->write(msg);
}
void SlackRTM::sendPing() {
m_counter++;
std::string msg = "{\"id\": " + boost::lexical_cast<std::string>(m_counter) + ", \"type\": \"ping\"}";
m_client->write(msg);
m_pingTimer->start();
}
void SlackRTM::handleRTMStart(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
if (!ok) {
LOG4CXX_ERROR(logger, req->getError());
LOG4CXX_ERROR(logger, data);
return;
}
rapidjson::Value &url = resp["url"];
if (!url.IsString()) {
LOG4CXX_ERROR(logger, "No 'url' object in the reply.");
LOG4CXX_ERROR(logger, data);
return;
}
rapidjson::Value &self = resp["self"];
if (!self.IsObject()) {
LOG4CXX_ERROR(logger, "No 'self' object in the reply.");
LOG4CXX_ERROR(logger, data);
return;
}
rapidjson::Value &selfName = self["name"];
if (!selfName.IsString()) {
LOG4CXX_ERROR(logger, "No 'name' string in the reply.");
LOG4CXX_ERROR(logger, data);
return;
}
m_idManager->setSelfName(selfName.GetString());
rapidjson::Value &selfId = self["id"];
if (!selfId.IsString()) {
LOG4CXX_ERROR(logger, "No 'id' string in the reply.");
LOG4CXX_ERROR(logger, data);
return;
}
m_idManager->setSelfId(selfId.GetString());
SlackAPI::getSlackChannelInfo(req, ok, resp, data, m_idManager->getChannels());
SlackAPI::getSlackImInfo(req, ok, resp, data, m_idManager->getIMs());
SlackAPI::getSlackUserInfo(req, ok, resp, data, m_idManager->getUsers());
std::string u = url.GetString();
LOG4CXX_INFO(logger, "Started RTM, WebSocket URL is " << u);
LOG4CXX_INFO(logger, data);
#ifndef LIBTRANSPORT_TEST
m_client->connectServer(u);
#endif
}
void SlackRTM::handleWebSocketConnected() {
if (!m_started) {
onRTMStarted();
m_started = true;
}
m_pingTimer->start();
}
void SlackRTM::handleWebSocketDisconnected(const boost::optional<Swift::Connection::Error> &error) {
m_pingTimer->stop();
}
}
|