2013-10-23 10:26:17 -04:00
|
|
|
/*
|
|
|
|
This file is part of telegram-client.
|
|
|
|
|
|
|
|
Telegram-client 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-client 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-client. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright Vitaly Valtman 2013
|
|
|
|
*/
|
2014-01-13 08:05:25 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2013-11-09 17:47:19 -05:00
|
|
|
#include "config.h"
|
2014-01-13 08:05:25 -05:00
|
|
|
#endif
|
|
|
|
|
2014-01-13 08:58:14 -05:00
|
|
|
#define _GNU_SOURCE
|
2014-01-13 08:05:25 -05:00
|
|
|
#define READLINE_CALLBACKS
|
2013-10-03 08:38:25 -04:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-11-09 17:47:19 -05:00
|
|
|
#ifdef READLINE_GNU
|
2013-10-03 08:38:25 -04:00
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
2013-11-09 17:47:19 -05:00
|
|
|
#else
|
2013-11-11 06:34:45 -05:00
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
2013-11-09 17:47:19 -05:00
|
|
|
#endif
|
2013-10-03 08:38:25 -04:00
|
|
|
|
2013-10-03 12:09:06 -04:00
|
|
|
#include <errno.h>
|
2013-10-11 16:52:20 -04:00
|
|
|
#include <poll.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2013-10-03 12:09:06 -04:00
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
#include "interface.h"
|
2013-10-11 16:52:20 -04:00
|
|
|
#include "telegram.h"
|
2013-11-04 12:34:27 -05:00
|
|
|
#include "loop.h"
|
2014-01-11 19:43:29 -05:00
|
|
|
#include "lua-tg.h"
|
2014-08-13 08:56:55 -04:00
|
|
|
#include "tgl.h"
|
2013-10-11 16:52:20 -04:00
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
extern char *default_username;
|
|
|
|
extern char *auth_token;
|
|
|
|
void set_default_username (const char *s);
|
2013-11-12 19:11:25 -05:00
|
|
|
extern int binlog_enabled;
|
2013-10-03 08:38:25 -04:00
|
|
|
|
2013-10-23 06:24:59 -04:00
|
|
|
extern int unknown_user_list_pos;
|
|
|
|
extern int unknown_user_list[];
|
2013-11-21 14:35:49 -05:00
|
|
|
int register_mode;
|
2013-12-21 06:53:32 -05:00
|
|
|
extern int safe_quit;
|
|
|
|
extern int queries_num;
|
2013-10-03 08:38:25 -04:00
|
|
|
|
2013-11-09 17:47:19 -05:00
|
|
|
void got_it (char *line, int len);
|
2013-10-11 16:52:20 -04:00
|
|
|
void net_loop (int flags, int (*is_end)(void)) {
|
|
|
|
while (!is_end ()) {
|
|
|
|
struct pollfd fds[101];
|
|
|
|
int cc = 0;
|
2013-11-09 17:47:19 -05:00
|
|
|
if (flags & 3) {
|
2013-10-11 16:52:20 -04:00
|
|
|
fds[0].fd = 0;
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
cc ++;
|
|
|
|
}
|
2013-10-03 08:38:25 -04:00
|
|
|
|
2014-08-14 14:03:33 -04:00
|
|
|
//write_state_file ();
|
2013-10-11 16:52:20 -04:00
|
|
|
int x = connections_make_poll_array (fds + cc, 101 - cc) + cc;
|
|
|
|
double timer = next_timer_in ();
|
|
|
|
if (timer > 1000) { timer = 1000; }
|
|
|
|
if (poll (fds, x, timer) < 0) {
|
|
|
|
work_timers ();
|
|
|
|
continue;
|
2013-10-03 12:09:06 -04:00
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
work_timers ();
|
2013-11-09 17:47:19 -05:00
|
|
|
if ((flags & 3) && (fds[0].revents & POLLIN)) {
|
2014-08-13 11:55:16 -04:00
|
|
|
tgl_state.unread_messages = 0;
|
2013-11-09 17:47:19 -05:00
|
|
|
if (flags & 1) {
|
|
|
|
rl_callback_read_char ();
|
|
|
|
} else {
|
|
|
|
char *line = 0;
|
|
|
|
size_t len = 0;
|
|
|
|
assert (getline (&line, &len, stdin) >= 0);
|
|
|
|
got_it (line, strlen (line));
|
|
|
|
}
|
2013-10-03 12:09:06 -04:00
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
connections_poll_result (fds + cc, x - cc);
|
2014-01-11 19:43:29 -05:00
|
|
|
#ifdef USE_LUA
|
|
|
|
lua_do_all ();
|
|
|
|
#endif
|
2013-12-21 06:53:32 -05:00
|
|
|
if (safe_quit && !queries_num) {
|
|
|
|
printf ("All done. Exit\n");
|
|
|
|
rl_callback_handler_remove ();
|
|
|
|
exit (0);
|
|
|
|
}
|
2013-10-23 06:24:59 -04:00
|
|
|
if (unknown_user_list_pos) {
|
2014-08-13 11:55:16 -04:00
|
|
|
tgl_do_get_user_list_info_silent (unknown_user_list_pos, unknown_user_list);
|
2013-10-23 06:24:59 -04:00
|
|
|
unknown_user_list_pos = 0;
|
2013-12-21 06:53:32 -05:00
|
|
|
}
|
2013-10-03 12:09:06 -04:00
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
|
2013-10-27 19:10:40 -04:00
|
|
|
char **_s;
|
|
|
|
size_t *_l;
|
|
|
|
int got_it_ok;
|
|
|
|
|
2013-11-09 17:47:19 -05:00
|
|
|
void got_it (char *line, int len) {
|
2013-12-18 10:21:49 -05:00
|
|
|
assert (len > 0);
|
2013-11-09 17:47:19 -05:00
|
|
|
line[-- len] = 0; // delete end of line
|
|
|
|
*_s = line;
|
|
|
|
*_l = len;
|
2013-10-27 19:10:40 -04:00
|
|
|
got_it_ok = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int is_got_it (void) {
|
|
|
|
return got_it_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
int net_getline (char **s, size_t *l) {
|
2013-11-09 17:47:19 -05:00
|
|
|
fflush (stdout);
|
|
|
|
// rl_already_prompted = 1;
|
2013-10-27 19:10:40 -04:00
|
|
|
got_it_ok = 0;
|
|
|
|
_s = s;
|
|
|
|
_l = l;
|
2013-11-09 17:47:19 -05:00
|
|
|
// rl_callback_handler_install (0, got_it);
|
|
|
|
net_loop (2, is_got_it);
|
2013-10-27 19:10:40 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
int ret1 (void) { return 0; }
|
|
|
|
|
|
|
|
int main_loop (void) {
|
|
|
|
net_loop (1, ret1);
|
2013-10-03 12:09:06 -04:00
|
|
|
return 0;
|
2013-10-03 08:38:25 -04:00
|
|
|
}
|
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
char *get_auth_key_filename (void);
|
2013-11-04 12:34:27 -05:00
|
|
|
char *get_state_filename (void);
|
|
|
|
char *get_secret_chat_filename (void);
|
2013-10-11 16:52:20 -04:00
|
|
|
int zero[512];
|
|
|
|
|
2013-10-27 19:10:40 -04:00
|
|
|
extern int max_chat_size;
|
|
|
|
int mcs (void) {
|
|
|
|
return max_chat_size;
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
extern int difference_got;
|
|
|
|
int dgot (void) {
|
2014-02-03 10:50:57 -05:00
|
|
|
return difference_got;
|
|
|
|
}
|
|
|
|
int dlgot (void) {
|
|
|
|
return dialog_list_got;
|
2013-11-04 12:34:27 -05:00
|
|
|
}
|
|
|
|
|
2013-10-13 06:18:08 -04:00
|
|
|
int readline_active;
|
2013-10-27 19:10:40 -04:00
|
|
|
int new_dc_num;
|
2014-02-03 10:50:57 -05:00
|
|
|
int wait_dialog_list;
|
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
int loop (void) {
|
2014-08-14 14:03:33 -04:00
|
|
|
//on_start ();
|
|
|
|
tgl_init ();
|
|
|
|
|
|
|
|
double t = get_double_time ();
|
|
|
|
logprintf ("replay log start\n");
|
|
|
|
tgl_replay_log ();
|
|
|
|
logprintf ("replay log end in %lf seconds\n", get_double_time () - t);
|
|
|
|
tgl_reopen_binlog_for_writing ();
|
|
|
|
#ifdef USE_LUA
|
|
|
|
lua_binlog_end ();
|
|
|
|
#endif
|
2013-11-09 17:47:19 -05:00
|
|
|
update_prompt ();
|
2013-10-27 19:10:40 -04:00
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
assert (DC_list[dc_working_num]);
|
2013-11-14 19:08:24 -05:00
|
|
|
if (!DC_working || !DC_working->auth_key_id) {
|
|
|
|
// if (auth_state == 0) {
|
2013-10-27 19:10:40 -04:00
|
|
|
DC_working = DC_list[dc_working_num];
|
|
|
|
assert (!DC_working->auth_key_id);
|
2013-10-11 16:52:20 -04:00
|
|
|
dc_authorize (DC_working);
|
2013-10-27 19:10:40 -04:00
|
|
|
assert (DC_working->auth_key_id);
|
|
|
|
auth_state = 100;
|
|
|
|
write_auth_file ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbosity) {
|
|
|
|
logprintf ("Requesting info about DC...\n");
|
|
|
|
}
|
2014-08-13 11:55:16 -04:00
|
|
|
tgl_do_help_get_config ();
|
2013-10-27 19:10:40 -04:00
|
|
|
net_loop (0, mcs);
|
|
|
|
if (verbosity) {
|
|
|
|
logprintf ("DC_info: %d new DC got\n", new_dc_num);
|
|
|
|
}
|
2013-10-27 19:24:03 -04:00
|
|
|
int i;
|
|
|
|
for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->auth_key_id) {
|
|
|
|
dc_authorize (DC_list[i]);
|
|
|
|
assert (DC_list[i]->auth_key_id);
|
|
|
|
write_auth_file ();
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
2013-10-27 19:10:40 -04:00
|
|
|
|
2013-11-14 19:08:24 -05:00
|
|
|
if (auth_state == 100 || !(DC_working->has_auth)) {
|
2013-10-11 16:52:20 -04:00
|
|
|
if (!default_username) {
|
|
|
|
size_t size = 0;
|
|
|
|
char *user = 0;
|
|
|
|
|
2013-10-27 19:10:40 -04:00
|
|
|
if (!user) {
|
2013-10-11 16:52:20 -04:00
|
|
|
printf ("Telephone number (with '+' sign): ");
|
2013-10-27 19:10:40 -04:00
|
|
|
if (net_getline (&user, &size) == -1) {
|
2013-10-11 16:52:20 -04:00
|
|
|
perror ("getline()");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
set_default_username (user);
|
|
|
|
}
|
2013-10-03 12:10:53 -04:00
|
|
|
}
|
2014-08-13 11:55:16 -04:00
|
|
|
int res = tgl_do_auth_check_phone (default_username);
|
2013-10-27 19:10:40 -04:00
|
|
|
assert (res >= 0);
|
|
|
|
logprintf ("%s\n", res > 0 ? "phone registered" : "phone not registered");
|
2013-11-21 14:35:49 -05:00
|
|
|
if (res > 0 && !register_mode) {
|
2014-08-13 11:55:16 -04:00
|
|
|
tgl_do_send_code (default_username);
|
2013-10-27 19:10:40 -04:00
|
|
|
char *code = 0;
|
|
|
|
size_t size = 0;
|
2014-02-24 06:27:30 -05:00
|
|
|
printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): ");
|
2013-10-27 19:10:40 -04:00
|
|
|
while (1) {
|
|
|
|
if (net_getline (&code, &size) == -1) {
|
|
|
|
perror ("getline()");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2014-02-24 06:27:30 -05:00
|
|
|
if (!strcmp (code, "call")) {
|
2014-02-25 15:31:27 -05:00
|
|
|
printf ("You typed \"call\", switching to phone system.\n");
|
2014-08-13 11:55:16 -04:00
|
|
|
tgl_do_phone_call (default_username);
|
2014-02-24 01:59:20 -05:00
|
|
|
printf ("Calling you! Code: ");
|
|
|
|
continue;
|
|
|
|
}
|
2014-08-13 11:55:16 -04:00
|
|
|
if (tgl_do_send_code_result (code) >= 0) {
|
2013-10-27 19:10:40 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf ("Invalid code. Try again: ");
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree_str (code);
|
2013-10-27 19:10:40 -04:00
|
|
|
}
|
|
|
|
auth_state = 300;
|
|
|
|
} else {
|
|
|
|
printf ("User is not registered. Do you want to register? [Y/n] ");
|
|
|
|
char *code;
|
|
|
|
size_t size;
|
|
|
|
if (net_getline (&code, &size) == -1) {
|
2013-10-11 16:52:20 -04:00
|
|
|
perror ("getline()");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2013-11-01 15:57:57 -04:00
|
|
|
if (!*code || *code == 'y' || *code == 'Y') {
|
2013-10-27 19:10:40 -04:00
|
|
|
printf ("Ok, starting registartion.\n");
|
|
|
|
} else {
|
|
|
|
printf ("Then try again\n");
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
char *first_name;
|
2014-02-07 10:40:39 -05:00
|
|
|
printf ("First name: ");
|
2013-10-27 19:10:40 -04:00
|
|
|
if (net_getline (&first_name, &size) == -1) {
|
|
|
|
perror ("getline()");
|
|
|
|
exit (EXIT_FAILURE);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
2013-10-27 19:10:40 -04:00
|
|
|
char *last_name;
|
2014-02-07 10:40:39 -05:00
|
|
|
printf ("Last name: ");
|
2013-10-27 19:10:40 -04:00
|
|
|
if (net_getline (&last_name, &size) == -1) {
|
|
|
|
perror ("getline()");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
int dc_num = tgl_do_get_nearest_dc ();
|
2013-10-27 19:10:40 -04:00
|
|
|
assert (dc_num >= 0 && dc_num <= MAX_DC_NUM && DC_list[dc_num]);
|
|
|
|
dc_working_num = dc_num;
|
|
|
|
DC_working = DC_list[dc_working_num];
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
tgl_do_send_code (default_username);
|
2014-02-24 06:27:30 -05:00
|
|
|
printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): ");
|
2013-10-27 19:10:40 -04:00
|
|
|
while (1) {
|
|
|
|
if (net_getline (&code, &size) == -1) {
|
|
|
|
perror ("getline()");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2014-02-24 06:27:30 -05:00
|
|
|
if (!strcmp (code, "call")) {
|
2014-02-25 15:31:27 -05:00
|
|
|
printf ("You typed \"call\", switching to phone system.\n");
|
2014-08-13 11:55:16 -04:00
|
|
|
tgl_do_phone_call (default_username);
|
2014-02-24 01:59:20 -05:00
|
|
|
printf ("Calling you! Code: ");
|
|
|
|
continue;
|
|
|
|
}
|
2014-08-13 11:55:16 -04:00
|
|
|
if (tgl_do_send_code_result_auth (code, first_name, last_name) >= 0) {
|
2013-10-27 19:10:40 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf ("Invalid code. Try again: ");
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree_str (code);
|
2013-10-27 19:10:40 -04:00
|
|
|
}
|
|
|
|
auth_state = 300;
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
2013-10-03 08:38:25 -04:00
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
|
2013-10-27 19:10:40 -04:00
|
|
|
for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->has_auth) {
|
2014-08-13 11:55:16 -04:00
|
|
|
tgl_do_export_auth (i);
|
|
|
|
tgl_do_import_auth (i);
|
2013-11-21 14:35:49 -05:00
|
|
|
bl_do_dc_signed (i);
|
2013-10-27 19:10:40 -04:00
|
|
|
write_auth_file ();
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
write_auth_file ();
|
2013-10-27 15:35:02 -04:00
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
fflush (stdout);
|
|
|
|
fflush (stderr);
|
2013-10-03 08:38:25 -04:00
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
read_state_file ();
|
|
|
|
read_secret_chat_file ();
|
2013-11-09 17:47:19 -05:00
|
|
|
|
|
|
|
set_interface_callbacks ();
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
tgl_do_get_difference ();
|
2013-11-04 12:34:27 -05:00
|
|
|
net_loop (0, dgot);
|
2014-01-11 19:43:29 -05:00
|
|
|
#ifdef USE_LUA
|
|
|
|
lua_diff_end ();
|
|
|
|
#endif
|
2014-08-13 11:55:16 -04:00
|
|
|
tglm_send_all_unsent ();
|
2013-11-29 16:43:56 -05:00
|
|
|
|
2014-01-11 19:43:29 -05:00
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
tgl_do_get_dialog_list ();
|
2014-02-03 10:50:57 -05:00
|
|
|
if (wait_dialog_list) {
|
|
|
|
dialog_list_got = 0;
|
|
|
|
net_loop (0, dlgot);
|
|
|
|
}
|
2013-10-24 18:32:13 -04:00
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
return main_loop ();
|
|
|
|
}
|
|
|
|
|