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
|
#pragma once
#define DBKEY_ID "id"
class CMTProto;
typedef void (CMTProto:: *TG_QUERY_HANDLER)(td::ClientManager::Response &response);
typedef void (CMTProto:: *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(CMTProto *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(CMTProto *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(CMTProto *ppro, td::ClientManager::Response &response) override
{
(ppro->*pHandler)(response, pUserInfo);
}
};
/////////////////////////////////////////////////////////////////////////////////////////
struct TG_USER
{
TG_USER(uint64_t _1, MCONTACT _2, bool _3 = false) :
id(_1),
hContact(_2),
isGroupChat(_3)
{}
uint64_t id;
MCONTACT hContact;
bool isGroupChat;
};
class CMTProto : public PROTO<CMTProto>
{
std::unique_ptr<td::ClientManager> m_pClientMmanager;
TD::object_ptr<TD::AuthorizationState> pAuthState;
bool m_bAuthorized, m_bRunning = false, m_bTerminated;
int32_t m_iClientId, m_iMsgId;
uint64_t m_iQueryId;
OBJLIST<TG_REQUEST_BASE> 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 ProcessResponse(td::ClientManager::Response);
void SendQuery(TD::Function *pFunc, TG_QUERY_HANDLER pHandler = nullptr);
void SendQuery(TD::Function *pFunc, TG_QUERY_HANDLER_FULL pHandler, void *pUserInfo);
void ProcessAuth(TD::updateAuthorizationState *pObj);
void ProcessChat(TD::updateNewChat *pObj);
void ProcessGroups(TD::updateChatFilters *pObj);
void ProcessMessage(TD::updateNewMessage *pObj);
void ProcessUser(TD::updateUser *pObj);
void OnSendMessage(td::ClientManager::Response &response, void *pUserInfo);
int SendTextMessage(uint64_t chatId, const char *pszMessage);
void UpdateString(MCONTACT hContact, const char *pszSetting, const std::string &str);
// Users
int64_t m_iOwnId;
OBJLIST<TG_USER> m_arUsers;
TG_USER* FindUser(uint64_t id);
TG_USER* AddUser(uint64_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
CMTProto(const char *protoName, const wchar_t *userName);
~CMTProto();
//////////////////////////////////////////////////////////////////////////////////////
// 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);
// 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 *);
};
|