forked from vysheng/tg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop.c
923 lines (815 loc) · 24.6 KB
/
loop.c
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
/*
This file is part of telegram-cli.
Telegram-cli 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, either version 2 of the License, or
(at your option) any later version.
Telegram-cli 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 telegram-cli. If not, see <http://www.gnu.org/licenses/>.
Copyright Vitaly Valtman 2013-2014
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define _GNU_SOURCE
#define READLINE_CALLBACKS
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef READLINE_GNU
#include <readline/readline.h>
#include <readline/history.h>
#else
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include <errno.h>
#include <poll.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#ifdef EVENT_V2
#include <event2/event.h>
#include <event2/bufferevent.h>
#else
#include <event.h>
#include "event-old.h"
#endif
#include "interface.h"
#include "telegram.h"
#include "loop.h"
#include "lua-tg.h"
#include <tgl/tgl.h>
#include <tgl/tgl-binlog.h>
#include <tgl/tgl-net.h>
#include <tgl/tgl-timers.h>
#include <openssl/sha.h>
int verbosity;
extern int readline_disabled;
int binlog_read;
extern char *default_username;
extern char *auth_token;
void set_default_username (const char *s);
extern int binlog_enabled;
extern int unknown_user_list_pos;
extern int unknown_user_list[];
int register_mode;
extern int safe_quit;
extern int sync_from_start;
extern int disable_output;
extern int reset_authorization;
extern int sfd;
extern int usfd;
void got_it (char *line, int len);
void write_state_file (void);
static char *line_buffer;
static int line_buffer_size;
static int line_buffer_pos;
static int delete_stdin_event;
extern volatile int sigterm_cnt;
extern char *start_command;
extern struct tgl_state *TLS;
static void stdin_read_callback_all (int arg, short what, struct event *self) {
if (!readline_disabled) {
if (((long)arg) & 1) {
rl_callback_read_char ();
} else {
char *line = 0;
size_t len = 0;
assert (getline (&line, &len, stdin) >= 0);
got_it (line, strlen (line));
}
} else {
while (1) {
if (line_buffer_pos == line_buffer_size) {
line_buffer = realloc (line_buffer, line_buffer_size * 2 + 100);
assert (line_buffer);
line_buffer_size = line_buffer_size * 2 + 100;
assert (line_buffer);
}
int r = read (0, line_buffer + line_buffer_pos, line_buffer_size - line_buffer_pos);
//logprintf ("r = %d, size = %d, pos = %d, what = 0x%x, fd = %d\n", r, line_buffer_size, line_buffer_pos, (int)what, fd);
if (r < 0) {
perror ("read");
delete_stdin_event = 1;
break;
}
if (r == 0) {
//struct event *ev = event_base_get_running_event (TLS->ev_base);
//event_del (ev);
//event_del (self);
delete_stdin_event = 1;
break;
}
line_buffer_pos += r;
while (1) {
int p = 0;
while (p < line_buffer_pos && line_buffer[p] != '\n') { p ++; }
if (p < line_buffer_pos) {
if (((long)arg) & 1) {
line_buffer[p] = 0;
interpreter (line_buffer);
} else {
got_it (line_buffer, p + 1);
}
memmove (line_buffer, line_buffer + p + 1, line_buffer_pos - p - 1);
line_buffer_pos -= (p + 1);
} else {
break;
}
}
break;
}
}
}
static void stdin_read_callback_char (evutil_socket_t fd, short what, void *arg) {
stdin_read_callback_all (1, what, arg);
}
static void stdin_read_callback_line (evutil_socket_t fd, short what, void *arg) {
stdin_read_callback_all (2, what, arg);
}
struct event *term_ev = 0;
void net_loop (int flags, int (*is_end)(void)) {
delete_stdin_event = 0;
if (verbosity >= E_DEBUG) {
logprintf ("Starting netloop\n");
}
if (flags & 3) {
if (flags & 1) {
term_ev = event_new (TLS->ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback_char, 0);
} else {
term_ev = event_new (TLS->ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback_line, 0);
}
event_add (term_ev, 0);
}
int last_get_state = time (0);
while (!is_end || !is_end ()) {
event_base_loop (TLS->ev_base, EVLOOP_ONCE);
if (term_ev && delete_stdin_event) {
event_free (term_ev);
term_ev = 0;
}
#ifdef USE_LUA
lua_do_all ();
#endif
if (safe_quit && !TLS->active_queries) {
printf ("All done. Exit\n");
do_halt (0);
}
if (sigterm_cnt > 0) {
do_halt (0);
}
if (time (0) - last_get_state > 3600) {
tgl_do_lookup_state (TLS);
last_get_state = time (0);
}
write_state_file ();
update_prompt ();
if (unknown_user_list_pos) {
int i;
for (i = 0; i < unknown_user_list_pos; i++) {
tgl_do_get_user_info (TLS, TGL_MK_USER (unknown_user_list[i]), 0, 0, 0);
}
unknown_user_list_pos = 0;
}
}
if (term_ev) {
event_free (term_ev);
term_ev = 0;
}
if (verbosity >= E_DEBUG) {
logprintf ("End of netloop\n");
}
}
char **_s;
size_t *_l;
int got_it_ok;
void got_it (char *line, int len) {
assert (len > 0);
line[-- len] = 0; // delete end of line
*_s = line;
*_l = len;
got_it_ok = 1;
}
int is_got_it (void) {
return got_it_ok;
}
int net_getline (char **s, size_t *l) {
fflush (stdout);
// rl_already_prompted = 1;
got_it_ok = 0;
_s = s;
_l = l;
// rl_callback_handler_install (0, got_it);
net_loop (2, is_got_it);
return 0;
}
int main_loop (void) {
net_loop (1, 0);
return 0;
}
struct tgl_dc *cur_a_dc;
int is_authorized (void) {
return tgl_authorized_dc (TLS, cur_a_dc);
}
int all_authorized (void) {
int i;
for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i]) {
if (!tgl_authorized_dc (TLS, TLS->DC_list[i])) {
return 0;
}
}
return 1;
}
int config_got;
int got_config (void) {
return config_got;
}
void on_get_config (void *extra, int success) {
if (!success) {
logprintf ("Can not get config.\n");
do_halt (1);
}
config_got = 1;
}
int should_register;
char *hash;
void sign_in_callback (struct tgl_state *TLSR, void *extra, int success, int registered, const char *mhash) {
assert (TLSR == TLS);
if (!success) {
logprintf ("Can not send code\n");
do_halt (1);
}
should_register = !registered;
hash = strdup (mhash);
assert (hash);
}
int signed_in_ok;
void sign_in_result (struct tgl_state *TLSR, void *extra, int success, struct tgl_user *U) {
assert (TLSR == TLS);
if (!success) {
logprintf ("Can not login\n");
do_halt (1);
}
signed_in_ok = 1;
}
int signed_in (void) {
return signed_in_ok;
}
int sent_code (void) {
return hash != 0;
}
int dc_signed_in (void) {
return tgl_signed_dc (TLS, cur_a_dc);
}
void export_auth_callback (struct tgl_state *TLSR, void *DC, int success) {
assert (TLSR == TLS);
if (!success) {
logprintf ("Can not export auth\n");
do_halt (1);
}
}
int d_got_ok;
void get_difference_callback (struct tgl_state *TLSR, void *extra, int success) {
assert (TLSR == TLS);
assert (success);
d_got_ok = 1;
}
int dgot (void) {
return d_got_ok;
}
int zero[512];
int readline_active;
int new_dc_num;
int wait_dialog_list;
extern struct tgl_update_callback upd_cb;
#define DC_SERIALIZED_MAGIC 0x868aa81d
#define STATE_FILE_MAGIC 0x28949a93
#define SECRET_CHAT_FILE_MAGIC 0x37a1988a
char *get_auth_key_filename (void);
char *get_state_filename (void);
char *get_secret_chat_filename (void);
void read_state_file (void) {
if (binlog_enabled) { return; }
int state_file_fd = open (get_state_filename (), O_CREAT | O_RDWR, 0600);
if (state_file_fd < 0) {
return;
}
int version, magic;
if (read (state_file_fd, &magic, 4) < 4) { close (state_file_fd); return; }
if (magic != (int)STATE_FILE_MAGIC) { close (state_file_fd); return; }
if (read (state_file_fd, &version, 4) < 4) { close (state_file_fd); return; }
assert (version >= 0);
int x[4];
if (read (state_file_fd, x, 16) < 16) {
close (state_file_fd);
return;
}
int pts = x[0];
int qts = x[1];
int seq = x[2];
int date = x[3];
close (state_file_fd);
bl_do_set_seq (TLS, seq);
bl_do_set_pts (TLS, pts);
bl_do_set_qts (TLS, qts);
bl_do_set_date (TLS, date);
}
void write_state_file (void) {
if (binlog_enabled) { return; }
static int wseq;
static int wpts;
static int wqts;
static int wdate;
if (wseq >= TLS->seq && wpts >= TLS->pts && wqts >= TLS->qts && wdate >= TLS->date) { return; }
wseq = TLS->seq; wpts = TLS->pts; wqts = TLS->qts; wdate = TLS->date;
int state_file_fd = open (get_state_filename (), O_CREAT | O_RDWR, 0600);
if (state_file_fd < 0) {
logprintf ("Can not write state file '%s': %m\n", get_state_filename ());
do_halt (1);
}
int x[6];
x[0] = STATE_FILE_MAGIC;
x[1] = 0;
x[2] = wpts;
x[3] = wqts;
x[4] = wseq;
x[5] = wdate;
assert (write (state_file_fd, x, 24) == 24);
close (state_file_fd);
}
void write_dc (struct tgl_dc *DC, void *extra) {
int auth_file_fd = *(int *)extra;
if (!DC) {
int x = 0;
assert (write (auth_file_fd, &x, 4) == 4);
return;
} else {
int x = 1;
assert (write (auth_file_fd, &x, 4) == 4);
}
assert (DC->has_auth);
assert (write (auth_file_fd, &DC->port, 4) == 4);
int l = strlen (DC->ip);
assert (write (auth_file_fd, &l, 4) == 4);
assert (write (auth_file_fd, DC->ip, l) == l);
assert (write (auth_file_fd, &DC->auth_key_id, 8) == 8);
assert (write (auth_file_fd, DC->auth_key, 256) == 256);
}
void write_auth_file (void) {
if (binlog_enabled) { return; }
int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, 0600);
assert (auth_file_fd >= 0);
int x = DC_SERIALIZED_MAGIC;
assert (write (auth_file_fd, &x, 4) == 4);
assert (write (auth_file_fd, &TLS->max_dc_num, 4) == 4);
assert (write (auth_file_fd, &TLS->dc_working_num, 4) == 4);
tgl_dc_iterator_ex (TLS, write_dc, &auth_file_fd);
assert (write (auth_file_fd, &TLS->our_id, 4) == 4);
close (auth_file_fd);
}
void write_secret_chat (tgl_peer_t *_P, void *extra) {
struct tgl_secret_chat *P = (void *)_P;
if (tgl_get_peer_type (P->id) != TGL_PEER_ENCR_CHAT) { return; }
if (P->state != sc_ok) { return; }
int *a = extra;
int fd = a[0];
a[1] ++;
int id = tgl_get_peer_id (P->id);
assert (write (fd, &id, 4) == 4);
//assert (write (fd, &P->flags, 4) == 4);
int l = strlen (P->print_name);
assert (write (fd, &l, 4) == 4);
assert (write (fd, P->print_name, l) == l);
assert (write (fd, &P->user_id, 4) == 4);
assert (write (fd, &P->admin_id, 4) == 4);
assert (write (fd, &P->date, 4) == 4);
assert (write (fd, &P->ttl, 4) == 4);
assert (write (fd, &P->layer, 4) == 4);
assert (write (fd, &P->access_hash, 8) == 8);
assert (write (fd, &P->state, 4) == 4);
assert (write (fd, &P->key_fingerprint, 8) == 8);
assert (write (fd, &P->key, 256) == 256);
assert (write (fd, &P->first_key_sha, 20) == 20);
assert (write (fd, &P->in_seq_no, 4) == 4);
assert (write (fd, &P->last_in_seq_no, 4) == 4);
assert (write (fd, &P->out_seq_no, 4) == 4);
}
void write_secret_chat_file (void) {
if (binlog_enabled) { return; }
int secret_chat_fd = open (get_secret_chat_filename (), O_CREAT | O_RDWR, 0600);
assert (secret_chat_fd >= 0);
int x = SECRET_CHAT_FILE_MAGIC;
assert (write (secret_chat_fd, &x, 4) == 4);
x = 2;
assert (write (secret_chat_fd, &x, 4) == 4); // version
assert (write (secret_chat_fd, &x, 4) == 4); // num
int y[2];
y[0] = secret_chat_fd;
y[1] = 0;
tgl_peer_iterator_ex (TLS, write_secret_chat, y);
lseek (secret_chat_fd, 8, SEEK_SET);
assert (write (secret_chat_fd, &y[1], 4) == 4);
close (secret_chat_fd);
}
void read_dc (int auth_file_fd, int id, unsigned ver) {
int port = 0;
assert (read (auth_file_fd, &port, 4) == 4);
int l = 0;
assert (read (auth_file_fd, &l, 4) == 4);
assert (l >= 0 && l < 100);
char ip[100];
assert (read (auth_file_fd, ip, l) == l);
ip[l] = 0;
long long auth_key_id;
static unsigned char auth_key[256];
assert (read (auth_file_fd, &auth_key_id, 8) == 8);
assert (read (auth_file_fd, auth_key, 256) == 256);
//bl_do_add_dc (id, ip, l, port, auth_key_id, auth_key);
bl_do_dc_option (TLS, id, 2, "DC", l, ip, port);
bl_do_set_auth_key_id (TLS, id, auth_key);
bl_do_dc_signed (TLS, id);
}
void empty_auth_file (void) {
if (TLS->test_mode) {
bl_do_dc_option (TLS, 1, 0, "", strlen (TG_SERVER_TEST_1), TG_SERVER_TEST_1, 443);
bl_do_dc_option (TLS, 2, 0, "", strlen (TG_SERVER_TEST_2), TG_SERVER_TEST_2, 443);
bl_do_dc_option (TLS, 3, 0, "", strlen (TG_SERVER_TEST_3), TG_SERVER_TEST_3, 443);
bl_do_set_working_dc (TLS, 2);
} else {
bl_do_dc_option (TLS, 1, 0, "", strlen (TG_SERVER_1), TG_SERVER_1, 443);
bl_do_dc_option (TLS, 2, 0, "", strlen (TG_SERVER_2), TG_SERVER_2, 443);
bl_do_dc_option (TLS, 3, 0, "", strlen (TG_SERVER_3), TG_SERVER_3, 443);
bl_do_dc_option (TLS, 4, 0, "", strlen (TG_SERVER_4), TG_SERVER_4, 443);
bl_do_dc_option (TLS, 5, 0, "", strlen (TG_SERVER_5), TG_SERVER_5, 443);
bl_do_set_working_dc (TLS, 4);
}
}
int need_dc_list_update;
void read_auth_file (void) {
if (binlog_enabled) { return; }
int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, 0600);
if (auth_file_fd < 0) {
empty_auth_file ();
return;
}
assert (auth_file_fd >= 0);
unsigned x;
unsigned m;
if (read (auth_file_fd, &m, 4) < 4 || (m != DC_SERIALIZED_MAGIC)) {
close (auth_file_fd);
empty_auth_file ();
return;
}
assert (read (auth_file_fd, &x, 4) == 4);
assert (x > 0);
int dc_working_num;
assert (read (auth_file_fd, &dc_working_num, 4) == 4);
int i;
for (i = 0; i <= (int)x; i++) {
int y;
assert (read (auth_file_fd, &y, 4) == 4);
if (y) {
read_dc (auth_file_fd, i, m);
}
}
bl_do_set_working_dc (TLS, dc_working_num);
int our_id;
int l = read (auth_file_fd, &our_id, 4);
if (l < 4) {
assert (!l);
}
if (our_id) {
bl_do_set_our_id (TLS, our_id);
}
close (auth_file_fd);
}
void read_secret_chat (int fd, int v) {
int id, l, user_id, admin_id, date, ttl, layer, state;
long long access_hash, key_fingerprint;
static char s[1000];
static unsigned char key[256];
static unsigned char sha[20];
assert (read (fd, &id, 4) == 4);
//assert (read (fd, &flags, 4) == 4);
assert (read (fd, &l, 4) == 4);
assert (l > 0 && l < 1000);
assert (read (fd, s, l) == l);
assert (read (fd, &user_id, 4) == 4);
assert (read (fd, &admin_id, 4) == 4);
assert (read (fd, &date, 4) == 4);
assert (read (fd, &ttl, 4) == 4);
assert (read (fd, &layer, 4) == 4);
assert (read (fd, &access_hash, 8) == 8);
assert (read (fd, &state, 4) == 4);
assert (read (fd, &key_fingerprint, 8) == 8);
assert (read (fd, &key, 256) == 256);
if (v >= 2) {
assert (read (fd, sha, 20) == 20);
}
int in_seq_no = 0, out_seq_no = 0, last_in_seq_no = 0;
if (v >= 1) {
assert (read (fd, &in_seq_no, 4) == 4);
assert (read (fd, &last_in_seq_no, 4) == 4);
assert (read (fd, &out_seq_no, 4) == 4);
}
bl_do_encr_chat_create (TLS, id, user_id, admin_id, s, l);
struct tgl_secret_chat *P = (void *)tgl_peer_get (TLS, TGL_MK_ENCR_CHAT (id));
assert (P && (P->flags & FLAG_CREATED));
bl_do_encr_chat_set_date (TLS, P, date);
bl_do_encr_chat_set_ttl (TLS, P, ttl);
bl_do_encr_chat_set_layer (TLS ,P, layer);
bl_do_encr_chat_set_access_hash (TLS, P, access_hash);
bl_do_encr_chat_set_state (TLS, P, state);
bl_do_encr_chat_set_key (TLS, P, key, key_fingerprint);
if (v >= 2) {
bl_do_encr_chat_set_sha (TLS, P, sha);
} else {
SHA1 ((void *)key, 256, sha);
bl_do_encr_chat_set_sha (TLS, P, sha);
}
if (v >= 1) {
bl_do_encr_chat_set_seq (TLS, P, in_seq_no, last_in_seq_no, out_seq_no);
}
}
void read_secret_chat_file (void) {
if (binlog_enabled) { return; }
int secret_chat_fd = open (get_secret_chat_filename (), O_RDWR, 0600);
if (secret_chat_fd < 0) { return; }
//assert (secret_chat_fd >= 0);
int x;
if (read (secret_chat_fd, &x, 4) < 4) { close (secret_chat_fd); return; }
if (x != SECRET_CHAT_FILE_MAGIC) { close (secret_chat_fd); return; }
int v = 0;
assert (read (secret_chat_fd, &v, 4) == 4);
assert (v == 0 || v == 1 || v == 2); // version
assert (read (secret_chat_fd, &x, 4) == 4);
assert (x >= 0);
while (x --> 0) {
read_secret_chat (secret_chat_fd, v);
}
close (secret_chat_fd);
}
void dlist_cb (struct tgl_state *TLSR, void *callback_extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]) {
assert (TLSR == TLS);
d_got_ok = 1;
}
static void read_incoming (struct bufferevent *bev, void *_arg) {
vlogprintf (E_WARNING, "Read from incoming connection\n");
struct in_ev *ev = _arg;
assert (ev->bev == bev);
ev->in_buf_pos += bufferevent_read (bev, ev->in_buf + ev->in_buf_pos, 4096 - ev->in_buf_pos);
while (1) {
int pos = 0;
int ok = 0;
while (pos < ev->in_buf_pos) {
if (ev->in_buf[pos] == '\n') {
if (!ev->error) {
ev->in_buf[pos] = 0;
interpreter_ex (ev->in_buf, ev);
} else {
ev->error = 0;
}
ok = 1;
ev->in_buf_pos -= (pos + 1);
memmove (ev->in_buf, ev->in_buf + pos + 1, ev->in_buf_pos);
pos = 0;
} else {
pos ++;
}
}
if (ok) {
ev->in_buf_pos += bufferevent_read (bev, ev->in_buf + ev->in_buf_pos, 4096 - ev->in_buf_pos);
} else {
if (ev->in_buf_pos == 4096) {
ev->error = 1;
}
break;
}
}
}
static void event_incoming (struct bufferevent *bev, short what, void *_arg) {
struct in_ev *ev = _arg;
if (what & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
vlogprintf (E_WARNING, "Closing incoming connection\n");
assert (ev->fd >= 0);
close (ev->fd);
bufferevent_free (bev);
ev->bev = 0;
if (!--ev->refcnt) { free (ev); }
}
}
static void accept_incoming (evutil_socket_t efd, short what, void *arg) {
vlogprintf (E_WARNING, "Accepting incoming connection\n");
unsigned clilen = 0;
struct sockaddr_in cli_addr;
int fd = accept (efd, (struct sockaddr *)&cli_addr, &clilen);
assert (fd >= 0);
struct bufferevent *bev = bufferevent_socket_new (TLS->ev_base, fd, 0);
struct in_ev *e = malloc (sizeof (*e));
e->bev = bev;
e->refcnt = 1;
e->in_buf_pos = 0;
e->error = 0;
e->fd = fd;
bufferevent_setcb (bev, read_incoming, 0, event_incoming, e);
bufferevent_enable(bev, EV_READ|EV_WRITE);
}
char *get_downloads_directory (void);
int loop (void) {
//on_start ();
tgl_set_callback (TLS, &upd_cb);
//TLS->temp_key_expire_time = 60;
struct event_base *ev = event_base_new ();
tgl_set_ev_base (TLS, ev);
tgl_set_net_methods (TLS, &tgl_conn_methods);
tgl_set_timer_methods (TLS, &tgl_libevent_timers);
assert (TLS->timer_methods);
tgl_set_download_directory (TLS, get_downloads_directory ());
tgl_register_app_id (TLS, TELEGRAM_CLI_APP_ID, TELEGRAM_CLI_APP_HASH);
tgl_init (TLS);
if (binlog_enabled) {
double t = tglt_get_double_time ();
if (verbosity >= E_DEBUG) {
logprintf ("replay log start\n");
}
tgl_replay_log (TLS);
if (verbosity >= E_DEBUG) {
logprintf ("replay log end in %lf seconds\n", tglt_get_double_time () - t);
}
tgl_reopen_binlog_for_writing (TLS);
} else {
read_auth_file ();
read_state_file ();
read_secret_chat_file ();
}
binlog_read = 1;
#ifdef USE_LUA
lua_binlog_end ();
#endif
if (sfd >= 0) {
struct event *ev = event_new (TLS->ev_base, sfd, EV_READ | EV_PERSIST, accept_incoming, 0);
event_add (ev, 0);
}
if (usfd >= 0) {
struct event *ev = event_new (TLS->ev_base, usfd, EV_READ | EV_PERSIST, accept_incoming, 0);
event_add (ev, 0);
}
update_prompt ();
if (reset_authorization) {
tgl_peer_t *P = tgl_peer_get (TLS, TGL_MK_USER (TLS->our_id));
if (P && P->user.phone && reset_authorization == 1) {
set_default_username (P->user.phone);
}
bl_do_reset_authorization (TLS);
}
net_loop (0, all_authorized);
int i;
for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i] && !tgl_authorized_dc (TLS, TLS->DC_list[i])) {
assert (0);
}
if (!tgl_signed_dc (TLS, TLS->DC_working)) {
if (disable_output) {
fprintf (stderr, "Can not login without output\n");
do_halt (1);
}
if (!default_username) {
size_t size = 0;
char *user = 0;
if (!user) {
printf ("Telephone number (with '+' sign): ");
if (net_getline (&user, &size) == -1) {
perror ("getline()");
do_halt (1);
}
set_default_username (user);
}
}
tgl_do_send_code (TLS, default_username, sign_in_callback, 0);
net_loop (0, sent_code);
if (verbosity >= E_DEBUG) {
logprintf ("%s\n", should_register ? "phone not registered" : "phone registered");
}
if (!should_register) {
char *code = 0;
size_t size = 0;
printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): ");
while (1) {
if (net_getline (&code, &size) == -1) {
perror ("getline()");
do_halt (1);
}
if (!strcmp (code, "call")) {
printf ("You typed \"call\", switching to phone system.\n");
tgl_do_phone_call (TLS, default_username, hash, 0, 0);
printf ("Calling you! Code: ");
continue;
}
if (tgl_do_send_code_result (TLS, default_username, hash, code, sign_in_result, 0) >= 0) {
break;
}
printf ("Invalid code. Try again: ");
free (code);
}
} else {
printf ("User is not registered. Do you want to register? [Y/n] ");
char *code;
size_t size;
if (net_getline (&code, &size) == -1) {
perror ("getline()");
do_halt (1);
}
if (!*code || *code == 'y' || *code == 'Y') {
printf ("Ok, starting registartion.\n");
} else {
printf ("Then try again\n");
do_halt (1);
}
char *first_name;
printf ("First name: ");
if (net_getline (&first_name, &size) == -1) {
perror ("getline()");
do_halt (1);
}
char *last_name;
printf ("Last name: ");
if (net_getline (&last_name, &size) == -1) {
perror ("getline()");
do_halt (1);
}
printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): ");
while (1) {
if (net_getline (&code, &size) == -1) {
perror ("getline()");
do_halt (1);
}
if (!strcmp (code, "call")) {
printf ("You typed \"call\", switching to phone system.\n");
tgl_do_phone_call (TLS, default_username, hash, 0, 0);
printf ("Calling you! Code: ");
continue;
}
if (tgl_do_send_code_result_auth (TLS, default_username, hash, code, first_name, last_name, sign_in_result, 0) >= 0) {
break;
}
printf ("Invalid code. Try again: ");
free (code);
}
}
net_loop (0, signed_in);
//bl_do_dc_signed (TLS->DC_working);
}
for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i] && !tgl_signed_dc (TLS, TLS->DC_list[i])) {
tgl_do_export_auth (TLS, i, export_auth_callback, (void*)(long)TLS->DC_list[i]);
cur_a_dc = TLS->DC_list[i];
net_loop (0, dc_signed_in);
assert (tgl_signed_dc (TLS, TLS->DC_list[i]));
}
write_auth_file ();
fflush (stdout);
fflush (stderr);
//read_state_file ();
//read_secret_chat_file ();
set_interface_callbacks ();
tglm_send_all_unsent (TLS);
tgl_do_get_difference (TLS, sync_from_start, get_difference_callback, 0);
net_loop (0, dgot);
assert (!(TLS->locks & TGL_LOCK_DIFF));
TLS->started = 1;
if (wait_dialog_list) {
d_got_ok = 0;
tgl_do_get_dialog_list (TLS, dlist_cb, 0);
net_loop (0, dgot);
}
#ifdef USE_LUA
lua_diff_end ();
#endif
if (start_command) {
safe_quit = 1;
while (*start_command) {
char *start = start_command;
while (*start_command && *start_command != '\n') {
start_command ++;
}
if (*start_command) {
*start_command = 0;
start_command ++;
}
interpreter_ex (start, 0);
}
}
/*tgl_do_get_dialog_list (get_dialogs_callback, 0);
if (wait_dialog_list) {
dialog_list_got = 0;
net_loop (0, dlgot);
}*/
return main_loop ();
}