Changeset - 384d4bfcd0c1
[Not reviewed]
0 4 0
Jan Kaluza - 10 years ago 2016-01-27 15:01:39
jkaluza@redhat.com
Create main slack channel after registering to Slack
4 files changed with 47 insertions and 9 deletions:
0 comments (0 inline, 0 general)
spectrum/src/frontends/slack/SlackFrontend.cpp
Show inline comments
 
@@ -114,24 +114,24 @@ void SlackFrontend::connectToServer() {
 
	m_transport->handleConnected();
 
}
 

	
 
std::string SlackFrontend::setOAuth2Code(const std::string &code, const std::string &state) {
 
	return static_cast<SlackUserManager *>(m_userManager)->handleOAuth2Code(code, state);
 
}
 

	
 
std::string SlackFrontend::getOAuth2URL(const std::vector<std::string> &args) {
 
	return static_cast<SlackUserManager *>(m_userManager)->getOAuth2URL(args);
 
}
 

	
 
std::string SlackFrontend::getRegistrationFields() {
 
	return "Slack team name\n3rd-party network username\n3rd-party network password";
 
	return "Main Slack channel\n3rd-party network username\n3rd-party network password";
 
}
 

	
 
bool SlackFrontend::handleAdminMessage(Swift::Message::ref message) {
 
	return static_cast<SlackUserManager *>(m_userManager)->handleAdminMessage(message);
 
}
 

	
 
void SlackFrontend::disconnectFromServer() {
 

	
 
}
 

	
 
}
spectrum/src/frontends/slack/SlackSession.cpp
Show inline comments
 
@@ -206,24 +206,58 @@ void SlackSession::handleJoinRoomList(HTTPRequest *req, bool ok, rapidjson::Docu
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, args[1] << ": Channel " << args[5] << " does not exit. Creating it.");
 
		m_api->channelsCreate(args[5], boost::bind(&SlackSession::handleJoinRoomCreate, this, _1, _2, _3, _4, args));
 
	}
 
}
 

	
 
void SlackSession::handleJoinMessage(const std::string &message, std::vector<std::string> &args, bool quiet) {
 
	LOG4CXX_INFO(logger, args[1] << ": Going to join the room, checking the ID of channel " << args[5]);
 
	m_api->channelsList(boost::bind(&SlackSession::handleJoinRoomList, this, _1, _2, _3, _4, args));
 
}
 

	
 
void SlackSession::handleSlackChannelCreate(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
	std::string channelId = m_api->getChannelId(req, ok, resp, data);
 
	if (channelId.empty()) {
 
		LOG4CXX_INFO(logger,"Error creating channel " << m_slackChannel << ".");
 
		return;
 
	}
 

	
 
	m_slackChannel = channelId;
 
	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::handleSlackChannelList(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
	std::map<std::string, SlackChannelInfo> channels;
 
	SlackAPI::getSlackChannelInfo(req, ok, resp, data, channels);
 

	
 
	if (channels.find(m_slackChannel) != channels.end()) {
 
		m_slackChannel = channels[m_slackChannel].id;
 
		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 {
 
		m_api->channelsCreate(m_slackChannel, boost::bind(&SlackSession::handleSlackChannelCreate, this, _1, _2, _3, _4));
 
	}
 
}
 

	
 
void SlackSession::handleLeaveMessage(const std::string &message, std::vector<std::string> &args, bool quiet) {
 
	// .spectrum2 leave.room channel
 
	std::string slackChannel = SlackAPI::SlackObjectToPlainText(args[2], true);
 
	std::string to = m_channel2jid[slackChannel];
 
	if (to.empty()) {
 
		m_rtm->sendMessage(m_ownerChannel, "Spectrum 2 is not configured to transport this Slack channel.");
 
		return;
 
	}
 

	
 
	std::string rooms = "";
 
	int type = (int) TYPE_STRING;
 
	m_storageBackend->getUserSetting(m_uinfo.id, "rooms", type, rooms);
 
@@ -443,30 +477,25 @@ 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 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()) {
 
				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);
 
				m_api->channelsList(boost::bind(&SlackSession::handleSlackChannelList, this, _1, _2, _3, _4));
 
			}
 
			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);
 
			}
 
		}
spectrum/src/frontends/slack/SlackSession.h
Show inline comments
 
@@ -64,24 +64,27 @@ 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);
 

	
 
	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 joinRoom(std::vector<std::string> args);
 
		void handleJoinRoomCreate(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::vector<std::string> args);
 
		void handleJoinRoomList(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::vector<std::string> args);
 

	
 
		void handleSlackChannelCreate(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data);
 
		void handleSlackChannelList(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data);
 

	
 
		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;
spectrum/src/frontends/slack/SlackUserRegistration.cpp
Show inline comments
 
@@ -103,24 +103,26 @@ std::string SlackUserRegistration::getTeamDomain(const std::string &token) {
 
		LOG4CXX_ERROR(logger, req.getRawData());
 
		return "";
 
	}
 

	
 
	return domain.GetString();
 
}
 

	
 
std::string SlackUserRegistration::handleOAuth2Code(const std::string &code, const std::string &state) {
 
	OAuth2 *oauth2 = NULL;
 
	std::string token;
 
	std::string access_token;
 
	std::vector<std::string> data;
 
	std::string value;
 
	int type = (int) TYPE_STRING;
 

	
 
	if (state == "use_bot_token") {
 
		token = code;
 
		access_token = code;
 
	}
 
	else {
 
		if (m_auths.find(state) != m_auths.end()) {
 
			oauth2 = m_auths[state];
 
			data = m_authsData[state];
 
		}
 
		else {
 
			return "Received state code '" + state + "' not found in state codes list.";
 
@@ -139,35 +141,39 @@ std::string SlackUserRegistration::handleOAuth2Code(const std::string &code, con
 

	
 
	std::string domain = getTeamDomain(access_token);
 
	if (domain.empty()) {
 
		return "The token you have provided is invalid";
 
	}
 

	
 
	UserInfo user;
 
	user.uin = "";
 
	user.password = "";
 
	user.id = 0;
 
	m_storageBackend->getUser(domain, user);
 

	
 
	value = user.jid;
 
	user.jid = domain;
 
	user.language = "en";
 
	user.encoding = "";
 
	user.vip = 0;
 

	
 
	registerUser(user, true);
 

	
 
	m_storageBackend->getUser(user.jid, user);
 

	
 
	std::string value = token;
 
	int type = (int) TYPE_STRING;
 
	if (!value.empty()) {
 
		m_storageBackend->getUserSetting(user.id, "slack_channel", type, value);
 
	}
 

	
 
	value = token;
 
	m_storageBackend->getUserSetting(user.id, "bot_token", type, value);
 

	
 
	value = access_token;
 
	m_storageBackend->getUserSetting(user.id, "access_token", type, value);
 

	
 
	LOG4CXX_INFO(logger, "Registered Slack user " << user.jid);
 

	
 
	if (oauth2) {
 
		m_auths.erase(state);
 
		delete oauth2;
 

	
 
		m_authsData.erase(state);
0 comments (0 inline, 0 general)