Changeset - f6d1844943c8
[Not reviewed]
0 2 0
Jan Kaluza - 9 years ago 2016-01-29 14:34:07
jkaluza@redhat.com
Slack: remove unused code
2 files changed with 48 insertions and 197 deletions:
0 comments (0 inline, 0 general)
spectrum/src/frontends/slack/SlackSession.cpp
Show inline comments
 
@@ -203,166 +203,75 @@ void SlackSession::handleSlackChannelCreated(const std::string &channelId) {
 
}
 

	
 
void SlackSession::leaveRoom(const std::string &channel) {
 
	std::string channelId = m_idManager->getId(channel);
 
	std::string to = m_channel2jid[channel];
 
	if (to.empty()) {
 
		m_rtm->sendMessage(m_ownerChannel, "Spectrum 2 is not configured to transport this Slack channel.");
 
		LOG4CXX_ERROR(logger, "Spectrum 2 is not configured to transport this Slack channel.")
 
		return;
 
	}
 

	
 
	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::handleRegisterMessage(const std::string &message, std::vector<std::string> &args, bool quiet) {
 
	// .spectrum2 register test@xmpp.tld mypassword #slack_channel
 
	m_uinfo.uin = SlackAPI::SlackObjectToPlainText(args[2]);
 
	m_uinfo.password = args[3];
 
	std::string slackChannel = SlackAPI::SlackObjectToPlainText(args[4], true);
 

	
 
	m_storageBackend->setUser(m_uinfo);
 
	int type = (int) TYPE_STRING;
 
	m_storageBackend->getUserSetting(m_uinfo.id, "slack_channel", type, slackChannel);
 

	
 
	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);
 

	
 
	if (!quiet) {
 
		std::string msg;
 
		msg += "You have successfully registered 3rd-party account. Spectrum 2 is now connecting to the 3rd-party network.";
 
		m_rtm->sendMessage(m_ownerChannel, msg);
 
	}
 
}
 

	
 
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_idManager->getName(user) == m_idManager->getSelfName()) {
 
			return;
 
		}
 
	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 (!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
 
		// 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);
 
			}
 
		if (!m_user || !m_user->getRosterManager()) {
 
			return;
 
		}
 
		return;
 
	}
 

	
 
	std::vector<std::string> args;
 
	boost::split(args, message, boost::is_any_of(" "));
 
		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 (args.size() < 2 || args[0] != ".spectrum2") {
 
		m_rtm->sendMessage(m_ownerChannel, "Unknown command. Use \".spectrum2 help\" for help.");
 
		return;
 
	}
 
			if (!(b->getStatus(s, statusMessage))) {
 
				continue;
 
			}
 

	
 
	if (args[1] == "join.room" && args.size() == 6) {
 
		handleJoinMessage(message, args, quiet);
 
	}
 
	else if (args[1] == "leave.room" && args.size() == 3) {
 
// 		handleLeaveMessage(message, args, quiet);
 
	}
 
	else if (args[1] == "register" && args.size() == 5) {
 
		handleRegisterMessage(message, args, quiet);
 
	}
 
	else if (args[1] == "set_main_channel" && args.size() == 3) {
 
		std::string slackChannel = SlackAPI::SlackObjectToPlainText(args[2], true);
 
			if (s.getType() == Swift::StatusShow::None) {
 
				continue;
 
			}
 

	
 
		m_storageBackend->setUser(m_uinfo);
 
		int type = (int) TYPE_STRING;
 
		m_storageBackend->getUserSetting(m_uinfo.id, "slack_channel", type, slackChannel);
 

	
 
		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);
 
	}
 
	else if (args[1] == "list.rooms" && args.size() == 2) {
 
		// .spectrum2 list.rooms
 
		std::string rooms = "";
 
		int type = (int) TYPE_STRING;
 
		m_storageBackend->getUserSetting(m_uinfo.id, "rooms", type, rooms);
 

	
 
		std::string msg = "Spectrum 2 is configured for following channels:\\n";
 
		msg += "```" + rooms  + "```";
 
		m_rtm->sendMessage(m_ownerChannel, msg);
 
	}
 
	else if (args[1] == "reconnect") {
 
		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);
 
	}
 
	else if (args[1] == "help") {
 
		std::string msg;
 
		msg =  "Following commands are supported:\\n";
 
		msg += "```.spectrum2 help``` Shows this help message.\\n";
 
		msg += "```.spectrum2 join.room <3rdPartyBotName> <#3rdPartyRoom> <3rdPartyServer> <#SlackChannel>``` Starts transport between 3rd-party room and Slack channel.";
 
		msg += "```.spectrum2 leave.room <#SlackChannel>``` Leaves the 3rd-party room connected with the given Slack channel.";
 
		msg += "```.spectrum2 list.rooms``` List all the transported rooms.";
 
		msg += "```.spectrum2 reconnect``` Reconnects to 3rd-party network manually in case of fatal error.";
 
		msg += "```.spectrum2 register <3rdPartyNetworkAccount> <3rdPartyPassword> <#SlackChannel>``` Registers 3rd-party account for transportation.";
 
		m_rtm->sendMessage(m_ownerChannel, msg);
 
			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);
 
		}
 
	}
 
// 	else {
 
// 		m_rtm->sendMessage(m_ownerChannel, "Unknown command. Use `.spectrum2 help` for help.");
 
// 	}
 
}
 

	
 
void SlackSession::handleDisconnected() {
 
	m_disconnected = true;
 
}
 

	
 
@@ -370,93 +279,38 @@ void SlackSession::setUser(User *user) {
 
	m_user = user;
 
}
 

	
 

	
 
void SlackSession::handleConnected() {
 
	if (m_disconnected) {
 
		m_rtm->getAPI()->imOpen(m_ownerId, boost::bind(&SlackSession::handleImOpen, this, _1, _2, _3, _4));
 
		handleRTMStarted();
 
		m_disconnected = false;
 
	}
 
}
 

	
 
void SlackSession::handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
	m_ownerChannel = m_rtm->getAPI()->getChannelId(req, ok, resp, data);
 
	LOG4CXX_INFO(logger, "Opened channel with team owner: " << m_ownerChannel);
 

	
 
void SlackSession::handleRTMStarted() {
 
	std::string rooms = "";
 
	int type = (int) TYPE_STRING;
 
	m_storageBackend->getUserSetting(m_uinfo.id, "rooms", type, rooms);
 

	
 
	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 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);
 
		}
 
	}
 
	else {
 
		if (m_uinfo.uin.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 += "To get full list of available commands, executa `.spectrum2 help`\\n";
 
			m_rtm->sendMessage(m_ownerChannel, msg);
 
		}
 
		else {
 
			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 {
 
				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 set_main_channel #SlackChannel```\\n";
 
				msg += "To get full list of available commands, executa `.spectrum2 help`\\n";
 
				m_rtm->sendMessage(m_ownerChannel, msg);
 
			}
 
		}
 
	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));
 
	}
 

	
 
	// 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) {
 
				LOG4CXX_INFO(logger, m_uinfo.jid << ": Sending command from storage: " << command);
 
				handleMessageReceived(m_ownerChannel, "owner", command, "", true);
 
				std::vector<std::string> args;
 
				boost::split(args, command, boost::is_any_of(" "));
 
				handleJoinMessage("", args, false);
 
			}
 
		}
 
	}
 
}
 

	
 
void SlackSession::handleRTMStarted() {
 
	std::map<std::string, SlackUserInfo> &users = m_idManager->getUsers();
 
	for (std::map<std::string, SlackUserInfo>::iterator it = users.begin(); it != users.end(); it++) {
 
		SlackUserInfo &info = it->second;
 
		if (info.isPrimaryOwner) {
 
			m_ownerId = it->first;
 
			break;
 
		}
 
	}
 

	
 
	m_rtm->getAPI()->imOpen(m_ownerId, boost::bind(&SlackSession::handleImOpen, this, _1, _2, _3, _4));
 
}
 

	
 

	
 
}
spectrum/src/frontends/slack/SlackSession.h
Show inline comments
 
@@ -65,26 +65,23 @@ class SlackSession {
 
		void leaveRoom(const std::string &channel);
 
		void handleRegisterMessage(const std::string &message, std::vector<std::string> &args, bool quiet = false);
 

	
 
	private:
 
		void handleRTMStarted();
 
		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 handleJoinRoomCreated(const std::string &channelId, std::vector<std::string> args);
 
		void handleSlackChannelCreated(const std::string &channelId);
 

	
 
		void sendOnlineBuddies();
 

	
 
	private:
 
		Component *m_component;
 
		StorageBackend *m_storageBackend;
 
		UserInfo m_uinfo;
 
		std::string m_ownerName;
 
		SlackRTM *m_rtm;
 
		std::string m_ownerChannel;
 
		std::map<std::string, std::string> m_jid2channel;
 
		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;
0 comments (0 inline, 0 general)