Changeset - 1ad7baa1e4a0
[Not reviewed]
0 1 0
Sarang Bharadwaj - 13 years ago 2012-05-24 14:47:24
sarang.bh@gmail.com
Added code to login to twitter
1 file changed with 35 insertions and 2 deletions:
0 comments (0 inline, 0 general)
backends/twitter/main.cpp
Show inline comments
 
@@ -327,61 +327,94 @@ class TwitterPlugin : public NetworkPlugin {
 
		void 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 _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 handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
 
			LOG4CXX_INFO(logger, std::string("Received login request for ") + user)
 
			if(sessions.count(user)) {
 
				LOG4CXX_INFO(logger, "A session corresponding to " + user + "  is already active\n")
 
				LOG4CXX_INFO(logger, std::string("A session corresponding to ") + user + std::string(" is already active"))
 
				return;
 
			}
 
			
 
			//twitCurl &twitterObj = sessions[user];
 
			//std::string myOAuthAccessTokenSecret, myOAuthAccessTokenKey;
 
        	//twitterObj.getOAuth().getOAuthTokenKey( myOAuthAccessTokenKey );
 
        	//twitterObj.getOAuth().getOAuthTokenSecret( myOAuthAccessTokenSecret );
 

	
 
			//if(myOAuthAccessTokenSecret.size() && myOAuthAccessTokenKey.size()) {	
 
			//}
 
			
 
			std::string username = user.substr(0,user.find('@'));
 
			std::string passwd = "dummy"; // Not needed since we are using OAuth
 
			LOG4CXX_INFO(logger, username + "  " + passwd)
 

	
 
			sessions[user] = new twitCurl();
 
			handleConnected(user);
 
			handleBuddyChanged(user, "twitter-account", "twitter", std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
			connectionState[user] = NEW;
 
			
 
			sessions[user]->setTwitterUsername(username);
 
			sessions[user]->setTwitterPassword(passwd); 
 
			sessions[user]->getOAuth().setConsumerKey( std::string( "qxfSCX7WN7SZl7dshqGZA" ) );
 
			sessions[user]->getOAuth().setConsumerSecret( std::string( "ypWapSj87lswvnksZ46hMAoAZvST4ePGPxAQw6S2o" ) );
 
			
 
			std::string authUrl;
 
			sessions[user]->oAuthRequestToken( authUrl );
 
			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;	
 
		}
 
		
 
		// User logging out
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName) {
 
			delete sessions[user];
 
			sessions[user] = NULL;
 
			connectionState[user] = DISCONNECTED;
 
		}
 

	
 

	
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "") {
 
			LOG4CXX_INFO(logger, "Sending message from " << user << " to " << legacyName << ".");
 
			if(legacyName == "twitter-account") {
 
				handleMessage(user, "twitter-account",message);
 
				if(message.substr(0,3) == "pin") {
 
					sessions[user]->getOAuth().setOAuthPin( message.substr(4) );
 
					sessions[user]->oAuthAccessToken();
 
					connectionState[user] = CONNECTED;
 
					LOG4CXX_INFO(logger, "Sent PIN " << message.substr(4) << " and obtained access token");
 
				}
 
			}
 
		}
 

	
 
		void 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);
 
		}
 

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

	
 
		}
 

	
 
	private:
 
		enum status {NEW, WAITING_FOR_PIN, CONNECTED, DISCONNECTED};
 
		Config *config;
 
		std::map<std::string, twitCurl> sessions;
 
		std::map<std::string, twitCurl*> sessions;
 
		std::map<std::string, status> connectionState;
 
};
 

	
 
static void spectrum_sigchld_handler(int sig)
 
{
 
	int status;
 
	pid_t pid;
 

	
 
	do {
 
		pid = waitpid(-1, &status, WNOHANG);
 
	} while (pid != 0 && pid != (pid_t)-1);
 

	
 
	if ((pid == (pid_t) - 1) && (errno != ECHILD)) {
0 comments (0 inline, 0 general)