This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmthandling.cpp
213 lines (165 loc) · 5.84 KB
/
mthandling.cpp
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
#include "telegramclient.h"
#include "mtschema.h"
#include <QStringList>
#include <QDebug>
#include <QtEndian>
#include <qcompressor.h>
#include "tlschema.h"
qint64 TelegramClient::pingDelayDisconnect(qint64 ping_id, qint32 delay)
{
qDebug() << "[PING] Sending ID:" << ping_id << "with delay:" << delay;
TGOBJECT(pingDD, MTType::PingDelayDisconnectMethod);
pingDD["ping_id"] = ping_id;
pingDD["disconnect_delay"] = delay;
return sendMTObject<&writeMTMethodPingDelayDisconnect>(pingDD);
}
void TelegramClient::handlePong(QByteArray data, qint64 mtm)
{
TelegramPacket packet(data);
QVariant var;
readMTPong(packet, var);
TelegramObject obj = var.toMap();
qDebug() << "[PONG] Got ID:" << obj["ping_id"].toLongLong() << "from message ID:" << obj["msg_id"].toLongLong();
}
void TelegramClient::handleBadServerSalt(QByteArray data, qint64 mtm)
{
TelegramPacket packet(data);
QVariant var;
readMTBadMsgNotification(packet, var);
TelegramObject badServerSalt = var.toMap();
qDebug() << "Got a bad salt notification:" << badServerSalt["error_code"].toInt();
session.salt = badServerSalt["new_server_salt"].toULongLong();
sendMTPacket(messages[badServerSalt["bad_msg_id"].toLongLong()]);
messages.remove(badServerSalt["bad_msg_id"].toLongLong());
messagesConIds.remove(badServerSalt["bad_msg_id"].toLongLong());
emit gotMessageError(mtm, badServerSalt["error_code"].toInt());
}
void TelegramClient::handleRpcResult(QByteArray data, qint64 mtm)
{
TelegramPacket packet(data);
packet.skipRawBytes(4); //conId
QVariant var;
readInt64(packet, var);
data.remove(0, 12);
handleMessage(data, var.toLongLong());
messages.remove(mtm);
messagesConIds.remove(mtm);
}
void TelegramClient::handleGzipPacked(QByteArray data, qint64 mtm)
{
TelegramPacket packet(data);
packet.skipRawBytes(4); //conId
QVariant var;
readByteArray(packet, var);
if (!QCompressor::gzipDecompress(var.toByteArray(), data)) {
qDebug() << "[ERROR] Gzip decompression error.";
return;
}
handleMessage(data, mtm);
}
void TelegramClient::handleMsgContainer(QByteArray data, qint64 mtm)
{
TelegramPacket packet(data);
QVariant var;
readInt32(packet, var); //conId
readInt32(packet, var); //size
qint32 size = var.toInt();
QByteArray array;
for (qint32 i = 0; i < size; ++i) {
readInt64(packet, var); //msgId
readInt32(packet, var); //seqNo
readInt32(packet, var); //size
packet.readRawBytes(array, var.toInt());
handleMessage(array, mtm);
}
}
void TelegramClient::handleBadMsgNotification(QByteArray data, qint64 mtm)
{
TelegramPacket packet(data);
QVariant var;
readMTBadMsgNotification(packet, var);
TelegramObject badMsgNotify = var.toMap();
qDebug() << "Got a bad msg notification:" << badMsgNotify["error_code"].toInt();
emit gotMessageError(mtm, badMsgNotify["error_code"].toInt());
}
void TelegramClient::handleNewSessionCreated(QByteArray data, qint64 mtm)
{
//changeState(AUTHORIZED);
TelegramPacket packet(data);
QVariant var;
readMTNewSession(packet, var);
TelegramObject newSessionCreated = var.toMap();
session.salt = newSessionCreated["server_salt"].toULongLong();
qDebug() << "New session created.";
sync();
}
void TelegramClient::handleRpcError(QByteArray data, qint64 mtm)
{
TelegramPacket packet(data);
QVariant var;
readMTRpcError(packet, var);
TelegramObject rpcError = var.toMap();
qDebug() << "Got RPC error:" << QString::number((quint32) messagesConIds[mtm], 16) << rpcError["error_code"].toInt() << rpcError["error_message"].toString();
QString errorMsg = rpcError["error_message"].toString();
bool handled = true;
if (errorMsg.startsWith("PHONE_MIGRATE_")) {
reconnectToDC(errorMsg.split("PHONE_MIGRATE_").last().toInt());
resendRequired.append(messages[mtm]);
}
else if (errorMsg.startsWith("FILE_MIGRATE_")) {
reconnectToDC(errorMsg.split("FILE_MIGRATE_").last().toInt());
resendRequired.append(messages[mtm]);
}
else if (errorMsg.startsWith("USER_MIGRATE_")) {
reconnectToDC(errorMsg.split("USER_MIGRATE_").last().toInt());
resendRequired.append(messages[mtm]);
}
else if (errorMsg.startsWith("NETWORK_MIGRATE_")) {
reconnectToDC(errorMsg.split("NETWORK_MIGRATE_").last().toInt());
resendRequired.append(messages[mtm]);
}
else if (errorMsg.startsWith("INPUT_METHOD_INVALID_")) {
//TODO
}
else if (errorMsg == "AUTH_KEY_UNREGISTERED" || errorMsg == "SESSION_REVOKED") {
reset();
}
emit gotRPCError(mtm, rpcError["error_code"].toInt(), rpcError["error_message"].toString(), handled);
//TODO handle errors
//PHONE_CODE_INVALID
//SESSION_PASSWORD_NEEDED
//FLOOD_WAIT_
messages.remove(mtm);
messagesConIds.remove(mtm);
}
void TelegramClient::handleConfig(QByteArray data, qint64 mtm)
{
TelegramPacket packet(data);
QVariant var;
readTLConfig(packet, var);
dcConfig = var.toMap();
changeState(INITED);
if (session.importId && !session.importBytes.isEmpty()) {
TGOBJECT(imp, TLType::AuthImportAuthorizationMethod);
imp["id"] = session.importId;
imp["bytes"] = session.importBytes;
sendMTObject<&writeTLMethodAuthImportAuthorization>(imp);
}
else if (isLoggedIn()) {
changeState(LOGGED_IN);
}
qDebug() << "Got a config. Connection inited.";
sync();
}
void TelegramClient::handleMsgCopy(QByteArray data, qint64 mtm)
{
TelegramPacket packet(data);
QVariant var;
readInt32(packet, var); //conId
readInt64(packet, var); //msgId
readInt32(packet, var); //seqNo
readInt32(packet, var); //size
QByteArray array;
packet.readRawBytes(array, var.toInt());
handleMessage(array, mtm);
}