Files
@ 10cef3dd0995
Branch filter:
Location: libtransport.git/backends/twitter/TwitterPlugin.h - annotation
10cef3dd0995
6.2 KiB
text/plain
Merge branch 'master' of https://github.com/hanzz/libtransport
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f b3f59f9bb668 cb62e786889f cb62e786889f f9696d0db6f1 cb62e786889f cb62e786889f cb62e786889f cb62e786889f f9696d0db6f1 cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f 85c28989bdb8 cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f 6bcaaeb731ef cb62e786889f cb62e786889f cb62e786889f cb62e786889f c236c464ffac cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f c79af87410fd c79af87410fd cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f 6e1413bf21d3 6e1413bf21d3 6e1413bf21d3 6e1413bf21d3 cb62e786889f f7dc91ccb482 cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f bdb8aed78f63 bdb8aed78f63 f8a183661cfa c2fefc36c734 c2fefc36c734 f8a183661cfa cb62e786889f cb62e786889f 561a55e96363 561a55e96363 cb62e786889f cb62e786889f c0c5673c97b6 cb62e786889f cb62e786889f cb62e786889f cb62e786889f 728517cf2312 c2fefc36c734 728517cf2312 728517cf2312 cb62e786889f c2fefc36c734 c2fefc36c734 c2fefc36c734 c2fefc36c734 80a0bda3f5bc 80a0bda3f5bc 80a0bda3f5bc 80a0bda3f5bc 80a0bda3f5bc 80a0bda3f5bc 6bcaaeb731ef c236c464ffac 6bcaaeb731ef 6e1413bf21d3 6bcaaeb731ef c236c464ffac 6bcaaeb731ef c236c464ffac 6bcaaeb731ef c236c464ffac 6bcaaeb731ef c236c464ffac 6bcaaeb731ef c236c464ffac 6bcaaeb731ef c236c464ffac 6bcaaeb731ef c236c464ffac 6bcaaeb731ef c236c464ffac 5b13c80ee9ea 5b13c80ee9ea cb62e786889f 9388d599156c 8ee6fb968955 9388d599156c cb62e786889f 5b13c80ee9ea 5b13c80ee9ea cb62e786889f 6e1413bf21d3 80a0bda3f5bc 6e1413bf21d3 6e1413bf21d3 cb62e786889f cb62e786889f cb62e786889f cb62e786889f cb62e786889f 80a0bda3f5bc cb62e786889f cb62e786889f cb62e786889f cb62e786889f f8a183661cfa 9e70a739b7c2 9e70a739b7c2 c0c5673c97b6 561a55e96363 9e70a739b7c2 9e70a739b7c2 9e70a739b7c2 9e70a739b7c2 9e70a739b7c2 9e70a739b7c2 9e70a739b7c2 9e70a739b7c2 9e70a739b7c2 9e70a739b7c2 9e70a739b7c2 9e70a739b7c2 9e70a739b7c2 9e70a739b7c2 9e70a739b7c2 16e3bc94250c cb62e786889f cb62e786889f | #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"
#include "transport/threadpool.h"
#include "Swiften/Swiften.h"
#ifndef _WIN32
#include "unistd.h"
#include "signal.h"
#include "sys/wait.h"
#include "sys/signal.h"
#endif
#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>
#include "Swiften/StringCodecs/SHA1.h"
using namespace boost::filesystem;
using namespace boost::program_options;
using namespace Transport;
#define STR(x) (std::string("(") + x.from + ", " + x.to + ", " + x.message + ")")
class TwitterPlugin;
extern TwitterPlugin *np;
extern Swift::SimpleEventLoop *loop_; // Event Loop
class TwitterPlugin : public NetworkPlugin {
public:
Swift::BoostNetworkFactories *m_factories;
Swift::BoostIOServiceThread m_boostIOServiceThread;
boost::shared_ptr<Swift::Connection> m_conn;
Swift::Timer::ref tweet_timer;
Swift::Timer::ref message_timer;
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);
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*/);
void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "", const std::string &id = "");
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 handleVCardRequest(const std::string &/*user*/, const std::string &/*legacyName*/, unsigned int /*id*/);
void pollForTweets();
void pollForDirectMessages();
bool getUserOAuthKeyAndSecret(const std::string user, std::string &key, std::string &secret);
bool checkSpectrum1User(const std::string user);
bool storeUserOAuthKeyAndSecret(const std::string user, const std::string OAuthKey, const std::string OAuthSecret);
void initUserSession(const std::string user, const std::string legacyName, 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 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);
void clearRoster(const std::string user);
int getTwitterMode(const std::string user);
bool setTwitterMode(const std::string user, int m);
/****************** Twitter response handlers **************************************/
void statusUpdateResponse(std::string &user, Error &errMsg);
void helpMessageResponse(std::string &user, std::string &msg);
void populateRoster(std::string &user, std::vector<User> &friends, std::vector<std::string> &friendAvatars, Error &errMsg);
void displayFriendlist(std::string &user, std::vector<User> &friends, std::vector<std::string> &friendAvatars, Error &errMsg);
void displayTweets(std::string &user, std::string &userRequested, std::vector<Status> &tweets , Error &errMsg);
void directMessageResponse(std::string &user, std::string &username, std::vector<DirectMessage> &messages, Error &errMsg);
void createFriendResponse(std::string &user, User &frnd, std::string &img, Error &errMsg);
void deleteFriendResponse(std::string &user, User &frnd, Error &errMsg);
void RetweetResponse(std::string &user, Error &errMsg);
void profileImageResponse(std::string &user, std::string &buddy, std::string &img, unsigned int reqID, Error &errMsg);
/***********************************************************************************/
private:
std::string getMostRecentTweetIDUnsafe(const std::string user);
std::string getMostRecentDMIDUnsafe(const std::string user);
enum status {NEW, WAITING_FOR_PIN, CONNECTED, DISCONNECTED};
enum mode {SINGLECONTACT, MULTIPLECONTACT, CHATROOM};
Config *config;
std::string adminLegacyName;
std::string adminChatRoom;
std::string adminNickName;
std::string adminAlias;
std::string consumerKey;
std::string consumerSecret;
std::string OAUTH_KEY;
std::string OAUTH_SECRET;
std::string MODE;
boost::mutex dblock, userlock;
ThreadPool *tp;
std::set<std::string> onlineUsers;
struct UserData
{
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;
bool m_firstPing;
};
#endif
|