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
|
/*
Copyright (C) 2012-22 Miranda NG team (https://miranda-ng.org)
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 version 2
of the License.
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, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
void __cdecl CMTProto::ServerThread(void *)
{
m_bRunning = true;
m_bTerminated = m_bAuthorized = false;
SendQuery(new td::td_api::getOption("version"));
while (!m_bTerminated) {
ProcessResponse(m_pClientMmanager->receive(10));
}
m_bRunning = false;
}
void CMTProto::LogOut()
{
if (m_bTerminated)
return;
debugLogA("CMTProto::OnLoggedOut");
m_bTerminated = true;
m_bAuthorized = false;
ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, ID_STATUS_OFFLINE);
m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE;
setAllContactStatuses(ID_STATUS_OFFLINE, false);
}
void CMTProto::OnLoggedIn()
{
m_bAuthorized = true;
debugLogA("CMTProto::OnLoggedIn");
ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, m_iDesiredStatus);
m_iStatus = m_iDesiredStatus;
}
///////////////////////////////////////////////////////////////////////////////
INT_PTR CALLBACK CMTProto::EnterPhoneCode(void *param)
{
auto *ppro = (CMTProto *)param;
ENTER_STRING es = {};
es.szModuleName = ppro->m_szModuleName;
es.caption = TranslateT("Enter secret code sent to your phone");
if (EnterString(&es)) {
ppro->SendQuery(new td::td_api::checkAuthenticationCode(_T2A(es.ptszResult).get()), &CMTProto::OnUpdateAuth);
mir_free(es.ptszResult);
}
else ppro->LogOut();
return 0;
}
INT_PTR CALLBACK CMTProto::EnterPassword(void *param)
{
auto *ppro = (CMTProto *)param;
CMStringW wszTitle(TranslateT("Enter password"));
auto *pAuth = (td::td_api::authorizationStateWaitPassword *)ppro->pAuthState.get();
if (!pAuth->password_hint_.empty())
wszTitle.AppendFormat(TranslateT(" (hint: %s)"), Utf2T(pAuth->password_hint_.c_str()).get());
ENTER_STRING es = {};
es.szModuleName = ppro->m_szModuleName;
es.caption = wszTitle;
es.type = ESF_PASSWORD;
if (EnterString(&es)) {
ppro->SendQuery(new td::td_api::checkAuthenticationPassword(_T2A(es.ptszResult).get()), &CMTProto::OnUpdateAuth);
mir_free(es.ptszResult);
}
else ppro->LogOut();
return 0;
}
void CMTProto::ProcessAuth(td::td_api::updateAuthorizationState *pObj)
{
pAuthState = std::move(pObj->authorization_state_);
switch (pAuthState->get_id()) {
case td::td_api::authorizationStateWaitTdlibParameters::ID:
{
char text[100];
Miranda_GetVersionText(text, sizeof(text));
CMStringW wszPath(GetProtoFolder());
auto *request = new td::td_api::setTdlibParameters();
request->database_directory_ = T2Utf(wszPath).get();
request->use_message_database_ = false;
request->use_secret_chats_ = true;
request->api_id_ = 94575;
request->api_hash_ = "a3406de8d171bb422bb6ddf3bbd800e2";
request->system_language_code_ = "en";
request->device_model_ = "Miranda NG";
request->application_version_ = text;
request->enable_storage_optimizer_ = true;
SendQuery(request, &CMTProto::OnUpdateAuth);
}
break;
case td::td_api::authorizationStateWaitPhoneNumber::ID:
SendQuery(new td::td_api::setAuthenticationPhoneNumber(_T2A(m_szOwnPhone).get(), nullptr), &CMTProto::OnUpdateAuth);
break;
case td::td_api::authorizationStateWaitCode::ID:
CallFunctionSync(EnterPhoneCode, this);
break;
case td::td_api::authorizationStateWaitPassword::ID:
CallFunctionSync(EnterPassword, this);
break;
case td::td_api::authorizationStateReady::ID:
OnLoggedIn();
break;
case td::td_api::authorizationStateLoggingOut::ID:
debugLogA("Server required us to log out, exiting");
LogOut();
break;
case td::td_api::authorizationStateClosing::ID:
debugLogA("Connection terminated, exiting");
LogOut();
break;
}
}
void CMTProto::OnUpdateAuth(td::ClientManager::Response &response)
{
if (response.object->get_id() == td::td_api::error::ID) {
auto *pError = (td::td_api::error*)response.object.get();
debugLogA("error happened: %s", to_string(*pError).c_str());
if (pError->message_ == "PHONE_CODE_EXPIRED")
Popup(0, TranslateT("Phone code expired"), TranslateT("Error"));
else if(pError->message_ == "INVALID_PHONE_CODE")
Popup(0, TranslateT("Invalid phone code"), TranslateT("Error"));
pAuthState = std::move(nullptr);
LogOut();
}
}
///////////////////////////////////////////////////////////////////////////////
void CMTProto::ProcessGroups(td::td_api::updateChatFilters *pObj)
{
for (auto &grp : pObj->chat_filters_) {
if (grp->icon_name_ != "Custom")
continue;
CMStringA szSetting("ChatFilter%d", grp->id_);
CMStringW wszOldValue(getMStringW(szSetting));
Utf2T wszNewValue(grp->title_.c_str());
if (wszOldValue.IsEmpty()) {
Clist_GroupCreate(0, wszNewValue);
setWString(szSetting, wszNewValue);
}
else if (wszOldValue != wszNewValue) {
MGROUP oldGroup = Clist_GroupExists(wszNewValue);
if (!oldGroup)
Clist_GroupCreate(0, wszNewValue);
else
Clist_GroupRename(oldGroup, wszNewValue);
setWString(szSetting, wszNewValue);
}
}
}
///////////////////////////////////////////////////////////////////////////////
void CMTProto::ProcessResponse(td::ClientManager::Response response)
{
if (!response.object)
return;
debugLogA("ProcessResponse: id=%d (%s)", int(response.request_id), to_string(response.object).c_str());
if (response.request_id) {
auto *p = m_arRequests.find((TG_REQUEST *)&response.request_id);
if (p) {
(this->*p->pHandler)(response);
m_arRequests.remove(p);
}
return;
}
switch (response.object->get_id()) {
case td::td_api::updateAuthorizationState::ID:
ProcessAuth((td::td_api::updateAuthorizationState *)response.object.get());
break;
case td::td_api::updateChatFilters::ID:
ProcessGroups((td::td_api::updateChatFilters *)response.object.get());
break;
}
}
void CMTProto::SendQuery(td::td_api::Function *pFunc, TG_QUERY_HANDLER pHandler)
{
int queryId = ++m_iQueryId;
m_pClientMmanager->send(m_iClientId, queryId, td::td_api::object_ptr<td::td_api::Function>(pFunc));
if (pHandler)
m_arRequests.insert(new TG_REQUEST(queryId, pHandler));
}
|