Changeset - 5dc0721756d9
[Not reviewed]
1 1 0
Sarang Bharadwaj - 13 years ago 2012-05-24 20:53:51
sarang.bh@gmail.com
Code for logging in and status update
2 files changed with 31 insertions and 4 deletions:
0 comments (0 inline, 0 general)
backends/twitter/.main.cpp.swp
Show inline comments
 
deleted file
 
binary diff not shown
backends/twitter/main.cpp
Show inline comments
 
@@ -312,127 +312,154 @@ class TwitterPlugin : public NetworkPlugin {
 
		Swift::BoostNetworkFactories *m_factories;
 
		Swift::BoostIOServiceThread m_boostIOServiceThread;
 
		boost::shared_ptr<Swift::Connection> m_conn;
 

	
 
		TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
 
			this->config = config;
 
			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));
 

	
 
			LOG4CXX_INFO(logger, "Starting the plugin.");
 
		}
 

	
 
		// Send data to NetworkPlugin server
 
		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, 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);
 
	        
 
			std::string ip = "10.93.0.36";
 
			std::string port = "3128";
 
			std::string puser = "cs09s022";
 
			std::string ppasswd = "$@R@ng123";
 
			sessions[user]->setProxyServerIp(ip);
 
	        sessions[user]->setProxyServerPort(port);
 
	        sessions[user]->setProxyUserName(puser);
 
	        sessions[user]->setProxyPassword(ppasswd);
 
			
 
			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) );
 
				std::string cmd = message.substr(0, message.find(':'));
 
				std::string data = message.substr(message.find(':') + 1);
 
				
 
				handleMessage(user, "twitter-account", cmd + " " + data);
 

	
 
				if(cmd == "pin") {
 
					sessions[user]->getOAuth().setOAuthPin( data );
 
					sessions[user]->oAuthAccessToken();
 
					connectionState[user] = CONNECTED;
 
					LOG4CXX_INFO(logger, "Sent PIN " << message.substr(4) << " and obtained access token");
 
					LOG4CXX_INFO(logger, "Sent PIN " << data << " and obtained access token");
 
				}
 

	
 
				if(cmd == "status") {
 
					LOG4CXX_INFO(logger, "Updating status for " << user << ": " << data);
 
					std::string replyMsg; 
 
					if( sessions[user]->statusUpdate( data ) ) {
 
						sessions[user]->getLastWebResponse( replyMsg );
 
						LOG4CXX_INFO(logger, "twitterClient:: twitCurl::statusUpdate web response: " << replyMsg );
 
					}
 
					else {
 
						sessions[user]->getLastCurlError( replyMsg );
 
						LOG4CXX_INFO(logger, "twitterClient:: twitCurl::statusUpdate error: " << replyMsg );
 
					}
 
				}
 
			}
 
		}
 

	
 
		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, 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)) {
 
		char errmsg[BUFSIZ];
 
		snprintf(errmsg, BUFSIZ, "Warning: waitpid() returned %d", pid);
 
		perror(errmsg);
 
	}
 
}
 

	
 

	
 
int main (int argc, char* argv[]) {
 
	std::string host;
 
	int port;
 

	
 
	if (signal(SIGCHLD, spectrum_sigchld_handler) == SIG_ERR) {
 
		std::cout << "SIGCHLD handler can't be set\n";
 
		return -1;
 
	}
 

	
 
	boost::program_options::options_description desc("Usage: spectrum [OPTIONS] <config_file.cfg>\nAllowed options");
 
	desc.add_options()
0 comments (0 inline, 0 general)