This commit is contained in:
Vysheng 2014-08-15 18:57:43 +04:00
parent b12ca53816
commit e2e7d0c906
11 changed files with 232 additions and 139 deletions

View File

@ -82,6 +82,7 @@ static int fetch_comb_binlog_dc_option (void *extra) {
vlogprintf (E_NOTICE, "DC%d '%.*s' update: %.*s:%d\n", id, l1, name, l2, ip, port); vlogprintf (E_NOTICE, "DC%d '%.*s' update: %.*s:%d\n", id, l1, name, l2, ip, port);
tglmp_alloc_dc (id, tstrndup (ip, l2), port); tglmp_alloc_dc (id, tstrndup (ip, l2), port);
return 0; return 0;
} }

View File

@ -19,7 +19,7 @@
#ifndef __BINLOG_H__ #ifndef __BINLOG_H__
#define __BINLOG_H__ #define __BINLOG_H__
#include "structures.h" //#include "structures.h"
void bl_do_set_auth_key_id (int num, unsigned char *buf); void bl_do_set_auth_key_id (int num, unsigned char *buf);

230
loop.c
View File

@ -43,12 +43,16 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <event2/event.h>
#include "interface.h" #include "interface.h"
#include "telegram.h" #include "telegram.h"
#include "loop.h" #include "loop.h"
#include "lua-tg.h" #include "lua-tg.h"
#include "tgl.h" #include "tgl.h"
int verbosity;
extern char *default_username; extern char *default_username;
extern char *auth_token; extern char *auth_token;
void set_default_username (const char *s); void set_default_username (const char *s);
@ -61,37 +65,27 @@ extern int safe_quit;
extern int queries_num; extern int queries_num;
void got_it (char *line, int len); void got_it (char *line, int len);
void net_loop (int flags, int (*is_end)(void)) {
while (!is_end ()) {
struct pollfd fds[101];
int cc = 0;
if (flags & 3) {
fds[0].fd = 0;
fds[0].events = POLLIN;
cc ++;
}
//write_state_file (); static void stdin_read_callback (evutil_socket_t fd, short what, void *arg) {
int x = connections_make_poll_array (fds + cc, 101 - cc) + cc; if (((long)arg) & 1) {
double timer = next_timer_in (); rl_callback_read_char ();
if (timer > 1000) { timer = 1000; } } else {
if (poll (fds, x, timer) < 0) { char *line = 0;
work_timers (); size_t len = 0;
continue; assert (getline (&line, &len, stdin) >= 0);
} got_it (line, strlen (line));
work_timers (); }
if ((flags & 3) && (fds[0].revents & POLLIN)) { }
tgl_state.unread_messages = 0; void net_loop (int flags, int (*is_end)(void)) {
if (flags & 1) { struct event *ev = 0;
rl_callback_read_char (); if (flags & 3) {
} else { ev = event_new (tgl_state.ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback, (void *)(long)flags);
char *line = 0; event_add (ev, 0);
size_t len = 0; }
assert (getline (&line, &len, stdin) >= 0); while (!is_end || !is_end ()) {
got_it (line, strlen (line));
} event_base_loop (tgl_state.ev_base, EVLOOP_ONCE);
}
connections_poll_result (fds + cc, x - cc);
#ifdef USE_LUA #ifdef USE_LUA
lua_do_all (); lua_do_all ();
#endif #endif
@ -101,10 +95,17 @@ void net_loop (int flags, int (*is_end)(void)) {
exit (0); exit (0);
} }
if (unknown_user_list_pos) { if (unknown_user_list_pos) {
tgl_do_get_user_list_info_silent (unknown_user_list_pos, unknown_user_list); int i;
for (i = 0; i < unknown_user_list_pos; i++) {
tgl_do_get_user_info (TGL_MK_USER (unknown_user_list[i]), 0, 0, 0);
}
unknown_user_list_pos = 0; unknown_user_list_pos = 0;
} }
} }
if (ev) {
event_free (ev);
}
} }
char **_s; char **_s;
@ -134,30 +135,74 @@ int net_getline (char **s, size_t *l) {
return 0; return 0;
} }
int ret1 (void) { return 0; }
int main_loop (void) { int main_loop (void) {
net_loop (1, ret1); net_loop (1, 0);
return 0; return 0;
} }
char *get_auth_key_filename (void); struct dc *cur_a_dc;
char *get_state_filename (void); int is_authorized (void) {
char *get_secret_chat_filename (void); return tgl_authorized_dc (cur_a_dc);
}
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");
exit (1);
}
config_got = 1;
}
int should_register;
char *hash;
void sign_in_callback (void *extra, int success, int registered, const char *mhash) {
if (!success) {
logprintf ("Can not send code\n");
exit (1);
}
should_register = !registered;
hash = strdup (mhash);
}
int signed_in_ok;
void sign_in_result (void *extra, int success, struct tgl_user *U) {
if (!success) {
logprintf ("Can not login\n");
exit (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 (cur_a_dc);
}
void export_auth_callback (void *DC, int success) {
if (!success) {
logprintf ("Can not export auth\n");
exit (1);
}
}
int zero[512]; int zero[512];
extern int max_chat_size;
int mcs (void) {
return max_chat_size;
}
extern int difference_got;
int dgot (void) {
return difference_got;
}
int dlgot (void) {
return dialog_list_got;
}
int readline_active; int readline_active;
int new_dc_num; int new_dc_num;
@ -167,43 +212,37 @@ int loop (void) {
//on_start (); //on_start ();
tgl_init (); tgl_init ();
double t = get_double_time (); double t = tglt_get_double_time ();
logprintf ("replay log start\n"); logprintf ("replay log start\n");
tgl_replay_log (); tgl_replay_log ();
logprintf ("replay log end in %lf seconds\n", get_double_time () - t); logprintf ("replay log end in %lf seconds\n", tglt_get_double_time () - t);
tgl_reopen_binlog_for_writing (); tgl_reopen_binlog_for_writing ();
#ifdef USE_LUA #ifdef USE_LUA
lua_binlog_end (); lua_binlog_end ();
#endif #endif
update_prompt (); update_prompt ();
assert (DC_list[dc_working_num]); if (!tgl_authorized_dc (tgl_state.DC_working)) {
if (!DC_working || !DC_working->auth_key_id) { cur_a_dc = tgl_state.DC_working;
// if (auth_state == 0) { tgl_dc_authorize (tgl_state.DC_working);
DC_working = DC_list[dc_working_num]; net_loop (0, is_authorized);
assert (!DC_working->auth_key_id);
dc_authorize (DC_working);
assert (DC_working->auth_key_id);
auth_state = 100;
write_auth_file ();
} }
if (verbosity) { tgl_do_help_get_config (on_get_config, 0);
logprintf ("Requesting info about DC...\n"); net_loop (0, got_config);
}
tgl_do_help_get_config ();
net_loop (0, mcs);
if (verbosity) { if (verbosity) {
logprintf ("DC_info: %d new DC got\n", new_dc_num); logprintf ("DC_info: %d new DC got\n", new_dc_num);
} }
int i; int i;
for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->auth_key_id) { for (i = 0; i <= tgl_state.max_dc_num; i++) if (tgl_state.DC_list[i] && !tgl_authorized_dc (tgl_state.DC_list[i])) {
dc_authorize (DC_list[i]); cur_a_dc = tgl_state.DC_list[i];
assert (DC_list[i]->auth_key_id); tgl_dc_authorize (cur_a_dc);
write_auth_file (); net_loop (0, is_authorized);
} }
if (auth_state == 100 || !(DC_working->has_auth)) { if (!tgl_signed_dc (tgl_state.DC_working)) {
if (!default_username) { if (!default_username) {
size_t size = 0; size_t size = 0;
char *user = 0; char *user = 0;
@ -217,11 +256,11 @@ int loop (void) {
set_default_username (user); set_default_username (user);
} }
} }
int res = tgl_do_auth_check_phone (default_username); tgl_do_send_code (default_username, sign_in_callback, 0);
assert (res >= 0); net_loop (0, sent_code);
logprintf ("%s\n", res > 0 ? "phone registered" : "phone not registered");
if (res > 0 && !register_mode) { logprintf ("%s\n", should_register ? "phone not registered" : "phone registered");
tgl_do_send_code (default_username); if (!should_register) {
char *code = 0; char *code = 0;
size_t size = 0; size_t size = 0;
printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): "); printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): ");
@ -232,17 +271,16 @@ int loop (void) {
} }
if (!strcmp (code, "call")) { if (!strcmp (code, "call")) {
printf ("You typed \"call\", switching to phone system.\n"); printf ("You typed \"call\", switching to phone system.\n");
tgl_do_phone_call (default_username); tgl_do_phone_call (default_username, hash, 0, 0);
printf ("Calling you! Code: "); printf ("Calling you! Code: ");
continue; continue;
} }
if (tgl_do_send_code_result (code) >= 0) { if (tgl_do_send_code_result (default_username, hash, code, sign_in_result, 0) >= 0) {
break; break;
} }
printf ("Invalid code. Try again: "); printf ("Invalid code. Try again: ");
tfree_str (code); free (code);
} }
auth_state = 300;
} else { } else {
printf ("User is not registered. Do you want to register? [Y/n] "); printf ("User is not registered. Do you want to register? [Y/n] ");
char *code; char *code;
@ -269,13 +307,6 @@ int loop (void) {
perror ("getline()"); perror ("getline()");
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
int dc_num = tgl_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];
tgl_do_send_code (default_username);
printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): "); printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): ");
while (1) { while (1) {
if (net_getline (&code, &size) == -1) { if (net_getline (&code, &size) == -1) {
@ -284,37 +315,38 @@ int loop (void) {
} }
if (!strcmp (code, "call")) { if (!strcmp (code, "call")) {
printf ("You typed \"call\", switching to phone system.\n"); printf ("You typed \"call\", switching to phone system.\n");
tgl_do_phone_call (default_username); tgl_do_phone_call (default_username, hash, 0, 0);
printf ("Calling you! Code: "); printf ("Calling you! Code: ");
continue; continue;
} }
if (tgl_do_send_code_result_auth (code, first_name, last_name) >= 0) { if (tgl_do_send_code_result_auth (default_username, hash, code, first_name, last_name, sign_in_result, 0) >= 0) {
break; break;
} }
printf ("Invalid code. Try again: "); printf ("Invalid code. Try again: ");
tfree_str (code); free (code);
} }
auth_state = 300;
} }
net_loop (0, signed_in);
bl_do_dc_signed (tgl_state.DC_working);
} }
for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->has_auth) { for (i = 0; i <= tgl_state.max_dc_num; i++) if (tgl_state.DC_list[i] && !tgl_signed_dc (tgl_state.DC_list[i])) {
tgl_do_export_auth (i); tgl_do_export_auth (i, export_auth_callback, (void*)(long)tgl_state.DC_list[i]);
tgl_do_import_auth (i); cur_a_dc = tgl_state.DC_working;
bl_do_dc_signed (i); net_loop (0, dc_signed_in);
write_auth_file ();
} }
write_auth_file (); //write_auth_file ();
fflush (stdout); fflush (stdout);
fflush (stderr); fflush (stderr);
read_state_file (); //read_state_file ();
read_secret_chat_file (); //read_secret_chat_file ();
set_interface_callbacks (); set_interface_callbacks ();
tgl_do_get_difference (); tgl_do_get_difference (0, 0);
net_loop (0, dgot); net_loop (0, dgot);
#ifdef USE_LUA #ifdef USE_LUA
lua_diff_end (); lua_diff_end ();

25
main.c
View File

@ -374,7 +374,6 @@ void usage (void) {
printf (" -N message num mode\n"); printf (" -N message num mode\n");
printf (" -c config-file specify config file\n"); printf (" -c config-file specify config file\n");
printf (" -p prefix specify prefix\n"); printf (" -p prefix specify prefix\n");
printf (" -R register mode\n");
printf (" -f sync from start\n"); printf (" -f sync from start\n");
printf (" -B enable binlog\n"); printf (" -B enable binlog\n");
printf (" -E disable auto accept\n"); printf (" -E disable auto accept\n");
@ -400,7 +399,7 @@ char *lua_file;
void args_parse (int argc, char **argv) { void args_parse (int argc, char **argv) {
int opt = 0; int opt = 0;
while ((opt = getopt (argc, argv, "u:hk:vn:Nc:p:l:RfBL:Es:wW")) != -1) { while ((opt = getopt (argc, argv, "u:hk:vn:Nc:p:l:fBEs:wW")) != -1) {
switch (opt) { switch (opt) {
case 'u': case 'u':
set_default_username (optarg); set_default_username (optarg);
@ -425,23 +424,23 @@ void args_parse (int argc, char **argv) {
case 'l': case 'l':
log_level = atoi (optarg); log_level = atoi (optarg);
break; break;
case 'R': //case 'R':
register_mode = 1; // register_mode = 1;
break; // break;
case 'f': case 'f':
sync_from_start = 1; sync_from_start = 1;
break; break;
case 'B': case 'B':
binlog_enabled = 1; binlog_enabled = 1;
break; break;
case 'L': //case 'L':
if (log_net_file) { // if (log_net_file) {
usage (); // usage ();
} // }
log_net_file = tstrdup (optarg); // log_net_file = tstrdup (optarg);
log_net_f = fopen (log_net_file, "a"); // log_net_f = fopen (log_net_file, "a");
assert (log_net_f); // assert (log_net_f);
break; // break;
case 'E': case 'E':
disable_auto_accept = 1; disable_auto_accept = 1;
break; break;

View File

@ -1158,6 +1158,9 @@ struct dc *tglmp_alloc_dc (int id, char *ip, int port UU) {
DC->ip = ip; DC->ip = ip;
DC->port = port; DC->port = port;
tgl_state.DC_list[id] = DC; tgl_state.DC_list[id] = DC;
if (id > tgl_state.max_dc_num) {
tgl_state.max_dc_num = id;
}
return DC; return DC;
} }
@ -1171,7 +1174,7 @@ void tglmp_dc_create_session (struct dc *DC) {
struct session *S = talloc0 (sizeof (*S)); struct session *S = talloc0 (sizeof (*S));
assert (RAND_pseudo_bytes ((unsigned char *) &S->session_id, 8) >= 0); assert (RAND_pseudo_bytes ((unsigned char *) &S->session_id, 8) >= 0);
S->dc = DC; S->dc = DC;
S->c = tgl_state.net_methods->create_connection (DC->ip, DC->port, DC, S, &mtproto_methods); S->c = tgl_state.net_methods->create_connection (DC->ip, DC->port, S, DC, &mtproto_methods);
if (!S->c) { if (!S->c) {
vlogprintf (E_DEBUG, "Can not create connection to DC. Is network down?\n"); vlogprintf (E_DEBUG, "Can not create connection to DC. Is network down?\n");
exit (1); exit (1);

29
net.c
View File

@ -244,7 +244,7 @@ static void conn_try_write (evutil_socket_t fd, short what, void *arg) {
} }
} }
struct connection *tgln_create_connection (const char *host, int port, struct session *session, struct mtproto_methods *methods) { struct connection *tgln_create_connection (const char *host, int port, struct session *session, struct dc *dc, struct mtproto_methods *methods) {
struct connection *c = talloc0 (sizeof (*c)); struct connection *c = talloc0 (sizeof (*c));
int fd = socket (AF_INET, SOCK_STREAM, 0); int fd = socket (AF_INET, SOCK_STREAM, 0);
if (fd == -1) { if (fd == -1) {
@ -294,6 +294,10 @@ struct connection *tgln_create_connection (const char *host, int port, struct se
start_ping_timer (c); start_ping_timer (c);
c->dc = dc;
c->session = session;
c->methods = methods;
char byte = 0xef; char byte = 0xef;
assert (tgln_write_out (c, &byte, 1) == 1); assert (tgln_write_out (c, &byte, 1) == 1);
tgln_flush_out (c); tgln_flush_out (c);
@ -569,3 +573,26 @@ void tgl_connections_poll_result (struct pollfd *fds, int max) {
} }
} }
} }
static void incr_out_packet_num (struct connection *c) {
c->out_packet_num ++;
}
static struct dc *get_dc (struct connection *c) {
return c->dc;
}
static struct session *get_session (struct connection *c) {
return c->session;
}
struct tgl_net_methods tgl_conn_methods = {
.write_out = tgln_write_out,
.read_in = tgln_read_in,
.read_in_lookup = tgln_read_in_lookup,
.flush_out = tgln_flush_out,
.incr_out_packet_num = incr_out_packet_num,
.get_dc = get_dc,
.get_session = get_session,
.create_connection = tgln_create_connection
};

4
net.h
View File

@ -71,6 +71,7 @@ struct connection {
int in_fail_timer; int in_fail_timer;
struct mtproto_methods *methods; struct mtproto_methods *methods;
struct session *session; struct session *session;
struct dc *dc;
void *extra; void *extra;
struct event *ping_ev; struct event *ping_ev;
struct event *fail_ev; struct event *fail_ev;
@ -88,13 +89,14 @@ int tgln_read_in_lookup (struct connection *c, void *data, int len);
void tgln_insert_msg_id (struct session *S, long long id); void tgln_insert_msg_id (struct session *S, long long id);
extern struct tgl_net_methods tgl_conn_methods;
//void create_all_outbound_connections (void); //void create_all_outbound_connections (void);
//struct connection *create_connection (const char *host, int port, struct session *session, struct connection_methods *methods); //struct connection *create_connection (const char *host, int port, struct session *session, struct connection_methods *methods);
struct dc *tgln_alloc_dc (int id, char *ip, int port); struct dc *tgln_alloc_dc (int id, char *ip, int port);
void tgln_dc_create_session (struct dc *DC, struct mtproto_methods *methods); void tgln_dc_create_session (struct dc *DC, struct mtproto_methods *methods);
struct connection *tgln_create_connection (const char *host, int port, struct session *session, struct mtproto_methods *methods); struct connection *tgln_create_connection (const char *host, int port, struct session *session, struct dc *dc, struct mtproto_methods *methods);
#define GET_DC(c) (c->session->dc) #define GET_DC(c) (c->session->dc)
#endif #endif

View File

@ -368,8 +368,8 @@ void tgl_do_help_get_config (void (*callback)(void *, int), void *callback_extra
/* }}} */ /* }}} */
/* {{{ Send code */ /* {{{ Send code */
static char *phone_code_hash;
static int send_code_on_answer (struct query *q UU) { static int send_code_on_answer (struct query *q UU) {
static char *phone_code_hash;
assert (fetch_int () == (int)CODE_auth_sent_code); assert (fetch_int () == (int)CODE_auth_sent_code);
int registered = fetch_bool (); int registered = fetch_bool ();
int l = prefetch_strlen (); int l = prefetch_strlen ();
@ -380,7 +380,8 @@ static int send_code_on_answer (struct query *q UU) {
phone_code_hash = tstrndup (s, l); phone_code_hash = tstrndup (s, l);
fetch_int (); fetch_int ();
fetch_bool (); fetch_bool ();
want_dc_num = -1; tfree_str (q->extra);
if (q->callback) { if (q->callback) {
((void (*)(void *, int, int, const char *))(q->callback)) (q->callback_extra, 1, registered, phone_code_hash); ((void (*)(void *, int, int, const char *))(q->callback)) (q->callback_extra, 1, registered, phone_code_hash);
} }
@ -390,6 +391,7 @@ static int send_code_on_answer (struct query *q UU) {
static int send_code_on_error (struct query *q UU, int error_code, int l, char *error) { static int send_code_on_error (struct query *q UU, int error_code, int l, char *error) {
int s = strlen ("PHONE_MIGRATE_"); int s = strlen ("PHONE_MIGRATE_");
int s2 = strlen ("NETWORK_MIGRATE_"); int s2 = strlen ("NETWORK_MIGRATE_");
int want_dc_num = 0;
if (l >= s && !memcmp (error, "PHONE_MIGRATE_", s)) { if (l >= s && !memcmp (error, "PHONE_MIGRATE_", s)) {
int i = error[s] - '0'; int i = error[s] - '0';
want_dc_num = i; want_dc_num = i;
@ -400,9 +402,13 @@ static int send_code_on_error (struct query *q UU, int error_code, int l, char *
vlogprintf (E_ERROR, "error_code = %d, error = %.*s\n", error_code, l, error); vlogprintf (E_ERROR, "error_code = %d, error = %.*s\n", error_code, l, error);
assert (0); assert (0);
} }
if (q->callback) { bl_do_set_working_dc (want_dc_num);
((void (*)(void *, int, int, const char *))(q->callback)) (q->callback_extra, 0, 0, 0); //if (q->callback) {
} // ((void (*)(void *, int, int, const char *))(q->callback)) (q->callback_extra, 0, 0, 0);
//}
assert (DC_working->id == want_dc_num);
tgl_do_send_code (q->extra, q->callback, q->callback_extra);
tfree_str (q->extra);
return 0; return 0;
} }
@ -413,11 +419,10 @@ static struct query_methods send_code_methods = {
}; };
//char *suser; //char *suser;
extern int dc_working_num; //extern int dc_working_num;
void tgl_do_send_code (const char *user, void (*callback)(void *callback_extra, int success, int registered, const char *hash), void *callback_extra) { void tgl_do_send_code (const char *user, void (*callback)(void *callback_extra, int success, int registered, const char *hash), void *callback_extra) {
vlogprintf (E_DEBUG, "sending code to dc %d\n", dc_working_num); vlogprintf (E_DEBUG, "sending code to dc %d\n", tgl_state.dc_working_num);
//suser = tstrdup (user); //suser = tstrdup (user);
want_dc_num = 0;
clear_packet (); clear_packet ();
tgl_do_insert_header (); tgl_do_insert_header ();
out_int (CODE_auth_send_code); out_int (CODE_auth_send_code);
@ -427,7 +432,7 @@ void tgl_do_send_code (const char *user, void (*callback)(void *callback_extra,
out_string (TG_APP_HASH); out_string (TG_APP_HASH);
out_string ("en"); out_string ("en");
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_code_methods, 0, callback, callback_extra); tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_code_methods, tstrdup (user), callback, callback_extra);
} }
@ -444,7 +449,7 @@ static struct query_methods phone_call_methods = {
.type = TYPE_TO_PARAM(bool) .type = TYPE_TO_PARAM(bool)
}; };
void tgl_do_phone_call (const char *user, void (*callback)(void *callback_extra, int success), void *callback_extra) { void tgl_do_phone_call (const char *user, const char *hash,void (*callback)(void *callback_extra, int success), void *callback_extra) {
vlogprintf (E_DEBUG, "calling user\n"); vlogprintf (E_DEBUG, "calling user\n");
//suser = tstrdup (user); //suser = tstrdup (user);
want_dc_num = 0; want_dc_num = 0;
@ -452,7 +457,7 @@ void tgl_do_phone_call (const char *user, void (*callback)(void *callback_extra,
tgl_do_insert_header (); tgl_do_insert_header ();
out_int (CODE_auth_send_call); out_int (CODE_auth_send_call);
out_string (user); out_string (user);
out_string (phone_code_hash); out_string (hash);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &phone_call_methods, 0, callback, callback_extra); tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &phone_call_methods, 0, callback, callback_extra);
} }
@ -581,21 +586,21 @@ static struct query_methods sign_in_methods = {
.type = TYPE_TO_PARAM(auth_authorization) .type = TYPE_TO_PARAM(auth_authorization)
}; };
int tgl_do_send_code_result (const char *user, const char *code, void (*callback)(void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) { int tgl_do_send_code_result (const char *user, const char *hash, const char *code, void (*callback)(void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) {
clear_packet (); clear_packet ();
out_int (CODE_auth_sign_in); out_int (CODE_auth_sign_in);
out_string (user); out_string (user);
out_string (phone_code_hash); out_string (hash);
out_string (code); out_string (code);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra); tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra);
return 0; return 0;
} }
int tgl_do_send_code_result_auth (const char *user, const char *code, const char *first_name, const char *last_name, void (*callback)(void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) { int tgl_do_send_code_result_auth (const char *user, const char *hash, const char *code, const char *first_name, const char *last_name, void (*callback)(void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) {
clear_packet (); clear_packet ();
out_int (CODE_auth_sign_up); out_int (CODE_auth_sign_up);
out_string (user); out_string (user);
out_string (phone_code_hash); out_string (hash);
out_string (code); out_string (code);
out_string (first_name); out_string (first_name);
out_string (last_name); out_string (last_name);
@ -2116,6 +2121,8 @@ static int import_auth_on_answer (struct query *q UU) {
fetch_int (); // expires fetch_int (); // expires
tglf_fetch_alloc_user (); tglf_fetch_alloc_user ();
bl_do_dc_signed (((struct dc *)q->extra)->id);
if (q->callback) { if (q->callback) {
((void (*)(void *, int))q->callback) (q->callback_extra, 1); ((void (*)(void *, int))q->callback) (q->callback_extra, 1);
} }
@ -2140,7 +2147,7 @@ static int export_auth_on_answer (struct query *q UU) {
out_int (CODE_auth_import_authorization); out_int (CODE_auth_import_authorization);
out_int (tgl_state.our_id); out_int (tgl_state.our_id);
out_cstring (s, l); out_cstring (s, l);
tglq_send_query (q->extra, packet_ptr - packet_buffer, packet_buffer, &import_auth_methods, 0, q->callback, q->callback_extra); tglq_send_query (q->extra, packet_ptr - packet_buffer, packet_buffer, &import_auth_methods, q->extra, q->callback, q->callback_extra);
tfree (s, l); tfree (s, l);
return 0; return 0;
} }

15
tgl.c
View File

@ -5,6 +5,7 @@
#include "tgl.h" #include "tgl.h"
#include "tools.h" #include "tools.h"
#include "mtproto-client.h" #include "mtproto-client.h"
#include "net.h"
#include <event2/event.h> #include <event2/event.h>
struct tgl_state tgl_state; struct tgl_state tgl_state;
@ -36,5 +37,19 @@ void tgl_set_rsa_key (const char *key) {
void tgl_init (void) { void tgl_init (void) {
tgl_state.ev_base = event_base_new (); tgl_state.ev_base = event_base_new ();
if (!tgl_state.net_methods) {
tgl_state.net_methods = &tgl_conn_methods;
}
tglmp_on_start (tgl_state.rsa_key); tglmp_on_start (tgl_state.rsa_key);
} }
int tgl_authorized_dc (struct dc *DC) {
assert (DC);
return DC->auth_key_id;
}
int tgl_signed_dc (struct dc *DC) {
assert (DC);
return DC->has_auth;
}

19
tgl.h
View File

@ -31,15 +31,15 @@ struct tgl_update_callback {
}; };
struct tgl_net_methods { struct tgl_net_methods {
int (*write_out) (struct connection *c, void *data, int len); int (*write_out) (struct connection *c, const void *data, int len);
int (*read_in) (struct connection *c, void *data, int len); int (*read_in) (struct connection *c, void *data, int len);
int (*read_in_lookup) (struct connection *c, void *data, int len); int (*read_in_lookup) (struct connection *c, void *data, int len);
int (*flush_out) (struct connection *c); void (*flush_out) (struct connection *c);
void (*incr_out_packet_num) (struct connection *c); void (*incr_out_packet_num) (struct connection *c);
struct dc *(*get_dc) (struct connection *c); struct dc *(*get_dc) (struct connection *c);
struct session *(*get_session) (struct connection *c); struct session *(*get_session) (struct connection *c);
struct connection *(*create_connection) (const char *host, int port, struct dc *dc, struct session *session, struct mtproto_methods *methods); struct connection *(*create_connection) (const char *host, int port, struct session *session, struct dc *dc, struct mtproto_methods *methods);
}; };
@ -64,6 +64,7 @@ struct tgl_state {
struct dc *DC_list[TGL_MAX_DC_NUM]; struct dc *DC_list[TGL_MAX_DC_NUM];
struct dc *DC_working; struct dc *DC_working;
int max_dc_num;
int dc_working_num; int dc_working_num;
long long cur_uploading_bytes; long long cur_uploading_bytes;
@ -154,9 +155,9 @@ void tgl_connections_poll_result (struct pollfd *fds, int max);
void tgl_do_help_get_config (void (*callback)(void *callback_extra, int success), void *callback_extra); void tgl_do_help_get_config (void (*callback)(void *callback_extra, int success), void *callback_extra);
void tgl_do_send_code (const char *user, void (*callback)(void *callback_extra, int success, int registered, const char *hash), void *callback_extra); void tgl_do_send_code (const char *user, void (*callback)(void *callback_extra, int success, int registered, const char *hash), void *callback_extra);
void tgl_do_phone_call (const char *user, void (*callback)(void *callback_extra, int success), void *callback_extra); void tgl_do_phone_call (const char *user, const char *hash, void (*callback)(void *callback_extra, int success), void *callback_extra);
int tgl_do_send_code_result (const char *user, const char *code, void (*callback)(void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) ; int tgl_do_send_code_result (const char *user, const char *hash, const char *code, void (*callback)(void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) ;
int tgl_do_send_code_result_auth (const char *user, const char *code, const char *first_name, const char *last_name, void (*callback)(void *callback_extra, int success, struct tgl_user *Self), void *callback_extra); int tgl_do_send_code_result_auth (const char *user, const char *hash, const char *code, const char *first_name, const char *last_name, void (*callback)(void *callback_extra, int success, struct tgl_user *Self), void *callback_extra);
void tgl_do_update_contact_list (void (*callback) (void *callback_extra, int success, int size, struct tgl_user *contacts[]), void *callback_extra); void tgl_do_update_contact_list (void (*callback) (void *callback_extra, int success, int size, struct tgl_user *contacts[]), void *callback_extra);
void tgl_do_send_message (tgl_peer_id_t id, const char *msg, int len, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); void tgl_do_send_message (tgl_peer_id_t id, const char *msg, int len, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_send_msg (struct tgl_message *M, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); void tgl_do_send_msg (struct tgl_message *M, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
@ -195,6 +196,9 @@ void tgl_do_visualize_key (tgl_peer_id_t id, unsigned char buf[16]);
void tgl_do_send_ping (struct connection *c); void tgl_do_send_ping (struct connection *c);
int tgl_authorized_dc (struct dc *DC);
int tgl_signed_dc (struct dc *DC);
//void tgl_do_get_suggested (void); //void tgl_do_get_suggested (void);
void tgl_do_create_keys_end (struct tgl_secret_chat *U); void tgl_do_create_keys_end (struct tgl_secret_chat *U);
@ -208,4 +212,7 @@ struct mtproto_methods {
void tgl_init (void); void tgl_init (void);
void tgl_dc_authorize (struct dc *DC); void tgl_dc_authorize (struct dc *DC);
double tglt_get_double_time (void);
#endif #endif

View File

@ -20,7 +20,7 @@
#ifndef __TOOLS_H__ #ifndef __TOOLS_H__
#define __TOOLS_H__ #define __TOOLS_H__
double get_double_time (void); double tglt_get_double_time (void);
void *talloc (size_t size); void *talloc (size_t size);
void *trealloc (void *ptr, size_t old_size, size_t size); void *trealloc (void *ptr, size_t old_size, size_t size);