diff --git a/backends/twitter/Requests/DirectMessageRequest.cpp b/backends/twitter/Requests/DirectMessageRequest.cpp index 5cee8969..cfbf210e 100644 --- a/backends/twitter/Requests/DirectMessageRequest.cpp +++ b/backends/twitter/Requests/DirectMessageRequest.cpp @@ -1,13 +1,17 @@ #include "DirectMessageRequest.h" -#include "../TwitterResponseParser.h" DEFINE_LOGGER(logger, "DirectMessageRequest") void DirectMessageRequest::run() { replyMsg = ""; - success = twitObj->directMessageSend(username, data, false); - if(success) twitObj->getLastWebResponse( replyMsg ); + if(username != "") success = twitObj->directMessageSend(username, data, false); + else success = twitObj->directMessageGet(data); /* data will contain sinceId */ + + if(success) { + twitObj->getLastWebResponse( replyMsg ); + if(username == "" ) messages = getDirectMessages( replyMsg ); + } } void DirectMessageRequest::finalize() @@ -15,11 +19,11 @@ void DirectMessageRequest::finalize() if(!success) { LOG4CXX_ERROR(logger, user << ": Error while sending directed message to " << username ); twitObj->getLastCurlError( replyMsg ); - callBack(user, replyMsg); + callBack(user, messages, replyMsg); } else { std::string error = getErrorMessage(replyMsg); if(error.length()) LOG4CXX_ERROR(logger, user << " - " << error) else LOG4CXX_INFO(logger, user << " - " << replyMsg) - callBack(user, error); + callBack(user, messages, error); } } diff --git a/backends/twitter/Requests/DirectMessageRequest.h b/backends/twitter/Requests/DirectMessageRequest.h index c1e00c53..5eb1d774 100644 --- a/backends/twitter/Requests/DirectMessageRequest.h +++ b/backends/twitter/Requests/DirectMessageRequest.h @@ -2,6 +2,7 @@ #define DIRECT_MESSAGE #include "../ThreadPool.h" +#include "../TwitterResponseParser.h" #include "../libtwitcurl/twitcurl.h" #include "transport/logging.h" #include @@ -17,12 +18,13 @@ class DirectMessageRequest : public Thread std::string user; std::string username; std::string replyMsg; - boost::function< void (std::string&, std::string&) > callBack; + boost::function< void (std::string&, std::vector&, std::string&) > callBack; + std::vector 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::string&) > cb) { + boost::function< void (std::string&, std::vector&, std::string&) > cb) { twitObj = obj->clone(); data = _data; user = _user; diff --git a/backends/twitter/TwitterPlugin.cpp b/backends/twitter/TwitterPlugin.cpp index ecdadaba..9d2fe101 100644 --- a/backends/twitter/TwitterPlugin.cpp +++ b/backends/twitter/TwitterPlugin.cpp @@ -131,7 +131,7 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std: 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))); + boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3))); } else if(cmd == "#status") tp->runAsThread(new StatusUpdateRequest(np, sessions[user], user, data)); else if(cmd == "#timeline") tp->runAsThread(new TimelineRequest(sessions[user], user, data, "", @@ -143,7 +143,7 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std: else { tp->runAsThread(new DirectMessageRequest(sessions[user], user, legacyName, message, - boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2))); + boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3))); } } @@ -354,9 +354,25 @@ void TwitterPlugin::displayTweets(std::string &user, std::string &userRequested, } else handleMessage(user, "twitter-account", errMsg); } -void TwitterPlugin::directMessageResponse(std::string &user, std::string &errMsg) +void TwitterPlugin::directMessageResponse(std::string &user, std::vector &messages, std::string &errMsg) { if(errMsg.length()) { handleMessage(user, "twitter-account", std::string("Error while sending direct message! - ") + errMsg); + return; + } + + if(!messages.size()) return; + + if(twitterMode == SINGLECONTACT) { + std::string msglist = "\n***************MSG LIST****************\n"; + for(int i=0 ; i < messages.size() ; i++) { + msglist += " - " + messages[i].getSenderData().getScreenName() + ": " + messages[i].getMessage() + "\n"; + } + msglist += "***************************************\n"; + handleMessage(user, "twitter-account", msglist); + } else if(twitterMode == MULTIPLECONTACT) { + for(int i=0 ; i < messages.size() ; i++) { + handleMessage(user, messages[i].getSenderData().getScreenName(), messages[i].getMessage()); + } } } diff --git a/backends/twitter/TwitterPlugin.h b/backends/twitter/TwitterPlugin.h index d300e735..08189390 100644 --- a/backends/twitter/TwitterPlugin.h +++ b/backends/twitter/TwitterPlugin.h @@ -98,7 +98,7 @@ class TwitterPlugin : public NetworkPlugin { void populateRoster(std::string &user, std::vector &friends, std::string &errMsg); void displayFriendlist(std::string &user, std::vector &friends, std::string &errMsg); void displayTweets(std::string &user, std::string &userRequested, std::vector &tweets , std::string &errMsg); - void directMessageResponse(std::string &user, std::string &errMsg); + void directMessageResponse(std::string &user, std::vector &messages, std::string &errMsg); /***********************************************************************************/ private: diff --git a/backends/twitter/TwitterResponseParser.cpp b/backends/twitter/TwitterResponseParser.cpp index c2d2eec3..bb6ba276 100644 --- a/backends/twitter/TwitterResponseParser.cpp +++ b/backends/twitter/TwitterResponseParser.cpp @@ -6,7 +6,9 @@ DEFINE_LOGGER(logger, "TwitterResponseParser") User getUser(const Swift::ParserElement::ref &element, const std::string xmlns) { User user; - if(element->getName() != "user") { + if(element->getName() != TwitterReponseTypes::user + && element->getName() != TwitterReponseTypes::sender + && element->getName() != TwitterReponseTypes::recipient) { LOG4CXX_ERROR(logger, "Not a user element!") return user; } @@ -40,6 +42,26 @@ Status getStatus(const Swift::ParserElement::ref &element, const std::string xml return status; } +DirectMessage getDirectMessage(const Swift::ParserElement::ref &element, const std::string xmlns) +{ + DirectMessage DM; + if(element->getName() != TwitterReponseTypes::direct_message) { + LOG4CXX_ERROR(logger, "Not a direct_message element!") + return DM; + } + + DM.setCreationTime( std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) ); + DM.setID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) ); + DM.setMessage( std::string( element->getChild(TwitterReponseTypes::text, xmlns)->getText() ) ); + DM.setSenderID( std::string( element->getChild(TwitterReponseTypes::sender_id, xmlns)->getText() ) ); + DM.setRecipientID( std::string( element->getChild(TwitterReponseTypes::recipient_id, xmlns)->getText() ) ); + DM.setSenderScreenName( std::string( element->getChild(TwitterReponseTypes::sender_screen_name, xmlns)->getText() ) ); + DM.setRecipientScreenName( std::string( element->getChild(TwitterReponseTypes::recipient_screen_name, xmlns)->getText() ) ); + DM.setSenderData( getUser(element->getChild(TwitterReponseTypes::sender, xmlns), xmlns) ); + DM.setRecipientData( getUser(element->getChild(TwitterReponseTypes::recipient, xmlns), xmlns) ); + return DM; +} + std::vector getTimeline(std::string &xml) { std::vector statuses; @@ -60,6 +82,26 @@ std::vector getTimeline(std::string &xml) return statuses; } +std::vector getDirectMessages(std::string &xml) +{ + std::vector DMs; + Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml); + + if(rootElement->getName() != TwitterReponseTypes::directmessages) { + LOG4CXX_ERROR(logger, "XML doesn't correspond to direct-messages") + return DMs; + } + + const std::string xmlns = rootElement->getNamespace(); + const std::vector children = rootElement->getChildren(TwitterReponseTypes::direct_message, xmlns); + + for(int i = 0; i < children.size() ; i++) { + const Swift::ParserElement::ref dm = children[i]; + DMs.push_back(getDirectMessage(dm, xmlns)); + } + return DMs; +} + std::vector getUsers(std::string &xml) { std::vector users; diff --git a/backends/twitter/TwitterResponseParser.h b/backends/twitter/TwitterResponseParser.h index 32acfcf8..d98f99bc 100644 --- a/backends/twitter/TwitterResponseParser.h +++ b/backends/twitter/TwitterResponseParser.h @@ -26,6 +26,14 @@ namespace TwitterReponseTypes const std::string users = "users"; const std::string status = "status"; const std::string error = "error"; + const std::string direct_message = "direct_message"; + const std::string directmessages = "direct-messages"; + const std::string sender_id = "sender_id"; + const std::string recipient_id = "recipient_id"; + const std::string sender_screen_name = "sender_screen_name"; + const std::string recipient_screen_name = "recipient_screen_name"; + const std::string sender = "sender"; + const std::string recipient = "recipient"; }; //Class holding user data @@ -96,10 +104,50 @@ class Status void setRetweeted(bool val) {retweeted = val;} }; +//Class representing a Direct Message +class DirectMessage +{ + std::string created_at; + std::string ID; + std::string text; + std::string sender_id; + std::string recipient_id; + std::string sender_screen_name; + std::string recipient_screen_name; + User sender, recipient; + + public: + DirectMessage():created_at(""),ID(""),text(""),sender_id(""),recipient_id(""), + sender_screen_name(""),recipient_screen_name(""),sender(User()),recipient(User()){} + + std::string getCreationTime() {return created_at;} + std::string getID() {return ID;} + std::string getMessage() {return text;} + std::string getSenderID() {return sender_id;} + std::string getRecipientID() {return recipient_id;} + std::string getSenderScreenName() {return sender_screen_name;} + std::string getRecipientScreenName() {return recipient_screen_name;} + User getSenderData() {return sender;} + User getRecipientData() {return recipient;} + + void setCreationTime(std::string _created) {created_at = _created;} + void setID(std::string _id) {ID = _id;} + void setMessage(std::string _text) {text = _text;} + void setSenderID(std::string _id) {sender_id = _id;} + void setRecipientID(std::string _id) {recipient_id = _id;} + void setSenderScreenName(std::string _name) {sender_screen_name = _name;} + void setRecipientScreenName(std::string _name) {recipient_screen_name = _name;} + void setSenderData(User u) {sender = u;} + void setRecipientData(User u) {recipient = u;} +}; + std::vector getTimeline(std::string &xml); +std::vector getDirectMessages(std::string &xml); std::vector getIDs(std::string &xml); std::vector getUsers(std::string &xml); std::string getErrorMessage(std::string &xml); + Status getStatus(const Swift::ParserElement::ref &element, const std::string xmlns); +DirectMessage getDirectMessage(const Swift::ParserElement::ref &element, const std::string xmlns); User getUser(const Swift::ParserElement::ref &element, const std::string xmlns); #endif diff --git a/backends/twitter/libtwitcurl/twitcurl.cpp b/backends/twitter/libtwitcurl/twitcurl.cpp index 887215a9..21cef059 100644 --- a/backends/twitter/libtwitcurl/twitcurl.cpp +++ b/backends/twitter/libtwitcurl/twitcurl.cpp @@ -711,11 +711,17 @@ bool twitCurl::followersGet( std::string userInfo, bool isUserId ) * response by twitter. Use getLastWebResponse() for that. * *--*/ -bool twitCurl::directMessageGet() +bool twitCurl::directMessageGet( std::string sinceId ) { + std::string buildUrl = twitterDefaults::TWITCURL_DIRECTMESSAGES_URL + twitCurlDefaults::TWITCURL_EXTENSIONFORMATS[m_eApiFormatType]; + + if(sinceId.length()) + { + buildUrl += twitCurlDefaults::TWITCURL_URL_SEP_QUES + twitCurlDefaults::TWITCURL_SINCEID + sinceId; + } + /* Perform GET */ - return performGet( twitterDefaults::TWITCURL_DIRECTMESSAGES_URL + - twitCurlDefaults::TWITCURL_EXTENSIONFORMATS[m_eApiFormatType] ); + return performGet( buildUrl ); } /*++ diff --git a/backends/twitter/libtwitcurl/twitcurl.h b/backends/twitter/libtwitcurl/twitcurl.h index 8db0d682..d04ad042 100644 --- a/backends/twitter/libtwitcurl/twitcurl.h +++ b/backends/twitter/libtwitcurl/twitcurl.h @@ -163,7 +163,7 @@ public: bool followersGet( std::string userInfo = "" /* in */, bool isUserId = false /* in */ ); /* Twitter direct message APIs */ - bool directMessageGet(); + bool directMessageGet( std::string sinceId /* in */ ); bool directMessageSend( std::string& userInfo /* in */, std::string& dMsg /* in */, bool isUserId = false /* in */ ); bool directMessageGetSent(); bool directMessageDestroyById( std::string& dMsgId /* in */ );