-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvk_captcha.pas
199 lines (173 loc) · 6.67 KB
/
vk_captcha.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
(*
VKontakte plugin for Miranda IM: the free IM client for Microsoft Windows
Copyright (c) 2010 Andrey Lukyanov, Artyom Zhurkin
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
{-----------------------------------------------------------------------------
vk_captcha.pas
[ Description ]
Module to work with VKontakte's captcha
[ Known Issues ]
None
Contributors: LA
-----------------------------------------------------------------------------}
unit vk_captcha;
interface
uses
m_api,
vk_global, // module with global variables and constant used
vk_http, // module to connect with the site
vk_common, // module with common functions
Windows,
Messages;
function DlgCaptcha(Dialog: HWnd; Msg: cardinal; wParam, lParam: DWord): boolean; stdcall;
function ProcessCaptcha(CaptchaId: string; CaptchaURL: string = ''): string;
implementation
uses
vk_core, // module with core functions
SysUtils;
var
hBmpCaptcha: THandle;
EditFunctionOriginal: Pointer;
CaptchaValue: string;
// =============================================================================
// Dialog function to display captcha
// -----------------------------------------------------------------------------
function DlgCaptcha(Dialog: HWnd; Msg: cardinal; wParam, lParam: DWord): boolean; stdcall;
// new edit function for input box
// allows only latin characters and digits, they are input regardless of user's
// current keyboard layout/lang
function EditFunctionNew(Wnd: HWnd; Msg, wParam, lParam: integer): integer; stdcall;
// deletes the requested message from the queue, but throw back
// any WM_QUIT msgs that PeekMessage may also return
procedure KillMessage(Wnd: HWnd; Msg: integer);
var
M: TMsg;
begin
M.Message := 0;
if PeekMessage(M, Wnd, Msg, Msg, pm_Remove) and (M.Message = WM_QUIT) then
PostQuitMessage(M.WParam);
end;
begin
if (Msg = WM_KEYDOWN) then
begin
// codes - http://msdn.microsoft.com/en-us/library/dd375731(VS.85).aspx
if not (LoWord(wParam) in [VK_LEFT, VK_RIGHT, VK_DELETE, VK_BACK, VK_HOME, VK_END, VK_TAB, VK_RETURN, VK_ESCAPE]) then
begin
KillMessage(Wnd, WM_CHAR);
//if SendMessage(Wnd, WM_GETTEXTLENGTH, 0, 0) < 5 then // max 5 symbols are allowed
case LoWord(wParam) of
Ord('A')..Ord('Z'):
PostMessage(Wnd, WM_CHAR, LoWord(wParam) + 32, 0); // post lowercase latin symbol
Ord('0')..Ord('9'):
PostMessage(Wnd, WM_CHAR, LoWord(wParam), 0);
end;
Result := 0;
Exit;
end;
end;
// call original edit function
Result := CallWindowProc(EditFunctionOriginal, Wnd, Msg, wParam, lParam);
end;
// taken from m_imgsrvc.inc
const
MS_IMG_LOAD = 'IMG/Load';
var
rc: TRect;
DC, BitmapDC: hDC;
memBmp: THandle;
begin
Result := False;
case Msg of
WM_INITDIALOG:
begin
// translate all dialog texts
TranslateDialogDefault(Dialog);
// assign window icon
SendMessage(Dialog, WM_SETICON, ICON_BIG, LoadIcon(hInstance, 'ICON_PROTO'));
// load picture, filename is passed in lParam during dialog creation
hBmpCaptcha := pluginLink^.CallService(MS_IMG_LOAD, Windows.wParam(lParam), 0);
// delete our captcha file
DeleteFile(string(lParam));
SendMessage(GetDlgItem(Dialog, VK_CAPTCHA_CODE), EM_SETLIMITTEXT, 7, 0);
// assign new procedure to work with VK_CAPTCHA_CODE edit control
EditFunctionOriginal := Pointer(SetWindowLong(GetDlgItem(Dialog, VK_CAPTCHA_CODE), GWL_WNDPROC, integer(@EditFunctionNew)));
Result := True;
end;
WM_CLOSE:
begin
EndDialog(Dialog, 0);
end;
WM_DRAWITEM:
begin
DC := GetDC(GetDlgItem(Dialog, VK_CAPTCHA_PIC));
// get size of our picture control
GetClientRect(GetDlgItem(Dialog, VK_CAPTCHA_PIC), rc);
// clear it
FillRect(DC, rc, GetSysColorBrush(COLOR_BTNFACE));
if hBmpCaptcha <> 0 then
begin
BitmapDC := CreateCompatibleDC(DC);
memBmp := SelectObject(BitmapDC, hBmpCaptcha);
BitBlt(DC,
(rc.Right - 130) div 2,
(rc.Bottom - 50) div 2,
130, // captcha picture width
50, // height
BitmapDC, 0, 0, SRCCOPY);
DeleteDC(BitmapDC);
DeleteObject(memBmp);
end;
FrameRect(DC, rc, GetSysColorBrush(COLOR_BTNSHADOW));
ReleaseDC(GetDlgItem(Dialog, VK_CAPTCHA_PIC), DC);
Result := True;
end;
WM_COMMAND:
begin
case wParam of
VK_CAPTCHA_OK:
begin
CaptchaValue := GetDlgString(dialog, VK_CAPTCHA_CODE);
EndDialog(Dialog, 0);
Result := True;
end;
end;
end;
end;
end;
function ProcessCaptcha(CaptchaId: string; CaptchaURL: string = ''): string;
var
TempDir, TempFile: string;
Buf: array[0..1023] of char;
begin
if CaptchaURL = '' then
CaptchaURL := vk_url + Format(vk_url_captcha, [CaptchaId]);
Netlib_Log(vk_hNetlibUser, PChar('(vk_CaptchaProcessing) ... captcha ID is ' + CaptchaId));
Netlib_Log(vk_hNetlibUser, PChar('(vk_CaptchaProcessing) ... captcha URL is ' + CaptchaURL));
if (CaptchaId <> '') and (CaptchaURL <> '') then
begin
SetString(TempDir, Buf, GetTempPath(Sizeof(Buf) - 1, Buf)); // getting path to Temp directory
TempFile := TempDir + 'vk_captcha.jpg';
if HTTP_NL_GetPicture(CaptchaURL, TempFile) then
begin // file downloaded successfully
Netlib_Log(vk_hNetlibUser, PChar('(vk_CaptchaProcessing) ... captcha downloaded successfully and saved to ' + TempDir));
// ask user to input the value
DialogBoxParamW(hInstance, MAKEINTRESOURCEW(WideString('VK_CAPTCHA')), 0, @DlgCaptcha, Windows.lParam(TempFile));
Result := CaptchaValue;
end
else
Result := 'captcha_download_failed';
end;
end;
begin
end.