1 /*
2     This file is part of telegram-cli.
3 
4     Telegram-cli is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 2 of the License, or
7     (at your option) any later version.
8 
9     Telegram-cli is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this telegram-cli.  If not, see <http://www.gnu.org/licenses/>.
16 
17     Copyright Vitaly Valtman 2013-2015
18 */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #if USE_PYTHON
25 #include "python-tg.h"
26 #endif
27 
28 #ifndef _GNU_SOURCE
29 #define _GNU_SOURCE
30 #endif
31 #define READLINE_CALLBACKS
32 
33 #include <assert.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #ifdef READLINE_GNU
38 #include <readline/readline.h>
39 #include <readline/history.h>
40 #else
41 #include <readline/readline.h>
42 #include <readline/history.h>
43 #endif
44 
45 #include <errno.h>
46 #include <poll.h>
47 #include <unistd.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <sys/socket.h>
51 #include <fcntl.h>
52 #include <netinet/in.h>
53 
54 #ifdef EVENT_V2
55 #include <event2/event.h>
56 #include <event2/bufferevent.h>
57 #else
58 #include <event.h>
59 #include "event-old.h"
60 #endif
61 
62 #include "interface.h"
63 #include "telegram.h"
64 #include "loop.h"
65 #if USE_LUA
66 #include "lua-tg.h"
67 #endif
68 
69 #include <tgl/tgl.h>
70 #include <tgl/tgl-binlog.h>
71 #include <tgl/tgl-net.h>
72 #include <tgl/tgl-timers.h>
73 #include <tgl/tgl-queries.h>
74 
75 #include <openssl/sha.h>
76 
77 int verbosity;
78 extern int readline_disabled;
79 extern char *bot_hash;
80 
81 extern int bot_mode;
82 int binlog_read;
83 extern char *default_username;
84 extern char *auth_token;
85 void set_default_username (const char *s);
86 extern int binlog_enabled;
87 
88 extern int unknown_user_list_pos;
89 extern int unknown_user_list[];
90 int register_mode;
91 extern int safe_quit;
92 extern int sync_from_start;
93 
94 extern int disable_output;
95 extern int reset_authorization;
96 
97 extern int sfd;
98 extern int usfd;
99 
100 void got_it (char *line, int len);
101 void write_state_file (void);
102 
103 static char *line_buffer;
104 static int line_buffer_size;
105 static int line_buffer_pos;
106 static int delete_stdin_event;
107 
108 extern volatile int sigterm_cnt;
109 
110 extern char *start_command;
111 extern struct tgl_state *TLS;
112 extern int ipv6_enabled;
113 
114 struct event *term_ev = 0;
115 int read_one_string;
116 #define MAX_ONE_STRING_LEN 511
117 char one_string[MAX_ONE_STRING_LEN + 1];
118 int one_string_len;
119 void (*one_string_cb)(struct tgl_state *TLS, const char *string[], void *arg);
120 enum tgl_value_type one_string_type;
121 int one_string_num;
122 int one_string_total_args;
123 char *one_string_results[10];
124 
125 void *string_cb_arg;
126 char *one_string_prompt;
127 int one_string_flags;
128 extern int disable_link_preview;
129 
130 void deactivate_readline (void);
131 void reactivate_readline (void);
132 
133 void do_get_string (struct tgl_state *TLS);
one_string_read_end(void)134 static void one_string_read_end (void) {
135   printf ("\n");
136   fflush (stdout);
137 
138   read_one_string = 0;
139   tfree_str (one_string_prompt);
140   one_string_prompt = NULL;
141   reactivate_readline ();
142 
143   one_string_results[one_string_num] = tstrdup (one_string);
144   ++one_string_num;
145 
146   if (one_string_num < one_string_total_args) {
147     do_get_string (TLS);
148   } else {
149     one_string_cb (TLS, (void *)one_string_results, string_cb_arg);
150     int i;
151     for (i = 0; i < one_string_total_args; i++) {
152       tfree_str (one_string_results[i]);
153     }
154   }
155 }
156 
generate_prompt(enum tgl_value_type type,int num)157 void generate_prompt (enum tgl_value_type type, int num) {
158   switch (type) {
159   case tgl_phone_number:
160     assert (!num);
161     one_string_prompt = tstrdup ("phone number: ");
162     one_string_flags = 0;
163     return;
164   case tgl_code:
165     assert (!num);
166     one_string_prompt = tstrdup ("code ('CALL' for phone code): ");
167     one_string_flags = 0;
168     return;
169   case tgl_register_info:
170     one_string_flags = 0;
171     switch (num) {
172     case 0:
173       one_string_prompt = tstrdup ("register (Y/n): ");
174       return;
175     case 1:
176       one_string_prompt = tstrdup ("first name: ");
177       return;
178     case 2:
179       one_string_prompt = tstrdup ("last name: ");
180       return;
181     default:
182       assert (0);
183     }
184     return;
185   case tgl_new_password:
186     one_string_flags = 1;
187     switch (num) {
188     case 0:
189       one_string_prompt = tstrdup ("new password: ");
190       return;
191     case 1:
192       one_string_prompt = tstrdup ("retype new password: ");
193       return;
194     default:
195       assert (0);
196     }
197     return;
198   case tgl_cur_and_new_password:
199     one_string_flags = 1;
200     switch (num) {
201     case 0:
202       one_string_prompt = tstrdup ("old password: ");
203       return;
204     case 1:
205       one_string_prompt = tstrdup ("new password: ");
206       return;
207     case 2:
208       one_string_prompt = tstrdup ("retype new password: ");
209       return;
210     default:
211       assert (0);
212     }
213     return;
214   case tgl_cur_password:
215     one_string_flags = 1;
216     assert (!num);
217     one_string_prompt = tstrdup ("password: ");
218     return;
219   case tgl_bot_hash:
220     one_string_flags = 0;
221     assert (!num);
222     one_string_prompt = tstrdup ("hash: ");
223     return;
224   default:
225     assert (0);
226   }
227 }
228 
do_get_string(struct tgl_state * TLS)229 void do_get_string (struct tgl_state *TLS) {
230   deactivate_readline ();
231   generate_prompt (one_string_type, one_string_num);
232   printf ("%s", one_string_prompt);
233   fflush (stdout);
234   read_one_string = 1;
235   one_string_len = 0;
236 }
237 
do_get_values(struct tgl_state * TLS,enum tgl_value_type type,const char * prompt,int num_values,void (* callback)(struct tgl_state * TLS,const char * string[],void * arg),void * arg)238 void do_get_values (struct tgl_state *TLS, enum tgl_value_type type, const char *prompt, int num_values,
239           void (*callback)(struct tgl_state *TLS, const char *string[], void *arg), void *arg) {
240   if (type == tgl_bot_hash && bot_hash) {
241     assert (num_values == 1);
242     one_string_results[0] = bot_hash;
243     callback (TLS, (void *)one_string_results, arg);
244     return;
245   }
246   one_string_cb = callback;
247   one_string_num = 0;
248   one_string_total_args = num_values;
249   one_string_type = type;
250   string_cb_arg = arg;
251   do_get_string (TLS);
252 }
253 
stdin_read_callback(evutil_socket_t fd,short what,void * arg)254 static void stdin_read_callback (evutil_socket_t fd, short what, void *arg) {
255   if (!readline_disabled && !read_one_string) {
256     rl_callback_read_char ();
257     return;
258   }
259   if (read_one_string) {
260     char c;
261     int r = read (0, &c, 1);
262     if (r <= 0) {
263       perror ("read");
264       delete_stdin_event = 1;
265       return;
266     }
267     if (c == '\n' || c == '\r') {
268       one_string[one_string_len] = 0;
269       one_string_read_end ();
270       return;
271     }
272     if (one_string_len < MAX_ONE_STRING_LEN) {
273       one_string[one_string_len ++] = c;
274       if (!(one_string_flags & 1)) {
275         printf ("%c", c);
276         fflush (stdout);
277       }
278     }
279     return;
280   }
281 
282   if (line_buffer_pos == line_buffer_size) {
283     line_buffer = realloc (line_buffer, line_buffer_size * 2 + 100);
284     assert (line_buffer);
285     line_buffer_size = line_buffer_size * 2 + 100;
286     assert (line_buffer);
287   }
288   int r = read (0, line_buffer + line_buffer_pos, line_buffer_size - line_buffer_pos);
289   if (r <= 0) {
290     perror ("read");
291     delete_stdin_event = 1;
292     return;
293   }
294   line_buffer_pos += r;
295 
296   while (1) {
297     int p = 0;
298     while (p < line_buffer_pos && line_buffer[p] != '\n') { p ++; }
299     if (p < line_buffer_pos) {
300       line_buffer[p] = 0;
301       interpreter (line_buffer);
302       memmove (line_buffer, line_buffer + p + 1, line_buffer_pos - p - 1);
303       line_buffer_pos -= (p + 1);
304     } else {
305       break;
306     }
307   }
308 }
309 
310 
net_loop(void)311 void net_loop (void) {
312   delete_stdin_event = 0;
313   if (verbosity >= E_DEBUG) {
314     logprintf ("Starting netloop\n");
315   }
316   term_ev = event_new (TLS->ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback, 0);
317   event_add (term_ev, 0);
318 
319   int last_get_state = time (0);
320   while (1) {
321     event_base_loop (TLS->ev_base, EVLOOP_ONCE);
322 
323     if (term_ev && delete_stdin_event) {
324       logprintf ("delete stdin\n");
325       event_free (term_ev);
326       term_ev = 0;
327     }
328 
329     #ifdef USE_LUA
330       lua_do_all ();
331     #endif
332 
333     #ifdef USE_PYTHON
334       py_do_all ();
335     #endif
336 
337     if (safe_quit && !TLS->active_queries) {
338       printf ("All done. Exit\n");
339       do_halt (0);
340       safe_quit = 0;
341     }
342     if (sigterm_cnt > 0) {
343       do_halt (0);
344     }
345     if (time (0) - last_get_state > 3600) {
346       tgl_do_lookup_state (TLS);
347       last_get_state = time (0);
348     }
349 
350     write_state_file ();
351     update_prompt ();
352 
353 /*    if (unknown_user_list_pos) {
354       int i;
355       for (i = 0; i < unknown_user_list_pos; i++) {
356         tgl_do_get_user_info (TLS, TGL_MK_USER (unknown_user_list[i]), 0, 0, 0);
357       }
358       unknown_user_list_pos = 0;
359     }   */
360   }
361 
362   if (term_ev) {
363     event_free (term_ev);
364     term_ev = 0;
365   }
366 
367   if (verbosity >= E_DEBUG) {
368     logprintf ("End of netloop\n");
369   }
370 }
371 
372 struct tgl_dc *cur_a_dc;
is_authorized(void)373 int is_authorized (void) {
374   return tgl_authorized_dc (TLS, cur_a_dc);
375 }
376 
all_authorized(void)377 int all_authorized (void) {
378   int i;
379   for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i]) {
380     if (!tgl_authorized_dc (TLS, TLS->DC_list[i])) {
381       return 0;
382     }
383   }
384   return 1;
385 }
386 
387 int zero[512];
388 
389 
390 int readline_active;
391 int new_dc_num;
392 int wait_dialog_list;
393 
394 extern struct tgl_update_callback upd_cb;
395 
396 #define DC_SERIALIZED_MAGIC 0x868aa81d
397 #define STATE_FILE_MAGIC 0x28949a93
398 #define SECRET_CHAT_FILE_MAGIC 0x37a1988a
399 
400 char *get_auth_key_filename (void);
401 char *get_state_filename (void);
402 char *get_secret_chat_filename (void);
403 
read_state_file(void)404 void read_state_file (void) {
405   if (binlog_enabled) { return; }
406   int state_file_fd = open (get_state_filename (), O_CREAT | O_RDWR, 0600);
407   if (state_file_fd < 0) {
408     return;
409   }
410   int version, magic;
411   if (read (state_file_fd, &magic, 4) < 4) { close (state_file_fd); return; }
412   if (magic != (int)STATE_FILE_MAGIC) { close (state_file_fd); return; }
413   if (read (state_file_fd, &version, 4) < 4) { close (state_file_fd); return; }
414   assert (version >= 0);
415   int x[4];
416   if (read (state_file_fd, x, 16) < 16) {
417     close (state_file_fd);
418     return;
419   }
420   int pts = x[0];
421   int qts = x[1];
422   int seq = x[2];
423   int date = x[3];
424   close (state_file_fd);
425   bl_do_set_seq (TLS, seq);
426   bl_do_set_pts (TLS, pts);
427   bl_do_set_qts (TLS, qts);
428   bl_do_set_date (TLS, date);
429 }
430 
431 
write_state_file(void)432 void write_state_file (void) {
433   if (binlog_enabled) { return; }
434   static int wseq;
435   static int wpts;
436   static int wqts;
437   static int wdate;
438   if (wseq >= TLS->seq && wpts >= TLS->pts && wqts >= TLS->qts && wdate >= TLS->date) { return; }
439   wseq = TLS->seq; wpts = TLS->pts; wqts = TLS->qts; wdate = TLS->date;
440   int state_file_fd = open (get_state_filename (), O_CREAT | O_RDWR, 0600);
441   if (state_file_fd < 0) {
442     logprintf ("Can not write state file '%s': %m\n", get_state_filename ());
443     do_halt (1);
444   }
445   int x[6];
446   x[0] = STATE_FILE_MAGIC;
447   x[1] = 0;
448   x[2] = wpts;
449   x[3] = wqts;
450   x[4] = wseq;
451   x[5] = wdate;
452   assert (write (state_file_fd, x, 24) == 24);
453   close (state_file_fd);
454 }
455 
write_dc(struct tgl_dc * DC,void * extra)456 void write_dc (struct tgl_dc *DC, void *extra) {
457   int auth_file_fd = *(int *)extra;
458   if (!DC) {
459     int x = 0;
460     assert (write (auth_file_fd, &x, 4) == 4);
461     return;
462   } else {
463     int x = 1;
464     assert (write (auth_file_fd, &x, 4) == 4);
465   }
466 
467   assert (DC->flags & TGLDCF_LOGGED_IN);
468 
469   assert (write (auth_file_fd, &DC->options[0]->port, 4) == 4);
470   int l = strlen (DC->options[0]->ip);
471   assert (write (auth_file_fd, &l, 4) == 4);
472   assert (write (auth_file_fd, DC->options[0]->ip, l) == l);
473   assert (write (auth_file_fd, &DC->auth_key_id, 8) == 8);
474   assert (write (auth_file_fd, DC->auth_key, 256) == 256);
475 }
476 
write_auth_file(void)477 void write_auth_file (void) {
478   if (binlog_enabled) { return; }
479   int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, 0600);
480   assert (auth_file_fd >= 0);
481   int x = DC_SERIALIZED_MAGIC;
482   assert (write (auth_file_fd, &x, 4) == 4);
483   assert (write (auth_file_fd, &TLS->max_dc_num, 4) == 4);
484   assert (write (auth_file_fd, &TLS->dc_working_num, 4) == 4);
485 
486   tgl_dc_iterator_ex (TLS, write_dc, &auth_file_fd);
487 
488   assert (write (auth_file_fd, &TLS->our_id.peer_id, 4) == 4);
489   close (auth_file_fd);
490 }
491 
write_secret_chat(tgl_peer_t * Peer,void * extra)492 void write_secret_chat (tgl_peer_t *Peer, void *extra) {
493   struct tgl_secret_chat *P = (void *)Peer;
494   if (tgl_get_peer_type (P->id) != TGL_PEER_ENCR_CHAT) { return; }
495   if (P->state != sc_ok) { return; }
496   int *a = extra;
497   int fd = a[0];
498   a[1] ++;
499 
500   int id = tgl_get_peer_id (P->id);
501   assert (write (fd, &id, 4) == 4);
502   //assert (write (fd, &P->flags, 4) == 4);
503   int l = strlen (P->print_name);
504   assert (write (fd, &l, 4) == 4);
505   assert (write (fd, P->print_name, l) == l);
506   assert (write (fd, &P->user_id, 4) == 4);
507   assert (write (fd, &P->admin_id, 4) == 4);
508   assert (write (fd, &P->date, 4) == 4);
509   assert (write (fd, &P->ttl, 4) == 4);
510   assert (write (fd, &P->layer, 4) == 4);
511   assert (write (fd, &P->access_hash, 8) == 8);
512   assert (write (fd, &P->state, 4) == 4);
513   assert (write (fd, &P->key_fingerprint, 8) == 8);
514   assert (write (fd, &P->key, 256) == 256);
515   assert (write (fd, &P->first_key_sha, 20) == 20);
516   assert (write (fd, &P->in_seq_no, 4) == 4);
517   assert (write (fd, &P->last_in_seq_no, 4) == 4);
518   assert (write (fd, &P->out_seq_no, 4) == 4);
519 }
520 
write_secret_chat_file(void)521 void write_secret_chat_file (void) {
522   if (binlog_enabled) { return; }
523   int secret_chat_fd = open (get_secret_chat_filename (), O_CREAT | O_RDWR, 0600);
524   assert (secret_chat_fd >= 0);
525   int x = SECRET_CHAT_FILE_MAGIC;
526   assert (write (secret_chat_fd, &x, 4) == 4);
527   x = 2;
528   assert (write (secret_chat_fd, &x, 4) == 4); // version
529   assert (write (secret_chat_fd, &x, 4) == 4); // num
530 
531   int y[2];
532   y[0] = secret_chat_fd;
533   y[1] = 0;
534 
535   tgl_peer_iterator_ex (TLS, write_secret_chat, y);
536 
537   lseek (secret_chat_fd, 8, SEEK_SET);
538   assert (write (secret_chat_fd, &y[1], 4) == 4);
539   close (secret_chat_fd);
540 }
541 
read_dc(int auth_file_fd,int id,unsigned ver)542 void read_dc (int auth_file_fd, int id, unsigned ver) {
543   int port = 0;
544   assert (read (auth_file_fd, &port, 4) == 4);
545   int l = 0;
546   assert (read (auth_file_fd, &l, 4) == 4);
547   assert (l >= 0 && l < 100);
548   char ip[100];
549   assert (read (auth_file_fd, ip, l) == l);
550   ip[l] = 0;
551 
552   long long auth_key_id;
553   static unsigned char auth_key[256];
554   assert (read (auth_file_fd, &auth_key_id, 8) == 8);
555   assert (read (auth_file_fd, auth_key, 256) == 256);
556 
557   //bl_do_add_dc (id, ip, l, port, auth_key_id, auth_key);
558   bl_do_dc_option (TLS, 0, id, "DC", 2, ip, l, port);
559   bl_do_set_auth_key (TLS, id, auth_key);
560   bl_do_dc_signed (TLS, id);
561 }
562 
empty_auth_file(void)563 void empty_auth_file (void) {
564   if (TLS->test_mode) {
565     bl_do_dc_option (TLS, 0, 1, "", 0, TG_SERVER_TEST_1, strlen (TG_SERVER_TEST_1), 443);
566     bl_do_dc_option (TLS, 0, 2, "", 0, TG_SERVER_TEST_2, strlen (TG_SERVER_TEST_2), 443);
567     bl_do_dc_option (TLS, 0, 3, "", 0, TG_SERVER_TEST_3, strlen (TG_SERVER_TEST_3), 443);
568     bl_do_set_working_dc (TLS, TG_SERVER_TEST_DEFAULT);
569   } else {
570     bl_do_dc_option (TLS, 0, 1, "", 0, TG_SERVER_1, strlen (TG_SERVER_1), 443);
571     bl_do_dc_option (TLS, 0, 2, "", 0, TG_SERVER_2, strlen (TG_SERVER_2), 443);
572     bl_do_dc_option (TLS, 0, 3, "", 0, TG_SERVER_3, strlen (TG_SERVER_3), 443);
573     bl_do_dc_option (TLS, 0, 4, "", 0, TG_SERVER_4, strlen (TG_SERVER_4), 443);
574     bl_do_dc_option (TLS, 0, 5, "", 0, TG_SERVER_5, strlen (TG_SERVER_5), 443);
575 
576     bl_do_dc_option (TLS, 1, 1, "", 0, TG_SERVER_IPV6_1, strlen (TG_SERVER_IPV6_1), 443);
577     bl_do_dc_option (TLS, 1, 2, "", 0, TG_SERVER_IPV6_2, strlen (TG_SERVER_IPV6_2), 443);
578     bl_do_dc_option (TLS, 1, 3, "", 0, TG_SERVER_IPV6_3, strlen (TG_SERVER_IPV6_3), 443);
579     bl_do_dc_option (TLS, 1, 4, "", 0, TG_SERVER_IPV6_4, strlen (TG_SERVER_IPV6_4), 443);
580     bl_do_dc_option (TLS, 1, 5, "", 0, TG_SERVER_IPV6_5, strlen (TG_SERVER_IPV6_5), 443);
581 
582     bl_do_set_working_dc (TLS, TG_SERVER_DEFAULT);
583   }
584 }
585 
586 int need_dc_list_update;
read_auth_file(void)587 void read_auth_file (void) {
588   if (binlog_enabled) { return; }
589   int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, 0600);
590   if (auth_file_fd < 0) {
591     empty_auth_file ();
592     return;
593   }
594   assert (auth_file_fd >= 0);
595   unsigned x;
596   unsigned m;
597   if (read (auth_file_fd, &m, 4) < 4 || (m != DC_SERIALIZED_MAGIC)) {
598     close (auth_file_fd);
599     empty_auth_file ();
600     return;
601   }
602   assert (read (auth_file_fd, &x, 4) == 4);
603   assert (x > 0);
604   int dc_working_num;
605   assert (read (auth_file_fd, &dc_working_num, 4) == 4);
606 
607   int i;
608   for (i = 0; i <= (int)x; i++) {
609     int y;
610     assert (read (auth_file_fd, &y, 4) == 4);
611     if (y) {
612       read_dc (auth_file_fd, i, m);
613     }
614   }
615   bl_do_set_working_dc (TLS, dc_working_num);
616   int our_id;
617   int l = read (auth_file_fd, &our_id, 4);
618   if (l < 4) {
619     assert (!l);
620   }
621   if (our_id) {
622     bl_do_set_our_id (TLS, TGL_MK_USER (our_id));
623   }
624   close (auth_file_fd);
625 }
626 
read_secret_chat(int fd,int v)627 void read_secret_chat (int fd, int v) {
628   int id, l, user_id, admin_id, date, ttl, layer, state;
629   long long access_hash, key_fingerprint;
630   static char s[1000];
631   static unsigned char key[256];
632   static unsigned char sha[20];
633   assert (read (fd, &id, 4) == 4);
634   //assert (read (fd, &flags, 4) == 4);
635   assert (read (fd, &l, 4) == 4);
636   assert (l > 0 && l < 1000);
637   assert (read (fd, s, l) == l);
638   assert (read (fd, &user_id, 4) == 4);
639   assert (read (fd, &admin_id, 4) == 4);
640   assert (read (fd, &date, 4) == 4);
641   assert (read (fd, &ttl, 4) == 4);
642   assert (read (fd, &layer, 4) == 4);
643   assert (read (fd, &access_hash, 8) == 8);
644   assert (read (fd, &state, 4) == 4);
645   assert (read (fd, &key_fingerprint, 8) == 8);
646   assert (read (fd, &key, 256) == 256);
647   assert (read (fd, sha, 20) == 20);
648   int in_seq_no = 0, out_seq_no = 0, last_in_seq_no = 0;
649   if (v >= 1) {
650     assert (read (fd, &in_seq_no, 4) == 4);
651     assert (read (fd, &last_in_seq_no, 4) == 4);
652     assert (read (fd, &out_seq_no, 4) == 4);
653   }
654 
655   bl_do_encr_chat (TLS, id,
656     &access_hash,
657     &date,
658     &admin_id,
659     &user_id,
660     key,
661     NULL,
662     sha,
663     &state,
664     &ttl,
665     &layer,
666     &in_seq_no,
667     &last_in_seq_no,
668     &out_seq_no,
669     &key_fingerprint,
670     TGLECF_CREATE | TGLECF_CREATED,
671     NULL, 0
672   );
673 
674 }
675 
read_secret_chat_file(void)676 void read_secret_chat_file (void) {
677   if (binlog_enabled) { return; }
678   int secret_chat_fd = open (get_secret_chat_filename (), O_RDWR, 0600);
679   if (secret_chat_fd < 0) { return; }
680   //assert (secret_chat_fd >= 0);
681   int x;
682   if (read (secret_chat_fd, &x, 4) < 4) { close (secret_chat_fd); return; }
683   if (x != SECRET_CHAT_FILE_MAGIC) { close (secret_chat_fd); return; }
684   int v = 0;
685   assert (read (secret_chat_fd, &v, 4) == 4);
686   assert (v == 0 || v == 1 || v == 2); // version
687   assert (read (secret_chat_fd, &x, 4) == 4);
688   assert (x >= 0);
689   while (x --> 0) {
690     read_secret_chat (secret_chat_fd, v);
691   }
692   close (secret_chat_fd);
693 }
694 
read_incoming(struct bufferevent * bev,void * _arg)695 static void read_incoming (struct bufferevent *bev, void *_arg) {
696   vlogprintf (E_WARNING, "Read from incoming connection\n");
697   struct in_ev *ev = _arg;
698   assert (ev->bev == bev);
699   ev->in_buf_pos += bufferevent_read (bev, ev->in_buf + ev->in_buf_pos, 4096 - ev->in_buf_pos);
700 
701   while (1) {
702     int pos = 0;
703     int ok = 0;
704     while (pos < ev->in_buf_pos) {
705       if (ev->in_buf[pos] == '\n') {
706         if (!ev->error) {
707           ev->in_buf[pos] = 0;
708           interpreter_ex (ev->in_buf, ev);
709         } else {
710           ev->error = 0;
711         }
712         ok = 1;
713         ev->in_buf_pos -= (pos + 1);
714         memmove (ev->in_buf, ev->in_buf + pos + 1, ev->in_buf_pos);
715         pos = 0;
716       } else {
717         pos ++;
718       }
719     }
720     if (ok) {
721       ev->in_buf_pos += bufferevent_read (bev, ev->in_buf + ev->in_buf_pos, 4096 - ev->in_buf_pos);
722     } else {
723       if (ev->in_buf_pos == 4096) {
724         ev->error = 1;
725       }
726       break;
727     }
728   }
729 }
730 
event_incoming(struct bufferevent * bev,short what,void * _arg)731 void event_incoming (struct bufferevent *bev, short what, void *_arg) {
732   struct in_ev *ev = _arg;
733   if (what & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
734     vlogprintf (E_WARNING, "Closing incoming connection\n");
735     assert (ev->fd >= 0);
736     close (ev->fd);
737     bufferevent_free (bev);
738     ev->bev = 0;
739     if (!--ev->refcnt) { free (ev); }
740   }
741 }
742 
accept_incoming(evutil_socket_t efd,short what,void * arg)743 static void accept_incoming (evutil_socket_t efd, short what, void *arg) {
744   vlogprintf (E_WARNING, "Accepting incoming connection\n");
745   socklen_t clilen = 0;
746   struct sockaddr_in cli_addr;
747   int fd = accept (efd, (struct sockaddr *)&cli_addr, &clilen);
748 
749   assert (fd >= 0);
750   struct bufferevent *bev = bufferevent_socket_new (TLS->ev_base, fd, 0);
751   struct in_ev *e = malloc (sizeof (*e));
752   e->bev = bev;
753   e->refcnt = 1;
754   e->in_buf_pos = 0;
755   e->error = 0;
756   e->fd = fd;
757   bufferevent_setcb (bev, read_incoming, 0, event_incoming, e);
758   bufferevent_enable (bev, EV_READ | EV_WRITE);
759 }
760 
761 char *get_downloads_directory (void);
on_login(struct tgl_state * TLS)762 void on_login (struct tgl_state *TLS) {
763   write_auth_file ();
764 }
765 
on_failed_login(struct tgl_state * TLS)766 void on_failed_login (struct tgl_state *TLS) {
767   logprintf ("login failed\n");
768   logprintf ("login error #%d: %s\n", TLS->error_code, TLS->error);
769   logprintf ("you can relogin by deleting auth file or running telegram-cli with '-q' flag\n");
770   exit (2);
771 }
772 
773 void on_started (struct tgl_state *TLS);
clist_cb(struct tgl_state * TLSR,void * callback_extra,int success,int size,tgl_peer_id_t peers[],tgl_message_id_t * last_msg_id[],int unread_count[])774 void clist_cb (struct tgl_state *TLSR, void *callback_extra, int success, int size, tgl_peer_id_t peers[], tgl_message_id_t *last_msg_id[], int unread_count[]) {
775   on_started (TLS);
776 }
777 
dlist_cb(struct tgl_state * TLSR,void * callback_extra,int success,int size,tgl_peer_id_t peers[],tgl_message_id_t * last_msg_id[],int unread_count[])778 void dlist_cb (struct tgl_state *TLSR, void *callback_extra, int success, int size, tgl_peer_id_t peers[], tgl_message_id_t *last_msg_id[], int unread_count[])  {
779   tgl_do_get_channels_dialog_list (TLS, 100, 0, clist_cb, 0);
780 }
781 
on_started(struct tgl_state * TLS)782 void on_started (struct tgl_state *TLS) {
783   if (wait_dialog_list) {
784     wait_dialog_list = 0;
785     tgl_do_get_dialog_list (TLS, 100, 0, dlist_cb, 0);
786     return;
787   }
788   #ifdef USE_LUA
789     lua_diff_end ();
790   #endif
791 
792   #ifdef USE_PYTHON
793     py_diff_end ();
794   #endif
795 
796   if (start_command) {
797     safe_quit = 1;
798     while (*start_command) {
799       char *start = start_command;
800       while (*start_command && *start_command != '\n') {
801         start_command ++;
802       }
803       if (*start_command) {
804         *start_command = 0;
805         start_command ++;
806       }
807       interpreter_ex (start, 0);
808     }
809   }
810 }
811 
loop(void)812 int loop (void) {
813   tgl_set_callback (TLS, &upd_cb);
814   struct event_base *ev = event_base_new ();
815   tgl_set_ev_base (TLS, ev);
816   tgl_set_net_methods (TLS, &tgl_conn_methods);
817   tgl_set_timer_methods (TLS, &tgl_libevent_timers);
818   assert (TLS->timer_methods);
819   tgl_set_download_directory (TLS, get_downloads_directory ());
820   tgl_register_app_id (TLS, TELEGRAM_CLI_APP_ID, TELEGRAM_CLI_APP_HASH);
821   tgl_set_app_version (TLS, "Telegram-cli " TELEGRAM_CLI_VERSION);
822   if (ipv6_enabled) {
823     tgl_enable_ipv6 (TLS);
824   }
825   if (bot_mode) {
826     tgl_enable_bot (TLS);
827   }
828   if (disable_link_preview) {
829     tgl_disable_link_preview (TLS);
830   }
831   assert (tgl_init (TLS) >= 0);
832 
833   /*if (binlog_enabled) {
834     double t = tglt_get_double_time ();
835     if (verbosity >= E_DEBUG) {
836       logprintf ("replay log start\n");
837     }
838     tgl_replay_log (TLS);
839     if (verbosity >= E_DEBUG) {
840       logprintf ("replay log end in %lf seconds\n", tglt_get_double_time () - t);
841     }
842     tgl_reopen_binlog_for_writing (TLS);
843   } else {*/
844     read_auth_file ();
845     read_state_file ();
846     read_secret_chat_file ();
847   //}
848 
849   binlog_read = 1;
850   #ifdef USE_LUA
851     lua_binlog_end ();
852   #endif
853 
854   #ifdef USE_PYTHON
855     py_binlog_end ();
856   #endif
857 
858   if (sfd >= 0) {
859     struct event *ev = event_new (TLS->ev_base, sfd, EV_READ | EV_PERSIST, accept_incoming, 0);
860     event_add (ev, 0);
861   }
862   if (usfd >= 0) {
863     struct event *ev = event_new (TLS->ev_base, usfd, EV_READ | EV_PERSIST, accept_incoming, 0);
864     event_add (ev, 0);
865   }
866   update_prompt ();
867 
868   if (reset_authorization) {
869     tgl_peer_t *P = tgl_peer_get (TLS, TLS->our_id);
870     if (P && P->user.phone && reset_authorization == 1) {
871       set_default_username (P->user.phone);
872     }
873     bl_do_reset_authorization (TLS);
874   }
875 
876   set_interface_callbacks ();
877   tgl_login (TLS);
878   net_loop ();
879   return 0;
880 }
881 
882