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

class CMTProto;
typedef void (CMTProto::*TG_QUERY_HANDLER)(td::ClientManager::Response &response);

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

	td::ClientManager::RequestId requestId;
	TG_QUERY_HANDLER pHandler;
};

class CMTProto : public PROTO<CMTProto>
{
	std::unique_ptr<td::ClientManager> m_pClientMmanager;
	td::td_api::object_ptr<td::td_api::AuthorizationState> pAuthState;

	bool m_bAuthorized, m_bRunning = false, m_bTerminated;
	int32_t m_iClientId;
	uint64_t m_iQueryId;

	OBJLIST<TG_REQUEST> m_arRequests;

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

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

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

	void LogOut(void);
	void OnLoggedIn(void);
	void ProcessAuth(td::td_api::updateAuthorizationState *pObj);
	void ProcessResponse(td::ClientManager::Response);
	void SendQuery(td::td_api::Function *pFunc, TG_QUERY_HANDLER pHandler = nullptr);

	// Popups
	HANDLE m_hPopupClass;

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

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

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

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

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

	void OnErase() override;

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

	INT_PTR __cdecl SvcCreateAccMgrUI(WPARAM, LPARAM);
	
	// Events ////////////////////////////////////////////////////////////////////////////
	
	int __cdecl OnOptionsInit(WPARAM, LPARAM);

	// Options ///////////////////////////////////////////////////////////////////////////
	
	CMOption<wchar_t*> m_szOwnPhone;       // our own phone number
	CMOption<wchar_t*> m_wszDefaultGroup;  // clist group to store contacts
	CMOption<bool>     m_bHideGroupchats;  // do not open chat windows on creation
	CMOption<bool>     m_bUsePopups;

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

	void __cdecl ServerThread(void *);
};