summaryrefslogtreecommitdiff
path: root/protocols/ICQ-WIM/src/proto.cpp
blob: aec09ffb55a95a6e233d790fbd0e1f7c222ccb45 (plain)
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
// ---------------------------------------------------------------------------80
//                ICQ plugin for Miranda Instant Messenger
//                ________________________________________
//
// Copyright © 2000-2001 Richard Hughes, Roland Rabien, Tristan Van de Vreede
// Copyright © 2001-2002 Jon Keating, Richard Hughes
// Copyright © 2002-2004 Martin Öberg, Sam Kothari, Robert Rainwater
// Copyright © 2004-2010 Joe Kucera, George Hazan
// Copyright © 2012-2023 Miranda NG team
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// -----------------------------------------------------------------------------
//  DESCRIPTION:
//
//  Protocol Interface Implementation
// -----------------------------------------------------------------------------

#include "stdafx.h"

#include "m_icolib.h"

#pragma warning(disable:4355)

static int CompareCache(const IcqUser *p1, const IcqUser *p2)
{
	return mir_wstrcmp(p1->m_aimid, p2->m_aimid);
}

CIcqProto::CIcqProto(const char *aProtoName, const wchar_t *aUserName) :
	PROTO<CIcqProto>(aProtoName, aUserName),
	m_impl(*this),
	m_arHttpQueue(10),
	m_arOwnIds(1, PtrKeySortT),
	m_arCache(20, &CompareCache),
	m_arGroups(10, NumericKeySortT),
	m_arLastSeenQueue(10, NumericKeySortT),
	m_arMarkReadQueue(10, NumericKeySortT),
	m_evRequestsQueue(CreateEvent(nullptr, FALSE, FALSE, nullptr)),
	m_szOwnId(this, DB_KEY_ID),
	m_iStatus1(this, "Status1", ID_STATUS_AWAY),
	m_iStatus2(this, "Status2", ID_STATUS_NA),
	m_iTimeDiff1(this, "TimeDiff1", 0),
	m_iTimeDiff2(this, "TimeDiff2", 0),
	m_bHideGroupchats(this, "HideChats", true),
	m_bUseTrayIcon(this, "UseTrayIcon", false),
	m_bErrorPopups(this, "ShowErrorPopups", true),
	m_bLaunchMailbox(this, "LaunchMailbox", true)
{
	db_set_resident(m_szModuleName, DB_KEY_IDLE);
	db_set_resident(m_szModuleName, DB_KEY_ONLINETS);

	m_isMra = !stricmp(Proto_GetAccount(m_szModuleName)->szProtoName, "MRA");

	g_plugin.addPopupOption(CMStringW(FORMAT, TranslateT("%s error notifications"), m_tszUserName), m_bErrorPopups);

	// services
	CreateProtoService(PS_GETAVATARCAPS, &CIcqProto::GetAvatarCaps);
	CreateProtoService(PS_GETAVATARINFO, &CIcqProto::GetAvatarInfo);
	CreateProtoService(PS_GETMYAVATAR, &CIcqProto::GetAvatar);
	CreateProtoService(PS_SETMYAVATAR, &CIcqProto::SetAvatar);

	CreateProtoService(PS_MENU_LOADHISTORY, &CIcqProto::OnMenuLoadHistory);
	CreateProtoService(PS_GETUNREADEMAILCOUNT, &CIcqProto::GetEmailCount);
	CreateProtoService(PS_GOTO_INBOX, &CIcqProto::GotoInbox);

	// cloud file transfer
	CreateProtoService(PS_OFFLINEFILE, &CIcqProto::SvcOfflineFile);

	// events
	HookProtoEvent(ME_CLIST_GROUPCHANGE, &CIcqProto::OnGroupChange);
	HookProtoEvent(ME_GC_EVENT, &CIcqProto::GroupchatEventHook);
	HookProtoEvent(ME_GC_BUILDMENU, &CIcqProto::GroupchatMenuHook);
	HookProtoEvent(ME_OPT_INITIALISE, &CIcqProto::OnOptionsInit);

	// group chats
	GCREGISTER gcr = {};
	gcr.dwFlags = GC_TYPNOTIF | GC_CHANMGR | GC_DATABASE;
	gcr.ptszDispName = m_tszUserName;
	gcr.pszModule = m_szModuleName;
	Chat_Register(&gcr);

	// avatars
	CreateDirectoryTreeW(GetAvatarPath());

	// netlib handle
	NETLIBUSER nlu = {};
	nlu.szSettingsModule = m_szModuleName;
	nlu.flags = NUF_OUTGOING | NUF_HTTPCONNS | NUF_UNICODE;
	nlu.szDescriptiveName.w = m_tszUserName;
	m_hNetlibUser = Netlib_RegisterUser(&nlu);

	// this was previously an old ICQ account
	ptrW wszUin(GetUIN(0));
	if (wszUin != nullptr) {
		delSetting("UIN");

		m_szOwnId = wszUin;

		for (auto &it : AccContacts())
			delSetting(it, "e-mail");
	}
	// this was previously an old MRA account
	else {
		CMStringW wszEmail(getMStringW("e-mail"));
		if (!wszEmail.IsEmpty()) {
			m_szOwnId = wszEmail;
			delSetting("e-mail");
		}
	}

	m_hWorkerThread = ForkThreadEx(&CIcqProto::ServerThread, nullptr, nullptr);
}

CIcqProto::~CIcqProto()
{
	::CloseHandle(m_evRequestsQueue);
}

////////////////////////////////////////////////////////////////////////////////////////
// OnModulesLoadedEx - performs hook registration

void CIcqProto::OnModulesLoaded()
{
	InitContactCache();

	HookProtoEvent(ME_USERINFO_INITIALISE, &CIcqProto::OnUserInfoInit);

	// load custom smilies
	SmileyAdd_LoadContactSmileys(SMADD_FOLDER, m_szModuleName, GetAvatarPath() + L"\\Stickers\\*.png");
}

void CIcqProto::OnShutdown()
{
	m_bTerminated = true;
}

void CIcqProto::OnContactAdded(MCONTACT hContact)
{
	CMStringW wszId(getMStringW(hContact, DB_KEY_ID));
	if (!wszId.IsEmpty() && !FindUser(wszId)) {
		mir_cslock l(m_csCache);
		m_arCache.insert(new IcqUser(wszId, hContact));
	}
}

void CIcqProto::OnContactDeleted(MCONTACT hContact)
{
	CMStringW szId(GetUserId(hContact));
	if (!isChatRoom(hContact)) {
		mir_cslock lck(m_csCache);
		m_arCache.remove(FindUser(szId));
	}

	Push(new AsyncHttpRequest(CONN_MAIN, REQUEST_GET, ICQ_API_SERVER "/buddylist/removeBuddy")
		<< AIMSID(this) << WCHAR_PARAM("buddy", szId) << INT_PARAM("allGroups", 1));
}

void CIcqProto::OnReceiveOfflineFile(DB::FILE_BLOB &blob, void *ft)
{
	if (auto *pFileInfo = (IcqFileInfo *)ft) {
		blob.setUrl(pFileInfo->szOrigUrl);
		blob.setSize(pFileInfo->dwFileSize);
	}
}

void CIcqProto::OnSendOfflineFile(DB::EventInfo &, DB::FILE_BLOB &blob, void *hTransfer)
{	
	auto *ft = (IcqFileTransfer *)hTransfer;

	auto *p = wcsrchr(ft->m_wszFileName, '\\');
	if (p == nullptr)
		p = ft->m_wszFileName;
	else
		p++;
	blob.setName(p);

	blob.setUrl("boo");
	blob.complete(ft->pfts.currentFileSize);
	blob.setLocalName(ft->m_wszFileName);
}

void CIcqProto::OnEventEdited(MCONTACT, MEVENT, const DBEVENTINFO &)
{

}

/////////////////////////////////////////////////////////////////////////////////////////

void CIcqProto::OnFileRecv(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq)
{
	if (pReply->resultCode != 200)
		return;

	auto *ofd = (OFDTHREAD *)pReq->pUserInfo;
	debugLogW(L"Saving to [%s]", ofd->wszPath.c_str());
	int fileId = _wopen(ofd->wszPath, _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY, _S_IREAD | _S_IWRITE);
	if (fileId == -1) {
		debugLogW(L"Cannot open [%s] for writing", ofd->wszPath.c_str());
		return;
	}

	int cbWritten = _write(fileId, pReply->pData, pReply->dataLength);
	_close(fileId);
	if (cbWritten != pReply->dataLength) {
		debugLogW(L"Error writing data into [%s]: %d instead of %d", ofd->wszPath.c_str(), cbWritten, pReply->dataLength);
		return;
	}

	DBVARIANT dbv = { DBVT_DWORD };
	dbv.dVal = cbWritten;
	db_event_setJson(ofd->hDbEvent, "ft", &dbv);

	ofd->Finish();
	delete ofd;
}

void __cdecl CIcqProto::OfflineFileThread(void *pParam)
{
	auto *ofd = (OFDTHREAD *)pParam;

	DB::EventInfo dbei(ofd->hDbEvent);
	if (m_bOnline && dbei && !strcmp(dbei.szModule, m_szModuleName) && dbei.eventType == EVENTTYPE_FILE) {
		DB::FILE_BLOB blob(dbei);
		MCONTACT hContact = db_event_getContact(ofd->hDbEvent);
		if (auto *pFileInfo = RetrieveFileInfo(hContact, fileText2url(blob.getUrl()))) {
			if (!ofd->bCopy) {
				auto *pReq = new AsyncHttpRequest(CONN_NONE, REQUEST_GET, pFileInfo->szUrl, &CIcqProto::OnFileRecv);
				pReq->pUserInfo = ofd;
				pReq->AddHeader("Sec-Fetch-User", "?1");
				pReq->AddHeader("Sec-Fetch-Site", "cross-site");
				pReq->AddHeader("Sec-Fetch-Mode", "navigate");
				Push(pReq);
				return; // ofd is used inside CIcqProto::OnFileRecv, don't remove it
			}

			ofd->wszPath.Empty();
			ofd->wszPath.Append(_A2T(pFileInfo->szUrl));
			ofd->pCallback->Invoke(*ofd);
		}
	}

	delete ofd;
}

INT_PTR __cdecl CIcqProto::SvcOfflineFile(WPARAM param, LPARAM)
{
	ForkThread((MyThreadFunc)&CIcqProto::OfflineFileThread, (void *)param);
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

INT_PTR CIcqProto::OnMenuLoadHistory(WPARAM hContact, LPARAM)
{
	delSetting(hContact, DB_KEY_LASTMSGID);

	RetrieveUserHistory(hContact, 1, true);
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

void CIcqProto::OnBuildProtoMenu()
{
	CMenuItem mi(&g_plugin);
	mi.root = Menu_GetProtocolRoot(this);
	mi.flags = CMIF_UNMOVABLE;

	// Groups uploader
	mi.pszService = "/UploadGroups";
	CreateProtoService(mi.pszService, &CIcqProto::UploadGroups);
	mi.name.a = LPGEN("Synchronize server groups");
	mi.position = 200001;
	mi.hIcolibItem = Skin_GetIconHandle(SKINICON_OTHER_GROUP);
	m_hUploadGroups = Menu_AddProtoMenuItem(&mi, m_szModuleName);

	Menu_ShowItem(m_hUploadGroups, false);

	// Groups editor
	mi.pszService = "/EditGroups";
	CreateProtoService(mi.pszService, &CIcqProto::EditGroups);
	mi.name.a = LPGEN("Edit server groups");
	mi.position = 200002;
	mi.hIcolibItem = Skin_GetIconHandle(SKINICON_OTHER_GROUP);
	Menu_AddProtoMenuItem(&mi, m_szModuleName);

	// Profile viewer
	if (!m_isMra) {
		mi.pszService = "/EditProfile";
		CreateProtoService(mi.pszService, &CIcqProto::EditProfile);
		mi.name.a = LPGEN("Edit my web profile");
		mi.position = 210001;
		mi.hIcolibItem = Skin_GetIconHandle(SKINICON_OTHER_MIRANDAWEB);
		Menu_AddProtoMenuItem(&mi, m_szModuleName);
	}
}

INT_PTR CIcqProto::EditProfile(WPARAM, LPARAM)
{
	if (mir_wstrlen(m_szOwnId))
		Utils_OpenUrlW(CMStringW(FORMAT, L"https://icq.com/people/%s/edit/", (wchar_t*)m_szOwnId));
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

INT_PTR CIcqProto::GetEmailCount(WPARAM, LPARAM)
{
	if (!m_bOnline)
		return 0;
	return m_unreadEmails;
}

INT_PTR CIcqProto::GotoInbox(WPARAM, LPARAM)
{
	Utils_OpenUrl("https://e.mail.ru/messages/inbox");
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

void CIcqProto::OnLastSeen(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *)
{
	RobustReply root(pReply);
	if (root.error() != 20000)
		return;

	const JSONNode &results = root.results();
	for (auto &it : results["entries"]) {
		if (auto *pUser = FindUser(it["sn"].as_mstring())) {
			int iLastSeen = it["lastseen"].as_int();
			if (iLastSeen != 0) {
				setDword(pUser->m_hContact, DB_KEY_LASTSEEN, iLastSeen);
				ProcessStatus(pUser, ID_STATUS_OFFLINE);
			}
			else ProcessStatus(pUser, ID_STATUS_ONLINE);
		}
	}
}

void CIcqProto::SendLastSeen()
{
	mir_cslock lck(m_csLastSeenQueue);

	auto *pReq = new AsyncRapiRequest(this, "getUserLastseen", &CIcqProto::OnLastSeen);
	JSONNode ids(JSON_ARRAY); ids.set_name("ids"); 
	for (auto &it: m_arLastSeenQueue)
		ids << WCHAR_PARAM("", GetUserId(it->m_hContact));
	pReq->params.push_back(ids);
	Push(pReq);

	m_arLastSeenQueue.destroy();
}

/////////////////////////////////////////////////////////////////////////////////////////

void CIcqProto::SendMarkRead()
{
	mir_cslock lck(m_csMarkReadQueue);
	while (m_arMarkReadQueue.getCount()) {
		auto *pUser = m_arMarkReadQueue[0];

		auto *pReq = new AsyncRapiRequest(this, "setDlgStateWim");
		pReq->params << WCHAR_PARAM("sn", GetUserId(pUser->m_hContact)) << INT64_PARAM("lastRead", getId(pUser->m_hContact, DB_KEY_LASTMSGID));
		Push(pReq);

		m_arMarkReadQueue.remove(0);
	}
}

void CIcqProto::OnMarkRead(MCONTACT hContact, MEVENT)
{
	if (!m_bOnline)
		return;

	m_impl.m_markRead.Start(200);

	auto *pUser = FindUser(GetUserId(hContact));
	if (pUser) {
		mir_cslock lck(m_csMarkReadQueue);
		if (m_arMarkReadQueue.indexOf(pUser) == -1)
			m_arMarkReadQueue.insert(pUser);
	}
}

int CIcqProto::OnGroupChange(WPARAM hContact, LPARAM lParam)
{
	if (!m_bOnline)
		return 0;

	CLISTGROUPCHANGE *pParam = (CLISTGROUPCHANGE*)lParam;
	if (hContact == 0) { // whole group is changed
		if (pParam->pszOldName == nullptr) {
			for (auto &it : m_arGroups)
				if (it->wszName == pParam->pszNewName)
					return 0;

			Push(new AsyncHttpRequest(CONN_MAIN, REQUEST_GET, ICQ_API_SERVER "/buddylist/addGroup")
				<< AIMSID(this) << GROUP_PARAM("group", pParam->pszNewName));
		}
		else if (pParam->pszNewName == nullptr) {
			Push(new AsyncHttpRequest(CONN_MAIN, REQUEST_GET, ICQ_API_SERVER "/buddylist/removeGroup") 
				<< AIMSID(this) << GROUP_PARAM("group", pParam->pszOldName));
		}
		else {
			Push(new AsyncHttpRequest(CONN_MAIN, REQUEST_GET, ICQ_API_SERVER "/buddylist/renameGroup") 
				<< AIMSID(this) << GROUP_PARAM("oldGroup", pParam->pszOldName) << GROUP_PARAM("newGroup", pParam->pszNewName));
		}
	}
	else MoveContactToGroup(hContact, ptrW(getWStringA(hContact, "IcqGroup")), pParam->pszNewName);
	
	return 0;
}

////////////////////////////////////////////////////////////////////////////////////////
// PS_AddToList - adds a contact to the contact list

MCONTACT CIcqProto::AddToList(int, PROTOSEARCHRESULT *psr)
{
	if (mir_wstrlen(psr->id.w) == 0)
		return 0;

	MCONTACT hContact = CreateContact(psr->id.w, true);
	if (psr->nick.w)
		setWString(hContact, "Nick", psr->nick.w);
	if (psr->firstName.w)
		setWString(hContact, "FirstName", psr->firstName.w);
	if (psr->lastName.w)
		setWString(hContact, "LastName", psr->lastName.w);

	return hContact;
}

////////////////////////////////////////////////////////////////////////////////////////
// PSR_AUTH

int CIcqProto::AuthRecv(MCONTACT, PROTORECVEVENT *pre)
{
	return Proto_AuthRecv(m_szModuleName, pre);
}

////////////////////////////////////////////////////////////////////////////////////////
// PSS_AUTHREQUEST

int CIcqProto::AuthRequest(MCONTACT hContact, const wchar_t* szMessage)
{
	ptrW wszGroup(Clist_GetGroup(hContact));
	if (!wszGroup)
		wszGroup = mir_wstrdup(L"General");

	auto *pReq = new AsyncHttpRequest(CONN_MAIN, REQUEST_POST, ICQ_API_SERVER "/buddylist/addBuddy", &CIcqProto::OnAddBuddy);
	pReq << AIMSID(this) << WCHAR_PARAM("authorizationMsg", szMessage) << WCHAR_PARAM("buddy", GetUserId(hContact)) << WCHAR_PARAM("group", wszGroup) << INT_PARAM("preAuthorized", 1);
	pReq->hContact = hContact;
	Push(pReq);
	return 0;
}

////////////////////////////////////////////////////////////////////////////////////////
// GetCaps - return protocol capabilities bits

INT_PTR CIcqProto::GetCaps(int type, MCONTACT)
{
	INT_PTR nReturn = 0;

	switch (type) {
	case PFLAGNUM_1:
		nReturn = PF1_IM | PF1_AUTHREQ | PF1_BASICSEARCH | PF1_ADDSEARCHRES | /*PF1_SEARCHBYNAME | TODO */
			PF1_VISLIST | PF1_FILE | PF1_CONTACT | PF1_SERVERCLIST;
		break;

	case PFLAGNUM_2:
	case PFLAGNUM_3:
		return PF2_ONLINE | PF2_SHORTAWAY | PF2_LONGAWAY | PF2_LIGHTDND | PF2_HEAVYDND | PF2_INVISIBLE;
	
	case PFLAGNUM_5:
		return PF2_SHORTAWAY | PF2_LONGAWAY | PF2_LIGHTDND | PF2_HEAVYDND | PF2_INVISIBLE;

	case PFLAGNUM_4:
		nReturn = PF4_FORCEAUTH | PF4_SUPPORTIDLE | PF4_OFFLINEFILES | PF4_IMSENDOFFLINE | PF4_SUPPORTTYPING | PF4_AVATARS | PF4_SERVERMSGID | PF4_READNOTIFY;
		break;

	case PFLAG_UNIQUEIDTEXT:
		return (INT_PTR)TranslateT("UIN/e-mail/phone");
	}

	return nReturn;
}

////////////////////////////////////////////////////////////////////////////////////////
// GetInfo - retrieves a contact info

int CIcqProto::GetInfo(MCONTACT hContact, int)
{
	RetrieveUserInfo(hContact);
	return 0;
}

////////////////////////////////////////////////////////////////////////////////////////
// SearchBasic - searches the contact by UID

HANDLE CIcqProto::SearchBasic(const wchar_t *pszSearch)
{
	if (!m_bOnline)
		return nullptr;

	auto *pReq = new AsyncRapiRequest(this, "search", &CIcqProto::OnSearchResults);
	pReq->params << WCHAR_PARAM(*pszSearch == '+' ? "phonenum" : "keyword", pszSearch);
	Push(pReq);

	return pReq;
}

////////////////////////////////////////////////////////////////////////////////////////
// SendFile - sends a file

HANDLE CIcqProto::SendFile(MCONTACT hContact, const wchar_t *szDescription, wchar_t **ppszFiles)
{
	IcqFileTransfer *pTransfer = nullptr;

	for (int i = 0; ppszFiles[i] != 0; i++) {
		struct _stat statbuf;
		if (_wstat(ppszFiles[0], &statbuf)) {
			debugLogW(L"'%s' is an invalid filename", ppszFiles[i]);
			continue;
		}

		int iFileId = _wopen(ppszFiles[i], _O_RDONLY | _O_BINARY, _S_IREAD);
		if (iFileId < 0)
			continue;

		pTransfer = new IcqFileTransfer(hContact, ppszFiles[i]);
		pTransfer->pfts.totalFiles = 1;
		pTransfer->pfts.currentFileSize = pTransfer->pfts.totalBytes = statbuf.st_size;
		pTransfer->m_fileId = iFileId;
		if (mir_wstrlen(szDescription))
			pTransfer->m_wszDescr = szDescription;

		auto *pReq = new AsyncHttpRequest(CONN_NONE, REQUEST_GET, "https://files.icq.com/files/init", &CIcqProto::OnFileInit);
		pReq << CHAR_PARAM("a", m_szAToken) << CHAR_PARAM("client", "icq") << CHAR_PARAM("f", "json") << WCHAR_PARAM("filename", pTransfer->m_wszShortName)
			<< CHAR_PARAM("k", appId()) << INT_PARAM("size", statbuf.st_size) << INT_PARAM("ts", TS());
		CalcHash(pReq);
		pReq->pUserInfo = pTransfer;
		Push(pReq);
	}

	return pTransfer; // Success, if at least one file was sent
}

////////////////////////////////////////////////////////////////////////////////////////
// PS_SendMessage - sends a message

int CIcqProto::SendMsg(MCONTACT hContact, const char *pszSrc)
{
	CMStringA szUserid(GetUserId(hContact));
	if (szUserid.IsEmpty())
		return 0;

	int id = InterlockedIncrement(&m_msgId);
	auto *pReq = new AsyncHttpRequest(CONN_MAIN, REQUEST_POST, ICQ_API_SERVER "/im/sendIM", &CIcqProto::OnSendMessage);

	auto *pOwn = new IcqOwnMessage(hContact, id, pReq->m_reqId, pszSrc);
	pReq->pUserInfo = pOwn;
	{
		mir_cslock lck(m_csOwnIds);
		m_arOwnIds.insert(pOwn);
	}

	pReq << AIMSID(this) << CHAR_PARAM("a", m_szAToken) << CHAR_PARAM("k", appId()) << CHAR_PARAM("mentions", "") 
		<< CHAR_PARAM("message", pszSrc) << CHAR_PARAM("offlineIM", "true") << CHAR_PARAM("t", szUserid) << INT_PARAM("ts", TS());
	Push(pReq);
	return id;
}

////////////////////////////////////////////////////////////////////////////////////////
// PS_SetStatus - sets the protocol status

int CIcqProto::SetStatus(int iNewStatus)
{
	debugLogA("CIcqProto::SetStatus iNewStatus = %d, m_iStatus = %d, m_iDesiredStatus = %d m_hWorkerThread = %p", iNewStatus, m_iStatus, m_iDesiredStatus, m_hWorkerThread);

	if (iNewStatus == m_iStatus)
		return 0;

	m_iDesiredStatus = iNewStatus;
	int iOldStatus = m_iStatus;

	// go offline
	if (iNewStatus == ID_STATUS_OFFLINE) {
		if (m_bOnline)
			SetServerStatus(ID_STATUS_OFFLINE);

		m_iStatus = m_iDesiredStatus;
		setAllContactStatuses(ID_STATUS_OFFLINE, false);

		ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)iOldStatus, m_iStatus);
	}
	// not logged in? come on
	else if (!m_bOnline && !IsStatusConnecting(m_iStatus)) {
		m_iStatus = ID_STATUS_CONNECTING;
		ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)iOldStatus, m_iStatus);

		if (mir_wstrlen(m_szOwnId) == 0) {
			debugLogA("Thread ended, UIN/password are not configured");
			ConnectionFailed(LOGINERR_BADUSERID);
			return 0;
		}

		if (!RetrievePassword()) {
			debugLogA("Thread ended, password is not configured");
			ConnectionFailed(LOGINERR_BADUSERID);
			return 0;
		}

		CheckPassword();
	}
	else if (m_bOnline) {
		debugLogA("setting server online status to %d", iNewStatus);
		SetServerStatus(iNewStatus);
	}

	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// PS_UserIsTyping - sends a UTN notification

int CIcqProto::UserIsTyping(MCONTACT hContact, int type)
{
	Push(new AsyncHttpRequest(CONN_MAIN, REQUEST_GET, ICQ_API_SERVER "/im/setTyping")
		<< AIMSID(this) << WCHAR_PARAM("t", GetUserId(hContact)) << CHAR_PARAM("typingStatus", (type == PROTOTYPE_SELFTYPING_ON) ? "typing" : "typed"));
	return 0;
}

////////////////////////////////////////////////////////////////////////////////////////
// PS_SetApparentMode - sets the visibility status

int CIcqProto::SetApparentMode(MCONTACT hContact, int iMode)
{
	int oldMode = getWord(hContact, "ApparentMode");
	if (oldMode != iMode) {
		setWord(hContact, "ApparentMode", iMode);
		SetPermitDeny(GetUserId(hContact), iMode != ID_STATUS_OFFLINE);
	}
	return 0;
}