Files
@ 7ba8909d719f
Branch filter:
Location: libtransport.git/spectrum/src/frontends/slack/SlackSession.cpp - annotation
7ba8909d719f
11.1 KiB
text/x-c++hdr
Use value_or instead of get_value_or
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 e0ea6762c78c 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 26a01b8efa0a b55393bf9fb6 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 05bf03704aec 2922d57bfd23 24d902983a17 24d902983a17 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 05bf03704aec 05bf03704aec 05bf03704aec 05bf03704aec 26fc8a0323bb 26fc8a0323bb 05bf03704aec 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 e0ea6762c78c 5adb3d1f9733 deba434ac02b 5adb3d1f9733 5adb3d1f9733 5adb3d1f9733 b55393bf9fb6 b55393bf9fb6 b55393bf9fb6 e0ea6762c78c 26a01b8efa0a 4b7baa691020 05bf03704aec 24d902983a17 24d902983a17 fd9c9af71bfb fd9c9af71bfb fd9c9af71bfb fd9c9af71bfb f6949a020ad3 80a43cb5048f 80a43cb5048f 5adb3d1f9733 5adb3d1f9733 e0ea6762c78c 3fb55b243e0e fd9c9af71bfb b55393bf9fb6 24d902983a17 80a43cb5048f 80a43cb5048f 05bf03704aec 05bf03704aec 24d902983a17 24d902983a17 05bf03704aec 05bf03704aec 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 004f9410384a 004f9410384a 004f9410384a 004f9410384a 004f9410384a 80a43cb5048f 004f9410384a 004f9410384a 004f9410384a 004f9410384a 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 d06d6f74149a 26a01b8efa0a 26a01b8efa0a d06d6f74149a 2922d57bfd23 2922d57bfd23 2922d57bfd23 2922d57bfd23 2922d57bfd23 af75453834dd 26a01b8efa0a 26a01b8efa0a 26a01b8efa0a 26a01b8efa0a 26a01b8efa0a d06d6f74149a d06d6f74149a 80a43cb5048f 26fc8a0323bb 7ba8909d719f 26fc8a0323bb 26fc8a0323bb 26fc8a0323bb 26fc8a0323bb 26a01b8efa0a 26a01b8efa0a 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 24d902983a17 26a01b8efa0a 26a01b8efa0a 26a01b8efa0a 80a43cb5048f 703af16e8b75 3fb55b243e0e 3fb55b243e0e efd60bd374ad efd60bd374ad 2922d57bfd23 fd9c9af71bfb fd9c9af71bfb fd9c9af71bfb 2922d57bfd23 2922d57bfd23 80a43cb5048f 80a43cb5048f 80a43cb5048f 80a43cb5048f 80a43cb5048f 80a43cb5048f 26a01b8efa0a 2922d57bfd23 2922d57bfd23 2922d57bfd23 2922d57bfd23 6dd386320050 2922d57bfd23 2922d57bfd23 2922d57bfd23 2922d57bfd23 2922d57bfd23 24d902983a17 fd9c9af71bfb 24d902983a17 fd9c9af71bfb 80a43cb5048f b55393bf9fb6 2922d57bfd23 2922d57bfd23 efd60bd374ad efd60bd374ad efd60bd374ad 80a43cb5048f 52347fa379b0 6dd386320050 52347fa379b0 52347fa379b0 52347fa379b0 52347fa379b0 52347fa379b0 52347fa379b0 b55393bf9fb6 b55393bf9fb6 cedac0a9b001 2922d57bfd23 f6d1844943c8 2922d57bfd23 2922d57bfd23 2922d57bfd23 80a43cb5048f 80a43cb5048f 2922d57bfd23 6dd386320050 2922d57bfd23 2922d57bfd23 2922d57bfd23 2922d57bfd23 2922d57bfd23 2922d57bfd23 26a01b8efa0a f6d1844943c8 f6d1844943c8 f6d1844943c8 f6d1844943c8 24d902983a17 f6d1844943c8 f6d1844943c8 f6d1844943c8 f6d1844943c8 6dd386320050 f6d1844943c8 f6d1844943c8 f6d1844943c8 f6d1844943c8 f6d1844943c8 f6d1844943c8 26a01b8efa0a 26a01b8efa0a 26a01b8efa0a f6d1844943c8 f6d1844943c8 26a01b8efa0a f6d1844943c8 f6d1844943c8 2922d57bfd23 05bf03704aec f6d1844943c8 f6d1844943c8 f6d1844943c8 f6d1844943c8 f6d1844943c8 f6d1844943c8 f6d1844943c8 f6d1844943c8 05bf03704aec f6d1844943c8 f6d1844943c8 f6d1844943c8 05bf03704aec f6d1844943c8 f6d1844943c8 f6d1844943c8 1bf36dbf68fe f6d1844943c8 f6d1844943c8 6dd386320050 f6d1844943c8 f6d1844943c8 f6d1844943c8 05bf03704aec 5adb3d1f9733 5adb3d1f9733 deba434ac02b deba434ac02b deba434ac02b deba434ac02b deba434ac02b deba434ac02b deba434ac02b deba434ac02b deba434ac02b deba434ac02b deba434ac02b f6d1844943c8 deba434ac02b deba434ac02b deba434ac02b deba434ac02b f6d1844943c8 d06d6f74149a d06d6f74149a d06d6f74149a d06d6f74149a f6d1844943c8 f6d1844943c8 f6d1844943c8 2922d57bfd23 80a43cb5048f 80a43cb5048f 80a43cb5048f 2922d57bfd23 2922d57bfd23 2922d57bfd23 d06d6f74149a d06d6f74149a 5adb3d1f9733 d06d6f74149a ab8a950661d4 f6d1844943c8 f6d1844943c8 f6d1844943c8 ab8a950661d4 d06d6f74149a d06d6f74149a 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 "SlackSession.h"
#include "SlackFrontend.h"
#include "SlackUser.h"
#include "SlackRTM.h"
#include "SlackRosterManager.h"
#include "SlackIdManager.h"
#include "transport/Transport.h"
#include "transport/HTTPRequest.h"
#include "transport/Util.h"
#include "transport/Buddy.h"
#include "transport/Config.h"
#include "transport/ConversationManager.h"
#include "transport/Conversation.h"
#include <boost/foreach.hpp>
#include <boost/make_shared.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include "Swiften/Elements/MUCPayload.h"
#include <Swiften/Version.h>
#define HAVE_SWIFTEN_3 (SWIFTEN_VERSION >= 0x030000)
#include <map>
#include <iterator>
namespace Transport {
DEFINE_LOGGER(logger, "SlackSession");
SlackSession::SlackSession(Component *component, StorageBackend *storageBackend, UserInfo uinfo) : m_uinfo(uinfo), m_user(NULL), m_disconnected(false) {
m_component = component;
m_storageBackend = storageBackend;
m_idManager = new SlackIdManager();
m_rtm = new SlackRTM(component, storageBackend, m_idManager, uinfo);
m_rtm->onRTMStarted.connect(boost::bind(&SlackSession::handleRTMStarted, this));
m_rtm->onMessageReceived.connect(boost::bind(&SlackSession::handleMessageReceived, this, _1, _2, _3, _4, false));
m_rtm->start();
m_onlineBuddiesTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(20000);
m_onlineBuddiesTimer->onTick.connect(boost::bind(&SlackSession::sendOnlineBuddies, this));
int type = (int) TYPE_STRING;
std::string token;
m_storageBackend->getUserSetting(m_uinfo.id, "access_token", type, token);
m_api = new SlackAPI(m_component, m_idManager, token, m_uinfo.jid);
LOG4CXX_INFO(logger, m_uinfo.jid << ": SlackSession created.");
}
SlackSession::~SlackSession() {
delete m_rtm;
delete m_api;
delete m_idManager;
m_onlineBuddiesTimer->stop();
LOG4CXX_INFO(logger, m_uinfo.jid << ": SlackSession destroyed.");
}
void SlackSession::sendOnlineBuddies() {
if (!m_user) {
return;
}
std::map<std::string, Conversation *> convs = m_user->getConversationManager()->getConversations();
for (std::map<std::string, Conversation *> ::const_iterator it = convs.begin(); it != convs.end(); it++) {
Conversation *conv = it->second;
if (!conv) {
continue;
}
std::string onlineBuddies = "Online users: " + conv->getParticipants();
if (m_onlineBuddies[it->first] != onlineBuddies) {
m_onlineBuddies[it->first] = onlineBuddies;
std::string legacyName = it->first;
if (legacyName.find_last_of("@") != std::string::npos) {
legacyName.replace(legacyName.find_last_of("@"), 1, "%"); // OK
}
std::string to = legacyName + "@" + m_component->getJID().toBare().toString();
setPurpose(onlineBuddies, m_jid2channel[to]);
}
}
m_onlineBuddiesTimer->start();
}
void SlackSession::sendMessageToAll(const std::string &msg) {
std::vector<std::string> channels;
for (std::map<std::string, std::string>::const_iterator it = m_jid2channel.begin(); it != m_jid2channel.end(); it++) {
if (std::find(channels.begin(), channels.end(), it->second) == channels.end()) {
channels.push_back(it->second);
m_rtm->getAPI()->sendMessage("Spectrum 2", it->second, msg);
}
}
}
void SlackSession::sendMessage(boost::shared_ptr<Swift::Message> message) {
if (m_user) {
std::map<std::string, Conversation *> convs = m_user->getConversationManager()->getConversations();
for (std::map<std::string, Conversation *> ::const_iterator it = convs.begin(); it != convs.end(); it++) {
Conversation *conv = it->second;
if (!conv) {
continue;
}
if (conv->getNickname() == message->getFrom().getResource()) {
return;
}
}
}
std::string from = message->getFrom().getResource();
std::string channel = m_jid2channel[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(), m_user);
Buddy *b;
if (m_user && (b = m_user->getRosterManager()->getBuddy(from)) != NULL) {
from = b->getAlias() + " (" + from + ")";
}
}
LOG4CXX_INFO(logger, m_uinfo.jid << "Sending message to Slack channel " << channel << " from " << from);
#if HAVE_SWIFTEN_3
std::string body = message->getBody().get_value_or("");
#else
std::string body = message->getBody();
#endif
m_rtm->getAPI()->sendMessage(from, channel, body);
}
void SlackSession::setPurpose(const std::string &purpose, const std::string &channel) {
std::string ch = channel;
if (ch.empty()) {
ch = m_slackChannel;
}
if (ch.empty()) {
return;
}
LOG4CXX_INFO(logger, m_uinfo.jid << ": Setting channel purppose: " << ch << " " << purpose);
m_api->setPurpose(ch, purpose);
}
void SlackSession::handleJoinRoomCreated(const std::string &channelId, std::vector<std::string> args) {
args[5] = channelId;
std::string &name = args[2];
std::string &legacyRoom = args[3];
std::string &legacyServer = args[4];
std::string &slackChannel = args[5];
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);
// }
LOG4CXX_INFO(logger, m_uinfo.jid << ": Channel " << args[5] << " is created. Joining the room on legacy network.");
m_jid2channel[to] = slackChannel;
m_channel2jid[slackChannel] = to;
Swift::Presence::ref presence = Swift::Presence::create();
presence->setFrom(Swift::JID(m_uinfo.jid + "/default"));
presence->setTo(Swift::JID(to + "/" + name));
presence->setType(Swift::Presence::Available);
presence->addPayload(boost::shared_ptr<Swift::Payload>(new Swift::MUCPayload()));
m_component->getFrontend()->onPresenceReceived(presence);
m_onlineBuddiesTimer->start();
}
void SlackSession::handleJoinMessage(const std::string &message, std::vector<std::string> &args, bool quiet) {
LOG4CXX_INFO(logger, m_uinfo.jid << ": Going to join the room " << args[3] << "@" << args[4] << ", transporting it to channel " << args[5]);
m_api->createChannel(args[5], m_idManager->getSelfId(), boost::bind(&SlackSession::handleJoinRoomCreated, this, _1, args));
}
void SlackSession::handleSlackChannelCreated(const std::string &channelId) {
m_slackChannel = channelId;
LOG4CXX_INFO(logger, m_uinfo.jid << ": Main Slack Channel created, connecting the legacy network");
Swift::Presence::ref presence = Swift::Presence::create();
presence->setFrom(Swift::JID(m_uinfo.jid + "/default"));
presence->setTo(m_component->getJID());
presence->setType(Swift::Presence::Available);
presence->addPayload(boost::shared_ptr<Swift::Payload>(new Swift::MUCPayload()));
m_component->getFrontend()->onPresenceReceived(presence);
}
void SlackSession::leaveRoom(const std::string &channel) {
std::string channelId = m_idManager->getId(channel);
std::string to = m_channel2jid[channelId];
if (to.empty()) {
LOG4CXX_ERROR(logger, "Spectrum 2 is not configured to transport this Slack channel.")
return;
}
LOG4CXX_INFO(logger, m_uinfo.jid << ": Leaving the legacy network room " << to);
Swift::Presence::ref presence = Swift::Presence::create();
presence->setFrom(Swift::JID(m_uinfo.jid + "/default"));
presence->setTo(Swift::JID(to + "/" + m_uinfo.uin));
presence->setType(Swift::Presence::Unavailable);
presence->addPayload(boost::shared_ptr<Swift::Payload>(new Swift::MUCPayload()));
m_component->getFrontend()->onPresenceReceived(presence);
}
void SlackSession::handleMessageReceived(const std::string &channel, const std::string &user, const std::string &message, const std::string &ts, bool quiet) {
std::string to = m_channel2jid[channel];
if (m_idManager->getName(user) == m_idManager->getSelfName()) {
return;
}
if (!to.empty()) {
boost::shared_ptr<Swift::Message> msg(new Swift::Message());
msg->setType(Swift::Message::Groupchat);
msg->setTo(to);
msg->setFrom(Swift::JID(m_uinfo.jid + "/default"));
msg->setBody("<" + m_idManager->getName(user) + "> " + message);
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<Swift::Message> msg(new Swift::Message());
msg->setTo(b->getJID());
msg->setFrom(Swift::JID(m_uinfo.jid + "/default"));
msg->setBody("<" + m_idManager->getName(user) + "> " + message);
m_component->getFrontend()->onMessageReceived(msg);
}
}
}
void SlackSession::handleDisconnected() {
m_disconnected = true;
}
void SlackSession::setUser(User *user) {
m_user = user;
}
void SlackSession::handleConnected() {
if (m_disconnected) {
handleRTMStarted();
m_disconnected = false;
}
}
void SlackSession::handleRTMStarted() {
std::string rooms = "";
int type = (int) TYPE_STRING;
m_storageBackend->getUserSetting(m_uinfo.id, "rooms", type, rooms);
m_storageBackend->getUserSetting(m_uinfo.id, "slack_channel", type, m_slackChannel);
if (!m_slackChannel.empty()) {
m_api->createChannel(m_slackChannel, m_idManager->getSelfId(), boost::bind(&SlackSession::handleSlackChannelCreated, this, _1));
}
else {
LOG4CXX_WARN(logger, m_uinfo.jid << ": There is no Main Slack Channel set for this user.");
}
// Auto-join the rooms configured by the Slack channel owner.
if (!rooms.empty()) {
std::vector<std::string> commands;
boost::split(commands, rooms, boost::is_any_of("\n"));
BOOST_FOREACH(const std::string &command, commands) {
if (command.size() > 5) {
std::vector<std::string> args;
boost::split(args, command, boost::is_any_of(" "));
handleJoinMessage("", args, false);
}
}
}
}
}
|