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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/*
Copyright (c) 2013-14 Miranda NG project (http://miranda-ng.org)
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 version 2
of the License.
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, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
TCHAR* CVkProto::GetUserStoredPassword()
{
ptrA szRawPass( getStringA("Password"));
return (szRawPass != NULL) ? mir_utf8decodeT(szRawPass) : NULL;
}
void CVkProto::SetAllContactStatuses(int iStatus)
{
for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) {
if (isChatRoom(hContact))
SetChatStatus(hContact, iStatus);
else if (getWord(hContact, "Status", 0) != iStatus)
setWord(hContact, "Status", iStatus);
}
}
MCONTACT CVkProto::FindUser(LONG dwUserid, bool bCreate)
{
if (!dwUserid)
return NULL;
for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) {
LONG dbUserid = getDword(hContact, "ID", -1);
if (dbUserid == -1)
continue;
if (dbUserid == dwUserid)
return hContact;
}
if (!bCreate)
return NULL;
MCONTACT hNewContact = (MCONTACT)CallService(MS_DB_CONTACT_ADD, 0, 0);
CallService(MS_PROTO_ADDTOCONTACT, (WPARAM)hNewContact, (LPARAM)m_szModuleName);
setDword(hNewContact, "ID", dwUserid);
db_set_ts(hNewContact, "CList", "Group", m_defaultGroup);
return hNewContact;
}
bool CVkProto::CheckMid(int guid)
{
for (int i=m_sendIds.getCount()-1; i >= 0; i--)
if ((int)m_sendIds[i] == guid) {
m_sendIds.remove(i);
return true;
}
return false;
}
LPCSTR findHeader(NETLIBHTTPREQUEST *pReq, LPCSTR szField)
{
for (int i=0; i < pReq->headersCount; i++)
if (!_stricmp(pReq->headers[i].szName, szField))
return pReq->headers[i].szValue;
return NULL;
}
JSONNODE* CVkProto::CheckJsonResponse(AsyncHttpRequest *pReq, NETLIBHTTPREQUEST *reply, JSONROOT &pRoot)
{
pRoot.Parse(reply->pData);
if (pRoot == NULL)
return NULL;
if (!CheckJsonResult(pReq, reply, pRoot))
return NULL;
return json_get(pRoot, "response");
}
bool CVkProto::CheckJsonResult(AsyncHttpRequest *pReq, NETLIBHTTPREQUEST *reply, JSONNODE *pNode)
{
if (pNode == NULL)
return false;
JSONNODE *pError = json_get(pNode, "error"), *pErrorCode = json_get(pError, "error_code");
if (pError == NULL || pErrorCode == NULL)
return true;
int iErrorCode = json_as_int(pErrorCode);
switch (iErrorCode){
case VKERR_AUTHORIZATION_FAILED:
ConnectionFailed(LOGINERR_WRONGPASSWORD);
break;
case VKERR_CAPTCHA_NEEDED:
ApplyCaptcha(pReq, pError);
break;
case VKERR_UNKNOWN:
case VKERR_TOO_MANY_REQ_PER_SEC:
case VKERR_FLOOD_CONTROL:
case VKERR_INTERNAL_SERVER_ERR:
pReq->bNeedsRestart = true;
Sleep(500); //Pause for fix err
break;
case VKERR_HIMSELF_AS_FRIEND:
case VKERR_YOU_ON_BLACKLIST:
case VKERR_USER_ON_BLACKLIST:
CVkSendMsgParam *param = (CVkSendMsgParam*)pReq->pUserInfo;
if (param)
param->iCount = iErrorCode;
break;
}
return iErrorCode == 0;
}
void CVkProto::OnReceiveSmth(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq)
{
JSONROOT pRoot;
JSONNODE *pResponse = CheckJsonResponse(pReq, reply, pRoot);
debugLogA("CVkProto::OnReceiveSmth %s", json_as_string(pResponse));
}
/////////////////////////////////////////////////////////////////////////////////////////
static IconItem iconList[] =
{
{ LPGEN("Captcha form icon"), "key", IDI_KEYS }
};
void InitIcons()
{
Icon_Register(hInst, LPGEN("Protocols")"/"LPGEN("VKontakte"), iconList, SIZEOF(iconList), "VKontakte");
}
HANDLE GetIconHandle(int iCommand)
{
for (int i=0; i < SIZEOF(iconList); i++)
if (iconList[i].defIconID == iCommand)
return iconList[i].hIcolib;
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Quick & dirty form parser
static CMStringA getAttr(char *szSrc, LPCSTR szAttrName)
{
char *pEnd = strchr(szSrc, '>');
if (pEnd == NULL)
return "";
*pEnd = 0;
char *p1 = strstr(szSrc, szAttrName);
if (p1 == NULL) {
LBL_NotFound:
*pEnd = '>';
return "";
}
p1 += strlen(szAttrName);
if (p1[0] != '=' || p1[1] != '\"')
goto LBL_NotFound;
p1 += 2;
char *p2 = strchr(p1, '\"');
if (p2 == NULL)
goto LBL_NotFound;
*pEnd = '>';
return CMStringA(p1, (int)(p2-p1));
}
bool CVkProto::AutoFillForm(char *pBody, CMStringA &szAction, CMStringA& szResult)
{
szResult.Empty();
char *pFormBeg = strstr(pBody, "<form ");
if (pFormBeg == NULL) return false;
char *pFormEnd = strstr(pFormBeg, "</form>");
if (pFormEnd == NULL) return false;
*pFormEnd = 0;
szAction = getAttr(pFormBeg, "action");
CMStringA result;
char *pFieldBeg = pFormBeg;
while (true) {
if ((pFieldBeg = strstr(pFieldBeg+1, "<input ")) == NULL)
break;
CMStringA type = getAttr(pFieldBeg, "type");
if (type != "submit") {
CMStringA name = getAttr(pFieldBeg, "name");
CMStringA value = getAttr(pFieldBeg, "value");
if (name == "email")
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, "<img id=\"captcha\"");
if (pCaptchaBeg != NULL)
if (!RunCaptchaForm( getAttr(pCaptchaBeg, "src"), value))
return false;
}
if (!result.IsEmpty())
result.AppendChar('&');
result += name + "=";
result += ptrA( mir_urlEncode(value));
}
}
szResult = result;
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////
AsyncHttpRequest::AsyncHttpRequest()
{
cbSize = sizeof(NETLIBHTTPREQUEST);
m_bApiReq = true;
AddHeader("Connection", "keep-alive");
AddHeader("Accept-Encoding", "booo");
pUserInfo = NULL;
}
AsyncHttpRequest::AsyncHttpRequest(CVkProto *ppro, int iRequestType, LPCSTR _url, bool bSecure, VK_REQUEST_HANDLER pFunc)
{
cbSize = sizeof(NETLIBHTTPREQUEST);
m_bApiReq = true;
AddHeader("Connection", "keep-alive");
AddHeader("Accept-Encoding", "booo");
flags = VK_NODUMPHEADERS | NLHRF_DUMPASTEXT | NLHRF_HTTP11 | NLHRF_REDIRECT;
if (bSecure)
flags |= NLHRF_SSL;
if (*_url == '/') { // relative url leads to a site
m_szUrl = ((bSecure) ? "https://" : "http://") + CMStringA("api.vk.com");
m_szUrl += _url;
bIsMainConn = true;
}
else m_szUrl = _url;
if (bSecure)
this << CHAR_PARAM("access_token", ppro->m_szAccessToken);
requestType = iRequestType;
m_pFunc = pFunc;
pUserInfo = NULL;
}
AsyncHttpRequest::~AsyncHttpRequest()
{
for (int i=0; i < headersCount; i++) {
mir_free(headers[i].szName);
mir_free(headers[i].szValue);
}
mir_free(headers);
mir_free(pData);
}
void AsyncHttpRequest::AddHeader(LPCSTR szName, LPCSTR szValue)
{
headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount+1));
headers[headersCount].szName = mir_strdup(szName);
headers[headersCount].szValue = mir_strdup(szValue);
headersCount++;
}
void AsyncHttpRequest::Redirect(NETLIBHTTPREQUEST *nhr)
{
for (int i=0; i < nhr->headersCount; i++) {
LPCSTR szValue = nhr->headers[i].szValue;
if (!_stricmp(nhr->headers[i].szName, "Location"))
m_szUrl = szValue;
}
}
void CVkProto::GrabCookies(NETLIBHTTPREQUEST *nhr)
{
for (int i=0; i < nhr->headersCount; i++) {
if ( _stricmp(nhr->headers[i].szName, "Set-cookie"))
continue;
CMStringA szValue = nhr->headers[i].szValue, szCookieName, szCookieVal, szDomain;
int iStart = 0;
while (true) {
bool bFirstToken = iStart == 0;
CMStringA szToken = szValue.Tokenize(";", iStart).Trim();
if (iStart == -1)
break;
if (bFirstToken) {
int iStart2 = 0;
szCookieName = szToken.Tokenize("=", iStart2);
szCookieVal = szToken.Tokenize("=", iStart2);
}
else if (!strncmp(szToken, "domain=", 7))
szDomain = szToken.Mid(7);
}
if (!szCookieName.IsEmpty() && !szDomain.IsEmpty()) {
int k;
for (k=0; k < m_cookies.getCount(); k++) {
if (m_cookies[k].m_name == szCookieName) {
m_cookies[k].m_value = szCookieVal;
break;
}
}
if (k == m_cookies.getCount())
m_cookies.insert( new Cookie(szCookieName, szCookieVal, szDomain));
}
}
}
void CVkProto::ApplyCookies(AsyncHttpRequest *pReq)
{
CMStringA szCookie;
for (int i=0; i < m_cookies.getCount(); i++) {
if (!strstr(pReq->m_szUrl, m_cookies[i].m_domain))
continue;
if (!szCookie.IsEmpty())
szCookie.Append("; ");
szCookie.Append(m_cookies[i].m_name);
szCookie.AppendChar('=');
szCookie.Append(m_cookies[i].m_value);
}
if (!szCookie.IsEmpty())
pReq->AddHeader("Cookie", szCookie);
}
void CVkProto::DBAddAuthRequest(const MCONTACT hContact)
{
//char* szJid = mir_utf8encodeT(jid);
CMString tszNick = db_get_sa(hContact, m_szModuleName, "Nick");
char* szNick = mir_utf8encodeT(tszNick.GetBuffer());
//blob is: uin(DWORD), hContact(DWORD), nick(ASCIIZ), first(ASCIIZ), last(ASCIIZ), email(ASCIIZ), reason(ASCIIZ)
//blob is: 0(DWORD), hContact(DWORD), nick(ASCIIZ), ""(ASCIIZ), ""(ASCIIZ), ""(ASCIIZ), ""(ASCIIZ)
DBEVENTINFO dbei = { sizeof(DBEVENTINFO) };
dbei.szModule = m_szModuleName;
dbei.timestamp = (DWORD)time(NULL);
dbei.flags = DBEF_UTF;
dbei.eventType = EVENTTYPE_AUTHREQUEST;
dbei.cbBlob = (DWORD)(sizeof(DWORD) * 2 + strlen(szNick) + 5);
PBYTE pCurBlob = dbei.pBlob = (PBYTE)mir_alloc(dbei.cbBlob);
*((PDWORD)pCurBlob) = 0;
pCurBlob += sizeof(DWORD); // uin(DWORD) = 0 (DWORD)
*((PDWORD)pCurBlob) = (DWORD)hContact;
pCurBlob += sizeof(DWORD); // hContact(DWORD)
strcpy((char*)pCurBlob, szNick);
pCurBlob += strlen(szNick) + 1;
*pCurBlob = '\0'; //firstName
pCurBlob++;
*pCurBlob = '\0'; //lastName
pCurBlob++;
*pCurBlob = '\0'; //email
pCurBlob++;
*pCurBlob = '\0'; //reason
db_event_add(NULL, &dbei);
debugLogA("CVkProto::DBAddAuthRequest '%s'", szNick);
mir_free(szNick);
}
|