Changeset - cca74cda6862
[Not reviewed]
0 6 0
Sarang Bharadwaj - 13 years ago 2012-07-04 19:54:28
sarang.bh@gmail.com
Some more changes to Chatroom mode
6 files changed with 50 insertions and 28 deletions:
0 comments (0 inline, 0 general)
backends/twitter/Requests/DirectMessageRequest.cpp
Show inline comments
 
@@ -16,14 +16,14 @@ void DirectMessageRequest::run()
 

	
 
void DirectMessageRequest::finalize()
 
{
 
	if(!success) {
 
		LOG4CXX_ERROR(logger, user << ": Error while sending directed message to " << username );
 
		twitObj->getLastCurlError( replyMsg );
 
		callBack(user, messages, replyMsg);
 
		callBack(user, username, messages, replyMsg);
 
	} else {
 
		std::string error = getErrorMessage(replyMsg);
 
		if(error.length()) LOG4CXX_ERROR(logger,  user << " - " << error)
 
		else LOG4CXX_INFO(logger, user << " - " << replyMsg)
 
		callBack(user, messages, error);	
 
		callBack(user, username, messages, error);	
 
	}
 
}
backends/twitter/Requests/DirectMessageRequest.h
Show inline comments
 
@@ -15,19 +15,19 @@ class DirectMessageRequest : public Thread
 
{
 
	twitCurl *twitObj;
 
	std::string data;
 
	std::string user;
 
	std::string username;
 
	std::string replyMsg;
 
	boost::function< void (std::string&, std::vector<DirectMessage>&, std::string&) > callBack;
 
	boost::function< void (std::string&, std::string &, std::vector<DirectMessage>&, std::string&) > callBack;
 
	std::vector<DirectMessage> messages;
 
	bool success;
 

	
 
	public:
 
	DirectMessageRequest(twitCurl *obj, const std::string &_user, const std::string & _username, const std::string &_data,
 
			     		boost::function< void (std::string&, std::vector<DirectMessage>&, std::string&) >  cb) {
 
			     		boost::function< void (std::string&, std::string &, std::vector<DirectMessage>&, std::string&) >  cb) {
 
		twitObj = obj->clone();
 
		data = _data;
 
		user = _user;
 
		username = _username;
 
		callBack = cb;
 
	}
backends/twitter/Requests/OAuthFlow.cpp
Show inline comments
 
@@ -6,12 +6,13 @@ void OAuthFlow::run()
 
}
 

	
 
void OAuthFlow::finalize()
 
{
 
	if (!success) {
 
		LOG4CXX_ERROR(logger, "Error creating twitter authorization url!");
 
		np->handleMessage(user, "twitter-account", "Error creating twitter authorization url!");
 
		np->handleLogoutRequest(user, username);
 
	} else {
 
		np->handleMessage(user, "twitter-account", std::string("Please visit the following link and authorize this application: ") + authUrl);
 
		np->handleMessage(user, "twitter-account", std::string("Please reply with the PIN provided by twitter. Prefix the pin with '#pin'. Ex. '#pin 1234'"));
 
		np->OAuthFlowComplete(user, twitObj);
 
	}	
backends/twitter/Requests/PINExchangeProcess.cpp
Show inline comments
 
@@ -9,12 +9,13 @@ void PINExchangeProcess::run()
 
}
 

	
 
void PINExchangeProcess::finalize()
 
{
 
	if(!success) {
 
		LOG4CXX_ERROR(logger, user << ": Error while exchanging PIN for Access Token!")
 
		np->handleMessage(user, "twitter-account", "Error while exchanging PIN for Access Token!");
 
		np->handleLogoutRequest(user, "");
 
	} else {
 
		std::string replyMsg;
 
		while(replyMsg.length() == 0) {
 
			twitObj->getLastWebResponse(replyMsg);
 
		}
backends/twitter/TwitterPlugin.cpp
Show inline comments
 
@@ -188,13 +188,13 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
 

	
 
		if(cmd == "#pin") tp->runAsThread(new PINExchangeProcess(np, sessions[user], user, data));
 
		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)));
 
												     boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3, _4)));
 
		}
 
		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,
 
@@ -204,29 +204,30 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
 
		else if(cmd == "#unfollow") tp->runAsThread(new DestroyFriendRequest(sessions[user], user, data.substr(0,data.find('@')),
 
													   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 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;
 
			if(chatroomBuddies[user].count(buddy) == 0) {
 
				tp->runAsThread(new StatusUpdateRequest(sessions[user], user, message,
 
														boost::bind(&TwitterPlugin::statusUpdateResponse, this, _1, _2)));
 
			} else {
 
				data = message.substr(message.find(":")+1);
 
				tp->runAsThread(new DirectMessageRequest(sessions[user], user, buddy, data,
 
												 		 boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3, _4)));
 
			}
 
			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 {	
 
		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)));
 
												 boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3, _4)));
 
		}
 
	}
 
}
 

	
 
void TwitterPlugin::handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) 
 
{
 
@@ -268,13 +269,13 @@ 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 DirectMessageRequest(sessions[user], user, "", mostRecentDirectMessageID[user],
 
											boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3)));
 
											boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3, _4)));
 
		it++;
 
	}
 
	message_timer->start();
 
}
 

	
 

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

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

	
 
@@ -448,31 +451,40 @@ void TwitterPlugin::displayFriendlist(std::string &user, std::vector<User> &frie
 
void TwitterPlugin::displayTweets(std::string &user, std::string &userRequested, std::vector<Status> &tweets , std::string &errMsg)
 
{
 
	if(errMsg.length() == 0) {
 
		std::string timeline = "";
 

	
 
		for(int i=0 ; i<tweets.size() ; i++) {
 
			timeline += " - " + tweets[i].getUserData().getScreenName() + ": " + tweets[i].getTweet() + " (MsgId: " + tweets[i].getID() + ")\n";
 

	
 
			if(twitterMode != CHATROOM) {
 
				timeline += " - " + tweets[i].getUserData().getScreenName() + ": " + tweets[i].getTweet() + " (MsgId: " + tweets[i].getID() + ")\n";
 
			} else {
 
				handleMessage(user, adminLegacyName, tweets[i].getTweet(), tweets[i].getUserData().getScreenName());
 
			}
 
		}
 

	
 
		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
 
		}
 

	
 
		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)
 
void TwitterPlugin::directMessageResponse(std::string &user, std::string &username, std::vector<DirectMessage> &messages, std::string &errMsg)
 
{
 
	if(errMsg.length()) {
 
		handleMessage(user, adminLegacyName, std::string("Error while sending direct message! - ") + errMsg, adminNickName);	
 
		return;
 
	}
 

	
 
	if(username != "") {
 
		handleMessage(user, adminLegacyName, "Message delivered!", adminNickName);	
 
		return;
 
	}
 
	
 
	if(!messages.size()) return;
 
	
 
	if(twitterMode == SINGLECONTACT) {
 

	
 
		std::string msglist = "";
 
@@ -486,20 +498,23 @@ void TwitterPlugin::directMessageResponse(std::string &user, std::vector<DirectM
 
			}
 
		}	
 

	
 
		if(msglist.length()) handleMessage(user, adminLegacyName, msglist, adminNickName);	
 
		updateLastDMID(user, maxID);
 

	
 
	} else if(twitterMode == MULTIPLECONTACT) {
 
	} else {
 
		
 
		std::string msgID = getMostRecentDMID(user);
 
		std::string maxID = msgID;
 

	
 
		for(int i=0 ; i < messages.size() ; i++) {
 
			if(cmp(msgID, messages[i].getID()) == -1) {
 
				handleMessage(user, messages[i].getSenderData().getScreenName(), messages[i].getMessage(), adminNickName);
 
				if(twitterMode == MULTIPLECONTACT)
 
					handleMessage(user, messages[i].getSenderData().getScreenName(), messages[i].getMessage(), adminNickName);
 
				else
 
					handleMessage(user, adminLegacyName, std::string("*") + messages[i].getMessage(), messages[i].getSenderData().getScreenName());
 
				if(cmp(maxID, messages[i].getID()) == -1) maxID = messages[i].getID();
 
			}
 
		}	
 
		
 
		if(maxID == getMostRecentDMID(user)) LOG4CXX_INFO(logger, "No new direct messages for " << user)
 
		updateLastDMID(user, maxID);
 
@@ -510,30 +525,34 @@ void TwitterPlugin::createFriendResponse(std::string &user, std::string &frnd, s
 
{
 
	if(errMsg.length()) {
 
		handleMessage(user, adminLegacyName, errMsg, adminNickName);
 
		return;
 
	}
 

	
 
	if(twitterMode == SINGLECONTACT) {
 
		handleMessage(user, adminLegacyName, std::string("You are now following ") + frnd, adminNickName);
 
	} else if(twitterMode == MULTIPLECONTACT) {
 
		handleMessage(user, adminLegacyName, std::string("You are now following ") + frnd, adminNickName);
 
	handleMessage(user, adminLegacyName, std::string("You are now following ") + frnd, adminNickName);
 
	if(twitterMode == MULTIPLECONTACT) {
 
		handleBuddyChanged(user, frnd, frnd, std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
	} else if(twitterMode == CHATROOM) {
 
		handleParticipantChanged(user, frnd, adminLegacyName, 0, pbnetwork::STATUS_ONLINE);
 
		chatroomBuddies[user].insert(frnd);
 
	}
 
}
 

	
 
void TwitterPlugin::deleteFriendResponse(std::string &user, std::string &frnd, std::string &errMsg)
 
{
 
	if(errMsg.length()) {
 
		handleMessage(user, adminLegacyName, errMsg, adminNickName);
 
		return;
 
	} if(twitterMode == SINGLECONTACT) {
 
		handleMessage(user, adminLegacyName, std::string("You are not following ") + frnd + " anymore", adminNickName);
 
	} else if(twitterMode == MULTIPLECONTACT) {
 
		handleMessage(user, adminLegacyName, std::string("You are not following ") + frnd + " anymore", adminNickName);
 
	} 
 
	
 
	handleMessage(user, adminLegacyName, std::string("You are not following ") + frnd + " anymore", adminNickName);
 
	if(twitterMode == MULTIPLECONTACT) {
 
		handleBuddyRemoved(user, frnd);
 
	} else if (twitterMode == CHATROOM) {
 
		handleParticipantChanged(user, frnd, adminLegacyName, 0, pbnetwork::STATUS_NONE);
 
		chatroomBuddies[user].erase(frnd);
 
	}
 
}
 

	
 

	
 
void TwitterPlugin::RetweetResponse(std::string &user, std::string &errMsg)
 
{
backends/twitter/TwitterPlugin.h
Show inline comments
 
@@ -102,13 +102,13 @@ class TwitterPlugin : public NetworkPlugin {
 
		/****************** 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);
 
		void directMessageResponse(std::string &user, std::vector<DirectMessage> &messages, std::string &errMsg);
 
		void directMessageResponse(std::string &user, std::string &username, std::vector<DirectMessage> &messages, std::string &errMsg);
 
		void createFriendResponse(std::string &user, std::string &frnd, std::string &errMsg);
 
		void deleteFriendResponse(std::string &user, std::string &frnd, std::string &errMsg);
 
		void RetweetResponse(std::string &user, std::string &errMsg);
 
		/***********************************************************************************/
 

	
 
	private:
 
@@ -131,10 +131,11 @@ class TwitterPlugin : public NetworkPlugin {
 
		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;
 
		std::map<std::string,std::string> nickName;
 
		std::map< std::string,std::set<std::string> > chatroomBuddies;
 
		mode twitterMode;
 
};
 

	
 
#endif
0 comments (0 inline, 0 general)