Changeset - c2fefc36c734
[Not reviewed]
0 4 0
Sarang Bharadwaj - 13 years ago 2012-06-10 18:53:22
sarang.bh@gmail.com
Polling for user's DMs
4 files changed with 70 insertions and 23 deletions:
0 comments (0 inline, 0 general)
backends/twitter/Requests/DirectMessageRequest.cpp
Show inline comments
 
@@ -3,26 +3,23 @@
 

	
 
DEFINE_LOGGER(logger, "DirectMessageRequest")
 

	
 
void DirectMessageRequest::run() 
 
{
 
	replyMsg = "";
 
	if(twitObj->directMessageSend(username, data, false) == false) {
 
		LOG4CXX_ERROR(logger, user << ": Error while sending directed message to " << username );
 
		return;
 
	}
 
	twitObj->getLastWebResponse( replyMsg );
 
	success = twitObj->directMessageSend(username, data, false);
 
	if(success) twitObj->getLastWebResponse( replyMsg );
 
}
 

	
 
void DirectMessageRequest::finalize()
 
{
 
	if(replyMsg.length()) {
 
		std::string error = getErrorMessage(replyMsg);
 
		if(error.length()) {
 
			np->handleMessage(user, "twitter-account", error);
 
			LOG4CXX_INFO(logger, user << ": " << error);
 
	if(!success) {
 
		LOG4CXX_ERROR(logger, user << ": Error while sending directed message to " << username );
 
		twitObj->getLastCurlError( replyMsg );
 
		callBack(user, replyMsg);
 
	} else {
 
			LOG4CXX_INFO(logger, user << ": Sent " << data << " to " << username)
 
			LOG4CXX_INFO(logger, user << ": Twitter reponse - " << replyMsg)
 
		}
 
		std::string error = getErrorMessage(replyMsg);
 
		if(error.length()) LOG4CXX_ERROR(logger,  user << " - " << error)
 
		else LOG4CXX_INFO(logger, user << " - " << replyMsg)
 
		callBack(user, error);	
 
	}
 
}
backends/twitter/Requests/DirectMessageRequest.h
Show inline comments
 
#ifndef DIRECT_MESSAGE
 
#define DIRECT_MESSAGE
 

	
 
#include "../ThreadPool.h"
 
#include "../libtwitcurl/twitcurl.h"
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 
#include <string>
 
#include <boost/function.hpp>
 
#include <iostream>
 

	
 
using namespace Transport;
 

	
 
class DirectMessageRequest : public Thread
 
{
 
	twitCurl *twitObj;
 
	std::string data;
 
	std::string user;
 
	std::string username;
 
	std::string replyMsg;
 
	NetworkPlugin *np;
 
	boost::function< void (std::string&, std::string&) > callBack;
 
	bool success;
 

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

	
 
	~DirectMessageRequest() {
 
		delete twitObj;
 
	}
 

	
backends/twitter/TwitterPlugin.cpp
Show inline comments
 
@@ -39,12 +39,13 @@ TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, Stora
 
	m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(host), port));
 

	
 
	tp = new ThreadPool(loop_, 10);
 
		
 
	m_timer = m_factories->getTimerFactory()->createTimer(60000);
 
	m_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this));
 
	m_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this));
 
	m_timer->start();
 
	
 
	LOG4CXX_INFO(logger, "Starting the plugin.");
 
}
 

	
 
TwitterPlugin::~TwitterPlugin() 
 
@@ -126,21 +127,27 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
 
		//handleMessage(user, "twitter-account", 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[0] == '@') {
 
			std::string username = cmd.substr(1); 
 
			tp->runAsThread(new DirectMessageRequest(np, sessions[user], user, username, data));
 
			tp->runAsThread(new DirectMessageRequest(sessions[user], user, username, data,
 
												     boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2)));
 
		}
 
		else if(cmd == "#status") tp->runAsThread(new StatusUpdateRequest(np, sessions[user], user, data));
 
		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,
 
													   boost::bind(&TwitterPlugin::displayFriendlist, this, _1, _2, _3)));
 
		else handleMessage(user, "twitter-account", "Unknown command! Type #help for a list of available commands.");
 
	} 
 

	
 
	else {
 
		tp->runAsThread(new DirectMessageRequest(sessions[user], user, legacyName, message,
 
												 boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2)));
 
	}
 
}
 

	
 
void TwitterPlugin::handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) 
 
{
 
	LOG4CXX_INFO(logger, user << ": Added buddy " << buddyName << ".");
 
	handleBuddyChanged(user, buddyName, alias, groups, pbnetwork::STATUS_ONLINE);
 
@@ -162,12 +169,25 @@ void TwitterPlugin::pollForTweets()
 
											boost::bind(&TwitterPlugin::displayTweets, this, _1, _2, _3, _4)));
 
		it++;
 
	}
 
	m_timer->start();
 
}
 

	
 
void TwitterPlugin::pollForDirectMessages()
 
{
 
	/*boost::mutex::scoped_lock lock(userlock);
 
	std::set<std::string>::iterator it = onlineUsers.begin();
 
	while(it != onlineUsers.end()) {
 
		std::string user = *it;
 
		tp->runAsThread(new DirectMessage(sessions[user], user, "", mostRecentDirectMessageID[user],
 
											boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3)));
 
		it++;
 
	}
 
	m_timer->start();*/
 
}
 

	
 

	
 
bool TwitterPlugin::getUserOAuthKeyAndSecret(const std::string user, std::string &key, std::string &secret) 
 
{
 
	boost::mutex::scoped_lock lock(dblock);
 
	
 
	UserInfo info;
 
@@ -255,15 +275,16 @@ void TwitterPlugin::pinExchangeComplete(const std::string user, const std::strin
 
		tp->runAsThread(new FetchFriends(sessions[user], user,
 
										 boost::bind(&TwitterPlugin::populateRoster, this, _1, _2, _3)));
 
	}
 

	
 
	onlineUsers.insert(user);
 
	mostRecentTweetID[user] = "";
 
	mostRecentDirectMessageID[user] = "";
 
}	
 

	
 
void TwitterPlugin::updateUsersLastTweetID(const std::string user, const std::string ID)
 
void TwitterPlugin::updateLastTweetID(const std::string user, const std::string ID)
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 
	mostRecentTweetID[user] = ID;
 
}
 

	
 
std::string TwitterPlugin::getMostRecentTweetID(const std::string user)
 
@@ -271,20 +292,34 @@ std::string TwitterPlugin::getMostRecentTweetID(const std::string user)
 
	boost::mutex::scoped_lock lock(userlock);	
 
	std::string ID = "-1";
 
	if(onlineUsers.count(user)) ID = mostRecentTweetID[user];
 
	return ID;
 
}
 

	
 
void TwitterPlugin::updateLastDMID(const std::string user, const std::string ID)
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 
	mostRecentDirectMessageID[user] = ID;
 
}
 

	
 
std::string TwitterPlugin::getMostRecentDMID(const std::string user)
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 
	std::string ID = "-1";
 
	if(onlineUsers.count(user)) ID = mostRecentDirectMessageID[user];
 
	return ID;
 
}
 

	
 
/************************************** Twitter response functions **********************************/
 

	
 
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].getUserName(), friends[i].getScreenName(), std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
			handleBuddyChanged(user, friends[i].getScreenName(), friends[i].getUserName(), std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
		}
 
	} else handleMessage(user, "twitter-account", std::string("Error populating roster - ") + errMsg);	
 
}
 

	
 
void TwitterPlugin::displayFriendlist(std::string &user, std::vector<User> &friends, std::string &errMsg)
 
{
 
@@ -308,15 +343,20 @@ void TwitterPlugin::displayTweets(std::string &user, std::string &userRequested,
 
		for(int i=0 ; i<tweets.size() ; i++) {
 
			timeline += " - " + tweets[i].getUserData().getScreenName() + ": " + tweets[i].getTweet() + "\n";
 
		}
 

	
 
		if((userRequested == "" || userRequested == user) && tweets.size()) {
 
			std::string tweetID = getMostRecentTweetID(user);
 
			if(tweetID != tweets[0].getID()) updateUsersLastTweetID(user, tweets[0].getID());
 
			if(tweetID != tweets[0].getID()) updateLastTweetID(user, tweets[0].getID());
 
			else timeline = ""; //have already sent the tweet earlier
 
		}
 

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

	
 

	
 
void TwitterPlugin::directMessageResponse(std::string &user, std::string &errMsg)
 
{
 
	if(errMsg.length()) {
 
		handleMessage(user, "twitter-account", std::string("Error while sending direct message! - ") + errMsg);	
 
	}
 
}
backends/twitter/TwitterPlugin.h
Show inline comments
 
@@ -71,30 +71,37 @@ class TwitterPlugin : public NetworkPlugin {
 
		void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups);
 

	
 
		void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups);
 
		
 
		void pollForTweets();
 

	
 
		void pollForDirectMessages();
 
		
 
		bool getUserOAuthKeyAndSecret(const std::string user, std::string &key, std::string &secret);
 
		
 
		bool storeUserOAuthKeyAndSecret(const std::string user, const std::string OAuthKey, const std::string OAuthSecret);
 
		
 
		void initUserSession(const std::string user, const std::string password);
 
		
 
		void OAuthFlowComplete(const std::string user, twitCurl *obj);
 
		
 
		void pinExchangeComplete(const std::string user, const std::string OAuthAccessTokenKey, const std::string OAuthAccessTokenSecret);
 
		
 
		void updateUsersLastTweetID(const std::string user, const std::string ID);
 
		void updateLastTweetID(const std::string user, const std::string ID);
 

	
 
		std::string getMostRecentTweetID(const std::string user);
 

	
 
		void updateLastDMID(const std::string user, const std::string ID);
 
		
 
		std::string getMostRecentDMID(const std::string user);
 

	
 
		/****************** Twitter Response handlers **************************************/
 
		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);
 
		void directMessageResponse(std::string &user, std::string &errMsg);
 
		/***********************************************************************************/
 

	
 
	private:
 
		enum status {NEW, WAITING_FOR_PIN, CONNECTED, DISCONNECTED};
 
		enum mode {SINGLECONTACT, MULTIPLECONTACT, CHATROOM};
 

	
 
@@ -108,11 +115,12 @@ class TwitterPlugin : public NetworkPlugin {
 
		boost::mutex dblock, userlock;
 

	
 
		ThreadPool *tp;
 
		std::map<std::string, twitCurl*> sessions;		
 
		std::map<std::string, status> connectionState;
 
		std::map<std::string, std::string> mostRecentTweetID;
 
		std::map<std::string, std::string> mostRecentDirectMessageID;
 
		std::set<std::string> onlineUsers;
 
		mode twitterMode;
 
};
 

	
 
#endif
0 comments (0 inline, 0 general)