summaryrefslogtreecommitdiff
path: root/protocols/Telegram/src/proto.h
blob: 5801fb30dca77f325fb9c609301d0dfa89094b49 (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
#pragma once

#define DBKEY_ID "id"
#define DBKEY_COMPAT "Compatibility"
#define DBKEY_OWNER  "OwnerId"
#define DBKEY_THREAD "ThreadId"
#define DBKEY_AUTHORIZED "Authorized"

#define DBKEY_AVATAR_HASH "AvatarHash"
#define DBKEY_AVATAR_TYPE "AvatarType"

class CTelegramProto;
typedef void (CTelegramProto:: *TG_QUERY_HANDLER)(td::ClientManager::Response &response);
typedef void (CTelegramProto:: *TG_QUERY_HANDLER_FULL)(td::ClientManager::Response &response, void *pUserInfo);

struct TG_REQUEST_BASE
{
	TG_REQUEST_BASE(td::ClientManager::RequestId _1) :
		requestId(_1)
	{}

	virtual ~TG_REQUEST_BASE()
	{}

	td::ClientManager::RequestId requestId;

	virtual void Execute(CTelegramProto *ppro, td::ClientManager::Response &response) = 0;
};

struct TG_REQUEST : public TG_REQUEST_BASE
{
	TG_REQUEST(td::ClientManager::RequestId _1, TG_QUERY_HANDLER _2) :
		TG_REQUEST_BASE(_1),
		pHandler(_2)
	{}

	TG_QUERY_HANDLER pHandler;

	void Execute(CTelegramProto *ppro, td::ClientManager::Response &response) override
	{
		(ppro->*pHandler)(response);
	}
};

struct TG_REQUEST_FULL : public TG_REQUEST_BASE
{
	TG_REQUEST_FULL(td::ClientManager::RequestId _1, TG_QUERY_HANDLER_FULL _2, void *_3) :
		TG_REQUEST_BASE(_1),
		pHandler(_2),
		pUserInfo(_3)
	{}

	TG_QUERY_HANDLER_FULL pHandler;
	void *pUserInfo;

	void Execute(CTelegramProto *ppro, td::ClientManager::Response &response) override
	{
		(ppro->*pHandler)(response, pUserInfo);
	}
};

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

struct TG_FILE_REQUEST : public MZeroedObject
{
	enum Type { AVATAR = 1, FILE = 2, PICTURE = 3, VIDEO = 4, VOICE = 5 };

	TG_FILE_REQUEST(Type _1, TD::int53 _2, const char *_3) :
		m_type(_1),
		m_fileId(_2),
		m_uniqueId(_3)
	{}

	~TG_FILE_REQUEST()
	{	delete ofd;
	}

	Type m_type;
	MCONTACT m_hContact = 0;
	TD::int53 m_fileId, m_fileSize = 0;
	CMStringA m_uniqueId, m_szUserId;
	CMStringW m_destPath, m_fileName, m_wszDescr;
	OFDTHREAD *ofd = 0;
	bool m_bRecv = false, m_isSmiley = false;
};

struct TG_USER : public MZeroedObject
{
	TG_USER(int64_t _1, MCONTACT _2, bool _3 = false) :
		id(_1),
		hContact(_2),
		isGroupChat(_3)
	{
	}

	~TG_USER()
	{
		delete pReactions;
	}

	int64_t   id, chatId = -1;
	MCONTACT  hContact;
	int       folderId = -1, nHistoryChunks;
	bool      isGroupChat, isChannel, isBot, isForum, bLoadMembers, bStartChat, bInited;
	CMStringA szAvatarHash;
	CMStringW wszNick, wszFirstName, wszLastName;
	time_t    m_timer1 = 0, m_timer2 = 0;
	SESSION_INFO *m_si = nullptr;
	TD::chatNotificationSettings notificationSettings;
	OBJLIST<char> *pReactions = nullptr;

	CMStringW getDisplayName() const;
};

struct TG_SUPER_GROUP
{
	TG_SUPER_GROUP(int64_t _1, TD::object_ptr<TD::supergroup> _2) :
		id(_1),
		group(std::move(_2))
	{}

	int64_t id;
	TD::object_ptr<TD::supergroup> group;
};

struct TG_BASIC_GROUP
{
	TG_BASIC_GROUP(int64_t _1, TD::object_ptr<TD::basicGroup> _2) :
		id(_1),
		group(std::move(_2))
	{}

	int64_t id;
	TD::object_ptr<TD::basicGroup> group;
};

struct TG_OWN_MESSAGE
{
	TG_OWN_MESSAGE(MCONTACT _1, HANDLE _2, const char *_3) :
		hContact(_1),
		hAck(_2),
		szMsgId(_3)
	{}

	int64_t   tmpFileId = -1;
	HANDLE    hAck;
	MCONTACT  hContact;
	CMStringA szMsgId;
};

class CTelegramProto : public PROTO<CTelegramProto>
{
	friend class CReplyDlg;
	friend class CForwardDlg;
	friend class CReactionsDlg;
	friend class COptionsDlg;
	friend class COptSessionsDlg;
	friend class CAddPhoneContactDlg;

	class CProtoImpl
	{
		friend class CTelegramProto;
		CTelegramProto &m_proto;

		CTimer m_keepAlive, m_markRead, m_deleteMsg;
		void OnKeepAlive(CTimer *)
		{	m_proto.SendKeepAlive();
		}

		void OnDeleteMsg(CTimer *)
		{	m_proto.SendDeleteMsg();
		}

		void OnMarkRead(CTimer *)
		{	m_proto.SendMarkRead();
		}

		CProtoImpl(CTelegramProto &pro) :
			m_proto(pro),
			m_markRead(Miranda_GetSystemWindow(), UINT_PTR(this)),
			m_keepAlive(Miranda_GetSystemWindow(), UINT_PTR(this)+1),
			m_deleteMsg(Miranda_GetSystemWindow(), UINT_PTR(this)+2)
		{
			m_markRead.OnEvent = Callback(this, &CProtoImpl::OnMarkRead);
			m_deleteMsg.OnEvent = Callback(this, &CProtoImpl::OnDeleteMsg);
			m_keepAlive.OnEvent = Callback(this, &CProtoImpl::OnKeepAlive);
		}
	} m_impl;

	bool __forceinline isRunning() const 
	{	return m_pClientManager != nullptr; 
	}

	std::unique_ptr<td::ClientManager> m_pClientManager;
	TD::object_ptr<TD::AuthorizationState> pAuthState;
	TD::object_ptr<TD::ConnectionState> pConnState;

	TD::array<TD::int53> m_botIds;

	mir_cs m_csMarkRead;
	TD::int53 m_markChatId = 0;
	TD::array<TD::int53> m_markIds;

	mir_cs m_csDeleteMsg;
	bool m_bDeleteForAll;
	TD::int53 m_deleteChatId = 0;
	TD::array<TD::int53> m_deleteIds;

	bool m_bAuthorized, m_bTerminated, m_bUnregister = false, m_bSmileyAdd = false;
	int32_t m_iClientId, m_iQueryId;
	TD::int32 m_iDefaultMutePrivate, m_iDefaultMuteGroup, m_iDefaultMuteChannel;
	CMStringA m_defaultEmoji;

	OBJLIST<TG_OWN_MESSAGE> m_arOwnMsg;

	mir_cs m_csRequests;
	OBJLIST<TG_REQUEST_BASE> m_arRequests;

	mir_cs m_csFiles;
	LIST<TG_FILE_REQUEST> m_arFiles;
	TG_FILE_REQUEST* FindFile(const char *pszUniqueId);

	static INT_PTR CALLBACK EnterEmail(void *param);
	static INT_PTR CALLBACK EnterEmailCode(void *param);
	static INT_PTR CALLBACK EnterPassword(void *param);
	static INT_PTR CALLBACK EnterPhoneCode(void *param);

	CMStringW GetProtoFolder() const
	{	return CMStringW(VARSW(L"%miranda_userdata%")) + L"\\" + _A2T(m_szModuleName);
	}

	void OnAvatarSet(td::ClientManager::Response &response, void *pUserInfo);
	void OnEndSession(td::ClientManager::Response &response);
	void OnGetFileInfo(td::ClientManager::Response &response, void *pUserInfo);
	void OnGetFileLink(td::ClientManager::Response &response);
	void OnGetHistory(td::ClientManager::Response &response, void *pUserInfo);
	void OnGetSessions(td::ClientManager::Response &response, void *pUserInfo);
	void OnKillSession(td::ClientManager::Response &response, void *pUserInfo);
	void OnLeaveChat(td::ClientManager::Response &response, void *pUserInfo);
	void OnSendFile(td::ClientManager::Response &response, void *pUserInfo);
	void OnSendMessage(td::ClientManager::Response &response);
	void OnUpdateAuth(td::ClientManager::Response &response);
	void OnGetChats(td::ClientManager::Response &response);

	void LogOut(void);
	void OnLoggedIn(void);
	void ProcessResponse(td::ClientManager::Response);

	void SendKeepAlive(void);
	void SendDeleteMsg(void);
	void SendMarkRead(void);
	int  SendQuery(TD::Function *pFunc, TG_QUERY_HANDLER pHandler = nullptr);
	int  SendQuery(TD::Function *pFunc, TG_QUERY_HANDLER_FULL pHandler, void *pUserInfo);
	int  SendTextMessage(int64_t chatId, int64_t threadId, int64_t replyId, const char *pszMessage);

	void ProcessAvatar(const TD::file *pFile, TG_USER *pUser);
	void ProcessAuth(TD::updateAuthorizationState *pObj);
	void ProcessBasicGroup(TD::updateBasicGroup *pObj);
	void ProcessBasicGroupInfo(TD::updateBasicGroupFullInfo *pObj);
	void ProcessChat(TD::updateNewChat *pObj);
	void ProcessChatAction(TD::updateChatAction *pObj);
	void ProcessChatHasProtected(TD::updateChatHasProtectedContent *pObj);
	void ProcessChatLastMessage(TD::updateChatLastMessage *pObj);
	void ProcessChatNotification(TD::updateChatNotificationSettings *pObj);
	void ProcessChatPosition(TD::updateChatPosition *pObj);
	void ProcessChatReactions(TD::updateChatAvailableReactions *);
	void ProcessConnectionState(TD::updateConnectionState *pObj);
	void ProcessActiveEmoji(TD::updateActiveEmojiReactions *pObj);
	void ProcessDeleteMessage(TD::updateDeleteMessages *pObj);
	void ProcessFile(TD::updateFile *pObj);
	void ProcessFileMessage(TG_FILE_REQUEST *ft, const TD::message *pMsg, bool);
	void ProcessForum(TD::updateForumTopicInfo *pForum);
	void ProcessGroups(TD::updateChatFolders *pObj);
	void ProcessMarkRead(TD::updateChatReadInbox *pObj);
	void ProcessMessage(const TD::message *pMsg);
	void ProcessMessageContent(TD::updateMessageContent *pObj);
	void ProcessMessageReactions(TD::updateMessageInteractionInfo *pObj);
	void ProcessOption(TD::updateOption *pObj);
	void ProcessRemoteMarkRead(TD::updateChatReadOutbox *pObj);
	void ProcessScopeNotification(TD::updateScopeNotificationSettings *pObj);
	void ProcessStatus(TD::updateUserStatus *pObj);
	void ProcessSuperGroup(TD::updateSupergroup *pObj);
	void ProcessSuperGroupInfo(TD::updateSupergroupFullInfo *pObj);
	void ProcessUser(TD::updateUser *pObj);

	void UnregisterSession();

	struct EmbeddedFile
	{
		EmbeddedFile(CMStringA &_1) :
			szBody(_1)
		{}

		CMStringA &szBody;
		const TG_USER *pUser;
		const char *pszId, *pszUser;
		const TD::message *pMsg;
		bool bRead;
	};
	
	bool GetMessageFile(const EmbeddedFile &embed, TG_FILE_REQUEST::Type, const TD::file *pFile, const char *pszFileName, const char *pszCaption);
	
	CMStringA GetFormattedText(TD::object_ptr<TD::formattedText> &pText);
	CMStringA GetMessagePreview(const TD::file *pFile);
	CMStringA GetMessageSticker(const TD::file *pFile, const char *pwszExtension);
	CMStringA GetMessageText(TG_USER *pUser, const TD::message *pMsg, bool bSkipJoin = false, bool bRead = false);

	void UpdateString(MCONTACT hContact, const char *pszSetting, const std::string &str);

	// Avatars
	CMStringW GetAvatarFilename(MCONTACT hContact);

	INT_PTR __cdecl SvcGetAvatarCaps(WPARAM, LPARAM);
	INT_PTR __cdecl SvcGetAvatarInfo(WPARAM, LPARAM);
	INT_PTR __cdecl SvcGetMyAvatar(WPARAM, LPARAM);
	INT_PTR __cdecl SvcSetMyAvatar(WPARAM, LPARAM);

	// Group chats
	OBJLIST<TG_BASIC_GROUP>  m_arBasicGroups;
	OBJLIST<TG_SUPER_GROUP>  m_arSuperGroups;

	INT_PTR __cdecl SvcLeaveChat(WPARAM, LPARAM);

	void InitGroupChat(TG_USER *pUser, const wchar_t *pwszTitle);
	void StartGroupChat(td::ClientManager::Response &response, void *pUserData);
	
	void Chat_SendPrivateMessage(GCHOOK *gch);
	void Chat_LogMenu(GCHOOK *gch);

	bool GetGcUserId(TG_USER *pUser, const TD::message *pMsg, char *dest);
	void GcAddMembers(TG_USER *pChat, const TD::array<TD::object_ptr<TD::chatMember>> &pMembers, bool bSilent);
	void GcChangeMember(TG_USER *pChat, const char *adminId, TD::int53 userId, bool bJoined);
	void GcChangeTopic(TG_USER *pChat, const std::string &szNewTopic);
	void GcRun(TG_USER *pChat);

	// Search
	volatile unsigned m_iSearchCount;
	TD::array<TD::int53> m_searchIds;

	void OnSearchResults(td::ClientManager::Response &response);

	bool CheckSearchUser(TG_USER *pUser);
	void ReportSearchUser(TG_USER *pUser);

	// Users
	int64_t m_iOwnId;
	MGROUP m_iBaseGroup;
	MCONTACT m_iSavedMessages;
	LIST<TG_USER> m_arChats;
	OBJLIST<TG_USER> m_arUsers;

	TG_USER* FindChat(int64_t id);
	TG_USER* FindUser(int64_t id);
	TG_USER* AddUser(int64_t id, bool bIsChat);
	TG_USER* AddFakeUser(int64_t id, bool bIsChat);
	TG_USER* GetSender(const TD::MessageSender *pSender);
	
	int64_t  GetId(MCONTACT, const char *pszSetting = DBKEY_ID);
	void     SetId(MCONTACT, int64_t id, const char *pszSetting = DBKEY_ID);

	int      GetDefaultMute(const TG_USER *pUser);

	MCONTACT GetRealContact(const TG_USER *pUser);
	void     RemoveFromClist(TG_USER *pUser);
	void     MarkRead(MCONTACT hContact, const CMStringA &szMaxId, bool bSent);

	// Menus
	HGENMENU hmiForward, hmiReaction;

	void InitMenus();

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

	// Popups
	HANDLE m_hPopupClass;

	void InitPopups(void);
	void Popup(MCONTACT hContact, const wchar_t *szMsg, const wchar_t *szTitle);

public:
	//////////////////////////////////////////////////////////////////////////////////////
	// Ctors

	CTelegramProto(const char *protoName, const wchar_t *userName);
	~CTelegramProto();

	//////////////////////////////////////////////////////////////////////////////////////
	// Virtual functions

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

	INT_PTR  GetCaps(int type, MCONTACT hContact = NULL) override;

	HANDLE   SendFile(MCONTACT hContact, const wchar_t *szDescription, wchar_t **ppszFiles) override;

	HANDLE   SearchByName(const wchar_t *nick, const wchar_t *firstName, const wchar_t *lastName) override;
	int      SendMsg(MCONTACT hContact, MEVENT hReplyEvent, const char *pszMessage) override;
	int      SetStatus(int iNewStatus) override;

	int      UserIsTyping(MCONTACT hContact, int type) override;
		
	void     OnBuildProtoMenu() override;
	void     OnCacheInit() override;
	void     OnContactAdded(MCONTACT hContact) override;
	bool     OnContactDeleted(MCONTACT hContact, uint32_t flags) override;
	MWindow  OnCreateAccMgrUI(MWindow hwndParent) override;
	void     OnErase() override;
	void     OnEventDeleted(MCONTACT, MEVENT, int) override;
	void     OnEventEdited(MCONTACT, MEVENT, const DBEVENTINFO &dbei) override;
	void     OnMarkRead(MCONTACT, MEVENT) override;
	void     OnModulesLoaded() override;
	void     OnReceiveOfflineFile(DB::EventInfo &dbei, DB::FILE_BLOB &blob) override;
	void     OnSendOfflineFile(DB::EventInfo &dbei, DB::FILE_BLOB &blob, void *hTransfer) override;
	void     OnShutdown() override;

	// Events ////////////////////////////////////////////////////////////////////////////
	
	int __cdecl OnOptionsInit(WPARAM, LPARAM);
	int __cdecl OnWindowEvent(WPARAM, LPARAM);

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

	// Services //////////////////////////////////////////////////////////////////////////

	INT_PTR __cdecl SvcAddByPhone(WPARAM, LPARAM);
	INT_PTR __cdecl SvcOfflineFile(WPARAM, LPARAM);

	INT_PTR __cdecl SvcCanEmptyHistory(WPARAM, LPARAM);
	INT_PTR __cdecl SvcLoadServerHistory(WPARAM, LPARAM);
	INT_PTR __cdecl SvcEmptyServerHistory(WPARAM, LPARAM);

	// Options ///////////////////////////////////////////////////////////////////////////
	
	CMOption<uint32_t> m_iCountry;		   // set this status to m_iStatus1 after this interval of secs
	CMOption<wchar_t*> m_szOwnPhone;       // our own phone number
	CMOption<wchar_t*> m_wszDefaultGroup;  // clist group to store contacts
	CMOption<wchar_t*> m_wszDeviceName;    // how do you see this session in Device List
	CMOption<bool>     m_bHideGroupchats;  // do not open chat windows on creation
	CMOption<bool>     m_bUsePopups;
	CMOption<bool>     m_bUrlPreview;      // embrace URLs into [url] tags
	CMOption<bool>     m_bCompressFiles;   // embed pictures & videos into a message on send
	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;

	// Processing Threads ////////////////////////////////////////////////////////////////

	void __cdecl OfflineFileThread(void *);
	void __cdecl ServerThread(void *);
};

typedef CProtoDlgBase<CTelegramProto> CTelegramDlgBase;