forked from QtGram/LibQTelegram
-
Notifications
You must be signed in to change notification settings - Fork 2
/
telegram.cpp
511 lines (385 loc) · 14 KB
/
telegram.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include "telegram.h"
#include "mtproto/mtprotoupdatehandler.h"
#include "cache/telegramcache.h"
#include "cache/file/filecache.h"
#include "types/telegramhelper.h"
#define StatusUpdateDelay 5000 // 5 seconds
Telegram::Telegram(QObject *parent) : QObject(parent), _initializer(NULL), _timstatus(0), _autodownload(false), _loggedin(false), _online(false)
{
connect(DCSessionManager_instance, &DCSessionManager::mainSessionConnectedChanged, this, &Telegram::connectedChanged);
connect(DCSessionManager_instance, &DCSessionManager::mainSessionTimeout, this, &Telegram::connectionTimeout);
connect(UpdateHandler_instance, &MTProtoUpdateHandler::syncingChanged, this, &Telegram::syncingChanged);
connect(this, &Telegram::loginCompleted, this, &Telegram::loggedInChanged);
}
Telegram::~Telegram()
{
if(!this->connected())
return;
DC_CloseSession(DC_MainSession);
DCSessionManager_instance->deleteLater();
TelegramCache_instance->deleteLater();
}
TelegramInitializer *Telegram::initializer() const
{
return this->_initializer;
}
User *Telegram::me() const
{
return TelegramConfig_me;
}
int Telegram::apiLayer() const
{
return TELEGRAM_API_LAYER;
}
bool Telegram::loggedIn() const
{
return this->_loggedin;
}
bool Telegram::connected() const
{
if(!DC_MainSession)
return false;
DC* dc = SessionToDC(DC_MainSession);
return dc->state() == DC::ConnectedState;
}
bool Telegram::syncing() const
{
return UpdateHandler_syncing;
}
bool Telegram::autoDownload() const
{
return this->_autodownload;
}
bool Telegram::online() const
{
return this->_online;
}
int Telegram::unreadCount() const
{
if(!this->_loggedin)
return 0;
return TelegramCache_unreadCount;
}
void Telegram::setInitializer(TelegramInitializer *initializer)
{
if(this->_initializer == initializer)
return;
if(this->_initializer)
{
disconnect(this->_initializer, &TelegramInitializer::floodLock, this, 0);
disconnect(this->_initializer, &TelegramInitializer::phoneCodeError, this, 0);
disconnect(this->_initializer, &TelegramInitializer::signUpRequested, this, 0);
disconnect(this->_initializer, &TelegramInitializer::signInRequested, this, 0);
disconnect(this->_initializer, &TelegramInitializer::invalidPassword, this, 0);
disconnect(this->_initializer, &TelegramInitializer::sessionPasswordNeeded, this, 0);
disconnect(this->_initializer, &TelegramInitializer::loginCompleted, this, 0);
}
this->_initializer = initializer;
connect(this->_initializer, &TelegramInitializer::floodLock, this, &Telegram::floodLock);
connect(this->_initializer, &TelegramInitializer::phoneCodeError, this, &Telegram::phoneCodeError);
connect(this->_initializer, &TelegramInitializer::signUpRequested, this, &Telegram::signUpRequested);
connect(this->_initializer, &TelegramInitializer::signInRequested, this, &Telegram::signInRequested);
connect(this->_initializer, &TelegramInitializer::invalidPassword, this, &Telegram::invalidPassword);
connect(this->_initializer, &TelegramInitializer::sessionPasswordNeeded, this, &Telegram::sessionPasswordNeeded);
connect(this->_initializer, &TelegramInitializer::loginCompleted, this, &Telegram::onLoginCompleted);
emit initializerChanged();
}
void Telegram::setAutoDownload(bool b)
{
if(this->_autodownload == b)
return;
this->_autodownload = b;
if(TelegramConfig_instance)
TelegramConfig_setAutoDownload(b);
emit autoDownloadChanged();
}
void Telegram::setOnline(bool b)
{
if(!this->connected() || !this->loggedIn() || (this->_online == b))
return;
this->_online = b;
if(!this->_timstatus)
this->_timstatus = this->startTimer(StatusUpdateDelay);
emit onlineChanged();
}
void Telegram::sortDialogs(QList<Dialog *> &dialogs) const
{
std::sort(dialogs.begin(), dialogs.end(), [](Dialog* dlg1, Dialog* dlg2) {
if(!dlg1->topMessage())
return false;
if(!dlg2->topMessage())
return true;
Message* msg1 = TelegramCache_message(dlg1->topMessage(), dlg1);
Message* msg2 = TelegramCache_message(dlg2->topMessage(), dlg2);
if(!msg1)
return false;
if(!msg2)
return true;
return msg1->date() > msg2->date();
});
}
bool Telegram::muteDialog(Dialog *dialog, bool mute)
{
PeerNotifySettings* notifysettings = dialog->notifySettings();
if(TelegramHelper::isMuted(dialog) == mute)
return false;
notifysettings->setIsSilent(mute);
notifysettings->setMuteUntil(mute ? Future10Years : 0);
TelegramCache_store(dialog);
InputNotifyPeer* inputnotifypeer = TelegramHelper::inputNotifyPeer(dialog, TelegramCache_accessHash(dialog));
InputPeerNotifySettings* inputpeernotifysettings = TelegramHelper::inputPeerNotifySettings(notifysettings);
TelegramAPI::accountUpdateNotifySettings(DC_MainSession, inputnotifypeer, inputpeernotifysettings);
inputpeernotifysettings->deleteLater();
inputnotifypeer->deleteLater();
return true;
}
bool Telegram::dialogIsWritable(Dialog *dialog) const
{
TLInt dialogid = TelegramHelper::identifier(dialog);
if(TelegramHelper::isChat(dialog) || TelegramHelper::isChannel(dialog))
{
Chat* chat = TelegramCache_chat(dialogid);
if(!chat)
return false;
if(chat->isBroadcast())
return dialogid == TelegramConfig_me->id();
return (chat->constructorId() == TLTypes::Chat) || (chat->constructorId() == TLTypes::Channel);
}
return true;
}
QString Telegram::messageMediaText(MessageMedia *messagemedia) const
{
TLConstructor ctorid = messagemedia->constructorId();
if(ctorid == TLTypes::MessageMediaWebPage)
return messagemedia->caption();
if(ctorid == TLTypes::MessageMediaDocument)
{
DocumentAttribute* attribute = NULL;
Document* document = messagemedia->document();
if(TelegramHelper::documentHas(document, TLTypes::DocumentAttributeAnimated))
return tr("GIF");
if((attribute = TelegramHelper::documentHas(document, TLTypes::DocumentAttributeSticker)))
return tr("%1 Sticker").arg(attribute->alt());
if(TelegramHelper::documentHas(document, TLTypes::DocumentAttributeVideo))
return tr("Video file");
if(TelegramHelper::documentHas(document, TLTypes::DocumentAttributeAudio))
return tr("Audio recording");
if(TelegramHelper::documentHas(document, TLTypes::DocumentAttributeHasStickers))
return tr("Sticker Set");
if((attribute = TelegramHelper::documentHas(document, TLTypes::DocumentAttributeFilename)))
return attribute->fileName();
return tr("Document");
}
QString result;
if(ctorid == TLTypes::MessageMediaContact)
result = tr("Contact");
else if(ctorid == TLTypes::MessageMediaGame)
result = tr("Game");
else if((ctorid == TLTypes::MessageMediaGeo) || (ctorid == TLTypes::MessageMediaVenue))
result = tr("Location");
else if(ctorid == TLTypes::MessageMediaPhoto)
result = tr("Photo");
if(!messagemedia->caption().isEmpty())
result += ", " + messagemedia->caption();
return result;
}
QString Telegram::messageActionText(Message* message) const
{
User *fromuser = NULL, *inviteruser = NULL, *user = NULL;
QString fromfullname, fullname, inviterfullname;
MessageAction* messageaction = message->action();
TLConstructor ctorid = messageaction->constructorId();
if(message->fromId())
{
fromuser = TelegramCache_user(message->fromId());
if(fromuser)
fromfullname = TelegramHelper::fullName(fromuser);
}
if(messageaction->userId())
{
user = TelegramCache_user(messageaction->userId());
if(user)
fullname = TelegramHelper::fullName(user);
}
if(messageaction->inviterId())
{
inviteruser = TelegramCache_user(messageaction->inviterId());
if(inviteruser)
inviterfullname = TelegramHelper::fullName(inviteruser);
}
if(ctorid == TLTypes::MessageActionChatCreate)
{
Chat* chat = TelegramCache_chat(TelegramHelper::identifier(message->toId()));
if(messageaction->channelId())
return tr("%1 created channel «%2»").arg(fromfullname, (chat ? chat->title() : QString()));
return tr("%1 created group «%2»").arg(fromfullname, (chat ? chat->title() : QString()));
}
if(ctorid == TLTypes::MessageActionChatEditTitle)
{
if(message->isPost())
return tr("Channel name changed to «%1»").arg(messageaction->title());
return tr("«%1» changed group name to «%2»").arg(fromfullname, messageaction->title());
}
if(ctorid == TLTypes::MessageActionChatEditPhoto)
{
if(message->isPost())
return tr("Channel photo updated");
return tr("«%1» updated group photo").arg(fromfullname);
}
if(ctorid == TLTypes::MessageActionChatDeletePhoto)
{
if(message->isPost())
return tr("Channel photo deleted");
return tr("«%1» deleted group photo").arg(fromfullname);
}
if(ctorid == TLTypes::MessageActionChatAddUser)
{
if(messageaction->users().length() > 0)
return tr("«%1» added «%2» ").arg(fromfullname, this->userList(messageaction->users()));
return tr("«%1» has joined the group").arg(fullname);
}
if(ctorid == TLTypes::MessageActionChatDeleteUser)
{
if(messageaction->userId() && (message->fromId() != messageaction->userId()))
return tr("«%1» removed «%2»").arg(fromfullname, fullname);
return tr("«%1» has left the group").arg(fullname);
}
if(ctorid == TLTypes::MessageActionChatJoinedByLink)
return tr("«%1» has joined the group via invite link").arg(fromfullname);
if(ctorid == TLTypes::MessageActionChannelCreate)
return tr("Channel «%1» created").arg(messageaction->title());
if(ctorid == TLTypes::MessageActionChatMigrateTo)
{
if(messageaction->channelId())
return tr("This group was upgraded to a supergroup");
return tr("Unhandled chat migration type");
}
if(ctorid == TLTypes::MessageActionGameScore) // NOTE: MessageActionGameScore, unhandled and untested
return tr("Unhandled MessageActionGameScore");
return QString("Unhandled action: %1").arg(ctorid, 0, 16);
}
QString Telegram::dialogTitle(Dialog *dialog) const
{
if(!dialog)
return QString();
TLInt peerid = TelegramHelper::identifier(dialog->peer());
if(TelegramHelper::isChat(dialog) || TelegramHelper::isChannel(dialog))
{
Chat* chat = TelegramCache_chat(peerid);
return chat->title();
}
User* user = TelegramCache_user(peerid);
return TelegramHelper::fullName(user);
}
QString Telegram::dialogStatusText(Dialog *dialog) const
{
if(!dialog)
return QString();
TLInt dialogid = TelegramHelper::identifier(dialog);
if(TelegramHelper::isChannel(dialog))
{
ChatFull* chatfull = TelegramCache_chatFull(dialogid);
return tr("%1 members").arg(chatfull ? chatfull->participantsCount() : 0);
}
if(TelegramHelper::isChat(dialog))
{
Chat* chat = TelegramCache_chat(dialogid);
return tr("%1 members").arg(chat ? chat->participantsCount() : 0);
}
User* user = TelegramCache_user(dialogid);
if(user)
return TelegramHelper::statusText(user);
return QString();
}
TelegramObject *Telegram::messageFrom(Message *message) const
{
if(!message)
return NULL;
if(message->isPost()) // Post = messages from channels
{
Chat* chat = TelegramCache_chat(TelegramHelper::identifier(message->toId()));
if(chat)
return chat;
}
else
{
User* user = TelegramCache_user(message->fromId());
if(user)
return user;
}
return NULL;
}
QString Telegram::messageText(Message *message) const
{
if(!message)
return QString();
if(message->action() && (message->action()->constructorId() != TLTypes::MessageActionEmpty))
return this->messageActionText(message);
return message->message();
}
QString Telegram::messagePreview(Message *message) const
{
if(!message)
return QString();
if(message->action())
return this->messageActionText(message);
if(message->media())
{
QString mediatext = this->messageMediaText(message->media());
if(!mediatext.isEmpty())
return mediatext;
}
return message->message();
}
void Telegram::signIn(const QString &phonecode) const
{
this->_initializer->signIn(phonecode);
}
void Telegram::signUp(const QString &firstname, const QString &lastname, const QString &phonecode) const
{
this->_initializer->signUp(firstname, lastname, phonecode);
}
void Telegram::sendPassword(const QString &password) const
{
this->_initializer->sendPassword(password);
}
void Telegram::reconnect() const
{
DCSessionManager_instance->restoreSessions();
}
void Telegram::resendCode() const
{
this->_initializer->resendCode();
}
void Telegram::onLoginCompleted()
{
this->_loggedin = true;
TelegramConfig_setAutoDownload(this->_autodownload);
connect(TelegramCache_instance, &TelegramCache::unreadCountChanged, this, &Telegram::unreadCountChanged);
this->setOnline(true);
emit loginCompleted();
emit unreadCountChanged();
}
void Telegram::timerEvent(QTimerEvent *event)
{
if(event->timerId() == this->_timstatus)
{
TelegramAPI::accountUpdateStatus(DC_MainSession, !this->_online);
this->killTimer(event->timerId());
this->_timstatus = 0;
}
}
QString Telegram::userList(const TLVector<TLInt> users) const
{
QString s;
foreach(TLInt userid, users)
{
User* user = TelegramCache_user(userid);
if(!user)
continue;
if(!s.isEmpty())
s += ", ";
s += TelegramHelper::fullName(user);
}
return s;
}