Changeset - e0ea6762c78c
[Not reviewed]
2 4 2
Jan Kaluza - 10 years ago 2015-11-25 19:20:19
jkaluza@redhat.com
Rename SlackInstallation to SlackSession. Allow SlackUser to move SlackSession from SlackUserManager once the SlackUser is created.
6 files changed with 51 insertions and 34 deletions:
0 comments (0 inline, 0 general)
spectrum/src/frontends/slack/SlackSession.cpp
Show inline comments
 
file renamed from spectrum/src/frontends/slack/SlackInstallation.cpp to spectrum/src/frontends/slack/SlackSession.cpp
 
@@ -15,13 +15,13 @@
 
 *
 
 * 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 "SlackInstallation.h"
 
#include "SlackSession.h"
 
#include "SlackFrontend.h"
 
#include "SlackUser.h"
 
#include "SlackRTM.h"
 

	
 
#include "transport/Transport.h"
 
#include "transport/HTTPRequest.h"
 
@@ -37,39 +37,39 @@
 

	
 
#include <map>
 
#include <iterator>
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "SlackInstallation");
 
DEFINE_LOGGER(logger, "SlackSession");
 

	
 
SlackInstallation::SlackInstallation(Component *component, StorageBackend *storageBackend, UserInfo uinfo) : m_uinfo(uinfo) {
 
SlackSession::SlackSession(Component *component, StorageBackend *storageBackend, UserInfo uinfo) : m_uinfo(uinfo) {
 
	m_component = component;
 
	m_storageBackend = storageBackend;
 

	
 
	m_rtm = new SlackRTM(component, storageBackend, uinfo);
 
	m_rtm->onRTMStarted.connect(boost::bind(&SlackInstallation::handleRTMStarted, this));
 
	m_rtm->onMessageReceived.connect(boost::bind(&SlackInstallation::handleMessageReceived, this, _1, _2, _3));
 
	m_rtm->onRTMStarted.connect(boost::bind(&SlackSession::handleRTMStarted, this));
 
	m_rtm->onMessageReceived.connect(boost::bind(&SlackSession::handleMessageReceived, this, _1, _2, _3));
 

	
 
// 	m_api = new SlackAPI(component, m_uinfo.encoding);
 
}
 

	
 
SlackInstallation::~SlackInstallation() {
 
SlackSession::~SlackSession() {
 
	delete m_rtm;
 
// 	delete m_api;
 
}
 

	
 
void SlackInstallation::sendMessage(boost::shared_ptr<Swift::Message> message) {
 
void SlackSession::sendMessage(boost::shared_ptr<Swift::Message> message) {
 
	LOG4CXX_INFO(logger, "SEND MESSAGE");
 
	if (message->getFrom().getResource() == "myfavouritebot") {
 
		return;
 
	}
 
	m_rtm->getAPI()->sendMessage(message->getFrom().getResource(), m_jid2channel[message->getFrom().toBare().toString()], message->getBody());
 
}
 

	
 
void SlackInstallation::handleMessageReceived(const std::string &channel, const std::string &user, const std::string &message) {
 
void SlackSession::handleMessageReceived(const std::string &channel, const std::string &user, const std::string &message) {
 
	if (m_ownerChannel != channel) {
 
		std::string to = m_channel2jid[channel];
 
		if (!to.empty()) {
 
			boost::shared_ptr<Swift::Message> msg(new Swift::Message());
 
			msg->setType(Swift::Message::Groupchat);
 
			msg->setTo(to);
 
@@ -128,34 +128,34 @@ void SlackInstallation::handleMessageReceived(const std::string &channel, const
 
	}
 
	else {
 
		m_rtm->sendMessage(m_ownerChannel, "Unknown command. Use \".spectrum2 help\" for help.");
 
	}
 
}
 

	
 
void SlackInstallation::handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
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);
 

	
 
	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.";
 
	m_rtm->sendMessage(m_ownerChannel, msg);
 

	
 
	msg = "To configure IRC network you want to connect to, type: \".spectrum2 register <bot_name>@<ircnetwork>\". For example for Freenode, the command looks like \".spectrum2 register MySlackBot@irc.freenode.net\".";
 
	m_rtm->sendMessage(m_ownerChannel, msg);
 
}
 

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

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

	
 

	
 
}
spectrum/src/frontends/slack/SlackSession.h
Show inline comments
 
file renamed from spectrum/src/frontends/slack/SlackInstallation.h to spectrum/src/frontends/slack/SlackSession.h
 
@@ -36,17 +36,17 @@ namespace Transport {
 
class Component;
 
class StorageBackend;
 
class HTTPRequest;
 
class SlackRTM;
 
class SlackAPI;
 

	
 
class SlackInstallation {
 
class SlackSession {
 
	public:
 
		SlackInstallation(Component *component, StorageBackend *storageBackend, UserInfo uinfo);
 
		SlackSession(Component *component, StorageBackend *storageBackend, UserInfo uinfo);
 

	
 
		virtual ~SlackInstallation();
 
		virtual ~SlackSession();
 

	
 
		boost::signal<void (const std::string &user)> onInstallationDone;
 

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

	
 
	private:
spectrum/src/frontends/slack/SlackUser.cpp
Show inline comments
 
@@ -17,12 +17,14 @@
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#include "SlackUser.h"
 
#include "SlackFrontend.h"
 
#include "SlackSession.h"
 
#include "SlackUserManager.h"
 

	
 
#include "transport/Transport.h"
 
#include "transport/UserManager.h"
 
#include "transport/Logging.h"
 

	
 
#include <boost/foreach.hpp>
 
@@ -38,15 +40,20 @@ DEFINE_LOGGER(logger, "SlackUser");
 
SlackUser::SlackUser(const Swift::JID &jid, UserInfo &userInfo, Component *component, UserManager *userManager) : User(jid, userInfo, component, userManager) {
 
	m_jid = jid.toBare();
 

	
 
	m_component = component;
 
	m_userManager = userManager;
 
	m_userInfo = userInfo;
 

	
 
	m_session = static_cast<SlackUserManager *>(userManager)->moveTempSession(m_jid.toString());
 
}
 

	
 
SlackUser::~SlackUser(){
 
	if (m_session) {
 
		delete m_session;
 
	}
 
}
 

	
 
void SlackUser::disconnectUser(const std::string &error, Swift::SpectrumErrorPayload::Error e) {
 

	
 
}
 

	
spectrum/src/frontends/slack/SlackUser.h
Show inline comments
 
@@ -29,23 +29,29 @@ namespace Transport {
 
class Component;
 
class RosterManager;
 
class ConversationManager;
 
class UserManager;
 
class PresenceOracle;
 
struct UserInfo;
 
class SlackSession;
 

	
 
class SlackUser : public User {
 
	public:
 
		SlackUser(const Swift::JID &jid, UserInfo &userInfo, Component * component, UserManager *userManager);
 

	
 
		virtual ~SlackUser();
 

	
 
		void disconnectUser(const std::string &error, Swift::SpectrumErrorPayload::Error e);
 

	
 
		SlackSession *getSession() {
 
			return m_session;
 
		}
 

	
 
	private:
 
		Swift::JID m_jid;
 
		Component *m_component;
 
		UserManager *m_userManager;
 
		UserInfo m_userInfo;
 
		SlackSession *m_session;
 
};
 

	
 
}
spectrum/src/frontends/slack/SlackUserManager.cpp
Show inline comments
 
@@ -18,13 +18,14 @@
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#include "SlackUserManager.h"
 
#include "SlackUserRegistration.h"
 
#include "SlackFrontend.h"
 
#include "SlackInstallation.h"
 
#include "SlackSession.h"
 
#include "SlackUser.h"
 

	
 
#include "transport/User.h"
 
#include "transport/Transport.h"
 
#include "transport/StorageBackend.h"
 
#include "transport/Logging.h"
 

	
 
@@ -46,36 +47,37 @@ void SlackUserManager::reconnectUser(const std::string &user) {
 
	UserInfo uinfo;
 
	if (!m_storageBackend->getUser(user, uinfo)) {
 
		LOG4CXX_ERROR(logger, "User " << user << " tried to reconnect, but he's not registered.");
 
		return;
 
	}
 

	
 
// 	if (!uinfo.uin.empty()) {
 
// 		LOG4CXX_INFO(logger, "Reconnecting user " << user);
 
// 		Swift::Presence::ref response = Swift::Presence::create();
 
// 		response->setTo(m_component->getJID());
 
// 		response->setFrom(user + "@" + m_component->getJID().toString());
 
// 		response->setType(Swift::Presence::Available);
 
// 	}
 
// 	else {
 
		LOG4CXX_INFO(logger, "Cannot reconnect user " << user << ","
 
			"because he does not have legacy network configured. "
 
			"Continuing in Installation mode for this user until "
 
			"he configures the legacy network.");
 
		m_installations[user] = new SlackInstallation(m_component, m_storageBackend, uinfo);
 
		m_installations[user]->onInstallationDone.connect(boost::bind(&SlackUserManager::reconnectUser, this, _1));
 
// 	}
 
	LOG4CXX_INFO(logger, "Connecting user " << user << " to Slack network.");
 
	m_tempSessions[user] = new SlackSession(m_component, m_storageBackend, uinfo);
 
}
 

	
 
void SlackUserManager::sendVCard(unsigned int id, Swift::VCard::ref vcard) {
 

	
 
}
 

	
 
void SlackUserManager::sendMessage(boost::shared_ptr<Swift::Message> message) {
 
	LOG4CXX_INFO(logger, message->getTo().toBare().toString());
 
	m_installations[message->getTo().toBare().toString()]->sendMessage(message);
 
	User *user = getUser(message->getTo().toBare().toString());
 
	if (!user) {
 
		LOG4CXX_ERROR(logger, "Received message for unknown user " << message->getTo().toBare().toString());
 
		return;
 
	}
 

	
 
	static_cast<SlackUser *>(user)->getSession()->sendMessage(message);
 
}
 

	
 
SlackSession *SlackUserManager::moveTempSession(const std::string &user) {
 
	if (m_tempSessions.find(user) != m_tempSessions.end()) {
 
		SlackSession *session = m_tempSessions[user];
 
		m_tempSessions.erase(user);
 
		return session;
 
	}
 
	return NULL;
 
}
 

	
 

	
 
UserRegistration *SlackUserManager::getUserRegistration() {
 
	return m_userRegistration;
 
}
spectrum/src/frontends/slack/SlackUserManager.h
Show inline comments
 
@@ -36,13 +36,13 @@ class StorageBackend;
 
class StorageResponder;
 
class VCardResponder;
 
class XMPPUserRegistration;
 
class GatewayResponder;
 
class AdHocManager;
 
class SettingsAdHocCommandFactory;
 
class SlackInstallation;
 
class SlackSession;
 

	
 
class SlackUserManager : public UserManager {
 
	public:
 
		SlackUserManager(Component *component, UserRegistry *userRegistry, StorageBackend *storageBackend = NULL);
 

	
 
		virtual ~SlackUserManager();
 
@@ -56,14 +56,16 @@ class SlackUserManager : public UserManager {
 
		std::string handleOAuth2Code(const std::string &code, const std::string &state);
 

	
 
		std::string getOAuth2URL(const std::vector<std::string> &args);
 

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

	
 
		SlackSession *moveTempSession(const std::string &user);
 

	
 
	private:
 
		Component *m_component;
 
		UserRegistration *m_userRegistration;
 
		StorageBackend *m_storageBackend;
 
		std::map<std::string, SlackInstallation *> m_installations;
 
		std::map<std::string, SlackSession *> m_tempSessions;
 
};
 

	
 
}
0 comments (0 inline, 0 general)