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

#define STATUS_SWITCH_TIMEOUT 600

#define DBKEY_ID "id"

#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
{
	enum Type { AVATAR = 1 };

	TG_FILE_REQUEST(Type _1, const char *_2, const wchar_t *_3) :
		m_type(_1),
		m_uniqueId(_2),
		m_destPath(_3)
	{}

	Type m_type;
	CMStringA m_uniqueId;
	CMStringW m_destPath;
};

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

	int64_t  id;
	MCONTACT  hContact;
	bool      isGroupChat;
	CMStringA szAvatarHash;
	CMStringW wszNick;
	time_t    m_timer1 = 0, m_timer2 = 0;
	SESSION_INFO *m_si = nullptr;
};

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;
};

class CTelegramProto : public PROTO<CTelegramProto>
{
	class CProtoImpl
	{
		friend class CTelegramProto;
		CTelegramProto &m_proto;

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

		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_markRead.OnEvent = Callback(this, &CProtoImpl::OnMarkRead);
			m_keepAlive.OnEvent = Callback(this, &CProtoImpl::OnKeepAlive);
		}
	} m_impl;

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

	std::unique_ptr<td::ClientManager> m_pClientMmanager;
	TD::object_ptr<TD::AuthorizationState> pAuthState;

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

	bool m_bAuthorized, m_bTerminated, m_bUnregister = false, m_bSmileyAdd = false;
	int32_t m_iClientId, m_iMsgId;
	int64_t m_iQueryId;
	CMStringA m_szFullPhone;

	OBJLIST<TG_REQUEST_BASE> m_arRequests;
	OBJLIST<TG_FILE_REQUEST> m_arFiles;

	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 OnEndSession(td::ClientManager::Response &response);
	void OnSendMessage(td::ClientManager::Response &response, void *pUserInfo);
	void OnUpdateAuth(td::ClientManager::Response &response);

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

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

	void ProcessAuth(TD::updateAuthorizationState *pObj);
	void ProcessBasicGroup(TD::updateBasicGroup *pObj);
	void ProcessChat(TD::updateNewChat *pObj);
	void ProcessChatPosition(TD::updateChatPosition *pObj);
	void ProcessFile(TD::updateFile *pObj);
	void ProcessGroups(TD::updateChatFilters *pObj);
	void ProcessMarkRead(TD::updateChatReadInbox *pObj);
	void ProcessMessage(TD::updateNewMessage *pObj);
	void ProcessStatus(TD::updateUserStatus *pObj);
	void ProcessSuperGroup(TD::updateSupergroup *pObj);
	void ProcessUser(TD::updateUser *pObj);

	CMStringA GetMessageText(TD::MessageContent *pBody);

	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;

	void InitGroupChat(TG_USER *pUser, const TD::chat *pChat, bool bUpdateMembers);
	void StartGroupChat(td::ClientManager::Response &response, void *pUserData);

	// Users
	int64_t m_iOwnId;
	MGROUP m_iBaseGroup;
	OBJLIST<TG_USER> m_arUsers;

	TG_USER* FindUser(int64_t id);
	TG_USER* AddUser(int64_t id, bool bIsChat);

	// 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

	INT_PTR GetCaps(int type, MCONTACT hContact = NULL) override;
	
	int SendMsg(MCONTACT hContact, int flags, const char *pszMessage) override;
	int SetStatus(int iNewStatus) override;

	void OnModulesLoaded() override;
	void OnShutdown() override;
	void OnErase() override;

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

	INT_PTR __cdecl SvcCreateAccMgrUI(WPARAM, LPARAM);
	
	// Events ////////////////////////////////////////////////////////////////////////////
	
	int __cdecl OnOptionsInit(WPARAM, LPARAM);
	int __cdecl OnDbMarkedRead(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<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 ServerThread(void *);
};