From d3a10049eee36dc0c451cbed542314a94f62ab3c Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 31 Mar 2015 16:42:07 +0000 Subject: Twitter: - unused module removed; - copyright update; - code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@12575 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Twitter/src/connection.cpp | 392 +++++++++++++++++------------------ 1 file changed, 187 insertions(+), 205 deletions(-) (limited to 'protocols/Twitter/src/connection.cpp') diff --git a/protocols/Twitter/src/connection.cpp b/protocols/Twitter/src/connection.cpp index a0b990dfc1..2db1d892b9 100644 --- a/protocols/Twitter/src/connection.cpp +++ b/protocols/Twitter/src/connection.cpp @@ -1,4 +1,5 @@ /* +Copyright © 2012-15 Miranda NG team Copyright © 2009 Jim Porter This program is free software: you can redistribute it and/or modify @@ -25,23 +26,23 @@ void CALLBACK TwitterProto::APC_callback(ULONG_PTR p) } template -inline static T db_pod_get(MCONTACT hContact,const char *module,const char *setting,T errorValue) +inline static T db_pod_get(MCONTACT hContact, const char *module, const char *setting, T errorValue) { DBVARIANT dbv; - if(db_get(hContact, module, setting, &dbv)) + if (db_get(hContact, module, setting, &dbv)) return errorValue; // TODO: remove this, it's just a temporary workaround - if(dbv.type == DBVT_DWORD) + if (dbv.type == DBVT_DWORD) return dbv.dVal; - if(dbv.cpbVal != sizeof(T)) + if (dbv.cpbVal != sizeof(T)) return errorValue; return *reinterpret_cast(dbv.pbVal); } template -inline static INT_PTR db_pod_set(MCONTACT hContact,const char *module,const char *setting,T val) +inline static INT_PTR db_pod_set(MCONTACT hContact, const char *module, const char *setting, T val) { return db_set_blob(hContact, module, setting, &val, sizeof(T)); } @@ -49,29 +50,27 @@ inline static INT_PTR db_pod_set(MCONTACT hContact,const char *module,const char void TwitterProto::SignOn(void*) { debugLogA("***** Beginning SignOn process"); - WaitForSingleObject(&signon_lock_,INFINITE); + mir_cslock lck(signon_lock_); // Kill the old thread if it's still around // this doesn't seem to work.. should we wait infinitely? - if(hMsgLoop_) - { + if (hMsgLoop_) { debugLogA("***** Requesting MessageLoop to exit"); - QueueUserAPC(APC_callback,hMsgLoop_,(ULONG_PTR)this); + QueueUserAPC(APC_callback, hMsgLoop_, (ULONG_PTR)this); debugLogA("***** Waiting for old MessageLoop to exit"); //WaitForSingleObject(hMsgLoop_,INFINITE); - WaitForSingleObject(hMsgLoop_,180000); + WaitForSingleObject(hMsgLoop_, 180000); CloseHandle(hMsgLoop_); } - if(NegotiateConnection()) // Could this be? The legendary Go Time?? + if (NegotiateConnection()) // Could this be? The legendary Go Time?? { - if(!in_chat_ && db_get_b(0,m_szModuleName,TWITTER_KEY_CHATFEED,0)) - OnJoinChat(0,true); - + if (!in_chat_ && db_get_b(0, m_szModuleName, TWITTER_KEY_CHATFEED, 0)) + OnJoinChat(0, true); + SetAllContactStatuses(ID_STATUS_ONLINE); hMsgLoop_ = ForkThreadEx(&TwitterProto::MessageLoop, NULL, 0); } - ReleaseMutex(signon_lock_); debugLogA("***** SignOn complete"); } @@ -85,50 +84,50 @@ bool TwitterProto::NegotiateConnection() DBVARIANT dbv; wstring oauthToken; - wstring oauthTokenSecret; + wstring oauthTokenSecret; wstring oauthAccessToken; wstring oauthAccessTokenSecret; string screenName; - INT_PTR dbTOK = db_get_ws(0,m_szModuleName,TWITTER_KEY_OAUTH_TOK,&dbv); + INT_PTR dbTOK = db_get_ws(0, m_szModuleName, TWITTER_KEY_OAUTH_TOK, &dbv); if (!dbTOK) { oauthToken = dbv.pwszVal; db_free(&dbv); //debugLogW("**NegotiateConnection - we have an oauthToken already in the db - %s", oauthToken); } - - INT_PTR dbTOKSec = db_get_ws(0,m_szModuleName,TWITTER_KEY_OAUTH_TOK_SECRET,&dbv); + + INT_PTR dbTOKSec = db_get_ws(0, m_szModuleName, TWITTER_KEY_OAUTH_TOK_SECRET, &dbv); if (!dbTOKSec) { oauthTokenSecret = dbv.pwszVal; db_free(&dbv); //debugLogW("**NegotiateConnection - we have an oauthTokenSecret already in the db - %s", oauthTokenSecret); } - INT_PTR dbName = db_get_s(0,m_szModuleName,TWITTER_KEY_NICK,&dbv); + INT_PTR dbName = db_get_s(0, m_szModuleName, TWITTER_KEY_NICK, &dbv); if (!dbName) { screenName = dbv.pszVal; db_free(&dbv); //debugLogW("**NegotiateConnection - we have a username already in the db - %s", screenName); } else { - dbName = db_get_s(0,m_szModuleName,TWITTER_KEY_UN,&dbv); + dbName = db_get_s(0, m_szModuleName, TWITTER_KEY_UN, &dbv); if (!dbName) { screenName = dbv.pszVal; - db_set_s(0,m_szModuleName,TWITTER_KEY_NICK,dbv.pszVal); + db_set_s(0, m_szModuleName, TWITTER_KEY_NICK, dbv.pszVal); db_free(&dbv); //debugLogW("**NegotiateConnection - we have a username already in the db - %s", screenName); } } - // twitter changed the base URL in v1.1 of the API, I don't think users will need to modify it, so + // twitter changed the base URL in v1.1 of the API, I don't think users will need to modify it, so // i'll be forcing it to the new API URL here. After a while I can get rid of this as users will // have either had this run at least once, or have reset their miranda profile. 14/10/2012 - if(db_get_b(0,m_szModuleName,"UpgradeBaseURL",1)) { - db_set_s(0,m_szModuleName,TWITTER_KEY_BASEURL,"https://api.twitter.com/"); - db_set_b(0,m_szModuleName,"UpgradeBaseURL",0); + if (db_get_b(0, m_szModuleName, "UpgradeBaseURL", 1)) { + db_set_s(0, m_szModuleName, TWITTER_KEY_BASEURL, "https://api.twitter.com/"); + db_set_b(0, m_szModuleName, "UpgradeBaseURL", 0); } - if((oauthToken.size() <= 1) || (oauthTokenSecret.size() <= 1)) { + if ((oauthToken.size() <= 1) || (oauthTokenSecret.size() <= 1)) { // first, reset all the keys so we can start fresh resetOAuthKeys(); debugLogA("**NegotiateConnection - Reset OAuth Keys"); @@ -163,46 +162,46 @@ bool TwitterProto::NegotiateConnection() } //write those bitches to the db foe latta - db_set_ws(0,m_szModuleName,TWITTER_KEY_OAUTH_TOK,oauthToken.c_str()); - db_set_ws(0,m_szModuleName,TWITTER_KEY_OAUTH_TOK_SECRET,oauthTokenSecret.c_str()); - + db_set_ws(0, m_szModuleName, TWITTER_KEY_OAUTH_TOK, oauthToken.c_str()); + db_set_ws(0, m_szModuleName, TWITTER_KEY_OAUTH_TOK_SECRET, oauthTokenSecret.c_str()); + // this looks like bad code.. can someone clean this up please? or confirm that it's ok wchar_t buf[1024] = {}; mir_snwprintf(buf, SIZEOF(buf), AuthorizeUrl.c_str(), oauthToken.c_str()); - debugLogW( _T("**NegotiateConnection - Launching %s"), buf); + debugLogW(_T("**NegotiateConnection - Launching %s"), buf); ShellExecute(NULL, L"open", buf, NULL, NULL, SW_SHOWNORMAL); - + ShowPinDialog(); } - if (!db_get_ts(NULL,m_szModuleName,TWITTER_KEY_GROUP,&dbv)) { - CallService(MS_CLIST_GROUPCREATE, 0, (LPARAM)dbv.ptszVal ); - db_free(&dbv); + if (!db_get_ts(NULL, m_szModuleName, TWITTER_KEY_GROUP, &dbv)) { + CallService(MS_CLIST_GROUPCREATE, 0, (LPARAM)dbv.ptszVal); + db_free(&dbv); } bool realAccessTok = false; bool realAccessTokSecret = false; // remember, dbTOK is 0 (false) if the db setting has returned something - dbTOK = db_get_ws(0,m_szModuleName,TWITTER_KEY_OAUTH_ACCESS_TOK,&dbv); - if (!dbTOK) { + dbTOK = db_get_ws(0, m_szModuleName, TWITTER_KEY_OAUTH_ACCESS_TOK, &dbv); + if (!dbTOK) { oauthAccessToken = dbv.pwszVal; db_free(&dbv); // this bit is saying "if we have found the db key, but it contains no data, then set dbTOK to 1" - if (oauthAccessToken.size() > 1) { - realAccessTok = true; + if (oauthAccessToken.size() > 1) { + realAccessTok = true; //debugLogW("**NegotiateConnection - we have an oauthAccessToken already in the db - %s", oauthAccessToken); } else { debugLogA("**NegotiateConnection - oauthAccesToken too small? this is.. weird."); } } - dbTOKSec = db_get_ws(0,m_szModuleName,TWITTER_KEY_OAUTH_ACCESS_TOK_SECRET,&dbv); - if (!dbTOKSec) { + dbTOKSec = db_get_ws(0, m_szModuleName, TWITTER_KEY_OAUTH_ACCESS_TOK_SECRET, &dbv); + if (!dbTOKSec) { oauthAccessTokenSecret = dbv.pwszVal; db_free(&dbv); - if (oauthAccessTokenSecret.size() > 1) { - realAccessTokSecret = true; + if (oauthAccessTokenSecret.size() > 1) { + realAccessTokSecret = true; //debugLogW("**NegotiateConnection - we have an oauthAccessTokenSecret already in the db - %s", oauthAccessTokenSecret); } else { debugLogA("**NegotiateConnection - oauthAccessTokenSecret too small? weird"); } @@ -211,7 +210,7 @@ bool TwitterProto::NegotiateConnection() if (!realAccessTok || !realAccessTokSecret) { // if we don't have one of these beasties then lets go get 'em! wstring pin; debugLogA("**NegotiateConnection - either the accessToken or accessTokenSecret was not there.."); - if (!db_get_ws(0,m_szModuleName,TWITTER_KEY_OAUTH_PIN,&dbv)) { + if (!db_get_ws(0, m_szModuleName, TWITTER_KEY_OAUTH_PIN, &dbv)) { pin = dbv.pwszVal; //debugLogW("**NegotiateConnection - we have an pin already in the db - %s", pin); db_free(&dbv); @@ -221,12 +220,12 @@ bool TwitterProto::NegotiateConnection() debugLogA("**NegotiateConnection - We don't have a PIN? this doesn't make sense. Resetting OAuth keys and setting offline."); resetOAuthKeys(); - ProtoBroadcastAck(0,ACKTYPE_STATUS,ACKRESULT_FAILED,(HANDLE)old_status,m_iStatus); + ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_FAILED, (HANDLE)old_status, m_iStatus); // Set to offline old_status = m_iStatus; m_iDesiredStatus = m_iStatus = ID_STATUS_OFFLINE; - ProtoBroadcastAck(0,ACKTYPE_STATUS,ACKRESULT_SUCCESS,(HANDLE)old_status,m_iStatus); + ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus); return false; } @@ -248,12 +247,12 @@ bool TwitterProto::NegotiateConnection() resetOAuthKeys(); - ProtoBroadcastAck(0,ACKTYPE_STATUS,ACKRESULT_FAILED,(HANDLE)old_status,m_iStatus); + ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_FAILED, (HANDLE)old_status, m_iStatus); // Set to offline old_status = m_iStatus; m_iDesiredStatus = m_iStatus = ID_STATUS_OFFLINE; - ProtoBroadcastAck(0,ACKTYPE_STATUS,ACKRESULT_SUCCESS,(HANDLE)old_status,m_iStatus); + ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus); return false; } @@ -262,7 +261,7 @@ bool TwitterProto::NegotiateConnection() wstring rdata_WSTR2 = UTF8ToWide(accessResp.data); //debugLogW("**NegotiateConnection - accessToken STring is %s", rdata_WSTR2); - + OAuthParameters accessTokenParameters = twit_.ParseQueryString(rdata_WSTR2); oauthAccessToken = accessTokenParameters[L"oauth_token"]; @@ -273,29 +272,28 @@ bool TwitterProto::NegotiateConnection() screenName = WideToUTF8(accessTokenParameters[L"screen_name"]); debugLogA("**NegotiateConnection - screen name is %s", screenName.c_str()); - + //save em - db_set_ws(0,m_szModuleName,TWITTER_KEY_OAUTH_ACCESS_TOK,oauthAccessToken.c_str()); - db_set_ws(0,m_szModuleName,TWITTER_KEY_OAUTH_ACCESS_TOK_SECRET,oauthAccessTokenSecret.c_str()); - db_set_s(0,m_szModuleName,TWITTER_KEY_NICK,screenName.c_str()); - db_set_s(0,m_szModuleName,TWITTER_KEY_UN,screenName.c_str()); + db_set_ws(0, m_szModuleName, TWITTER_KEY_OAUTH_ACCESS_TOK, oauthAccessToken.c_str()); + db_set_ws(0, m_szModuleName, TWITTER_KEY_OAUTH_ACCESS_TOK_SECRET, oauthAccessTokenSecret.c_str()); + db_set_s(0, m_szModuleName, TWITTER_KEY_NICK, screenName.c_str()); + db_set_s(0, m_szModuleName, TWITTER_KEY_UN, screenName.c_str()); } } -/* if( !db_get_s(0,m_szModuleName,TWITTER_KEY_PASS,&dbv)) { - CallService(MS_DB_CRYPT_DECODESTRING,strlen(dbv.pszVal)+1, + /* if( !db_get_s(0,m_szModuleName,TWITTER_KEY_PASS,&dbv)) { + CallService(MS_DB_CRYPT_DECODESTRING,strlen(dbv.pszVal)+1, reinterpret_cast(dbv.pszVal)); - pass = dbv.pszVal; - db_free(&dbv); - } - else { - ShowPopup(TranslateT("Please enter a password.")); - return false; - }*/ + pass = dbv.pszVal; + db_free(&dbv); + } + else { + ShowPopup(TranslateT("Please enter a password.")); + return false; + }*/ - if( !db_get_s(0,m_szModuleName,TWITTER_KEY_BASEURL,&dbv)) - { - ScopedLock s(twitter_lock_); + if (!db_get_s(0, m_szModuleName, TWITTER_KEY_BASEURL, &dbv)) { + mir_cslock s(twitter_lock_); twit_.set_base_url(dbv.pszVal); db_free(&dbv); } @@ -308,30 +306,30 @@ bool TwitterProto::NegotiateConnection() } bool success; - { - ScopedLock s(twitter_lock_); + { + mir_cslock s(twitter_lock_); success = twit_.set_credentials(screenName, ConsumerKey, ConsumerSecret, oauthAccessToken, oauthAccessTokenSecret, L"", true); } - if(!success) { + if (!success) { //ShowPopup(TranslateT("Something went wrong with authorization, OAuth keys have been reset. Please try to reconnect. If problems persist, please se your doctor")); debugLogA("**NegotiateConnection - Verifying credentials failed! No internet maybe?"); //resetOAuthKeys(); - ProtoBroadcastAck(0,ACKTYPE_STATUS,ACKRESULT_FAILED,(HANDLE)old_status,m_iStatus); + ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_FAILED, (HANDLE)old_status, m_iStatus); // Set to offline old_status = m_iStatus; m_iDesiredStatus = m_iStatus = ID_STATUS_OFFLINE; - ProtoBroadcastAck(0,ACKTYPE_STATUS,ACKRESULT_SUCCESS,(HANDLE)old_status,m_iStatus); + ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus); return false; } else { m_iStatus = m_iDesiredStatus; - ProtoBroadcastAck(0,ACKTYPE_STATUS,ACKRESULT_SUCCESS,(HANDLE)old_status,m_iStatus); + ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus); return true; } } @@ -341,45 +339,44 @@ void TwitterProto::MessageLoop(void*) { debugLogA("***** Entering Twitter::MessageLoop"); - since_id_ = db_pod_get(0,m_szModuleName,TWITTER_KEY_SINCEID,0); - dm_since_id_ = db_pod_get(0,m_szModuleName,TWITTER_KEY_DMSINCEID,0); + since_id_ = db_pod_get(0, m_szModuleName, TWITTER_KEY_SINCEID, 0); + dm_since_id_ = db_pod_get(0, m_szModuleName, TWITTER_KEY_DMSINCEID, 0); - bool new_account = db_get_b(0,m_szModuleName,TWITTER_KEY_NEW,1) != 0; - bool popups = db_get_b(0,m_szModuleName,TWITTER_KEY_POPUP_SIGNON,1) != 0; + bool new_account = db_get_b(0, m_szModuleName, TWITTER_KEY_NEW, 1) != 0; + bool popups = db_get_b(0, m_szModuleName, TWITTER_KEY_POPUP_SIGNON, 1) != 0; // if this isn't set, it will automatically not turn a tweet into a msg. probably should make the default that it does turn a tweet into a message - bool tweetToMsg = db_get_b(0,m_szModuleName,TWITTER_KEY_TWEET_TO_MSG,0) != 0; + bool tweetToMsg = db_get_b(0, m_szModuleName, TWITTER_KEY_TWEET_TO_MSG, 0) != 0; - int poll_rate = db_get_dw(0,m_szModuleName,TWITTER_KEY_POLLRATE,80); + int poll_rate = db_get_dw(0, m_szModuleName, TWITTER_KEY_POLLRATE, 80); - for(unsigned int i=0;;i++) - { + for (unsigned int i = 0;; i++) { - if(m_iStatus != ID_STATUS_ONLINE) + if (m_iStatus != ID_STATUS_ONLINE) break; -// if(i%10 == 0) -// UpdateFriends(); + // if(i%10 == 0) + // UpdateFriends(); - if(m_iStatus != ID_STATUS_ONLINE) + if (m_iStatus != ID_STATUS_ONLINE) break; - UpdateStatuses(new_account,popups, tweetToMsg); + UpdateStatuses(new_account, popups, tweetToMsg); - if(m_iStatus != ID_STATUS_ONLINE) + if (m_iStatus != ID_STATUS_ONLINE) break; - - if(i%10 == 0) + + if (i % 10 == 0) UpdateMessages(new_account); - if(new_account) // Not anymore! + if (new_account) // Not anymore! { new_account = false; - db_set_b(0,m_szModuleName,TWITTER_KEY_NEW,0); + db_set_b(0, m_szModuleName, TWITTER_KEY_NEW, 0); } - if(m_iStatus != ID_STATUS_ONLINE) + if (m_iStatus != ID_STATUS_ONLINE) break; debugLogA("***** TwitterProto::MessageLoop going to sleep..."); - if(SleepEx(poll_rate*1000,true) == WAIT_IO_COMPLETION) + if (SleepEx(poll_rate * 1000, true) == WAIT_IO_COMPLETION) break; debugLogA("***** TwitterProto::MessageLoop waking up..."); @@ -387,15 +384,15 @@ void TwitterProto::MessageLoop(void*) } { - ScopedLock s(twitter_lock_); - twit_.set_credentials("",L"",L"",L"",L"",L"", false); + mir_cslock s(twitter_lock_); + twit_.set_credentials("", L"", L"", L"", L"", L"", false); } debugLogA("***** Exiting TwitterProto::MessageLoop"); } struct update_avatar { - update_avatar(MCONTACT hContact,const std::string &url) : hContact(hContact),url(url) {} + update_avatar(MCONTACT hContact, const std::string &url) : hContact(hContact), url(url) {} MCONTACT hContact; std::string url; }; @@ -403,22 +400,22 @@ struct update_avatar /* void *p should always be a struct of type update_avatar */ void TwitterProto::UpdateAvatarWorker(void *p) { - if(p == 0) + if (p == 0) return; - std::auto_ptr data( static_cast(p)); - DBVARIANT dbv = {0}; + std::auto_ptr data(static_cast(p)); + DBVARIANT dbv = { 0 }; // db_get_s returns 0 when it suceeds, so if this suceeds it will return 0, or false. // therefore if it returns 1, or true, we want to return as there is no such user. // as a side effect, dbv now has the username in it i think - if(db_get_ts(data->hContact,m_szModuleName,TWITTER_KEY_UN,&dbv)) + if (db_get_ts(data->hContact, m_szModuleName, TWITTER_KEY_UN, &dbv)) return; std::string ext = data->url.substr(data->url.rfind('.')); // finds the filetype of the avatar std::tstring filename = GetAvatarFolder() + _T('\\') + dbv.ptszVal + (TCHAR*)_A2T(ext.c_str()); // local filename and path db_free(&dbv); - PROTO_AVATAR_INFORMATIONT ai = {sizeof(ai)}; + PROTO_AVATAR_INFORMATIONT ai = { sizeof(ai) }; ai.hContact = data->hContact; ai.format = ProtoGetAvatarFormat(filename.c_str()); @@ -426,11 +423,12 @@ void TwitterProto::UpdateAvatarWorker(void *p) debugLogA("***** Update avatar: Terminated for this contact, extension format unknown for %s", data->url.c_str()); return; // lets just ignore unknown formats... if not it crashes miranda. should probably speak to borkra about this. } - - _tcsncpy(ai.filename,filename.c_str(),MAX_PATH); // puts the local file name in the avatar struct, to a max of 260 chars (as of now) + + _tcsncpy(ai.filename, filename.c_str(), MAX_PATH); // puts the local file name in the avatar struct, to a max of 260 chars (as of now) debugLogA("***** Updating avatar: %s", data->url.c_str()); - WaitForSingleObjectEx(avatar_lock_,INFINITE,true); + mir_cslock lck(avatar_lock_); + if (CallService(MS_SYSTEM_TERMINATED, 0, 0)) // if miranda is shutting down... { debugLogA("***** Terminating avatar update early: %s", data->url.c_str()); @@ -442,32 +440,27 @@ void TwitterProto::UpdateAvatarWorker(void *p) ProtoBroadcastAck(data->hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, &ai, 0); } else ProtoBroadcastAck(data->hContact, ACKTYPE_AVATAR, ACKRESULT_FAILED, &ai, 0); - - ReleaseMutex(avatar_lock_); + debugLogA("***** Done avatar: %s", data->url.c_str()); } -void TwitterProto::UpdateAvatar(MCONTACT hContact,const std::string &url,bool force) +void TwitterProto::UpdateAvatar(MCONTACT hContact, const std::string &url, bool force) { - DBVARIANT dbv = {0}; + DBVARIANT dbv = { 0 }; - if( !force && (!db_get_s(hContact,m_szModuleName,TWITTER_KEY_AV_URL,&dbv) && url == dbv.pszVal)) - { + if (!force && (!db_get_s(hContact, m_szModuleName, TWITTER_KEY_AV_URL, &dbv) && url == dbv.pszVal)) { debugLogA("***** Avatar already up-to-date: %s", url.c_str()); } - else - { + else { // TODO: more defaults (configurable?) - if(url == "http://static.twitter.com/images/default_profile_normal.png") - { - PROTO_AVATAR_INFORMATIONT ai = {sizeof(ai),hContact}; - - db_set_s(hContact,m_szModuleName,TWITTER_KEY_AV_URL,url.c_str()); - ProtoBroadcastAck(hContact,ACKTYPE_AVATAR,ACKRESULT_SUCCESS,&ai,0); + if (url == "http://static.twitter.com/images/default_profile_normal.png") { + PROTO_AVATAR_INFORMATIONT ai = { sizeof(ai), hContact }; + + db_set_s(hContact, m_szModuleName, TWITTER_KEY_AV_URL, url.c_str()); + ProtoBroadcastAck(hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, &ai, 0); } - else - { - ForkThread(&TwitterProto::UpdateAvatarWorker, new update_avatar(hContact,url)); + else { + ForkThread(&TwitterProto::UpdateAvatarWorker, new update_avatar(hContact, url)); } } @@ -476,24 +469,24 @@ void TwitterProto::UpdateAvatar(MCONTACT hContact,const std::string &url,bool fo void TwitterProto::UpdateFriends() { - try - { - ScopedLock s(twitter_lock_); - std::vector friends = twit_.get_friends(); - s.Unlock(); - for(std::vector::iterator i=friends.begin(); i!=friends.end(); ++i) + try { + std::vector friends; { - if(i->username == twit_.get_username()) + mir_cslock s(twitter_lock_); + friends = twit_.get_friends(); + } + + for (std::vector::iterator i = friends.begin(); i != friends.end(); ++i) { + if (i->username == twit_.get_username()) continue; - MCONTACT hContact = AddToClientList(i->username.c_str(),i->status.text.c_str()); - UpdateAvatar(hContact,i->profile_image_url); + MCONTACT hContact = AddToClientList(i->username.c_str(), i->status.text.c_str()); + UpdateAvatar(hContact, i->profile_image_url); } - disconnectionCount = 0; + disconnectionCount = 0; debugLogA("***** Friends list updated"); } - catch(const bad_response &) - { + catch (const bad_response &) { ++disconnectionCount; debugLogA("***** UpdateFriends - Bad response from server, this has happened %d time(s)", disconnectionCount); if (disconnectionCount > 2) { @@ -501,69 +494,61 @@ void TwitterProto::UpdateFriends() SetStatus(ID_STATUS_OFFLINE); } } - catch(const std::exception &e) - { - ShowPopup( (std::string("While updating friends list, an error occurred: ")+e.what()).c_str()); + catch (const std::exception &e) { + ShowPopup((std::string("While updating friends list, an error occurred: ") + e.what()).c_str()); debugLogA("***** Error updating friends list: %s", e.what()); } - } - LRESULT CALLBACK PopupWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { - switch (message) - { + std::string *url; + + switch (message) { case WM_COMMAND: - { // Get the plugin data (we need the Popup service to do it) - std::string *url = (std::string *)PUGetPluginData(hwnd); + url = (std::string *)PUGetPluginData(hwnd); if (url != NULL) { //std::string url = profile_base_url("https://twitter.com/") + http::url_encode(dbv.pszVal); CallService(MS_UTILS_OPENURL, OUF_NEWWINDOW, reinterpret_cast(url->c_str())); } // Intentionally no break here - } + case WM_CONTEXTMENU: - { // After a click, destroy popup PUDeletePopup(hwnd); return TRUE; - } + case UM_FREEPLUGINDATA: - { // After close, free - std::string *url = (std::string *)PUGetPluginData(hwnd); + url = (std::string *)PUGetPluginData(hwnd); delete url; return FALSE; } - } return DefWindowProc(hwnd, message, wParam, lParam); }; -void TwitterProto::ShowContactPopup(MCONTACT hContact,const std::string &text,const std::string *url) +void TwitterProto::ShowContactPopup(MCONTACT hContact, const std::string &text, const std::string *url) { - if(!ServiceExists(MS_POPUP_ADDPOPUPT) || db_get_b(0,m_szModuleName,TWITTER_KEY_POPUP_SHOW,0) == 0) - { + if (!ServiceExists(MS_POPUP_ADDPOPUPT) || db_get_b(0, m_szModuleName, TWITTER_KEY_POPUP_SHOW, 0) == 0) { return; } POPUPDATAT popup = {}; popup.lchContact = hContact; - popup.iSeconds = db_get_dw(0,m_szModuleName,TWITTER_KEY_POPUP_TIMEOUT,0); - - popup.colorBack = db_get_dw(0,m_szModuleName,TWITTER_KEY_POPUP_COLBACK,0); - if(popup.colorBack == 0xFFFFFFFF) + popup.iSeconds = db_get_dw(0, m_szModuleName, TWITTER_KEY_POPUP_TIMEOUT, 0); + + popup.colorBack = db_get_dw(0, m_szModuleName, TWITTER_KEY_POPUP_COLBACK, 0); + if (popup.colorBack == 0xFFFFFFFF) popup.colorBack = GetSysColor(COLOR_WINDOW); - popup.colorText = db_get_dw(0,m_szModuleName,TWITTER_KEY_POPUP_COLTEXT,0); - if(popup.colorBack == 0xFFFFFFFF) + popup.colorText = db_get_dw(0, m_szModuleName, TWITTER_KEY_POPUP_COLTEXT, 0); + if (popup.colorBack == 0xFFFFFFFF) popup.colorBack = GetSysColor(COLOR_WINDOWTEXT); DBVARIANT dbv; - if( !db_get_ts(hContact,"CList","MyHandle",&dbv) || !db_get_ts(hContact,m_szModuleName,TWITTER_KEY_UN,&dbv)) - { - _tcsncpy(popup.lptzContactName,dbv.ptszVal,MAX_CONTACTNAME); + if (!db_get_ts(hContact, "CList", "MyHandle", &dbv) || !db_get_ts(hContact, m_szModuleName, TWITTER_KEY_UN, &dbv)) { + _tcsncpy(popup.lptzContactName, dbv.ptszVal, MAX_CONTACTNAME); db_free(&dbv); } @@ -572,38 +557,38 @@ void TwitterProto::ShowContactPopup(MCONTACT hContact,const std::string &text,co popup.PluginData = (void *)url; } - mbcs_to_tcs(CP_UTF8,text.c_str(),popup.lptzText,MAX_SECONDLINE); + mbcs_to_tcs(CP_UTF8, text.c_str(), popup.lptzText, MAX_SECONDLINE); PUAddPopupT(&popup); } void TwitterProto::UpdateStatuses(bool pre_read, bool popups, bool tweetToMsg) { - try - { - ScopedLock s(twitter_lock_); - twitter::status_list updates = twit_.get_statuses(200,since_id_); - s.Unlock(); - if(!updates.empty()) { - since_id_ = std::max(since_id_, updates[0].status.id); + try { + twitter::status_list updates; + { + mir_cslock s(twitter_lock_); + updates = twit_.get_statuses(200, since_id_); } - for(twitter::status_list::reverse_iterator i=updates.rbegin(); i!=updates.rend(); ++i) - { + if (!updates.empty()) + since_id_ = std::max(since_id_, updates[0].status.id); - if(!pre_read && in_chat_) + for (twitter::status_list::reverse_iterator i = updates.rbegin(); i != updates.rend(); ++i) { + + if (!pre_read && in_chat_) UpdateChat(*i); - if(i->username == twit_.get_username()) + if (i->username == twit_.get_username()) continue; - MCONTACT hContact = AddToClientList(i->username.c_str(),""); - UpdateAvatar(hContact,i->profile_image_url); // as UpdateFriends() doesn't work at the moment, i'm going to update the avatars here + MCONTACT hContact = AddToClientList(i->username.c_str(), ""); + UpdateAvatar(hContact, i->profile_image_url); // as UpdateFriends() doesn't work at the moment, i'm going to update the avatars here // i think we maybe should just do that DBEF_READ line instead of stopping ALL this code. have to test. if (tweetToMsg) { - DBEVENTINFO dbei = {sizeof(dbei)}; + DBEVENTINFO dbei = { sizeof(dbei) }; dbei.pBlob = (BYTE*)(i->status.text.c_str()); - dbei.cbBlob = (int)i->status.text.size()+1; + dbei.cbBlob = (int)i->status.text.size() + 1; dbei.eventType = TWITTER_DB_EVENT_TYPE_TWEET; dbei.flags = DBEF_UTF | DBEF_READ; dbei.timestamp = static_cast(i->status.time); @@ -611,7 +596,7 @@ void TwitterProto::UpdateStatuses(bool pre_read, bool popups, bool tweetToMsg) db_event_add(hContact, &dbei); } - db_set_utf(hContact,"CList","StatusMsg",i->status.text.c_str()); + db_set_utf(hContact, "CList", "StatusMsg", i->status.text.c_str()); if (!pre_read && popups) { std::stringstream url; @@ -620,12 +605,11 @@ void TwitterProto::UpdateStatuses(bool pre_read, bool popups, bool tweetToMsg) } } - db_pod_set(0,m_szModuleName,TWITTER_KEY_SINCEID,since_id_); + db_pod_set(0, m_szModuleName, TWITTER_KEY_SINCEID, since_id_); disconnectionCount = 0; debugLogA("***** Status messages updated"); } - catch(const bad_response &) - { + catch (const bad_response &) { ++disconnectionCount; debugLogA("***** UpdateStatuses - Bad response from server, this has happened %d time(s)", disconnectionCount); if (disconnectionCount > 2) { @@ -633,44 +617,42 @@ void TwitterProto::UpdateStatuses(bool pre_read, bool popups, bool tweetToMsg) SetStatus(ID_STATUS_OFFLINE); } } - catch(const std::exception &e) - { - ShowPopup( (std::string("While updating status messages, an error occurred: ") - +e.what()).c_str()); + catch (const std::exception &e) { + ShowPopup((std::string("While updating status messages, an error occurred: ") + + e.what()).c_str()); debugLogA("***** Error updating status messages: %s", e.what()); } } void TwitterProto::UpdateMessages(bool pre_read) { - try - { - ScopedLock s(twitter_lock_); - twitter::status_list messages = twit_.get_direct(dm_since_id_); - s.Unlock(); + try { + twitter::status_list messages; + { + mir_cslock s(twitter_lock_); + messages = twit_.get_direct(dm_since_id_); + } - if(messages.size()) + if (messages.size()) dm_since_id_ = std::max(dm_since_id_, messages[0].status.id); - for(twitter::status_list::reverse_iterator i=messages.rbegin(); i!=messages.rend(); ++i) - { - MCONTACT hContact = AddToClientList(i->username.c_str(),""); + for (twitter::status_list::reverse_iterator i = messages.rbegin(); i != messages.rend(); ++i) { + MCONTACT hContact = AddToClientList(i->username.c_str(), ""); PROTORECVEVENT recv = { 0 }; recv.flags = PREF_UTF; - if(pre_read) + if (pre_read) recv.flags |= PREF_CREATEREAD; recv.szMessage = const_cast(i->status.text.c_str()); recv.timestamp = static_cast(i->status.time); ProtoChainRecvMsg(hContact, &recv); } - db_pod_set(0,m_szModuleName,TWITTER_KEY_DMSINCEID,dm_since_id_); + db_pod_set(0, m_szModuleName, TWITTER_KEY_DMSINCEID, dm_since_id_); disconnectionCount = 0; debugLogA("***** Direct messages updated"); } - catch(const bad_response &) - { + catch (const bad_response &) { ++disconnectionCount; debugLogA("***** UpdateMessages - Bad response from server, this has happened %d time(s)", disconnectionCount); if (disconnectionCount > 2) { @@ -678,17 +660,17 @@ void TwitterProto::UpdateMessages(bool pre_read) SetStatus(ID_STATUS_OFFLINE); } } - catch(const std::exception &e) - { - ShowPopup( (std::string("While updating direct messages, an error occurred: ")+e.what()).c_str()); + catch (const std::exception &e) { + ShowPopup((std::string("While updating direct messages, an error occurred: ") + e.what()).c_str()); debugLogA("***** Error updating direct messages: %s", e.what()); } } -void TwitterProto::resetOAuthKeys() { - db_unset(0,m_szModuleName,TWITTER_KEY_OAUTH_ACCESS_TOK); - db_unset(0,m_szModuleName,TWITTER_KEY_OAUTH_ACCESS_TOK_SECRET); - db_unset(0,m_szModuleName,TWITTER_KEY_OAUTH_TOK); - db_unset(0,m_szModuleName,TWITTER_KEY_OAUTH_TOK_SECRET); - db_unset(0,m_szModuleName,TWITTER_KEY_OAUTH_PIN); +void TwitterProto::resetOAuthKeys() +{ + db_unset(0, m_szModuleName, TWITTER_KEY_OAUTH_ACCESS_TOK); + db_unset(0, m_szModuleName, TWITTER_KEY_OAUTH_ACCESS_TOK_SECRET); + db_unset(0, m_szModuleName, TWITTER_KEY_OAUTH_TOK); + db_unset(0, m_szModuleName, TWITTER_KEY_OAUTH_TOK_SECRET); + db_unset(0, m_szModuleName, TWITTER_KEY_OAUTH_PIN); } -- cgit v1.2.3