From f63eb2bfc28dff8bbf9d56dd57b2bc6b53487f56 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 29 May 2017 23:45:19 +0300 Subject: Skype: - fixes #829; - massive code cleaning --- protocols/SkypeWeb/src/skype_login.cpp | 145 +++++++++++++-------------------- 1 file changed, 55 insertions(+), 90 deletions(-) (limited to 'protocols/SkypeWeb/src/skype_login.cpp') diff --git a/protocols/SkypeWeb/src/skype_login.cpp b/protocols/SkypeWeb/src/skype_login.cpp index 98ccf8b66c..b49ed92647 100644 --- a/protocols/SkypeWeb/src/skype_login.cpp +++ b/protocols/SkypeWeb/src/skype_login.cpp @@ -29,21 +29,15 @@ void CSkypeProto::Login() li.szSkypename = getStringA(SKYPE_SETTINGS_ID); pass_ptrA szPassword(getStringA(SKYPE_SETTINGS_PASSWORD)); - if (li.szSkypename == NULL || szPassword == NULL) - { + if (li.szSkypename.IsEmpty() || szPassword == NULL) { ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); return; - } + } + m_bHistorySynced = m_bThreadsTerminated = false; if ((tokenExpires - 1800) > time(NULL)) OnLoginSuccess(); - /*else - { - if (strchr(li.szSkypename, '@')) - SendRequest(new LoginMSRequest(), &CSkypeProto::OnMSLoginFirst); - else - SendRequest(new LoginOAuthRequest((char*)li.szSkypename, szPassword), &CSkypeProto::OnLoginOAuth); - }*/ + PushRequest(new OAuthRequest(), &CSkypeProto::OnOAuthStart); } @@ -51,54 +45,43 @@ void CSkypeProto::OnLoginOAuth(const NETLIBHTTPREQUEST *response) { if (!IsStatusConnecting(m_iStatus)) return; - if (response == NULL || response->pData == NULL) - { + if (response == NULL || response->pData == NULL) { ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; } JSONNode json = JSONNode::parse(response->pData); - if (!json) - { + if (!json) { ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; } - if (response->resultCode != 200) - { + if (response->resultCode != 200) { int error = 0; - if (json["status"]) - { + if (json["status"]) { const JSONNode &status = json["status"]; - if (status["code"]) - { - switch(status["code"].as_int()) - { + if (status["code"]) { + switch (status["code"].as_int()) { case 40002: - { - ShowNotification(L"Skype", TranslateT("Authentication failed. Invalid username."), NULL, 1); - error = LOGINERR_BADUSERID; - break; - } + ShowNotification(L"Skype", TranslateT("Authentication failed. Invalid username."), NULL, 1); + error = LOGINERR_BADUSERID; + break; + case 40120: - { - ShowNotification(L"Skype", TranslateT("Authentication failed. Bad username or password."), NULL, 1); - error = LOGINERR_WRONGPASSWORD; - break; - } + ShowNotification(L"Skype", TranslateT("Authentication failed. Bad username or password."), NULL, 1); + error = LOGINERR_WRONGPASSWORD; + break; + case 40121: - { - ShowNotification(L"Skype", TranslateT("Too many failed authentication attempts with given username or IP."), NULL, 1); - error = LOGIN_ERROR_TOOMANY_REQUESTS; - break; - } - default: - { - ShowNotification(L"Skype", status["text"] ? status["text"].as_mstring() : TranslateT("Authentication failed. Unknown error."), NULL, 1); - error = LOGIN_ERROR_UNKNOWN; - } + ShowNotification(L"Skype", TranslateT("Too many failed authentication attempts with given username or IP."), NULL, 1); + error = LOGIN_ERROR_TOOMANY_REQUESTS; + break; + + default: + ShowNotification(L"Skype", status["text"] ? status["text"].as_mstring() : TranslateT("Authentication failed. Unknown error."), NULL, 1); + error = LOGIN_ERROR_UNKNOWN; } } } @@ -108,8 +91,7 @@ void CSkypeProto::OnLoginOAuth(const NETLIBHTTPREQUEST *response) return; } - if (!json["skypetoken"] || !json["expiresIn"]) - { + if (!json["skypetoken"] || !json["expiresIn"]) { ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; @@ -120,9 +102,11 @@ void CSkypeProto::OnLoginOAuth(const NETLIBHTTPREQUEST *response) OnLoginSuccess(); } + void CSkypeProto::OnLoginSuccess() { - if (!IsStatusConnecting(m_iStatus)) return; + if (!IsStatusConnecting(m_iStatus)) + return; m_bThreadsTerminated = false; ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_SUCCESS, NULL, 0); @@ -140,22 +124,18 @@ void CSkypeProto::OnEndpointCreated(const NETLIBHTTPREQUEST *response) m_iStatus++; - if (response == NULL) - { + if (response == NULL) { debugLogA(__FUNCTION__ ": failed to get create endpoint"); ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; } - for (int i = 0; i < response->headersCount; i++) - { - if (!mir_strcmpi(response->headers[i].szName, "Set-RegistrationToken")) - { + for (int i = 0; i < response->headersCount; i++) { + if (!mir_strcmpi(response->headers[i].szName, "Set-RegistrationToken")) { CMStringA szValue = response->headers[i].szValue, szCookieName, szCookieVal; int iStart = 0; - while (true) - { + while (true) { CMStringA szToken = szValue.Tokenize(";", iStart).Trim(); if (iStart == -1) break; @@ -168,58 +148,48 @@ void CSkypeProto::OnEndpointCreated(const NETLIBHTTPREQUEST *response) li.endpoint.szToken = szCookieVal.Detach(); else if (szCookieName == "endpointId") li.endpoint.szId = szCookieVal.Detach(); - } } - else if (!mir_strcmpi(response->headers[i].szName, "Location")) - { + else if (!mir_strcmpi(response->headers[i].szName, "Location")) { CMStringA szValue = response->headers[i].szValue; li.endpoint.szServer = GetServerFromUrl(szValue).Detach(); setString("Server", li.endpoint.szServer); } - } - if (m_iStatus++ > SKYPE_MAX_CONNECT_RETRIES) - { + if (m_iStatus++ > SKYPE_MAX_CONNECT_RETRIES) { debugLogA(__FUNCTION__ ": failed to create endpoint (too many connect retries)"); ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; } - if (response->resultCode != 201) - { - if (response->resultCode == 401) - { + if (response->resultCode != 201) { + if (response->resultCode == 401) { delSetting("TokenExpiresIn"); - SendRequest(new LoginOAuthRequest((char*)li.szSkypename, pass_ptrA(getStringA(SKYPE_SETTINGS_PASSWORD))), &CSkypeProto::OnLoginOAuth); + SendRequest(new LoginOAuthRequest(li.szSkypename, pass_ptrA(getStringA(SKYPE_SETTINGS_PASSWORD))), &CSkypeProto::OnLoginOAuth); return; } - else if (response->resultCode == 400) - { + if (response->resultCode == 400) { delSetting("TokenExpiresIn"); ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; } - else //it should be rewritten - { - SendRequest(new CreateEndpointRequest(li), &CSkypeProto::OnEndpointCreated); - return; - } + // it should be rewritten + SendRequest(new CreateEndpointRequest(li), &CSkypeProto::OnEndpointCreated); + return; } - SendRequest(new CreateSubscriptionsRequest(li), &CSkypeProto::OnSubscriptionsCreated); } void CSkypeProto::OnSubscriptionsCreated(const NETLIBHTTPREQUEST *response) { - if (!IsStatusConnecting(m_iStatus)) return; + if (!IsStatusConnecting(m_iStatus)) + return; - if (response == NULL) - { + if (response == NULL) { debugLogA(__FUNCTION__ ": failed to create subscription"); ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); @@ -235,8 +205,7 @@ void CSkypeProto::SendPresence(bool isLogin) if (!m_opts.bUseHostnameAsPlace && m_opts.wstrPlace && *m_opts.wstrPlace) epname = mir_utf8encodeW(m_opts.wstrPlace); - else - { + else { wchar_t compName[MAX_COMPUTERNAME_LENGTH + 1]; DWORD size = _countof(compName); GetComputerName(compName, &size); @@ -245,16 +214,16 @@ void CSkypeProto::SendPresence(bool isLogin) if (isLogin) SendRequest(new SendCapabilitiesRequest(epname, li), &CSkypeProto::OnCapabilitiesSended); - else + else PushRequest(new SendCapabilitiesRequest(epname, li)); } void CSkypeProto::OnCapabilitiesSended(const NETLIBHTTPREQUEST *response) { - if (!IsStatusConnecting(m_iStatus)) return; + if (!IsStatusConnecting(m_iStatus)) + return; - if (response == NULL || response->pData == NULL) - { + if (response == NULL || response->pData == NULL) { ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); return; @@ -264,10 +233,9 @@ void CSkypeProto::OnCapabilitiesSended(const NETLIBHTTPREQUEST *response) LIST skypenames(1); for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) - { if (!isChatRoom(hContact)) skypenames.insert(getStringA(hContact, SKYPE_SETTINGS_ID)); - } + SendRequest(new CreateContactsSubscriptionRequest(skypenames, li)); FreeList(skypenames); skypenames.destroy(); @@ -278,7 +246,7 @@ void CSkypeProto::OnCapabilitiesSended(const NETLIBHTTPREQUEST *response) SendRequest(new CreateTrouterRequest(), &CSkypeProto::OnCreateTrouter); PushRequest(new GetContactListRequest(li, NULL), &CSkypeProto::LoadContactList); PushRequest(new GetAvatarRequest(ptrA(getStringA("AvatarUrl"))), &CSkypeProto::OnReceiveAvatar, NULL); - + if (m_opts.bAutoHistorySync) PushRequest(new SyncHistoryFirstRequest(100, li), &CSkypeProto::OnSyncHistory); @@ -291,8 +259,7 @@ void CSkypeProto::OnCapabilitiesSended(const NETLIBHTTPREQUEST *response) void CSkypeProto::OnStatusChanged(const NETLIBHTTPREQUEST *response) { - if (response == NULL || response->pData == NULL) - { + if (response == NULL || response->pData == NULL) { debugLogA(__FUNCTION__ ": failed to change status"); ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); @@ -300,8 +267,7 @@ void CSkypeProto::OnStatusChanged(const NETLIBHTTPREQUEST *response) } JSONNode json = JSONNode::parse(response->pData); - if (!json) - { + if (!json) { debugLogA(__FUNCTION__ ": failed to change status"); ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); @@ -315,8 +281,7 @@ void CSkypeProto::OnStatusChanged(const NETLIBHTTPREQUEST *response) } int iNewStatus = SkypeToMirandaStatus(nStatus.as_string().c_str()); - if (iNewStatus == ID_STATUS_OFFLINE) - { + if (iNewStatus == ID_STATUS_OFFLINE) { debugLogA(__FUNCTION__ ": failed to change status"); ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGIN_ERROR_UNKNOWN); SetStatus(ID_STATUS_OFFLINE); -- cgit v1.2.3