summaryrefslogtreecommitdiff
path: root/protocols/ICQ-WIM/src/proto.h
blob: 027747b562a8530f9b6b3592ca5f8ce8c1e30663 (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
// ---------------------------------------------------------------------------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-2024 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 declarations
// -----------------------------------------------------------------------------

#ifndef _ICQ_PROTO_H_
#define _ICQ_PROTO_H_

#include "m_system.h"
#include "m_protoint.h"

#define MRA_APP_ID "ic1pzYNtEU6dDnEQ"
#define ICQ_APP_ID "ic1nmMjqg7Yu-0hL"
#define ICQ_FILE_SERVER "https://u.icq.net/files/api/v1.1"
#define ICQ_FAKE_EVENT_ID 0xBABAEB
#define ICQ_ROBUST_SERVER "https://u.icq.net/rapi"

#define PS_DUMMY "/DoNothing"
#define PS_GOTO_INBOX "/SvcGotoInbox"

#define WIM_CAP_VOIP_VOICE         "094613504c7f11d18222444553540000"
#define WIM_CAP_VOIP_VIDEO         "094613514c7f11d18222444553540000"
#define WIM_CAP_FILETRANSFER       "094613434c7f11d18222444553540000"
#define WIM_CAP_UNIQ_REQ_ID        "094613534c7f11d18222444553540000"
#define WIM_CAP_EMOJI              "094613544c7f11d18222444553540000"
#define WIM_CAP_MENTIONS           "0946135b4c7f11d18222444553540000"
#define WIM_CAP_MAIL_NOTIFICATIONS "094613594c7f11d18222444553540000"
#define WIM_CAP_INTRO_DLG_STATE    "0946135a4c7f11d18222444553540000"

#define NG_CAP_SECUREIM            "4d69724e47536563757265494d000000"

typedef CProtoDlgBase<CIcqProto> CIcqDlgBase;

struct AIMSID
{
	AIMSID(CIcqProto *_ppro) :
		m_ppro(_ppro)
	{}

	CIcqProto *m_ppro;
};

enum ChatMenuItems
{
	IDM_INVITE = 10, IDM_LEAVE
};

struct IcqFileInfo
{
	IcqFileInfo(const std::string &pszUrl, const CMStringW &pwszDescr, uint32_t dwSize) :
		szUrl(pszUrl.c_str()),
		wszDescr(pwszDescr),
		dwFileSize(dwSize)
	{}

	CMStringA szUrl, szOrigUrl;
	CMStringW wszDescr;
	uint32_t dwFileSize;
	bool bIsSticker = false;
};

struct IcqGroup
{
	IcqGroup(int _p1, const CMStringW &_p2) :
		id(_p1),
		wszSrvName(_p2)
	{
		SetName(_p2);
	}

	int id;
	int level;
	CMStringW wszName, wszSrvName;

	void SetName(const CMStringW &str)
	{
		wszName = str;
		level = wszName.SpanIncluding(L">").GetLength();
		if (level != 0)
			wszName.Delete(0, level);
		wszName.Replace(L">", L"\\");
	}
};

struct IcqUser : public MZeroedObject
{
	IcqUser(const CMStringW &wszId, MCONTACT _contact) :
		m_aimid(wszId),
		m_hContact(_contact)
	{}

	CMStringW m_aimid;
	MCONTACT  m_hContact;
	bool      m_bInList, m_bGotCaps, m_bWasOnline;
	__int64   m_iProcessedMsgId;
	int       m_iApparentMode;
	time_t    m_timer1, m_timer2;
};

struct IcqConn
{
	HNETLIBCONN s;
	int lastTs, timeout;
};

struct IcqFileTransfer : public MZeroedObject
{
	bool m_bCanceled = false, m_bStarted = false;
	int m_fileId = -1;
	CMStringA m_szHost, m_szMsgId;
	CMStringW m_wszFileName, m_wszDescr;
	const wchar_t *m_wszShortName;
	PROTOFILETRANSFERSTATUS pfts;

	// create an object for sending
	IcqFileTransfer(MCONTACT hContact, const wchar_t *pwszFileName);

	~IcqFileTransfer();

	void FillHeaders(AsyncHttpRequest *pReq);
};

struct IcqOwnMessage
{
	IcqOwnMessage(MCONTACT _hContact, int _msgid, const char *pszText) :
		m_msgid(_msgid),
		m_hContact(_hContact),
		m_szText(mir_strdup(pszText))
	{
	}

	void setGuid(const char *pszGuid)
	{
		strncpy_s(m_guid, pszGuid, _TRUNCATE);
	}

	MCONTACT m_hContact;
	int m_msgid;
	char m_guid[50];
	ptrA m_szText;
	IcqFileTransfer *pTransfer = nullptr;
};

class CIcqProto : public PROTO<CIcqProto>
{
	friend class CForwardDlg;
	friend struct AsyncRapiRequest;

	class CIcqProtoImpl
	{
		friend class CIcqProto;

		CIcqProto &m_proto;
		CTimer m_heartBeat, m_markRead, m_delete;
		
		void OnHeartBeat(CTimer *) {
			m_proto.CheckStatus();
		}

		void OnMarkRead(CTimer *pTimer) {
			m_proto.SendMarkRead();
			pTimer->Stop();
		}

		void OnDelete(CTimer *pTimer) {
			m_proto.BatchDeleteMsg();
			pTimer->Stop();
		}

		CIcqProtoImpl(CIcqProto &pro) :
			m_proto(pro),
			m_delete(Miranda_GetSystemWindow(), UINT_PTR(this)),
			m_markRead(Miranda_GetSystemWindow(), UINT_PTR(this) + 1),
			m_heartBeat(Miranda_GetSystemWindow(), UINT_PTR(this) + 2)
		{
			m_delete.OnEvent = Callback(this, &CIcqProtoImpl::OnDelete);
			m_markRead.OnEvent = Callback(this, &CIcqProtoImpl::OnMarkRead);
			m_heartBeat.OnEvent = Callback(this, &CIcqProtoImpl::OnHeartBeat);
		}
	} m_impl;

	friend struct CIcqRegistrationDlg;
	friend class CGroupchatInviteDlg;
	friend class CEditIgnoreListDlg;
	friend class COptionsDlg;
	friend class CIcqEnterLoginDlg;
	friend class CGroupEditDlg;

	friend AsyncHttpRequest* operator <<(AsyncHttpRequest*, const AIMSID&);

	bool          m_bOnline, m_bTerminated, m_bFirstBos, m_isMra, m_bError462, m_bInvisible;
	int           m_iTimeShift;
		
	MCONTACT      CheckOwnMessage(const CMStringA &reqId, const CMStringA &msgId, bool bRemove);
	void          CheckPassword(void);
	void          ConnectionFailed(int iReason, int iErrorCode = 0);
	void          EmailNotification(const wchar_t *pwszText);
	void          ForwardMessage(MEVENT hEVent, MCONTACT hContact);
	void          GetPermitDeny();
	wchar_t*      GetUIN(MCONTACT hContact);
	void          MoveContactToGroup(MCONTACT hContact, const wchar_t *pwszGroup, const wchar_t *pwszNewGroup);
	void          RetrieveHistoryChunk(MCONTACT hContact, __int64 patchVer, __int64 startMsgId, unsigned iCount);
	bool          RetrievePassword();
	void          RetrievePresence(MCONTACT hContact);
	void          RetrieveUserCaps(IcqUser *pUser);
	void          RetrieveUserHistory(MCONTACT, __int64 startMsgId, bool bCreateRead);
	void          RetrieveUserInfo(MCONTACT hContact);
	void          SendMrimLogin(MHttpResponse *pReply);
	void          SendMessageParts(MCONTACT hContact, const JSONNode &parts, IcqOwnMessage *pOwn = nullptr);
	void          SetOwnId(const CMStringW &wszId);
	void          SetServerStatus(int iNewStatus);
	void          ShutdownSession(void);
	void          StartSession(void);
				     
	void          CheckAvatarChange(MCONTACT hContact, const JSONNode&);
	bool          CheckFile(MCONTACT hContact, CMStringW &wszFileName, IcqFileInfo* &pFileInfo);
	void          CheckLastId(MCONTACT hContact, const JSONNode&);
	void          Json2int(MCONTACT, const JSONNode&, const char *szJson, const char *szSetting, bool bIsPartial);
	void          Json2string(MCONTACT, const JSONNode&, const char *szJson, const char *szSetting, bool bIsPartial);
	MCONTACT      ParseBuddyInfo(const JSONNode &buddy, MCONTACT hContact = INVALID_CONTACT_ID, bool bIsPartial = false);
	void          ParseMessage(MCONTACT hContact, __int64 &lastMsgId, const JSONNode &msg, bool bCreateRead, bool bLocalTime);
	void          ParseMessagePart(MCONTACT hContact, const JSONNode &msg, IcqFileInfo *&pFileInfo);
	IcqFileInfo*  RetrieveFileInfo(MCONTACT hContact, const CMStringW &wszUrl);
	int           StatusFromPresence(const JSONNode &presence, MCONTACT hContact);
	void          ProcessPatchVersion(MCONTACT hContact, __int64 currPatch);
	void          ProcessStatus(IcqUser *pUser, int iStatus);
				     
	void          OnLoggedIn(void);
	void          OnLoggedOut(void);

	mir_cs        m_csMarkReadQueue;
	LIST<IcqUser> m_arMarkReadQueue;
	void          SendMarkRead();

	mir_cs        m_csDeleteQueue;
	LIST<char>    m_arDeleteQueue;
	MCONTACT      m_hDeleteContact = INVALID_CONTACT_ID;
	void          BatchDeleteMsg();

	__int64       getId(MCONTACT hContact, const char *szSetting);
	void          setId(MCONTACT hContact, const char *szSetting, __int64 iValue);
				     
	void          OnAddBuddy(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnAddClient(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnCheckMraAuth(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnCheckMraAuthFinal(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnCheckMrimLogin(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnCheckPassword(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnCheckPhone(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnFetchEvents(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnFileContinue(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnFileInit(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnFileInfo(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnGenToken(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnGetChatInfo(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnGetPatches(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnGetPermitDeny(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnGePresence(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnGetSticker(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnGetUserCaps(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnGetUserHistory(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnGetUserInfo(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnLeaveChat(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnLoginViaPhone(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnNormalizePhone(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnReceiveAvatar(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnSearchResults(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnSendMessage(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnSessionEnd(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnStartSession(MHttpResponse *pReply, AsyncHttpRequest *pReq);
	void          OnValidateSms(MHttpResponse *pReply, AsyncHttpRequest *pReq);
				     
	void          ProcessBuddyList(const JSONNode &pRoot);
	void          ProcessDiff(const JSONNode &pRoot);
	void          ProcessEvent(const JSONNode &pRoot);
	void          ProcessGroupChat(const JSONNode &pRoot);
	void          ProcessHistData(const JSONNode &pRoot);
	void          ProcessImState(const JSONNode &pRoot);
	void          ProcessMyInfo(const JSONNode &pRoot);
	void          ProcessNotification(const JSONNode &pRoot);
	void          ProcessOnline(const JSONNode &presence, MCONTACT hContact);
	void          ProcessPermissions(const JSONNode &pRoot);
	void          ProcessPresence(const JSONNode &pRoot);
	void          ProcessSessionEnd(const JSONNode &pRoot);
	void          ProcessTyping(const JSONNode &pRoot);

	IcqConn       m_ConnPool[CONN_LAST];
	CMStringA     m_szPassword;
	CMStringA     m_szSessionKey;
	CMStringA     m_szAToken;
	CMStringA     m_szRToken;
	CMStringA     m_fetchBaseURL;
	CMStringA     m_aimsid;
	CMStringA     m_szMraCookie;
	LONG          m_msgId = 1;
	int           m_iRClientId;
	HGENMENU      m_hUploadGroups;
	MCONTACT      m_hFavContact = INVALID_CONTACT_ID;

	mir_cs        m_csOwnIds;
	OBJLIST<IcqOwnMessage> m_arOwnIds;

	OBJLIST<IcqGroup> m_arGroups;

	int           m_unreadEmails = -1;
	CMStringA     m_szMailBox;

	bool          m_bIgnoreListEmpty = true;
	bool          m_bRememberPwd; // store password in a database
	bool          m_bDlgActive;

	////////////////////////////////////////////////////////////////////////////////////////
	// group chats

	int       __cdecl GcEventHook(WPARAM, LPARAM);
	int       __cdecl GcMenuHook(WPARAM, LPARAM);

	void      GcProcessLogMenu(SESSION_INFO *si, int);
	void      GcSendPrivateMessage(GCHOOK *gch);

	SESSION_INFO* GcCreate(const wchar_t *pwszId, const wchar_t *pwszNick);

	void      RetrieveChatInfo(MCONTACT hContact);

	void      InviteUserToChat(SESSION_INFO *si);
	void      LeaveDestroyChat(SESSION_INFO *si);

	////////////////////////////////////////////////////////////////////////////////////////
	// http queue

	mir_cs    m_csHttpQueue;
	HANDLE    m_evRequestsQueue;
	LIST<AsyncHttpRequest> m_arHttpQueue;

	void      CalcHash(AsyncHttpRequest*);
	void      DropQueue();
	bool      ExecuteRequest(AsyncHttpRequest*);
	bool      IsQueueEmpty();
	void      Push(MHttpRequest*);
	bool      RefreshRobustToken(AsyncHttpRequest *pReq);

	////////////////////////////////////////////////////////////////////////////////////////
	// cache

	mir_cs    m_csCache;
	OBJLIST<IcqUser> m_arCache;

	void      InitContactCache(void);
	IcqUser*  FindUser(const CMStringW &pwszId);
	MCONTACT  CreateContact(const CMStringW &pwszId, bool bTemporary);
	
	void      GetAvatarFileName(MCONTACT hContact, wchar_t *pszDest, size_t cbLen);

	////////////////////////////////////////////////////////////////////////////////////////
	// Menus

	HGENMENU  hmiForward, hmiConvert, hmiFavorites;

	void      InitMenus();

	INT_PTR   __cdecl SvcExecMenu(WPARAM, LPARAM);
	int       __cdecl OnPrebuildMenu(WPARAM, LPARAM);

	////////////////////////////////////////////////////////////////////////////////////////
	// threads

	HANDLE    m_hWorkerThread;
	void      __cdecl ServerThread(void*);
	void      __cdecl PollThread(void*);
	void      __cdecl OfflineFileThread(void*);

	////////////////////////////////////////////////////////////////////////////////////////
	// services

	INT_PTR   __cdecl GetAvatar(WPARAM, LPARAM);
	INT_PTR   __cdecl GetAvatarCaps(WPARAM, LPARAM);
	INT_PTR   __cdecl GetAvatarInfo(WPARAM, LPARAM);
	INT_PTR   __cdecl SetAvatar(WPARAM, LPARAM);
	
	INT_PTR   __cdecl SvcLeaveChat(WPARAM, LPARAM);
	INT_PTR   __cdecl SvcOfflineFile(WPARAM, LPARAM);

	INT_PTR   __cdecl EditGroups(WPARAM, LPARAM);
	INT_PTR   __cdecl EditProfile(WPARAM, LPARAM);
	INT_PTR   __cdecl SvcGetEmailCount(WPARAM, LPARAM);
	INT_PTR   __cdecl SvcGotoInbox(WPARAM, LPARAM);
	INT_PTR   __cdecl UploadGroups(WPARAM, LPARAM);

	INT_PTR   __cdecl SvcLoadHistory(WPARAM, LPARAM);

	////////////////////////////////////////////////////////////////////////////////////////
	// events

	int       __cdecl OnGroupChange(WPARAM, LPARAM);
	int       __cdecl OnOptionsInit(WPARAM, LPARAM);
	int       __cdecl OnUserInfoInit(WPARAM, LPARAM);

	////////////////////////////////////////////////////////////////////////////////////////
	// PROTO_INTERFACE

	MCONTACT  AddToList( int flags, PROTOSEARCHRESULT *psr) override;
			    
	int       AuthRecv(MCONTACT, PROTORECVEVENT *pre) override;
	int       AuthRequest(MCONTACT hContact, const wchar_t *szMessage) override;

	INT_PTR   GetCaps(int type, MCONTACT hContact = NULL) override;
	int       GetInfo(MCONTACT hContact, int infoType) override;
			    
	HANDLE    SearchBasic(const wchar_t *id) override;

	HANDLE    SendFile(MCONTACT hContact, const wchar_t *szDescription, wchar_t **ppszFiles) override;
	int       SendMsg(MCONTACT hContact, MEVENT hReplyEvent, const char *msg) override;
			    
	int       SetStatus(int iNewStatus) override;
			    
	int       UserIsTyping(MCONTACT hContact, int type) override;
			    
	void      OnBuildProtoMenu(void) override;
	void      OnContactAdded(MCONTACT) override;
	bool      OnContactDeleted(MCONTACT) override;
	MWindow   OnCreateAccMgrUI(MWindow) override;
	void      OnEventDeleted(MCONTACT, MEVENT) override;
	void      OnEventEdited(MCONTACT, MEVENT, const DBEVENTINFO &dbei) override;
	void      OnMarkRead(MCONTACT, MEVENT) override;
	void      OnModulesLoaded() override;
	void      OnReceiveOfflineFile(DB::FILE_BLOB &blob, void *ft) override;
	void      OnSendOfflineFile(DB::EventInfo &dbei, DB::FILE_BLOB &blob, void *ft) override;
	void      OnShutdown() override;

public:
	CIcqProto(const char*, const wchar_t*);
	~CIcqProto();

	CMOption<wchar_t*> m_szOwnId;         // our own aim id

	CMOption<bool>     m_bHideGroupchats; // don't pop up group chat windows on startup
	CMOption<bool>     m_bUseTrayIcon;    // use tray icon notifications
	CMOption<bool>     m_bErrorPopups;    // display popups with errors
	CMOption<bool>     m_bLaunchMailbox;  // launch browser to view email

	CMOption<uint32_t> m_iTimeDiff1;		  // set this status to m_iStatus1 after this interval of secs
	CMOption<uint32_t> m_iStatus1;
	CMOption<uint32_t> m_iTimeDiff2;		  // set this status to m_iStatus2 after this interval of secs
	CMOption<uint32_t> m_iStatus2;

	void CheckStatus(void);
	MCONTACT GetRealContact(IcqUser *pUser);
	CMStringW GetUserId(MCONTACT);

	__forceinline int TS() const
	{	return time(0) - m_iTimeShift;
	}

	__forceinline const char *appId() const
	{	return (m_isMra) ? MRA_APP_ID : ICQ_APP_ID;
	}

	void SetPermitDeny(const CMStringW &userId, bool bAllow);
};

struct CMPlugin : public ACCPROTOPLUGIN<CIcqProto>
{
	CMPlugin();

	int Load() override;
};

#endif