Changeset - 24d902983a17
[Not reviewed]
0 8 0
Jan Kaluza - 10 years ago 2015-12-07 18:17:04
jkaluza@redhat.com
Send list of online users to Slack channel when transporting to 3rd-party network room.
8 files changed with 123 insertions and 15 deletions:
0 comments (0 inline, 0 general)
backends/libpurple/main.cpp
Show inline comments
 
@@ -670,7 +670,11 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 
				comps = purple_chat_get_components_wrapped(chat);
 
			}
 
			else if (PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl)->chat_info_defaults != NULL) {
 
				comps = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl)->chat_info_defaults(gc, room.c_str());
 
				if (CONFIG_STRING(config, "service.protocol") == "prpl-jabber") {
 
					comps = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl)->chat_info_defaults(gc, (room + "/" + nickname).c_str());
 
				} else {
 
					comps = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl)->chat_info_defaults(gc, room.c_str());
 
				}
 
			}
 

	
 
			LOG4CXX_INFO(logger, user << ": Joining the room " << room);
include/transport/Conversation.h
Show inline comments
 
@@ -146,6 +146,7 @@ class Conversation {
 

	
 
		void destroyRoom();
 

	
 
		std::string getParticipants();
 
		void sendParticipants(const Swift::JID &to);
 

	
 
		void sendCachedMessages(const Swift::JID &to = Swift::JID());
include/transport/ConversationManager.h
Show inline comments
 
@@ -78,6 +78,10 @@ class ConversationManager {
 
		void removeJID(const Swift::JID &jid);
 
		void clearJIDs();
 

	
 
		std::map<std::string, Conversation *> &getConversations() {
 
			return m_convs;
 
		}
 

	
 
	private:
 
		void handleMessageReceived(Swift::Message::ref message);
 

	
spectrum/src/frontends/slack/SlackRTM.cpp
Show inline comments
 
@@ -110,6 +110,14 @@ void SlackRTM::sendPing() {
 
	m_pingTimer->start();
 
}
 

	
 
const std::string &SlackRTM::getUserName(const std::string &id) {
 
	if (m_users.find(id) == m_users.end()) {
 
		return id;
 
	}
 

	
 
	return m_users[id].name;
 
}
 

	
 
void SlackRTM::handleRTMStart(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
	if (!ok) {
 
		LOG4CXX_ERROR(logger, req->getError());
 
@@ -124,6 +132,22 @@ void SlackRTM::handleRTMStart(HTTPRequest *req, bool ok, rapidjson::Document &re
 
		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_selfName = selfName.GetString();
 

	
 
	SlackAPI::getSlackChannelInfo(req, ok, resp, data, m_channels);
 
	SlackAPI::getSlackImInfo(req, ok, resp, data, m_ims);
 
	SlackAPI::getSlackUserInfo(req, ok, resp, data, m_users);
spectrum/src/frontends/slack/SlackRTM.h
Show inline comments
 
@@ -82,6 +82,12 @@ class SlackRTM {
 

	
 
		boost::signal<void (const std::string &channel, const std::string &user, const std::string &text, const std::string &ts)> onMessageReceived;
 

	
 
		const std::string &getUserName(const std::string &id);
 

	
 
		const std::string &getSelfName() {
 
			return m_selfName;
 
		}
 

	
 
	private:
 
		void handlePayloadReceived(const std::string &payload);
 
		void handleRTMStart(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data);
 
@@ -91,6 +97,7 @@ class SlackRTM {
 
		std::map<std::string, SlackChannelInfo> m_channels;
 
		std::map<std::string, SlackImInfo> m_ims;
 
		std::map<std::string, SlackUserInfo> m_users;
 
		std::string m_selfName;
 

	
 
	private:
 
		Component *m_component;
spectrum/src/frontends/slack/SlackSession.cpp
Show inline comments
 
@@ -29,6 +29,8 @@
 
#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>
 
@@ -52,16 +54,57 @@ SlackSession::SlackSession(Component *component, StorageBackend *storageBackend,
 
	m_rtm->onRTMStarted.connect(boost::bind(&SlackSession::handleRTMStarted, this));
 
	m_rtm->onMessageReceived.connect(boost::bind(&SlackSession::handleMessageReceived, this, _1, _2, _3, _4, false));
 

	
 
	m_onlineBuddiesTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(20000);
 
	m_onlineBuddiesTimer->onTick.connect(boost::bind(&SlackSession::sendOnlineBuddies, this));
 
}
 

	
 
SlackSession::~SlackSession() {
 
	delete m_rtm;
 
	m_onlineBuddiesTimer->stop();
 
}
 

	
 
void SlackSession::sendMessage(boost::shared_ptr<Swift::Message> message) {
 
	if (message->getFrom().getResource() == m_uinfo.uin) {
 
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::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()];
 
@@ -84,13 +127,17 @@ void SlackSession::sendMessage(boost::shared_ptr<Swift::Message> message) {
 
	m_rtm->getAPI()->sendMessage(from, channel, message->getBody());
 
}
 

	
 
void SlackSession::setPurpose(const std::string &purpose) {
 
	if (m_slackChannel.empty()) {
 
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, "Setting channel purppose: " << m_slackChannel << " " << purpose);
 
	m_rtm->getAPI()->setPurpose(m_slackChannel, purpose);
 
	LOG4CXX_INFO(logger, "Setting channel purppose: " << ch << " " << purpose);
 
	m_rtm->getAPI()->setPurpose(ch, purpose);
 
}
 

	
 
void SlackSession::handleJoinMessage(const std::string &message, std::vector<std::string> &args, bool quiet) {
 
@@ -114,11 +161,13 @@ void SlackSession::handleJoinMessage(const std::string &message, std::vector<std
 

	
 
	LOG4CXX_INFO(logger, "Setting transport between " << to << " and " << slackChannel);
 

	
 
	std::string rooms = "";
 
	int type = (int) TYPE_STRING;
 
	m_storageBackend->getUserSetting(m_uinfo.id, "rooms", type, rooms);
 
	rooms += message + "\n";
 
	m_storageBackend->updateUserSetting(m_uinfo.id, "rooms", rooms);
 
	if (!quiet) {
 
		std::string rooms = "";
 
		int type = (int) TYPE_STRING;
 
		m_storageBackend->getUserSetting(m_uinfo.id, "rooms", type, rooms);
 
		rooms += message + "\n";
 
		m_storageBackend->updateUserSetting(m_uinfo.id, "rooms", rooms);
 
	}
 

	
 
	Swift::Presence::ref presence = Swift::Presence::create();
 
	presence->setFrom(Swift::JID("", m_uinfo.jid, "default"));
 
@@ -127,6 +176,8 @@ void SlackSession::handleJoinMessage(const std::string &message, std::vector<std
 
	presence->addPayload(boost::shared_ptr<Swift::Payload>(new Swift::MUCPayload()));
 
	m_component->getFrontend()->onPresenceReceived(presence);
 

	
 
	m_onlineBuddiesTimer->start();
 

	
 
	if (!quiet) {
 
		std::string msg;
 
		msg += "Spectrum 2 is now joining the room. To leave the room later to disable transporting, you can use `.spectrum2 leave.room #" + SlackAPI::SlackObjectToPlainText(args[5], true, true) + "`.";
 
@@ -200,12 +251,16 @@ void SlackSession::handleRegisterMessage(const std::string &message, std::vector
 
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 (m_rtm->getUserName(user) == m_rtm->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("<" + user + "> " + message);
 
			msg->setBody("<" + m_rtm->getUserName(user) + "> " + message);
 
			m_component->getFrontend()->onMessageReceived(msg);
 
		}
 
		else {
 
@@ -241,7 +296,7 @@ void SlackSession::handleMessageReceived(const std::string &channel, const std::
 
				boost::shared_ptr<Swift::Message> msg(new Swift::Message());
 
				msg->setTo(b->getJID());
 
				msg->setFrom(Swift::JID("", m_uinfo.jid, "default"));
 
				msg->setBody("<" + user + "> " + message);
 
				msg->setBody("<" + m_rtm->getUserName(user) + "> " + message);
 
				m_component->getFrontend()->onMessageReceived(msg);
 
			}
 
		}
spectrum/src/frontends/slack/SlackSession.h
Show inline comments
 
@@ -28,6 +28,7 @@
 
#include <map>
 

	
 
#include "Swiften/Elements/Message.h"
 
#include "Swiften/Network/Timer.h"
 

	
 
#include <boost/signal.hpp>
 

	
 
@@ -50,7 +51,7 @@ class SlackSession {
 

	
 
		void sendMessage(boost::shared_ptr<Swift::Message> message);
 

	
 
		void setPurpose(const std::string &purpose);
 
		void setPurpose(const std::string &purpose, const std::string &channel = "");
 

	
 
		void setUser(User *user) {
 
			m_user = user;
 
@@ -65,6 +66,8 @@ class SlackSession {
 
		void handleLeaveMessage(const std::string &message, std::vector<std::string> &args, bool quiet = false);
 
		void handleRegisterMessage(const std::string &message, std::vector<std::string> &args, bool quiet = false);
 

	
 
		void sendOnlineBuddies();
 

	
 
	private:
 
		Component *m_component;
 
		StorageBackend *m_storageBackend;
 
@@ -76,6 +79,8 @@ class SlackSession {
 
		std::map<std::string, std::string> m_channel2jid;
 
		std::string m_slackChannel;
 
		User *m_user;
 
		Swift::Timer::ref m_onlineBuddiesTimer;
 
		std::map<std::string, std::string> m_onlineBuddies;
 
};
 

	
 
}
src/Conversation.cpp
Show inline comments
 
@@ -204,6 +204,14 @@ void Conversation::handleMessage(boost::shared_ptr<Swift::Message> &message, con
 
	handleRawMessage(message);
 
}
 

	
 
std::string Conversation::getParticipants() {
 
	std::string ret;
 
	for (std::map<std::string, Swift::Presence::ref>::iterator it = m_participants.begin(); it != m_participants.end(); it++) {
 
		ret += (*it).second->getFrom().getResource() + ", ";
 
	}
 
	return ret;
 
}
 

	
 
void Conversation::sendParticipants(const Swift::JID &to) {
 
	for (std::map<std::string, Swift::Presence::ref>::iterator it = m_participants.begin(); it != m_participants.end(); it++) {
 
		(*it).second->setTo(to);
0 comments (0 inline, 0 general)