Changeset - 6e1413bf21d3
[Not reviewed]
0 7 0
Sarang Bharadwaj - 13 years ago 2012-06-21 13:35:36
sarang.bh@gmail.com
Chatroom mode - basic working code
7 files changed with 143 insertions and 65 deletions:
0 comments (0 inline, 0 general)
backends/twitter/Requests/HelpMessageRequest.cpp
Show inline comments
 
@@ -2,18 +2,19 @@
 
DEFINE_LOGGER(logger, "HelpMessageRequest")
 
void HelpMessageRequest::run() 
 
{
 
	std::string helpMsg = "";
 
	helpMsg = helpMsg
 
			+ "\n******************************HELP************************************\n"
 
			+ "#status <your status> ==> Update your status\n"
 
			+ "#timeline [username]  ==> Retrieve <username>'s timeline; Default - own timeline\n"
 
			+ "@<username> <message> ==> Send a directed message to the user <username>\n"
 
			+ "#help                 ==> Print this help message\n"
 
			+ "#status <your status>      ==> Update your status\n"
 
			+ "#timeline [username]       ==> Retrieve <username>'s timeline; Default - own timeline\n"
 
			+ "@<username> <message>      ==> Send a directed message to the user <username>\n"
 
			+ "#retweet <unique_tweet_id> ==> Retweet the tweet having id <unique_tweet_id> \n"
 
			+ "#follow <username>         ==> Follow user <username>\n"
 
			+ "#unfollow <username>       ==> Stop Following user <username>\n"
 
			+ "#help                      ==> Print this help message\n"
 
			+ "************************************************************************\n";
 

	
 
	np->handleMessage(user, "twitter-account", helpMsg);
 
}
 

	
 
void HelpMessageRequest::finalize()
 
{
 
	callBack(user, helpMsg);
 
}
backends/twitter/Requests/HelpMessageRequest.h
Show inline comments
 
@@ -6,6 +6,7 @@
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 
#include <string>
 
#include <boost/function.hpp>
 
#include <iostream>
 

	
 
using namespace Transport;
 
@@ -13,12 +14,13 @@ using namespace Transport;
 
class HelpMessageRequest : public Thread
 
{
 
	std::string user;
 
	NetworkPlugin *np;
 

	
 
	std::string helpMsg;
 
	boost::function<void (std::string &, std::string &)> callBack;
 
	
 
	public:
 
	HelpMessageRequest(NetworkPlugin *_np, const std::string &_user) {
 
	HelpMessageRequest(const std::string &_user, boost::function<void (std::string &, std::string &)> cb) {
 
		user = _user;
 
		np = _np;
 
		callBack = cb;
 
	}
 

	
 
	void run();
backends/twitter/Requests/OAuthFlow.cpp
Show inline comments
 
@@ -16,13 +16,3 @@ void OAuthFlow::finalize()
 
		np->OAuthFlowComplete(user, twitObj);
 
	}	
 
}
 

	
 
/*std::string authUrl;
 
if (sessions[user]->oAuthRequestToken( authUrl ) == false ) {
 
	LOG4CXX_ERROR(logger, "Error creating twitter authorization url!");
 
	handleLogoutRequest(user, username);
 
	return;
 
}
 
handleMessage(user, "twitter-account", std::string("Please visit the following link and authorize this application: ") + authUrl);
 
handleMessage(user, "twitter-account", std::string("Please reply with the PIN provided by twitter. Prefix the pin with 'pin:'. Ex. 'pin: 1234'"));
 
connectionState[user] = WAITING_FOR_PIN;*/
backends/twitter/Requests/StatusUpdateRequest.cpp
Show inline comments
 
@@ -5,25 +5,26 @@ DEFINE_LOGGER(logger, "StatusUpdateRequest")
 
void StatusUpdateRequest::run() 
 
{
 
	replyMsg = "";
 
	if( twitObj->statusUpdate( data ) ) {
 
		while(replyMsg.length() == 0) {
 
			twitObj->getLastWebResponse( replyMsg );
 
		}
 
	success = twitObj->statusUpdate(data);
 
	if(success) {
 
		twitObj->getLastWebResponse( replyMsg );
 
		LOG4CXX_INFO(logger, user << "StatusUpdateRequest response " << replyMsg );
 
	} 
 
	}
 
}
 

	
 
void StatusUpdateRequest::finalize()
 
{
 
	if(replyMsg != "" ) {
 
		std::string error = getErrorMessage(replyMsg);
 
		if(error.length()) {
 
			np->handleMessage(user, "twitter-account", error);
 
			LOG4CXX_INFO(logger, user << ": " << error);
 
		} else	LOG4CXX_INFO(logger, "Updated status for " << user << ": " << data);
 
	} else {
 
	if(!success) {
 
		twitObj->getLastCurlError( replyMsg );
 
		LOG4CXX_ERROR(logger, user << ": CurlError - " << replyMsg );
 
		callBack(user, replyMsg);
 
	} else {
 
		std::string error = getErrorMessage(replyMsg);
 
		if(error.length()) {
 
			LOG4CXX_ERROR(logger, user << ": " << error);
 
			callBack(user, error);
 
		}
 
		else LOG4CXX_INFO(logger, "Updated status for " << user << ": " << data);
 
	}
 
	return;
 
}
backends/twitter/Requests/StatusUpdateRequest.h
Show inline comments
 
@@ -5,6 +5,7 @@
 
#include "../libtwitcurl/twitcurl.h"
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 
#include <boost/function.hpp>
 
#include <string>
 
#include <iostream>
 

	
 
@@ -15,13 +16,16 @@ class StatusUpdateRequest : public Thread
 
	std::string data;
 
	std::string user;
 
	std::string replyMsg;
 
	NetworkPlugin *np;
 
	boost::function<void (std::string& user, std::string& errMsg)> callBack;
 
	bool success;
 

	
 
	public:
 
	StatusUpdateRequest(NetworkPlugin *_np, twitCurl *obj, const std::string &_user, const std::string &_data) {
 
	StatusUpdateRequest(twitCurl *obj, const std::string &_user, const std::string &_data,
 
						boost::function<void (std::string& user, std::string& errMsg)> cb) {
 
		twitObj = obj->clone();
 
		data = _data;
 
		user = _user;
 
		np = _np;
 
		callBack = cb;
 
	}
 

	
 
	~StatusUpdateRequest() {
backends/twitter/TwitterPlugin.cpp
Show inline comments
 
@@ -45,6 +45,11 @@ TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, Stora
 
	} 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";
 
@@ -60,8 +65,8 @@ TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, Stora
 
	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();
 
@@ -105,7 +110,10 @@ void TwitterPlugin::handleLoginRequest(const std::string &user, const std::strin
 
	initUserSession(user, password);
 
	
 
	handleConnected(user);
 
	handleBuddyChanged(user, "twitter-account", "twitter", std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
	
 
	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)
 
	
 
@@ -125,17 +133,48 @@ void TwitterPlugin::handleLoginRequest(const std::string &user, const std::strin
 
// User logging out
 
void TwitterPlugin::handleLogoutRequest(const std::string &user, const std::string &legacyName) 
 
{
 
	delete sessions[user];
 
	sessions[user] = NULL;
 
	connectionState[user] = DISCONNECTED;
 
	onlineUsers.erase(user);
 
	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)) {
 
		delete sessions[user];
 
		sessions[user] = NULL;
 
		connectionState[user] = DISCONNECTED;
 
		onlineUsers.erase(user);
 
	}
 
}
 

	
 

	
 
void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml) 
 
{
 
	
 
	if(legacyName == "twitter-account") {
 
	if(legacyName == adminLegacyName) {
 

	
 
		//char ch;
 
		std::string cmd = "", data = "";
 
@@ -145,16 +184,17 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
 
		while(i<message.size() && message[i] == ' ') i++;
 
		data = message.substr(i);
 
		
 
		//handleMessage(user, "twitter-account", cmd + " " + data);
 
		//handleMessage(user, adminLegacyName, cmd + " " + data);
 

	
 
		if(cmd == "#pin") tp->runAsThread(new PINExchangeProcess(np, sessions[user], user, data));
 
		else if(cmd == "#help") tp->runAsThread(new HelpMessageRequest(np, user));
 
		else if(cmd == "#help") tp->runAsThread(new HelpMessageRequest(user, boost::bind(&TwitterPlugin::helpMessageResponse, this, _1, _2)));
 
		else if(cmd[0] == '@') {
 
			std::string username = cmd.substr(1); 
 
			tp->runAsThread(new DirectMessageRequest(sessions[user], user, username, data,
 
												     boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3)));
 
		}
 
		else if(cmd == "#status") tp->runAsThread(new StatusUpdateRequest(np, sessions[user], user, data));
 
		else if(cmd == "#status") tp->runAsThread(new StatusUpdateRequest(sessions[user], user, data,
 
														boost::bind(&TwitterPlugin::statusUpdateResponse, this, _1, _2)));
 
		else if(cmd == "#timeline") tp->runAsThread(new TimelineRequest(sessions[user], user, data, "",
 
														boost::bind(&TwitterPlugin::displayTweets, this, _1, _2, _3, _4)));
 
		else if(cmd == "#friends") tp->runAsThread(new FetchFriends(sessions[user], user,
 
@@ -165,12 +205,26 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
 
													   boost::bind(&TwitterPlugin::deleteFriendResponse, this, _1, _2, _3)));
 
		else if(cmd == "#retweet") tp->runAsThread(new RetweetRequest(sessions[user], user, data,
 
													   boost::bind(&TwitterPlugin::RetweetResponse, this, _1, _2)));
 
		else handleMessage(user, "twitter-account", "Unknown command! Type #help for a list of available commands.");
 
		else if(twitterMode == CHATROOM) {
 
			std::string buddy = message.substr(0, message.find(":"));
 
			if(buddy == "") {
 
				handleMessage(user, adminLegacyName, "Unknown command! Type #help for a list of available commands.", adminNickName);
 
				return;
 
			}
 
			data = message.substr(message.find(":")+1);
 
			tp->runAsThread(new DirectMessageRequest(sessions[user], user, buddy, data,
 
												 boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3)));
 
		}
 
		else handleMessage(user, adminLegacyName, "Unknown command! Type #help for a list of available commands.", adminNickName);
 
	} 
 

	
 
	else {
 
		tp->runAsThread(new DirectMessageRequest(sessions[user], user, legacyName, message,
 
	else {	
 
		std::string buddy;
 
		if(twitterMode == CHATROOM) buddy = legacyName.substr(legacyName.find("/") + 1);
 
		if(legacyName != "twitter") {
 
			tp->runAsThread(new DirectMessageRequest(sessions[user], user, buddy, message,
 
												 boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3)));
 
		}
 
	}
 
}
 

	
 
@@ -348,15 +402,31 @@ std::string TwitterPlugin::getMostRecentDMID(const std::string user)
 
}
 

	
 
/************************************** Twitter response functions **********************************/
 
void TwitterPlugin::statusUpdateResponse(std::string &user, std::string &errMsg)
 
{
 
	if(errMsg.length()) {
 
		handleMessage(user, adminLegacyName, errMsg, adminNickName);
 
	}
 
}
 

	
 
void TwitterPlugin::helpMessageResponse(std::string &user, std::string &msg)
 
{
 
	handleMessage(user, adminLegacyName, msg, adminNickName);
 
}
 

	
 
void TwitterPlugin::populateRoster(std::string &user, std::vector<User> &friends, std::string &errMsg) 
 
{
 
	if(errMsg.length() == 0) 
 
	{
 
		for(int i=0 ; i<friends.size() ; i++) {
 
			handleBuddyChanged(user, friends[i].getScreenName(), friends[i].getUserName(), std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
			if(twitterMode == MULTIPLECONTACT)
 
				handleBuddyChanged(user, friends[i].getScreenName(), friends[i].getUserName(), std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
			else 
 
				handleParticipantChanged(user, friends[i].getScreenName(), adminLegacyName, 0, pbnetwork::STATUS_ONLINE);
 
		}
 
	} else handleMessage(user, "twitter-account", std::string("Error populating roster - ") + errMsg);	
 
	} else handleMessage(user, adminLegacyName, std::string("Error populating roster - ") + errMsg, adminNickName);	
 

	
 
	if(twitterMode == CHATROOM) handleParticipantChanged(user, nickName[user], adminLegacyName, 0, pbnetwork::STATUS_ONLINE);
 
}
 

	
 
void TwitterPlugin::displayFriendlist(std::string &user, std::vector<User> &friends, std::string &errMsg)
 
@@ -368,8 +438,8 @@ void TwitterPlugin::displayFriendlist(std::string &user, std::vector<User> &frie
 
			userlist += " - " + friends[i].getUserName() + " (" + friends[i].getScreenName() + ")\n";
 
		}	
 
		userlist += "***************************************\n";
 
		handleMessage(user, "twitter-account", userlist);	
 
	} else handleMessage(user, "twitter-account", errMsg);	
 
		handleMessage(user, adminLegacyName, userlist, adminNickName);	
 
	} else handleMessage(user, adminLegacyName, errMsg, adminNickName);	
 
 
 
}
 

	
 
@@ -385,17 +455,17 @@ void TwitterPlugin::displayTweets(std::string &user, std::string &userRequested,
 
		if((userRequested == "" || userRequested == user) && tweets.size()) {
 
			std::string tweetID = getMostRecentTweetID(user);
 
			if(tweetID != tweets[0].getID()) updateLastTweetID(user, tweets[0].getID());
 
			else timeline = ""; //have already sent the tweet earlier
 
			//else timeline = ""; have already sent the tweet earlier
 
		}
 

	
 
		if(timeline.length()) handleMessage(user, "twitter-account", timeline);
 
	} else handleMessage(user, "twitter-account", errMsg);	
 
		if(timeline.length()) handleMessage(user, adminLegacyName, timeline, adminNickName);
 
	} else handleMessage(user, adminLegacyName, errMsg, adminNickName);	
 
}
 

	
 
void TwitterPlugin::directMessageResponse(std::string &user, std::vector<DirectMessage> &messages, std::string &errMsg)
 
{
 
	if(errMsg.length()) {
 
		handleMessage(user, "twitter-account", std::string("Error while sending direct message! - ") + errMsg);	
 
		handleMessage(user, adminLegacyName, std::string("Error while sending direct message! - ") + errMsg, adminNickName);	
 
		return;
 
	}
 
	
 
@@ -414,7 +484,7 @@ void TwitterPlugin::directMessageResponse(std::string &user, std::vector<DirectM
 
			}
 
		}	
 

	
 
		if(msglist.length()) handleMessage(user, "twitter-account", msglist);	
 
		if(msglist.length()) handleMessage(user, adminLegacyName, msglist, adminNickName);	
 
		updateLastDMID(user, maxID);
 

	
 
	} else if(twitterMode == MULTIPLECONTACT) {
 
@@ -424,7 +494,7 @@ void TwitterPlugin::directMessageResponse(std::string &user, std::vector<DirectM
 

	
 
		for(int i=0 ; i < messages.size() ; i++) {
 
			if(cmp(msgID, messages[i].getID()) == -1) {
 
				handleMessage(user, messages[i].getSenderData().getScreenName(), messages[i].getMessage());
 
				handleMessage(user, messages[i].getSenderData().getScreenName(), messages[i].getMessage(), adminNickName);
 
				if(cmp(maxID, messages[i].getID()) == -1) maxID = messages[i].getID();
 
			}
 
		}	
 
@@ -437,12 +507,12 @@ void TwitterPlugin::directMessageResponse(std::string &user, std::vector<DirectM
 
void TwitterPlugin::createFriendResponse(std::string &user, std::string &frnd, std::string &errMsg)
 
{
 
	if(errMsg.length()) {
 
		handleMessage(user, "twitter-account", errMsg);
 
		handleMessage(user, adminLegacyName, errMsg, adminNickName);
 
		return;
 
	}
 

	
 
	if(twitterMode == SINGLECONTACT) {
 
		handleMessage(user, "twitter-account", std::string("You are now following ") + frnd);
 
		handleMessage(user, adminLegacyName, std::string("You are now following ") + frnd, adminNickName);
 
	} else if(twitterMode == MULTIPLECONTACT) {
 
		handleBuddyChanged(user, frnd, frnd, std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
	}
 
@@ -451,10 +521,10 @@ void TwitterPlugin::createFriendResponse(std::string &user, std::string &frnd, s
 
void TwitterPlugin::deleteFriendResponse(std::string &user, std::string &frnd, std::string &errMsg)
 
{
 
	if(errMsg.length()) {
 
		handleMessage(user, "twitter-account", errMsg);
 
		handleMessage(user, adminLegacyName, errMsg, adminNickName);
 
		return;
 
	} if(twitterMode == SINGLECONTACT) {
 
		handleMessage(user, "twitter-account", std::string("You are not following ") + frnd + "anymore");
 
		handleMessage(user, adminLegacyName, std::string("You are not following ") + frnd + "anymore", adminNickName);
 
	} else if(twitterMode == MULTIPLECONTACT) {
 
		handleBuddyRemoved(user, frnd);
 
	}
 
@@ -464,7 +534,7 @@ void TwitterPlugin::deleteFriendResponse(std::string &user, std::string &frnd, s
 
void TwitterPlugin::RetweetResponse(std::string &user, std::string &errMsg)
 
{
 
	if(errMsg.length()) {
 
		handleMessage(user, "twitter-account", errMsg);
 
		handleMessage(user, adminLegacyName, errMsg, adminNickName);
 
		return;
 
	}
 
}
backends/twitter/TwitterPlugin.h
Show inline comments
 
@@ -66,6 +66,10 @@ class TwitterPlugin : public NetworkPlugin {
 
		
 
		// User logging out
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName);
 
		
 
		void handleJoinRoomRequest(const std::string &/*user*/, const std::string &/*room*/, const std::string &/*nickname*/, const std::string &/*pasword*/);
 

	
 
		void handleLeaveRoomRequest(const std::string &/*user*/, const std::string &/*room*/);
 

	
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "");
 

	
 
@@ -96,6 +100,8 @@ class TwitterPlugin : public NetworkPlugin {
 
		std::string getMostRecentDMID(const std::string user);
 

	
 
		/****************** Twitter Response handlers **************************************/
 
		void statusUpdateResponse(std::string &user, std::string &errMsg);
 
		void helpMessageResponse(std::string &user, std::string &msg);
 
		void populateRoster(std::string &user, std::vector<User> &friends, std::string &errMsg);
 
		void displayFriendlist(std::string &user, std::vector<User> &friends, std::string &errMsg);
 
		void displayTweets(std::string &user, std::string &userRequested, std::vector<Status> &tweets , std::string &errMsg);
 
@@ -110,6 +116,9 @@ class TwitterPlugin : public NetworkPlugin {
 
		enum mode {SINGLECONTACT, MULTIPLECONTACT, CHATROOM};
 

	
 
		Config *config;
 
		std::string adminLegacyName;
 
		std::string adminNickName;
 
		std::string adminAlias;
 

	
 
		std::string consumerKey;
 
		std::string consumerSecret;
 
@@ -124,6 +133,7 @@ class TwitterPlugin : public NetworkPlugin {
 
		std::map<std::string, std::string> mostRecentTweetID;
 
		std::map<std::string, std::string> mostRecentDirectMessageID;
 
		std::set<std::string> onlineUsers;
 
		std::map<std::string,std::string> nickName;
 
		mode twitterMode;
 
};
 

	
0 comments (0 inline, 0 general)