-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTdcurses.cpp
2550 lines (2256 loc) · 111 KB
/
Tdcurses.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "td/actor/impl/ActorId.h"
#include "td/actor/impl/Scheduler-decl.h"
#include "td/actor/impl/Scheduler.h"
#include "td/tdactor/td/actor/actor.h"
#include "td/tdutils/td/utils/int_types.h"
#include "td/telegram/StickerType.h"
#include "td/tl/TlObject.h"
#include "td/generate/auto/td/telegram/td_api.h"
#include "td/generate/auto/td/telegram/td_api.hpp"
#include "td/telegram/ClientActor.h"
#include "td/tdactor/td/actor/PromiseFuture.h"
#include "td/tdactor/td/actor/ConcurrentScheduler.h"
#include "td/tdutils/td/utils/type_traits.h"
#include "td/tdutils/td/utils/buffer.h"
#include "td/tdutils/td/utils/Variant.h"
#include "td/utils/Promise.h"
#include "td/utils/Slice-decl.h"
#include "td/utils/Status.h"
#include "td/utils/Time.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/overloaded.h"
#include "td/utils/port/detail/NativeFd.h"
#include "td/utils/port/detail/PollableFd.h"
#include "td/utils/port/signals.h"
#include "td/utils/OptionParser.h"
#include "td/utils/filesystem.h"
#include "td/utils/port/path.h"
#include "td/utils/FileLog.h"
#include "td/telegram/Td.h"
#include "td/utils/port/StdStreams.h"
#include "windows/EditorWindow.hpp"
#include "windows/Markup.hpp"
#include "windows/unicode.h"
//#include "telegram-cli-output.h"
#include "StickerManager.hpp"
#include "Tdcurses.hpp"
#include "Outputter.hpp"
#include "TdObjectsOutput.h"
#include "ChatSearchWindow.hpp"
#include "ChatWindow.hpp"
#include "CommandLineWindow.hpp"
#include "ComposeWindow.hpp"
#include "ConfigWindow.hpp"
#include "DialogListWindow.hpp"
#include "FileManager.hpp"
#include "ChatInfoWindow.hpp"
#include "StatusLineWindow.hpp"
#include "TdcursesLayout.hpp"
#include "windows/LogWindow.hpp"
#include "GlobalParameters.hpp"
#include "FileSelectionWindow.hpp"
#include "YesNoWindow.hpp"
#include "NotificationManager.hpp"
#include "qrcodegen/qrcodegen.hpp"
#include "td/utils/port/Stat.h"
#include <atomic>
#include <cstdio>
#include <cmath>
#include <limits>
#include <memory>
#include <ostream>
#include <string>
#include <utility>
#include <iostream>
#include <vector>
#include <libconfig.h++>
#include <signal.h>
#include <openssl/opensslv.h>
#if HAVE_PWD_H
#include <pwd.h>
#endif
#if HAVE_EXECINFO_H
#include <execinfo.h>
#endif
td::int32 total_updates;
bool db_closed{false};
bool received_stop_signal{false};
bool forwarded_stop_signal{false};
namespace tdcurses {
std::string detect_home_dir() {
auto env = ::getenv("HOME");
if (env && *env) {
return env;
}
#if HAVE_PWD_H
uid_t uid = getuid();
struct passwd *pw = getpwuid(uid);
if (pw == NULL) {
LOG(WARNING) << "cannot find HOME dir";
exit(EXIT_FAILURE);
}
return pw->pw_dir;
#else
LOG(WARNING) << "cannot find HOME dir";
exit(EXIT_FAILURE);
#endif
}
std::string detect_config_name() {
auto env = ::getenv("XDG_CONFIG_HOME");
if (env && *env) {
std::string e = env;
if (e[e.size() - 1] != '/') {
e = e + "/";
}
td::mkdir(e + "telegram-curses").ensure();
return e + "telegram-curses/config";
} else {
LOG(WARNING) << "XDG_CONFIG_HOME not defined";
std::string e = detect_home_dir() + "/.config/";
td::mkdir(e + "telegram-curses").ensure();
return e + "telegram-curses/config";
}
}
std::string detect_db_root_name() {
auto env = ::getenv("XDG_DATA_HOME");
if (env && *env) {
std::string e = env;
if (e[e.size() - 1] != '/') {
e = e + "/";
}
td::mkdir(e + "telegram-curses").ensure();
return e + "telegram-curses/";
} else {
LOG(WARNING) << "XDG_DATA_HOME not defined";
std::string e = detect_home_dir() + "/.local/share/";
td::mkdir(e + "telegram-curses").ensure();
return e + "telegram-curses/";
}
}
class TdcursesImpl : public Tdcurses {
private:
td::tl_object_ptr<td::td_api::setTdlibParameters> tdlib_parameters_;
td::ActorId<TdcursesImpl> self_;
std::string root_directory_ = ".tdcurses";
bool starting_ = false;
td::ActorOwn<td::ClientActor> td_;
td::unique_ptr<td::TdCallback> make_td_callback() {
class TdCallbackImpl : public td::TdCallback {
public:
explicit TdCallbackImpl(td::ActorId<TdcursesImpl> client) : client_(client) {
}
void on_result(td::uint64 id, td::tl_object_ptr<td::td_api::Object> result) override {
if (id == 0) {
td::send_closure(client_, &TdcursesImpl::on_update, td::move_tl_object_as<td::td_api::Update>(result));
} else {
td::send_closure(client_, &TdcursesImpl::on_result, id, std::move(result));
}
}
void on_error(td::uint64 id, td::tl_object_ptr<td::td_api::error> error) override {
LOG(ERROR) << "received error: " << error->code_ << " " << error->message_;
td::send_closure(client_, &TdcursesImpl::on_error, id, std::move(error));
}
private:
td::ActorId<TdcursesImpl> client_;
};
return td::make_unique<TdCallbackImpl>(self_);
}
public:
TdcursesImpl() {
}
void get_os_type() {
}
auto clone_tdlib_parameters() const {
//setTdlibParameters use_test_dc:Bool database_directory:string files_directory:string database_encryption_key:bytes use_file_database:Bool use_chat_info_database:Bool use_message_database:Bool use_secret_chats:Bool api_id:int32 api_hash:string system_language_code:string device_model:string system_version:string application_version:string = Ok;
return td::make_tl_object<td::td_api::setTdlibParameters>(
tdlib_parameters_->use_test_dc_, tdlib_parameters_->database_directory_, tdlib_parameters_->files_directory_,
tdlib_parameters_->database_encryption_key_, tdlib_parameters_->use_file_database_,
tdlib_parameters_->use_chat_info_database_, tdlib_parameters_->use_message_database_,
tdlib_parameters_->use_secret_chats_, tdlib_parameters_->api_id_, tdlib_parameters_->api_hash_,
tdlib_parameters_->system_language_code_, tdlib_parameters_->device_model_, tdlib_parameters_->system_version_,
tdlib_parameters_->application_version_);
}
void change_system_version(std::string version) {
CHECK(!starting_);
tdlib_parameters_->system_version_ = version;
}
void start(td::tl_object_ptr<td::td_api::setTdlibParameters> tdlib_parameters,
std::unique_ptr<TdcursesParameters> tdcurses_params) {
self_ = actor_id(this);
tdlib_parameters_ = std::move(tdlib_parameters);
starting_ = true;
start_curses(*tdcurses_params);
td_ = td::create_actor<td::ClientActor>("ClientActor", make_td_callback());
send_request(td::make_tl_object<td::td_api::setOption>("notification_group_count_max",
td::make_tl_object<td::td_api::optionValueInteger>(25)),
[](td::Result<td::tl_object_ptr<td::td_api::ok>> R) { R.ensure(); });
send_request(td::make_tl_object<td::td_api::setOption>("notification_group_size_max",
td::make_tl_object<td::td_api::optionValueInteger>(25)),
[](td::Result<td::tl_object_ptr<td::td_api::ok>> R) { R.ensure(); });
}
void notify() override {
// NB: Interface will be changed
td::send_closure_later(self_, &TdcursesImpl::on_net);
}
void on_net() {
loop();
}
void on_result(td::uint64 id, td::tl_object_ptr<td::td_api::Object> result) {
auto it = handlers_.find(id);
it->second.set_result(std::move(result));
handlers_.erase(it);
refresh();
}
void on_error(td::uint64 id, td::tl_object_ptr<td::td_api::error> result) {
auto it = handlers_.find(id);
it->second.set_error(td::Status::Error(result->code_, result->message_));
handlers_.erase(it);
refresh();
}
void on_update(td::tl_object_ptr<td::td_api::Update> update) {
total_updates++;
td::td_api::downcast_call(*update.get(), [Self = this](auto &obj) { Self->process_update(obj); });
refresh();
}
void process_auth_state(td::td_api::authorizationStateWaitTdlibParameters &state) {
send_request(clone_tdlib_parameters(),
td::PromiseCreator::lambda([self = self_](td::Result<td::tl_object_ptr<td::td_api::ok>> R) {
if (R.is_error()) {
CHECK(R.error().code() == 401);
td::send_closure(self, &TdcursesImpl::request_database_password);
}
}));
}
void received_database_password(td::Result<td::BufferSlice> res) {
if (res.is_error()) {
request_database_password();
} else {
auto params = clone_tdlib_parameters();
auto key = res.move_as_ok();
params->database_encryption_key_ = std::string(key.data(), key.size());
send_request(std::move(params),
td::PromiseCreator::lambda([SelfId = self_](td::Result<td::tl_object_ptr<td::td_api::ok>> R) {
if (R.is_error()) {
td::send_closure(SelfId, &TdcursesImpl::request_database_password);
}
}));
}
}
void request_database_password() {
class Callback : public windows::OneLineInputWindow::Callback {
public:
Callback(TdcursesImpl *ptr) : ptr_(ptr) {
}
void on_answer(windows::OneLineInputWindow *self, std::string text) override {
ptr_->received_database_password(td::BufferSlice(text));
ptr_->del_popup_window(window_);
}
void on_abort(windows::OneLineInputWindow *self, std::string text) override {
ptr_->received_database_password(td::Status::Error("cancelled"));
ptr_->del_popup_window(window_);
}
void set_window(windows::Window *window) {
window_ = window;
}
private:
TdcursesImpl *ptr_;
windows::Window *window_;
};
auto cb = std::make_unique<Callback>(this);
auto w = std::make_shared<windows::OneLineInputWindow>("db password: ", true, nullptr);
auto box = std::make_shared<windows::BorderedWindow>(w, windows::BorderType::Double);
cb->set_window(box.get());
w->set_callback(std::move(cb));
add_popup_window(box, 0);
}
//@description TDLib needs the user's email address to authorize. Call setAuthenticationEmailAddress to provide the email address, or directly call checkAuthenticationEmailCode with Apple ID/Google ID token if allowed
//@allow_apple_id True, if authorization through Apple ID is allowed
//@allow_google_id True, if authorization through Google ID is allowed
void process_auth_state(td::td_api::authorizationStateWaitEmailAddress &state) {
UNREACHABLE();
}
//@description TDLib needs the user's authentication code sent to an email address to authorize. Call checkAuthenticationEmailCode to provide the code
//@allow_apple_id True, if authorization through Apple ID is allowed
//@allow_google_id True, if authorization through Google ID is allowed
//@code_info Information about the sent authentication code
//@next_phone_number_authorization_date Point in time (Unix timestamp) when the user will be able to authorize with a code sent to the user's phone number; 0 if unknown
//authorizationStateWaitEmailCode allow_apple_id:Bool allow_google_id:Bool code_info:emailAddressAuthenticationCodeInfo next_phone_number_authorization_date:int32 = AuthorizationState;
void process_auth_state(td::td_api::authorizationStateWaitEmailCode &state) {
UNREACHABLE();
}
void received_phone_number(td::Result<td::BufferSlice> res) {
if (res.is_error()) {
request_phone_number();
} else {
//phoneNumberAuthenticationSettings allow_flash_call:Bool allow_missed_call:Bool is_current_phone_number:Bool has_unknown_phone_number:Bool allow_sms_retriever_api:Bool firebase_authentication_settings:FirebaseAuthenticationSettings authentication_tokens:vector<string> = PhoneNumberAuthenticationSettings;
send_request(td::make_tl_object<td::td_api::setAuthenticationPhoneNumber>(
res.move_as_ok().as_slice().str(),
td::make_tl_object<td::td_api::phoneNumberAuthenticationSettings>(
/* allow flash call */ true, /* allow missed call */ true,
/* is current phone number */ false, /* has unknown phone number */ false,
/* allow sms retriever */ false, /* firebase */ nullptr,
/* tokens */ std::vector<std::string>())),
td::PromiseCreator::lambda([SelfId = self_](td::Result<td::tl_object_ptr<td::td_api::ok>> R) {
if (R.is_error()) {
td::send_closure(SelfId, &TdcursesImpl::request_phone_number);
}
}));
}
}
void request_phone_number() {
class Callback : public windows::OneLineInputWindow::Callback {
public:
Callback(TdcursesImpl *ptr) : ptr_(ptr) {
}
void on_answer(windows::OneLineInputWindow *self, std::string text) override {
ptr_->received_phone_number(td::BufferSlice(text));
ptr_->del_popup_window(window_);
}
void on_abort(windows::OneLineInputWindow *self, std::string text) override {
ptr_->received_phone_number(td::Status::Error("cancelled"));
ptr_->del_popup_window(window_);
}
void set_window(windows::Window *window) {
window_ = window;
}
private:
TdcursesImpl *ptr_;
windows::Window *window_;
};
auto cb = std::make_unique<Callback>(this);
auto w = std::make_shared<windows::OneLineInputWindow>("phone number: ", false, nullptr);
auto box = std::make_shared<windows::BorderedWindow>(w, windows::BorderType::Double);
cb->set_window(box.get());
w->set_callback(std::move(cb));
add_popup_window(box, 0);
}
void request_qr_code() {
send_request(td::make_tl_object<td::td_api::requestQrCodeAuthentication>(std::vector<td::int64>()),
td::PromiseCreator::lambda([SelfId = self_](td::Result<td::tl_object_ptr<td::td_api::ok>> R) {
if (R.is_error()) {
td::send_closure(SelfId, &TdcursesImpl::request_phone_number);
}
}));
}
void process_auth_state(td::td_api::authorizationStateWaitPhoneNumber &state) {
request_qr_code();
//request_phone_number();
}
void received_code(td::Result<td::BufferSlice> res) {
if (res.is_error()) {
request_code();
} else {
send_request(td::make_tl_object<td::td_api::checkAuthenticationCode>(res.move_as_ok().as_slice().str()),
td::PromiseCreator::lambda([SelfId = self_](td::Result<td::tl_object_ptr<td::td_api::ok>> R) {
if (R.is_error()) {
td::send_closure(SelfId, &TdcursesImpl::request_code);
}
}));
}
}
void request_code() {
class Callback : public windows::OneLineInputWindow::Callback {
public:
Callback(TdcursesImpl *ptr) : ptr_(ptr) {
}
void on_answer(windows::OneLineInputWindow *self, std::string text) override {
ptr_->received_code(td::BufferSlice(text));
ptr_->del_popup_window(window_);
}
void on_abort(windows::OneLineInputWindow *self, std::string text) override {
ptr_->received_code(td::Status::Error("cancelled"));
ptr_->del_popup_window(window_);
}
void set_window(windows::Window *window) {
window_ = window;
}
private:
TdcursesImpl *ptr_;
windows::Window *window_;
};
auto cb = std::make_unique<Callback>(this);
auto w = std::make_shared<windows::OneLineInputWindow>("auth code: ", false, nullptr);
auto box = std::make_shared<windows::BorderedWindow>(w, windows::BorderType::Double);
cb->set_window(box.get());
w->set_callback(std::move(cb));
add_popup_window(box, 0);
}
//authorizationStateWaitCode is_registered:Bool terms_of_service:termsOfService code_info:authenticationCodeInfo = AuthorizationState;
void process_auth_state(td::td_api::authorizationStateWaitCode &state) {
request_code();
}
void process_auth_state(td::td_api::authorizationStateWaitOtherDeviceConfirmation &state) {
auto code = qrcodegen::QrCode::encodeText(state.link_.c_str(), qrcodegen::QrCode::Ecc::MEDIUM);
auto size = code.getSize();
if ((size + 2) * 2 > width() - 2 || size + 2 > height() - 2) {
return request_phone_number();
}
Outputter out;
{
for (td::int32 i = -1; i <= size; i++) {
for (td::int32 j = -1; j <= size; j++) {
auto col = code.getModule(i, j);
if (col) {
out << Outputter::FgColor{windows::Color::Black};
} else {
out << Outputter::FgColor{windows::Color::White};
}
out << "\xe2\x96\x88"
<< "\xe2\x96\x88";
out << Outputter::FgColor{windows::Color::Revert};
}
out << "\n";
}
out << "\n";
}
class QrWindow : public windows::ViewWindow {
public:
QrWindow(std::string text, std::vector<windows::MarkupElement> markup, td::int32 size)
: windows::ViewWindow(std::move(text), std::move(markup), nullptr), size_(size) {};
td::int32 min_width() override {
return 2 * size_ + 4;
}
td::int32 min_height() override {
return size_ + 2;
}
td::int32 best_width() override {
return 2 * size_ + 4;
}
td::int32 best_height() override {
return size_ + 2;
}
private:
td::int32 size_;
//UNREACHABLE();
};
auto w = std::make_shared<QrWindow>(out.as_str(), out.markup(), size);
auto box = std::make_shared<windows::BorderedWindow>(w, windows::BorderType::Double);
add_qr_code_window(box);
}
void received_name(td::Result<td::BufferSlice> res) {
if (res.is_error()) {
request_name();
} else {
auto str = res.move_as_ok().as_slice().str();
auto ch = str.find(' ');
std::string first_name = str;
std::string last_name = "";
if (ch != std::string::npos) {
first_name = str.substr(0, ch);
last_name = str.substr(ch + 1);
}
send_request(td::make_tl_object<td::td_api::registerUser>(first_name, last_name, false),
td::PromiseCreator::lambda([SelfId = self_](td::Result<td::tl_object_ptr<td::td_api::ok>> R) {
if (R.is_error()) {
td::send_closure(SelfId, &TdcursesImpl::request_name);
}
}));
}
}
void request_name() {
class Callback : public windows::OneLineInputWindow::Callback {
public:
Callback(TdcursesImpl *ptr) : ptr_(ptr) {
}
void on_answer(windows::OneLineInputWindow *self, std::string text) override {
ptr_->received_name(td::BufferSlice(text));
ptr_->del_popup_window(window_);
}
void on_abort(windows::OneLineInputWindow *self, std::string text) override {
ptr_->received_name(td::Status::Error("cancelled"));
ptr_->del_popup_window(window_);
}
void set_window(windows::Window *window) {
window_ = window;
}
private:
TdcursesImpl *ptr_;
windows::Window *window_;
};
auto cb = std::make_unique<Callback>(this);
auto w = std::make_shared<windows::OneLineInputWindow>("your name: ", false, nullptr);
auto box = std::make_shared<windows::BorderedWindow>(w, windows::BorderType::Double);
cb->set_window(box.get());
w->set_callback(std::move(cb));
add_popup_window(box, 0);
}
void process_auth_state(td::td_api::authorizationStateWaitRegistration &state) {
request_name();
}
void received_password(td::Result<td::BufferSlice> R) {
if (R.is_error()) {
request_password();
return;
}
send_request(td::make_tl_object<td::td_api::checkAuthenticationPassword>(R.move_as_ok().as_slice().str()),
td::PromiseCreator::lambda([SelfId = self_](td::Result<td::tl_object_ptr<td::td_api::ok>> R) {
if (R.is_error()) {
td::send_closure(SelfId, &TdcursesImpl::request_password);
}
}));
}
void request_password() {
class Callback : public windows::OneLineInputWindow::Callback {
public:
Callback(TdcursesImpl *ptr) : ptr_(ptr) {
}
void on_answer(windows::OneLineInputWindow *self, std::string text) override {
ptr_->received_password(td::BufferSlice(text));
ptr_->del_popup_window(window_);
}
void on_abort(windows::OneLineInputWindow *self, std::string text) override {
ptr_->received_password(td::Status::Error("cancelled"));
ptr_->del_popup_window(window_);
}
void set_window(windows::Window *window) {
window_ = window;
}
private:
TdcursesImpl *ptr_;
windows::Window *window_;
};
auto cb = std::make_unique<Callback>(this);
auto w = std::make_shared<windows::OneLineInputWindow>("password: ", true, nullptr);
auto box = std::make_shared<windows::BorderedWindow>(w, windows::BorderType::Double);
cb->set_window(box.get());
w->set_callback(std::move(cb));
add_popup_window(box, 0);
}
//authorizationStateWaitPassword password_hint:string has_recovery_email_address:Bool recovery_email_address_pattern:string = AuthorizationState;
void process_auth_state(td::td_api::authorizationStateWaitPassword &state) {
request_password();
}
void process_auth_state(td::td_api::authorizationStateReady &state) {
LOG(INFO) << "ready";
// do nothing
dialog_list_window()->request_bottom_elements();
}
void process_auth_state(td::td_api::authorizationStateLoggingOut &state) {
}
void process_auth_state(td::td_api::authorizationStateClosing &state) {
}
void process_auth_state(td::td_api::authorizationStateClosed &state) {
screen()->stop();
_Exit(0);
db_closed = true;
}
//@description The user authorization state has changed
//@authorization_state New authorization state
//updateAuthorizationState authorization_state:AuthorizationState = Update;
void process_update(td::td_api::updateAuthorizationState &update) {
del_qr_code_window();
td::td_api::downcast_call(*update.authorization_state_, [&](auto &obj) { process_auth_state(obj); });
}
//@description A new message was received; can also be an outgoing message
//@message The new message
//updateNewMessage message:message = Update;
void process_update(td::td_api::updateNewMessage &update) {
auto c = chat_window();
if (c && c->main_chat_id() == update.message_->chat_id_) {
c->process_update(update);
}
}
//@description A request to send a message has reached the Telegram server. This doesn't mean that the message will be sent successfully.
//-This update is sent only if the option "use_quick_ack" is set to true. This update may be sent multiple times for the same message
//@chat_id The chat identifier of the sent message
//@message_id A temporary message identifier
//updateMessageSendAcknowledged chat_id:int53 message_id:int53 = Update;
void process_update(td::td_api::updateMessageSendAcknowledged &update) {
auto c = chat_window();
if (c && c->main_chat_id() == update.chat_id_) {
c->process_update(update);
}
}
//@description A message has been successfully sent
//@message The sent message. Usually only the message identifier, date, and content are changed, but almost all other fields can also change
//@old_message_id The previous temporary message identifier
//updateMessageSendSucceeded message:message old_message_id:int53 = Update;
void process_update(td::td_api::updateMessageSendSucceeded &update) {
auto c = chat_window();
if (c && c->main_chat_id() == update.message_->chat_id_) {
c->process_update(update);
}
}
//@description A message failed to send. Be aware that some messages being sent can be irrecoverably deleted, in which case updateDeleteMessages will be received instead of this update
//@message The failed to send message
//@old_message_id The previous temporary message identifier
//@error The cause of the message sending failure
//updateMessageSendFailed message:message old_message_id:int53 error:error = Update;
void process_update(td::td_api::updateMessageSendFailed &update) {
auto c = chat_window();
if (c && c->main_chat_id() == update.message_->chat_id_) {
c->process_update(update);
}
}
//@description The message content has changed
//@chat_id Chat identifier
//@message_id Message identifier
//@new_content New message content
//updateMessageContent chat_id:int53 message_id:int53 new_content:MessageContent = Update;
void process_update(td::td_api::updateMessageContent &update) {
auto c = chat_window();
if (c && c->main_chat_id() == update.chat_id_) {
c->process_update(update);
}
}
//@description A message was edited. Changes in the message content will come in a separate updateMessageContent
//@chat_id Chat identifier
//@message_id Message identifier
//@edit_date Point in time (Unix timestamp) when the message was edited
//@reply_markup New message reply markup; may be null
//updateMessageEdited chat_id:int53 message_id:int53 edit_date:int32 reply_markup:ReplyMarkup = Update;
void process_update(td::td_api::updateMessageEdited &update) {
auto c = chat_window();
if (c && c->main_chat_id() == update.chat_id_) {
c->process_update(update);
}
}
//@description The message pinned state was changed
//@chat_id Chat identifier @message_id The message identifier
//@is_pinned True, if the message is pinned
//updateMessageIsPinned chat_id:int53 message_id:int53 is_pinned:Bool = Update;
void process_update(td::td_api::updateMessageIsPinned &update) {
auto c = chat_window();
if (c && c->main_chat_id() == update.chat_id_) {
c->process_update(update);
}
}
//@description The information about interactions with a message has changed
//@chat_id Chat identifier
//@message_id Message identifier
//@interaction_info New information about interactions with the message; may be null
//updateMessageInteractionInfo chat_id:int53 message_id:int53 interaction_info:messageInteractionInfo = Update;
void process_update(td::td_api::updateMessageInteractionInfo &update) {
auto c = chat_window();
if (c && c->main_chat_id() == update.chat_id_) {
c->process_update(update);
}
}
//@description The message content was opened. Updates voice note messages to "listened", video note messages to "viewed" and starts the self-destruct timer
//@chat_id Chat identifier
//@message_id Message identifier
//updateMessageContentOpened chat_id:int53 message_id:int53 = Update;
void process_update(td::td_api::updateMessageContentOpened &update) {
auto c = chat_window();
if (c && c->main_chat_id() == update.chat_id_) {
c->process_update(update);
}
}
//@description A message with an unread mention was read
//@chat_id Chat identifier
//@message_id Message identifier
//@unread_mention_count The new number of unread mention messages left in the chat
//updateMessageMentionRead chat_id:int53 message_id:int53 unread_mention_count:int32 = Update;
void process_update(td::td_api::updateMessageMentionRead &update) {
auto c = chat_window();
if (c && c->main_chat_id() == update.chat_id_) {
c->process_update(update);
}
}
//@description The list of unread reactions added to a message was changed
//@chat_id Chat identifier
//@message_id Message identifier
//@unread_reactions The new list of unread reactions
//@unread_reaction_count The new number of messages with unread reactions left in the chat
//updateMessageUnreadReactions chat_id:int53 message_id:int53 unread_reactions:vector<unreadReaction> unread_reaction_count:int32 = Update;
void process_update(td::td_api::updateMessageUnreadReactions &update) {
auto c = chat_window();
if (c && c->main_chat_id() == update.chat_id_) {
c->process_update(update);
}
}
//@description A fact-check added to a message was changed
//@chat_id Chat identifier
//@message_id Message identifier
//@fact_check The new fact-check
//updateMessageFactCheck chat_id:int53 message_id:int53 fact_check:factCheck = Update;
void process_update(td::td_api::updateMessageFactCheck &update) {
auto c = chat_window();
if (c && c->main_chat_id() == update.chat_id_) {
c->process_update(update);
}
}
//@description A message with a live location was viewed. When the update is received, the application is supposed to update the live location
//@chat_id Identifier of the chat with the live location message
//@message_id Identifier of the message with live location
//updateMessageLiveLocationViewed chat_id:int53 message_id:int53 = Update;
void process_update(td::td_api::updateMessageLiveLocationViewed &update) {
auto c = chat_window();
if (c && c->main_chat_id() == update.chat_id_) {
c->process_update(update);
}
}
//@description An automatically scheduled message with video has been successfully sent after conversion
//@chat_id Identifier of the chat with the message
//@message_id Identifier of the sent message
//updateVideoPublished chat_id:int53 message_id:int53 = Update;
void process_update(td::td_api::updateVideoPublished &update) {
}
//@description A new chat has been loaded/created. This update is guaranteed to come before the chat identifier is returned to the application. The chat field changes will be reported through separate updates
//@chat The chat
//updateNewChat chat:chat = Update;
void process_update(td::td_api::updateNewChat &update) {
dialog_list_window()->process_update(update);
}
//@description The title of a chat was changed
//@chat_id Chat identifier
//@title The new chat title
//updateChatTitle chat_id:int53 title:string = Update;
void process_update(td::td_api::updateChatTitle &update) {
dialog_list_window()->process_update(update);
}
//@description A chat photo was changed
//@chat_id Chat identifier
//@photo The new chat photo; may be null
//updateChatPhoto chat_id:int53 photo:chatPhotoInfo = Update;
void process_update(td::td_api::updateChatPhoto &update) {
dialog_list_window()->process_update(update);
}
//@description Chat accent colors have changed
//@chat_id Chat identifier
//@accent_color_id The new chat accent color identifier
//@background_custom_emoji_id The new identifier of a custom emoji to be shown on the reply header and link preview background; 0 if none
//@profile_accent_color_id The new chat profile accent color identifier; -1 if none
//@profile_background_custom_emoji_id The new identifier of a custom emoji to be shown on the profile background; 0 if none
//updateChatAccentColors chat_id:int53 accent_color_id:int32 background_custom_emoji_id:int64 profile_accent_color_id:int32 profile_background_custom_emoji_id:int64 = Update;
void process_update(td::td_api::updateChatAccentColors &update) {
dialog_list_window()->process_update(update);
}
//@description Chat permissions were changed
//@chat_id Chat identifier
//@permissions The new chat permissions
//updateChatPermissions chat_id:int53 permissions:chatPermissions = Update;
void process_update(td::td_api::updateChatPermissions &update) {
dialog_list_window()->process_update(update);
}
//@description The last message of a chat was changed
//@chat_id Chat identifier
//@last_message The new last message in the chat; may be null if the last message became unknown. While the last message is unknown, new messages can be added to the chat without corresponding updateNewMessage update
//@positions The new chat positions in the chat lists
//updateChatLastMessage chat_id:int53 last_message:message positions:vector<chatPosition> = Update;
void process_update(td::td_api::updateChatLastMessage &update) {
dialog_list_window()->process_update(update);
}
//@description The position of a chat in a chat list has changed. An updateChatLastMessage or updateChatDraftMessage update might be sent instead of the update
//@chat_id Chat identifier
//@position New chat position. If new order is 0, then the chat needs to be removed from the list
//updateChatPosition chat_id:int53 position:chatPosition = Update;
void process_update(td::td_api::updateChatPosition &update) {
dialog_list_window()->process_update(update);
}
//@description A chat was added to a chat list
//@chat_id Chat identifier
//@chat_list The chat list to which the chat was added
//updateChatAddedToList chat_id:int53 chat_list:ChatList = Update;
void process_update(td::td_api::updateChatAddedToList &update) {
dialog_list_window()->process_update(update);
}
//@description A chat was removed from a chat list
//@chat_id Chat identifier
//@chat_list The chat list from which the chat was removed
//updateChatRemovedFromList chat_id:int53 chat_list:ChatList = Update;
void process_update(td::td_api::updateChatRemovedFromList &update) {
dialog_list_window()->process_update(update);
}
//@description Incoming messages were read or the number of unread messages has been changed
//@chat_id Chat identifier
//@last_read_inbox_message_id Identifier of the last read incoming message
//@unread_count The number of unread messages left in the chat
//updateChatReadInbox chat_id:int53 last_read_inbox_message_id:int53 unread_count:int32 = Update;
void process_update(td::td_api::updateChatReadInbox &update) {
dialog_list_window()->process_update(update);
}
//@description Outgoing messages were read
//@chat_id Chat identifier
//@last_read_outbox_message_id Identifier of last read outgoing message
//updateChatReadOutbox chat_id:int53 last_read_outbox_message_id:int53 = Update;
void process_update(td::td_api::updateChatReadOutbox &update) {
dialog_list_window()->process_update(update);
}
//@description The chat action bar was changed
//@chat_id Chat identifier
//@action_bar The new value of the action bar; may be null
//updateChatActionBar chat_id:int53 action_bar:ChatActionBar = Update;
void process_update(td::td_api::updateChatActionBar &update) {
dialog_list_window()->process_update(update);
}
//@description The bar for managing business bot was changed in a chat
//@chat_id Chat identifier
//@business_bot_manage_bar The new value of the business bot manage bar; may be null
//updateChatBusinessBotManageBar chat_id:int53 business_bot_manage_bar:businessBotManageBar = Update;
void process_update(td::td_api::updateChatBusinessBotManageBar &update) {
dialog_list_window()->process_update(update);
}
//@description The chat available reactions were changed
//@chat_id Chat identifier @available_reactions The new reactions, available in the chat
//updateChatAvailableReactions chat_id:int53 available_reactions:ChatAvailableReactions = Update;
void process_update(td::td_api::updateChatAvailableReactions &update) {
dialog_list_window()->process_update(update);
}
//@description A chat draft has changed. Be aware that the update may come in the currently opened chat but with old content of the draft. If the user has changed the content of the draft, this update mustn't be applied
//@chat_id Chat identifier
//@draft_message The new draft message; may be null if none
//@positions The new chat positions in the chat lists
//updateChatDraftMessage chat_id:int53 draft_message:draftMessage positions:vector<chatPosition> = Update;
void process_update(td::td_api::updateChatDraftMessage &update) {
dialog_list_window()->process_update(update);
}
//@description Chat emoji status has changed
//@chat_id Chat identifier
//@emoji_status The new chat emoji status; may be null
//updateChatEmojiStatus chat_id:int53 emoji_status:emojiStatus = Update;
void process_update(td::td_api::updateChatEmojiStatus &update) {
dialog_list_window()->process_update(update);
}
//@description The message sender that is selected to send messages in a chat has changed
//@chat_id Chat identifier
//@message_sender_id New value of message_sender_id; may be null if the user can't change message sender
//updateChatMessageSender chat_id:int53 message_sender_id:MessageSender = Update;
void process_update(td::td_api::updateChatMessageSender &update) {
dialog_list_window()->process_update(update);
}
//@description The message auto-delete or self-destruct timer setting for a chat was changed
//@chat_id Chat identifier
//@message_auto_delete_time New value of message_auto_delete_time
//updateChatMessageAutoDeleteTime chat_id:int53 message_auto_delete_time:int32 = Update;
void process_update(td::td_api::updateChatMessageAutoDeleteTime &update) {
dialog_list_window()->process_update(update);
}
//@description Notification settings for a chat were changed
//@chat_id Chat identifier
//@notification_settings The new notification settings
//updateChatNotificationSettings chat_id:int53 notification_settings:chatNotificationSettings = Update;
void process_update(td::td_api::updateChatNotificationSettings &update) {
dialog_list_window()->process_update(update);
}
//@description The chat pending join requests were changed
//@chat_id Chat identifier
//@pending_join_requests The new data about pending join requests; may be null
//updateChatPendingJoinRequests chat_id:int53 pending_join_requests:chatJoinRequestsInfo = Update;
void process_update(td::td_api::updateChatPendingJoinRequests &update) {
dialog_list_window()->process_update(update);
}
//@description The default chat reply markup was changed. Can occur because new messages with reply markup were received or because an old reply markup was hidden by the user
//@chat_id Chat identifier
//@reply_markup_message_id Identifier of the message from which reply markup needs to be used; 0 if there is no default custom reply markup in the chat
//updateChatReplyMarkup chat_id:int53 reply_markup_message_id:int53 = Update;
void process_update(td::td_api::updateChatReplyMarkup &update) {
dialog_list_window()->process_update(update);
}
//@description The chat background was changed
//@chat_id Chat identifier
//@background The new chat background; may be null if background was reset to default
//updateChatBackground chat_id:int53 background:chatBackground = Update;
void process_update(td::td_api::updateChatBackground &update) {
dialog_list_window()->process_update(update);
}
//@description The chat theme was changed
//@chat_id Chat identifier
//@theme_name The new name of the chat theme; may be empty if theme was reset to default
//updateChatTheme chat_id:int53 theme_name:string = Update;
void process_update(td::td_api::updateChatTheme &update) {
dialog_list_window()->process_update(update);
}
//@description The chat unread_mention_count has changed
//@chat_id Chat identifier
//@unread_mention_count The number of unread mention messages left in the chat
//updateChatUnreadMentionCount chat_id:int53 unread_mention_count:int32 = Update;
void process_update(td::td_api::updateChatUnreadMentionCount &update) {
dialog_list_window()->process_update(update);
}
//@description The chat unread_reaction_count has changed
//@chat_id Chat identifier
//@unread_reaction_count The number of messages with unread reactions left in the chat
//updateChatUnreadReactionCount chat_id:int53 unread_reaction_count:int32 = Update;
void process_update(td::td_api::updateChatUnreadReactionCount &update) {
dialog_list_window()->process_update(update);
}
//@description A chat video chat state has changed
//@chat_id Chat identifier
//@video_chat New value of video_chat
//updateChatVideoChat chat_id:int53 video_chat:videoChat = Update;
void process_update(td::td_api::updateChatVideoChat &update) {
dialog_list_window()->process_update(update);
}
//@description The value of the default disable_notification parameter, used when a message is sent to the chat, was changed
//@chat_id Chat identifier
//@default_disable_notification The new default_disable_notification value
//updateChatDefaultDisableNotification chat_id:int53 default_disable_notification:Bool = Update;
void process_update(td::td_api::updateChatDefaultDisableNotification &update) {
dialog_list_window()->process_update(update);
}
//@description A chat content was allowed or restricted for saving
//@chat_id Chat identifier
//@has_protected_content New value of has_protected_content
//updateChatHasProtectedContent chat_id:int53 has_protected_content:Bool = Update;
void process_update(td::td_api::updateChatHasProtectedContent &update) {
dialog_list_window()->process_update(update);
}