Changeset - 7166b462847f
[Not reviewed]
0 1 0
Sarang Bharadwaj - 13 years ago 2012-07-02 12:48:29
sarang.bh@gmail.com
Chatroom mode
1 file changed with 2 insertions and 2 deletions:
0 comments (0 inline, 0 general)
backends/twitter/TwitterPlugin.cpp
Show inline comments
 
#include "TwitterPlugin.h"
 
#include "Requests/StatusUpdateRequest.h"
 
#include "Requests/DirectMessageRequest.h"
 
#include "Requests/TimelineRequest.h"
 
#include "Requests/FetchFriends.h"
 
#include "Requests/HelpMessageRequest.h"
 
#include "Requests/PINExchangeProcess.h"
 
#include "Requests/OAuthFlow.h"
 
#include "Requests/CreateFriendRequest.h"
 
#include "Requests/DestroyFriendRequest.h"
 
#include "Requests/RetweetRequest.h"
 

	
 
DEFINE_LOGGER(logger, "Twitter Backend");
 

	
 
TwitterPlugin *np = NULL;
 
Swift::SimpleEventLoop *loop_; // Event Loop
 

	
 
#define abs(x) ((x)<0?-(x):(x))
 
static int cmp(std::string a, std::string b)
 
{
 
	int diff = abs((int)a.size() - (int)b.size());
 
	if(a.size() < b.size()) a = std::string(diff,'0') + a;
 
	else b = std::string(diff,'0') + b;
 
	
 
	if(a == b) return 0;
 
	if(a < b) return -1;
 
	return 1;
 
}
 

	
 

	
 
TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin() 
 
{
 
	this->config = config;
 
	this->storagebackend = storagebackend;
 

	
 
	if(CONFIG_HAS_KEY(config, "twitter.consumer_key") == false ||
 
	   CONFIG_HAS_KEY(config, "twitter.consumer_secret") == false) {
 
		LOG4CXX_ERROR(logger, "Couldn't find consumer key and/or secret. Please check config file.");
 
		exit(0);
 
	}
 
	
 
	twitterMode = SINGLECONTACT;
 
	if(CONFIG_HAS_KEY(config, "twitter.mode") == false) {
 
		LOG4CXX_INFO(logger, "Using default single contact mode!");
 
	} else twitterMode = (mode)CONFIG_INT(config, "twitter.mode");
 

	
 

	
 
	adminLegacyName = "twitter-account"; 
 
	adminNickName = ""; 
 
	adminAlias = "twitter";
 
	if(twitterMode == CHATROOM) adminNickName = "twitter";
 

	
 
	consumerKey = CONFIG_STRING(config, "twitter.consumer_key");
 
	consumerSecret = CONFIG_STRING(config, "twitter.consumer_secret");
 
	OAUTH_KEY = "oauth_key";
 
	OAUTH_SECRET = "oauth_secret";
 

	
 
	m_factories = new Swift::BoostNetworkFactories(loop);
 
	m_conn = m_factories->getConnectionFactory()->createConnection();
 
	m_conn->onDataRead.connect(boost::bind(&TwitterPlugin::_handleDataRead, this, _1));
 
	m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(host), port));
 

	
 
	tp = new ThreadPool(loop_, 10);
 
		
 
	tweet_timer = m_factories->getTimerFactory()->createTimer(60000);
 
	message_timer = m_factories->getTimerFactory()->createTimer(60000);
 

	
 
	//tweet_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this));
 
	//message_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this));
 
	tweet_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this));
 
	message_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this));
 

	
 
	tweet_timer->start();
 
	message_timer->start();
 
	
 
	LOG4CXX_INFO(logger, "Starting the plugin.");
 
}
 

	
 
TwitterPlugin::~TwitterPlugin() 
 
{
 
	delete storagebackend;
 
	std::map<std::string, twitCurl*>::iterator it;
 
	for(it = sessions.begin() ; it != sessions.end() ; it++) delete it->second;
 
	delete tp;
 
}
 

	
 
// Send data to NetworkPlugin server
 
void TwitterPlugin::sendData(const std::string &string) 
 
{
 
	m_conn->write(Swift::createSafeByteArray(string));
 
}
 

	
 
// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
 
void TwitterPlugin::_handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data) 
 
{
 
	std::string d(data->begin(), data->end());
 
	handleDataRead(d);
 
}
 

	
 
// User trying to login into his twitter account
 
void TwitterPlugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) 
 
{
 
	if(connectionState.count(user) && (connectionState[user] == NEW || 
 
										connectionState[user] == CONNECTED || 
 
										connectionState[user] == WAITING_FOR_PIN)) {
 
		LOG4CXX_INFO(logger, std::string("A session corresponding to ") + user + std::string(" is already active"))
 
		return;
 
	}
 
	
 
	LOG4CXX_INFO(logger, std::string("Received login request for ") + user)
 
	
 
	initUserSession(user, password);
 
	
 
	handleConnected(user);
 
	
 
	LOG4CXX_INFO(logger, user << ": Adding Buddy " << adminLegacyName << " " << adminAlias)
 
	handleBuddyChanged(user, adminLegacyName, adminAlias, std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
	nickName[user] = "";
 
	
 
	LOG4CXX_INFO(logger, "Querying database for usersettings of " << user)
 
	
 
	std::string key, secret;
 
	getUserOAuthKeyAndSecret(user, key, secret);
 

	
 
	if(key == "" || secret == "") {			
 
		LOG4CXX_INFO(logger, "Intiating OAuth Flow for user " << user)
 
		tp->runAsThread(new OAuthFlow(np, sessions[user], user, sessions[user]->getTwitterUsername()));
 
	} else {
 
		LOG4CXX_INFO(logger, user << " is already registerd. Using the stored oauth key and secret")
 
		LOG4CXX_INFO(logger, key << " " << secret)	
 
		pinExchangeComplete(user, key, secret);
 
	}
 
}
 

	
 
// User logging out
 
void TwitterPlugin::handleLogoutRequest(const std::string &user, const std::string &legacyName) 
 
{
 
	if(onlineUsers.count(user)) {
 
		delete sessions[user];
 
		sessions[user] = NULL;
 
		connectionState[user] = DISCONNECTED;
 
		onlineUsers.erase(user);
 
	}
 
}
 

	
 
void TwitterPlugin::handleJoinRoomRequest(const std::string &user, const std::string &room, const std::string &nickname, const std::string &password)
 
{
 
	if(room == adminLegacyName) {
 
		
 
		LOG4CXX_INFO(logger, std::string("Received Join Twitter room request for ") + user)
 
			
 
		handleParticipantChanged(user, adminNickName, room, 0, pbnetwork::STATUS_ONLINE);
 

	
 
		nickName[user] = nickname;
 

	
 
		if(twitterMode == CHATROOM) {
 
			handleMessage(user, adminLegacyName, "Connected to Twitter Room! Populating your followers list", adminNickName);
 
		}	
 
		
 
		tp->runAsThread(new FetchFriends(sessions[user], user,
 
										 boost::bind(&TwitterPlugin::populateRoster, this, _1, _2, _3)));
 
	}
 
}
 

	
 
void TwitterPlugin::handleLeaveRoomRequest(const std::string &user, const std::string &room)
 
{
 
	if(room == adminLegacyName && onlineUsers.count(user)) {
0 comments (0 inline, 0 general)