From cbbbbae0ec63187368817c0c49741c4ec7a9fe41 Mon Sep 17 00:00:00 2001 From: Fishbone Date: Fri, 7 Jun 2013 16:36:25 +0000 Subject: WhatsApp: - removed duplicate resource file in "src"-directory - finished registration (we don't know, if it already works. Hopefully I didn't break code-request again) git-svn-id: http://svn.miranda-ng.org/main/trunk@4892 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/WhatsApp/src/common.h | 3 +- protocols/WhatsApp/src/constants.h | 3 + protocols/WhatsApp/src/db.h | 1 + protocols/WhatsApp/src/dialogs.cpp | 43 +++++++++++-- protocols/WhatsApp/src/proto.cpp | 120 ++++++++++++++++++++++++++++--------- protocols/WhatsApp/src/proto.h | 3 +- 6 files changed, 138 insertions(+), 35 deletions(-) (limited to 'protocols/WhatsApp/src') diff --git a/protocols/WhatsApp/src/common.h b/protocols/WhatsApp/src/common.h index 6075138f94..92631c1365 100644 --- a/protocols/WhatsApp/src/common.h +++ b/protocols/WhatsApp/src/common.h @@ -74,13 +74,14 @@ class WhatsAppProto; #include "constants.h" #include "utils.h" #include "db.h" -#include "resource.h" +#include "../res/resource.h" #include "dialogs.h" #include "theme.h" #include "definitions.h" #include "WASocketConnection.h" #include "proto.h" #include "entities.h" +#include "cJSON.h" #if defined _DEBUG #include diff --git a/protocols/WhatsApp/src/constants.h b/protocols/WhatsApp/src/constants.h index e14a02cf3f..7ec8e6390a 100644 --- a/protocols/WhatsApp/src/constants.h +++ b/protocols/WhatsApp/src/constants.h @@ -64,4 +64,7 @@ #define IS_CHAT 1 +#define REG_STATE_REQ_CODE 1 +#define REG_STATE_REG_CODE 2 + #endif \ No newline at end of file diff --git a/protocols/WhatsApp/src/db.h b/protocols/WhatsApp/src/db.h index d78d2e75d6..39508a98b9 100644 --- a/protocols/WhatsApp/src/db.h +++ b/protocols/WhatsApp/src/db.h @@ -31,6 +31,7 @@ #define WHATSAPP_KEY_AVATAR_ID "AvatarId" #define WHATSAPP_KEY_SYSTRAY_NOTIFY "UseSystrayNotify" #define WHATSAPP_KEY_DEF_GROUP "DefaultGroup" +#define WHATSAPP_KEY_REG_CODE "RegistrationCode" #define WHATSAPP_KEY_EVENT_CLIENT_ENABLE "EventClientEnable" #define WHATSAPP_KEY_EVENT_OTHER_ENABLE "EventOtherEnable" diff --git a/protocols/WhatsApp/src/dialogs.cpp b/protocols/WhatsApp/src/dialogs.cpp index 81a17351fb..ef0c2fac4c 100644 --- a/protocols/WhatsApp/src/dialogs.cpp +++ b/protocols/WhatsApp/src/dialogs.cpp @@ -52,13 +52,46 @@ INT_PTR CALLBACK WhatsAppAccountProc( HWND hwnd, UINT message, WPARAM wparam, LP return TRUE; case WM_COMMAND: - if (LOWORD(wparam) == IDC_BUTTON_REQUEST_CODE) + if (LOWORD(wparam) == IDC_BUTTON_REQUEST_CODE || LOWORD(wparam) == IDC_BUTTON_REGISTER) { - if (MessageBox(NULL, _T("An SMS with registration-code will be sent to your mobile phone.\nNotice that you are not able to use the real WhatsApp and this plugin simultaneousley!\nContinue?"), - PRODUCT_NAME, MB_YESNO) == IDYES) + proto = reinterpret_cast(GetWindowLongPtr(hwnd,GWLP_USERDATA)); + + char cc[5]; + GetDlgItemTextA(hwnd, IDC_CC, cc, sizeof(cc)); + char number[128]; + GetDlgItemTextA(hwnd, IDC_LOGIN, number, sizeof(number)); + + if (LOWORD(wparam) == IDC_BUTTON_REQUEST_CODE) + { + if (MessageBox(NULL, TranslateT("An SMS with registration-code will be sent to your mobile phone.\nNotice that you are not able to use the real WhatsApp and this plugin simultaneousley!\nContinue?"), + PRODUCT_NAME, MB_YESNO) == IDYES) + { + proto->Register(REG_STATE_REQ_CODE, string(cc), string(number), string()); + } + } + else if (LOWORD(wparam) == IDC_BUTTON_REGISTER) { - proto = reinterpret_cast(GetWindowLongPtr(hwnd,GWLP_USERDATA)); - proto->RequestCode(); + char code[7]; + if (SendDlgItemMessage(hwnd, IDC_REG_CODE_1, WM_GETTEXTLENGTH, 0, 0) == 3 && + SendDlgItemMessage(hwnd, IDC_REG_CODE_2, WM_GETTEXTLENGTH, 0, 0) == 3) + { + GetDlgItemTextA(hwnd, IDC_REG_CODE_1, code, 4); + GetDlgItemTextA(hwnd, IDC_REG_CODE_2, &(code[3]), 4); + } + else + { + MessageBox(NULL, TranslateT("Please correctly specify your registration code received by SMS"), + PRODUCT_NAME, MB_ICONEXCLAMATION); + return TRUE; + } + string pw = proto->Register(REG_STATE_REG_CODE, string(cc), string(number), string(code)); + if (!pw.empty()) + { + CallService(MS_DB_CRYPT_ENCODESTRING,sizeof(pw.c_str()),reinterpret_cast(pw.c_str())); + db_set_s(0, proto->ModuleName(), WHATSAPP_KEY_PASS, pw.c_str()); + MessageBox(NULL, TranslateT("Your password has been set automatically.\nIf you change your password manually you may lose it and need to request a new code!"), + PRODUCT_NAME, MB_ICONWARNING); + } } } diff --git a/protocols/WhatsApp/src/proto.cpp b/protocols/WhatsApp/src/proto.cpp index 8c9669d58c..acfe83f5a6 100644 --- a/protocols/WhatsApp/src/proto.cpp +++ b/protocols/WhatsApp/src/proto.cpp @@ -166,31 +166,19 @@ HANDLE WhatsAppProto::SearchBasic( const PROTOCHAR* id ) return email; } -void WhatsAppProto::RequestCode() +string WhatsAppProto::Register(int state, string cc, string number, string code) { - std::string cc, number, idx; + string idx; + string ret; DBVARIANT dbv; if ( WASocketConnection::hNetlibUser == NULL) { - NotifyEvent(m_tszUserName,TranslateT("Network-connection error."),NULL,WHATSAPP_EVENT_CLIENT); - return; + NotifyEvent(m_tszUserName, TranslateT("Network-connection error."), NULL, WHATSAPP_EVENT_CLIENT); + return ret; } - if ( !db_get_s(NULL,m_szModuleName,WHATSAPP_KEY_IDX,&dbv,DBVT_ASCIIZ)) - { - idx = dbv.pszVal; - db_free(&dbv); - } - if (idx.empty()) - { - std::stringstream tm; - tm << time(NULL); - BYTE idxBuf[16]; - utils::md5string(tm.str(), idxBuf); - idx = std::string((const char*) idxBuf, 16); - db_set_s(0, m_szModuleName,WHATSAPP_KEY_IDX, idx.c_str()); - } + /* if ( !db_get_s(NULL,m_szModuleName,WHATSAPP_KEY_CC,&dbv, DBVT_ASCIIZ)) { cc = dbv.pszVal; @@ -201,7 +189,7 @@ void WhatsAppProto::RequestCode() return; } } - if ( !db_get_s(NULL,m_szModuleName,WHATSAPP_KEY_LOGIN,&dbv, DBVT_ASCIIZ)) + if ( !db_get_s(NULL,m_szModuleName,WHATSAPP_KEY_LOGIN,&dbv, DBVT_ASCIIZ)) { number = dbv.pszVal; db_free(&dbv); @@ -211,8 +199,35 @@ void WhatsAppProto::RequestCode() return; } } + */ + if ( !db_get_s(NULL,m_szModuleName,WHATSAPP_KEY_IDX,&dbv,DBVT_ASCIIZ)) + { + idx = dbv.pszVal; + db_free(&dbv); + } + if (idx.empty()) + { + std::stringstream tm; + tm << time(NULL); + BYTE idxBuf[16]; + utils::md5string(tm.str(), idxBuf); + idx = std::string((const char*) idxBuf, 16); + db_set_s(0, m_szModuleName,WHATSAPP_KEY_IDX, idx.c_str()); + } - std::string token(Utilities::md5String(std::string(ACCOUNT_TOKEN_PREFIX1) + ACCOUNT_TOKEN_PREFIX2 + number)); + string url; + if (state == REG_STATE_REQ_CODE) + { + string token(Utilities::md5String(std::string(ACCOUNT_TOKEN_PREFIX1) + ACCOUNT_TOKEN_PREFIX2 + number)); + url = string(ACCOUNT_URL_CODEREQUESTV2); + url += "?lc=US&lg=en&mcc=000&mnc=000&method=sms&token="+ token; + } + else if (state == REG_STATE_REG_CODE) + { + url = string(ACCOUNT_URL_REGISTERREQUESTV2); + url += "?code="+ code; + } + url += "&cc="+ cc +"&in="+ number +"&id="+ idx; NETLIBHTTPHEADER agentHdr; agentHdr.szName = "User-Agent"; @@ -230,9 +245,6 @@ void WhatsAppProto::RequestCode() NETLIBHTTPREQUEST nlhr = {sizeof(NETLIBHTTPREQUEST)}; nlhr.requestType = REQUEST_POST; - string url = std::string(ACCOUNT_URL_CODEREQUESTV2); - url += "?cc="+ cc + "&in="+ number + - "&lc=US&lg=en&mcc=000&mnc=000&method=sms&id=" + idx + "&token="+ token; nlhr.szUrl = (char*) url.c_str(); nlhr.headers = &headers[0]; nlhr.headersCount = 3; @@ -241,13 +253,67 @@ void WhatsAppProto::RequestCode() NETLIBHTTPREQUEST* pnlhr = (NETLIBHTTPREQUEST*) CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM) WASocketConnection::hNetlibUser, (LPARAM)&nlhr); + LOG("Server response: %s", pnlhr->pData); MessageBoxA(NULL, pnlhr->pData, "Debug", MB_OK); - // #TODO -} + cJSON* resp = cJSON_Parse(pnlhr->pData); + cJSON* val; + string title = this->TranslateStr("Registration"); -void WhatsAppProto::RegisterCode(const std::string& code) -{ + // Invalid + if (resp == NULL) + { + this->NotifyEvent(title, this->TranslateStr("Registration failed. Invalid server response."), NULL, WHATSAPP_EVENT_CLIENT); + return ret; + } + + // Status = fail + val = cJSON_GetObjectItem(resp, "status"); + if (strcmp(val->valuestring, "fail") == 0) + { + cJSON* tmpVal = cJSON_GetObjectItem(resp, "reason"); + if (strcmp(tmpVal->valuestring, "stale") == 0) + { + this->NotifyEvent(title, this->TranslateStr("Registration failed due to stale code. Please request a new code"), NULL, WHATSAPP_EVENT_CLIENT); + } + else + { + this->NotifyEvent(title, this->TranslateStr("Registration failed."), NULL, WHATSAPP_EVENT_CLIENT); + } + + tmpVal = cJSON_GetObjectItem(resp, "retry_after"); + if (tmpVal != NULL) + { + this->NotifyEvent(title, this->TranslateStr("Please try again in %i seconds", tmpVal->valueint), NULL, WHATSAPP_EVENT_OTHER); + } + } + + // Request code + else if (state == REG_STATE_REQ_CODE) + { + if (strcmp(val->valuestring, "sent") == 0) + { + this->NotifyEvent(title, this->TranslateStr("Registration code has been sent to your phone."), NULL, WHATSAPP_EVENT_OTHER); + } + } + + // Register + else if (state == REG_STATE_REG_CODE) + { + val = cJSON_GetObjectItem(resp, "pw"); + if (val == NULL) + { + this->NotifyEvent(title, this->TranslateStr("Registration failed."), NULL, WHATSAPP_EVENT_CLIENT); + } + if (strcmp(val->valuestring, "ok") == 0) + { + ret = val->valuestring; + } + } + + cJSON_Delete(resp); + + return ret; } ////////////////////////////////////////////////////////////////////////////// diff --git a/protocols/WhatsApp/src/proto.h b/protocols/WhatsApp/src/proto.h index e7a548f9af..524b429dae 100644 --- a/protocols/WhatsApp/src/proto.h +++ b/protocols/WhatsApp/src/proto.h @@ -134,8 +134,7 @@ public: } // Registration - void RequestCode(); - void RegisterCode(const std::string& code); + string Register(int state, string cc, string number, string code); // Helpers std::tstring GetAvatarFolder(); -- cgit v1.2.3