spectrum2/backends/twitter/TwitterPlugin.h

177 lines
6 KiB
C
Raw Normal View History

2012-06-03 22:34:33 +05:30
#ifndef TWITTER_PLUGIN
#define TWITTER_PLUGIN
#include "transport/config.h"
#include "transport/networkplugin.h"
#include "transport/logging.h"
#include "transport/sqlite3backend.h"
#include "transport/mysqlbackend.h"
#include "transport/pqxxbackend.h"
#include "transport/storagebackend.h"
2012-08-07 09:31:15 +02:00
#include "transport/threadpool.h"
2012-06-03 22:34:33 +05:30
#include "Swiften/Swiften.h"
#include "unistd.h"
#include "signal.h"
#include "sys/wait.h"
#include "sys/signal.h"
#include <boost/algorithm/string.hpp>
#include <boost/signal.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include "twitcurl.h"
#include "TwitterResponseParser.h"
#include <iostream>
#include <sstream>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <cstdio>
2012-07-23 19:22:51 +05:30
#include "Swiften/StringCodecs/SHA1.h"
2012-06-03 22:34:33 +05:30
using namespace boost::filesystem;
using namespace boost::program_options;
using namespace Transport;
#define STR(x) (std::string("(") + x.from + ", " + x.to + ", " + x.message + ")")
2012-07-24 10:56:37 +05:30
2012-06-03 22:34:33 +05:30
class TwitterPlugin;
extern TwitterPlugin *np;
extern Swift::SimpleEventLoop *loop_; // Event Loop
2012-08-12 20:11:06 +05:30
2012-06-03 22:34:33 +05:30
class TwitterPlugin : public NetworkPlugin {
public:
Swift::BoostNetworkFactories *m_factories;
Swift::BoostIOServiceThread m_boostIOServiceThread;
boost::shared_ptr<Swift::Connection> m_conn;
2012-06-12 00:53:40 +05:30
Swift::Timer::ref tweet_timer;
Swift::Timer::ref message_timer;
2012-06-03 22:34:33 +05:30
StorageBackend *storagebackend;
TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port);
~TwitterPlugin();
// Send data to NetworkPlugin server
void sendData(const std::string &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);
// User trying to login into his twitter account
void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password);
// User logging out
void handleLogoutRequest(const std::string &user, const std::string &legacyName);
2012-06-21 17:05:36 +05:30
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*/);
2012-06-03 22:34:33 +05:30
void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "");
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);
2012-07-19 19:11:48 +05:30
void handleVCardRequest(const std::string &/*user*/, const std::string &/*legacyName*/, unsigned int /*id*/);
2012-06-06 16:42:27 +05:30
void pollForTweets();
2012-06-10 22:23:22 +05:30
void pollForDirectMessages();
2012-06-06 16:42:27 +05:30
2012-06-03 22:34:33 +05:30
bool getUserOAuthKeyAndSecret(const std::string user, std::string &key, std::string &secret);
bool checkSpectrum1User(const std::string user);
2012-06-03 22:34:33 +05:30
bool storeUserOAuthKeyAndSecret(const std::string user, const std::string OAuthKey, const std::string OAuthSecret);
2012-08-06 22:48:21 +05:30
void initUserSession(const std::string user, const std::string legacyName, const std::string password);
2012-06-03 22:34:33 +05:30
void OAuthFlowComplete(const std::string user, twitCurl *obj);
void pinExchangeComplete(const std::string user, const std::string OAuthAccessTokenKey, const std::string OAuthAccessTokenSecret);
2012-06-10 22:23:22 +05:30
void updateLastTweetID(const std::string user, const std::string ID);
std::string getMostRecentTweetID(const std::string user);
2012-06-03 22:34:33 +05:30
2012-06-10 22:23:22 +05:30
void updateLastDMID(const std::string user, const std::string ID);
std::string getMostRecentDMID(const std::string user);
2012-07-11 14:12:27 +05:30
void clearRoster(const std::string user);
int getTwitterMode(const std::string user);
bool setTwitterMode(const std::string user, int m);
2012-07-24 10:56:37 +05:30
/****************** Twitter response handlers **************************************/
2012-08-12 20:11:06 +05:30
void statusUpdateResponse(std::string &user, Error &errMsg);
2012-07-24 10:56:37 +05:30
2012-06-21 17:05:36 +05:30
void helpMessageResponse(std::string &user, std::string &msg);
2012-07-24 10:56:37 +05:30
2012-08-12 20:11:06 +05:30
void populateRoster(std::string &user, std::vector<User> &friends, std::vector<std::string> &friendAvatars, Error &errMsg);
2012-07-24 10:56:37 +05:30
2012-08-12 20:11:06 +05:30
void displayFriendlist(std::string &user, std::vector<User> &friends, std::vector<std::string> &friendAvatars, Error &errMsg);
2012-07-24 10:56:37 +05:30
2012-08-12 20:11:06 +05:30
void displayTweets(std::string &user, std::string &userRequested, std::vector<Status> &tweets , Error &errMsg);
2012-07-24 10:56:37 +05:30
2012-08-12 20:11:06 +05:30
void directMessageResponse(std::string &user, std::string &username, std::vector<DirectMessage> &messages, Error &errMsg);
2012-07-24 10:56:37 +05:30
2012-08-12 20:11:06 +05:30
void createFriendResponse(std::string &user, User &frnd, std::string &img, Error &errMsg);
2012-07-24 10:56:37 +05:30
2012-08-12 20:11:06 +05:30
void deleteFriendResponse(std::string &user, User &frnd, Error &errMsg);
2012-07-24 10:56:37 +05:30
2012-08-12 20:11:06 +05:30
void RetweetResponse(std::string &user, Error &errMsg);
2012-07-24 10:56:37 +05:30
2012-08-12 20:11:06 +05:30
void profileImageResponse(std::string &user, std::string &buddy, std::string &img, unsigned int reqID, Error &errMsg);
2012-06-10 21:39:29 +05:30
/***********************************************************************************/
2012-06-03 22:34:33 +05:30
private:
enum status {NEW, WAITING_FOR_PIN, CONNECTED, DISCONNECTED};
2012-06-10 21:39:29 +05:30
enum mode {SINGLECONTACT, MULTIPLECONTACT, CHATROOM};
2012-06-03 22:34:33 +05:30
Config *config;
2012-06-21 17:05:36 +05:30
std::string adminLegacyName;
2012-07-11 14:12:27 +05:30
std::string adminChatRoom;
2012-06-21 17:05:36 +05:30
std::string adminNickName;
std::string adminAlias;
2012-06-03 22:34:33 +05:30
std::string consumerKey;
std::string consumerSecret;
std::string OAUTH_KEY;
std::string OAUTH_SECRET;
2012-07-11 14:12:27 +05:30
std::string MODE;
2012-06-03 22:34:33 +05:30
boost::mutex dblock, userlock;
ThreadPool *tp;
2012-06-06 16:42:27 +05:30
std::set<std::string> onlineUsers;
struct UserData
{
2012-08-06 22:48:21 +05:30
std::string legacyName;
bool spectrum1User; //Legacy support
User userTwitterObj;
std::string userImg;
twitCurl* sessions;
status connectionState;
std::string mostRecentTweetID;
std::string mostRecentDirectMessageID;
std::string nickName;
std::set<std::string> buddies;
std::map<std::string, User> buddiesInfo;
std::map<std::string, std::string> buddiesImgs;
mode twitterMode;
UserData() { sessions = NULL; }
};
std::map<std::string, UserData> userdb;
2012-06-03 22:34:33 +05:30
};
#endif