Changeset - 05b24bbd6e85
[Not reviewed]
0 4 0
Jan Kaluza - 10 years ago 2016-02-19 12:00:54
jkaluza@redhat.com
Slack: More logging
4 files changed with 13 insertions and 2 deletions:
0 comments (0 inline, 0 general)
libtransport/UserManager.cpp
Show inline comments
 
@@ -98,48 +98,49 @@ User *UserManager::getUser(const std::string &barejid){
 
// 
 
// 	User *user = it->second;
 
// 	return user->getCaps(jid);
 
// }
 

	
 
void UserManager::removeUser(User *user, bool onUserBehalf) {
 
	m_users.erase(user->getJID().toBare().toString());
 
	if (m_cachedUser == user)
 
		m_cachedUser = NULL;
 

	
 
	if (m_component->inServerMode()) {
 
		disconnectUser(user->getJID());
 
	}
 
	else {
 
		// User could be disconnected by User::handleDisconnect() method, but
 
		// Transport::PresenceOracle could still contain his last presence.
 
		// We have to clear all received presences for this user in PresenceOracle.
 
		m_component->getPresenceOracle()->clearPresences(user->getJID().toBare());
 
	}
 

	
 
	if (m_storageBackend && onUserBehalf) {
 
		m_storageBackend->setUserOnline(user->getUserInfo().id, false);
 
	}
 

	
 
	LOG4CXX_INFO(logger, user->getJID().toBare().toString() << ": Disconnecting user");
 
	onUserDestroyed(user);
 
	delete user;
 
#ifndef WIN32
 
#ifndef __FreeBSD__
 
#ifndef __MACH__
 
	malloc_trim(0);
 
#endif
 
#endif
 
#endif
 
// 	VALGRIND_DO_LEAK_CHECK;
 
}
 

	
 
void UserManager::removeAllUsers(bool onUserBehalf) {
 
	while(m_users.begin() != m_users.end()) {
 
		removeUser((*m_users.begin()).second, onUserBehalf);
 
	}
 
}
 

	
 
int UserManager::getUserCount() {
 
	return m_users.size();
 
}
 

	
 
void UserManager::handleDiscoInfo(const Swift::JID& jid, boost::shared_ptr<Swift::DiscoInfo> info) {
 
	User *user = getUser(jid.toBare().toString());
libtransport/UserRegistration.cpp
Show inline comments
 
@@ -45,43 +45,46 @@ UserRegistration::UserRegistration(Component *component, UserManager *userManage
 
}
 

	
 
UserRegistration::~UserRegistration(){
 
}
 

	
 
bool UserRegistration::registerUser(const UserInfo &row, bool allowPasswordChange) {
 
	UserInfo dummy;
 
	bool registered = m_storageBackend->getUser(row.jid, dummy);
 

	
 
	if (!registered || (registered && allowPasswordChange)) {
 
		m_storageBackend->setUser(row);
 
		doUserRegistration(row);
 
		onUserRegistered(row);
 
	}
 

	
 
	return !registered;
 
}
 

	
 
bool UserRegistration::unregisterUser(const std::string &barejid) {
 
	UserInfo userInfo;
 
	bool registered = m_storageBackend->getUser(barejid, userInfo);
 

	
 
	// This user is not registered, nothing to do
 
	if (!registered) {
 
		LOG4CXX_WARN(logger, "Tried to unregister already unregistered user " << barejid);
 
		return false;
 
	}
 

	
 
	LOG4CXX_INFO(logger, "Unregistering user " << barejid);
 
	// Remove user from database
 
	m_storageBackend->removeUser(userInfo.id);
 

	
 
	// Disconnect the user
 
	User *user = m_userManager->getUser(barejid);
 
	if (user) {
 
		LOG4CXX_INFO(logger, "Disconnecting user " << barejid);
 
		m_userManager->removeUser(user);
 
	}
 

	
 
	doUserUnregistration(userInfo);
 
	onUserUnregistered(userInfo);
 

	
 
	return true;
 
}
 

	
 
}
spectrum/src/frontends/slack/SlackUser.cpp
Show inline comments
 
@@ -46,31 +46,31 @@ SlackUser::SlackUser(const Swift::JID &jid, UserInfo &userInfo, Component *compo
 

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

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

	
 
void SlackUser::disconnectUser(const std::string &error, Swift::SpectrumErrorPayload::Error e) {
 
	if (!m_session) {
 
		return;
 
	}
 

	
 
	if (!error.empty()) {
 
		m_session->sendMessageToAll(error);
 
	}
 
	else {
 
		m_session->sendMessageToAll("Disconnected from 3rd-party network for unknown reason.");
 
	}
 

	
 
	m_session->handleDisconnected();
 
	m_session->sendMessageToAll("Maybe you have provided wrong 3rd-party network account information. In that case, you can use ```.spectrum2 register ...``` command again with corrected data. You can also try using ```.spectrum2 reconnect``` to reconnect.");
 
	m_session->sendMessageToAll("Maybe you have provided wrong 3rd-party network account information.");
 
	static_cast<SlackUserManager *>(m_userManager)->moveTempSession(m_jid.toString(), m_session);
 
	m_session = NULL;
 
}
 

	
 

	
 
}
spectrum/src/frontends/slack/SlackUserRegistration.cpp
Show inline comments
 
@@ -53,48 +53,55 @@ SlackUserRegistration::SlackUserRegistration(Component *component, UserManager *
 
	m_component = component;
 
	m_config = m_component->getConfig();
 
	m_storageBackend = storageBackend;
 
	m_userManager = userManager;
 

	
 
}
 

	
 
SlackUserRegistration::~SlackUserRegistration(){
 
	
 
}
 

	
 
std::string SlackUserRegistration::createOAuth2URL(const std::vector<std::string> &args) {
 
	std::string redirect_url = "https://slack.spectrum.im/oauth2/" + CONFIG_STRING(m_config, "service.jid");
 
	OAuth2 *oauth2 = new OAuth2(CONFIG_STRING_DEFAULTED(m_config, "service.client_id",""),
 
						  CONFIG_STRING_DEFAULTED(m_config, "service.client_secret",""),
 
						  "https://slack.com/oauth/authorize",
 
						  "https://slack.com/api/oauth.access",
 
						  redirect_url,
 
						  "channels:read channels:write team:read im:read im:write chat:write:bot bot");
 
	std::string url = oauth2->generateAuthURL();
 

	
 
	m_auths[oauth2->getState()] = oauth2;
 
	m_authsData[oauth2->getState()] = args;
 

	
 
	if (args.size() >= 3) {
 
		LOG4CXX_INFO(logger, "Generating OAUth2 URL with slack_channel=" << args[0] << ", 3rd_party_account=" << args[1]);
 
	}
 
	else {
 
		LOG4CXX_WARN(logger, "Generating OAUth2 URL with too few arguments");
 
	}
 

	
 
	return url;
 
}
 

	
 
std::string SlackUserRegistration::getTeamDomain(const std::string &token) {
 
	std::string url = "https://slack.com/api/team.info?token=" + Util::urlencode(token);
 

	
 
	rapidjson::Document resp;
 
	HTTPRequest req(HTTPRequest::Get, url);
 
	if (!req.execute(resp)) {
 
		LOG4CXX_ERROR(logger, url);
 
		LOG4CXX_ERROR(logger, req.getError());
 
		return "";
 
	}
 

	
 
	rapidjson::Value &team = resp["team"];
 
	if (!team.IsObject()) {
 
		LOG4CXX_ERROR(logger, "No 'team' object in the reply.");
 
		LOG4CXX_ERROR(logger, url);
 
		LOG4CXX_ERROR(logger, req.getRawData());
 
		return "";
 
	}
 

	
 
	rapidjson::Value &domain = team["domain"];
 
	if (!domain.IsString()) {
 
@@ -161,49 +168,49 @@ std::string SlackUserRegistration::handleOAuth2Code(const std::string &code, con
 
	user.id = 0;
 
	m_storageBackend->getUser(domain, user);
 

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

	
 
	registerUser(user, true);
 

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

	
 
	if (!slackChannel.empty()) {
 
		m_storageBackend->getUserSetting(user.id, "slack_channel", type, slackChannel);
 
	}
 

	
 
	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);
 
	LOG4CXX_INFO(logger, "Registered Slack user " << user.jid << ", slack_channel=" << slackChannel);
 

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

	
 
		m_authsData.erase(state);
 
	}
 

	
 
	m_component->getFrontend()->reconnectUser(user.jid);
 

	
 
	return "Registered as " + domain;
 
}
 

	
 
bool SlackUserRegistration::doUserRegistration(const UserInfo &row) {
 
	return true;
 
}
 

	
 
bool SlackUserRegistration::doUserUnregistration(const UserInfo &row) {
 
	return true;
 
}
 

	
 

	
 

	
 
}
0 comments (0 inline, 0 general)