From a61c282b82ac68a5d694de4de0645de52bf5585a Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 12 Oct 2013 18:33:31 +0000 Subject: VK: captcha form git-svn-id: http://svn.miranda-ng.org/main/trunk@6460 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/VKontakte/src/main.cpp | 2 + protocols/VKontakte/src/misc.cpp | 136 +++++++++++++++++++++++++++++++++++++ protocols/VKontakte/src/resource.h | 15 +++- protocols/VKontakte/src/stdafx.h | 1 + protocols/VKontakte/src/vk.h | 2 + protocols/VKontakte/src/vk_proto.h | 2 + 6 files changed, 155 insertions(+), 3 deletions(-) (limited to 'protocols/VKontakte/src') diff --git a/protocols/VKontakte/src/main.cpp b/protocols/VKontakte/src/main.cpp index 32e3246751..5264952384 100644 --- a/protocols/VKontakte/src/main.cpp +++ b/protocols/VKontakte/src/main.cpp @@ -78,6 +78,8 @@ extern "C" int __declspec(dllexport) Load() { mir_getLP(&pluginInfo); + InitIcons(); + // Register protocol module PROTOCOLDESCRIPTOR pd = { sizeof(pd) }; pd.szName = "VKontakte"; diff --git a/protocols/VKontakte/src/misc.cpp b/protocols/VKontakte/src/misc.cpp index edb7739e6e..b4f27823cb 100644 --- a/protocols/VKontakte/src/misc.cpp +++ b/protocols/VKontakte/src/misc.cpp @@ -83,6 +83,137 @@ bool CVkProto::CheckJsonResult(JSONNODE *pNode) return iErrorCode == 0; } +///////////////////////////////////////////////////////////////////////////////////////// + +static IconItem iconList[] = +{ + { LPGEN("Captcha form icon"), "key", IDI_KEYS } +}; + +void InitIcons() +{ + Icon_Register(hInst, LPGEN("Protocols")"/"LPGEN("VKontakte"), iconList, SIZEOF(iconList), "VKontakte"); +} + +///////////////////////////////////////////////////////////////////////////////////////// +// Captcha form + +struct CAPTCHA_FORM_PARAMS +{ + HBITMAP bmp; + int w,h; + char Result[100]; +}; + +static INT_PTR CALLBACK CaptchaFormDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) +{ + CAPTCHA_FORM_PARAMS *params = (CAPTCHA_FORM_PARAMS*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA); + switch (msg) { + case WM_INITDIALOG: { + TranslateDialogDefault(hwndDlg); + SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)Skin_GetIconByHandle(iconList[0].hIcolib, TRUE)); + SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)Skin_GetIconByHandle(iconList[0].hIcolib)); + params = (CAPTCHA_FORM_PARAMS*)lParam; + + SetDlgItemText(hwndDlg, IDC_INSTRUCTION, TranslateT("Enter the text you see")); + SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG)params); + + return TRUE; + } + case WM_CTLCOLORSTATIC: + switch(GetWindowLongPtr((HWND)lParam, GWL_ID)) { + case IDC_WHITERECT: + case IDC_INSTRUCTION: + case IDC_TITLE: + return (BOOL)GetStockObject(WHITE_BRUSH); + } + return NULL; + + case WM_PAINT: + if (params) { + PAINTSTRUCT ps; + HDC hdc, hdcMem; + RECT rc; + + GetClientRect(hwndDlg, &rc); + hdc = BeginPaint(hwndDlg, &ps); + hdcMem = CreateCompatibleDC(hdc); + HGDIOBJ hOld = SelectObject(hdcMem, params->bmp); + + int y = (rc.bottom + rc.top - params->h) / 2; + int x = (rc.right + rc.left - params->w) / 2; + BitBlt(hdc, x, y, params->w, params->h, hdcMem, 0,0, SRCCOPY); + SelectObject(hdcMem, hOld); + DeleteDC(hdcMem); + + EndPaint(hwndDlg, &ps); + } + break; + + case WM_COMMAND: + switch (LOWORD(wParam)) { + case IDCANCEL: + EndDialog(hwndDlg, 0); + return TRUE; + + case IDOK: + GetDlgItemTextA(hwndDlg, IDC_VALUE, params->Result, SIZEOF(params->Result)); + EndDialog(hwndDlg, 1); + return TRUE; + } + break; + + case WM_CLOSE: + EndDialog(hwndDlg, 0); + break; + + case WM_DESTROY: + Skin_ReleaseIcon((HICON)SendMessage(hwndDlg, WM_SETICON, ICON_BIG, 0)); + Skin_ReleaseIcon((HICON)SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, 0)); + break; + } + return FALSE; +} + +CMStringA CVkProto::RunCaptchaForm(LPCSTR szUrl) +{ + debugLogA("CVkProto::RunCaptchaForm: reading picture from %s", szUrl); + + NETLIBHTTPREQUEST req = { sizeof(req) }; + req.requestType = REQUEST_GET; + req.szUrl = (LPSTR)szUrl; + req.flags = NLHRF_HTTP11; + req.timeout = 60; + + NETLIBHTTPREQUEST *reply = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)m_hNetlibUser, (LPARAM)&req); + if (reply == NULL) + return ""; + + if (reply->resultCode != 200) { + debugLogA("CVkProto::RunCaptchaForm: failed with code %d", reply->resultCode); + return ""; + } + + CAPTCHA_FORM_PARAMS param = { 0 }; + + IMGSRVC_MEMIO memio = { 0 }; + memio.iLen = reply->dataLength; + memio.pBuf = reply->pData; + memio.fif = FIF_UNKNOWN; /* detect */ + param.bmp = (HBITMAP)CallService(MS_IMG_LOADFROMMEM, (WPARAM)&memio, 0); + + BITMAP bmp = {0}; + GetObject(param.bmp, sizeof(bmp), &bmp); + param.w = bmp.bmWidth; + param.h = bmp.bmHeight; + int res = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_CAPTCHAFORM), NULL, CaptchaFormDlgProc, (LPARAM)¶m); + if (res == 0) + return ""; + + debugLogA("CVkProto::RunCaptchaForm: user entered text %s", param.Result); + return param.Result; +} + ///////////////////////////////////////////////////////////////////////////////////////// // Quick & dirty form parser @@ -138,6 +269,11 @@ CMStringA CVkProto::AutoFillForm(char *pBody, CMStringA &szAction) value = ptrA( mir_utf8encodeT( ptrT( getTStringA("Login")))); else if (name == "pass") value = ptrA( mir_utf8encodeT( ptrT( GetUserStoredPassword()))); + else if (name == "captcha_key") { + char *pCaptchaBeg = strstr(pFormBeg, ". #include #include #include +#include #include #include #include diff --git a/protocols/VKontakte/src/vk.h b/protocols/VKontakte/src/vk.h index 8b979cd8cf..e9f0779ca8 100644 --- a/protocols/VKontakte/src/vk.h +++ b/protocols/VKontakte/src/vk.h @@ -28,3 +28,5 @@ struct HttpParam extern HINSTANCE hInst; LPCSTR findHeader(NETLIBHTTPREQUEST *hdr, LPCSTR szField); + +void InitIcons(void); diff --git a/protocols/VKontakte/src/vk_proto.h b/protocols/VKontakte/src/vk_proto.h index c8275ae6f1..a0a7cbb708 100644 --- a/protocols/VKontakte/src/vk_proto.h +++ b/protocols/VKontakte/src/vk_proto.h @@ -139,7 +139,9 @@ private: int SetupConnection(void); void __cdecl WorkerThread(void*); + CMStringA RunCaptchaForm(LPCSTR szUrl); CMStringA AutoFillForm(char*, CMStringA&); + void ConnectionFailed(int iReason); bool CheckJsonResult(JSONNODE*); void OnLoggedIn(); -- cgit v1.2.3