From 661376ac4624e302525a3849c243652a60c5577c Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 24 Jul 2018 23:18:04 +0300 Subject: code cleaning --- protocols/Twitter/src/connection.cpp | 56 ++++-------------- protocols/Twitter/src/oauth.cpp | 110 +++++++++-------------------------- protocols/Twitter/src/oauth.dev.h | 4 -- protocols/Twitter/src/proto.cpp | 2 +- 4 files changed, 38 insertions(+), 134 deletions(-) delete mode 100644 protocols/Twitter/src/oauth.dev.h (limited to 'protocols/Twitter/src') diff --git a/protocols/Twitter/src/connection.cpp b/protocols/Twitter/src/connection.cpp index af7f0774ab..6e389007f5 100644 --- a/protocols/Twitter/src/connection.cpp +++ b/protocols/Twitter/src/connection.cpp @@ -94,21 +94,18 @@ bool TwitterProto::NegotiateConnection() if (!dbTOK) { oauthToken = dbv.pwszVal; db_free(&dbv); - //debugLogW("**NegotiateConnection - we have an oauthToken already in the db - %s", oauthToken); } INT_PTR dbTOKSec = getWString(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 = getString(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 = getString(TWITTER_KEY_UN, &dbv); @@ -116,7 +113,6 @@ bool TwitterProto::NegotiateConnection() screenName = dbv.pszVal; setString(TWITTER_KEY_NICK, dbv.pszVal); db_free(&dbv); - //debugLogW("**NegotiateConnection - we have a username already in the db - %s", screenName); } } @@ -133,28 +129,16 @@ bool TwitterProto::NegotiateConnection() resetOAuthKeys(); debugLogA("**NegotiateConnection - Reset OAuth Keys"); - //twit_.set_credentials(ConsumerKey, ConsumerSecret, oauthAccessToken, oauthAccessTokenSecret, L"", false); - // i think i was doin the wrong thing here.. i was setting the credentials as oauthAccessToken instead of oauthToken - // have to test.. debugLogA("**NegotiateConnection - Setting Consumer Keys..."); - /*debugLogW("**NegotiateConnection - sending set_cred: consumerKey is %s", ConsumerKey); - debugLogW("**NegotiateConnection - sending set_cred: consumerSecret is %s", ConsumerSecret); - debugLogW("**NegotiateConnection - sending set_cred: oauthToken is %s", oauthToken); - debugLogW("**NegotiateConnection - sending set_cred: oauthTokenSecret is %s", oauthTokenSecret); - debugLogA("**NegotiateConnection - sending set_cred: no pin");*/ twit_.set_credentials("", ConsumerKey, ConsumerSecret, oauthToken, oauthTokenSecret, L"", false); debugLogA("**NegotiateConnection - Requesting oauthTokens"); http::response resp = twit_.request_token(); - //wstring rdata_WSTR(resp.data.length(),L' '); wstring rdata_WSTR = UTF8ToWide(resp.data); - //debugLogW("**NegotiateConnection - REQUEST TOKEN IS %s", rdata_WSTR); OAuthParameters response = twit_.ParseQueryString(rdata_WSTR); oauthToken = response[L"oauth_token"]; oauthTokenSecret = response[L"oauth_token_secret"]; - //debugLogW("**NegotiateConnection - oauthToken is %s", oauthToken); - //debugLogW("**NegotiateConnection - oauthTokenSecret is %s", oauthTokenSecret); if (oauthToken.length() < 1) { ShowPopup("OAuth Tokens not received, check your internet connection?", 1); @@ -162,7 +146,7 @@ bool TwitterProto::NegotiateConnection() return false; } - //write those bitches to the db foe latta + // write those bitches to the db foe latta setWString(TWITTER_KEY_OAUTH_TOK, oauthToken.c_str()); setWString(TWITTER_KEY_OAUTH_TOK_SECRET, oauthTokenSecret.c_str()); @@ -190,22 +174,20 @@ bool TwitterProto::NegotiateConnection() 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) { + 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."); + else + debugLogA("**NegotiateConnection - oauthAccesToken too small? this is.. weird."); } dbTOKSec = getWString(TWITTER_KEY_OAUTH_ACCESS_TOK_SECRET, &dbv); if (!dbTOKSec) { oauthAccessTokenSecret = dbv.pwszVal; db_free(&dbv); - if (oauthAccessTokenSecret.size() > 1) { + 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"); } + else + debugLogA("**NegotiateConnection - oauthAccessTokenSecret too small? weird"); } if (!realAccessTok || !realAccessTokSecret) { // if we don't have one of these beasties then lets go get 'em! @@ -213,7 +195,6 @@ bool TwitterProto::NegotiateConnection() debugLogA("**NegotiateConnection - either the accessToken or accessTokenSecret was not there.."); if (!getWString(TWITTER_KEY_OAUTH_PIN, &dbv)) { pin = dbv.pwszVal; - //debugLogW("**NegotiateConnection - we have an pin already in the db - %s", pin); db_free(&dbv); } else { @@ -227,16 +208,10 @@ bool TwitterProto::NegotiateConnection() old_status = m_iStatus; m_iDesiredStatus = m_iStatus = ID_STATUS_OFFLINE; ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus); - return false; } debugLogA("**NegotiateConnection - Setting Consumer Keys and PIN..."); - /*debugLogW("**NegotiateConnection - sending set_cred: consumerKey is %s", ConsumerKey); - debugLogW("**NegotiateConnection - sending set_cred: consumerSecret is %s", ConsumerSecret); - debugLogW("**NegotiateConnection - sending set_cred: oauthToken is %s", oauthToken); - debugLogW("**NegotiateConnection - sending set_cred: oauthTokenSecret is %s", oauthTokenSecret); - debugLogW("**NegotiateConnection - sending set_cred: pin is %s", pin);*/ twit_.set_credentials("", ConsumerKey, ConsumerSecret, oauthToken, oauthTokenSecret, pin, false); @@ -261,16 +236,10 @@ bool TwitterProto::NegotiateConnection() debugLogA("**NegotiateConnection - Successfully retrieved Access Tokens"); 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"]; - //debugLogW("**NegotiateConnection - oauthAccessToken is %s", oauthAccessToken); - oauthAccessTokenSecret = accessTokenParameters[L"oauth_token_secret"]; - //debugLogW("**NegotiateConnection - oauthAccessTokenSecret is %s", oauthAccessTokenSecret); - screenName = WideToUTF8(accessTokenParameters[L"screen_name"]); debugLogA("**NegotiateConnection - screen name is %s", screenName.c_str()); @@ -302,10 +271,8 @@ bool TwitterProto::NegotiateConnection() } 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); // Set to offline @@ -315,15 +282,12 @@ bool TwitterProto::NegotiateConnection() return false; } - else { - m_iStatus = m_iDesiredStatus; - ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus); - return true; - } + m_iStatus = m_iDesiredStatus; + ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus); + return true; } - void TwitterProto::MessageLoop(void*) { debugLogA("***** Entering Twitter::MessageLoop"); diff --git a/protocols/Twitter/src/oauth.cpp b/protocols/Twitter/src/oauth.cpp index 97af28c8f7..1105b6e650 100644 --- a/protocols/Twitter/src/oauth.cpp +++ b/protocols/Twitter/src/oauth.cpp @@ -42,11 +42,11 @@ OAuthParameters mir_twitter::BuildSignedOAuthParameters( oauthParameters[L"oauth_version"] = L"1.0"; oauthParameters[L"oauth_signature_method"] = L"HMAC-SHA1"; oauthParameters[L"oauth_consumer_key"] = consumerKey; + oauthParameters[L"oauth_callback"] = L"oob"; // add the request token if found - if (!requestToken.empty()) { - oauthParameters[L"oauth_token"] = requestToken; /*debugLogW("requestToken not empty: %s", requestToken);*/ - } + if (!requestToken.empty()) + oauthParameters[L"oauth_token"] = requestToken; // add the authorization pin if found if (!pin.empty()) { @@ -57,25 +57,19 @@ OAuthParameters mir_twitter::BuildSignedOAuthParameters( // this will be used to create the parameter signature OAuthParameters allParameters = requestParameters; - if (Compare(httpMethod, L"POST", false) && postData) { - //debugLogA("in post section of buildOAuthParams"); + if (Compare(httpMethod, L"POST", false) && postData) allParameters.insert(postData->begin(), postData->end()); - } allParameters.insert(oauthParameters.begin(), oauthParameters.end()); // prepare a signature base, a carefully formatted string containing // all of the necessary information needed to generate a valid signature wstring normalUrl = OAuthNormalizeUrl(url); - //debugLogW("normalURL is %s", normalUrl); wstring normalizedParameters = OAuthNormalizeRequestParameters(allParameters); - //debugLogW("normalisedparams is %s", normalizedParameters); wstring signatureBase = OAuthConcatenateRequestElements(httpMethod, normalUrl, normalizedParameters); - //debugLogW("sigBase is %s", signatureBase); // obtain a signature and add it to header requestParameters wstring signature = OAuthCreateSignature(signatureBase, consumerSecret, requestTokenSecret); - //debugLogW("**BuildSignedOAuthParameters - sig is %s", signature); oauthParameters[L"oauth_signature"] = signature; return oauthParameters; @@ -83,34 +77,17 @@ OAuthParameters mir_twitter::BuildSignedOAuthParameters( wstring mir_twitter::UrlGetQuery(const wstring& url) { - wstring query; - /* - URL_COMPONENTS components = {sizeof(URL_COMPONENTS)}; - - wchar_t buf[1024*4] = {}; - - components.lpszExtraInfo = buf; - components.dwExtraInfoLength = _countof(buf); - - BOOL crackUrlOk = InternetCrackUrl(url.c_str(), url.size(), 0, &components); - _ASSERTE(crackUrlOk); - if(crackUrlOk) - {*/ - map brokenURL = CrackURL(url); - query = brokenURL[L"extraInfo"]; - //debugLogW("inside crack, url is %s", url); + wstring query = brokenURL[L"extraInfo"]; wstring::size_type q = query.find_first_of(L'?'); - if (q != wstring::npos) { + if (q != wstring::npos) query = query.substr(q + 1); - } wstring::size_type h = query.find_first_of(L'#'); - if (h != wstring::npos) { + if (h != wstring::npos) query = query.substr(0, h); - } - //} + return query; } @@ -127,12 +104,10 @@ wstring mir_twitter::OAuthWebRequestSubmit( const wstring& consumerSecret, const wstring& oauthToken, const wstring& oauthTokenSecret, - const wstring& pin - ) + const wstring& pin) { - //debugLogW("URL is %s", url); wstring query = UrlGetQuery(url); - //debugLogW("query is %s", query); + OAuthParameters originalParameters = ParseQueryString(query); OAuthParameters oauthSignedParameters = BuildSignedOAuthParameters( @@ -147,29 +122,17 @@ wstring mir_twitter::OAuthWebRequestSubmit( wstring mir_twitter::OAuthWebRequestSubmit(const OAuthParameters ¶meters, const wstring&) { - //debugLogW("OAuthWebRequestSubmit(%s)", url); - - //wstring oauthHeader = L"Authorization: OAuth "; wstring oauthHeader = L"OAuth "; - for (OAuthParameters::const_iterator it = parameters.begin(); - it != parameters.end(); - ++it) { - //debugLogW("%s = ", it->first); - //debugLogW("%s", it->second); - //debugLogA("---------"); - - if (it != parameters.begin()) { + for (auto &it : parameters) { + if (it != *parameters.begin()) oauthHeader += L","; - } wstring pair; - pair += it->first + L"=\"" + it->second + L"\""; + pair += it.first + L"=\"" + it.second + L"\""; oauthHeader += pair; } - //debugLogW("oauthheader is %s", oauthHeader); - return oauthHeader; } @@ -177,20 +140,13 @@ wstring mir_twitter::OAuthWebRequestSubmit(const OAuthParameters ¶meters, co std::wstring mir_twitter::BuildQueryString(const OAuthParameters ¶meters) { wstring query; - //debugLogA("do we ever get here?"); - for (OAuthParameters::const_iterator it = parameters.begin(); - it != parameters.end(); - ++it) { - //debugLogA("aww como ONNNNNN"); - //debugLogA("%s = %s", it->first.c_str(), it->second.c_str()); - //debugLogW("in buildqueryString bit, first is %s", it->first); - if (it != parameters.begin()) { + for (auto &it : parameters) { + if (it != *parameters.begin()) query += L"&"; - } wstring pair; - pair += it->first + L"=" + it->second + L""; + pair += it.first + L"=" + it.second + L""; query += pair; } return query; @@ -199,34 +155,28 @@ std::wstring mir_twitter::BuildQueryString(const OAuthParameters ¶meters) wstring mir_twitter::OAuthConcatenateRequestElements(const wstring& httpMethod, wstring url, const wstring& parameters) { wstring escapedUrl = UrlEncode(url); - //debugLogW("before OAUTHConcat, params are %s", parameters); wstring escapedParameters = UrlEncode(parameters); - //debugLogA(")))))))))))))))))))))))))))))))))))))))))))))))"); - //debugLogW("after url encode, its %s", escapedParameters); wstring ret = httpMethod + L"&" + escapedUrl + L"&" + escapedParameters; return ret; } -/* CrackURL.. just basically pulls apart a url into a map of wstrings: - * scheme, domain, port, path, extraInfo, explicitPort - * explicitPort will be 0 or 1, 0 if there was no actual port in the url, - * and 1 if it was explicitely specified. - * eg "http://twitter.com/blah.htm" will give: - * http, twitter.com, 80, blah.htm, "", 0 - * "https://twitter.com:989/blah.htm?boom" will give: - * https, twitter.com, 989, blah.htm?boom, ?boom, 1 - */ +// CrackURL.. just basically pulls apart a url into a map of wstrings: +// scheme, domain, port, path, extraInfo, explicitPort +// explicitPort will be 0 or 1, 0 if there was no actual port in the url, +// and 1 if it was explicitely specified. +// eg "http://twitter.com/blah.htm" will give: +// http, twitter.com, 80, blah.htm, "", 0 +// "https://twitter.com:989/blah.htm?boom" will give: +// https, twitter.com, 989, blah.htm?boom, ?boom, 1 + map mir_twitter::CrackURL(wstring url) { - wstring scheme1, domain1, port1, path1, extraInfo, explicitPort; vector urlToks, urlToks2, extraInfoToks; Split(url, urlToks, L':', false); - //debugLogW("**CRACK - URL to split is %s", url); scheme1 = urlToks[0]; - //debugLogW("**CRACK - scheme is %s", scheme1); if (urlToks.size() == 2) { // if there is only 1 ":" in the url if (Compare(scheme1, L"http", false)) { @@ -236,19 +186,16 @@ map mir_twitter::CrackURL(wstring url) port1 = L"443"; } - //debugLogW("**CRACK::2 - port is %s", port1); - Split(urlToks[1], urlToks2, L'/', false); domain1 = urlToks2[0]; - //debugLogW("**CRACK::2 - domain is %s", domain1); explicitPort = L"0"; } else if (urlToks.size() == 3) { // if there are 2 ":"s in the URL, ie a port is explicitly set domain1 = urlToks[1].substr(2, urlToks[1].size()); - //debugLogW("**CRACK::3 - domain is %s", domain1); + Split(urlToks[2], urlToks2, L'/', false); port1 = urlToks2[0]; - //debugLogW("**CRACK::3 - port is %s", port1); + explicitPort = L"1"; } else ppro_->debugLogW(L"**CRACK - not a proper URL? doesn't have a colon. URL is %s", url.c_str()); @@ -259,7 +206,6 @@ map mir_twitter::CrackURL(wstring url) } path1 += urlToks2[i]; } - //debugLogW("**CRACK - path is %s", path1); wstring::size_type foundHash = path1.find(L"#"); wstring::size_type foundQ = path1.find(L"?"); @@ -284,8 +230,6 @@ map mir_twitter::CrackURL(wstring url) extraInfo = L""; } - //debugLogW("**CRACK - extraInfo is %s", extraInfo); - map result; result[L"scheme"] = scheme1; result[L"domain"] = domain1; diff --git a/protocols/Twitter/src/oauth.dev.h b/protocols/Twitter/src/oauth.dev.h deleted file mode 100644 index 24deff074c..0000000000 --- a/protocols/Twitter/src/oauth.dev.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -#define OAUTH_CONSUMER_KEY L"AwSuQV9A7uXpat81MQB48g" -#define OAUTH_CONSUMER_SECRET L"x8pPGCCV5wFs26euODb9gv4VQ4kiuxTp3ed2P8Of4" \ No newline at end of file diff --git a/protocols/Twitter/src/proto.cpp b/protocols/Twitter/src/proto.cpp index b09db7ed71..a6f38105c7 100644 --- a/protocols/Twitter/src/proto.cpp +++ b/protocols/Twitter/src/proto.cpp @@ -22,7 +22,7 @@ along with this program. If not, see . #include "utility.h" #include "theme.h" #include "ui.h" -#include "oauth.dev.h" +#include "..\..\..\..\miranda-private-keys\Twitter\oauth.dev.h" static volatile LONG g_msgid = 1; -- cgit v1.2.3