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 "net.h"
|
|
|
|
#include "mtproto-client.h"
|
|
|
|
#include "mtproto-common.h"
|
|
|
|
#include "queries.h"
|
|
|
|
#include "telegram.h"
|
2013-11-04 12:34:27 -05:00
|
|
|
#include "loop.h"
|
2013-11-12 19:11:25 -05:00
|
|
|
#include "binlog.h"
|
2014-01-11 19:43:29 -05:00
|
|
|
#include "lua-tg.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;
|
2013-11-08 13:58:28 -05:00
|
|
|
extern int test_dc;
|
2013-10-03 08:38:25 -04:00
|
|
|
void set_default_username (const char *s);
|
2013-10-11 16:52:20 -04:00
|
|
|
int default_dc_num;
|
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-10-24 11:44:54 -04:00
|
|
|
int unread_messages;
|
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
|
|
|
|
2013-11-04 12:34:27 -05: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)) {
|
2013-10-24 11:44:54 -04:00
|
|
|
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) {
|
|
|
|
do_get_user_list_info_silent (unknown_user_list_pos, unknown_user_list);
|
|
|
|
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
|
|
|
|
|
|
|
struct dc *DC_list[MAX_DC_ID + 1];
|
|
|
|
struct dc *DC_working;
|
|
|
|
int dc_working_num;
|
|
|
|
int auth_state;
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
|
|
void write_dc (int auth_file_fd, struct dc *DC) {
|
|
|
|
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);
|
|
|
|
if (DC->flags & 1) {
|
|
|
|
assert (write (auth_file_fd, &DC->auth_key_id, 8) == 8);
|
|
|
|
assert (write (auth_file_fd, DC->auth_key, 256) == 256);
|
|
|
|
} else {
|
|
|
|
assert (write (auth_file_fd, zero, 256 + 8) == 256 + 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (write (auth_file_fd, &DC->server_salt, 8) == 8);
|
2013-10-27 19:10:40 -04:00
|
|
|
assert (write (auth_file_fd, &DC->has_auth, 4) == 4);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
|
2013-10-18 15:30:24 -04:00
|
|
|
int our_id;
|
2013-10-11 16:52:20 -04:00
|
|
|
void write_auth_file (void) {
|
2013-11-14 19:08:24 -05:00
|
|
|
if (binlog_enabled) { return; }
|
2013-10-26 15:57:22 -04:00
|
|
|
int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, 0600);
|
2013-10-11 16:52:20 -04:00
|
|
|
assert (auth_file_fd >= 0);
|
2013-10-27 19:10:40 -04:00
|
|
|
int x = DC_SERIALIZED_MAGIC_V2;
|
2013-10-11 16:52:20 -04:00
|
|
|
assert (write (auth_file_fd, &x, 4) == 4);
|
|
|
|
x = MAX_DC_ID;
|
|
|
|
assert (write (auth_file_fd, &x, 4) == 4);
|
|
|
|
assert (write (auth_file_fd, &dc_working_num, 4) == 4);
|
|
|
|
assert (write (auth_file_fd, &auth_state, 4) == 4);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i <= MAX_DC_ID; i++) {
|
|
|
|
if (DC_list[i]) {
|
|
|
|
x = 1;
|
|
|
|
assert (write (auth_file_fd, &x, 4) == 4);
|
|
|
|
write_dc (auth_file_fd, DC_list[i]);
|
|
|
|
} else {
|
|
|
|
x = 0;
|
|
|
|
assert (write (auth_file_fd, &x, 4) == 4);
|
|
|
|
}
|
|
|
|
}
|
2013-10-18 15:30:24 -04:00
|
|
|
assert (write (auth_file_fd, &our_id, 4) == 4);
|
2013-10-11 16:52:20 -04:00
|
|
|
close (auth_file_fd);
|
|
|
|
}
|
|
|
|
|
2013-10-27 19:10:40 -04:00
|
|
|
void read_dc (int auth_file_fd, int id, unsigned ver) {
|
2013-10-11 16:52:20 -04:00
|
|
|
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);
|
2014-01-10 06:32:57 -05:00
|
|
|
char *ip = talloc (l + 1);
|
2013-10-11 16:52:20 -04:00
|
|
|
assert (read (auth_file_fd, ip, l) == l);
|
|
|
|
ip[l] = 0;
|
|
|
|
struct dc *DC = alloc_dc (id, ip, port);
|
|
|
|
assert (read (auth_file_fd, &DC->auth_key_id, 8) == 8);
|
|
|
|
assert (read (auth_file_fd, &DC->auth_key, 256) == 256);
|
|
|
|
assert (read (auth_file_fd, &DC->server_salt, 8) == 8);
|
|
|
|
if (DC->auth_key_id) {
|
|
|
|
DC->flags |= 1;
|
|
|
|
}
|
2013-10-27 19:10:40 -04:00
|
|
|
if (ver != DC_SERIALIZED_MAGIC) {
|
|
|
|
assert (read (auth_file_fd, &DC->has_auth, 4) == 4);
|
|
|
|
} else {
|
|
|
|
DC->has_auth = 0;
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void empty_auth_file (void) {
|
2014-01-10 06:32:57 -05:00
|
|
|
alloc_dc (1, tstrdup (test_dc ? TG_SERVER_TEST : TG_SERVER), 443);
|
2013-10-11 16:52:20 -04:00
|
|
|
dc_working_num = 1;
|
2013-10-27 19:10:40 -04:00
|
|
|
auth_state = 0;
|
2013-10-11 16:52:20 -04:00
|
|
|
write_auth_file ();
|
|
|
|
}
|
|
|
|
|
2013-10-27 19:10:40 -04:00
|
|
|
int need_dc_list_update;
|
2013-10-11 16:52:20 -04:00
|
|
|
void read_auth_file (void) {
|
2013-11-14 19:08:24 -05:00
|
|
|
if (binlog_enabled) { return; }
|
2013-10-26 15:57:22 -04:00
|
|
|
int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, 0600);
|
2013-10-11 16:52:20 -04:00
|
|
|
if (auth_file_fd < 0) {
|
|
|
|
empty_auth_file ();
|
|
|
|
}
|
|
|
|
assert (auth_file_fd >= 0);
|
2013-10-27 19:10:40 -04:00
|
|
|
unsigned x;
|
|
|
|
unsigned m;
|
|
|
|
if (read (auth_file_fd, &m, 4) < 4 || (m != DC_SERIALIZED_MAGIC && m != DC_SERIALIZED_MAGIC_V2)) {
|
2013-10-11 16:52:20 -04:00
|
|
|
close (auth_file_fd);
|
|
|
|
empty_auth_file ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert (read (auth_file_fd, &x, 4) == 4);
|
2013-10-27 19:10:40 -04:00
|
|
|
assert (x <= MAX_DC_ID);
|
2013-10-11 16:52:20 -04:00
|
|
|
assert (read (auth_file_fd, &dc_working_num, 4) == 4);
|
|
|
|
assert (read (auth_file_fd, &auth_state, 4) == 4);
|
2013-10-27 19:10:40 -04:00
|
|
|
if (m == DC_SERIALIZED_MAGIC) {
|
|
|
|
auth_state = 700;
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
int i;
|
2013-10-27 19:10:40 -04:00
|
|
|
for (i = 0; i <= (int)x; i++) {
|
2013-10-11 16:52:20 -04:00
|
|
|
int y;
|
|
|
|
assert (read (auth_file_fd, &y, 4) == 4);
|
|
|
|
if (y) {
|
2013-10-27 19:10:40 -04:00
|
|
|
read_dc (auth_file_fd, i, m);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
}
|
2013-10-18 15:30:24 -04:00
|
|
|
int l = read (auth_file_fd, &our_id, 4);
|
|
|
|
if (l < 4) {
|
|
|
|
assert (!l);
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
close (auth_file_fd);
|
2013-10-27 19:10:40 -04:00
|
|
|
DC_working = DC_list[dc_working_num];
|
|
|
|
if (m == DC_SERIALIZED_MAGIC) {
|
|
|
|
DC_working->has_auth = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
int pts, qts, seq, last_date;
|
|
|
|
|
|
|
|
void read_state_file (void) {
|
2013-11-21 16:40:31 -05:00
|
|
|
if (binlog_enabled) { return; }
|
2013-11-04 12:34:27 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
pts = x[0];
|
|
|
|
qts = x[1];
|
|
|
|
seq = x[2];
|
|
|
|
last_date = x[3];
|
|
|
|
close (state_file_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_state_file (void) {
|
2013-11-21 16:40:31 -05:00
|
|
|
if (binlog_enabled) { return; }
|
2013-11-04 12:34:27 -05:00
|
|
|
static int wseq;
|
|
|
|
static int wpts;
|
|
|
|
static int wqts;
|
|
|
|
static int wdate;
|
|
|
|
if (wseq >= seq && wpts >= pts && wqts >= qts && wdate >= last_date) { return; }
|
|
|
|
int state_file_fd = open (get_state_filename (), O_CREAT | O_RDWR, 0600);
|
|
|
|
if (state_file_fd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int x[6];
|
|
|
|
x[0] = STATE_FILE_MAGIC;
|
|
|
|
x[1] = 0;
|
|
|
|
x[2] = pts;
|
|
|
|
x[3] = qts;
|
|
|
|
x[4] = seq;
|
|
|
|
x[5] = last_date;
|
|
|
|
assert (write (state_file_fd, x, 24) == 24);
|
|
|
|
close (state_file_fd);
|
|
|
|
wseq = seq; wpts = pts; wqts = qts; wdate = last_date;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern peer_t *Peers[];
|
|
|
|
extern int peer_num;
|
|
|
|
|
2013-11-05 17:53:40 -05:00
|
|
|
extern int encr_root;
|
|
|
|
extern unsigned char *encr_prime;
|
|
|
|
extern int encr_param_version;
|
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
void read_secret_chat_file (void) {
|
2013-11-14 19:08:24 -05:00
|
|
|
if (binlog_enabled) { return; }
|
2013-11-04 12:34:27 -05:00
|
|
|
int fd = open (get_secret_chat_filename (), O_CREAT | O_RDWR, 0600);
|
|
|
|
if (fd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int x[2];
|
|
|
|
if (read (fd, x, 8) < 8) {
|
|
|
|
close (fd); return;
|
|
|
|
}
|
|
|
|
if (x[0] != (int)SECRET_CHAT_FILE_MAGIC) { close (fd); return; }
|
|
|
|
int version = x[1];
|
|
|
|
assert (version >= 0);
|
|
|
|
int cc;
|
|
|
|
assert (read (fd, &cc, 4) == 4);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < cc; i++) {
|
2014-01-10 06:32:57 -05:00
|
|
|
peer_t *P = talloc0 (sizeof (*P));
|
2013-11-04 12:34:27 -05:00
|
|
|
struct secret_chat *E = &P->encr_chat;
|
|
|
|
int t;
|
|
|
|
assert (read (fd, &t, 4) == 4);
|
|
|
|
P->id = MK_ENCR_CHAT (t);
|
|
|
|
assert (read (fd, &P->flags, 4) == 4);
|
|
|
|
assert (read (fd, &t, 4) == 4);
|
|
|
|
assert (t > 0);
|
2014-01-10 06:32:57 -05:00
|
|
|
P->print_name = talloc (t + 1);
|
2013-11-04 12:34:27 -05:00
|
|
|
assert (read (fd, P->print_name, t) == t);
|
|
|
|
P->print_name[t] = 0;
|
2014-01-31 15:19:55 -05:00
|
|
|
peer_insert_name (P);
|
2013-11-04 12:34:27 -05:00
|
|
|
|
|
|
|
assert (read (fd, &E->state, 4) == 4);
|
|
|
|
assert (read (fd, &E->user_id, 4) == 4);
|
|
|
|
assert (read (fd, &E->admin_id, 4) == 4);
|
|
|
|
assert (read (fd, &E->ttl, 4) == 4);
|
|
|
|
assert (read (fd, &E->access_hash, 8) == 8);
|
|
|
|
|
|
|
|
if (E->state != sc_waiting) {
|
2014-01-10 06:32:57 -05:00
|
|
|
E->g_key = talloc (256);
|
2013-11-04 12:34:27 -05:00
|
|
|
assert (read (fd, E->g_key, 256) == 256);
|
2014-01-10 06:32:57 -05:00
|
|
|
E->nonce = talloc (256);
|
2013-11-05 17:24:26 -05:00
|
|
|
assert (read (fd, E->nonce, 256) == 256);
|
2013-11-04 12:34:27 -05:00
|
|
|
}
|
|
|
|
assert (read (fd, E->key, 256) == 256);
|
|
|
|
assert (read (fd, &E->key_fingerprint, 8) == 8);
|
|
|
|
insert_encrypted_chat (P);
|
|
|
|
}
|
2013-11-05 17:53:40 -05:00
|
|
|
if (version >= 1) {
|
|
|
|
assert (read (fd, &encr_root, 4) == 4);
|
|
|
|
if (encr_root) {
|
|
|
|
assert (read (fd, &encr_param_version, 4) == 4);
|
2014-01-10 06:32:57 -05:00
|
|
|
encr_prime = talloc (256);
|
2013-11-05 17:53:40 -05:00
|
|
|
assert (read (fd, encr_prime, 256) == 256);
|
|
|
|
}
|
|
|
|
}
|
2013-11-04 12:34:27 -05:00
|
|
|
close (fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_secret_chat_file (void) {
|
2013-11-14 19:08:24 -05:00
|
|
|
if (binlog_enabled) { return; }
|
2013-11-04 12:34:27 -05:00
|
|
|
int fd = open (get_secret_chat_filename (), O_CREAT | O_RDWR, 0600);
|
|
|
|
if (fd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int x[2];
|
|
|
|
x[0] = SECRET_CHAT_FILE_MAGIC;
|
2013-11-05 17:53:40 -05:00
|
|
|
x[1] = 1;
|
2013-11-04 12:34:27 -05:00
|
|
|
assert (write (fd, x, 8) == 8);
|
|
|
|
int i;
|
|
|
|
int cc = 0;
|
|
|
|
for (i = 0; i < peer_num; i++) if (get_peer_type (Peers[i]->id) == PEER_ENCR_CHAT) {
|
|
|
|
if (Peers[i]->encr_chat.state != sc_none && Peers[i]->encr_chat.state != sc_deleted) {
|
|
|
|
cc ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert (write (fd, &cc, 4) == 4);
|
|
|
|
for (i = 0; i < peer_num; i++) if (get_peer_type (Peers[i]->id) == PEER_ENCR_CHAT) {
|
|
|
|
if (Peers[i]->encr_chat.state != sc_none && Peers[i]->encr_chat.state != sc_deleted) {
|
|
|
|
int t = get_peer_id (Peers[i]->id);
|
|
|
|
assert (write (fd, &t, 4) == 4);
|
|
|
|
t = Peers[i]->flags;
|
|
|
|
assert (write (fd, &t, 4) == 4);
|
|
|
|
t = strlen (Peers[i]->print_name);
|
|
|
|
assert (write (fd, &t, 4) == 4);
|
|
|
|
assert (write (fd, Peers[i]->print_name, t) == t);
|
|
|
|
|
|
|
|
assert (write (fd, &Peers[i]->encr_chat.state, 4) == 4);
|
|
|
|
|
|
|
|
assert (write (fd, &Peers[i]->encr_chat.user_id, 4) == 4);
|
|
|
|
assert (write (fd, &Peers[i]->encr_chat.admin_id, 4) == 4);
|
|
|
|
assert (write (fd, &Peers[i]->encr_chat.ttl, 4) == 4);
|
|
|
|
assert (write (fd, &Peers[i]->encr_chat.access_hash, 8) == 8);
|
|
|
|
if (Peers[i]->encr_chat.state != sc_waiting) {
|
|
|
|
assert (write (fd, Peers[i]->encr_chat.g_key, 256) == 256);
|
|
|
|
}
|
2013-11-05 17:24:26 -05:00
|
|
|
if (Peers[i]->encr_chat.state != sc_waiting) {
|
|
|
|
assert (write (fd, Peers[i]->encr_chat.nonce, 256) == 256);
|
|
|
|
}
|
2013-11-04 12:34:27 -05:00
|
|
|
assert (write (fd, Peers[i]->encr_chat.key, 256) == 256);
|
|
|
|
assert (write (fd, &Peers[i]->encr_chat.key_fingerprint, 8) == 8);
|
|
|
|
}
|
|
|
|
}
|
2013-11-05 17:53:40 -05:00
|
|
|
assert (write (fd, &encr_root, 4) == 4);
|
|
|
|
if (encr_root) {
|
|
|
|
assert (write (fd, &encr_param_version, 4) == 4);
|
|
|
|
assert (write (fd, encr_prime, 256) == 256);
|
|
|
|
}
|
2013-11-04 12:34:27 -05:00
|
|
|
close (fd);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return !difference_got;
|
|
|
|
}
|
|
|
|
|
2013-10-13 06:18:08 -04:00
|
|
|
int readline_active;
|
2013-10-27 19:10:40 -04:00
|
|
|
int new_dc_num;
|
2013-10-03 08:38:25 -04:00
|
|
|
int loop (void) {
|
2013-10-11 16:52:20 -04:00
|
|
|
on_start ();
|
2013-11-12 19:11:25 -05:00
|
|
|
if (binlog_enabled) {
|
2013-11-29 16:43:56 -05:00
|
|
|
double t = get_double_time ();
|
|
|
|
logprintf ("replay log start\n");
|
2013-11-12 19:11:25 -05:00
|
|
|
replay_log ();
|
2013-11-29 16:43:56 -05:00
|
|
|
logprintf ("replay log end in %lf seconds\n", get_double_time () - t);
|
2013-11-14 19:08:24 -05:00
|
|
|
write_binlog ();
|
2014-01-11 19:43:29 -05:00
|
|
|
#ifdef USE_LUA
|
|
|
|
lua_binlog_end ();
|
|
|
|
#endif
|
2013-11-14 19:08:24 -05:00
|
|
|
} else {
|
|
|
|
read_auth_file ();
|
2013-11-12 19:11:25 -05:00
|
|
|
}
|
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");
|
|
|
|
}
|
|
|
|
do_help_get_config ();
|
|
|
|
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
|
|
|
}
|
2013-10-27 19:10:40 -04:00
|
|
|
int res = do_auth_check_phone (default_username);
|
|
|
|
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) {
|
2013-10-27 19:10:40 -04:00
|
|
|
do_send_code (default_username);
|
|
|
|
char *code = 0;
|
|
|
|
size_t size = 0;
|
|
|
|
printf ("Code from sms: ");
|
|
|
|
while (1) {
|
|
|
|
if (net_getline (&code, &size) == -1) {
|
|
|
|
perror ("getline()");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (do_send_code_result (code) >= 0) {
|
|
|
|
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;
|
|
|
|
printf ("Name: ");
|
|
|
|
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;
|
|
|
|
printf ("Name: ");
|
|
|
|
if (net_getline (&last_name, &size) == -1) {
|
|
|
|
perror ("getline()");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int dc_num = do_get_nearest_dc ();
|
|
|
|
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];
|
|
|
|
|
|
|
|
do_send_code (default_username);
|
|
|
|
printf ("Code from sms: ");
|
|
|
|
while (1) {
|
|
|
|
if (net_getline (&code, &size) == -1) {
|
|
|
|
perror ("getline()");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (do_send_code_result_auth (code, first_name, last_name) >= 0) {
|
|
|
|
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) {
|
|
|
|
do_export_auth (i);
|
|
|
|
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 ();
|
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
do_get_difference ();
|
|
|
|
net_loop (0, dgot);
|
2014-01-11 19:43:29 -05:00
|
|
|
#ifdef USE_LUA
|
|
|
|
lua_diff_end ();
|
|
|
|
#endif
|
2013-11-29 16:43:56 -05:00
|
|
|
send_all_unsent ();
|
|
|
|
|
2014-01-11 19:43:29 -05:00
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
do_get_dialog_list ();
|
2013-10-24 18:32:13 -04:00
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
return main_loop ();
|
|
|
|
}
|
|
|
|
|