diff --git a/Makefile.in b/Makefile.in index c78c62f..8dc3f1b 100644 --- a/Makefile.in +++ b/Makefile.in @@ -21,7 +21,7 @@ EXE_LIST=${EXE}/generate ${EXE}/tlc ${EXE}/telegram-cli LIB_LIST=${LIB}/libtgl.a TG_OBJECTS=${OBJ}/main.o ${OBJ}/loop.o ${OBJ}/interface.o ${OBJ}/lua-tg.o -TGL_OBJECTS=${OBJ}/net.o ${OBJ}/mtproto-common.o ${OBJ}/mtproto-client.o ${OBJ}/queries.o ${OBJ}/structures.o ${OBJ}/binlog.o ${OBJ}/auto/auto.o ${OBJ}/tgl.o ${OBJ}/updates.o +TGL_OBJECTS=${OBJ}/net.o ${OBJ}/mtproto-common.o ${OBJ}/mtproto-client.o ${OBJ}/queries.o ${OBJ}/structures.o ${OBJ}/binlog.o ${OBJ}/auto/auto.o ${OBJ}/tgl.o ${OBJ}/updates.o ${OBJ}/tgl-timers.o TLC_OBJECTS=${OBJ}/tlc.o ${OBJ}/tl-parser.o ${OBJ}/crc32.o TLD_OBJECTS=${OBJ}/dump-tl-file.o GENERATE_OBJECTS=${OBJ}/generate.o diff --git a/auto-static.c b/auto-static.c index 70d5634..ac92658 100644 --- a/auto-static.c +++ b/auto-static.c @@ -303,7 +303,7 @@ static void free_vars_to_be_freed (void) { fvars_pos = 0; } -int tglf_extf_autocomplete (const char *text, int text_len, int index, char **R, char *data, int data_len) { +int tglf_extf_autocomplete (struct tgl_state *TLS, const char *text, int text_len, int index, char **R, char *data, int data_len) { if (index == -1) { buffer_pos = data; buffer_end = data + data_len; @@ -330,7 +330,7 @@ int tglf_extf_autocomplete (const char *text, int text_len, int index, char **R, } } -struct paramed_type *tglf_extf_store (const char *data, int data_len) { +struct paramed_type *tglf_extf_store (struct tgl_state *TLS, const char *data, int data_len) { buffer_pos = (char *)data; buffer_end = (char *)(data + data_len); local_next_token (); @@ -432,7 +432,7 @@ static void print_offset (void) { } } -char *tglf_extf_fetch (struct paramed_type *T) { +char *tglf_extf_fetch (struct tgl_state *TLS, struct paramed_type *T) { out_buf_pos = 0; if (fetch_type_any (T) < 0) { return 0; } return out_buf; diff --git a/binlog.c b/binlog.c index 3166f87..482d064 100644 --- a/binlog.c +++ b/binlog.c @@ -36,7 +36,7 @@ #include "binlog.h" #include "mtproto-common.h" -#include "net.h" +//#include "net.h" #include "include.h" #include "mtproto-client.h" @@ -51,7 +51,7 @@ static int binlog_buffer[BINLOG_BUFFER_SIZE]; static int *rptr; static int *wptr; -//static int tgl_state.binlog_fd; +//static int TLS->binlog_fd; static int in_replay_log; // should be used ONLY for DEBUG @@ -65,11 +65,11 @@ static void *alloc_log_event (int l UU) { static long long binlog_pos; -static int fetch_comb_binlog_start (void *extra) { +static int fetch_comb_binlog_start (struct tgl_state *TLS, void *extra) { return 0; } -static int fetch_comb_binlog_dc_option (void *extra) { +static int fetch_comb_binlog_dc_option (struct tgl_state *TLS, void *extra) { int id = fetch_int (); int l1 = prefetch_strlen (); assert (l1 >= 0); @@ -81,214 +81,214 @@ 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); - tglmp_alloc_dc (id, tstrndup (ip, l2), port); + tglmp_alloc_dc (TLS, id, tstrndup (ip, l2), port); return 0; } -static int fetch_comb_binlog_auth_key (void *extra) { +static int fetch_comb_binlog_auth_key (struct tgl_state *TLS, void *extra) { int num = fetch_int (); assert (num >= 0 && num <= MAX_DC_ID); - assert (tgl_state.DC_list[num]); - tgl_state.DC_list[num]->auth_key_id = fetch_long (); - fetch_ints (tgl_state.DC_list[num]->auth_key, 64); - tgl_state.DC_list[num]->flags |= 1; + assert (TLS->DC_list[num]); + TLS->DC_list[num]->auth_key_id = fetch_long (); + fetch_ints (TLS->DC_list[num]->auth_key, 64); + TLS->DC_list[num]->flags |= 1; return 0; } -static int fetch_comb_binlog_default_dc (void *extra) { +static int fetch_comb_binlog_default_dc (struct tgl_state *TLS, void *extra) { int num = fetch_int (); assert (num >= 0 && num <= MAX_DC_ID); - tgl_state.DC_working = tgl_state.DC_list[num]; - tgl_state.dc_working_num = num; + TLS->DC_working = TLS->DC_list[num]; + TLS->dc_working_num = num; return 0; } -static int fetch_comb_binlog_our_id (void *extra) { - tgl_state.our_id = fetch_int (); - if (tgl_state.callback.our_id) { - tgl_state.callback.our_id (tgl_state.our_id); +static int fetch_comb_binlog_our_id (struct tgl_state *TLS, void *extra) { + TLS->our_id = fetch_int (); + if (TLS->callback.our_id) { + TLS->callback.our_id (TLS, TLS->our_id); } return 0; } -static int fetch_comb_binlog_dc_signed (void *extra) { +static int fetch_comb_binlog_dc_signed (struct tgl_state *TLS, void *extra) { int num = fetch_int (); assert (num >= 0 && num <= MAX_DC_ID); - assert (tgl_state.DC_list[num]); - tgl_state.DC_list[num]->has_auth = 1; + assert (TLS->DC_list[num]); + TLS->DC_list[num]->has_auth = 1; return 0; } -static int fetch_comb_binlog_dc_salt (void *extra) { +static int fetch_comb_binlog_dc_salt (struct tgl_state *TLS, void *extra) { int num = fetch_int (); assert (num >= 0 && num <= MAX_DC_ID); - assert (tgl_state.DC_list[num]); - tgl_state.DC_list[num]->server_salt = fetch_long (); + assert (TLS->DC_list[num]); + TLS->DC_list[num]->server_salt = fetch_long (); return 0; } -static int fetch_comb_binlog_set_dh_params (void *extra) { - if (tgl_state.encr_prime) { tfree (tgl_state.encr_prime, 256); } - tgl_state.encr_root = fetch_int (); - tgl_state.encr_prime = talloc (256); - fetch_ints (tgl_state.encr_prime, 64); - tgl_state.encr_param_version = fetch_int (); +static int fetch_comb_binlog_set_dh_params (struct tgl_state *TLS, void *extra) { + if (TLS->encr_prime) { tfree (TLS->encr_prime, 256); } + TLS->encr_root = fetch_int (); + TLS->encr_prime = talloc (256); + fetch_ints (TLS->encr_prime, 64); + TLS->encr_param_version = fetch_int (); return 0; } -static int fetch_comb_binlog_set_pts (void *extra) { +static int fetch_comb_binlog_set_pts (struct tgl_state *TLS, void *extra) { int new_pts = fetch_int (); - assert (new_pts >= tgl_state.pts); - vlogprintf (E_DEBUG - 1 + 2 * in_replay_log, "pts %d=>%d\n", tgl_state.pts, new_pts); - tgl_state.pts = new_pts; + assert (new_pts >= TLS->pts); + vlogprintf (E_DEBUG - 1 + 2 * in_replay_log, "pts %d=>%d\n", TLS->pts, new_pts); + TLS->pts = new_pts; return 0; } -static int fetch_comb_binlog_set_qts (void *extra) { +static int fetch_comb_binlog_set_qts (struct tgl_state *TLS, void *extra) { int new_qts = fetch_int (); - assert (new_qts >= tgl_state.qts); - vlogprintf (E_DEBUG - 1 + 2 * in_replay_log, "qts %d=>%d\n", tgl_state.qts, new_qts); - tgl_state.qts = new_qts; + assert (new_qts >= TLS->qts); + vlogprintf (E_DEBUG - 1 + 2 * in_replay_log, "qts %d=>%d\n", TLS->qts, new_qts); + TLS->qts = new_qts; return 0; } -static int fetch_comb_binlog_set_date (void *extra) { +static int fetch_comb_binlog_set_date (struct tgl_state *TLS, void *extra) { int new_date = fetch_int (); - if (new_date < tgl_state.date) { return 0; } - assert (new_date >= tgl_state.date); - tgl_state.date = new_date; + if (new_date < TLS->date) { return 0; } + assert (new_date >= TLS->date); + TLS->date = new_date; return 0; } -static int fetch_comb_binlog_set_seq (void *extra) { +static int fetch_comb_binlog_set_seq (struct tgl_state *TLS, void *extra) { int new_seq = fetch_int (); - if (new_seq < tgl_state.seq) { - vlogprintf (E_ERROR, "Error: old_seq = %d, new_seq = %d\n", tgl_state.seq, new_seq); + if (new_seq < TLS->seq) { + vlogprintf (E_ERROR, "Error: old_seq = %d, new_seq = %d\n", TLS->seq, new_seq); } - assert (new_seq >= tgl_state.seq); - vlogprintf (E_DEBUG - 1 + 2 * in_replay_log, "seq %d=>%d\n", tgl_state.seq, new_seq); - tgl_state.seq = new_seq; + assert (new_seq >= TLS->seq); + vlogprintf (E_DEBUG - 1 + 2 * in_replay_log, "seq %d=>%d\n", TLS->seq, new_seq); + TLS->seq = new_seq; return 0; } -static int fetch_comb_binlog_user_add (void *extra) { +static int fetch_comb_binlog_user_add (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *_U = tgl_peer_get (id); + tgl_peer_t *_U = tgl_peer_get (TLS, id); if (!_U) { _U = talloc0 (sizeof (*_U)); _U->id = id; - tglp_insert_user (_U); + tglp_insert_user (TLS, _U); } else { assert (!(_U->flags & FLAG_CREATED)); } struct tgl_user *U = (void *)_U; U->flags |= FLAG_CREATED; - if (tgl_get_peer_id (id) == tgl_state.our_id) { + if (tgl_get_peer_id (id) == TLS->our_id) { U->flags |= FLAG_USER_SELF; } U->first_name = fetch_str_dup (); U->last_name = fetch_str_dup (); assert (!U->print_name); - U->print_name = tgl_state.callback.create_print_name (U->id, U->first_name, U->last_name, 0, 0); + U->print_name = TLS->callback.create_print_name (TLS, U->id, U->first_name, U->last_name, 0, 0); - tglp_peer_insert_name ((void *)U); + tglp_peer_insert_name (TLS, (void *)U); U->access_hash = fetch_long (); U->phone = fetch_str_dup (); if (fetch_int ()) { U->flags |= FLAG_USER_CONTACT; } - if (tgl_state.callback.user_update) { - tgl_state.callback.user_update (U, TGL_UPDATE_CREATED); + if (TLS->callback.user_update) { + TLS->callback.user_update (TLS, U, TGL_UPDATE_CREATED); } return 0; } -static int fetch_comb_binlog_user_delete (void *extra) { +static int fetch_comb_binlog_user_delete (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); U->flags |= FLAG_DELETED; - if (tgl_state.callback.user_update) { - tgl_state.callback.user_update ((void *)U, TGL_UPDATE_DELETED); + if (TLS->callback.user_update) { + TLS->callback.user_update (TLS, (void *)U, TGL_UPDATE_DELETED); } return 0; } -static int fetch_comb_binlog_user_set_access_hash (void *extra) { +static int fetch_comb_binlog_user_set_access_hash (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); U->user.access_hash = fetch_long (); - if (tgl_state.callback.user_update) { - tgl_state.callback.user_update ((void *)U, TGL_UPDATE_ACCESS_HASH); + if (TLS->callback.user_update) { + TLS->callback.user_update (TLS, (void *)U, TGL_UPDATE_ACCESS_HASH); } return 0; } -static int fetch_comb_binlog_user_set_phone (void *extra) { +static int fetch_comb_binlog_user_set_phone (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); if (U->user.phone) { tfree_str (U->user.phone); } U->user.phone = fetch_str_dup (); - if (tgl_state.callback.user_update) { - tgl_state.callback.user_update ((void *)U, TGL_UPDATE_PHONE); + if (TLS->callback.user_update) { + TLS->callback.user_update (TLS, (void *)U, TGL_UPDATE_PHONE); } return 0; } -static int fetch_comb_binlog_user_set_friend (void *extra) { +static int fetch_comb_binlog_user_set_friend (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); int friend = fetch_int (); if (friend) { U->flags |= FLAG_USER_CONTACT; } else { U->flags &= ~FLAG_USER_CONTACT; } - if (tgl_state.callback.user_update) { - tgl_state.callback.user_update ((void *)U, TGL_UPDATE_CONTACT); + if (TLS->callback.user_update) { + TLS->callback.user_update (TLS, (void *)U, TGL_UPDATE_CONTACT); } return 0; } -static int fetch_comb_binlog_user_set_full_photo (void *extra) { +static int fetch_comb_binlog_user_set_full_photo (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); if (U->flags & FLAG_HAS_PHOTO) { - tgls_free_photo (&U->user.photo); + tgls_free_photo (TLS, &U->user.photo); } - tglf_fetch_photo (&U->user.photo); + tglf_fetch_photo (TLS, &U->user.photo); U->flags |= FLAG_HAS_PHOTO; - if (tgl_state.callback.user_update) { - tgl_state.callback.user_update ((void *)U, TGL_UPDATE_PHOTO); + if (TLS->callback.user_update) { + TLS->callback.user_update (TLS, (void *)U, TGL_UPDATE_PHOTO); } return 0; } -static int fetch_comb_binlog_user_set_blocked (void *extra) { +static int fetch_comb_binlog_user_set_blocked (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); U->user.blocked = fetch_int (); - if (tgl_state.callback.user_update) { - tgl_state.callback.user_update ((void *)U, TGL_UPDATE_BLOCKED); + if (TLS->callback.user_update) { + TLS->callback.user_update (TLS, (void *)U, TGL_UPDATE_BLOCKED); } return 0; } -static int fetch_comb_binlog_user_set_real_name (void *extra) { +static int fetch_comb_binlog_user_set_real_name (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); assert (U->flags & FLAG_CREATED); @@ -297,15 +297,15 @@ static int fetch_comb_binlog_user_set_real_name (void *extra) { U->user.real_first_name = fetch_str_dup (); U->user.real_last_name = fetch_str_dup (); - if (tgl_state.callback.user_update) { - tgl_state.callback.user_update ((void *)U, TGL_UPDATE_REAL_NAME); + if (TLS->callback.user_update) { + TLS->callback.user_update (TLS, (void *)U, TGL_UPDATE_REAL_NAME); } return 0; } -static int fetch_comb_binlog_user_set_name (void *extra) { +static int fetch_comb_binlog_user_set_name (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); if (U->user.first_name) { tfree_str (U->user.first_name); } @@ -313,35 +313,35 @@ static int fetch_comb_binlog_user_set_name (void *extra) { U->user.first_name = fetch_str_dup (); U->user.last_name = fetch_str_dup (); if (U->print_name) { - tglp_peer_delete_name (U); + tglp_peer_delete_name (TLS, U); tfree_str (U->print_name); } - U->print_name = tgl_state.callback.create_print_name (U->id, U->user.first_name, U->user.last_name, 0, 0); - tglp_peer_insert_name ((void *)U); + U->print_name = TLS->callback.create_print_name (TLS, U->id, U->user.first_name, U->user.last_name, 0, 0); + tglp_peer_insert_name (TLS, (void *)U); - if (tgl_state.callback.user_update) { - tgl_state.callback.user_update ((void *)U, TGL_UPDATE_NAME); + if (TLS->callback.user_update) { + TLS->callback.user_update (TLS, (void *)U, TGL_UPDATE_NAME); } return 0; } -static int fetch_comb_binlog_user_set_username (void *extra) { +static int fetch_comb_binlog_user_set_username (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); if (U->user.username) { tfree_str (U->user.username); } U->user.username = fetch_str_dup (); - if (tgl_state.callback.user_update) { - tgl_state.callback.user_update ((void *)U, TGL_UPDATE_USERNAME); + if (TLS->callback.user_update) { + TLS->callback.user_update (TLS, (void *)U, TGL_UPDATE_USERNAME); } return 0; } -static int fetch_comb_binlog_user_set_photo (void *extra) { +static int fetch_comb_binlog_user_set_photo (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); @@ -353,19 +353,19 @@ static int fetch_comb_binlog_user_set_photo (void *extra) { } else { assert (y == CODE_user_profile_photo); U->user.photo_id = fetch_long (); - tglf_fetch_file_location (&U->user.photo_small); - tglf_fetch_file_location (&U->user.photo_big); + tglf_fetch_file_location (TLS, &U->user.photo_small); + tglf_fetch_file_location (TLS, &U->user.photo_big); } - if (tgl_state.callback.user_update) { - tgl_state.callback.user_update ((void *)U, TGL_UPDATE_PHOTO); + if (TLS->callback.user_update) { + TLS->callback.user_update (TLS, (void *)U, TGL_UPDATE_PHOTO); } return 0; } -static int fetch_comb_binlog_encr_chat_delete (void *extra) { +static int fetch_comb_binlog_encr_chat_delete (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); - tgl_peer_t *_U = tgl_peer_get (id); + tgl_peer_t *_U = tgl_peer_get (TLS, id); assert (_U); struct tgl_secret_chat *U = &_U->encr_chat; memset (U->key, 0, sizeof (U->key)); @@ -380,19 +380,19 @@ static int fetch_comb_binlog_encr_chat_delete (void *extra) { U->g_key = 0; } - if (tgl_state.callback.secret_chat_update) { - tgl_state.callback.secret_chat_update (U, TGL_UPDATE_DELETED); + if (TLS->callback.secret_chat_update) { + TLS->callback.secret_chat_update (TLS, U, TGL_UPDATE_DELETED); } return 0; } -static int fetch_comb_binlog_encr_chat_requested (void *extra) { +static int fetch_comb_binlog_encr_chat_requested (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); - tgl_peer_t *_U = tgl_peer_get (id); + tgl_peer_t *_U = tgl_peer_get (TLS, id); if (!_U) { _U = talloc0 (sizeof (*_U)); _U->id = id; - tglp_insert_encrypted_chat (_U); + tglp_insert_encrypted_chat (TLS, _U); } else { assert (!(_U->flags & FLAG_CREATED)); } @@ -402,16 +402,16 @@ static int fetch_comb_binlog_encr_chat_requested (void *extra) { U->admin_id = fetch_int (); U->user_id = fetch_int (); - tgl_peer_t *Us = tgl_peer_get (TGL_MK_USER (U->user_id)); + tgl_peer_t *Us = tgl_peer_get (TLS, TGL_MK_USER (U->user_id)); assert (!U->print_name); if (Us) { - U->print_name = tgl_state.callback.create_print_name (id, "!", Us->user.first_name, Us->user.last_name, 0); + U->print_name = TLS->callback.create_print_name (TLS, id, "!", Us->user.first_name, Us->user.last_name, 0); } else { static char buf[100]; tsnprintf (buf, 99, "user#%d", U->user_id); - U->print_name = tgl_state.callback.create_print_name (id, "!", buf, 0, 0); + U->print_name = TLS->callback.create_print_name (TLS, id, "!", buf, 0, 0); } - tglp_peer_insert_name ((void *)U); + tglp_peer_insert_name (TLS, (void *)U); U->g_key = talloc (256); U->nonce = talloc (256); fetch_ints (U->g_key, 64); @@ -420,58 +420,58 @@ static int fetch_comb_binlog_encr_chat_requested (void *extra) { U->flags |= FLAG_CREATED; U->state = sc_request; - if (tgl_state.callback.secret_chat_update) { - tgl_state.callback.secret_chat_update (U, TGL_UPDATE_REQUESTED); + if (TLS->callback.secret_chat_update) { + TLS->callback.secret_chat_update (TLS, U, TGL_UPDATE_REQUESTED); } return 0; } -static int fetch_comb_binlog_encr_chat_set_access_hash (void *extra) { +static int fetch_comb_binlog_encr_chat_set_access_hash (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); U->encr_chat.access_hash = fetch_long (); - if (tgl_state.callback.secret_chat_update) { - tgl_state.callback.secret_chat_update ((void *)U, TGL_UPDATE_ACCESS_HASH); + if (TLS->callback.secret_chat_update) { + TLS->callback.secret_chat_update (TLS, (void *)U, TGL_UPDATE_ACCESS_HASH); } return 0; } -static int fetch_comb_binlog_encr_chat_set_date (void *extra) { +static int fetch_comb_binlog_encr_chat_set_date (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); U->encr_chat.date = fetch_int (); return 0; } -static int fetch_comb_binlog_encr_chat_set_ttl (void *extra) { +static int fetch_comb_binlog_encr_chat_set_ttl (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); U->encr_chat.ttl = fetch_int (); return 0; } -static int fetch_comb_binlog_encr_chat_set_layer (void *extra) { +static int fetch_comb_binlog_encr_chat_set_layer (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); U->encr_chat.layer = fetch_int (); return 0; } -static int fetch_comb_binlog_encr_chat_set_state (void *extra) { +static int fetch_comb_binlog_encr_chat_set_state (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); assert (U); U->encr_chat.state = fetch_int (); return 0; } -static int fetch_comb_binlog_encr_chat_accepted (void *extra) { +static int fetch_comb_binlog_encr_chat_accepted (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); - tgl_peer_t *_U = tgl_peer_get (id); + tgl_peer_t *_U = tgl_peer_get (TLS, id); assert (_U); struct tgl_secret_chat *U = &_U->encr_chat; if (!U->g_key) { @@ -486,19 +486,19 @@ static int fetch_comb_binlog_encr_chat_accepted (void *extra) { U->key_fingerprint = fetch_long (); if (U->state == sc_waiting) { - tgl_do_create_keys_end (U); + tgl_do_create_keys_end (TLS, U); } U->state = sc_ok; - if (tgl_state.callback.secret_chat_update) { - tgl_state.callback.secret_chat_update (U, TGL_UPDATE_WORKING); + if (TLS->callback.secret_chat_update) { + TLS->callback.secret_chat_update (TLS, U, TGL_UPDATE_WORKING); } return 0; } -static int fetch_comb_binlog_encr_chat_set_key (void *extra) { +static int fetch_comb_binlog_encr_chat_set_key (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); - tgl_peer_t *_U = tgl_peer_get (id); + tgl_peer_t *_U = tgl_peer_get (TLS, id); assert (_U); struct tgl_secret_chat *U = &_U->encr_chat; fetch_ints (U->key, 64); @@ -506,18 +506,18 @@ static int fetch_comb_binlog_encr_chat_set_key (void *extra) { return 0; } -static int fetch_comb_binlog_encr_chat_update_seq (void *extra) { +static int fetch_comb_binlog_encr_chat_update_seq (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); - tgl_peer_t *_U = tgl_peer_get (id); + tgl_peer_t *_U = tgl_peer_get (TLS, id); assert (_U); _U->encr_chat.in_seq_no = fetch_int (); _U->encr_chat.last_in_seq_no = fetch_int (); return 0; } -static int fetch_comb_binlog_encr_chat_set_seq (void *extra) { +static int fetch_comb_binlog_encr_chat_set_seq (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); - tgl_peer_t *_U = tgl_peer_get (id); + tgl_peer_t *_U = tgl_peer_get (TLS, id); assert (_U); _U->encr_chat.in_seq_no = fetch_int (); _U->encr_chat.last_in_seq_no = fetch_int (); @@ -525,54 +525,54 @@ static int fetch_comb_binlog_encr_chat_set_seq (void *extra) { return 0; } -static int fetch_comb_binlog_encr_chat_init (void *extra) { +static int fetch_comb_binlog_encr_chat_init (struct tgl_state *TLS, void *extra) { tgl_peer_t *P = talloc0 (sizeof (*P)); P->id = TGL_MK_ENCR_CHAT (fetch_int ()); - assert (!tgl_peer_get (P->id)); + assert (!tgl_peer_get (TLS, P->id)); P->encr_chat.user_id = fetch_int (); - P->encr_chat.admin_id = tgl_state.our_id; - tglp_insert_encrypted_chat (P); - tgl_peer_t *Us = tgl_peer_get (TGL_MK_USER (P->encr_chat.user_id)); + P->encr_chat.admin_id = TLS->our_id; + tglp_insert_encrypted_chat (TLS, P); + tgl_peer_t *Us = tgl_peer_get (TLS, TGL_MK_USER (P->encr_chat.user_id)); assert (Us); - P->print_name = tgl_state.callback.create_print_name (P->id, "!", Us->user.first_name, Us->user.last_name, 0); - tglp_peer_insert_name (P); + P->print_name = TLS->callback.create_print_name (TLS, P->id, "!", Us->user.first_name, Us->user.last_name, 0); + tglp_peer_insert_name (TLS, P); P->encr_chat.g_key = talloc (256); fetch_ints (P->encr_chat.key, 64); fetch_ints (P->encr_chat.g_key, 64); P->flags |= FLAG_CREATED; - if (tgl_state.callback.secret_chat_update) { - tgl_state.callback.secret_chat_update ((void *)P, TGL_UPDATE_CREATED); + if (TLS->callback.secret_chat_update) { + TLS->callback.secret_chat_update (TLS, (void *)P, TGL_UPDATE_CREATED); } return 0; } -static int fetch_comb_binlog_encr_chat_create (void *extra) { +static int fetch_comb_binlog_encr_chat_create (struct tgl_state *TLS, void *extra) { tgl_peer_t *P = talloc0 (sizeof (*P)); P->id = TGL_MK_ENCR_CHAT (fetch_int ()); - assert (!tgl_peer_get (P->id)); + assert (!tgl_peer_get (TLS, P->id)); P->encr_chat.user_id = fetch_int (); P->encr_chat.admin_id = fetch_int (); - tglp_insert_encrypted_chat (P); + tglp_insert_encrypted_chat (TLS, P); P->print_name = fetch_str_dup (); - tglp_peer_insert_name (P); + tglp_peer_insert_name (TLS, P); P->flags |= FLAG_CREATED; - if (tgl_state.callback.secret_chat_update) { - tgl_state.callback.secret_chat_update ((void *)P, TGL_UPDATE_CREATED); + if (TLS->callback.secret_chat_update) { + TLS->callback.secret_chat_update (TLS, (void *)P, TGL_UPDATE_CREATED); } return 0; } -static int fetch_comb_binlog_chat_create (void *extra) { +static int fetch_comb_binlog_chat_create (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_CHAT (fetch_int ()); - tgl_peer_t *_C = tgl_peer_get (id); + tgl_peer_t *_C = tgl_peer_get (TLS, id); if (!_C) { _C = talloc0 (sizeof (*_C)); _C->id = id; - tglp_insert_chat (_C); + tglp_insert_chat (TLS, _C); } else { assert (!(_C->flags & FLAG_CREATED)); } @@ -580,8 +580,8 @@ static int fetch_comb_binlog_chat_create (void *extra) { C->flags = FLAG_CREATED | fetch_int (); C->title = fetch_str_dup (); assert (!C->print_title); - C->print_title = tgl_state.callback.create_print_name (id, C->title, 0, 0, 0); - tglp_peer_insert_name ((void *)C); + C->print_title = TLS->callback.create_print_name (TLS, id, C->title, 0, 0, 0); + tglp_peer_insert_name (TLS, (void *)C); C->users_num = fetch_int (); C->date = fetch_int (); C->version = fetch_int (); @@ -589,83 +589,83 @@ static int fetch_comb_binlog_chat_create (void *extra) { fetch_data (&C->photo_big, sizeof (struct tgl_file_location)); fetch_data (&C->photo_small, sizeof (struct tgl_file_location)); - if (tgl_state.callback.chat_update) { - tgl_state.callback.chat_update (C, TGL_UPDATE_CREATED); + if (TLS->callback.chat_update) { + TLS->callback.chat_update (TLS, C, TGL_UPDATE_CREATED); } return 0; } -static int fetch_comb_binlog_chat_change_flags (void *extra) { - tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_change_flags (struct tgl_state *TLS, void *extra) { + tgl_peer_t *C = tgl_peer_get (TLS, TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); C->chat.flags |= fetch_int (); C->chat.flags &= ~fetch_int (); - if (tgl_state.callback.chat_update) { - tgl_state.callback.chat_update ((void *)C, TGL_UPDATE_FLAGS); + if (TLS->callback.chat_update) { + TLS->callback.chat_update (TLS, (void *)C, TGL_UPDATE_FLAGS); } return 0; } -static int fetch_comb_binlog_chat_set_title (void *extra) { - tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_title (struct tgl_state *TLS, void *extra) { + tgl_peer_t *C = tgl_peer_get (TLS, TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); if (C->chat.title) { tfree_str (C->chat.title); } C->chat.title = fetch_str_dup (); if (C->print_name) { - tglp_peer_delete_name ((void *)C); + tglp_peer_delete_name (TLS, (void *)C); tfree_str (C->print_name); } - C->print_name = tgl_state.callback.create_print_name (C->id, C->chat.title, 0, 0, 0); - tglp_peer_insert_name ((void *)C); + C->print_name = TLS->callback.create_print_name (TLS, C->id, C->chat.title, 0, 0, 0); + tglp_peer_insert_name (TLS, (void *)C); - if (tgl_state.callback.chat_update) { - tgl_state.callback.chat_update ((void *)C, TGL_UPDATE_TITLE); + if (TLS->callback.chat_update) { + TLS->callback.chat_update (TLS, (void *)C, TGL_UPDATE_TITLE); } return 0; } -static int fetch_comb_binlog_chat_set_photo (void *extra) { - tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_photo (struct tgl_state *TLS, void *extra) { + tgl_peer_t *C = tgl_peer_get (TLS, TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); fetch_data (&C->photo_big, sizeof (struct tgl_file_location)); fetch_data (&C->photo_small, sizeof (struct tgl_file_location)); - if (tgl_state.callback.chat_update) { - tgl_state.callback.chat_update ((void *)C, TGL_UPDATE_PHOTO); + if (TLS->callback.chat_update) { + TLS->callback.chat_update (TLS, (void *)C, TGL_UPDATE_PHOTO); } return 0; } -static int fetch_comb_binlog_chat_set_date (void *extra) { - tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_date (struct tgl_state *TLS, void *extra) { + tgl_peer_t *C = tgl_peer_get (TLS, TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); C->chat.date = fetch_int (); return 0; } -static int fetch_comb_binlog_chat_set_version (void *extra) { - tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_version (struct tgl_state *TLS, void *extra) { + tgl_peer_t *C = tgl_peer_get (TLS, TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); C->chat.version = fetch_int (); C->chat.users_num = fetch_int (); return 0; } -static int fetch_comb_binlog_chat_set_admin (void *extra) { - tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_admin (struct tgl_state *TLS, void *extra) { + tgl_peer_t *C = tgl_peer_get (TLS, TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); C->chat.admin_id = fetch_int (); - if (tgl_state.callback.chat_update) { - tgl_state.callback.chat_update ((void *)C, TGL_UPDATE_ADMIN); + if (TLS->callback.chat_update) { + TLS->callback.chat_update (TLS, (void *)C, TGL_UPDATE_ADMIN); } return 0; } -static int fetch_comb_binlog_chat_set_participants (void *extra) { - tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_participants (struct tgl_state *TLS, void *extra) { + tgl_peer_t *C = tgl_peer_get (TLS, TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); C->chat.user_list_version = fetch_int (); if (C->chat.user_list) { tfree (C->chat.user_list, 12 * C->chat.user_list_size); } @@ -673,32 +673,32 @@ static int fetch_comb_binlog_chat_set_participants (void *extra) { C->chat.user_list = talloc (12 * C->chat.user_list_size); fetch_ints (C->chat.user_list, 3 * C->chat.user_list_size); - if (tgl_state.callback.chat_update) { - tgl_state.callback.chat_update ((void *)C, TGL_UPDATE_MEMBERS); + if (TLS->callback.chat_update) { + TLS->callback.chat_update (TLS, (void *)C, TGL_UPDATE_MEMBERS); } return 0; } -static int fetch_comb_binlog_chat_set_full_photo (void *extra) { - tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_full_photo (struct tgl_state *TLS, void *extra) { + tgl_peer_t *C = tgl_peer_get (TLS, TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); assert (C && (C->flags & FLAG_CREATED)); if (C->flags & FLAG_HAS_PHOTO) { - tgls_free_photo (&C->chat.photo); + tgls_free_photo (TLS, &C->chat.photo); } - tglf_fetch_photo (&C->chat.photo); + tglf_fetch_photo (TLS, &C->chat.photo); C->flags |= FLAG_HAS_PHOTO; - if (tgl_state.callback.chat_update) { - tgl_state.callback.chat_update ((void *)C, TGL_UPDATE_PHOTO); + if (TLS->callback.chat_update) { + TLS->callback.chat_update (TLS, (void *)C, TGL_UPDATE_PHOTO); } return 0; } -static int fetch_comb_binlog_chat_add_participant (void *extra) { +static int fetch_comb_binlog_chat_add_participant (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_CHAT (fetch_int ()); - tgl_peer_t *_C = tgl_peer_get (id); + tgl_peer_t *_C = tgl_peer_get (TLS, id); assert (_C && (_C->flags & FLAG_CREATED)); struct tgl_chat *C = &_C->chat; @@ -720,15 +720,15 @@ static int fetch_comb_binlog_chat_add_participant (void *extra) { C->user_list[C->user_list_size - 1].date = date; C->user_list_version = version; - if (tgl_state.callback.chat_update) { - tgl_state.callback.chat_update (C, TGL_UPDATE_MEMBERS); + if (TLS->callback.chat_update) { + TLS->callback.chat_update (TLS, C, TGL_UPDATE_MEMBERS); } return 0; } -static int fetch_comb_binlog_chat_del_participant (void *extra) { +static int fetch_comb_binlog_chat_del_participant (struct tgl_state *TLS, void *extra) { tgl_peer_id_t id = TGL_MK_CHAT (fetch_int ()); - tgl_peer_t *_C = tgl_peer_get (id); + tgl_peer_t *_C = tgl_peer_get (TLS, id); assert (_C && (_C->flags & FLAG_CREATED)); struct tgl_chat *C = &_C->chat; @@ -750,18 +750,18 @@ static int fetch_comb_binlog_chat_del_participant (void *extra) { C->user_list = trealloc (C->user_list, 12 * C->user_list_size + 12, 12 * C->user_list_size); C->user_list_version = version; - if (tgl_state.callback.chat_update) { - tgl_state.callback.chat_update (C, TGL_UPDATE_MEMBERS); + if (TLS->callback.chat_update) { + TLS->callback.chat_update (TLS, C, TGL_UPDATE_MEMBERS); } return 0; } -static int fetch_comb_binlog_create_message_text (void *extra) { +static int fetch_comb_binlog_create_message_text (struct tgl_state *TLS, void *extra) { long long id = fetch_int (); - struct tgl_message *M = tgl_message_get (id); + struct tgl_message *M = tgl_message_get (TLS, id); if (!M) { - M = tglm_message_alloc (id); + M = tglm_message_alloc (TLS, id); } else { assert (!(M->flags & FLAG_CREATED)); } @@ -790,18 +790,18 @@ static int fetch_comb_binlog_create_message_text (void *extra) { } //M->unread = 1; - M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == TLS->our_id; - tglm_message_insert (M); + tglm_message_insert (TLS, M); return 0; } -static int fetch_comb_binlog_send_message_text (void *extra) { +static int fetch_comb_binlog_send_message_text (struct tgl_state *TLS, void *extra) { long long id = fetch_long (); - struct tgl_message *M = tgl_message_get (id); + struct tgl_message *M = tgl_message_get (TLS, id); if (!M) { - M = tglm_message_alloc (id); + M = tglm_message_alloc (TLS, id); } else { assert (!(M->flags & FLAG_CREATED)); } @@ -815,7 +815,7 @@ static int fetch_comb_binlog_send_message_text (void *extra) { M->to_id = tgl_set_peer_id (t, fetch_int ()); if (t == TGL_PEER_ENCR_CHAT) { - tgl_peer_t *P = tgl_peer_get (M->to_id); + tgl_peer_t *P = tgl_peer_get (TLS, M->to_id); if (P && P->encr_chat.layer >= 17) { P->encr_chat.out_seq_no ++; } @@ -835,20 +835,20 @@ static int fetch_comb_binlog_send_message_text (void *extra) { } M->unread = 1; - M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == TLS->our_id; - tglm_message_insert (M); - tglm_message_insert_unsent (M); + tglm_message_insert (TLS, M); + tglm_message_insert_unsent (TLS, M); M->flags |= FLAG_PENDING; return 0; } -static int fetch_comb_binlog_send_message_action_encr (void *extra) { +static int fetch_comb_binlog_send_message_action_encr (struct tgl_state *TLS, void *extra) { long long id = fetch_long (); - struct tgl_message *M = tgl_message_get (id); + struct tgl_message *M = tgl_message_get (TLS, id); if (!M) { - M = tglm_message_alloc (id); + M = tglm_message_alloc (TLS, id); } else { assert (!(M->flags & FLAG_CREATED)); } @@ -861,9 +861,9 @@ static int fetch_comb_binlog_send_message_action_encr (void *extra) { M->date = fetch_int (); M->media.type = tgl_message_media_none; - tglf_fetch_message_action_encrypted (&M->action); + tglf_fetch_message_action_encrypted (TLS, &M->action); - tgl_peer_t *P = tgl_peer_get (M->to_id); + tgl_peer_t *P = tgl_peer_get (TLS, M->to_id); if (P) { if (P->encr_chat.layer >= 17) { P->encr_chat.out_seq_no ++; @@ -871,21 +871,21 @@ static int fetch_comb_binlog_send_message_action_encr (void *extra) { } M->unread = 1; - M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == TLS->our_id; M->service = 1; - tglm_message_insert (M); - tglm_message_insert_unsent (M); + tglm_message_insert (TLS, M); + tglm_message_insert_unsent (TLS, M); M->flags |= FLAG_PENDING; return 0; } -static int fetch_comb_binlog_create_message_text_fwd (void *extra) { +static int fetch_comb_binlog_create_message_text_fwd (struct tgl_state *TLS, void *extra) { long long id = fetch_int (); - struct tgl_message *M = tgl_message_get (id); + struct tgl_message *M = tgl_message_get (TLS, id); if (!M) { - M = tglm_message_alloc (id); + M = tglm_message_alloc (TLS, id); } else { assert (!(M->flags & FLAG_CREATED)); } @@ -918,18 +918,18 @@ static int fetch_comb_binlog_create_message_text_fwd (void *extra) { } //M->unread = 1; - M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == TLS->our_id; - tglm_message_insert (M); + tglm_message_insert (TLS, M); return 0; } -static int fetch_comb_binlog_create_message_media (void *extra) { +static int fetch_comb_binlog_create_message_media (struct tgl_state *TLS, void *extra) { int id = fetch_int (); - struct tgl_message *M = tgl_message_get (id); + struct tgl_message *M = tgl_message_get (TLS, id); if (!M) { - M = tglm_message_alloc (id); + M = tglm_message_alloc (TLS, id); } else { assert (!(M->flags & FLAG_CREATED)); } @@ -947,19 +947,19 @@ static int fetch_comb_binlog_create_message_media (void *extra) { M->message[l] = 0; M->message_len = l; - tglf_fetch_message_media (&M->media); + tglf_fetch_message_media (TLS, &M->media); //M->unread = 1; - M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == TLS->our_id; - tglm_message_insert (M); + tglm_message_insert (TLS, M); return 0; } -static int fetch_comb_binlog_create_message_media_encr (void *extra) { +static int fetch_comb_binlog_create_message_media_encr (struct tgl_state *TLS, void *extra) { long long id = fetch_long (); - struct tgl_message *M = tgl_message_get (id); + struct tgl_message *M = tgl_message_get (TLS, id); if (!M) { - M = tglm_message_alloc (id); + M = tglm_message_alloc (TLS, id); } else { assert (!(M->flags & FLAG_CREATED)); } @@ -975,20 +975,20 @@ static int fetch_comb_binlog_create_message_media_encr (void *extra) { M->message[l] = 0; M->message_len = l; - tglf_fetch_message_media_encrypted (&M->media); - tglf_fetch_encrypted_message_file (&M->media); + tglf_fetch_message_media_encrypted (TLS, &M->media); + tglf_fetch_encrypted_message_file (TLS, &M->media); M->unread = 1; - M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == TLS->our_id; - tglm_message_insert (M); + tglm_message_insert (TLS, M); return 0; } -static int fetch_comb_binlog_create_message_media_encr_pending (void *extra) { +static int fetch_comb_binlog_create_message_media_encr_pending (struct tgl_state *TLS, void *extra) { long long id = fetch_long (); - struct tgl_message *M = tgl_message_get (id); + struct tgl_message *M = tgl_message_get (TLS, id); if (!M) { - M = tglm_message_alloc (id); + M = tglm_message_alloc (TLS, id); } else { assert (!(M->flags & FLAG_CREATED)); } @@ -998,7 +998,7 @@ static int fetch_comb_binlog_create_message_media_encr_pending (void *extra) { M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); - tgl_peer_t *P = tgl_peer_get (M->to_id); + tgl_peer_t *P = tgl_peer_get (TLS, M->to_id); if (P) { if (P->encr_chat.layer >= 17) { P->encr_chat.out_seq_no ++; @@ -1011,31 +1011,31 @@ static int fetch_comb_binlog_create_message_media_encr_pending (void *extra) { M->message[l] = 0; M->message_len = l; - tglf_fetch_message_media_encrypted (&M->media); + tglf_fetch_message_media_encrypted (TLS, &M->media); M->unread = 1; - M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == TLS->our_id; - tglm_message_insert (M); - tglm_message_insert_unsent (M); + tglm_message_insert (TLS, M); + tglm_message_insert_unsent (TLS, M); M->flags |= FLAG_PENDING; return 0; } -static int fetch_comb_binlog_create_message_media_encr_sent (void *extra) { +static int fetch_comb_binlog_create_message_media_encr_sent (struct tgl_state *TLS, void *extra) { long long id = fetch_long (); - struct tgl_message *M = tgl_message_get (id); + struct tgl_message *M = tgl_message_get (TLS, id); assert (M && (M->flags & FLAG_CREATED)); - tglf_fetch_encrypted_message_file (&M->media); - tglm_message_remove_unsent (M); + tglf_fetch_encrypted_message_file (TLS, &M->media); + tglm_message_remove_unsent (TLS, M); M->flags &= ~FLAG_PENDING; return 0; } -static int fetch_comb_binlog_create_message_media_fwd (void *extra) { +static int fetch_comb_binlog_create_message_media_fwd (struct tgl_state *TLS, void *extra) { int id = fetch_int (); - struct tgl_message *M = tgl_message_get (id); + struct tgl_message *M = tgl_message_get (TLS, id); if (!M) { - M = tglm_message_alloc (id); + M = tglm_message_alloc (TLS, id); } else { assert (!(M->flags & FLAG_CREATED)); } @@ -1056,19 +1056,19 @@ static int fetch_comb_binlog_create_message_media_fwd (void *extra) { M->message[l] = 0; M->message_len = l; - tglf_fetch_message_media (&M->media); + tglf_fetch_message_media (TLS, &M->media); //M->unread = 1; - M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == TLS->our_id; - tglm_message_insert (M); + tglm_message_insert (TLS, M); return 0; } -static int fetch_comb_binlog_create_message_service (void *extra) { +static int fetch_comb_binlog_create_message_service (struct tgl_state *TLS, void *extra) { int id = fetch_int (); - struct tgl_message *M = tgl_message_get (id); + struct tgl_message *M = tgl_message_get (TLS, id); if (!M) { - M = tglm_message_alloc (id); + M = tglm_message_alloc (TLS, id); } else { assert (!(M->flags & FLAG_CREATED)); } @@ -1080,20 +1080,20 @@ static int fetch_comb_binlog_create_message_service (void *extra) { M->unread = fetch_int (); - tglf_fetch_message_action (&M->action); + tglf_fetch_message_action (TLS, &M->action); //M->unread = 1; - M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == TLS->our_id; M->service = 1; - tglm_message_insert (M); + tglm_message_insert (TLS, M); return 0; } -static int fetch_comb_binlog_create_message_service_encr (void *extra) { +static int fetch_comb_binlog_create_message_service_encr (struct tgl_state *TLS, void *extra) { long long id = fetch_long (); - struct tgl_message *M = tgl_message_get (id); + struct tgl_message *M = tgl_message_get (TLS, id); if (!M) { - M = tglm_message_alloc (id); + M = tglm_message_alloc (TLS, id); } else { assert (!(M->flags & FLAG_CREATED)); } @@ -1104,27 +1104,27 @@ static int fetch_comb_binlog_create_message_service_encr (void *extra) { M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); - struct tgl_secret_chat *E = (void *)tgl_peer_get (M->to_id); + struct tgl_secret_chat *E = (void *)tgl_peer_get (TLS, M->to_id); assert (E); - tglf_fetch_message_action_encrypted (&M->action); + tglf_fetch_message_action_encrypted (TLS, &M->action); M->unread = 1; - M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == TLS->our_id; M->service = 1; if (!M->out && M->action.type == tgl_message_action_notify_layer) { E->layer = M->action.layer; } - tglm_message_insert (M); + tglm_message_insert (TLS, M); return 0; } -static int fetch_comb_binlog_create_message_service_fwd (void *extra) { +static int fetch_comb_binlog_create_message_service_fwd (struct tgl_state *TLS, void *extra) { int id = fetch_int (); - struct tgl_message *M = tgl_message_get (id); + struct tgl_message *M = tgl_message_get (TLS, id); if (!M) { - M = tglm_message_alloc (id); + M = tglm_message_alloc (TLS, id); } else { assert (!(M->flags & FLAG_CREATED)); } @@ -1139,137 +1139,137 @@ static int fetch_comb_binlog_create_message_service_fwd (void *extra) { M->unread = fetch_int (); - tglf_fetch_message_action (&M->action); + tglf_fetch_message_action (TLS, &M->action); //M->unread = 1; - M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == TLS->our_id; M->service = 1; - tglm_message_insert (M); + tglm_message_insert (TLS, M); return 0; } -static int fetch_comb_binlog_message_set_unread_long (void *extra) { - struct tgl_message *M = tgl_message_get (fetch_long ()); +static int fetch_comb_binlog_message_set_unread_long (struct tgl_state *TLS, void *extra) { + struct tgl_message *M = tgl_message_get (TLS, fetch_long ()); assert (M); if (M->unread) { M->unread = 0; - if (tgl_state.callback.marked_read) { - tgl_state.callback.marked_read (1, &M); + if (TLS->callback.marked_read) { + TLS->callback.marked_read (TLS, 1, &M); } } return 0; } -static int fetch_comb_binlog_message_set_unread (void *extra) { - struct tgl_message *M = tgl_message_get (fetch_int ()); +static int fetch_comb_binlog_message_set_unread (struct tgl_state *TLS, void *extra) { + struct tgl_message *M = tgl_message_get (TLS, fetch_int ()); assert (M); if (M->unread) { M->unread = 0; - if (tgl_state.callback.marked_read) { - tgl_state.callback.marked_read (1, &M); + if (TLS->callback.marked_read) { + TLS->callback.marked_read (TLS, 1, &M); } } return 0; } -static int fetch_comb_binlog_set_message_sent (void *extra) { - struct tgl_message *M = tgl_message_get (fetch_long ()); +static int fetch_comb_binlog_set_message_sent (struct tgl_state *TLS, void *extra) { + struct tgl_message *M = tgl_message_get (TLS, fetch_long ()); assert (M); - tglm_message_remove_unsent (M); + tglm_message_remove_unsent (TLS, M); M->flags &= ~FLAG_PENDING; return 0; } -static int fetch_comb_binlog_set_msg_id (void *extra) { - struct tgl_message *M = tgl_message_get (fetch_long ()); +static int fetch_comb_binlog_set_msg_id (struct tgl_state *TLS, void *extra) { + struct tgl_message *M = tgl_message_get (TLS, fetch_long ()); assert (M); if (M->flags & FLAG_PENDING) { - tglm_message_remove_unsent (M); + tglm_message_remove_unsent (TLS, M); M->flags &= ~FLAG_PENDING; } - tglm_message_remove_tree (M); - tglm_message_del_peer (M); + tglm_message_remove_tree (TLS, M); + tglm_message_del_peer (TLS, M); M->id = fetch_int (); - if (tgl_message_get (M->id)) { - tgls_free_message (M); + if (tgl_message_get (TLS, M->id)) { + tgls_free_message (TLS, M); } else { - tglm_message_insert_tree (M); - tglm_message_add_peer (M); + tglm_message_insert_tree (TLS, M); + tglm_message_add_peer (TLS, M); } return 0; } -static int fetch_comb_binlog_delete_msg (void *extra) { - struct tgl_message *M = tgl_message_get (fetch_long ()); +static int fetch_comb_binlog_delete_msg (struct tgl_state *TLS, void *extra) { + struct tgl_message *M = tgl_message_get (TLS, fetch_long ()); assert (M); if (M->flags & FLAG_PENDING) { - tglm_message_remove_unsent (M); + tglm_message_remove_unsent (TLS, M); M->flags &= ~FLAG_PENDING; } - tglm_message_remove_tree (M); - tglm_message_del_peer (M); - tglm_message_del_use (M); - tgls_free_message (M); + tglm_message_remove_tree (TLS, M); + tglm_message_del_peer (TLS, M); + tglm_message_del_use (TLS, M); + tgls_free_message (TLS, M); return 0; } -static int fetch_comb_binlog_msg_seq_update (void *extra) { - struct tgl_message *M = tgl_message_get (fetch_long ()); +static int fetch_comb_binlog_msg_seq_update (struct tgl_state *TLS, void *extra) { + struct tgl_message *M = tgl_message_get (TLS, fetch_long ()); assert (M); - tgl_state.seq ++; - vlogprintf (E_DEBUG - 1 + 2 * in_replay_log, "seq %d=>%d\n", tgl_state.seq - 1, tgl_state.seq); + TLS->seq ++; + vlogprintf (E_DEBUG - 1 + 2 * in_replay_log, "seq %d=>%d\n", TLS->seq - 1, TLS->seq); if (!(M->flags & FLAG_ENCRYPTED)) { - if (tgl_state.max_msg_id < M->id) { - tgl_state.max_msg_id = M->id; + if (TLS->max_msg_id < M->id) { + TLS->max_msg_id = M->id; } } - if (tgl_state.callback.msg_receive) { - tgl_state.callback.msg_receive (M); + if (TLS->callback.msg_receive) { + TLS->callback.msg_receive (TLS, M); } return 0; } -static int fetch_comb_binlog_msg_update (void *extra) { - struct tgl_message *M = tgl_message_get (fetch_long ()); +static int fetch_comb_binlog_msg_update (struct tgl_state *TLS, void *extra) { + struct tgl_message *M = tgl_message_get (TLS, fetch_long ()); if (!M) { return 0; } assert (M); if (!(M->flags & FLAG_ENCRYPTED)) { - if (tgl_state.max_msg_id < M->id) { - tgl_state.max_msg_id = M->id; + if (TLS->max_msg_id < M->id) { + TLS->max_msg_id = M->id; } } - if (tgl_state.callback.msg_receive) { - tgl_state.callback.msg_receive (M); + if (TLS->callback.msg_receive) { + TLS->callback.msg_receive (TLS, M); } return 0; } -static int fetch_comb_binlog_reset_authorization (void *extra) { +static int fetch_comb_binlog_reset_authorization (struct tgl_state *TLS, void *extra) { int i; - for (i = 0; i <= tgl_state.max_dc_num; i++) if (tgl_state.DC_list[i]) { - struct tgl_dc *D = tgl_state.DC_list[i]; + for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i]) { + struct tgl_dc *D = TLS->DC_list[i]; D->flags = 0; D->state = st_init; D->auth_key_id = D->temp_auth_key_id = 0; D->has_auth = 0; } - tgl_state.seq = 0; - tgl_state.qts = 0; + TLS->seq = 0; + TLS->qts = 0; return 0; } #define FETCH_COMBINATOR_FUNCTION(NAME) \ case CODE_ ## NAME:\ - ok = fetch_comb_ ## NAME (0); \ + ok = fetch_comb_ ## NAME (TLS, 0); \ break; \ -static void replay_log_event (void) { +static void replay_log_event (struct tgl_state *TLS) { assert (rptr < wptr); int op = *rptr; @@ -1368,13 +1368,13 @@ static void replay_log_event (void) { rptr = in_ptr; } -static void create_new_binlog (void) { +static void create_new_binlog (struct tgl_state *TLS) { clear_packet (); //static int s[1000]; //packet_ptr = s; out_int (CODE_binlog_start); - if (tgl_state.test_mode) { + if (TLS->test_mode) { out_int (CODE_binlog_dc_option); out_int (1); out_string (""); @@ -1432,11 +1432,11 @@ static void create_new_binlog (void) { } -void tgl_replay_log (void) { - if (!tgl_state.binlog_enabled) { return; } +void tgl_replay_log (struct tgl_state *TLS) { + if (!TLS->binlog_enabled) { return; } if (access (get_binlog_file_name (), F_OK) < 0) { printf ("No binlog found. Creating new one\n"); - create_new_binlog (); + create_new_binlog (TLS); } int fd = open (get_binlog_file_name (), O_RDONLY); if (fd < 0) { @@ -1468,7 +1468,7 @@ void tgl_replay_log (void) { wptr += (k / 4); } if (wptr == rptr) { break; } - replay_log_event (); + replay_log_event (TLS); } in_replay_log = 0; close (fd); @@ -1476,41 +1476,41 @@ void tgl_replay_log (void) { static int b_packet_buffer[PACKET_BUFFER_SIZE]; -void tgl_reopen_binlog_for_writing (void) { - tgl_state.binlog_fd = open (get_binlog_file_name (), O_WRONLY); - if (tgl_state.binlog_fd < 0) { +void tgl_reopen_binlog_for_writing (struct tgl_state *TLS) { + TLS->binlog_fd = open (get_binlog_file_name (), O_WRONLY); + if (TLS->binlog_fd < 0) { perror ("binlog open"); exit (2); } - assert (lseek (tgl_state.binlog_fd, binlog_pos, SEEK_SET) == binlog_pos); - if (flock (tgl_state.binlog_fd, LOCK_EX | LOCK_NB) < 0) { + assert (lseek (TLS->binlog_fd, binlog_pos, SEEK_SET) == binlog_pos); + if (flock (TLS->binlog_fd, LOCK_EX | LOCK_NB) < 0) { perror ("get lock"); exit (2); } } -static void add_log_event (const int *data, int len) { +static void add_log_event (struct tgl_state *TLS, const int *data, int len) { vlogprintf (E_DEBUG, "Add log event: magic = 0x%08x, len = %d\n", data[0], len); assert (!(len & 3)); rptr = (void *)data; wptr = rptr + (len / 4); int *in = in_ptr; int *end = in_end; - replay_log_event (); + replay_log_event (TLS); if (rptr != wptr) { vlogprintf (E_ERROR, "Unread %lld ints. Len = %d\n", (long long)(wptr - rptr), len); assert (rptr == wptr); } - if (tgl_state.binlog_enabled) { - assert (tgl_state.binlog_fd > 0); - assert (write (tgl_state.binlog_fd, data, len) == len); + if (TLS->binlog_enabled) { + assert (TLS->binlog_fd > 0); + assert (write (TLS->binlog_fd, data, len) == len); } in_ptr = in; in_end = end; } -void bl_do_set_auth_key_id (int num, unsigned char *buf) { +void bl_do_set_auth_key_id (struct tgl_state *TLS, int num, unsigned char *buf) { static unsigned char sha1_buffer[20]; SHA1 (buf, 256, sha1_buffer); long long fingerprint = *(long long *)(sha1_buffer + 12); @@ -1519,22 +1519,22 @@ void bl_do_set_auth_key_id (int num, unsigned char *buf) { ev[1] = num; *(long long *)(ev + 2) = fingerprint; memcpy (ev + 4, buf, 256); - add_log_event (ev, 8 + 8 + 256); + add_log_event (TLS, ev, 8 + 8 + 256); } -void bl_do_set_our_id (int id) { - if (tgl_state.our_id) { - assert (tgl_state.our_id == id); +void bl_do_set_our_id (struct tgl_state *TLS, int id) { + if (TLS->our_id) { + assert (TLS->our_id == id); return; } int *ev = alloc_log_event (8); ev[0] = CODE_binlog_our_id; ev[1] = id; - add_log_event (ev, 8); + add_log_event (TLS, ev, 8); //write_auth_file (); } -void bl_do_user_add (int id, const char *f, int fl, const char *l, int ll, long long access_token, const char *p, int pl, int contact) { +void bl_do_user_add (struct tgl_state *TLS, int id, const char *f, int fl, const char *l, int ll, long long access_token, const char *p, int pl, int contact) { clear_packet (); out_int (CODE_binlog_user_add); out_int (id); @@ -1543,25 +1543,25 @@ void bl_do_user_add (int id, const char *f, int fl, const char *l, int ll, long out_long (access_token); out_cstring (p ? p : "", pl); out_int (contact); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_user_delete (struct tgl_user *U) { +void bl_do_user_delete (struct tgl_state *TLS, struct tgl_user *U) { if (U->flags & FLAG_DELETED) { return; } int *ev = alloc_log_event (8); ev[0] = CODE_binlog_user_delete; ev[1] = tgl_get_peer_id (U->id); - add_log_event (ev, 8); + add_log_event (TLS, ev, 8); } -void bl_do_set_user_profile_photo (struct tgl_user *U, long long photo_id, struct tgl_file_location *big, struct tgl_file_location *small) { +void bl_do_set_user_profile_photo (struct tgl_state *TLS, struct tgl_user *U, long long photo_id, struct tgl_file_location *big, struct tgl_file_location *small) { if (photo_id == U->photo_id) { return; } if (!photo_id) { int *ev = alloc_log_event (12); ev[0] = CODE_binlog_user_set_photo; ev[1] = tgl_get_peer_id (U->id); ev[2] = CODE_user_profile_photo_empty; - add_log_event (ev, 12); + add_log_event (TLS, ev, 12); } else { clear_packet (); out_int (CODE_binlog_user_set_photo); @@ -1592,11 +1592,11 @@ void bl_do_set_user_profile_photo (struct tgl_user *U, long long photo_id, struc out_int (big->local_id); out_long (big->secret); } - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } } -void bl_do_user_set_name (struct tgl_user *U, const char *f, int fl, const char *l, int ll) { +void bl_do_user_set_name (struct tgl_state *TLS, struct tgl_user *U, const char *f, int fl, const char *l, int ll) { if ((U->first_name && (int)strlen (U->first_name) == fl && !strncmp (U->first_name, f, fl)) && (U->last_name && (int)strlen (U->last_name) == ll && !strncmp (U->last_name, l, ll))) { return; @@ -1606,10 +1606,10 @@ void bl_do_user_set_name (struct tgl_user *U, const char *f, int fl, const char out_int (tgl_get_peer_id (U->id)); out_cstring (f, fl); out_cstring (l, ll); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_user_set_username (struct tgl_user *U, const char *f, int l) { +void bl_do_user_set_username (struct tgl_state *TLS, struct tgl_user *U, const char *f, int l) { if ((U->username && (int)strlen (U->username) == l && !strncmp (U->username, f, l)) || (!l && !U->username)) { return; @@ -1618,19 +1618,19 @@ void bl_do_user_set_username (struct tgl_user *U, const char *f, int l) { out_int (CODE_binlog_user_set_username); out_int (tgl_get_peer_id (U->id)); out_cstring (f, l); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_user_set_access_hash (struct tgl_user *U, long long access_token) { +void bl_do_user_set_access_hash (struct tgl_state *TLS, struct tgl_user *U, long long access_token) { if (U->access_hash == access_token) { return; } int *ev = alloc_log_event (16); ev[0] = CODE_binlog_user_set_access_hash; ev[1] = tgl_get_peer_id (U->id); *(long long *)(ev + 2) = access_token; - add_log_event (ev, 16); + add_log_event (TLS, ev, 16); } -void bl_do_user_set_phone (struct tgl_user *U, const char *p, int pl) { +void bl_do_user_set_phone (struct tgl_state *TLS, struct tgl_user *U, const char *p, int pl) { if (U->phone && (int)strlen (U->phone) == pl && !strncmp (U->phone, p, pl)) { return; } @@ -1638,20 +1638,20 @@ void bl_do_user_set_phone (struct tgl_user *U, const char *p, int pl) { out_int (CODE_binlog_user_set_phone); out_int (tgl_get_peer_id (U->id)); out_cstring (p, pl); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_user_set_friend (struct tgl_user *U, int friend) { +void bl_do_user_set_friend (struct tgl_state *TLS, struct tgl_user *U, int friend) { if (friend == ((U->flags & FLAG_USER_CONTACT) != 0)) { return ; } int *ev = alloc_log_event (12); ev[0] = CODE_binlog_user_set_friend; ev[1] = tgl_get_peer_id (U->id); ev[2] = friend; - add_log_event (ev, 12); + add_log_event (TLS, ev, 12); } -void bl_do_dc_option (int id, int l1, const char *name, int l2, const char *ip, int port) { - struct tgl_dc *DC = tgl_state.DC_list[id]; +void bl_do_dc_option (struct tgl_state *TLS, int id, int l1, const char *name, int l2, const char *ip, int port) { + struct tgl_dc *DC = TLS->DC_list[id]; if (DC && !strncmp (ip, DC->ip, l2)) { return; } clear_packet (); @@ -1661,42 +1661,42 @@ void bl_do_dc_option (int id, int l1, const char *name, int l2, const char *ip, out_cstring (ip, l2); out_int (port); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_dc_signed (int id) { +void bl_do_dc_signed (struct tgl_state *TLS, int id) { int *ev = alloc_log_event (8); ev[0] = CODE_binlog_dc_signed; ev[1] = id; - add_log_event (ev, 8); + add_log_event (TLS, ev, 8); } -void bl_do_set_working_dc (int num) { +void bl_do_set_working_dc (struct tgl_state *TLS, int num) { int *ev = alloc_log_event (8); ev[0] = CODE_binlog_default_dc; ev[1] = num; - add_log_event (ev, 8); + add_log_event (TLS, ev, 8); } -void bl_do_user_set_full_photo (struct tgl_user *U, const int *start, int len) { +void bl_do_user_set_full_photo (struct tgl_state *TLS, struct tgl_user *U, const int *start, int len) { if (U->photo.id == *(long long *)(start + 1)) { return; } int *ev = alloc_log_event (len + 8); ev[0] = CODE_binlog_user_set_full_photo; ev[1] = tgl_get_peer_id (U->id); memcpy (ev + 2, start, len); - add_log_event (ev, len + 8); + add_log_event (TLS, ev, len + 8); } -void bl_do_user_set_blocked (struct tgl_user *U, int blocked) { +void bl_do_user_set_blocked (struct tgl_state *TLS, struct tgl_user *U, int blocked) { if (U->blocked == blocked) { return; } int *ev = alloc_log_event (12); ev[0] = CODE_binlog_user_set_blocked; ev[1] = tgl_get_peer_id (U->id); ev[2] = blocked; - add_log_event (ev, 12); + add_log_event (TLS, ev, 12); } -void bl_do_user_set_real_name (struct tgl_user *U, const char *f, int fl, const char *l, int ll) { +void bl_do_user_set_real_name (struct tgl_state *TLS, struct tgl_user *U, const char *f, int fl, const char *l, int ll) { if ((U->real_first_name && (int)strlen (U->real_first_name) == fl && !strncmp (U->real_first_name, f, fl)) && (U->real_last_name && (int)strlen (U->real_last_name) == ll && !strncmp (U->real_last_name, l, ll))) { return; @@ -1706,18 +1706,18 @@ void bl_do_user_set_real_name (struct tgl_user *U, const char *f, int fl, const out_int (tgl_get_peer_id (U->id)); out_cstring (f, fl); out_cstring (l, ll); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_encr_chat_delete (struct tgl_secret_chat *U) { +void bl_do_encr_chat_delete (struct tgl_state *TLS, struct tgl_secret_chat *U) { if (!(U->flags & FLAG_CREATED) || U->state == sc_deleted || U->state == sc_none) { return; } int *ev = alloc_log_event (8); ev[0] = CODE_binlog_encr_chat_delete; ev[1] = tgl_get_peer_id (U->id); - add_log_event (ev, 8); + add_log_event (TLS, ev, 8); } -void bl_do_encr_chat_requested (struct tgl_secret_chat *U, long long access_hash, int date, int admin_id, int user_id, unsigned char g_key[], unsigned char nonce[]) { +void bl_do_encr_chat_requested (struct tgl_state *TLS, struct tgl_secret_chat *U, long long access_hash, int date, int admin_id, int user_id, unsigned char g_key[], unsigned char nonce[]) { if (U->state != sc_none) { return; } int *ev = alloc_log_event (540); ev[0] = CODE_binlog_encr_chat_requested; @@ -1728,55 +1728,55 @@ void bl_do_encr_chat_requested (struct tgl_secret_chat *U, long long access_hash ev[6] = user_id; memcpy (ev + 7, g_key, 256); memcpy (ev + 7 + 64, nonce, 256); - add_log_event (ev, 540); + add_log_event (TLS, ev, 540); } -void bl_do_encr_chat_set_access_hash (struct tgl_secret_chat *U, long long access_hash) { +void bl_do_encr_chat_set_access_hash (struct tgl_state *TLS, struct tgl_secret_chat *U, long long access_hash) { if (U->access_hash == access_hash) { return; } int *ev = alloc_log_event (16); ev[0] = CODE_binlog_encr_chat_set_access_hash; ev[1] = tgl_get_peer_id (U->id); *(long long *)(ev + 2) = access_hash; - add_log_event (ev, 16); + add_log_event (TLS, ev, 16); } -void bl_do_encr_chat_set_date (struct tgl_secret_chat *U, int date) { +void bl_do_encr_chat_set_date (struct tgl_state *TLS, struct tgl_secret_chat *U, int date) { if (U->date == date) { return; } int *ev = alloc_log_event (12); ev[0] = CODE_binlog_encr_chat_set_date; ev[1] = tgl_get_peer_id (U->id); ev[2] = date; - add_log_event (ev, 12); + add_log_event (TLS, ev, 12); } -void bl_do_encr_chat_set_ttl (struct tgl_secret_chat *U, int ttl) { +void bl_do_encr_chat_set_ttl (struct tgl_state *TLS, struct tgl_secret_chat *U, int ttl) { if (U->ttl == ttl) { return; } int *ev = alloc_log_event (12); ev[0] = CODE_binlog_encr_chat_set_ttl; ev[1] = tgl_get_peer_id (U->id); ev[2] = ttl; - add_log_event (ev, 12); + add_log_event (TLS, ev, 12); } -void bl_do_encr_chat_set_layer (struct tgl_secret_chat *U, int layer) { +void bl_do_encr_chat_set_layer (struct tgl_state *TLS, struct tgl_secret_chat *U, int layer) { if (U->layer >= layer) { return; } int *ev = alloc_log_event (12); ev[0] = CODE_binlog_encr_chat_set_layer; ev[1] = tgl_get_peer_id (U->id); ev[2] = layer; - add_log_event (ev, 12); + add_log_event (TLS, ev, 12); } -void bl_do_encr_chat_set_state (struct tgl_secret_chat *U, enum tgl_secret_chat_state state) { +void bl_do_encr_chat_set_state (struct tgl_state *TLS, struct tgl_secret_chat *U, enum tgl_secret_chat_state state) { if (U->state == state) { return; } int *ev = alloc_log_event (12); ev[0] = CODE_binlog_encr_chat_set_state; ev[1] = tgl_get_peer_id (U->id); ev[2] = state; - add_log_event (ev, 12); + add_log_event (TLS, ev, 12); } -void bl_do_encr_chat_accepted (struct tgl_secret_chat *U, const unsigned char g_key[], const unsigned char nonce[], long long key_fingerprint) { +void bl_do_encr_chat_accepted (struct tgl_state *TLS, struct tgl_secret_chat *U, const unsigned char g_key[], const unsigned char nonce[], long long key_fingerprint) { if (U->state != sc_waiting && U->state != sc_request) { return; } int *ev = alloc_log_event (528); ev[0] = CODE_binlog_encr_chat_accepted; @@ -1784,103 +1784,103 @@ void bl_do_encr_chat_accepted (struct tgl_secret_chat *U, const unsigned char g_ memcpy (ev + 2, g_key, 256); memcpy (ev + 66, nonce, 256); *(long long *)(ev + 130) = key_fingerprint; - add_log_event (ev, 528); + add_log_event (TLS, ev, 528); } -void bl_do_encr_chat_create (int id, int user_id, int admin_id, char *name, int name_len) { +void bl_do_encr_chat_create (struct tgl_state *TLS, int id, int user_id, int admin_id, char *name, int name_len) { clear_packet (); out_int (CODE_binlog_encr_chat_create); out_int (id); out_int (user_id); out_int (admin_id); out_cstring (name, name_len); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_encr_chat_set_key (struct tgl_secret_chat *E, unsigned char key[], long long key_fingerprint) { +void bl_do_encr_chat_set_key (struct tgl_state *TLS, struct tgl_secret_chat *E, unsigned char key[], long long key_fingerprint) { int *ev = alloc_log_event (272); ev[0] = CODE_binlog_encr_chat_set_key; ev[1] = tgl_get_peer_id (E->id); memcpy (ev + 2, key, 256); *(long long *)(ev + 66) = key_fingerprint; - add_log_event (ev, 272); + add_log_event (TLS, ev, 272); } -void bl_do_encr_chat_update_seq (struct tgl_secret_chat *E, int in_seq_no, int out_seq_no) { +void bl_do_encr_chat_update_seq (struct tgl_state *TLS, struct tgl_secret_chat *E, int in_seq_no, int out_seq_no) { int *ev = alloc_log_event (16); ev[0] = CODE_binlog_encr_chat_update_seq; ev[1] = tgl_get_peer_id (E->id); ev[2] = in_seq_no; ev[3] = out_seq_no; - add_log_event (ev, 16); + add_log_event (TLS, ev, 16); } -void bl_do_encr_chat_set_seq (struct tgl_secret_chat *E, int in_seq_no, int last_in_seq_no, int out_seq_no) { +void bl_do_encr_chat_set_seq (struct tgl_state *TLS, struct tgl_secret_chat *E, int in_seq_no, int last_in_seq_no, int out_seq_no) { int *ev = alloc_log_event (20); ev[0] = CODE_binlog_encr_chat_set_seq; ev[1] = tgl_get_peer_id (E->id); ev[2] = in_seq_no; ev[3] = last_in_seq_no; ev[4] = out_seq_no; - add_log_event (ev, 20); + add_log_event (TLS, ev, 20); } -void bl_do_set_dh_params (int root, unsigned char prime[], int version) { +void bl_do_set_dh_params (struct tgl_state *TLS, int root, unsigned char prime[], int version) { int *ev = alloc_log_event (268); ev[0] = CODE_binlog_set_dh_params; ev[1] = root; memcpy (ev + 2, prime, 256); ev[66] = version; - add_log_event (ev, 268); + add_log_event (TLS, ev, 268); } -void bl_do_encr_chat_init (int id, int user_id, unsigned char random[], unsigned char g_a[]) { +void bl_do_encr_chat_init (struct tgl_state *TLS, int id, int user_id, unsigned char random[], unsigned char g_a[]) { int *ev = alloc_log_event (524); ev[0] = CODE_binlog_encr_chat_init; ev[1] = id; ev[2] = user_id; memcpy (ev + 3, random, 256); memcpy (ev + 67, g_a, 256); - add_log_event (ev, 524); + add_log_event (TLS, ev, 524); } -void bl_do_set_pts (int pts) { - if (tgl_state.locks & TGL_LOCK_DIFF) { return; } - if (pts <= tgl_state.pts) { return; } +void bl_do_set_pts (struct tgl_state *TLS, int pts) { + if (TLS->locks & TGL_LOCK_DIFF) { return; } + if (pts <= TLS->pts) { return; } int *ev = alloc_log_event (8); ev[0] = CODE_binlog_set_pts; ev[1] = pts; - add_log_event (ev, 8); + add_log_event (TLS, ev, 8); } -void bl_do_set_qts (int qts) { - if (tgl_state.locks & TGL_LOCK_DIFF) { return; } - if (qts <= tgl_state.qts) { return; } +void bl_do_set_qts (struct tgl_state *TLS, int qts) { + if (TLS->locks & TGL_LOCK_DIFF) { return; } + if (qts <= TLS->qts) { return; } int *ev = alloc_log_event (8); ev[0] = CODE_binlog_set_qts; ev[1] = qts; - add_log_event (ev, 8); + add_log_event (TLS, ev, 8); } -void bl_do_set_date (int date) { - if (tgl_state.locks & TGL_LOCK_DIFF) { return; } - if (date <= tgl_state.date) { return; } +void bl_do_set_date (struct tgl_state *TLS, int date) { + if (TLS->locks & TGL_LOCK_DIFF) { return; } + if (date <= TLS->date) { return; } int *ev = alloc_log_event (8); ev[0] = CODE_binlog_set_date; ev[1] = date; - add_log_event (ev, 8); + add_log_event (TLS, ev, 8); } -void bl_do_set_seq (int seq) { - if (tgl_state.locks & TGL_LOCK_DIFF) { return; } - if (seq == tgl_state.seq) { return; } +void bl_do_set_seq (struct tgl_state *TLS, int seq) { + if (TLS->locks & TGL_LOCK_DIFF) { return; } + if (seq == TLS->seq) { return; } int *ev = alloc_log_event (8); ev[0] = CODE_binlog_set_seq; ev[1] = seq; - add_log_event (ev, 8); + add_log_event (TLS, ev, 8); } -void bl_do_create_chat (struct tgl_chat *C, int y, const char *s, int l, int users_num, int date, int version, struct tgl_file_location *big, struct tgl_file_location *small) { +void bl_do_create_chat (struct tgl_state *TLS, struct tgl_chat *C, int y, const char *s, int l, int users_num, int date, int version, struct tgl_file_location *big, struct tgl_file_location *small) { clear_packet (); out_int (CODE_binlog_chat_create); out_int (tgl_get_peer_id (C->id)); @@ -1891,10 +1891,10 @@ void bl_do_create_chat (struct tgl_chat *C, int y, const char *s, int l, int use out_int (version); out_data (big, sizeof (struct tgl_file_location)); out_data (small, sizeof (struct tgl_file_location)); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_chat_forbid (struct tgl_chat *C, int on) { +void bl_do_chat_forbid (struct tgl_state *TLS, struct tgl_chat *C, int on) { if (on) { if (C->flags & FLAG_FORBIDDEN) { return; } int *ev = alloc_log_event (16); @@ -1902,7 +1902,7 @@ void bl_do_chat_forbid (struct tgl_chat *C, int on) { ev[1] = tgl_get_peer_id (C->id); ev[2] = FLAG_FORBIDDEN; ev[3] = 0; - add_log_event (ev, 16); + add_log_event (TLS, ev, 16); } else { if (!(C->flags & FLAG_FORBIDDEN)) { return; } int *ev = alloc_log_event (16); @@ -1910,20 +1910,20 @@ void bl_do_chat_forbid (struct tgl_chat *C, int on) { ev[1] = tgl_get_peer_id (C->id); ev[2] = 0; ev[3] = FLAG_FORBIDDEN; - add_log_event (ev, 16); + add_log_event (TLS, ev, 16); } } -void bl_do_chat_set_title (struct tgl_chat *C, const char *s, int l) { +void bl_do_chat_set_title (struct tgl_state *TLS, struct tgl_chat *C, const char *s, int l) { if (C->title && (int)strlen (C->title) == l && !strncmp (C->title, s, l)) { return; } clear_packet (); out_int (CODE_binlog_chat_set_title); out_int (tgl_get_peer_id (C->id)); out_cstring (s, l); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_chat_set_photo (struct tgl_chat *C, struct tgl_file_location *big, struct tgl_file_location *small) { +void bl_do_chat_set_photo (struct tgl_state *TLS, struct tgl_chat *C, struct tgl_file_location *big, struct tgl_file_location *small) { if (!memcmp (&C->photo_small, small, sizeof (struct tgl_file_location)) && !memcmp (&C->photo_big, big, sizeof (struct tgl_file_location))) { return; } clear_packet (); @@ -1931,19 +1931,19 @@ void bl_do_chat_set_photo (struct tgl_chat *C, struct tgl_file_location *big, st out_int (tgl_get_peer_id (C->id)); out_data (big, sizeof (struct tgl_file_location)); out_data (small, sizeof (struct tgl_file_location)); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_chat_set_date (struct tgl_chat *C, int date) { +void bl_do_chat_set_date (struct tgl_state *TLS, struct tgl_chat *C, int date) { if (C->date == date) { return; } int *ev = alloc_log_event (12); ev[0] = CODE_binlog_chat_set_date; ev[1] = tgl_get_peer_id (C->id); ev[2] = date; - add_log_event (ev, 12); + add_log_event (TLS, ev, 12); } -void bl_do_chat_set_set_in_chat (struct tgl_chat *C, int on) { +void bl_do_chat_set_set_in_chat (struct tgl_state *TLS, struct tgl_chat *C, int on) { if (on) { if (C->flags & FLAG_CHAT_IN_CHAT) { return; } int *ev = alloc_log_event (16); @@ -1951,7 +1951,7 @@ void bl_do_chat_set_set_in_chat (struct tgl_chat *C, int on) { ev[1] = tgl_get_peer_id (C->id); ev[2] = FLAG_CHAT_IN_CHAT; ev[3] = 0; - add_log_event (ev, 16); + add_log_event (TLS, ev, 16); } else { if (!(C->flags & FLAG_CHAT_IN_CHAT)) { return; } int *ev = alloc_log_event (16); @@ -1959,30 +1959,30 @@ void bl_do_chat_set_set_in_chat (struct tgl_chat *C, int on) { ev[1] = tgl_get_peer_id (C->id); ev[2] = 0; ev[3] = FLAG_CHAT_IN_CHAT; - add_log_event (ev, 16); + add_log_event (TLS, ev, 16); } } -void bl_do_chat_set_version (struct tgl_chat *C, int version, int user_num) { +void bl_do_chat_set_version (struct tgl_state *TLS, struct tgl_chat *C, int version, int user_num) { if (C->version >= version) { return; } int *ev = alloc_log_event (16); ev[0] = CODE_binlog_chat_set_version; ev[1] = tgl_get_peer_id (C->id); ev[2] = version; ev[3] = user_num; - add_log_event (ev, 16); + add_log_event (TLS, ev, 16); } -void bl_do_chat_set_admin (struct tgl_chat *C, int admin) { +void bl_do_chat_set_admin (struct tgl_state *TLS, struct tgl_chat *C, int admin) { if (C->admin_id == admin) { return; } int *ev = alloc_log_event (12); ev[0] = CODE_binlog_chat_set_admin; ev[1] = tgl_get_peer_id (C->id); ev[2] = admin; - add_log_event (ev, 12); + add_log_event (TLS, ev, 12); } -void bl_do_chat_set_participants (struct tgl_chat *C, int version, int user_num, struct tgl_chat_user *users) { +void bl_do_chat_set_participants (struct tgl_state *TLS, struct tgl_chat *C, int version, int user_num, struct tgl_chat_user *users) { if (C->user_list_version >= version) { return; } int *ev = alloc_log_event (12 * user_num + 16); ev[0] = CODE_binlog_chat_set_participants; @@ -1990,19 +1990,19 @@ void bl_do_chat_set_participants (struct tgl_chat *C, int version, int user_num, ev[2] = version; ev[3] = user_num; memcpy (ev + 4, users, 12 * user_num); - add_log_event (ev, 12 * user_num + 16); + add_log_event (TLS, ev, 12 * user_num + 16); } -void bl_do_chat_set_full_photo (struct tgl_chat *U, const int *start, int len) { +void bl_do_chat_set_full_photo (struct tgl_state *TLS, struct tgl_chat *U, const int *start, int len) { if (U->photo.id == *(long long *)(start + 1)) { return; } int *ev = alloc_log_event (len + 8); ev[0] = CODE_binlog_chat_set_full_photo; ev[1] = tgl_get_peer_id (U->id); memcpy (ev + 2, start, len); - add_log_event (ev, len + 8); + add_log_event (TLS, ev, len + 8); } -void bl_do_chat_add_user (struct tgl_chat *C, int version, int user, int inviter, int date) { +void bl_do_chat_add_user (struct tgl_state *TLS, struct tgl_chat *C, int version, int user, int inviter, int date) { if (C->user_list_version >= version || !C->user_list_version) { return; } int *ev = alloc_log_event (24); ev[0] = CODE_binlog_chat_add_participant; @@ -2011,20 +2011,20 @@ void bl_do_chat_add_user (struct tgl_chat *C, int version, int user, int inviter ev[3] = user; ev[4] = inviter; ev[5] = date; - add_log_event (ev, 24); + add_log_event (TLS, ev, 24); } -void bl_do_chat_del_user (struct tgl_chat *C, int version, int user) { +void bl_do_chat_del_user (struct tgl_state *TLS, struct tgl_chat *C, int version, int user) { if (C->user_list_version >= version || !C->user_list_version) { return; } int *ev = alloc_log_event (16); ev[0] = CODE_binlog_chat_del_participant; ev[1] = tgl_get_peer_id (C->id); ev[2] = version; ev[3] = user; - add_log_event (ev, 16); + add_log_event (TLS, ev, 16); } -void bl_do_create_message_text (int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s) { +void bl_do_create_message_text (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s) { clear_packet (); out_int (CODE_binlog_create_message_text); out_int (msg_id); @@ -2034,10 +2034,10 @@ void bl_do_create_message_text (int msg_id, int from_id, int to_type, int to_id, out_int (date); out_int (unread); out_cstring (s, l); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_send_message_text (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s) { +void bl_do_send_message_text (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s) { clear_packet (); out_int (CODE_binlog_send_message_text); out_long (msg_id); @@ -2046,10 +2046,10 @@ void bl_do_send_message_text (long long msg_id, int from_id, int to_type, int to out_int (to_id); out_int (date); out_cstring (s, l); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_send_message_action_encr (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const int *action) { +void bl_do_send_message_action_encr (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, int l, const int *action) { clear_packet (); out_int (CODE_binlog_send_message_action_encr); out_long (msg_id); @@ -2058,10 +2058,10 @@ void bl_do_send_message_action_encr (long long msg_id, int from_id, int to_type, out_int (to_id); out_int (date); out_ints (action, l); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_create_message_text_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s) { +void bl_do_create_message_text_fwd (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s) { clear_packet (); out_int (CODE_binlog_create_message_text_fwd); out_int (msg_id); @@ -2073,10 +2073,10 @@ void bl_do_create_message_text_fwd (int msg_id, int from_id, int to_type, int to out_int (fwd_date); out_int (unread); out_cstring (s, l); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_create_message_media (int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s, const int *data, int len) { +void bl_do_create_message_media (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s, const int *data, int len) { clear_packet (); out_int (CODE_binlog_create_message_media); out_int (msg_id); @@ -2087,10 +2087,10 @@ void bl_do_create_message_media (int msg_id, int from_id, int to_type, int to_id out_int (unread); out_cstring (s, l); out_ints (data, len); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_create_message_media_encr (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len, const int *data2, int len2) { +void bl_do_create_message_media_encr (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len, const int *data2, int len2) { clear_packet (); out_int (CODE_binlog_create_message_media_encr); out_long (msg_id); @@ -2101,10 +2101,10 @@ void bl_do_create_message_media_encr (long long msg_id, int from_id, int to_type out_cstring (s, l); out_ints (data, len); out_ints (data2, len2); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_create_message_media_encr_pending (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len) { +void bl_do_create_message_media_encr_pending (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len) { int *s_packet_buffer = packet_buffer; int *s_packet_ptr = packet_ptr; packet_buffer = b_packet_buffer; @@ -2117,20 +2117,20 @@ void bl_do_create_message_media_encr_pending (long long msg_id, int from_id, int out_int (date); out_cstring (s, l); out_ints (data, len); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); packet_buffer = s_packet_buffer; packet_ptr = s_packet_ptr; } -void bl_do_create_message_media_encr_sent (long long msg_id, const int *data, int len) { +void bl_do_create_message_media_encr_sent (struct tgl_state *TLS, long long msg_id, const int *data, int len) { clear_packet (); out_int (CODE_binlog_create_message_media_encr_sent); out_long (msg_id); out_ints (data, len); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_create_message_media_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s, const int *data, int len) { +void bl_do_create_message_media_fwd (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s, const int *data, int len) { clear_packet (); out_int (CODE_binlog_create_message_media_fwd); out_int (msg_id); @@ -2143,10 +2143,10 @@ void bl_do_create_message_media_fwd (int msg_id, int from_id, int to_type, int t out_int (unread); out_cstring (s, l); out_ints (data, len); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_create_message_service (int msg_id, int from_id, int to_type, int to_id, int date, int unread, const int *data, int len) { +void bl_do_create_message_service (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int unread, const int *data, int len) { clear_packet (); out_int (CODE_binlog_create_message_service); out_int (msg_id); @@ -2156,10 +2156,10 @@ void bl_do_create_message_service (int msg_id, int from_id, int to_type, int to_ out_int (date); out_int (unread); out_ints (data, len); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_create_message_service_encr (long long msg_id, int from_id, int to_type, int to_id, int date, const int *data, int len) { +void bl_do_create_message_service_encr (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, const int *data, int len) { clear_packet (); out_int (CODE_binlog_create_message_service_encr); out_long (msg_id); @@ -2168,10 +2168,10 @@ void bl_do_create_message_service_encr (long long msg_id, int from_id, int to_ty out_int (to_id); out_int (date); out_ints (data, len); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_create_message_service_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, const int *data, int len) { +void bl_do_create_message_service_fwd (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, const int *data, int len) { clear_packet (); out_int (CODE_binlog_create_message_service_fwd); out_int (msg_id); @@ -2183,71 +2183,71 @@ void bl_do_create_message_service_fwd (int msg_id, int from_id, int to_type, int out_int (fwd_date); out_int (unread); out_ints (data, len); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_set_unread_long (struct tgl_message *M, int unread) { +void bl_do_set_unread_long (struct tgl_state *TLS, struct tgl_message *M, int unread) { if (unread || !M->unread) { return; } clear_packet (); out_int (CODE_binlog_message_set_unread_long); out_long (M->id); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_set_unread (struct tgl_message *M, int unread) { - if (M->id != (int)M->id) { bl_do_set_unread_long (M, unread); } +void bl_do_set_unread (struct tgl_state *TLS, struct tgl_message *M, int unread) { + if (M->id != (int)M->id) { bl_do_set_unread_long (TLS, M, unread); } if (unread || !M->unread) { return; } clear_packet (); out_int (CODE_binlog_message_set_unread); out_int (M->id); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_set_message_sent (struct tgl_message *M) { +void bl_do_set_message_sent (struct tgl_state *TLS, struct tgl_message *M) { if (!(M->flags & FLAG_PENDING)) { return; } clear_packet (); out_int (CODE_binlog_set_message_sent); out_long (M->id); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_set_msg_id (struct tgl_message *M, int id) { +void bl_do_set_msg_id (struct tgl_state *TLS, struct tgl_message *M, int id) { if (M->id == id) { return; } clear_packet (); out_int (CODE_binlog_set_msg_id); out_long (M->id); out_int (id); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_delete_msg (struct tgl_message *M) { +void bl_do_delete_msg (struct tgl_state *TLS, struct tgl_message *M) { clear_packet (); out_int (CODE_binlog_delete_msg); out_long (M->id); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_msg_seq_update (long long id) { - if (tgl_state.locks & TGL_LOCK_DIFF) { +void bl_do_msg_seq_update (struct tgl_state *TLS, long long id) { + if (TLS->locks & TGL_LOCK_DIFF) { return; // We will receive this update in get_difference, that works now } clear_packet (); out_int (CODE_binlog_msg_seq_update); out_long (id); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_msg_update (long long id) { +void bl_do_msg_update (struct tgl_state *TLS, long long id) { clear_packet (); out_int (CODE_binlog_msg_update); out_long (id); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_reset_authorization (void) { +void bl_do_reset_authorization (struct tgl_state *TLS) { clear_packet (); out_int (CODE_binlog_reset_authorization); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); } /*void bl_do_add_dc (int id, const char *ip, int l, int port, long long auth_key_id, const char *auth_key) { clear_packet (); @@ -2257,5 +2257,5 @@ void bl_do_reset_authorization (void) { out_int (port); out_long (auth_key_id); out_ints ((void *)auth_key, 64); - add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + add_log_event (TLS, packet_buffer, 4 * (packet_ptr - packet_buffer)); }*/ diff --git a/binlog.h b/binlog.h index 858752c..e17c032 100644 --- a/binlog.h +++ b/binlog.h @@ -23,80 +23,80 @@ //#include "structures.h" #include "tgl.h" -void bl_do_set_auth_key_id (int num, unsigned char *buf); +void bl_do_set_auth_key_id (struct tgl_state *TLS, int num, unsigned char *buf); -void bl_do_dc_option (int id, int l1, const char *name, int l2, const char *ip, int port); +void bl_do_dc_option (struct tgl_state *TLS, int id, int l1, const char *name, int l2, const char *ip, int port); -void bl_do_set_our_id (int id); -void bl_do_user_add (int id, const char *f, int fl, const char *l, int ll, long long access_token, const char *p, int pl, int contact); -void bl_do_user_delete (struct tgl_user *U); -void bl_do_set_user_profile_photo (struct tgl_user *U, long long photo_id, struct tgl_file_location *big, struct tgl_file_location *small); -void bl_do_user_set_name (struct tgl_user *U, const char *f, int fl, const char *l, int ll); -void bl_do_user_set_username (struct tgl_user *U, const char *f, int l); -void bl_do_user_set_access_hash (struct tgl_user *U, long long access_token); -void bl_do_user_set_phone (struct tgl_user *U, const char *p, int pl); -void bl_do_user_set_friend (struct tgl_user *U, int friend); -void bl_do_user_set_full_photo (struct tgl_user *U, const int *start, int len); -void bl_do_user_set_blocked (struct tgl_user *U, int blocked); -void bl_do_user_set_real_name (struct tgl_user *U, const char *f, int fl, const char *l, int ll); +void bl_do_set_our_id (struct tgl_state *TLS, int id); +void bl_do_user_add (struct tgl_state *TLS, int id, const char *f, int fl, const char *l, int ll, long long access_token, const char *p, int pl, int contact); +void bl_do_user_delete (struct tgl_state *TLS, struct tgl_user *U); +void bl_do_set_user_profile_photo (struct tgl_state *TLS, struct tgl_user *U, long long photo_id, struct tgl_file_location *big, struct tgl_file_location *small); +void bl_do_user_set_name (struct tgl_state *TLS, struct tgl_user *U, const char *f, int fl, const char *l, int ll); +void bl_do_user_set_username (struct tgl_state *TLS, struct tgl_user *U, const char *f, int l); +void bl_do_user_set_access_hash (struct tgl_state *TLS, struct tgl_user *U, long long access_token); +void bl_do_user_set_phone (struct tgl_state *TLS, struct tgl_user *U, const char *p, int pl); +void bl_do_user_set_friend (struct tgl_state *TLS, struct tgl_user *U, int friend); +void bl_do_user_set_full_photo (struct tgl_state *TLS, struct tgl_user *U, const int *start, int len); +void bl_do_user_set_blocked (struct tgl_state *TLS, struct tgl_user *U, int blocked); +void bl_do_user_set_real_name (struct tgl_state *TLS, struct tgl_user *U, const char *f, int fl, const char *l, int ll); -void bl_do_encr_chat_delete (struct tgl_secret_chat *U); -void bl_do_encr_chat_requested (struct tgl_secret_chat *U, long long access_hash, int date, int admin_id, int user_id, unsigned char g_key[], unsigned char nonce[]); -void bl_do_encr_chat_create (int id, int user_id, int admin_id, char *name, int name_len); -void bl_do_encr_chat_set_access_hash (struct tgl_secret_chat *U, long long access_hash); -void bl_do_encr_chat_set_date (struct tgl_secret_chat *U, int date); -void bl_do_encr_chat_set_state (struct tgl_secret_chat *U, enum tgl_secret_chat_state state); -void bl_do_encr_chat_set_ttl (struct tgl_secret_chat *U, int ttl); -void bl_do_encr_chat_set_layer (struct tgl_secret_chat *U, int layer); -void bl_do_encr_chat_accepted (struct tgl_secret_chat *U, const unsigned char g_key[], const unsigned char nonce[], long long key_fingerprint); -void bl_do_encr_chat_set_key (struct tgl_secret_chat *E, unsigned char key[], long long key_fingerprint); -void bl_do_encr_chat_init (int id, int user_id, unsigned char random[], unsigned char g_a[]); -void bl_do_encr_chat_update_seq (struct tgl_secret_chat *E, int in_seq_no, int out_seq_no); -void bl_do_encr_chat_set_seq (struct tgl_secret_chat *E, int in_seq_no, int last_in_seq_no, int out_seq_no); +void bl_do_encr_chat_delete (struct tgl_state *TLS, struct tgl_secret_chat *U); +void bl_do_encr_chat_requested (struct tgl_state *TLS, struct tgl_secret_chat *U, long long access_hash, int date, int admin_id, int user_id, unsigned char g_key[], unsigned char nonce[]); +void bl_do_encr_chat_create (struct tgl_state *TLS, int id, int user_id, int admin_id, char *name, int name_len); +void bl_do_encr_chat_set_access_hash (struct tgl_state *TLS, struct tgl_secret_chat *U, long long access_hash); +void bl_do_encr_chat_set_date (struct tgl_state *TLS, struct tgl_secret_chat *U, int date); +void bl_do_encr_chat_set_state (struct tgl_state *TLS, struct tgl_secret_chat *U, enum tgl_secret_chat_state state); +void bl_do_encr_chat_set_ttl (struct tgl_state *TLS, struct tgl_secret_chat *U, int ttl); +void bl_do_encr_chat_set_layer (struct tgl_state *TLS, struct tgl_secret_chat *U, int layer); +void bl_do_encr_chat_accepted (struct tgl_state *TLS, struct tgl_secret_chat *U, const unsigned char g_key[], const unsigned char nonce[], long long key_fingerprint); +void bl_do_encr_chat_set_key (struct tgl_state *TLS, struct tgl_secret_chat *E, unsigned char key[], long long key_fingerprint); +void bl_do_encr_chat_init (struct tgl_state *TLS, int id, int user_id, unsigned char random[], unsigned char g_a[]); +void bl_do_encr_chat_update_seq (struct tgl_state *TLS, struct tgl_secret_chat *E, int in_seq_no, int out_seq_no); +void bl_do_encr_chat_set_seq (struct tgl_state *TLS, struct tgl_secret_chat *E, int in_seq_no, int last_in_seq_no, int out_seq_no); -void bl_do_dc_signed (int id); -void bl_do_set_working_dc (int num); -void bl_do_set_dh_params (int root, unsigned char prime[], int version); +void bl_do_dc_signed (struct tgl_state *TLS, int id); +void bl_do_set_working_dc (struct tgl_state *TLS, int num); +void bl_do_set_dh_params (struct tgl_state *TLS, int root, unsigned char prime[], int version); -void bl_do_set_pts (int pts); -void bl_do_set_qts (int qts); -void bl_do_set_seq (int seq); -void bl_do_set_date (int date); +void bl_do_set_pts (struct tgl_state *TLS, int pts); +void bl_do_set_qts (struct tgl_state *TLS, int qts); +void bl_do_set_seq (struct tgl_state *TLS, int seq); +void bl_do_set_date (struct tgl_state *TLS, int date); -void bl_do_create_chat (struct tgl_chat *C, int y, const char *s, int l, int users_num, int date, int version, struct tgl_file_location *big, struct tgl_file_location *small); -void bl_do_chat_forbid (struct tgl_chat *C, int on); -void bl_do_chat_set_title (struct tgl_chat *C, const char *s, int l); -void bl_do_chat_set_photo (struct tgl_chat *C, struct tgl_file_location *big, struct tgl_file_location *small); -void bl_do_chat_set_date (struct tgl_chat *C, int date); -void bl_do_chat_set_set_in_chat (struct tgl_chat *C, int on); -void bl_do_chat_set_version (struct tgl_chat *C, int version, int user_num); -void bl_do_chat_set_admin (struct tgl_chat *C, int admin); -void bl_do_chat_set_participants (struct tgl_chat *C, int version, int user_num, struct tgl_chat_user *users); -void bl_do_chat_set_full_photo (struct tgl_chat *U, const int *start, int len); -void bl_do_chat_add_user (struct tgl_chat *C, int version, int user, int inviter, int date); -void bl_do_chat_del_user (struct tgl_chat *C, int version, int user); +void bl_do_create_chat (struct tgl_state *TLS, struct tgl_chat *C, int y, const char *s, int l, int users_num, int date, int version, struct tgl_file_location *big, struct tgl_file_location *small); +void bl_do_chat_forbid (struct tgl_state *TLS, struct tgl_chat *C, int on); +void bl_do_chat_set_title (struct tgl_state *TLS, struct tgl_chat *C, const char *s, int l); +void bl_do_chat_set_photo (struct tgl_state *TLS, struct tgl_chat *C, struct tgl_file_location *big, struct tgl_file_location *small); +void bl_do_chat_set_date (struct tgl_state *TLS, struct tgl_chat *C, int date); +void bl_do_chat_set_set_in_chat (struct tgl_state *TLS, struct tgl_chat *C, int on); +void bl_do_chat_set_version (struct tgl_state *TLS, struct tgl_chat *C, int version, int user_num); +void bl_do_chat_set_admin (struct tgl_state *TLS, struct tgl_chat *C, int admin); +void bl_do_chat_set_participants (struct tgl_state *TLS, struct tgl_chat *C, int version, int user_num, struct tgl_chat_user *users); +void bl_do_chat_set_full_photo (struct tgl_state *TLS, struct tgl_chat *U, const int *start, int len); +void bl_do_chat_add_user (struct tgl_state *TLS, struct tgl_chat *C, int version, int user, int inviter, int date); +void bl_do_chat_del_user (struct tgl_state *TLS, struct tgl_chat *C, int version, int user); -void bl_do_create_message_text (int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s); -void bl_do_create_message_text_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s); -void bl_do_create_message_service (int msg_id, int from_id, int to_type, int to_id, int date, int unread, const int *data, int len); -void bl_do_create_message_service_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, const int *data, int len); -void bl_do_create_message_media (int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s, const int *data, int len); -void bl_do_create_message_media_encr_pending (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len); -void bl_do_create_message_media_encr_sent (long long msg_id, const int *data, int len); -void bl_do_create_message_media_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s, const int *data, int len); -void bl_do_create_message_media_encr (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len, const int *data2, int len2); -void bl_do_create_message_service_encr (long long msg_id, int from_id, int to_type, int to_id, int date, const int *data, int len); -void bl_do_send_message_text (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s); -void bl_do_send_message_action_encr (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const int *s); -void bl_do_set_unread (struct tgl_message *M, int unread); -void bl_do_set_message_sent (struct tgl_message *M); -void bl_do_set_msg_id (struct tgl_message *M, int id); -void bl_do_delete_msg (struct tgl_message *M); +void bl_do_create_message_text (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s); +void bl_do_create_message_text_fwd (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s); +void bl_do_create_message_service (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int unread, const int *data, int len); +void bl_do_create_message_service_fwd (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, const int *data, int len); +void bl_do_create_message_media (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s, const int *data, int len); +void bl_do_create_message_media_encr_pending (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len); +void bl_do_create_message_media_encr_sent (struct tgl_state *TLS, long long msg_id, const int *data, int len); +void bl_do_create_message_media_fwd (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s, const int *data, int len); +void bl_do_create_message_media_encr (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len, const int *data2, int len2); +void bl_do_create_message_service_encr (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, const int *data, int len); +void bl_do_send_message_text (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s); +void bl_do_send_message_action_encr (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, int l, const int *s); +void bl_do_set_unread (struct tgl_state *TLS, struct tgl_message *M, int unread); +void bl_do_set_message_sent (struct tgl_state *TLS, struct tgl_message *M); +void bl_do_set_msg_id (struct tgl_state *TLS, struct tgl_message *M, int id); +void bl_do_delete_msg (struct tgl_state *TLS, struct tgl_message *M); -void bl_do_msg_seq_update (long long id); -void bl_do_msg_update (long long id); +void bl_do_msg_seq_update (struct tgl_state *TLS, long long id); +void bl_do_msg_update (struct tgl_state *TLS, long long id); -void bl_do_reset_authorization (void); +void bl_do_reset_authorization (struct tgl_state *TLS); //void bl_do_add_dc (int id, const char *ip, int l, int port, long long auth_key_id, const char *auth_key); #endif diff --git a/interface.c b/interface.c index 835cea7..23da5da 100644 --- a/interface.c +++ b/interface.c @@ -105,6 +105,8 @@ extern int usfd; extern int sfd; extern int use_ids; +extern struct tgl_state *TLS; + int is_same_word (const char *s, size_t l, const char *word) { return s && word && strlen (word) == l && !memcmp (s, word, l); } @@ -335,7 +337,7 @@ tgl_peer_id_t cur_token_user (void) { else { return TGL_PEER_NOT_FOUND; } } - tgl_peer_t *P = tgl_peer_get_by_name (s); + tgl_peer_t *P = tgl_peer_get_by_name (TLS, s); cur_token[cur_token_len] = c; if (P && tgl_get_peer_type (P->id) == TGL_PEER_USER) { @@ -370,7 +372,7 @@ tgl_peer_id_t cur_token_chat (void) { else { return TGL_PEER_NOT_FOUND; } } - tgl_peer_t *P = tgl_peer_get_by_name (s); + tgl_peer_t *P = tgl_peer_get_by_name (TLS, s); cur_token[cur_token_len] = c; if (P && tgl_get_peer_type (P->id) == TGL_PEER_CHAT) { @@ -386,7 +388,7 @@ tgl_peer_id_t cur_token_encr_chat (void) { char c = cur_token[cur_token_len]; cur_token[cur_token_len] = 0; - tgl_peer_t *P = tgl_peer_get_by_name (s); + tgl_peer_t *P = tgl_peer_get_by_name (TLS, s); cur_token[cur_token_len] = c; if (P && tgl_get_peer_type (P->id) == TGL_PEER_ENCR_CHAT) { @@ -436,7 +438,7 @@ tgl_peer_id_t cur_token_peer (void) { else { return TGL_PEER_NOT_FOUND; } } - tgl_peer_t *P = tgl_peer_get_by_name (s); + tgl_peer_t *P = tgl_peer_get_by_name (TLS, s); cur_token[cur_token_len] = c; if (P) { @@ -448,15 +450,15 @@ tgl_peer_id_t cur_token_peer (void) { static tgl_peer_t *mk_peer (tgl_peer_id_t id) { if (tgl_get_peer_type (id) == NOT_FOUND) { return 0; } - tgl_peer_t *P = tgl_peer_get (id); + tgl_peer_t *P = tgl_peer_get (TLS, id); if (!P) { if (tgl_get_peer_type (id) == TGL_PEER_USER) { - tgl_insert_empty_user (tgl_get_peer_id (id)); + tgl_insert_empty_user (TLS, tgl_get_peer_id (id)); } if (tgl_get_peer_type (id) == TGL_PEER_CHAT) { - tgl_insert_empty_chat (tgl_get_peer_id (id)); + tgl_insert_empty_chat (TLS, tgl_get_peer_id (id)); } - P = tgl_peer_get (id); + P = tgl_peer_get (TLS, id); } return P; } @@ -465,26 +467,26 @@ char *get_default_prompt (void) { static char buf[1000]; int l = 0; if (in_chat_mode) { - tgl_peer_t *U = tgl_peer_get (chat_mode_id); + tgl_peer_t *U = tgl_peer_get (TLS, chat_mode_id); assert (U && U->print_name); l += snprintf (buf + l, 999 - l, COLOR_RED "%.*s " COLOR_NORMAL, 100, U->print_name); } - if (tgl_state.unread_messages || tgl_state.cur_uploading_bytes || tgl_state.cur_downloading_bytes) { + if (TLS->unread_messages || TLS->cur_uploading_bytes || TLS->cur_downloading_bytes) { l += snprintf (buf + l, 999 - l, COLOR_RED "["); int ok = 0; - if (tgl_state.unread_messages) { - l += snprintf (buf + l, 999 - l, "%d unread", tgl_state.unread_messages); + if (TLS->unread_messages) { + l += snprintf (buf + l, 999 - l, "%d unread", TLS->unread_messages); ok = 1; } - if (tgl_state.cur_uploading_bytes) { + if (TLS->cur_uploading_bytes) { if (ok) { *(buf + l) = ' '; l ++; } ok = 1; - l += snprintf (buf + l, 999 - l, "%lld%%Up", 100 * tgl_state.cur_uploaded_bytes / tgl_state.cur_uploading_bytes); + l += snprintf (buf + l, 999 - l, "%lld%%Up", 100 * TLS->cur_uploaded_bytes / TLS->cur_uploading_bytes); } - if (tgl_state.cur_downloading_bytes) { + if (TLS->cur_downloading_bytes) { if (ok) { *(buf + l) = ' '; l ++; } ok = 1; - l += snprintf (buf + l, 999 - l, "%lld%%Down", 100 * tgl_state.cur_downloaded_bytes / tgl_state.cur_downloading_bytes); + l += snprintf (buf + l, 999 - l, "%lld%%Down", 100 * TLS->cur_downloaded_bytes / TLS->cur_downloading_bytes); } l += snprintf (buf + l, 999 - l, "]" COLOR_NORMAL); l += snprintf (buf + l, 999 - l, "%s", default_prompt); @@ -568,17 +570,20 @@ struct command { int offline_mode; -void print_user_list_gw (void *extra, int success, int num, struct tgl_user *UL[]); -void print_msg_list_gw (void *extra, int success, int num, struct tgl_message *ML[]); -void print_dialog_list_gw (void *extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]); -void print_chat_info_gw (void *extra, int success, struct tgl_chat *C); -void print_user_info_gw (void *extra, int success, struct tgl_user *C); -void print_filename_gw (void *extra, int success, char *name); -void open_filename_gw (void *extra, int success, char *name); -void print_secret_chat_gw (void *extra, int success, struct tgl_secret_chat *E); -void print_card_gw (void *extra, int success, int size, int *card); -void print_user_gw (void *extra, int success, struct tgl_user *U); -void print_msg_gw (void *extra, int success, struct tgl_message *M); +void print_user_list_gw (struct tgl_state *TLS, void *extra, int success, int num, struct tgl_user *UL[]); +void print_msg_list_gw (struct tgl_state *TLS, void *extra, int success, int num, struct tgl_message *ML[]); +void print_dialog_list_gw (struct tgl_state *TLS, void *extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]); +void print_chat_info_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_chat *C); +void print_user_info_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_user *C); +void print_filename_gw (struct tgl_state *TLS, void *extra, int success, char *name); +void open_filename_gw (struct tgl_state *TLS, void *extra, int success, char *name); +void print_secret_chat_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_secret_chat *E); +void print_card_gw (struct tgl_state *TLS, void *extra, int success, int size, int *card); +void print_user_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_user *U); +void print_msg_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_message *M); +void print_msg_success_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_message *M); +void print_encr_chat_success_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_secret_chat *E);; +void print_success_gw (struct tgl_state *TLS, void *extra, int success); struct command commands[]; void do_help (int arg_num, struct arg args[], struct in_ev *ev) { @@ -600,13 +605,13 @@ void do_help (int arg_num, struct arg args[], struct in_ev *ev) { void do_contact_list (int arg_num, struct arg args[], struct in_ev *ev) { assert (!arg_num); if (ev) { ev->refcnt ++; } - tgl_do_update_contact_list (print_user_list_gw, ev); + tgl_do_update_contact_list (TLS, print_user_list_gw, ev); } void do_stats (int arg_num, struct arg args[], struct in_ev *ev) { assert (!arg_num); static char stat_buf[1 << 15]; - tgl_print_stat (stat_buf, (1 << 15) - 1); + tgl_print_stat (TLS, stat_buf, (1 << 15) - 1); if (ev) { mprint_start (ev); } mprintf (ev, "%s\n", stat_buf); if (ev) { mprint_end (ev); } @@ -618,92 +623,101 @@ void do_stats (int arg_num, struct arg args[], struct in_ev *ev) { void do_history (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 3); if (ev) { ev->refcnt ++; } - tgl_do_get_history_ext (args[0].P->id, args[2].num != NOT_FOUND ? args[2].num : 0, args[1].num != NOT_FOUND ? args[1].num : 40, offline_mode, print_msg_list_gw, ev); + tgl_do_get_history_ext (TLS, args[0].P->id, args[2].num != NOT_FOUND ? args[2].num : 0, args[1].num != NOT_FOUND ? args[1].num : 40, offline_mode, print_msg_list_gw, ev); } void do_dialog_list (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 0); if (ev) { ev->refcnt ++; } - tgl_do_get_dialog_list (print_dialog_list_gw, ev); + tgl_do_get_dialog_list (TLS, print_dialog_list_gw, ev); } void do_send_photo (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); - tgl_do_send_photo (tgl_message_media_photo, args[0].P->id, args[1].str, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_send_photo (TLS, tgl_message_media_photo, args[0].P->id, args[1].str, print_msg_success_gw, ev); } void do_send_audio (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); - tgl_do_send_photo (tgl_message_media_audio, args[0].P->id, args[1].str, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_send_photo (TLS, tgl_message_media_audio, args[0].P->id, args[1].str, print_msg_success_gw, ev); } void do_send_video (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); - tgl_do_send_photo (tgl_message_media_video, args[0].P->id, args[1].str, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_send_photo (TLS, tgl_message_media_video, args[0].P->id, args[1].str, print_msg_success_gw, ev); } void do_send_document (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); - tgl_do_send_photo (tgl_message_media_document, args[0].P->id, args[1].str, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_send_photo (TLS, tgl_message_media_document, args[0].P->id, args[1].str, print_msg_success_gw, ev); } void do_send_text (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); - tgl_do_send_text (args[0].P->id, args[1].str, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_send_text (TLS, args[0].P->id, args[1].str, print_msg_success_gw, ev); } void do_chat_info (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 1); if (ev) { ev->refcnt ++; } - tgl_do_get_chat_info (args[0].P->id, offline_mode, print_chat_info_gw, ev); + tgl_do_get_chat_info (TLS, args[0].P->id, offline_mode, print_chat_info_gw, ev); } void do_user_info (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 1); if (ev) { ev->refcnt ++; } - tgl_do_get_user_info (args[0].P->id, offline_mode, print_user_info_gw, ev); + tgl_do_get_user_info (TLS, args[0].P->id, offline_mode, print_user_info_gw, ev); } void do_fwd (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); - tgl_do_forward_message (args[0].P->id, args[1].num, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_forward_message (TLS, args[0].P->id, args[1].num, print_msg_success_gw, ev); } void do_fwd_media (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); - tgl_do_forward_message (args[0].P->id, args[1].num, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_forward_message (TLS, args[0].P->id, args[1].num, print_msg_success_gw, ev); } void do_msg (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); - tgl_do_send_message (args[0].P->id, args[1].str, strlen (args[1].str), 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_send_message (TLS, args[0].P->id, args[1].str, strlen (args[1].str), print_msg_success_gw, ev); } void do_rename_chat (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); - tgl_do_rename_chat (args[0].P->id, args[1].str, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_rename_chat (TLS, args[0].P->id, args[1].str, print_msg_success_gw, ev); } #define DO_LOAD_PHOTO(tp,act,actf) \ void do_ ## act ## _ ## tp (int arg_num, struct arg args[], struct in_ev *ev) { \ assert (arg_num == 1);\ - struct tgl_message *M = tgl_message_get (args[0].num);\ + struct tgl_message *M = tgl_message_get (TLS, args[0].num);\ if (M && !M->service && M->media.type == tgl_message_media_ ## tp) {\ if (ev) { ev->refcnt ++; } \ - tgl_do_load_ ## tp (&M->media.tp, actf, ev); \ + tgl_do_load_ ## tp (TLS, &M->media.tp, actf, ev); \ } else if (M && !M->service && M->media.type == tgl_message_media_ ## tp ## _encr) { \ if (ev) { ev->refcnt ++; } \ - tgl_do_load_encr_video (&M->media.encr_video, actf, ev); \ + tgl_do_load_encr_video (TLS, &M->media.encr_video, actf, ev); \ } \ } #define DO_LOAD_PHOTO_THUMB(tp,act,actf) \ void do_ ## act ## _ ## tp ## _thumb (int arg_num, struct arg args[], struct in_ev *ev) { \ assert (arg_num == 1);\ - struct tgl_message *M = tgl_message_get (args[0].num);\ + struct tgl_message *M = tgl_message_get (TLS, args[0].num);\ if (M && !M->service && M->media.type == tgl_message_media_ ## tp) { \ if (ev) { ev->refcnt ++; } \ - tgl_do_load_ ## tp ## _thumb (&M->media.tp, actf, ev); \ + tgl_do_load_ ## tp ## _thumb (TLS, &M->media.tp, actf, ev); \ }\ } @@ -723,19 +737,23 @@ DO_LOAD_PHOTO_THUMB(document, open, open_filename_gw) void do_add_contact (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 3); if (ev) { ev->refcnt ++; } - tgl_do_add_contact (args[0].str, strlen (args[0].str), args[1].str, strlen (args[1].str), args[2].str, strlen (args[2].str), 0, print_user_list_gw, ev); + tgl_do_add_contact (TLS, args[0].str, strlen (args[0].str), args[1].str, strlen (args[1].str), args[2].str, strlen (args[2].str), 0, print_user_list_gw, ev); } void do_del_contact (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 1); - tgl_do_del_contact (args[0].P->id, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_del_contact (TLS, args[0].P->id, print_success_gw, ev); } void do_rename_contact (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 3); if (args[0].P->user.phone) { if (ev) { ev->refcnt ++; } - tgl_do_add_contact (args[0].P->user.phone, strlen (args[0].P->user.phone), args[1].str, strlen (args[1].str), args[2].str, strlen (args[2].str), 0, print_user_list_gw, ev); + tgl_do_add_contact (TLS, args[0].P->user.phone, strlen (args[0].P->user.phone), args[1].str, strlen (args[1].str), args[2].str, strlen (args[2].str), 0, print_user_list_gw, ev); + } else { + if (ev) { ev->refcnt ++; } + print_success_gw (TLS, ev, 0); } } @@ -782,12 +800,13 @@ void do_search (int arg_num, struct arg args[], struct in_ev *ev) { offset = 0; } if (ev) { ev->refcnt ++; } - tgl_do_msg_search (id, from, to, limit, offset, args[5].str, print_msg_list_gw, ev); + tgl_do_msg_search (TLS, id, from, to, limit, offset, args[5].str, print_msg_list_gw, ev); } void do_mark_read (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 1); - tgl_do_mark_read (args[0].P->id, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_mark_read (TLS, args[0].P->id, print_success_gw, ev); } void do_visualize_key (int arg_num, struct arg args[], struct in_ev *ev) { @@ -796,7 +815,7 @@ void do_visualize_key (int arg_num, struct arg args[], struct in_ev *ev) { static unsigned char buf[16]; memset (buf, 0, sizeof (buf)); tgl_peer_id_t id = args[0].P->id; - tgl_do_visualize_key (id, buf); + tgl_do_visualize_key (TLS, id, buf); mprint_start (ev); int i; for (i = 0; i < 16; i++) { @@ -841,27 +860,31 @@ void do_visualize_key (int arg_num, struct arg args[], struct in_ev *ev) { void do_create_secret_chat (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 1); if (ev) { ev->refcnt ++; } - tgl_do_create_secret_chat (args[0].P->id, print_secret_chat_gw, ev); + tgl_do_create_secret_chat (TLS, args[0].P->id, print_secret_chat_gw, ev); } void do_chat_add_user (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 3); - tgl_do_add_user_to_chat (args[0].P->id, args[1].P->id, args[2].num != NOT_FOUND ? args[2].num : 100, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_add_user_to_chat (TLS, args[0].P->id, args[1].P->id, args[2].num != NOT_FOUND ? args[2].num : 100, print_msg_success_gw, ev); } void do_chat_del_user (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); - tgl_do_del_user_from_chat (args[0].P->id, args[1].P->id, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_del_user_from_chat (TLS, args[0].P->id, args[1].P->id, print_msg_success_gw, ev); } void do_status_online (int arg_num, struct arg args[], struct in_ev *ev) { assert (!arg_num); - tgl_do_update_status (1, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_update_status (TLS, 1, print_success_gw, ev); } void do_status_offline (int arg_num, struct arg args[], struct in_ev *ev) { assert (!arg_num); - tgl_do_update_status (0, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_update_status (TLS, 0, print_success_gw, ev); } void do_quit (int arg_num, struct arg args[], struct in_ev *ev) { @@ -875,7 +898,7 @@ void do_safe_quit (int arg_num, struct arg args[], struct in_ev *ev) { void do_set (int arg_num, struct arg args[], struct in_ev *ev) { int num = args[1].num; if (!strcmp (args[0].str, "debug_verbosity")) { - tgl_set_verbosity (num); + tgl_set_verbosity (TLS, num); } else if (!strcmp (args[0].str, "log_level")) { log_level = num; } else if (!strcmp (args[0].str, "msg_num")) { @@ -893,11 +916,13 @@ void do_chat_with_peer (int arg_num, struct arg args[], struct in_ev *ev) { } void do_delete_msg (int arg_num, struct arg args[], struct in_ev *ev) { - tgl_do_delete_msg (args[0].num, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_delete_msg (TLS, args[0].num, print_success_gw, ev); } void do_restore_msg (int arg_num, struct arg args[], struct in_ev *ev) { - tgl_do_restore_msg (args[0].num, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_restore_msg (TLS, args[0].num, print_success_gw, ev); } void do_create_group_chat (int arg_num, struct arg args[], struct in_ev *ev) { @@ -908,50 +933,61 @@ void do_create_group_chat (int arg_num, struct arg args[], struct in_ev *ev) { ids[i] = args[i + 1].P->id; } - tgl_do_create_group_chat_ex (arg_num - 1, ids, args[0].str, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_create_group_chat_ex (TLS, arg_num - 1, ids, args[0].str, print_msg_success_gw, ev); } void do_chat_set_photo (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); - tgl_do_set_chat_photo (args[0].P->id, args[1].str, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_set_chat_photo (TLS, args[0].P->id, args[1].str, print_msg_success_gw, ev); } void do_set_profile_photo (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 1); - tgl_do_set_profile_photo (args[0].str, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_set_profile_photo (TLS, args[0].str, print_success_gw, ev); } void do_set_profile_name (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); - tgl_do_set_profile_name (args[0].str, args[1].str, print_user_gw, ev); + if (ev) { ev->refcnt ++; } + tgl_do_set_profile_name (TLS, args[0].str, args[1].str, print_user_gw, ev); } void do_set_username (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 1); - tgl_do_set_username (args[0].str, print_user_gw, ev); + if (ev) { ev->refcnt ++; } + tgl_do_set_username (TLS, args[0].str, print_user_gw, ev); } void do_contact_search (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); - tgl_do_contact_search (args[0].str, args[1].num == NOT_FOUND ? args[1].num : 10, print_user_list_gw, ev); + if (ev) { ev->refcnt ++; } + tgl_do_contact_search (TLS, args[0].str, args[1].num == NOT_FOUND ? args[1].num : 10, print_user_list_gw, ev); } void do_accept_secret_chat (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 1); - tgl_do_accept_encr_chat_request (&args[0].P->encr_chat, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_accept_encr_chat_request (TLS, &args[0].P->encr_chat, print_encr_chat_success_gw, ev); } void do_set_ttl (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 2); if (args[0].P->encr_chat.state == sc_ok) { - tgl_do_set_encr_chat_ttl (&args[0].P->encr_chat, args[1].num, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_set_encr_chat_ttl (TLS, &args[0].P->encr_chat, args[1].num, print_msg_success_gw, ev); + } else { + if (ev) { ev->refcnt ++; } + print_success_gw (TLS, ev, 0); } } void do_export_card (int arg_num, struct arg args[], struct in_ev *ev) { assert (!arg_num); if (ev) { ev->refcnt ++; } - tgl_do_export_card (print_card_gw, ev); + tgl_do_export_card (TLS, print_card_gw, ev); } void do_import_card (int arg_num, struct arg args[], struct in_ev *ev) { @@ -981,7 +1017,7 @@ void do_import_card (int arg_num, struct arg args[], struct in_ev *ev) { if (ok) { p[pp ++] = cur; if (ev) { ev->refcnt ++; } - tgl_do_import_card (pp, p, print_user_gw, ev); + tgl_do_import_card (TLS, pp, p, print_user_gw, ev); } } } @@ -989,7 +1025,7 @@ void do_import_card (int arg_num, struct arg args[], struct in_ev *ev) { void do_send_contact (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 4); if (ev) { ev->refcnt ++; } - tgl_do_send_contact (args[0].P->id, args[1].str, strlen (args[1].str), args[2].str, strlen (args[2].str), args[3].str, strlen (args[3].str), print_msg_gw, ev); + tgl_do_send_contact (TLS, args[0].P->id, args[1].str, strlen (args[1].str), args[2].str, strlen (args[2].str), args[3].str, strlen (args[3].str), print_msg_gw, ev); } void do_main_session (int arg_num, struct arg args[], struct in_ev *ev) { @@ -1014,7 +1050,7 @@ extern struct event *term_ev; void do_clear (int arg_num, struct arg args[], struct in_ev *ev) { logprintf ("Do_clear\n"); - tgl_free_all (); + tgl_free_all (TLS); free (default_username); free (config_filename); free (prefix); @@ -1027,13 +1063,14 @@ void do_clear (int arg_num, struct arg args[], struct in_ev *ev) { free (lua_file); clear_history (); event_free (term_ev); - event_base_free (tgl_state.ev_base); + event_base_free (TLS->ev_base); do_halt (0); } void do_send_location (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 3); - tgl_do_send_location (args[0].P->id, args[1].dval, args[2].dval, 0, 0); + if (ev) { ev->refcnt ++; } + tgl_do_send_location (TLS, args[0].P->id, args[1].dval, args[2].dval, print_msg_success_gw, ev); } @@ -1242,6 +1279,9 @@ int complete_command_list (int index, const char *text, int len, char **R) { index ++; } if (commands[index].name) { +void print_msg_success_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_message *M); +void print_encr_chat_success_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_secret_chat *E);; +void print_success_gw (struct tgl_state *TLS, void *extra, int success); *R = strdup (commands[index].name); assert (*R); return index; @@ -1295,11 +1335,11 @@ char *command_generator (const char *text, int state) { if (c) { rl_line_buffer[rl_point] = c; } return R; case ca_user: - index = tgl_complete_user_list (index, command_pos, command_len, &R); + index = tgl_complete_user_list (TLS, index, command_pos, command_len, &R); if (c) { rl_line_buffer[rl_point] = c; } return R; case ca_peer: - index = tgl_complete_peer_list (index, command_pos, command_len, &R); + index = tgl_complete_peer_list (TLS, index, command_pos, command_len, &R); if (c) { rl_line_buffer[rl_point] = c; } return R; case ca_file_name: @@ -1308,11 +1348,11 @@ char *command_generator (const char *text, int state) { R = rl_filename_completion_function (command_pos, state); return R; case ca_chat: - index = tgl_complete_chat_list (index, command_pos, command_len, &R); + index = tgl_complete_chat_list (TLS, index, command_pos, command_len, &R); if (c) { rl_line_buffer[rl_point] = c; } return R; case ca_secret_chat: - index = tgl_complete_encr_chat_list (index, command_pos, command_len, &R); + index = tgl_complete_encr_chat_list (TLS, index, command_pos, command_len, &R); if (c) { rl_line_buffer[rl_point] = c; } return R; case ca_modifier: @@ -1321,7 +1361,7 @@ char *command_generator (const char *text, int state) { return R; #ifndef DISABLE_EXTF case ca_extf: - index = tglf_extf_autocomplete (text, len, index, &R, rl_line_buffer, rl_point); + index = tglf_extf_autocomplete (TLS, text, len, index, &R, rl_line_buffer, rl_point); if (c) { rl_line_buffer[rl_point] = c; } return R; #endif @@ -1346,13 +1386,48 @@ void work_modifier (const char *s, int l) { #endif } -void print_msg_list_gw (void *extra, int success, int num, struct tgl_message *ML[]) { +void print_fail (struct in_ev *ev) { + if (ev) { + mprint_start (ev); + mprintf (ev, "FAIL\n"); + mprint_end (ev); + } +} + +void print_success (struct in_ev *ev) { + if (ev) { + mprint_start (ev); + mprintf (ev, "SUCCESS\n"); + mprint_end (ev); + } +} +void print_success_gw (struct tgl_state *TLSR, void *extra, int success) { + assert (TLS == TLSR); struct in_ev *ev = extra; if (ev && !--ev->refcnt) { free (ev); return; } - if (!success) { return; } + if (!success) { print_fail (ev); return; } + else { print_success (ev); return; } +} + +void print_msg_success_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_message *M) { + print_success_gw (TLS, extra, success); +} + +void print_encr_chat_success_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_secret_chat *E) { + print_success_gw (TLS, extra, success); +} + +void print_msg_list_gw (struct tgl_state *TLSR, void *extra, int success, int num, struct tgl_message *ML[]) { + assert (TLS == TLSR); + struct in_ev *ev = extra; + if (ev && !--ev->refcnt) { + free (ev); + return; + } + if (!success) { print_fail (ev); return; } mprint_start (ev); int i; for (i = num - 1; i >= 0; i--) { @@ -1361,25 +1436,27 @@ void print_msg_list_gw (void *extra, int success, int num, struct tgl_message *M mprint_end (ev); } -void print_msg_gw (void *extra, int success, struct tgl_message *M) { +void print_msg_gw (struct tgl_state *TLSR, void *extra, int success, struct tgl_message *M) { + assert (TLS == TLSR); struct in_ev *ev = extra; if (ev && !--ev->refcnt) { free (ev); return; } - if (!success) { return; } + if (!success) { print_fail (ev); return; } mprint_start (ev); print_message (ev, M); mprint_end (ev); } -void print_user_list_gw (void *extra, int success, int num, struct tgl_user *UL[]) { +void print_user_list_gw (struct tgl_state *TLSR, void *extra, int success, int num, struct tgl_user *UL[]) { + assert (TLS == TLSR); struct in_ev *ev = extra; if (ev && !--ev->refcnt) { free (ev); return; } - if (!success) { return; } + if (!success) { print_fail (ev); return; } mprint_start (ev); int i; for (i = num - 1; i >= 0; i--) { @@ -1389,39 +1466,42 @@ void print_user_list_gw (void *extra, int success, int num, struct tgl_user *UL[ mprint_end (ev); } -void print_user_gw (void *extra, int success, struct tgl_user *U) { +void print_user_gw (struct tgl_state *TLSR, void *extra, int success, struct tgl_user *U) { + assert (TLS == TLSR); struct in_ev *ev = extra; if (ev && !--ev->refcnt) { free (ev); return; } - if (!success) { return; } + if (!success) { print_fail (ev); return; } mprint_start (ev); print_user_name (ev, U->id, (void *)U); mprintf (ev, "\n"); mprint_end (ev); } -void print_filename_gw (void *extra, int success, char *name) { +void print_filename_gw (struct tgl_state *TLSR, void *extra, int success, char *name) { + assert (TLS == TLSR); struct in_ev *ev = extra; if (ev && !--ev->refcnt) { free (ev); return; } - if (!success) { return; } + if (!success) { print_fail (ev); return; } mprint_start (ev); mprintf (ev, "Saved to %s\n", name); mprint_end (ev); } -void open_filename_gw (void *extra, int success, char *name) { +void open_filename_gw (struct tgl_state *TLSR, void *extra, int success, char *name) { + assert (TLS == TLSR); struct in_ev *ev = extra; if (ev && !--ev->refcnt) { free (ev); return; } if (ev) { return; } - if (!success) { return; } + if (!success) { print_fail (ev); return; } static char buf[PATH_MAX]; if (snprintf (buf, sizeof (buf), OPEN_BIN, name) >= (int) sizeof (buf)) { logprintf ("Open image command buffer overflow\n"); @@ -1434,16 +1514,14 @@ void open_filename_gw (void *extra, int success, char *name) { } } -void print_chat_info_gw (void *extra, int success, struct tgl_chat *C) { +void print_chat_info_gw (struct tgl_state *TLSR, void *extra, int success, struct tgl_chat *C) { + assert (TLS == TLSR); struct in_ev *ev = extra; if (ev && !--ev->refcnt) { free (ev); return; } - if (!success) { - vlogprintf (E_NOTICE, "Failed to get chat info\n"); - return; - } + if (!success) { print_fail (ev); return; } mprint_start (ev); tgl_peer_t *U = (void *)C; @@ -1454,9 +1532,9 @@ void print_chat_info_gw (void *extra, int success, struct tgl_chat *C) { int i; for (i = 0; i < C->user_list_size; i++) { mprintf (ev, "\t\t"); - print_user_name (ev, TGL_MK_USER (C->user_list[i].user_id), tgl_peer_get (TGL_MK_USER (C->user_list[i].user_id))); + print_user_name (ev, TGL_MK_USER (C->user_list[i].user_id), tgl_peer_get (TLS, TGL_MK_USER (C->user_list[i].user_id))); mprintf (ev, " invited by "); - print_user_name (ev, TGL_MK_USER (C->user_list[i].inviter_id), tgl_peer_get (TGL_MK_USER (C->user_list[i].inviter_id))); + print_user_name (ev, TGL_MK_USER (C->user_list[i].inviter_id), tgl_peer_get (TLS, TGL_MK_USER (C->user_list[i].inviter_id))); mprintf (ev, " at "); print_date_full (ev, C->user_list[i].date); if (C->user_list[i].user_id == C->admin_id) { @@ -1468,13 +1546,14 @@ void print_chat_info_gw (void *extra, int success, struct tgl_chat *C) { mprint_end (ev); } -void print_user_info_gw (void *extra, int success, struct tgl_user *U) { +void print_user_info_gw (struct tgl_state *TLSR, void *extra, int success, struct tgl_user *U) { + assert (TLS == TLSR); struct in_ev *ev = extra; if (ev && !--ev->refcnt) { free (ev); return; } - if (!success) { return; } + if (!success) { print_fail (ev); return; } tgl_peer_t *C = (void *)U; mprint_start (ev); mpush_color (ev, COLOR_YELLOW); @@ -1497,13 +1576,14 @@ void print_user_info_gw (void *extra, int success, struct tgl_user *U) { mprint_end (ev); } -void print_secret_chat_gw (void *extra, int success, struct tgl_secret_chat *E) { +void print_secret_chat_gw (struct tgl_state *TLSR, void *extra, int success, struct tgl_secret_chat *E) { + assert (TLS == TLSR); struct in_ev *ev = extra; if (ev && !--ev->refcnt) { free (ev); return; } - if (!success) { return; } + if (!success) { print_fail (ev); return; } mprint_start (ev); mpush_color (ev, COLOR_YELLOW); mprintf (ev, " Encrypted chat "); @@ -1513,13 +1593,14 @@ void print_secret_chat_gw (void *extra, int success, struct tgl_secret_chat *E) mprint_end (ev); } -void print_dialog_list_gw (void *extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]) { +void print_dialog_list_gw (struct tgl_state *TLSR, void *extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]) { + assert (TLS == TLSR); struct in_ev *ev = extra; if (ev && !--ev->refcnt) { free (ev); return; } - if (!success) { return; } + if (!success) { print_fail (ev); return; } mprint_start (ev); mpush_color (ev, COLOR_YELLOW); int i; @@ -1527,13 +1608,13 @@ void print_dialog_list_gw (void *extra, int success, int size, tgl_peer_id_t pee tgl_peer_t *UC; switch (tgl_get_peer_type (peers[i])) { case TGL_PEER_USER: - UC = tgl_peer_get (peers[i]); + UC = tgl_peer_get (TLS, peers[i]); mprintf (ev, "User "); print_user_name (ev, peers[i], UC); mprintf (ev, ": %d unread\n", unread_count[i]); break; case TGL_PEER_CHAT: - UC = tgl_peer_get (peers[i]); + UC = tgl_peer_get (TLS, peers[i]); mprintf (ev, "Chat "); print_chat_name (ev, peers[i], UC); mprintf (ev, ": %d unread\n", unread_count[i]); @@ -1555,15 +1636,15 @@ void interpreter_chat_mode (char *line) { int limit = 40; sscanf (line, "/history %99d", &limit); if (limit < 0 || limit > 1000) { limit = 40; } - tgl_do_get_history (chat_mode_id, limit, offline_mode, print_msg_list_gw, 0); + tgl_do_get_history (TLS, chat_mode_id, limit, offline_mode, print_msg_list_gw, 0); return; } if (!strncmp (line, "/read", 5)) { - tgl_do_mark_read (chat_mode_id, 0, 0); + tgl_do_mark_read (TLS, chat_mode_id, 0, 0); return; } if (strlen (line) > 0) { - tgl_do_send_message (chat_mode_id, line, strlen (line), 0, 0); + tgl_do_send_message (TLS, chat_mode_id, line, strlen (line), 0, 0); } } @@ -1579,7 +1660,7 @@ void print_read_list (int num, struct tgl_message *list[]) { mprint_start (ev); for (i = 0; i < num; i++) if (list[i]) { tgl_peer_id_t to_id; - if (tgl_get_peer_type (list[i]->to_id) == TGL_PEER_USER && tgl_get_peer_id (list[i]->to_id) == tgl_state.our_id) { + if (tgl_get_peer_type (list[i]->to_id) == TGL_PEER_USER && tgl_get_peer_id (list[i]->to_id) == TLS->our_id) { to_id = list[i]->from_id; } else { to_id = list[i]->to_id; @@ -1589,7 +1670,7 @@ void print_read_list (int num, struct tgl_message *list[]) { int c2 = 0; for (j = i; j < num; j++) if (list[j]) { tgl_peer_id_t end_id; - if (tgl_get_peer_type (list[j]->to_id) == TGL_PEER_USER && tgl_get_peer_id (list[j]->to_id) == tgl_state.our_id) { + if (tgl_get_peer_type (list[j]->to_id) == TGL_PEER_USER && tgl_get_peer_id (list[j]->to_id) == TLS->our_id) { end_id = list[j]->from_id; } else { end_id = list[j]->to_id; @@ -1609,15 +1690,15 @@ void print_read_list (int num, struct tgl_message *list[]) { switch (tgl_get_peer_type (to_id)) { case TGL_PEER_USER: mprintf (ev, "User "); - print_user_name (ev, to_id, tgl_peer_get (to_id)); + print_user_name (ev, to_id, tgl_peer_get (TLS, to_id)); break; case TGL_PEER_CHAT: mprintf (ev, "Chat "); - print_chat_name (ev, to_id, tgl_peer_get (to_id)); + print_chat_name (ev, to_id, tgl_peer_get (TLS, to_id)); break; case TGL_PEER_ENCR_CHAT: mprintf (ev, "Secret chat "); - print_encr_chat_name (ev, to_id, tgl_peer_get (to_id)); + print_encr_chat_name (ev, to_id, tgl_peer_get (TLS, to_id)); break; default: assert (0); @@ -1635,7 +1716,8 @@ void unread_message_alarm (evutil_socket_t fd, short what, void *arg) { unread_message_event = 0; } -void mark_read_upd (int num, struct tgl_message *list[]) { +void mark_read_upd (struct tgl_state *TLSR, int num, struct tgl_message *list[]) { + assert (TLSR == TLS); if (!binlog_read) { return; } if (log_level < 1) { return; } @@ -1644,7 +1726,7 @@ void mark_read_upd (int num, struct tgl_message *list[]) { unread_message_count += num; if (!unread_message_event) { - unread_message_event = evtimer_new (tgl_state.ev_base, unread_message_alarm, 0); + unread_message_event = evtimer_new (TLS->ev_base, unread_message_alarm, 0); static struct timeval ptimeout = { 1, 0}; event_add (unread_message_event, &ptimeout); } @@ -1697,7 +1779,8 @@ void print_typing (struct in_ev *ev, enum tgl_typing_status status) { } } -void type_notification_upd (struct tgl_user *U, enum tgl_typing_status status) { +void type_notification_upd (struct tgl_state *TLSR, struct tgl_user *U, enum tgl_typing_status status) { + assert (TLSR == TLS); if (log_level < 2 || (disable_output && !notify_ev)) { return; } struct in_ev *ev = notify_ev; mprint_start (ev); @@ -1711,7 +1794,8 @@ void type_notification_upd (struct tgl_user *U, enum tgl_typing_status status) { mprint_end (ev); } -void type_in_chat_notification_upd (struct tgl_user *U, struct tgl_chat *C, enum tgl_typing_status status) { +void type_in_chat_notification_upd (struct tgl_state *TLSR, struct tgl_user *U, struct tgl_chat *C, enum tgl_typing_status status) { + assert (TLSR == TLS); if (log_level < 2 || (disable_output && !notify_ev)) { return; } struct in_ev *ev = notify_ev; mprint_start (ev); @@ -1728,7 +1812,8 @@ void type_in_chat_notification_upd (struct tgl_user *U, struct tgl_chat *C, enum } -void print_message_gw (struct tgl_message *M) { +void print_message_gw (struct tgl_state *TLSR, struct tgl_message *M) { + assert (TLSR == TLS); #ifdef USE_LUA lua_new_msg (M); #endif @@ -1746,7 +1831,8 @@ void print_message_gw (struct tgl_message *M) { mprint_end (ev); } -void our_id_gw (int id) { +void our_id_gw (struct tgl_state *TLSR, int id) { + assert (TLSR == TLS); #ifdef USE_LUA lua_our_id (id); #endif @@ -1797,7 +1883,8 @@ void print_peer_updates (struct in_ev *ev, int flags) { } } -void user_update_gw (struct tgl_user *U, unsigned flags) { +void user_update_gw (struct tgl_state *TLSR, struct tgl_user *U, unsigned flags) { + assert (TLSR == TLS); #ifdef USE_LUA lua_user_update (U, flags); #endif @@ -1823,7 +1910,8 @@ void user_update_gw (struct tgl_user *U, unsigned flags) { } } -void chat_update_gw (struct tgl_chat *U, unsigned flags) { +void chat_update_gw (struct tgl_state *TLSR, struct tgl_chat *U, unsigned flags) { + assert (TLSR == TLS); #ifdef USE_LUA lua_chat_update (U, flags); #endif @@ -1849,7 +1937,8 @@ void chat_update_gw (struct tgl_chat *U, unsigned flags) { } } -void secret_chat_update_gw (struct tgl_secret_chat *U, unsigned flags) { +void secret_chat_update_gw (struct tgl_state *TLSR, struct tgl_secret_chat *U, unsigned flags) { + assert (TLSR == TLS); #ifdef USE_LUA lua_secret_chat_update (U, flags); #endif @@ -1861,7 +1950,7 @@ void secret_chat_update_gw (struct tgl_secret_chat *U, unsigned flags) { if (!binlog_read) { return; } if ((flags & TGL_UPDATE_REQUESTED) && !disable_auto_accept) { - tgl_do_accept_encr_chat_request (U, 0, 0); + tgl_do_accept_encr_chat_request (TLS, U, 0, 0); } if (disable_output && !notify_ev) { return; } @@ -1885,13 +1974,14 @@ void secret_chat_update_gw (struct tgl_secret_chat *U, unsigned flags) { } } -void print_card_gw (void *extra, int success, int size, int *card) { +void print_card_gw (struct tgl_state *TLSR, void *extra, int success, int size, int *card) { + assert (TLSR == TLS); struct in_ev *ev = extra; if (ev && !--ev->refcnt) { free (ev); return; } - if (!success) { return; } + if (!success) { print_fail (ev); return; } mprint_start (ev); mprintf (ev, "Card: "); int i; @@ -1901,13 +1991,13 @@ void print_card_gw (void *extra, int success, int size, int *card) { mprint_end (ev); } -void callback_extf (void *extra, int success, char *buf) { +void callback_extf (struct tgl_state *TLS, void *extra, int success, char *buf) { struct in_ev *ev = extra; if (ev && !--ev->refcnt) { free (ev); return; } - if (!success) { return; } + if (!success) { print_fail (ev); return; } mprint_start (ev); mprintf (ev, "%s\n", buf); mprint_end (ev); @@ -1957,7 +2047,7 @@ void interpreter_ex (char *line UU, void *ex) { if (*line == '(') { struct in_ev *ev = ex; if (ev) { ev->refcnt ++; } - tgl_do_send_extf (line, strlen (line), callback_extf, ev); + tgl_do_send_extf (TLS, line, strlen (line), callback_extf, ev); in_readline = 0; return; } @@ -2463,13 +2553,13 @@ void print_service_message (struct in_ev *ev, struct tgl_message *M) { mpop_color (ev); mprintf (ev, " "); if (tgl_get_peer_type (M->to_id) == TGL_PEER_CHAT) { - print_chat_name (ev, M->to_id, tgl_peer_get (M->to_id)); + print_chat_name (ev, M->to_id, tgl_peer_get (TLS, M->to_id)); } else { assert (tgl_get_peer_type (M->to_id) == TGL_PEER_ENCR_CHAT); - print_encr_chat_name (ev, M->to_id, tgl_peer_get (M->to_id)); + print_encr_chat_name (ev, M->to_id, tgl_peer_get (TLS, M->to_id)); } mprintf (ev, " "); - print_user_name (ev, M->from_id, tgl_peer_get (M->from_id)); + print_user_name (ev, M->from_id, tgl_peer_get (TLS, M->from_id)); switch (M->action.type) { case tgl_message_action_none: @@ -2496,12 +2586,12 @@ void print_service_message (struct in_ev *ev, struct tgl_message *M) { break; case tgl_message_action_chat_add_user: mprintf (ev, " added user "); - print_user_name (ev, tgl_set_peer_id (TGL_PEER_USER, M->action.user), tgl_peer_get (tgl_set_peer_id (TGL_PEER_USER, M->action.user))); + print_user_name (ev, tgl_set_peer_id (TGL_PEER_USER, M->action.user), tgl_peer_get (TLS, tgl_set_peer_id (TGL_PEER_USER, M->action.user))); mprintf (ev, "\n"); break; case tgl_message_action_chat_delete_user: mprintf (ev, " deleted user "); - print_user_name (ev, tgl_set_peer_id (TGL_PEER_USER, M->action.user), tgl_peer_get (tgl_set_peer_id (TGL_PEER_USER, M->action.user))); + print_user_name (ev, tgl_set_peer_id (TGL_PEER_USER, M->action.user), tgl_peer_get (TLS, tgl_set_peer_id (TGL_PEER_USER, M->action.user))); mprintf (ev, "\n"); break; case tgl_message_action_set_message_ttl: @@ -2564,7 +2654,7 @@ void print_message (struct in_ev *ev, struct tgl_message *M) { print_date (ev, M->date); mpop_color (ev); mprintf (ev, " "); - print_user_name (ev, M->to_id, tgl_peer_get (M->to_id)); + print_user_name (ev, M->to_id, tgl_peer_get (TLS, M->to_id)); mpush_color (ev, COLOR_GREEN); if (M->unread) { mprintf (ev, " <<< "); @@ -2579,7 +2669,7 @@ void print_message (struct in_ev *ev, struct tgl_message *M) { print_date (ev, M->date); mpop_color (ev); mprintf (ev, " "); - print_user_name (ev, M->from_id, tgl_peer_get (M->from_id)); + print_user_name (ev, M->from_id, tgl_peer_get (TLS, M->from_id)); mpush_color (ev, COLOR_BLUE); if (M->unread) { mprintf (ev, " >>> "); @@ -2588,7 +2678,7 @@ void print_message (struct in_ev *ev, struct tgl_message *M) { } } } else if (tgl_get_peer_type (M->to_id) == TGL_PEER_ENCR_CHAT) { - tgl_peer_t *P = tgl_peer_get (M->to_id); + tgl_peer_t *P = tgl_peer_get (TLS, M->to_id); assert (P); if (M->out) { mpush_color (ev, COLOR_GREEN); @@ -2629,10 +2719,10 @@ void print_message (struct in_ev *ev, struct tgl_message *M) { print_date (ev, M->date); mpop_color (ev); mprintf (ev, " "); - print_chat_name (ev, M->to_id, tgl_peer_get (M->to_id)); + print_chat_name (ev, M->to_id, tgl_peer_get (TLS, M->to_id)); mprintf (ev, " "); - print_user_name (ev, M->from_id, tgl_peer_get (M->from_id)); - if ((tgl_get_peer_type (M->from_id) == TGL_PEER_USER) && (tgl_get_peer_id (M->from_id) == tgl_state.our_id)) { + print_user_name (ev, M->from_id, tgl_peer_get (TLS, M->from_id)); + if ((tgl_get_peer_type (M->from_id) == TGL_PEER_USER) && (tgl_get_peer_id (M->from_id) == TLS->our_id)) { mpush_color (ev, COLOR_GREEN); } else { mpush_color (ev, COLOR_BLUE); @@ -2645,7 +2735,7 @@ void print_message (struct in_ev *ev, struct tgl_message *M) { } if (tgl_get_peer_type (M->fwd_from_id) == TGL_PEER_USER) { mprintf (ev, "[fwd from "); - print_user_name (ev, M->fwd_from_id, tgl_peer_get (M->fwd_from_id)); + print_user_name (ev, M->fwd_from_id, tgl_peer_get (TLS, M->fwd_from_id)); mprintf (ev, "] "); } if (M->message && strlen (M->message)) { diff --git a/interface.h b/interface.h index 02e1479..81fa41b 100644 --- a/interface.h +++ b/interface.h @@ -45,7 +45,7 @@ void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2))) #define vlogprintf(v,...) \ do { \ - if (tgl_state.verbosity >= (v)) {\ + if (TLS->verbosity >= (v)) {\ logprintf (__VA_ARGS__);\ }\ } while (0);\ diff --git a/loop.c b/loop.c index 50f177a..0d48cd4 100644 --- a/loop.c +++ b/loop.c @@ -59,6 +59,8 @@ #include "lua-tg.h" #include "tgl.h" #include "binlog.h" +#include "net.h" +#include "tgl-timers.h" int verbosity; extern int readline_disabled; @@ -92,6 +94,7 @@ static int delete_stdin_event; extern volatile int sigterm_cnt; extern char *start_command; +extern struct tgl_state *TLS; static void stdin_read_callback_all (int arg, short what, struct event *self) { if (!readline_disabled) { @@ -119,7 +122,7 @@ static void stdin_read_callback_all (int arg, short what, struct event *self) { break; } if (r == 0) { - //struct event *ev = event_base_get_running_event (tgl_state.ev_base); + //struct event *ev = event_base_get_running_event (TLS->ev_base); //event_del (ev); //event_del (self); @@ -165,16 +168,16 @@ void net_loop (int flags, int (*is_end)(void)) { } if (flags & 3) { if (flags & 1) { - term_ev = event_new (tgl_state.ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback_char, 0); + term_ev = event_new (TLS->ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback_char, 0); } else { - term_ev = event_new (tgl_state.ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback_line, 0); + term_ev = event_new (TLS->ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback_line, 0); } event_add (term_ev, 0); } int last_get_state = time (0); while (!is_end || !is_end ()) { - event_base_loop (tgl_state.ev_base, EVLOOP_ONCE); + event_base_loop (TLS->ev_base, EVLOOP_ONCE); if (term_ev && delete_stdin_event) { event_free (term_ev); @@ -184,7 +187,7 @@ void net_loop (int flags, int (*is_end)(void)) { #ifdef USE_LUA lua_do_all (); #endif - if (safe_quit && !tgl_state.active_queries) { + if (safe_quit && !TLS->active_queries) { printf ("All done. Exit\n"); do_halt (0); } @@ -192,7 +195,7 @@ void net_loop (int flags, int (*is_end)(void)) { do_halt (0); } if (time (0) - last_get_state > 3600) { - tgl_do_lookup_state (); + tgl_do_lookup_state (TLS); last_get_state = time (0); } write_state_file (); @@ -200,7 +203,7 @@ void net_loop (int flags, int (*is_end)(void)) { if (unknown_user_list_pos) { 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); + tgl_do_get_user_info (TLS, TGL_MK_USER (unknown_user_list[i]), 0, 0, 0); } unknown_user_list_pos = 0; } @@ -250,13 +253,13 @@ int main_loop (void) { struct tgl_dc *cur_a_dc; int is_authorized (void) { - return tgl_authorized_dc (cur_a_dc); + return tgl_authorized_dc (TLS, cur_a_dc); } int all_authorized (void) { int i; - for (i = 0; i <= tgl_state.max_dc_num; i++) if (tgl_state.DC_list[i]) { - if (!tgl_authorized_dc (tgl_state.DC_list[i])) { + for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i]) { + if (!tgl_authorized_dc (TLS, TLS->DC_list[i])) { return 0; } } @@ -280,7 +283,8 @@ void on_get_config (void *extra, int success) { int should_register; char *hash; -void sign_in_callback (void *extra, int success, int registered, const char *mhash) { +void sign_in_callback (struct tgl_state *TLSR, void *extra, int success, int registered, const char *mhash) { + assert (TLSR == TLS); if (!success) { logprintf ("Can not send code\n"); do_halt (1); @@ -293,7 +297,8 @@ void sign_in_callback (void *extra, int success, int registered, const char *mha int signed_in_ok; -void sign_in_result (void *extra, int success, struct tgl_user *U) { +void sign_in_result (struct tgl_state *TLSR, void *extra, int success, struct tgl_user *U) { + assert (TLSR == TLS); if (!success) { logprintf ("Can not login\n"); do_halt (1); @@ -310,10 +315,11 @@ int sent_code (void) { } int dc_signed_in (void) { - return tgl_signed_dc (cur_a_dc); + return tgl_signed_dc (TLS, cur_a_dc); } -void export_auth_callback (void *DC, int success) { +void export_auth_callback (struct tgl_state *TLSR, void *DC, int success) { + assert (TLSR == TLS); if (!success) { logprintf ("Can not export auth\n"); do_halt (1); @@ -321,7 +327,8 @@ void export_auth_callback (void *DC, int success) { } int d_got_ok; -void get_difference_callback (void *extra, int success) { +void get_difference_callback (struct tgl_state *TLSR, void *extra, int success) { + assert (TLSR == TLS); assert (success); d_got_ok = 1; } @@ -368,10 +375,10 @@ void read_state_file (void) { int seq = x[2]; int date = x[3]; close (state_file_fd); - bl_do_set_seq (seq); - bl_do_set_pts (pts); - bl_do_set_qts (qts); - bl_do_set_date (date); + bl_do_set_seq (TLS, seq); + bl_do_set_pts (TLS, pts); + bl_do_set_qts (TLS, qts); + bl_do_set_date (TLS, date); } @@ -381,8 +388,8 @@ void write_state_file (void) { static int wpts; static int wqts; static int wdate; - if (wseq >= tgl_state.seq && wpts >= tgl_state.pts && wqts >= tgl_state.qts && wdate >= tgl_state.date) { return; } - wseq = tgl_state.seq; wpts = tgl_state.pts; wqts = tgl_state.qts; wdate = tgl_state.date; + if (wseq >= TLS->seq && wpts >= TLS->pts && wqts >= TLS->qts && wdate >= TLS->date) { return; } + wseq = TLS->seq; wpts = TLS->pts; wqts = TLS->qts; wdate = TLS->date; int state_file_fd = open (get_state_filename (), O_CREAT | O_RDWR, 0600); if (state_file_fd < 0) { logprintf ("Can not write state file '%s': %m\n", get_state_filename ()); @@ -426,12 +433,12 @@ void write_auth_file (void) { assert (auth_file_fd >= 0); int x = DC_SERIALIZED_MAGIC; assert (write (auth_file_fd, &x, 4) == 4); - assert (write (auth_file_fd, &tgl_state.max_dc_num, 4) == 4); - assert (write (auth_file_fd, &tgl_state.dc_working_num, 4) == 4); + assert (write (auth_file_fd, &TLS->max_dc_num, 4) == 4); + assert (write (auth_file_fd, &TLS->dc_working_num, 4) == 4); - tgl_dc_iterator_ex (write_dc, &auth_file_fd); + tgl_dc_iterator_ex (TLS, write_dc, &auth_file_fd); - assert (write (auth_file_fd, &tgl_state.our_id, 4) == 4); + assert (write (auth_file_fd, &TLS->our_id, 4) == 4); close (auth_file_fd); } @@ -477,7 +484,7 @@ void write_secret_chat_file (void) { y[0] = secret_chat_fd; y[1] = 0; - tgl_peer_iterator_ex (write_secret_chat, y); + tgl_peer_iterator_ex (TLS, write_secret_chat, y); lseek (secret_chat_fd, 8, SEEK_SET); assert (write (secret_chat_fd, &y[1], 4) == 4); @@ -500,24 +507,24 @@ void read_dc (int auth_file_fd, int id, unsigned ver) { assert (read (auth_file_fd, auth_key, 256) == 256); //bl_do_add_dc (id, ip, l, port, auth_key_id, auth_key); - bl_do_dc_option (id, 2, "DC", l, ip, port); - bl_do_set_auth_key_id (id, auth_key); - bl_do_dc_signed (id); + bl_do_dc_option (TLS, id, 2, "DC", l, ip, port); + bl_do_set_auth_key_id (TLS, id, auth_key); + bl_do_dc_signed (TLS, id); } void empty_auth_file (void) { - if (tgl_state.test_mode) { - bl_do_dc_option (1, 0, "", strlen (TG_SERVER_TEST_1), TG_SERVER_TEST_1, 443); - bl_do_dc_option (2, 0, "", strlen (TG_SERVER_TEST_2), TG_SERVER_TEST_2, 443); - bl_do_dc_option (3, 0, "", strlen (TG_SERVER_TEST_3), TG_SERVER_TEST_3, 443); - bl_do_set_working_dc (2); + if (TLS->test_mode) { + bl_do_dc_option (TLS, 1, 0, "", strlen (TG_SERVER_TEST_1), TG_SERVER_TEST_1, 443); + bl_do_dc_option (TLS, 2, 0, "", strlen (TG_SERVER_TEST_2), TG_SERVER_TEST_2, 443); + bl_do_dc_option (TLS, 3, 0, "", strlen (TG_SERVER_TEST_3), TG_SERVER_TEST_3, 443); + bl_do_set_working_dc (TLS, 2); } else { - bl_do_dc_option (1, 0, "", strlen (TG_SERVER_1), TG_SERVER_1, 443); - bl_do_dc_option (2, 0, "", strlen (TG_SERVER_2), TG_SERVER_2, 443); - bl_do_dc_option (3, 0, "", strlen (TG_SERVER_3), TG_SERVER_3, 443); - bl_do_dc_option (4, 0, "", strlen (TG_SERVER_4), TG_SERVER_4, 443); - bl_do_dc_option (5, 0, "", strlen (TG_SERVER_5), TG_SERVER_5, 443); - bl_do_set_working_dc (2); + bl_do_dc_option (TLS, 1, 0, "", strlen (TG_SERVER_1), TG_SERVER_1, 443); + bl_do_dc_option (TLS, 2, 0, "", strlen (TG_SERVER_2), TG_SERVER_2, 443); + bl_do_dc_option (TLS, 3, 0, "", strlen (TG_SERVER_3), TG_SERVER_3, 443); + bl_do_dc_option (TLS, 4, 0, "", strlen (TG_SERVER_4), TG_SERVER_4, 443); + bl_do_dc_option (TLS, 5, 0, "", strlen (TG_SERVER_5), TG_SERVER_5, 443); + bl_do_set_working_dc (TLS, 4); } } @@ -550,14 +557,14 @@ void read_auth_file (void) { read_dc (auth_file_fd, i, m); } } - bl_do_set_working_dc (dc_working_num); + bl_do_set_working_dc (TLS, dc_working_num); int our_id; int l = read (auth_file_fd, &our_id, 4); if (l < 4) { assert (!l); } if (our_id) { - bl_do_set_our_id (our_id); + bl_do_set_our_id (TLS, our_id); } close (auth_file_fd); } @@ -588,17 +595,17 @@ void read_secret_chat (int fd, int v) { assert (read (fd, &out_seq_no, 4) == 4); } - bl_do_encr_chat_create (id, user_id, admin_id, s, l); - struct tgl_secret_chat *P = (void *)tgl_peer_get (TGL_MK_ENCR_CHAT (id)); + bl_do_encr_chat_create (TLS, id, user_id, admin_id, s, l); + struct tgl_secret_chat *P = (void *)tgl_peer_get (TLS, TGL_MK_ENCR_CHAT (id)); assert (P && (P->flags & FLAG_CREATED)); - bl_do_encr_chat_set_date (P, date); - bl_do_encr_chat_set_ttl (P, ttl); - bl_do_encr_chat_set_layer (P, layer); - bl_do_encr_chat_set_access_hash (P, access_hash); - bl_do_encr_chat_set_state (P, state); - bl_do_encr_chat_set_key (P, key, key_fingerprint); + bl_do_encr_chat_set_date (TLS, P, date); + bl_do_encr_chat_set_ttl (TLS, P, ttl); + bl_do_encr_chat_set_layer (TLS ,P, layer); + bl_do_encr_chat_set_access_hash (TLS, P, access_hash); + bl_do_encr_chat_set_state (TLS, P, state); + bl_do_encr_chat_set_key (TLS, P, key, key_fingerprint); if (v >= 1) { - bl_do_encr_chat_set_seq (P, in_seq_no, last_in_seq_no, out_seq_no); + bl_do_encr_chat_set_seq (TLS, P, in_seq_no, last_in_seq_no, out_seq_no); } } @@ -621,7 +628,8 @@ void read_secret_chat_file (void) { close (secret_chat_fd); } -void dlist_cb (void *callback_extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]) { +void dlist_cb (struct tgl_state *TLSR, void *callback_extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]) { + assert (TLSR == TLS); d_got_ok = 1; } @@ -680,7 +688,7 @@ static void accept_incoming (evutil_socket_t efd, short what, void *arg) { int fd = accept (efd, (struct sockaddr *)&cli_addr, &clilen); assert (fd >= 0); - struct bufferevent *bev = bufferevent_socket_new (tgl_state.ev_base, fd, 0); + struct bufferevent *bev = bufferevent_socket_new (TLS->ev_base, fd, 0); struct in_ev *e = malloc (sizeof (*e)); e->bev = bev; e->refcnt = 1; @@ -693,20 +701,24 @@ static void accept_incoming (evutil_socket_t efd, short what, void *arg) { int loop (void) { //on_start (); - tgl_set_callback (&upd_cb); - //tgl_state.temp_key_expire_time = 60; - tgl_init (); + tgl_set_callback (TLS, &upd_cb); + //TLS->temp_key_expire_time = 60; + struct event_base *ev = event_base_new (); + tgl_set_ev_base (TLS, ev); + tgl_set_net_methods (TLS, &tgl_conn_methods); + tgl_set_timer_methods (TLS, &tgl_libevent_timers); + tgl_init (TLS); if (binlog_enabled) { double t = tglt_get_double_time (); if (verbosity >= E_DEBUG) { logprintf ("replay log start\n"); } - tgl_replay_log (); + tgl_replay_log (TLS); if (verbosity >= E_DEBUG) { logprintf ("replay log end in %lf seconds\n", tglt_get_double_time () - t); } - tgl_reopen_binlog_for_writing (); + tgl_reopen_binlog_for_writing (TLS); } else { read_auth_file (); read_state_file (); @@ -719,31 +731,31 @@ int loop (void) { #endif if (sfd >= 0) { - struct event *ev = event_new (tgl_state.ev_base, sfd, EV_READ | EV_PERSIST, accept_incoming, 0); + struct event *ev = event_new (TLS->ev_base, sfd, EV_READ | EV_PERSIST, accept_incoming, 0); event_add (ev, 0); } if (usfd >= 0) { - struct event *ev = event_new (tgl_state.ev_base, usfd, EV_READ | EV_PERSIST, accept_incoming, 0); + struct event *ev = event_new (TLS->ev_base, usfd, EV_READ | EV_PERSIST, accept_incoming, 0); event_add (ev, 0); } update_prompt (); if (reset_authorization) { - tgl_peer_t *P = tgl_peer_get (TGL_MK_USER (tgl_state.our_id)); + tgl_peer_t *P = tgl_peer_get (TLS, TGL_MK_USER (TLS->our_id)); if (P && P->user.phone && reset_authorization == 1) { set_default_username (P->user.phone); } - bl_do_reset_authorization (); + bl_do_reset_authorization (TLS); } net_loop (0, all_authorized); int i; - for (i = 0; i <= tgl_state.max_dc_num; i++) if (tgl_state.DC_list[i] && !tgl_authorized_dc (tgl_state.DC_list[i])) { + for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i] && !tgl_authorized_dc (TLS, TLS->DC_list[i])) { assert (0); } - if (!tgl_signed_dc (tgl_state.DC_working)) { + if (!tgl_signed_dc (TLS, TLS->DC_working)) { if (disable_output) { fprintf (stderr, "Can not login without output\n"); do_halt (1); @@ -761,7 +773,7 @@ int loop (void) { set_default_username (user); } } - tgl_do_send_code (default_username, sign_in_callback, 0); + tgl_do_send_code (TLS, default_username, sign_in_callback, 0); net_loop (0, sent_code); if (verbosity >= E_DEBUG) { @@ -778,11 +790,11 @@ int loop (void) { } if (!strcmp (code, "call")) { printf ("You typed \"call\", switching to phone system.\n"); - tgl_do_phone_call (default_username, hash, 0, 0); + tgl_do_phone_call (TLS, default_username, hash, 0, 0); printf ("Calling you! Code: "); continue; } - if (tgl_do_send_code_result (default_username, hash, code, sign_in_result, 0) >= 0) { + if (tgl_do_send_code_result (TLS, default_username, hash, code, sign_in_result, 0) >= 0) { break; } printf ("Invalid code. Try again: "); @@ -822,11 +834,11 @@ int loop (void) { } if (!strcmp (code, "call")) { printf ("You typed \"call\", switching to phone system.\n"); - tgl_do_phone_call (default_username, hash, 0, 0); + tgl_do_phone_call (TLS, default_username, hash, 0, 0); printf ("Calling you! Code: "); continue; } - if (tgl_do_send_code_result_auth (default_username, hash, code, first_name, last_name, sign_in_result, 0) >= 0) { + if (tgl_do_send_code_result_auth (TLS, default_username, hash, code, first_name, last_name, sign_in_result, 0) >= 0) { break; } printf ("Invalid code. Try again: "); @@ -835,14 +847,14 @@ int loop (void) { } net_loop (0, signed_in); - //bl_do_dc_signed (tgl_state.DC_working); + //bl_do_dc_signed (TLS->DC_working); } - 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, export_auth_callback, (void*)(long)tgl_state.DC_list[i]); - cur_a_dc = tgl_state.DC_list[i]; + for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i] && !tgl_signed_dc (TLS, TLS->DC_list[i])) { + tgl_do_export_auth (TLS, i, export_auth_callback, (void*)(long)TLS->DC_list[i]); + cur_a_dc = TLS->DC_list[i]; net_loop (0, dc_signed_in); - assert (tgl_signed_dc (tgl_state.DC_list[i])); + assert (tgl_signed_dc (TLS, TLS->DC_list[i])); } write_auth_file (); @@ -854,14 +866,14 @@ int loop (void) { set_interface_callbacks (); - tglm_send_all_unsent (); - tgl_do_get_difference (sync_from_start, get_difference_callback, 0); + tglm_send_all_unsent (TLS); + tgl_do_get_difference (TLS, sync_from_start, get_difference_callback, 0); net_loop (0, dgot); - assert (!(tgl_state.locks & TGL_LOCK_DIFF)); - tgl_state.started = 1; + assert (!(TLS->locks & TGL_LOCK_DIFF)); + TLS->started = 1; if (wait_dialog_list) { d_got_ok = 0; - tgl_do_get_dialog_list (dlist_cb, 0); + tgl_do_get_dialog_list (TLS, dlist_cb, 0); net_loop (0, dgot); } #ifdef USE_LUA diff --git a/lua-tg.c b/lua-tg.c index 0b73d38..c1b2628 100644 --- a/lua-tg.c +++ b/lua-tg.c @@ -47,6 +47,7 @@ lua_State *luaState; #include extern int verbosity; +extern struct tgl_state *TLS; static int have_file; @@ -119,7 +120,7 @@ void push_chat (tgl_peer_t *P) { for (i = 0; i < P->chat.users_num; i++) { lua_pushnumber (luaState, i); tgl_peer_id_t id = TGL_MK_USER (P->chat.user_list[i].user_id); - push_peer (id, tgl_peer_get (id)); + push_peer (id, tgl_peer_get (TLS, id)); lua_settable (luaState, -3); } lua_settable (luaState, -3); @@ -129,7 +130,7 @@ void push_chat (tgl_peer_t *P) { void push_encr_chat (tgl_peer_t *P) { my_lua_checkstack (luaState, 4); lua_pushstring (luaState, "user"); - push_peer (TGL_MK_USER (P->encr_chat.user_id), tgl_peer_get (TGL_MK_USER (P->encr_chat.user_id))); + push_peer (TGL_MK_USER (P->encr_chat.user_id), tgl_peer_get (TLS, TGL_MK_USER (P->encr_chat.user_id))); lua_settable (luaState, -3); } @@ -298,18 +299,18 @@ void push_message (struct tgl_message *M) { if (tgl_get_peer_type (M->fwd_from_id)) { lua_pushstring (luaState, "fwd_from"); - push_peer (M->fwd_from_id, tgl_peer_get (M->fwd_from_id)); + push_peer (M->fwd_from_id, tgl_peer_get (TLS, M->fwd_from_id)); lua_settable (luaState, -3); // fwd_from lua_add_num_field ("fwd_date", M->fwd_date); } lua_pushstring (luaState, "from"); - push_peer (M->from_id, tgl_peer_get (M->from_id)); + push_peer (M->from_id, tgl_peer_get (TLS, M->from_id)); lua_settable (luaState, -3); lua_pushstring (luaState, "to"); - push_peer (M->to_id, tgl_peer_get (M->to_id)); + push_peer (M->to_id, tgl_peer_get (TLS, M->to_id)); lua_settable (luaState, -3); lua_pushstring (luaState, "out"); @@ -456,7 +457,7 @@ void *lua_ptr[MAX_LUA_COMMANDS]; static int pos; static inline tgl_peer_t *get_peer (const char *s) { - return tgl_peer_get_by_name (s); + return tgl_peer_get_by_name (TLS, s); } enum lua_query_type { @@ -508,7 +509,8 @@ struct lua_query_extra { int param; }; -void lua_empty_cb (void *cb_extra, int success) { +void lua_empty_cb (struct tgl_state *TLSR, void *cb_extra, int success) { + assert (TLSR == TLS); struct lua_query_extra *cb = cb_extra; lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -533,7 +535,8 @@ void lua_empty_cb (void *cb_extra, int success) { free (cb); } -void lua_contact_list_cb (void *cb_extra, int success, int num, struct tgl_user **UL) { +void lua_contact_list_cb (struct tgl_state *TLSR, void *cb_extra, int success, int num, struct tgl_user **UL) { + assert (TLSR == TLS); struct lua_query_extra *cb = cb_extra; lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -570,7 +573,8 @@ void lua_contact_list_cb (void *cb_extra, int success, int num, struct tgl_user free (cb); } -void lua_dialog_list_cb (void *cb_extra, int success, int num, tgl_peer_id_t peers[], int msgs[], int unread[]) { +void lua_dialog_list_cb (struct tgl_state *TLSR, void *cb_extra, int success, int num, tgl_peer_id_t peers[], int msgs[], int unread[]) { + assert (TLSR == TLS); struct lua_query_extra *cb = cb_extra; lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -589,10 +593,10 @@ void lua_dialog_list_cb (void *cb_extra, int success, int num, tgl_peer_id_t pee lua_newtable (luaState); lua_pushstring (luaState, "peer"); - push_peer (peers[i], tgl_peer_get (peers[i])); + push_peer (peers[i], tgl_peer_get (TLS, peers[i])); lua_settable (luaState, -3); - struct tgl_message *M = tgl_message_get (msgs[i]); + struct tgl_message *M = tgl_message_get (TLS, msgs[i]); if (M && (M->flags & FLAG_CREATED)) { lua_pushstring (luaState, "message"); push_message (M); @@ -623,7 +627,8 @@ void lua_dialog_list_cb (void *cb_extra, int success, int num, tgl_peer_id_t pee free (cb); } -void lua_msg_cb (void *cb_extra, int success, struct tgl_message *M) { +void lua_msg_cb (struct tgl_state *TLSR, void *cb_extra, int success, struct tgl_message *M) { + assert (TLSR == TLS); struct lua_query_extra *cb = cb_extra; lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -654,7 +659,8 @@ void lua_msg_cb (void *cb_extra, int success, struct tgl_message *M) { free (cb); } -void lua_msg_list_cb (void *cb_extra, int success, int num, struct tgl_message *M[]) { +void lua_msg_list_cb (struct tgl_state *TLSR, void *cb_extra, int success, int num, struct tgl_message *M[]) { + assert (TLSR == TLS); struct lua_query_extra *cb = cb_extra; lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -691,7 +697,8 @@ void lua_msg_list_cb (void *cb_extra, int success, int num, struct tgl_message * free (cb); } -void lua_file_cb (void *cb_extra, int success, char *file_name) { +void lua_file_cb (struct tgl_state *TLSR, void *cb_extra, int success, char *file_name) { + assert (TLSR == TLS); struct lua_query_extra *cb = cb_extra; lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -722,7 +729,8 @@ void lua_file_cb (void *cb_extra, int success, char *file_name) { free (cb); } -void lua_chat_cb (void *cb_extra, int success, struct tgl_chat *C) { +void lua_chat_cb (struct tgl_state *TLSR, void *cb_extra, int success, struct tgl_chat *C) { + assert (TLSR == TLS); struct lua_query_extra *cb = cb_extra; lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -753,7 +761,8 @@ void lua_chat_cb (void *cb_extra, int success, struct tgl_chat *C) { free (cb); } -void lua_secret_chat_cb (void *cb_extra, int success, struct tgl_secret_chat *C) { +void lua_secret_chat_cb (struct tgl_state *TLSR, void *cb_extra, int success, struct tgl_secret_chat *C) { + assert (TLSR == TLS); struct lua_query_extra *cb = cb_extra; lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -784,7 +793,8 @@ void lua_secret_chat_cb (void *cb_extra, int success, struct tgl_secret_chat *C) free (cb); } -void lua_user_cb (void *cb_extra, int success, struct tgl_user *C) { +void lua_user_cb (struct tgl_state *TLSR, void *cb_extra, int success, struct tgl_user *C) { + assert (TLSR == TLS); struct lua_query_extra *cb = cb_extra; lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -815,7 +825,8 @@ void lua_user_cb (void *cb_extra, int success, struct tgl_user *C) { free (cb); } -void lua_str_cb (void *cb_extra, int success, char *data) { +void lua_str_cb (struct tgl_state *TLSR, void *cb_extra, int success, char *data) { + assert (TLSR == TLS); struct lua_query_extra *cb = cb_extra; lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -856,60 +867,60 @@ void lua_do_all (void) { char *s, *s1, *s2, *s3; switch (f) { case lq_contact_list: - tgl_do_update_contact_list (lua_contact_list_cb, lua_ptr[p ++]); + tgl_do_update_contact_list (TLS, lua_contact_list_cb, lua_ptr[p ++]); break; case lq_dialog_list: - tgl_do_get_dialog_list (lua_dialog_list_cb, lua_ptr[p ++]); + tgl_do_get_dialog_list (TLS, lua_dialog_list_cb, lua_ptr[p ++]); break; case lq_msg: - tgl_do_send_message (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], strlen (lua_ptr[p + 2]), lua_msg_cb, lua_ptr[p]); + tgl_do_send_message (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], strlen (lua_ptr[p + 2]), lua_msg_cb, lua_ptr[p]); free (lua_ptr[p + 2]); p += 3; break; case lq_rename_chat: - tgl_do_rename_chat (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); + tgl_do_rename_chat (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); free (lua_ptr[p + 2]); p += 3; break; case lq_send_photo: - tgl_do_send_photo (tgl_message_media_photo, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); + tgl_do_send_photo (TLS, tgl_message_media_photo, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); free (lua_ptr[p + 2]); p += 3; break; case lq_send_video: - tgl_do_send_photo (tgl_message_media_video, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); + tgl_do_send_photo (TLS, tgl_message_media_video, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); free (lua_ptr[p + 2]); p += 3; break; case lq_send_audio: - tgl_do_send_photo (tgl_message_media_audio, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); + tgl_do_send_photo (TLS, tgl_message_media_audio, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); free (lua_ptr[p + 2]); p += 3; break; case lq_send_document: - tgl_do_send_photo (tgl_message_media_document, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); + tgl_do_send_photo (TLS, tgl_message_media_document, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); free (lua_ptr[p + 2]); p += 3; break; case lq_send_text: - tgl_do_send_text (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); + tgl_do_send_text (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); free (lua_ptr[p + 2]); p += 3; break; case lq_chat_set_photo: - tgl_do_set_chat_photo (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); + tgl_do_set_chat_photo (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]); free (lua_ptr[p + 2]); p += 3; break; case lq_load_photo: M = lua_ptr[p + 1]; if (!M || (M->media.type != tgl_message_media_photo && M->media.type != tgl_message_media_photo_encr)) { - lua_file_cb (lua_ptr[p], 0, 0); + lua_file_cb (TLS, lua_ptr[p], 0, 0); } else { if (M->media.type == tgl_message_media_photo) { - tgl_do_load_photo (&M->media.photo, lua_file_cb, lua_ptr[p]); + tgl_do_load_photo (TLS, &M->media.photo, lua_file_cb, lua_ptr[p]); } else { - tgl_do_load_encr_video (&M->media.encr_video, lua_file_cb, lua_ptr[p]); + tgl_do_load_encr_video (TLS, &M->media.encr_video, lua_file_cb, lua_ptr[p]); } } p += 2; @@ -917,12 +928,12 @@ void lua_do_all (void) { case lq_load_video: M = lua_ptr[p + 1]; if (!M || (M->media.type != tgl_message_media_video && M->media.type != tgl_message_media_video_encr)) { - lua_file_cb (lua_ptr[p], 0, 0); + lua_file_cb (TLS, lua_ptr[p], 0, 0); } else { if (M->media.type == tgl_message_media_video) { - tgl_do_load_video (&M->media.video, lua_file_cb, lua_ptr[p]); + tgl_do_load_video (TLS, &M->media.video, lua_file_cb, lua_ptr[p]); } else { - tgl_do_load_encr_video (&M->media.encr_video, lua_file_cb, lua_ptr[p]); + tgl_do_load_encr_video (TLS, &M->media.encr_video, lua_file_cb, lua_ptr[p]); } } p += 2; @@ -930,21 +941,21 @@ void lua_do_all (void) { case lq_load_video_thumb: M = lua_ptr[p + 1]; if (!M || (M->media.type != tgl_message_media_video)) { - lua_file_cb (lua_ptr[p], 0, 0); + lua_file_cb (TLS, lua_ptr[p], 0, 0); } else { - tgl_do_load_video_thumb (&M->media.video, lua_file_cb, lua_ptr[p]); + tgl_do_load_video_thumb (TLS, &M->media.video, lua_file_cb, lua_ptr[p]); } p += 2; break; case lq_load_audio: M = lua_ptr[p + 1]; if (!M || (M->media.type != tgl_message_media_audio && M->media.type != tgl_message_media_audio_encr)) { - lua_file_cb (lua_ptr[p], 0, 0); + lua_file_cb (TLS, lua_ptr[p], 0, 0); } else { if (M->media.type == tgl_message_media_audio) { - tgl_do_load_audio (&M->media.audio, lua_file_cb, lua_ptr[p]); + tgl_do_load_audio (TLS, &M->media.audio, lua_file_cb, lua_ptr[p]); } else { - tgl_do_load_encr_video (&M->media.encr_video, lua_file_cb, lua_ptr[p]); + tgl_do_load_encr_video (TLS, &M->media.encr_video, lua_file_cb, lua_ptr[p]); } } p += 2; @@ -952,12 +963,12 @@ void lua_do_all (void) { case lq_load_document: M = lua_ptr[p + 1]; if (!M || (M->media.type != tgl_message_media_document && M->media.type != tgl_message_media_document_encr)) { - lua_file_cb (lua_ptr[p], 0, 0); + lua_file_cb (TLS, lua_ptr[p], 0, 0); } else { if (M->media.type == tgl_message_media_document) { - tgl_do_load_document (&M->media.document, lua_file_cb, lua_ptr[p]); + tgl_do_load_document (TLS, &M->media.document, lua_file_cb, lua_ptr[p]); } else { - tgl_do_load_encr_video (&M->media.encr_video, lua_file_cb, lua_ptr[p]); + tgl_do_load_encr_video (TLS, &M->media.encr_video, lua_file_cb, lua_ptr[p]); } } p += 2; @@ -965,59 +976,59 @@ void lua_do_all (void) { case lq_load_document_thumb: M = lua_ptr[p + 1]; if (!M || (M->media.type != tgl_message_media_document)) { - lua_file_cb (lua_ptr[p], 0, 0); + lua_file_cb (TLS, lua_ptr[p], 0, 0); } else { - tgl_do_load_document_thumb (&M->media.document, lua_file_cb, lua_ptr[p]); + tgl_do_load_document_thumb (TLS, &M->media.document, lua_file_cb, lua_ptr[p]); } p += 2; break; case lq_fwd: - tgl_do_forward_message (((tgl_peer_t *)lua_ptr[p + 1])->id, ((struct tgl_message *)lua_ptr[p + 2])->id, lua_msg_cb, lua_ptr[p]); + tgl_do_forward_message (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, ((struct tgl_message *)lua_ptr[p + 2])->id, lua_msg_cb, lua_ptr[p]); p += 3; break; case lq_fwd_media: - tgl_do_forward_media (((tgl_peer_t *)lua_ptr[p + 1])->id, ((struct tgl_message *)lua_ptr[p + 2])->id, lua_msg_cb, lua_ptr[p]); + tgl_do_forward_media (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, ((struct tgl_message *)lua_ptr[p + 2])->id, lua_msg_cb, lua_ptr[p]); p += 3; break; case lq_chat_info: - tgl_do_get_chat_info (((tgl_peer_t *)lua_ptr[p + 1])->id, 0, lua_chat_cb, lua_ptr[p]); + tgl_do_get_chat_info (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, 0, lua_chat_cb, lua_ptr[p]); p += 2; break; case lq_user_info: - tgl_do_get_user_info (((tgl_peer_t *)lua_ptr[p + 1])->id, 0, lua_user_cb, lua_ptr[p]); + tgl_do_get_user_info (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, 0, lua_user_cb, lua_ptr[p]); p += 2; break; case lq_history: - tgl_do_get_history (((tgl_peer_t *)lua_ptr[p + 1])->id, (long)lua_ptr[p + 2], 0, lua_msg_list_cb, lua_ptr[p]); + tgl_do_get_history (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, (long)lua_ptr[p + 2], 0, lua_msg_list_cb, lua_ptr[p]); p += 3; break; case lq_chat_add_user: - tgl_do_add_user_to_chat (((tgl_peer_t *)lua_ptr[p + 1])->id, ((tgl_peer_t *)lua_ptr[p + 2])->id, 10, lua_msg_cb, lua_ptr[p]); + tgl_do_add_user_to_chat (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, ((tgl_peer_t *)lua_ptr[p + 2])->id, 10, lua_msg_cb, lua_ptr[p]); p += 3; break; case lq_chat_del_user: - tgl_do_del_user_from_chat (((tgl_peer_t *)lua_ptr[p + 1])->id, ((tgl_peer_t *)lua_ptr[p + 2])->id, lua_msg_cb, lua_ptr[p]); + tgl_do_del_user_from_chat (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, ((tgl_peer_t *)lua_ptr[p + 2])->id, lua_msg_cb, lua_ptr[p]); p += 3; break; case lq_add_contact: s1 = lua_ptr[p + 1]; s2 = lua_ptr[p + 2]; s3 = lua_ptr[p + 3]; - tgl_do_add_contact (s1, strlen (s1), s2, strlen (s2), s3, strlen (s3), 0, lua_contact_list_cb, lua_ptr[p]); + tgl_do_add_contact (TLS, s1, strlen (s1), s2, strlen (s2), s3, strlen (s3), 0, lua_contact_list_cb, lua_ptr[p]); free (s1); free (s2); free (s3); p += 4; break; case lq_del_contact: - tgl_do_del_contact (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_empty_cb, lua_ptr[p]); + tgl_do_del_contact (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_empty_cb, lua_ptr[p]); p += 2; break; case lq_rename_contact: s1 = lua_ptr[p + 1]; s2 = lua_ptr[p + 2]; s3 = lua_ptr[p + 3]; - tgl_do_add_contact (s1, strlen (s1), s2, strlen (s2), s3, strlen (s3), 1, lua_contact_list_cb, lua_ptr[p]); + tgl_do_add_contact (TLS, s1, strlen (s1), s2, strlen (s2), s3, strlen (s3), 1, lua_contact_list_cb, lua_ptr[p]); free (s1); free (s2); free (s3); @@ -1025,85 +1036,85 @@ void lua_do_all (void) { break; case lq_search: s = lua_ptr[p + 2]; - tgl_do_msg_search (((tgl_peer_t *)lua_ptr[p + 1])->id, 0, 0, 40, 0, s, lua_msg_list_cb, lua_ptr[p]); + tgl_do_msg_search (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, 0, 0, 40, 0, s, lua_msg_list_cb, lua_ptr[p]); free (s); p += 3; break; case lq_global_search: s = lua_ptr[p + 1]; - tgl_do_msg_search (tgl_set_peer_id (TGL_PEER_UNKNOWN, 0), 0, 0, 40, 0, s, lua_msg_list_cb, lua_ptr[p]); + tgl_do_msg_search (TLS, tgl_set_peer_id (TGL_PEER_UNKNOWN, 0), 0, 0, 40, 0, s, lua_msg_list_cb, lua_ptr[p]); free (s); p += 2; break; case lq_mark_read: - tgl_do_mark_read (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_empty_cb, lua_ptr[p]); + tgl_do_mark_read (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_empty_cb, lua_ptr[p]); p += 2; break; case lq_set_profile_photo: s = lua_ptr[p + 1]; - tgl_do_set_profile_photo (s, lua_empty_cb, lua_ptr[p]); + tgl_do_set_profile_photo (TLS, s, lua_empty_cb, lua_ptr[p]); free (s); p += 2; break; case lq_set_profile_name: s1 = lua_ptr[p + 1]; s2 = lua_ptr[p + 1]; - tgl_do_set_profile_name (s1, s2, lua_user_cb, lua_ptr[p]); + tgl_do_set_profile_name (TLS, s1, s2, lua_user_cb, lua_ptr[p]); free (s1); free (s2); p += 3; break; case lq_create_secret_chat: - tgl_do_create_secret_chat (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_secret_chat_cb, lua_ptr[p]); + tgl_do_create_secret_chat (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_secret_chat_cb, lua_ptr[p]); p += 2; break; case lq_create_group_chat: s = lua_ptr[p + 2]; - tgl_do_create_group_chat (((tgl_peer_t *)lua_ptr[p + 1])->id, s, lua_msg_cb, lua_ptr[p]); + tgl_do_create_group_chat (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, s, lua_msg_cb, lua_ptr[p]); free (s); p += 3; break; case lq_delete_msg: - tgl_do_delete_msg (((struct tgl_message *)lua_ptr[p + 1])->id, lua_empty_cb, lua_ptr[p]); + tgl_do_delete_msg (TLS, ((struct tgl_message *)lua_ptr[p + 1])->id, lua_empty_cb, lua_ptr[p]); p += 2; break; case lq_restore_msg: - tgl_do_delete_msg ((long)lua_ptr[p + 1], lua_empty_cb, lua_ptr[p]); + tgl_do_delete_msg (TLS, (long)lua_ptr[p + 1], lua_empty_cb, lua_ptr[p]); p += 2; break; case lq_accept_secret_chat: - tgl_do_accept_encr_chat_request (lua_ptr[p + 1], lua_secret_chat_cb, lua_ptr[p]); + tgl_do_accept_encr_chat_request (TLS, lua_ptr[p + 1], lua_secret_chat_cb, lua_ptr[p]); p += 2; break; case lq_send_contact: s1 = lua_ptr[p + 2]; s2 = lua_ptr[p + 3]; s3 = lua_ptr[p + 4]; - tgl_do_send_contact (((tgl_peer_t *)lua_ptr[p + 1])->id, s1, strlen (s1), s2, strlen (s2), s3, strlen (s3), lua_msg_cb, lua_ptr[p]); + tgl_do_send_contact (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, s1, strlen (s1), s2, strlen (s2), s3, strlen (s3), lua_msg_cb, lua_ptr[p]); free (s1); free (s2); free (s3); p += 5; break; case lq_status_online: - tgl_do_update_status (1, lua_empty_cb, lua_ptr[p]); + tgl_do_update_status (TLS, 1, lua_empty_cb, lua_ptr[p]); p ++; break; case lq_status_offline: - tgl_do_update_status (0, lua_empty_cb, lua_ptr[p]); + tgl_do_update_status (TLS, 0, lua_empty_cb, lua_ptr[p]); p ++; break; case lq_extf: s = lua_ptr[p + 1]; - tgl_do_send_extf (s, strlen (s), lua_str_cb, lua_ptr[p]); + tgl_do_send_extf (TLS, s, strlen (s), lua_str_cb, lua_ptr[p]); free (s); p += 2; break; case lq_send_location: if (sizeof (void *) == 4) { - tgl_do_send_location (((tgl_peer_t *)lua_ptr[p + 1])->id , *(float *)(lua_ptr + p + 2), *(float *)(lua_ptr + p + 3), lua_msg_cb, lua_ptr[p]); + tgl_do_send_location (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id , *(float *)(lua_ptr + p + 2), *(float *)(lua_ptr + p + 3), lua_msg_cb, lua_ptr[p]); } else { - tgl_do_send_location (((tgl_peer_t *)lua_ptr[p + 1])->id , *(double *)(lua_ptr + p + 2), *(double *)(lua_ptr + p + 3), lua_msg_cb, lua_ptr[p]); + tgl_do_send_location (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id , *(double *)(lua_ptr + p + 2), *(double *)(lua_ptr + p + 3), lua_msg_cb, lua_ptr[p]); } p += 4; break; @@ -1243,11 +1254,11 @@ static int parse_lua_function (lua_State *L, struct lua_function *F) { break; } if (sscanf (s, "user#id%lld", &num) == 1 && num > 0) { - tgl_insert_empty_user (num); - P = tgl_peer_get (TGL_MK_USER (num)); + tgl_insert_empty_user (TLS, num); + P = tgl_peer_get (TLS, TGL_MK_USER (num)); } else if (sscanf (s, "chat#id%lld", &num) == 1 && num > 0) { - tgl_insert_empty_chat (num); - P = tgl_peer_get (TGL_MK_CHAT (num)); + tgl_insert_empty_chat (TLS, num); + P = tgl_peer_get (TLS, TGL_MK_CHAT (num)); } else { P = get_peer (s); } @@ -1321,7 +1332,7 @@ static int parse_lua_function (lua_State *L, struct lua_function *F) { num = atoll (s); - M = tgl_message_get (num); + M = tgl_message_get (TLS, num); if (!M || !(M->flags & FLAG_CREATED)) { ok = 0; @@ -1398,7 +1409,7 @@ static int postpone_from_lua (lua_State *L) { int *t = malloc (16); assert (t); - struct event *ev = evtimer_new (tgl_state.ev_base, lua_postpone_alarm, t); + struct event *ev = evtimer_new (TLS->ev_base, lua_postpone_alarm, t); t[0] = a1; t[1] = a2; *(void **)(t + 2) = ev; diff --git a/main.c b/main.c index 1149df0..83fa3c1 100644 --- a/main.c +++ b/main.c @@ -114,6 +114,8 @@ int port; int use_ids; char *start_command; +struct tgl_state *TLS; + void set_default_username (const char *s) { if (default_username) { tfree_str (default_username); @@ -348,7 +350,7 @@ void parse_config (void) { strcpy (buf + l, "test"); config_lookup_bool (&conf, buf, &test_mode); if (test_mode) { - tgl_set_test_mode (); + tgl_set_test_mode (TLS); } strcpy (buf + l, "log_level"); @@ -378,20 +380,20 @@ void parse_config (void) { strcpy (buf + l, "pfs_enabled"); config_lookup_bool (&conf, buf, &pfs_enabled); if (pfs_enabled) { - tgl_enable_pfs (); + tgl_enable_pfs (TLS); } if (binlog_enabled) { parse_config_val (&conf, &binlog_file_name, "binlog", BINLOG_FILE, config_directory); - tgl_set_binlog_mode (1); - tgl_set_binlog_path (binlog_file_name); + tgl_set_binlog_mode (TLS, 1); + tgl_set_binlog_path (TLS, binlog_file_name); } else { - tgl_set_binlog_mode (0); + tgl_set_binlog_mode (TLS, 0); parse_config_val (&conf, &state_file_name, "state_file", STATE_FILE, config_directory); parse_config_val (&conf, &secret_chat_file_name, "secret", SECRET_CHAT_FILE, config_directory); //tgl_set_auth_file_path (auth_file_name); } - tgl_set_download_directory (downloads_directory); + tgl_set_download_directory (TLS, downloads_directory); if (!mkdir (config_directory, CONFIG_DIRECTORY_MODE)) { if (!disable_output) { @@ -414,16 +416,16 @@ void parse_config (void) { if (binlog_enabled) { tasprintf (&binlog_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, BINLOG_FILE); - tgl_set_binlog_mode (1); - tgl_set_binlog_path (binlog_file_name); + tgl_set_binlog_mode (TLS, 1); + tgl_set_binlog_path (TLS, binlog_file_name); } else { - tgl_set_binlog_mode (0); + tgl_set_binlog_mode (TLS, 0); //tgl_set_auth_file_path (auth_file_name; tasprintf (&auth_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, AUTH_KEY_FILE); tasprintf (&state_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, STATE_FILE); tasprintf (&secret_chat_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, SECRET_CHAT_FILE); } - tgl_set_download_directory (downloads_directory); + tgl_set_download_directory (TLS, downloads_directory); if (!mkdir (downloads_directory, CONFIG_DIRECTORY_MODE)) { if (!disable_output) { printf ("[%s] created\n", downloads_directory); @@ -568,6 +570,7 @@ int change_user_group () { char *unix_socket; void args_parse (int argc, char **argv) { + TLS = tgl_state_alloc (); int opt = 0; while ((opt = getopt (argc, argv, "u:hk:vNl:fEwWCRdL:DU:G:qP:S:e:I" #ifdef HAVE_LIBCONFIG @@ -586,10 +589,10 @@ void args_parse (int argc, char **argv) { break; case 'k': //rsa_public_key_name = tstrdup (optarg); - tgl_set_rsa_key (optarg); + tgl_set_rsa_key (TLS, optarg); break; case 'v': - tgl_incr_verbosity (); + tgl_incr_verbosity (TLS); verbosity ++; break; case 'N': @@ -722,8 +725,8 @@ void sig_term_handler (int signum __attribute__ ((unused))) { if (write (1, "SIGTERM/SIGINT received\n", 25) < 0) { // Sad thing } - if (tgl_state.ev_base) { - event_base_loopbreak(tgl_state.ev_base); + if (TLS && TLS->ev_base) { + event_base_loopbreak (TLS->ev_base); } sigterm_cnt ++; } @@ -841,8 +844,8 @@ int main (int argc, char **argv) { running_for_first_time (); parse_config (); - tgl_set_rsa_key ("/etc/" PROG_NAME "/server.pub"); - tgl_set_rsa_key ("tg-server.pub"); + tgl_set_rsa_key (TLS, "/etc/" PROG_NAME "/server.pub"); + tgl_set_rsa_key (TLS, "tg-server.pub"); get_terminal_attributes (); diff --git a/mtproto-client.c b/mtproto-client.c index f3e079b..e97423e 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -58,12 +58,6 @@ #include "tools.h" #include "tree.h" #include "updates.h" -#ifdef EVENT_V2 -#include -#else -#include -#include "event-old.h" -#endif #if defined(__FreeBSD__) #define __builtin_bswap32(x) bswap32(x) @@ -99,9 +93,9 @@ static int total_packets_sent; static long long total_data_sent; -static int rpc_execute (struct connection *c, int op, int len); -static int rpc_becomes_ready (struct connection *c); -static int rpc_close (struct connection *c); +static int rpc_execute (struct tgl_state *TLS, struct connection *c, int op, int len); +static int rpc_becomes_ready (struct tgl_state *TLS, struct connection *c); +static int rpc_close (struct tgl_state *TLS, struct connection *c); static long long precise_time; @@ -122,7 +116,6 @@ static double get_utime (int clock_id) { #define MAX_RESPONSE_SIZE (1L << 24) -static char Response[MAX_RESPONSE_SIZE]; //static int Response_len; /* @@ -133,19 +126,19 @@ static char Response[MAX_RESPONSE_SIZE]; #define TG_SERVER_PUBKEY_FILENAME "tg-server.pub" //static char *rsa_public_key_name; // = TG_SERVER_PUBKEY_FILENAME; -static RSA *pubKey; +//static RSA *pubKey; static long long pk_fingerprint; -static int rsa_load_public_key (const char *public_key_name) { - pubKey = NULL; +static int rsa_load_public_key (struct tgl_state *TLS, const char *public_key_name) { + TLS->pubKey = NULL; FILE *f = fopen (public_key_name, "r"); if (f == NULL) { vlogprintf (E_WARNING, "Couldn't open public key file: %s\n", public_key_name); return -1; } - pubKey = PEM_read_RSAPublicKey (f, NULL, NULL, NULL); + TLS->pubKey = PEM_read_RSAPublicKey (f, NULL, NULL, NULL); fclose (f); - if (pubKey == NULL) { + if (TLS->pubKey == NULL) { vlogprintf (E_WARNING, "PEM_read_RSAPublicKey returns NULL.\n"); return -1; } @@ -181,8 +174,8 @@ static int encrypt_buffer[ENCRYPT_BUFFER_INTS]; #define DECRYPT_BUFFER_INTS 16384 static int decrypt_buffer[ENCRYPT_BUFFER_INTS]; -static int encrypt_packet_buffer (void) { - return tgl_pad_rsa_encrypt ((char *) packet_buffer, (packet_ptr - packet_buffer) * 4, (char *) encrypt_buffer, ENCRYPT_BUFFER_INTS * 4, pubKey->n, pubKey->e); +static int encrypt_packet_buffer (struct tgl_state *TLS) { + return tgl_pad_rsa_encrypt (TLS, (char *) packet_buffer, (packet_ptr - packet_buffer) * 4, (char *) encrypt_buffer, ENCRYPT_BUFFER_INTS * 4, ((RSA *)TLS->pubKey)->n, ((RSA *)TLS->pubKey)->e); } static int encrypt_packet_buffer_aes_unauth (const char server_nonce[16], const char hidden_client_nonce[32]) { @@ -191,10 +184,10 @@ static int encrypt_packet_buffer_aes_unauth (const char server_nonce[16], const } -static int rpc_send_packet (struct connection *c) { +static int rpc_send_packet (struct tgl_state *TLS, struct connection *c) { int len = (packet_ptr - packet_buffer) * 4; //c->out_packet_num ++; - tgl_state.net_methods->incr_out_packet_num (c); + TLS->net_methods->incr_out_packet_num (c); long long next_msg_id = (long long) ((1LL << 32) * get_utime (CLOCK_REALTIME)) & -4; if (next_msg_id <= unenc_msg_header.out_msg_id) { unenc_msg_header.out_msg_id += 4; @@ -208,41 +201,41 @@ static int rpc_send_packet (struct connection *c) { total_len >>= 2; vlogprintf (E_DEBUG, "writing packet: total_len = %d, len = %d\n", total_len, len); if (total_len < 0x7f) { - assert (tgl_state.net_methods->write_out (c, &total_len, 1) == 1); + assert (TLS->net_methods->write_out (c, &total_len, 1) == 1); } else { total_len = (total_len << 8) | 0x7f; - assert (tgl_state.net_methods->write_out (c, &total_len, 4) == 4); + assert (TLS->net_methods->write_out (c, &total_len, 4) == 4); } - tgl_state.net_methods->write_out (c, &unenc_msg_header, 20); - tgl_state.net_methods->write_out (c, packet_buffer, len); - tgl_state.net_methods->flush_out (c); + TLS->net_methods->write_out (c, &unenc_msg_header, 20); + TLS->net_methods->write_out (c, packet_buffer, len); + TLS->net_methods->flush_out (c); total_packets_sent ++; total_data_sent += total_len; return 1; } -static int rpc_send_message (struct connection *c, void *data, int len) { +static int rpc_send_message (struct tgl_state *TLS, struct connection *c, void *data, int len) { assert (len > 0 && !(len & 0xfc000003)); int total_len = len >> 2; if (total_len < 0x7f) { - assert (tgl_state.net_methods->write_out (c, &total_len, 1) == 1); + assert (TLS->net_methods->write_out (c, &total_len, 1) == 1); } else { total_len = (total_len << 8) | 0x7f; - assert (tgl_state.net_methods->write_out (c, &total_len, 4) == 4); + assert (TLS->net_methods->write_out (c, &total_len, 4) == 4); } - tgl_state.net_methods->incr_out_packet_num (c); - assert (tgl_state.net_methods->write_out (c, data, len) == len); - tgl_state.net_methods->flush_out (c); + TLS->net_methods->incr_out_packet_num (c); + assert (TLS->net_methods->write_out (c, data, len) == len); + TLS->net_methods->flush_out (c); total_packets_sent ++; total_data_sent += total_len; return 1; } -static int send_req_pq_packet (struct connection *c) { - struct tgl_dc *D = tgl_state.net_methods->get_dc (c); +static int send_req_pq_packet (struct tgl_state *TLS, struct connection *c) { + struct tgl_dc *D = TLS->net_methods->get_dc (c); assert (D->state == st_init); tglt_secure_random (D->nonce, 16); @@ -250,14 +243,14 @@ static int send_req_pq_packet (struct connection *c) { clear_packet (); out_int (CODE_req_pq); out_ints ((int *)D->nonce, 4); - rpc_send_packet (c); + rpc_send_packet (TLS, c); D->state = st_reqpq_sent; return 1; } -static int send_req_pq_temp_packet (struct connection *c) { - struct tgl_dc *D = tgl_state.net_methods->get_dc (c); +static int send_req_pq_temp_packet (struct tgl_state *TLS, struct connection *c) { + struct tgl_dc *D = TLS->net_methods->get_dc (c); assert (D->state == st_authorized); tglt_secure_random (D->nonce, 16); @@ -265,7 +258,7 @@ static int send_req_pq_temp_packet (struct connection *c) { clear_packet (); out_int (CODE_req_pq); out_ints ((int *)D->nonce, 4); - rpc_send_packet (c); + rpc_send_packet (TLS, c); D->state = st_reqpq_sent_temp; return 1; @@ -278,8 +271,8 @@ static unsigned long long gcd (unsigned long long a, unsigned long long b) { //typedef unsigned int uint128_t __attribute__ ((mode(TI))); -static int process_respq_answer (struct connection *c, char *packet, int len, int temp_key) { - struct tgl_dc *D = tgl_state.net_methods->get_dc (c); +static int process_respq_answer (struct tgl_state *TLS, struct connection *c, char *packet, int len, int temp_key) { + struct tgl_dc *D = TLS->net_methods->get_dc (c); unsigned long long what; unsigned p1, p2; int i; @@ -410,11 +403,11 @@ static int process_respq_answer (struct connection *c, char *packet, int len, in tglt_secure_random (D->new_nonce, 32); out_ints ((int *) D->new_nonce, 8); if (temp_key) { - out_int (tgl_state.temp_key_expire_time); + out_int (TLS->temp_key_expire_time); } sha1 ((unsigned char *) (packet_buffer + 5), (packet_ptr - packet_buffer - 5) * 4, (unsigned char *) packet_buffer); - int l = encrypt_packet_buffer (); + int l = encrypt_packet_buffer (TLS); clear_packet (); out_int (CODE_req_DH_params); @@ -452,16 +445,16 @@ static int process_respq_answer (struct connection *c, char *packet, int len, in D->state = temp_key ? st_reqdh_sent_temp : st_reqdh_sent; - return rpc_send_packet (c); + return rpc_send_packet (TLS, c); } -static int check_prime (BIGNUM *p) { - int r = BN_is_prime (p, BN_prime_checks, 0, tgl_state.BN_ctx, 0); +static int check_prime (struct tgl_state *TLS, BIGNUM *p) { + int r = BN_is_prime (p, BN_prime_checks, 0, TLS->BN_ctx, 0); ensure (r >= 0); return r; } -int tglmp_check_DH_params (BIGNUM *p, int g) { +int tglmp_check_DH_params (struct tgl_state *TLS, BIGNUM *p, int g) { if (g < 2 || g > 7) { return -1; } if (BN_num_bits (p) != 2048) { return -1; } BIGNUM t; @@ -470,7 +463,7 @@ int tglmp_check_DH_params (BIGNUM *p, int g) { BN_init (&dh_g); ensure (BN_set_word (&dh_g, 4 * g)); - ensure (BN_mod (&t, p, &dh_g, tgl_state.BN_ctx)); + ensure (BN_mod (&t, p, &dh_g, TLS->BN_ctx)); int x = BN_get_word (&t); assert (x >= 0 && x < 4 * g); @@ -496,19 +489,19 @@ int tglmp_check_DH_params (BIGNUM *p, int g) { break; } - if (!check_prime (p)) { return -1; } + if (!check_prime (TLS, p)) { return -1; } BIGNUM b; BN_init (&b); ensure (BN_set_word (&b, 2)); - ensure (BN_div (&t, 0, p, &b, tgl_state.BN_ctx)); - if (!check_prime (&t)) { return -1; } + ensure (BN_div (&t, 0, p, &b, TLS->BN_ctx)); + if (!check_prime (TLS, &t)) { return -1; } BN_free (&b); BN_free (&t); return 0; } -int tglmp_check_g (unsigned char p[256], BIGNUM *g) { +int tglmp_check_g (struct tgl_state *TLS, unsigned char p[256], BIGNUM *g) { static unsigned char s[256]; memset (s, 0, 256); assert (BN_num_bytes (g) <= 256); @@ -544,16 +537,16 @@ int tglmp_check_g (unsigned char p[256], BIGNUM *g) { return 0; } -int tglmp_check_g_bn (BIGNUM *p, BIGNUM *g) { +int tglmp_check_g_bn (struct tgl_state *TLS, BIGNUM *p, BIGNUM *g) { static unsigned char s[256]; memset (s, 0, 256); assert (BN_num_bytes (p) == 256); BN_bn2bin (p, s); - return tglmp_check_g (s, g); + return tglmp_check_g (TLS, s, g); } -static int process_dh_answer (struct connection *c, char *packet, int len, int temp_key) { - struct tgl_dc *D = tgl_state.net_methods->get_dc (c); +static int process_dh_answer (struct tgl_state *TLS, struct connection *c, char *packet, int len, int temp_key) { + struct tgl_dc *D = TLS->net_methods->get_dc (c); vlogprintf (E_DEBUG, "process_dh_answer(), len=%d\n", len); //if (len < 116) { // vlogprintf (E_ERROR, "%u * %u = %llu", p1, p2, what); @@ -589,11 +582,11 @@ static int process_dh_answer (struct connection *c, char *packet, int len, int t BN_init (&g_a); assert (fetch_bignum (&dh_prime) > 0); assert (fetch_bignum (&g_a) > 0); - assert (tglmp_check_g_bn (&dh_prime, &g_a) >= 0); + assert (tglmp_check_g_bn (TLS, &dh_prime, &g_a) >= 0); int server_time = *in_ptr++; assert (in_ptr <= in_end); - assert (tglmp_check_DH_params (&dh_prime, g) >= 0); + assert (tglmp_check_DH_params (TLS, &dh_prime, g) >= 0); static char sha1_buffer[20]; sha1 ((unsigned char *) decrypt_buffer + 20, (in_ptr - decrypt_buffer - 5) * 4, (unsigned char *) sha1_buffer); @@ -621,12 +614,12 @@ static int process_dh_answer (struct connection *c, char *packet, int len, int t BIGNUM *y = BN_new (); ensure_ptr (y); - ensure (BN_mod_exp (y, &dh_g, dh_power, &dh_prime, tgl_state.BN_ctx)); + ensure (BN_mod_exp (y, &dh_g, dh_power, &dh_prime, TLS->BN_ctx)); out_bignum (y); BN_free (y); BN_init (&auth_key_num); - ensure (BN_mod_exp (&auth_key_num, &g_a, dh_power, &dh_prime, tgl_state.BN_ctx)); + ensure (BN_mod_exp (&auth_key_num, &g_a, dh_power, &dh_prime, TLS->BN_ctx)); l = BN_num_bytes (&auth_key_num); assert (l >= 250 && l <= 256); assert (BN_bn2bin (&auth_key_num, (unsigned char *)(temp_key ? D->temp_auth_key : D->auth_key))); @@ -658,19 +651,19 @@ static int process_dh_answer (struct connection *c, char *packet, int len, int t D->state = temp_key ? st_client_dh_sent_temp : st_client_dh_sent;; - return rpc_send_packet (c); + return rpc_send_packet (TLS, c); } -static void create_temp_auth_key (struct connection *c) { - send_req_pq_temp_packet (c); +static void create_temp_auth_key (struct tgl_state *TLS, struct connection *c) { + send_req_pq_temp_packet (TLS, c); } -int tglmp_encrypt_inner_temp (struct connection *c, int *msg, int msg_ints, int useful, void *data, long long msg_id); -static long long generate_next_msg_id (struct tgl_dc *DC); +int tglmp_encrypt_inner_temp (struct tgl_state *TLS, struct connection *c, int *msg, int msg_ints, int useful, void *data, long long msg_id); +static long long generate_next_msg_id (struct tgl_state *TLS, struct tgl_dc *DC); static long long msg_id_override; -static void mpc_on_get_config (void *extra, int success); -static int process_auth_complete (struct connection *c UU, char *packet, int len, int temp_key) { - struct tgl_dc *D = tgl_state.net_methods->get_dc (c); +static void mpc_on_get_config (struct tgl_state *TLS, void *extra, int success); +static int process_auth_complete (struct tgl_state *TLS, struct connection *c UU, char *packet, int len, int temp_key) { + struct tgl_dc *D = TLS->net_methods->get_dc (c); vlogprintf (E_DEBUG - 1, "process_dh_answer(), len=%d\n", len); long long packet_auth_key_id = *(long long *)packet; @@ -692,7 +685,7 @@ static int process_auth_complete (struct connection *c UU, char *packet, int len //GET_DC(c)->auth_key_id = *(long long *)(sha1_buffer + 12); if (!temp_key) { - bl_do_set_auth_key_id (D->id, (unsigned char *)D->auth_key); + bl_do_set_auth_key_id (TLS, D->id, (unsigned char *)D->auth_key); sha1 ((unsigned char *)D->auth_key, 256, sha1_buffer); } else { sha1 ((unsigned char *)D->temp_auth_key, 256, sha1_buffer); @@ -715,7 +708,7 @@ static int process_auth_complete (struct connection *c UU, char *packet, int len if (temp_key) { //D->flags |= 2; - long long msg_id = generate_next_msg_id (D); + long long msg_id = generate_next_msg_id (TLS, D); clear_packet (); out_int (CODE_bind_auth_key_inner); long long rand; @@ -724,29 +717,29 @@ static int process_auth_complete (struct connection *c UU, char *packet, int len out_long (D->temp_auth_key_id); out_long (D->auth_key_id); - struct tgl_session *S = tgl_state.net_methods->get_session (c); + struct tgl_session *S = TLS->net_methods->get_session (c); if (!S->session_id) { tglt_secure_random (&S->session_id, 8); } out_long (S->session_id); - int expires = time (0) + D->server_time_delta + tgl_state.temp_key_expire_time; + int expires = time (0) + D->server_time_delta + TLS->temp_key_expire_time; out_int (expires); static int data[1000]; - int len = tglmp_encrypt_inner_temp (c, packet_buffer, packet_ptr - packet_buffer, 0, data, msg_id); + int len = tglmp_encrypt_inner_temp (TLS, c, packet_buffer, packet_ptr - packet_buffer, 0, data, msg_id); msg_id_override = msg_id; - tgl_do_send_bind_temp_key (D, rand, expires, (void *)data, len, msg_id); + tgl_do_send_bind_temp_key (TLS, D, rand, expires, (void *)data, len, msg_id); msg_id_override = 0; } else { D->flags |= 1; - if (tgl_state.enable_pfs) { - create_temp_auth_key (c); + if (TLS->enable_pfs) { + create_temp_auth_key (TLS, c); } else { D->temp_auth_key_id = D->auth_key_id; memcpy (D->temp_auth_key, D->auth_key, 256); D->flags |= 2; if (!(D->flags & 4)) { - tgl_do_help_get_config_dc (D, mpc_on_get_config, D); + tgl_do_help_get_config_dc (TLS, D, mpc_on_get_config, D); } } } @@ -773,7 +766,7 @@ static double get_server_time (struct tgl_dc *DC) { return get_utime (CLOCK_MONOTONIC) + DC->server_time_udelta; } -static long long generate_next_msg_id (struct tgl_dc *DC) { +static long long generate_next_msg_id (struct tgl_state *TLS, struct tgl_dc *DC) { long long next_id = (long long) (get_server_time (DC) * (1LL << 32)) & -4; if (next_id <= client_last_msg_id) { next_id = client_last_msg_id += 4; @@ -783,7 +776,7 @@ static long long generate_next_msg_id (struct tgl_dc *DC) { return next_id; } -static void init_enc_msg (struct tgl_session *S, int useful) { +static void init_enc_msg (struct tgl_state *TLS, struct tgl_session *S, int useful) { struct tgl_dc *DC = S->dc; assert (DC->state == st_authorized); //assert (DC->flags & 2); @@ -797,7 +790,7 @@ static void init_enc_msg (struct tgl_session *S, int useful) { } enc_msg.session_id = S->session_id; //enc_msg.auth_key_id2 = auth_key_id; - enc_msg.msg_id = msg_id_override ? msg_id_override : generate_next_msg_id (DC); + enc_msg.msg_id = msg_id_override ? msg_id_override : generate_next_msg_id (TLS, DC); //enc_msg.msg_id -= 0x10000000LL * (lrand48 () & 15); //kprintf ("message id %016llx\n", enc_msg.msg_id); enc_msg.seq_no = S->seq_no; @@ -816,7 +809,7 @@ static void init_enc_msg_inner_temp (struct tgl_dc *DC, long long msg_id) { }; -static int aes_encrypt_message (char *key, struct encrypted_message *enc) { +static int aes_encrypt_message (struct tgl_state *TLS, char *key, struct encrypted_message *enc) { unsigned char sha1_buffer[20]; const int MINSZ = offsetof (struct encrypted_message, message); const int UNENCSZ = offsetof (struct encrypted_message, server_salt); @@ -831,12 +824,12 @@ static int aes_encrypt_message (char *key, struct encrypted_message *enc) { return tgl_pad_aes_encrypt ((char *) &enc->server_salt, enc_len, (char *) &enc->server_salt, MAX_MESSAGE_INTS * 4 + (MINSZ - UNENCSZ)); } -long long tglmp_encrypt_send_message (struct connection *c, int *msg, int msg_ints, int flags) { - struct tgl_dc *DC = tgl_state.net_methods->get_dc (c); +long long tglmp_encrypt_send_message (struct tgl_state *TLS, struct connection *c, int *msg, int msg_ints, int flags) { + struct tgl_dc *DC = TLS->net_methods->get_dc (c); if (!(DC->flags & 4) && !(flags & 2)) { - return generate_next_msg_id (DC); + return generate_next_msg_id (TLS, DC); } - struct tgl_session *S = tgl_state.net_methods->get_session (c); + struct tgl_session *S = TLS->net_methods->get_session (c); assert (S); const int UNENCSZ = offsetof (struct encrypted_message, server_salt); @@ -851,22 +844,22 @@ long long tglmp_encrypt_send_message (struct connection *c, int *msg, int msg_in return -1; } } - init_enc_msg (S, flags & 1); + init_enc_msg (TLS, S, flags & 1); //hexdump ((char *)msg, (char *)msg + (msg_ints * 4)); - int l = aes_encrypt_message (DC->temp_auth_key, &enc_msg); + int l = aes_encrypt_message (TLS, DC->temp_auth_key, &enc_msg); //hexdump ((char *)&enc_msg, (char *)&enc_msg + l + 24); assert (l > 0); vlogprintf (E_DEBUG, "Sending message to DC%d: %s:%d with key_id=%lld\n", DC->id, DC->ip, DC->port, enc_msg.auth_key_id); - rpc_send_message (c, &enc_msg, l + UNENCSZ); + rpc_send_message (TLS, c, &enc_msg, l + UNENCSZ); return client_last_msg_id; } -int tglmp_encrypt_inner_temp (struct connection *c, int *msg, int msg_ints, int useful, void *data, long long msg_id) { - struct tgl_dc *DC = tgl_state.net_methods->get_dc (c); - struct tgl_session *S = tgl_state.net_methods->get_session (c); +int tglmp_encrypt_inner_temp (struct tgl_state *TLS, struct connection *c, int *msg, int msg_ints, int useful, void *data, long long msg_id) { + struct tgl_dc *DC = TLS->net_methods->get_dc (c); + struct tgl_session *S = TLS->net_methods->get_session (c); assert (S); const int UNENCSZ = offsetof (struct encrypted_message, server_salt); @@ -878,7 +871,7 @@ int tglmp_encrypt_inner_temp (struct connection *c, int *msg, int msg_ints, int init_enc_msg_inner_temp (DC, msg_id); - int l = aes_encrypt_message (DC->auth_key, &enc_msg); + int l = aes_encrypt_message (TLS, DC->auth_key, &enc_msg); assert (l > 0); //rpc_send_message (c, &enc_msg, l + UNENCSZ); memcpy (data, &enc_msg, l + UNENCSZ); @@ -888,7 +881,7 @@ int tglmp_encrypt_inner_temp (struct connection *c, int *msg, int msg_ints, int static int good_messages; -static void rpc_execute_answer (struct connection *c, long long msg_id UU); +static void rpc_execute_answer (struct tgl_state *TLS, struct connection *c, long long msg_id UU); //int unread_messages; //int pts; @@ -896,7 +889,7 @@ static void rpc_execute_answer (struct connection *c, long long msg_id UU); //int last_date; //int seq; -static void work_container (struct connection *c, long long msg_id UU) { +static void work_container (struct tgl_state *TLS, struct connection *c, long long msg_id UU) { vlogprintf (E_DEBUG, "work_container: msg_id = %lld\n", msg_id); assert (fetch_int () == CODE_msg_container); int n = fetch_int (); @@ -906,30 +899,30 @@ static void work_container (struct connection *c, long long msg_id UU) { //int seqno = fetch_int (); fetch_int (); // seq_no if (id & 1) { - tgln_insert_msg_id (tgl_state.net_methods->get_session (c), id); + tgln_insert_msg_id (TLS, TLS->net_methods->get_session (c), id); } int bytes = fetch_int (); int *t = in_end; in_end = in_ptr + (bytes / 4); - rpc_execute_answer (c, id); + rpc_execute_answer (TLS, c, id); assert (in_ptr == in_end); in_end = t; } } -static void work_new_session_created (struct connection *c, long long msg_id UU) { +static void work_new_session_created (struct tgl_state *TLS, struct connection *c, long long msg_id UU) { vlogprintf (E_DEBUG, "work_new_session_created: msg_id = %lld\n", msg_id); assert (fetch_int () == (int)CODE_new_session_created); fetch_long (); // first message id //DC->session_id = fetch_long (); fetch_long (); // unique_id - tgl_state.net_methods->get_dc (c)->server_salt = fetch_long (); - if (tgl_state.started && !(tgl_state.locks & TGL_LOCK_DIFF)) { - tgl_do_get_difference (0, 0, 0); + TLS->net_methods->get_dc (c)->server_salt = fetch_long (); + if (TLS->started && !(TLS->locks & TGL_LOCK_DIFF)) { + tgl_do_get_difference (TLS, 0, 0, 0); } } -static void work_msgs_ack (struct connection *c UU, long long msg_id UU) { +static void work_msgs_ack (struct tgl_state *TLS, struct connection *c UU, long long msg_id UU) { vlogprintf (E_DEBUG, "work_msgs_ack: msg_id = %lld\n", msg_id); assert (fetch_int () == CODE_msgs_ack); assert (fetch_int () == CODE_vector); @@ -938,24 +931,24 @@ static void work_msgs_ack (struct connection *c UU, long long msg_id UU) { for (i = 0; i < n; i++) { long long id = fetch_long (); vlogprintf (E_DEBUG + 1, "ack for %lld\n", id); - tglq_query_ack (id); + tglq_query_ack (TLS, id); } } -static void work_rpc_result (struct connection *c UU, long long msg_id UU) { +static void work_rpc_result (struct tgl_state *TLS, struct connection *c UU, long long msg_id UU) { vlogprintf (E_DEBUG, "work_rpc_result: msg_id = %lld\n", msg_id); assert (fetch_int () == (int)CODE_rpc_result); long long id = fetch_long (); int op = prefetch_int (); if (op == CODE_rpc_error) { - tglq_query_error (id); + tglq_query_error (TLS, id); } else { - tglq_query_result (id); + tglq_query_result (TLS, id); } } #define MAX_PACKED_SIZE (1 << 24) -static void work_packed (struct connection *c, long long msg_id) { +static void work_packed (struct tgl_state *TLS, struct connection *c, long long msg_id) { assert (fetch_int () == CODE_gzip_packed); static int in_gzip; static int buf[MAX_PACKED_SIZE >> 2]; @@ -971,29 +964,29 @@ static void work_packed (struct connection *c, long long msg_id) { //assert (total_out % 4 == 0); in_ptr = buf; in_end = in_ptr + total_out / 4; - rpc_execute_answer (c, msg_id); + rpc_execute_answer (TLS, c, msg_id); in_ptr = end; in_end = eend; in_gzip = 0; } -static void work_bad_server_salt (struct connection *c UU, long long msg_id UU) { +static void work_bad_server_salt (struct tgl_state *TLS, struct connection *c UU, long long msg_id UU) { assert (fetch_int () == (int)CODE_bad_server_salt); long long id = fetch_long (); - tglq_query_restart (id); + tglq_query_restart (TLS, id); fetch_int (); // seq_no fetch_int (); // error_code long long new_server_salt = fetch_long (); - tgl_state.net_methods->get_dc (c)->server_salt = new_server_salt; + TLS->net_methods->get_dc (c)->server_salt = new_server_salt; } -static void work_pong (struct connection *c UU, long long msg_id UU) { +static void work_pong (struct tgl_state *TLS, struct connection *c UU, long long msg_id UU) { assert (fetch_int () == CODE_pong); fetch_long (); // msg_id fetch_long (); // ping_id } -static void work_detailed_info (struct connection *c UU, long long msg_id UU) { +static void work_detailed_info (struct tgl_state *TLS, struct connection *c UU, long long msg_id UU) { assert (fetch_int () == CODE_msg_detailed_info); fetch_long (); // msg_id fetch_long (); // answer_msg_id @@ -1001,14 +994,14 @@ static void work_detailed_info (struct connection *c UU, long long msg_id UU) { fetch_int (); // status } -static void work_new_detailed_info (struct connection *c UU, long long msg_id UU) { +static void work_new_detailed_info (struct tgl_state *TLS, struct connection *c UU, long long msg_id UU) { assert (fetch_int () == (int)CODE_msg_new_detailed_info); fetch_long (); // answer_msg_id fetch_int (); // bytes fetch_int (); // status } -static void work_bad_msg_notification (struct connection *c UU, long long msg_id UU) { +static void work_bad_msg_notification (struct tgl_state *TLS, struct connection *c UU, long long msg_id UU) { assert (fetch_int () == (int)CODE_bad_msg_notification); long long m1 = fetch_long (); int s = fetch_int (); @@ -1016,65 +1009,65 @@ static void work_bad_msg_notification (struct connection *c UU, long long msg_id vlogprintf (E_NOTICE, "bad_msg_notification: msg_id = %lld, seq = %d, error = %d\n", m1, s, e); } -static void rpc_execute_answer (struct connection *c, long long msg_id UU) { +static void rpc_execute_answer (struct tgl_state *TLS, struct connection *c, long long msg_id UU) { int op = prefetch_int (); switch (op) { case CODE_msg_container: - work_container (c, msg_id); + work_container (TLS, c, msg_id); return; case CODE_new_session_created: - work_new_session_created (c, msg_id); + work_new_session_created (TLS, c, msg_id); return; case CODE_msgs_ack: - work_msgs_ack (c, msg_id); + work_msgs_ack (TLS, c, msg_id); return; case CODE_rpc_result: - work_rpc_result (c, msg_id); + work_rpc_result (TLS, c, msg_id); return; case CODE_update_short: - tglu_work_update_short (c, msg_id); + tglu_work_update_short (TLS, c, msg_id); return; case CODE_updates: - tglu_work_updates (c, msg_id); + tglu_work_updates (TLS, c, msg_id); return; case CODE_update_short_message: - tglu_work_update_short_message (c, msg_id); + tglu_work_update_short_message (TLS, c, msg_id); return; case CODE_update_short_chat_message: - tglu_work_update_short_chat_message (c, msg_id); + tglu_work_update_short_chat_message (TLS, c, msg_id); return; case CODE_gzip_packed: - work_packed (c, msg_id); + work_packed (TLS, c, msg_id); return; case CODE_bad_server_salt: - work_bad_server_salt (c, msg_id); + work_bad_server_salt (TLS, c, msg_id); return; case CODE_pong: - work_pong (c, msg_id); + work_pong (TLS, c, msg_id); return; case CODE_msg_detailed_info: - work_detailed_info (c, msg_id); + work_detailed_info (TLS, c, msg_id); return; case CODE_msg_new_detailed_info: - work_new_detailed_info (c, msg_id); + work_new_detailed_info (TLS, c, msg_id); return; case CODE_updates_too_long: - tglu_work_updates_to_long (c, msg_id); + tglu_work_updates_to_long (TLS, c, msg_id); return; case CODE_bad_msg_notification: - work_bad_msg_notification (c, msg_id); + work_bad_msg_notification (TLS, c, msg_id); return; } vlogprintf (E_WARNING, "Unknown message: %08x\n", op); in_ptr = in_end; // Will not fail due to assertion in_ptr == in_end } -static int process_rpc_message (struct connection *c UU, struct encrypted_message *enc, int len) { +static int process_rpc_message (struct tgl_state *TLS, struct connection *c UU, struct encrypted_message *enc, int len) { const int MINSZ = offsetof (struct encrypted_message, message); const int UNENCSZ = offsetof (struct encrypted_message, server_salt); vlogprintf (E_DEBUG, "process_rpc_message(), len=%d\n", len); assert (len >= MINSZ && (len & 15) == (UNENCSZ & 15)); - struct tgl_dc *DC = tgl_state.net_methods->get_dc (c); + struct tgl_dc *DC = TLS->net_methods->get_dc (c); if (enc->auth_key_id != DC->temp_auth_key_id && enc->auth_key_id != DC->auth_key_id) { vlogprintf (E_WARNING, "received msg from dc %d with auth_key_id %lld (perm_auth_key_id %lld temp_auth_key_id %lld). Dropping\n", DC->id, enc->auth_key_id, DC->auth_key_id, DC->temp_auth_key_id); @@ -1136,7 +1129,7 @@ static int process_rpc_message (struct connection *c UU, struct encrypted_messag int p = 0; int i; //static int buf[10000]; - //assert (tgl_state.net_methods->read_in_lookup (c, buf, len) == len); + //assert (TLS->net_methods->read_in_lookup (c, buf, len) == len); for (i = 0; i < in_end - in_ptr; i++) { p += sprintf (s + p, "%08x ", *(int *)(in_ptr + i)); @@ -1144,19 +1137,19 @@ static int process_rpc_message (struct connection *c UU, struct encrypted_messag vlogprintf (E_DEBUG, "%s\n", s); }*/ - struct tgl_session *S = tgl_state.net_methods->get_session (c); + struct tgl_session *S = TLS->net_methods->get_session (c); if (enc->msg_id & 1) { - tgln_insert_msg_id (S, enc->msg_id); + tgln_insert_msg_id (TLS, S, enc->msg_id); } assert (S->session_id == enc->session_id); - rpc_execute_answer (c, enc->msg_id); + rpc_execute_answer (TLS, c, enc->msg_id); assert (in_ptr == in_end); return 0; } -static int rpc_execute (struct connection *c, int op, int len) { - struct tgl_dc *D = tgl_state.net_methods->get_dc (c); +static int rpc_execute (struct tgl_state *TLS, struct connection *c, int op, int len) { + struct tgl_dc *D = TLS->net_methods->get_dc (c); vlogprintf (E_DEBUG, "outbound rpc connection from dc #%d (%s:%d) : received rpc answer %d with %d content bytes\n", D->id, D->ip, D->port, op, len); /*{ @@ -1165,7 +1158,7 @@ static int rpc_execute (struct connection *c, int op, int len) { int p = 0; int i; static int buf[10000]; - assert (tgl_state.net_methods->read_in_lookup (c, buf, len) == len); + assert (TLS->net_methods->read_in_lookup (c, buf, len) == len); for (i = 0; i < len / 4; i++) { p += sprintf (s + p, "%08x ", *(int *)(buf + i)); @@ -1173,7 +1166,7 @@ static int rpc_execute (struct connection *c, int op, int len) { vlogprintf (E_DEBUG, "%s\n", s); }*/ /* if (op < 0) { - assert (tgl_state.net_methods->read_in (c, Response, Response_len) == Response_len); + assert (TLS->net_methods->read_in (c, Response, Response_len) == Response_len); return 0; }*/ @@ -1184,8 +1177,9 @@ static int rpc_execute (struct connection *c, int op, int len) { int Response_len = len; + static char Response[MAX_RESPONSE_SIZE]; vlogprintf (E_DEBUG, "Response_len = %d\n", Response_len); - assert (tgl_state.net_methods->read_in (c, Response, Response_len) == Response_len); + assert (TLS->net_methods->read_in (c, Response, Response_len) == Response_len); Response[Response_len] = 0; #if !defined(__MACH__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined (__CYGWIN__) @@ -1195,28 +1189,28 @@ static int rpc_execute (struct connection *c, int op, int len) { //if (D->flags & 1) { o = st_authorized;} switch (o) { case st_reqpq_sent: - process_respq_answer (c, Response/* + 8*/, Response_len/* - 12*/, 0); + process_respq_answer (TLS, c, Response/* + 8*/, Response_len/* - 12*/, 0); return 0; case st_reqdh_sent: - process_dh_answer (c, Response/* + 8*/, Response_len/* - 12*/, 0); + process_dh_answer (TLS, c, Response/* + 8*/, Response_len/* - 12*/, 0); return 0; case st_client_dh_sent: - process_auth_complete (c, Response/* + 8*/, Response_len/* - 12*/, 0); + process_auth_complete (TLS, c, Response/* + 8*/, Response_len/* - 12*/, 0); return 0; case st_reqpq_sent_temp: - process_respq_answer (c, Response/* + 8*/, Response_len/* - 12*/, 1); + process_respq_answer (TLS, c, Response/* + 8*/, Response_len/* - 12*/, 1); return 0; case st_reqdh_sent_temp: - process_dh_answer (c, Response/* + 8*/, Response_len/* - 12*/, 1); + process_dh_answer (TLS, c, Response/* + 8*/, Response_len/* - 12*/, 1); return 0; case st_client_dh_sent_temp: - process_auth_complete (c, Response/* + 8*/, Response_len/* - 12*/, 1); + process_auth_complete (TLS, c, Response/* + 8*/, Response_len/* - 12*/, 1); return 0; case st_authorized: if (op < 0 && op >= -999) { vlogprintf (E_WARNING, "Server error %d\n", op); } else { - process_rpc_message (c, (void *)(Response/* + 8*/), Response_len/* - 12*/); + process_rpc_message (TLS, c, (void *)(Response/* + 8*/), Response_len/* - 12*/); } return 0; default: @@ -1228,41 +1222,41 @@ static int rpc_execute (struct connection *c, int op, int len) { } -static int tc_close (struct connection *c, int who) { - vlogprintf (E_DEBUG, "outbound rpc connection from dc #%d : closing by %d\n", tgl_state.net_methods->get_dc(c)->id, who); +static int tc_close (struct tgl_state *TLS, struct connection *c, int who) { + vlogprintf (E_DEBUG, "outbound rpc connection from dc #%d : closing by %d\n", TLS->net_methods->get_dc(c)->id, who); return 0; } -static void mpc_on_get_config (void *extra, int success) { +static void mpc_on_get_config (struct tgl_state *TLS, void *extra, int success) { assert (success); struct tgl_dc *D = extra; D->flags |= 4; } -static int tc_becomes_ready (struct connection *c) { - vlogprintf (E_DEBUG, "outbound rpc connection from dc #%d becomed ready\n", tgl_state.net_methods->get_dc(c)->id); +static int tc_becomes_ready (struct tgl_state *TLS, struct connection *c) { + vlogprintf (E_DEBUG, "outbound rpc connection from dc #%d becomed ready\n", TLS->net_methods->get_dc(c)->id); //char byte = 0xef; - //assert (tgl_state.net_methods->write_out (c, &byte, 1) == 1); - //tgl_state.net_methods->flush_out (c); + //assert (TLS->net_methods->write_out (c, &byte, 1) == 1); + //TLS->net_methods->flush_out (c); - struct tgl_dc *D = tgl_state.net_methods->get_dc (c); + struct tgl_dc *D = TLS->net_methods->get_dc (c); if (D->flags & 1) { D->state = st_authorized; } int o = D->state; - if (o == st_authorized && !tgl_state.enable_pfs) { + if (o == st_authorized && !TLS->enable_pfs) { D->temp_auth_key_id = D->auth_key_id; memcpy (D->temp_auth_key, D->auth_key, 256); D->flags |= 2; } switch (o) { case st_init: - send_req_pq_packet (c); + send_req_pq_packet (TLS, c); break; case st_authorized: if (!(D->flags & 2)) { assert (!D->temp_auth_key_id); - create_temp_auth_key (c); + create_temp_auth_key (TLS, c); } else if (!(D->flags & 4)) { - tgl_do_help_get_config_dc (D, mpc_on_get_config, D); + tgl_do_help_get_config_dc (TLS, D, mpc_on_get_config, D); } break; default: @@ -1272,25 +1266,25 @@ static int tc_becomes_ready (struct connection *c) { return 0; } -static int rpc_becomes_ready (struct connection *c) { - return tc_becomes_ready (c); +static int rpc_becomes_ready (struct tgl_state *TLS, struct connection *c) { + return tc_becomes_ready (TLS, c); } -static int rpc_close (struct connection *c) { - return tc_close (c, 0); +static int rpc_close (struct tgl_state *TLS, struct connection *c) { + return tc_close (TLS, c, 0); } #define RANDSEED_PASSWORD_FILENAME NULL #define RANDSEED_PASSWORD_LENGTH 0 -void tglmp_on_start (void) { - tgl_prng_seed (RANDSEED_PASSWORD_FILENAME, RANDSEED_PASSWORD_LENGTH); +void tglmp_on_start (struct tgl_state *TLS) { + tgl_prng_seed (TLS, RANDSEED_PASSWORD_FILENAME, RANDSEED_PASSWORD_LENGTH); int i; int ok = 0; - for (i = 0; i < tgl_state.rsa_key_num; i++) { - char *key = tgl_state.rsa_key_list[i]; - if (rsa_load_public_key (key) < 0) { + for (i = 0; i < TLS->rsa_key_num; i++) { + char *key = TLS->rsa_key_list[i]; + if (rsa_load_public_key (TLS, key) < 0) { vlogprintf (E_WARNING, "Can not load key %s\n", key); } else { ok = 1; @@ -1303,13 +1297,13 @@ void tglmp_on_start (void) { exit (1); } - pk_fingerprint = tgl_do_compute_rsa_key_fingerprint (pubKey); + pk_fingerprint = tgl_do_compute_rsa_key_fingerprint (TLS->pubKey); } -void tgl_dc_authorize (struct tgl_dc *DC) { +void tgl_dc_authorize (struct tgl_state *TLS, struct tgl_dc *DC) { //c_state = 0; if (!DC->sessions[0]) { - tglmp_dc_create_session (DC); + tglmp_dc_create_session (TLS, DC); } vlogprintf (E_DEBUG, "Starting authorization for DC #%d: %s:%d\n", DC->id, DC->ip, DC->port); //net_loop (0, auth_ok); @@ -1318,7 +1312,7 @@ void tgl_dc_authorize (struct tgl_dc *DC) { #define long_cmp(a,b) ((a) > (b) ? 1 : (a) == (b) ? 0 : -1) DEFINE_TREE(long,long long,long_cmp,0) -static int send_all_acks (struct tgl_session *S) { +static int send_all_acks (struct tgl_state *TLS, struct tgl_session *S) { clear_packet (); out_int (CODE_msgs_ack); out_int (CODE_vector); @@ -1328,19 +1322,18 @@ static int send_all_acks (struct tgl_session *S) { out_long (x); S->ack_tree = tree_delete_long (S->ack_tree, x); } - tglmp_encrypt_send_message (S->c, packet_buffer, packet_ptr - packet_buffer, 0); + tglmp_encrypt_send_message (TLS, S->c, packet_buffer, packet_ptr - packet_buffer, 0); return 0; } -static void send_all_acks_gateway (evutil_socket_t fd, short what, void *arg) { - send_all_acks (arg); +static void send_all_acks_gateway (struct tgl_state *TLS, void *arg) { + send_all_acks (TLS, arg); } -void tgln_insert_msg_id (struct tgl_session *S, long long id) { +void tgln_insert_msg_id (struct tgl_state *TLS, struct tgl_session *S, long long id) { if (!S->ack_tree) { - static struct timeval ptimeout = { ACK_TIMEOUT, 0}; - event_add (S->ev, &ptimeout); + TLS->timer_methods->insert (S->ev, ACK_TIMEOUT); } if (!tree_lookup_long (S->ack_tree, id)) { S->ack_tree = tree_insert_long (S->ack_tree, id, lrand48 ()); @@ -1350,27 +1343,26 @@ void tgln_insert_msg_id (struct tgl_session *S, long long id) { //extern struct tgl_dc *DC_list[]; -static void regen_temp_key_gw (evutil_socket_t fd, short what, void *arg) { - tglmp_regenerate_temp_auth_key (arg); +static void regen_temp_key_gw (struct tgl_state *TLS, void *arg) { + tglmp_regenerate_temp_auth_key (TLS, arg); } -struct tgl_dc *tglmp_alloc_dc (int id, char *ip, int port UU) { - //assert (!tgl_state.DC_list[id]); - if (!tgl_state.DC_list[id]) { +struct tgl_dc *tglmp_alloc_dc (struct tgl_state *TLS, int id, char *ip, int port UU) { + //assert (!TLS->DC_list[id]); + if (!TLS->DC_list[id]) { struct tgl_dc *DC = talloc0 (sizeof (*DC)); DC->id = id; DC->ip = ip; DC->port = port; - tgl_state.DC_list[id] = DC; - if (id > tgl_state.max_dc_num) { - tgl_state.max_dc_num = id; + TLS->DC_list[id] = DC; + if (id > TLS->max_dc_num) { + TLS->max_dc_num = id; } - DC->ev = evtimer_new (tgl_state.ev_base, regen_temp_key_gw, DC); - static struct timeval p; - event_add (DC->ev, &p); + DC->ev = TLS->timer_methods->alloc (TLS, regen_temp_key_gw, DC); + TLS->timer_methods->insert (DC->ev, 0); return DC; } else { - struct tgl_dc *DC = tgl_state.DC_list[id]; + struct tgl_dc *DC = TLS->DC_list[id]; tfree_str (DC->ip); DC->ip = tstrdup (ip); return DC; @@ -1383,49 +1375,49 @@ static struct mtproto_methods mtproto_methods = { .close = rpc_close }; -void tglmp_dc_create_session (struct tgl_dc *DC) { +void tglmp_dc_create_session (struct tgl_state *TLS, struct tgl_dc *DC) { struct tgl_session *S = talloc0 (sizeof (*S)); assert (RAND_pseudo_bytes ((unsigned char *) &S->session_id, 8) >= 0); S->dc = DC; - S->c = tgl_state.net_methods->create_connection (DC->ip, DC->port, S, DC, &mtproto_methods); + S->c = TLS->net_methods->create_connection (TLS, DC->ip, DC->port, S, DC, &mtproto_methods); if (!S->c) { vlogprintf (E_DEBUG, "Can not create connection to DC. Is network down?\n"); exit (1); } - S->ev = evtimer_new (tgl_state.ev_base, send_all_acks_gateway, S); + S->ev = TLS->timer_methods->alloc (TLS, send_all_acks_gateway, S); assert (!DC->sessions[0]); DC->sessions[0] = S; } -void tgl_do_send_ping (struct connection *c) { +void tgl_do_send_ping (struct tgl_state *TLS, struct connection *c) { int x[3]; x[0] = CODE_ping; *(long long *)(x + 1) = lrand48 () * (1ll << 32) + lrand48 (); - tglmp_encrypt_send_message (c, x, 3, 0); + tglmp_encrypt_send_message (TLS, c, x, 3, 0); } -void tgl_dc_iterator (void (*iterator)(struct tgl_dc *DC)) { +void tgl_dc_iterator (struct tgl_state *TLS, void (*iterator)(struct tgl_dc *DC)) { int i; - for (i = 0; i <= tgl_state.max_dc_num; i++) { - iterator (tgl_state.DC_list[i]); + for (i = 0; i <= TLS->max_dc_num; i++) { + iterator (TLS->DC_list[i]); } } -void tgl_dc_iterator_ex (void (*iterator)(struct tgl_dc *DC, void *extra), void *extra) { +void tgl_dc_iterator_ex (struct tgl_state *TLS, void (*iterator)(struct tgl_dc *DC, void *extra), void *extra) { int i; - for (i = 0; i <= tgl_state.max_dc_num; i++) { - iterator (tgl_state.DC_list[i], extra); + for (i = 0; i <= TLS->max_dc_num; i++) { + iterator (TLS->DC_list[i], extra); } } -void tglmp_regenerate_temp_auth_key (struct tgl_dc *D) { +void tglmp_regenerate_temp_auth_key (struct tgl_state *TLS, struct tgl_dc *D) { D->flags &= ~6; D->temp_auth_key_id = 0; memset (D->temp_auth_key, 0, 256); if (!D->sessions[0]) { - tgl_dc_authorize (D); + tgl_dc_authorize (TLS, D); return; } @@ -1434,7 +1426,7 @@ void tglmp_regenerate_temp_auth_key (struct tgl_dc *D) { tglt_secure_random (&S->session_id, 8); S->seq_no = 0; - event_del (S->ev); + TLS->timer_methods->delete (S->ev); S->ack_tree = tree_clear_long (S->ack_tree); if (D->state != st_authorized) { @@ -1442,29 +1434,29 @@ void tglmp_regenerate_temp_auth_key (struct tgl_dc *D) { } if (S->c) { - create_temp_auth_key (S->c); + create_temp_auth_key (TLS, S->c); } } -void tgls_free_session (struct tgl_session *S) { +void tgls_free_session (struct tgl_state *TLS, struct tgl_session *S) { S->ack_tree = tree_clear_long (S->ack_tree); - if (S->ev) { event_free (S->ev); } + if (S->ev) { TLS->timer_methods->free (S->ev); } if (S->c) { - tgl_state.net_methods->free (S->c); + TLS->net_methods->free (S->c); } tfree (S, sizeof (*S)); } -void tgls_free_dc (struct tgl_dc *DC) { +void tgls_free_dc (struct tgl_state *TLS, struct tgl_dc *DC) { if (DC->ip) { tfree_str (DC->ip); } struct tgl_session *S = DC->sessions[0]; - if (S) { tgls_free_session (S); } + if (S) { tgls_free_session (TLS, S); } - if (DC->ev) { event_free (DC->ev); } + if (DC->ev) { TLS->timer_methods->free (DC->ev); } tfree (DC, sizeof (*DC)); } -void tgls_free_pubkey (void) { - RSA_free (pubKey); +void tgls_free_pubkey (struct tgl_state *TLS) { + RSA_free (TLS->pubKey); } diff --git a/mtproto-client.h b/mtproto-client.h index 65212de..692a4b8 100644 --- a/mtproto-client.h +++ b/mtproto-client.h @@ -45,16 +45,16 @@ struct tgl_dc; struct connection; -long long tglmp_encrypt_send_message (struct connection *c, int *msg, int msg_ints, int flags); -void tglmp_dc_create_session (struct tgl_dc *DC); -int tglmp_check_g (unsigned char p[256], BIGNUM *g); -int tglmp_check_DH_params (BIGNUM *p, int g); -struct tgl_dc *tglmp_alloc_dc (int id, char *ip, int port); -void tglmp_regenerate_temp_auth_key (struct tgl_dc *D); +long long tglmp_encrypt_send_message (struct tgl_state *TLS, struct connection *c, int *msg, int msg_ints, int flags); +void tglmp_dc_create_session (struct tgl_state *TLS, struct tgl_dc *DC); +int tglmp_check_g (struct tgl_state *TLS, unsigned char p[256], BIGNUM *g); +int tglmp_check_DH_params (struct tgl_state *TLS, BIGNUM *p, int g); +struct tgl_dc *tglmp_alloc_dc (struct tgl_state *TLS, int id, char *ip, int port); +void tglmp_regenerate_temp_auth_key (struct tgl_state *TLS, struct tgl_dc *D); -void tgln_insert_msg_id (struct tgl_session *S, long long id); -void tglmp_on_start (void); -void tgl_dc_authorize (struct tgl_dc *DC); -void tgls_free_dc (struct tgl_dc *DC); -void tgls_free_pubkey (void); +void tgln_insert_msg_id (struct tgl_state *TLS, struct tgl_session *S, long long id); +void tglmp_on_start (struct tgl_state *TLS); +void tgl_dc_authorize (struct tgl_state *TLS, struct tgl_dc *DC); +void tgls_free_dc (struct tgl_state *TLS, struct tgl_dc *DC); +void tgls_free_pubkey (struct tgl_state *TLS); #endif diff --git a/mtproto-common.c b/mtproto-common.c index 9071e7a..dd8caaf 100644 --- a/mtproto-common.c +++ b/mtproto-common.c @@ -57,7 +57,7 @@ static long long rsa_encrypted_chunks, rsa_decrypted_chunks; //int verbosity; -static int get_random_bytes (unsigned char *buf, int n) { +static int get_random_bytes (struct tgl_state *TLS, unsigned char *buf, int n) { int r = 0, h = open ("/dev/random", O_RDONLY | O_NONBLOCK); if (h >= 0) { r = read (h, buf, n); @@ -107,7 +107,7 @@ static __inline__ unsigned long long rdtsc (void) { } #endif -void tgl_prng_seed (const char *password_filename, int password_length) { +void tgl_prng_seed (struct tgl_state *TLS, const char *password_filename, int password_length) { struct timespec T; tgl_my_clock_gettime (CLOCK_REALTIME, &T); RAND_add (&T, sizeof (T), 4.0); @@ -120,7 +120,7 @@ void tgl_prng_seed (const char *password_filename, int password_length) { p = getppid (); RAND_add (&p, sizeof (p), 0.0); unsigned char rb[32]; - int s = get_random_bytes (rb, 32); + int s = get_random_bytes (TLS, rb, 32); if (s > 0) { RAND_add (rb, s, s); } @@ -142,8 +142,8 @@ void tgl_prng_seed (const char *password_filename, int password_length) { tfree_secure (a, password_length); } } - tgl_state.BN_ctx = BN_CTX_new (); - ensure_ptr (tgl_state.BN_ctx); + TLS->BN_ctx = BN_CTX_new (); + ensure_ptr (TLS->BN_ctx); } int tgl_serialize_bignum (BIGNUM *b, char *buffer, int maxlen) { @@ -250,7 +250,7 @@ int tgl_fetch_bignum (BIGNUM *x) { return l; } -int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E) { +int tgl_pad_rsa_encrypt (struct tgl_state *TLS, char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E) { int pad = (255000 - from_len - 32) % 255 + 32; int chunks = (from_len + pad) / 255; int bits = BN_num_bits (N); @@ -265,7 +265,7 @@ int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N rsa_encrypted_chunks += chunks; for (i = 0; i < chunks; i++) { BN_bin2bn ((unsigned char *) from, 255, &x); - assert (BN_mod_exp (&y, &x, E, N, tgl_state.BN_ctx) == 1); + assert (BN_mod_exp (&y, &x, E, N, TLS->BN_ctx) == 1); unsigned l = 256 - BN_num_bytes (&y); assert (l <= 256); memset (to, 0, l); @@ -277,7 +277,7 @@ int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N return chunks * 256; } -int tgl_pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D) { +int tgl_pad_rsa_decrypt (struct tgl_state *TLS, char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D) { if (from_len < 0 || from_len > 0x1000 || (from_len & 0xff)) { return -1; } @@ -292,7 +292,7 @@ int tgl_pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N for (i = 0; i < chunks; i++) { ++rsa_decrypted_chunks; BN_bin2bn ((unsigned char *) from, 256, &x); - assert (BN_mod_exp (&y, &x, D, N, tgl_state.BN_ctx) == 1); + assert (BN_mod_exp (&y, &x, D, N, TLS->BN_ctx) == 1); int l = BN_num_bytes (&y); if (l > 255) { BN_free (&x); diff --git a/mtproto-common.h b/mtproto-common.h index 731486c..a1d1ce5 100644 --- a/mtproto-common.h +++ b/mtproto-common.h @@ -117,7 +117,7 @@ struct encrypted_message { //BN_CTX *BN_ctx; -void tgl_prng_seed (const char *password_filename, int password_length); +void tgl_prng_seed (struct tgl_state *TLS, const char *password_filename, int password_length); int tgl_serialize_bignum (BIGNUM *b, char *buffer, int maxlen); long long tgl_do_compute_rsa_key_fingerprint (RSA *key); @@ -356,8 +356,8 @@ static inline int in_remaining (void) { //int get_random_bytes (unsigned char *buf, int n); -int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E); -int tgl_pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D); +int tgl_pad_rsa_encrypt (struct tgl_state *TLS, char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E); +int tgl_pad_rsa_decrypt (struct tgl_state *TLS, char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D); //extern long long rsa_encrypted_chunks, rsa_decrypted_chunks; diff --git a/net.c b/net.c index b8af790..8f48e80 100644 --- a/net.c +++ b/net.c @@ -71,6 +71,7 @@ static void fail_connection (struct connection *c); static void start_ping_timer (struct connection *c); static void ping_alarm (evutil_socket_t fd, short what, void *arg) { struct connection *c = arg; + struct tgl_state *TLS = c->TLS; vlogprintf (E_DEBUG + 2,"ping alarm\n"); assert (c->state == conn_ready || c->state == conn_connecting); if (tglt_get_double_time () - c->last_receive_time > 6 * PING_TIMEOUT) { @@ -78,7 +79,7 @@ static void ping_alarm (evutil_socket_t fd, short what, void *arg) { c->state = conn_failed; fail_connection (c); } else if (tglt_get_double_time () - c->last_receive_time > 3 * PING_TIMEOUT && c->state == conn_ready) { - tgl_do_send_ping (c); + tgl_do_send_ping (c->TLS, c); start_ping_timer (c); } else { start_ping_timer (c); @@ -124,6 +125,7 @@ static void delete_connection_buffer (struct connection_buffer *b) { } int tgln_write_out (struct connection *c, const void *_data, int len) { + struct tgl_state *TLS = c->TLS; vlogprintf (E_DEBUG, "write_out: %d bytes\n", len); const unsigned char *data = _data; if (!len) { return 0; } @@ -242,14 +244,16 @@ static void try_write (struct connection *c); static void conn_try_read (evutil_socket_t fd, short what, void *arg) { struct connection *c = arg; + struct tgl_state *TLS = c->TLS; vlogprintf (E_DEBUG + 1, "Try read. Fd = %d\n", c->fd); try_read (c); } static void conn_try_write (evutil_socket_t fd, short what, void *arg) { struct connection *c = arg; + struct tgl_state *TLS = c->TLS; if (c->state == conn_connecting) { c->state = conn_ready; - c->methods->ready (c); + c->methods->ready (TLS, c); } try_write (c); if (c->out_bytes) { @@ -257,8 +261,9 @@ static void conn_try_write (evutil_socket_t fd, short what, void *arg) { } } -struct connection *tgln_create_connection (const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods) { +struct connection *tgln_create_connection (struct tgl_state *TLS, const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods) { struct connection *c = talloc0 (sizeof (*c)); + c->TLS = TLS; int fd = socket (AF_INET, SOCK_STREAM, 0); if (fd == -1) { vlogprintf (E_ERROR, "Can not create socket: %m\n"); @@ -300,12 +305,12 @@ struct connection *tgln_create_connection (const char *host, int port, struct tg assert (!Connections[fd]); Connections[fd] = c; - c->ping_ev = evtimer_new (tgl_state.ev_base, ping_alarm, c); - c->fail_ev = evtimer_new (tgl_state.ev_base, fail_alarm, c); - c->write_ev = event_new (tgl_state.ev_base, c->fd, EV_WRITE, conn_try_write, c); + c->ping_ev = evtimer_new (TLS->ev_base, ping_alarm, c); + c->fail_ev = evtimer_new (TLS->ev_base, fail_alarm, c); + c->write_ev = event_new (TLS->ev_base, c->fd, EV_WRITE, conn_try_write, c); struct timeval tv = {5, 0}; - c->read_ev = event_new (tgl_state.ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c); + c->read_ev = event_new (TLS->ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c); event_add (c->read_ev, &tv); start_ping_timer (c); @@ -322,6 +327,7 @@ struct connection *tgln_create_connection (const char *host, int port, struct tg } static void restart_connection (struct connection *c) { + struct tgl_state *TLS = c->TLS; if (c->last_connect_time == time (0)) { start_fail_timer (c); return; @@ -369,10 +375,10 @@ static void restart_connection (struct connection *c) { start_ping_timer (c); Connections[fd] = c; - c->write_ev = event_new (tgl_state.ev_base, c->fd, EV_WRITE, conn_try_write, c); + c->write_ev = event_new (TLS->ev_base, c->fd, EV_WRITE, conn_try_write, c); struct timeval tv = {5, 0}; - c->read_ev = event_new (tgl_state.ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c); + c->read_ev = event_new (TLS->ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c); event_add (c->read_ev, &tv); char byte = 0xef; @@ -381,6 +387,7 @@ static void restart_connection (struct connection *c) { } static void fail_connection (struct connection *c) { + struct tgl_state *TLS = c->TLS; if (c->state == conn_ready || c->state == conn_connecting) { stop_ping_timer (c); } @@ -411,6 +418,7 @@ static void fail_connection (struct connection *c) { //extern FILE *log_net_f; static void try_write (struct connection *c) { + struct tgl_state *TLS = c->TLS; vlogprintf (E_DEBUG, "try write: fd = %d\n", c->fd); int x = 0; while (c->out_head) { @@ -443,6 +451,7 @@ static void try_write (struct connection *c) { static void try_rpc_read (struct connection *c) { assert (c->in_head); + struct tgl_state *TLS = c->TLS; while (1) { if (c->in_bytes < 1) { return; } @@ -472,11 +481,12 @@ static void try_rpc_read (struct connection *c) { len *= 4; int op; assert (tgln_read_in_lookup (c, &op, 4) == 4); - c->methods->execute (c, op, len); + c->methods->execute (TLS, c, op, len); } } static void try_read (struct connection *c) { + struct tgl_state *TLS = c->TLS; vlogprintf (E_DEBUG, "try read: fd = %d\n", c->fd); if (!c->in_tail) { c->in_head = c->in_tail = new_connection_buffer (1 << 20); @@ -518,7 +528,7 @@ static void try_read (struct connection *c) { try_rpc_read (c); } } - +/* int tgl_connections_make_poll_array (struct pollfd *fds, int max) { int _max = max; int i; @@ -562,7 +572,7 @@ void tgl_connections_poll_result (struct pollfd *fds, int max) { } } } -} +}*/ static void incr_out_packet_num (struct connection *c) { c->out_packet_num ++; diff --git a/net.h b/net.h index c128d44..00d7da2 100644 --- a/net.h +++ b/net.h @@ -20,23 +20,6 @@ #ifndef __NET_H__ #define __NET_H__ -#include - -#define DC_SERIALIZED_MAGIC 0x64582faa -#define DC_SERIALIZED_MAGIC_V2 0x94032abb -#define STATE_FILE_MAGIC 0x84217a0d -#define SECRET_CHAT_FILE_MAGIC 0xa9840add - -struct tgl_dc_serialized { - int magic; - int port; - char ip[64]; - char user[64]; - char auth_key[256]; - long long auth_key_id, server_salt; - int authorized; -}; - struct connection_buffer { unsigned char *start; unsigned char *end; @@ -71,6 +54,7 @@ struct connection { int last_connect_time; int in_fail_timer; struct mtproto_methods *methods; + struct tgl_state *TLS; struct tgl_session *session; struct tgl_dc *dc; void *extra; @@ -88,16 +72,16 @@ void tgln_flush_out (struct connection *c); int tgln_read_in (struct connection *c, void *data, int len); int tgln_read_in_lookup (struct connection *c, void *data, int len); -void tgln_insert_msg_id (struct tgl_session *S, long long id); +//void tgln_insert_msg_id (struct tgl_session *S, long long id); extern struct tgl_net_methods tgl_conn_methods; //void create_all_outbound_connections (void); //struct connection *create_connection (const char *host, int port, struct tgl_session *session, struct connection_methods *methods); -struct tgl_dc *tgln_alloc_dc (int id, char *ip, int port); -void tgln_dc_create_session (struct tgl_dc *DC, struct mtproto_methods *methods); -struct connection *tgln_create_connection (const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods); +//struct tgl_dc *tgln_alloc_dc (int id, char *ip, int port); +//void tgln_dc_create_session (struct tgl_dc *DC, struct mtproto_methods *methods); +struct connection *tgln_create_connection (struct tgl_state *TLS, const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods); #define GET_DC(c) (c->session->dc) #endif diff --git a/queries.c b/queries.c index 37469f8..0b79857 100644 --- a/queries.c +++ b/queries.c @@ -53,12 +53,6 @@ #include "updates.h" #include "auto.h" #include "tgl.h" -#ifdef EVENT_V2 -#include -#else -#include -#include "event-old.h" -#endif #define sha1 SHA1 @@ -80,30 +74,21 @@ char *get_downloads_directory (void); //static int queries_num; -static void out_peer_id (tgl_peer_id_t id); +static void out_peer_id (struct tgl_state *TLS, tgl_peer_id_t id); #define QUERY_TIMEOUT 6.0 #define memcmp8(a,b) memcmp ((a), (b), 8) DEFINE_TREE (query, struct query *, memcmp8, 0) ; -static struct tree_query *queries_tree; -struct query *tglq_query_get (long long id) { - return tree_lookup_query (queries_tree, (void *)&id); +struct query *tglq_query_get (struct tgl_state *TLS, long long id) { + return tree_lookup_query (TLS->queries_tree, (void *)&id); } -static int alarm_query (struct query *q) { +static int alarm_query (struct tgl_state *TLS, struct query *q) { assert (q); vlogprintf (E_DEBUG - 1, "Alarm query %lld\n", q->msg_id); - //q->ev.timeout = get_double_time () + QUERY_TIMEOUT; - //insert_event_timer (&q->ev); - - static struct timeval ptimeout = { QUERY_TIMEOUT, 0}; - event_add (q->ev, &ptimeout); - - /*if (q->session->c->out_bytes >= 100000) { - return 0; - }*/ + TLS->timer_methods->insert (q->ev, QUERY_TIMEOUT); if (q->session->session_id == q->session_id) { clear_packet (); @@ -114,14 +99,14 @@ static int alarm_query (struct query *q) { out_int (4 * q->data_len); out_ints (q->data, q->data_len); - tglmp_encrypt_send_message (q->session->c, packet_buffer, packet_ptr - packet_buffer, q->flags & QUERY_FORCE_SEND); + tglmp_encrypt_send_message (TLS, q->session->c, packet_buffer, packet_ptr - packet_buffer, q->flags & QUERY_FORCE_SEND); } else { q->flags &= ~QUERY_ACK_RECEIVED; - if (tree_lookup_query (queries_tree, q)) { - queries_tree = tree_delete_query (queries_tree, q); + if (tree_lookup_query (TLS->queries_tree, q)) { + TLS->queries_tree = tree_delete_query (TLS->queries_tree, q); } - q->msg_id = tglmp_encrypt_send_message (q->session->c, q->data, q->data_len, (q->flags & QUERY_FORCE_SEND) | 1); - queries_tree = tree_insert_query (queries_tree, q, lrand48 ()); + q->msg_id = tglmp_encrypt_send_message (TLS, q->session->c, q->data, q->data_len, (q->flags & QUERY_FORCE_SEND) | 1); + TLS->queries_tree = tree_insert_query (TLS->queries_tree, q, lrand48 ()); q->session_id = q->session->session_id; if (!(q->session->dc->flags & 4) && !(q->flags & QUERY_FORCE_SEND)) { q->session_id = 0; @@ -130,31 +115,31 @@ static int alarm_query (struct query *q) { return 0; } -void tglq_query_restart (long long id) { - struct query *q = tglq_query_get (id); +void tglq_query_restart (struct tgl_state *TLS, long long id) { + struct query *q = tglq_query_get (TLS, id); if (q) { - event_del (q->ev); - alarm_query (q); + TLS->timer_methods->delete (q->ev); + alarm_query (TLS, q); } } -static void alarm_query_gateway (evutil_socket_t fd, short what, void *arg) { - alarm_query (arg); +static void alarm_query_gateway (struct tgl_state *TLS, void *arg) { + alarm_query (TLS, arg); } -struct query *tglq_send_query_ex (struct tgl_dc *DC, int ints, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra, int flags) { +struct query *tglq_send_query_ex (struct tgl_state *TLS, struct tgl_dc *DC, int ints, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra, int flags) { assert (DC); assert (DC->auth_key_id); if (!DC->sessions[0]) { - tglmp_dc_create_session (DC); + tglmp_dc_create_session (TLS, DC); } vlogprintf (E_DEBUG, "Sending query of size %d to DC (%s:%d)\n", 4 * ints, DC->ip, DC->port); struct query *q = talloc0 (sizeof (*q)); q->data_len = ints; q->data = talloc (4 * ints); memcpy (q->data, data, 4 * ints); - q->msg_id = tglmp_encrypt_send_message (DC->sessions[0]->c, data, ints, 1 | (flags & QUERY_FORCE_SEND)); + q->msg_id = tglmp_encrypt_send_message (TLS, DC->sessions[0]->c, data, ints, 1 | (flags & QUERY_FORCE_SEND)); q->session = DC->sessions[0]; q->seq_no = q->session->seq_no - 1; q->session_id = q->session->session_id; @@ -166,63 +151,57 @@ struct query *tglq_send_query_ex (struct tgl_dc *DC, int ints, void *data, struc q->type = methods->type; q->DC = DC; q->flags = flags & QUERY_FORCE_SEND; - if (queries_tree) { - vlogprintf (E_DEBUG + 2, "%lld %lld\n", q->msg_id, queries_tree->x->msg_id); + if (TLS->queries_tree) { + vlogprintf (E_DEBUG + 2, "%lld %lld\n", q->msg_id, TLS->queries_tree->x->msg_id); } - queries_tree = tree_insert_query (queries_tree, q, lrand48 ()); + TLS->queries_tree = tree_insert_query (TLS->queries_tree, q, lrand48 ()); - //q->ev.alarm = (void *)alarm_query; - //q->ev.timeout = get_double_time () + QUERY_TIMEOUT; - //q->ev.self = (void *)q; - //insert_event_timer (&q->ev); - - q->ev = evtimer_new (tgl_state.ev_base, alarm_query_gateway, q); - static struct timeval ptimeout = { QUERY_TIMEOUT, 0}; - event_add (q->ev, &ptimeout); + q->ev = TLS->timer_methods->alloc (TLS, alarm_query_gateway, q); + TLS->timer_methods->insert (q->ev, QUERY_TIMEOUT); q->extra = extra; q->callback = callback; q->callback_extra = callback_extra; - tgl_state.active_queries ++; + TLS->active_queries ++; return q; } -struct query *tglq_send_query (struct tgl_dc *DC, int ints, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra) { - return tglq_send_query_ex (DC, ints, data, methods, extra, callback, callback_extra, 0); +struct query *tglq_send_query (struct tgl_state *TLS, struct tgl_dc *DC, int ints, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra) { + return tglq_send_query_ex (TLS, DC, ints, data, methods, extra, callback, callback_extra, 0); } -static int fail_on_error (struct query *q UU, int error_code UU, int l UU, char *error UU) { +static int fail_on_error (struct tgl_state *TLS, struct query *q UU, int error_code UU, int l UU, char *error UU) { fprintf (stderr, "error #%d: %.*s\n", error_code, l, error); assert (0); return 0; } -void tglq_query_ack (long long id) { - struct query *q = tglq_query_get (id); +void tglq_query_ack (struct tgl_state *TLS, long long id) { + struct query *q = tglq_query_get (TLS, id); if (q && !(q->flags & QUERY_ACK_RECEIVED)) { assert (q->msg_id == id); q->flags |= QUERY_ACK_RECEIVED; - event_del (q->ev); + TLS->timer_methods->delete (q->ev); } } -void tglq_query_error (long long id) { +void tglq_query_error (struct tgl_state *TLS, long long id) { assert (fetch_int () == CODE_rpc_error); int error_code = fetch_int (); int error_len = prefetch_strlen (); char *error = fetch_str (error_len); vlogprintf (E_WARNING, "error for query #%lld: #%d :%.*s\n", id, error_code, error_len, error); - struct query *q = tglq_query_get (id); + struct query *q = tglq_query_get (TLS, id); if (!q) { vlogprintf (E_WARNING, "No such query\n"); } else { if (!(q->flags & QUERY_ACK_RECEIVED)) { - event_del (q->ev); + TLS->timer_methods->delete (q->ev); } - queries_tree = tree_delete_query (queries_tree, q); + TLS->queries_tree = tree_delete_query (TLS->queries_tree, q); int res = 0; if (q->methods && q->methods->on_error && error_code != 500) { - res = q->methods->on_error (q, error_code, error_len, error); + res = q->methods->on_error (TLS, q, error_code, error_len, error); } else { if (error_code == 420 || error_code == 500) { int wait; @@ -237,26 +216,23 @@ void tglq_query_error (long long id) { wait = 10; } q->flags &= ~QUERY_ACK_RECEIVED; - static struct timeval ptimeout; - ptimeout.tv_sec = wait; - event_add (q->ev, &ptimeout); + TLS->timer_methods->insert (q->ev, wait); q->session_id = 0; res = 1; } } if (res <= 0) { tfree (q->data, q->data_len * 4); - event_free (q->ev); - tfree (q, sizeof (*q)); + TLS->timer_methods->free (q->ev); } } - tgl_state.active_queries --; + TLS->active_queries --; } #define MAX_PACKED_SIZE (1 << 24) static int packed_buffer[MAX_PACKED_SIZE / 4]; -void tglq_query_result (long long id UU) { +void tglq_query_result (struct tgl_state *TLS, long long id UU) { vlogprintf (E_DEBUG, "result for query #%lld. Size %ld bytes\n", id, (long)4 * (in_end - in_ptr)); /*if (verbosity >= 4) { logprintf ( "result: "); @@ -281,7 +257,7 @@ void tglq_query_result (long long id UU) { hexdump_in (); }*/ } - struct query *q = tglq_query_get (id); + struct query *q = tglq_query_get (TLS, id); if (!q) { //if (verbosity) { // logprintf ( "No such query\n"); @@ -290,9 +266,9 @@ void tglq_query_result (long long id UU) { in_ptr = in_end; } else { if (!(q->flags & QUERY_ACK_RECEIVED)) { - event_del (q->ev); + TLS->timer_methods->delete (q->ev); } - queries_tree = tree_delete_query (queries_tree, q); + TLS->queries_tree = tree_delete_query (TLS->queries_tree, q); if (q->methods && q->methods->on_answer) { if (q->type) { int *save = in_ptr; @@ -305,18 +281,19 @@ void tglq_query_result (long long id UU) { assert (in_ptr == in_end); in_ptr = save; } - q->methods->on_answer (q); + q->methods->on_answer (TLS, q); assert (in_ptr == in_end); } tfree (q->data, 4 * q->data_len); - event_free (q->ev); + TLS->timer_methods->free (q->ev); tfree (q, sizeof (*q)); + } if (end) { in_ptr = end; in_end = eend; } - tgl_state.active_queries --; + TLS->active_queries --; } @@ -325,7 +302,7 @@ void tglq_query_result (long long id UU) { //int want_dc_num; //int new_dc_num; //extern struct tgl_dc *DC_list[]; -//extern struct tgl_dc *tgl_state.DC_working; +//extern struct tgl_dc *TLS->DC_working; static void out_random (int n) { assert (n <= 32); @@ -359,7 +336,7 @@ void tgl_do_insert_header (void) { /* {{{ Get config */ -static void fetch_dc_option (void) { +static void fetch_dc_option (struct tgl_state *TLS) { assert (fetch_int () == CODE_dc_option); int id = fetch_int (); int l1 = prefetch_strlen (); @@ -367,12 +344,11 @@ static void fetch_dc_option (void) { int l2 = prefetch_strlen (); char *ip = fetch_str (l2); int port = fetch_int (); - vlogprintf (E_DEBUG, "id = %d, name = %.*s ip = %.*s port = %d\n", id, l1, name, l2, ip, port); - bl_do_dc_option (id, l1, name, l2, ip, port); + bl_do_dc_option (TLS, id, l1, name, l2, ip, port); } -static int help_get_config_on_answer (struct query *q UU) { +static int help_get_config_on_answer (struct tgl_state *TLS, struct query *q UU) { unsigned op = fetch_int (); assert (op == CODE_config || op == CODE_config_old); fetch_int (); @@ -387,7 +363,7 @@ static int help_get_config_on_answer (struct query *q UU) { assert (n <= 10); int i; for (i = 0; i < n; i++) { - fetch_dc_option (); + fetch_dc_option (TLS); } int max_chat_size = fetch_int (); int max_bcast_size = 0; @@ -397,7 +373,7 @@ static int help_get_config_on_answer (struct query *q UU) { vlogprintf (E_DEBUG, "chat_size = %d, bcast_size = %d\n", max_chat_size, max_bcast_size); if (q->callback) { - ((void (*)(void *, int))(q->callback))(q->callback_extra, 1); + ((void (*)(struct tgl_state *,void *, int))(q->callback))(TLS, q->callback_extra, 1); } return 0; } @@ -407,23 +383,23 @@ static struct query_methods help_get_config_methods = { .type = TYPE_TO_PARAM(config) }; -void tgl_do_help_get_config (void (*callback)(void *, int), void *callback_extra) { +void tgl_do_help_get_config (struct tgl_state *TLS, void (*callback)(struct tgl_state *,void *, int), void *callback_extra) { clear_packet (); tgl_do_insert_header (); out_int (CODE_help_get_config); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &help_get_config_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &help_get_config_methods, 0, callback, callback_extra); } -void tgl_do_help_get_config_dc (struct tgl_dc *D, void (*callback)(void *, int), void *callback_extra) { +void tgl_do_help_get_config_dc (struct tgl_state *TLS, struct tgl_dc *D, void (*callback)(struct tgl_state *, void *, int), void *callback_extra) { clear_packet (); tgl_do_insert_header (); out_int (CODE_help_get_config); - tglq_send_query_ex (D, packet_ptr - packet_buffer, packet_buffer, &help_get_config_methods, 0, callback, callback_extra, 2); + tglq_send_query_ex (TLS, D, packet_ptr - packet_buffer, packet_buffer, &help_get_config_methods, 0, callback, callback_extra, 2); } /* }}} */ /* {{{ Send code */ -static int send_code_on_answer (struct query *q UU) { +static int send_code_on_answer (struct tgl_state *TLS, struct query *q UU) { static char *phone_code_hash; assert (fetch_int () == (int)CODE_auth_sent_code); int registered = fetch_bool (); @@ -438,12 +414,12 @@ static int send_code_on_answer (struct query *q UU) { tfree_str (q->extra); if (q->callback) { - ((void (*)(void *, int, int, const char *))(q->callback)) (q->callback_extra, 1, registered, phone_code_hash); + ((void (*)(struct tgl_state *, void *, int, int, const char *))(q->callback)) (TLS, q->callback_extra, 1, registered, phone_code_hash); } return 0; } -static int send_code_on_error (struct query *q UU, int error_code, int l, char *error) { +static int send_code_on_error (struct tgl_state *TLS, struct query *q UU, int error_code, int l, char *error) { int s = strlen ("PHONE_MIGRATE_"); int s2 = strlen ("NETWORK_MIGRATE_"); int want_dc_num = 0; @@ -455,14 +431,17 @@ static int send_code_on_error (struct query *q UU, int error_code, int l, char * want_dc_num = i; } else { vlogprintf (E_ERROR, "error_code = %d, error = %.*s\n", error_code, l, error); - assert (0); + if (q->callback) { + ((void (*)(struct tgl_state *, void *, int, int, const char *))(q->callback)) (TLS, q->callback_extra, 0, 0, 0); + } + return 0; } - bl_do_set_working_dc (want_dc_num); + bl_do_set_working_dc (TLS, want_dc_num); //if (q->callback) { // ((void (*)(void *, int, int, const char *))(q->callback)) (q->callback_extra, 0, 0, 0); //} - assert (tgl_state.DC_working->id == want_dc_num); - tgl_do_send_code (q->extra, q->callback, q->callback_extra); + assert (TLS->DC_working->id == want_dc_num); + tgl_do_send_code (TLS, q->extra, q->callback, q->callback_extra); tfree_str (q->extra); return 0; } @@ -475,8 +454,8 @@ static struct query_methods send_code_methods = { //char *suser; //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) { - vlogprintf (E_DEBUG, "sending code to dc %d\n", tgl_state.dc_working_num); +void tgl_do_send_code (struct tgl_state *TLS, const char *user, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int registered, const char *hash), void *callback_extra) { + vlogprintf (E_DEBUG, "sending code to dc %d\n", TLS->dc_working_num); //suser = tstrdup (user); clear_packet (); tgl_do_insert_header (); @@ -487,14 +466,14 @@ void tgl_do_send_code (const char *user, void (*callback)(void *callback_extra, out_string (TG_APP_HASH); out_string ("en"); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_code_methods, tstrdup (user), callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &send_code_methods, tstrdup (user), callback, callback_extra); } -static int phone_call_on_answer (struct query *q UU) { +static int phone_call_on_answer (struct tgl_state *TLS, struct query *q UU) { fetch_bool (); if (q->callback) { - ((void (*)(void *, int))(q->callback))(q->callback_extra, 1); + ((void (*)(struct tgl_state *TLS, void *, int))(q->callback))(TLS, q->callback_extra, 1); } return 0; } @@ -504,7 +483,7 @@ static struct query_methods phone_call_methods = { .type = TYPE_TO_PARAM(bool) }; -void tgl_do_phone_call (const char *user, const char *hash,void (*callback)(void *callback_extra, int success), void *callback_extra) { +void tgl_do_phone_call (struct tgl_state *TLS, const char *user, const char *hash,void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra) { vlogprintf (E_DEBUG, "calling user\n"); //suser = tstrdup (user); //want_dc_num = 0; @@ -514,24 +493,24 @@ void tgl_do_phone_call (const char *user, const char *hash,void (*callback)(void out_string (user); out_string (hash); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &phone_call_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &phone_call_methods, 0, callback, callback_extra); } /* }}} */ /* {{{ Sign in / Sign up */ -static int sign_in_on_answer (struct query *q UU) { +static int sign_in_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == (int)CODE_auth_authorization); int expires = fetch_int (); vlogprintf (E_DEBUG, "Expires in %d\n", expires); - struct tgl_user *U = tglf_fetch_alloc_user (); + struct tgl_user *U = tglf_fetch_alloc_user (TLS); - tgl_state.DC_working->has_auth = 1; + TLS->DC_working->has_auth = 1; - bl_do_dc_signed (tgl_state.DC_working->id); + bl_do_dc_signed (TLS, TLS->DC_working->id); if (q->callback) { - ((void (*)(void *, int, struct tgl_user *))q->callback) (q->callback_extra, 1, U); + ((void (*)(struct tgl_state *TLS, void *, int, struct tgl_user *))q->callback) (TLS, q->callback_extra, 1, U); } return 0; @@ -542,17 +521,17 @@ static struct query_methods sign_in_methods = { .type = TYPE_TO_PARAM(auth_authorization) }; -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 (struct tgl_state *TLS, const char *user, const char *hash, const char *code, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) { clear_packet (); out_int (CODE_auth_sign_in); out_string (user); out_string (hash); out_string (code); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra); return 0; } -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) { +int tgl_do_send_code_result_auth (struct tgl_state *TLS, const char *user, const char *hash, const char *code, const char *first_name, const char *last_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) { clear_packet (); out_int (CODE_auth_sign_up); out_string (user); @@ -560,13 +539,13 @@ int tgl_do_send_code_result_auth (const char *user, const char *hash, const char out_string (code); out_string (first_name); out_string (last_name); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra); return 0; } /* }}} */ /* {{{ Get contacts */ -static int get_contacts_on_answer (struct query *q UU) { +static int get_contacts_on_answer (struct tgl_state *TLS, struct query *q UU) { int i; assert (fetch_int () == (int)CODE_contacts_contacts); @@ -581,14 +560,14 @@ static int get_contacts_on_answer (struct query *q UU) { n = fetch_int (); struct tgl_user **list = talloc (sizeof (void *) * n); for (i = 0; i < n; i++) { - list[i] = tglf_fetch_alloc_user (); + list[i] = tglf_fetch_alloc_user (TLS); } if (q->callback) { - ((void (*)(void *, int, int, struct tgl_user **))q->callback) (q->callback_extra, 1, n, list); + ((void (*)(struct tgl_state *TLS, void *, int, int, struct tgl_user **))q->callback) (TLS, q->callback_extra, 1, n, list); } tfree (list, sizeof (void *) * n); /* for (i = 0; i < n; i++) { - struct tgl_user *U = tglf_fetch_alloc_user (); + struct tgl_user *U = tglf_fetch_alloc_user (TLS); print_start (); push_color (COLOR_YELLOW); printf ("User #%d: ", tgl_get_peer_id (U->id)); @@ -625,11 +604,11 @@ static struct query_methods get_contacts_methods = { }; -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 (struct tgl_state *TLS, void (*callback) (struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_user *contacts[]), void *callback_extra) { clear_packet (); out_int (CODE_contacts_get_contacts); out_string (""); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_contacts_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &get_contacts_methods, 0, callback, callback_extra); } /* }}} */ @@ -714,76 +693,76 @@ static void encr_finish (struct tgl_secret_chat *E) { } /* }}} */ -void tgl_do_send_encr_chat_layer (struct tgl_secret_chat *E) { +void tgl_do_send_encr_chat_layer (struct tgl_state *TLS, struct tgl_secret_chat *E) { long long t; tglt_secure_random (&t, 8); int action[2]; action[0] = CODE_decrypted_message_action_notify_layer; action[1] = TGL_ENCRYPTED_LAYER; - bl_do_send_message_action_encr (t, tgl_state.our_id, tgl_get_peer_type (E->id), tgl_get_peer_id (E->id), time (0), 2, action); + bl_do_send_message_action_encr (TLS, t, TLS->our_id, tgl_get_peer_type (E->id), tgl_get_peer_id (E->id), time (0), 2, action); - struct tgl_message *M = tgl_message_get (t); + struct tgl_message *M = tgl_message_get (TLS, t); assert (M); assert (M->action.type == tgl_message_action_notify_layer); - tgl_do_send_msg (M, 0, 0); + tgl_do_send_msg (TLS, M, 0, 0); //print_message (M); } -void tgl_do_set_encr_chat_ttl (struct tgl_secret_chat *E, int ttl, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void tgl_do_set_encr_chat_ttl (struct tgl_state *TLS, struct tgl_secret_chat *E, int ttl, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { long long t; tglt_secure_random (&t, 8); int action[2]; action[0] = CODE_decrypted_message_action_set_message_t_t_l; action[1] = ttl; - bl_do_send_message_action_encr (t, tgl_state.our_id, tgl_get_peer_type (E->id), tgl_get_peer_id (E->id), time (0), 2, action); + bl_do_send_message_action_encr (TLS, t, TLS->our_id, tgl_get_peer_type (E->id), tgl_get_peer_id (E->id), time (0), 2, action); - struct tgl_message *M = tgl_message_get (t); + struct tgl_message *M = tgl_message_get (TLS, t); assert (M); assert (M->action.type == tgl_message_action_set_message_ttl); - tgl_do_send_msg (M, callback, callback_extra); + tgl_do_send_msg (TLS, M, callback, callback_extra); //print_message (M); } /* {{{ Seng msg (plain text) */ -static int msg_send_encr_on_answer (struct query *q UU) { +static int msg_send_encr_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == CODE_messages_sent_encrypted_message); struct tgl_message *M = q->extra; //M->date = fetch_int (); fetch_int (); if (M->flags & FLAG_PENDING) { - bl_do_set_message_sent (M); - bl_do_msg_update (M->id); + bl_do_set_message_sent (TLS, M); + bl_do_msg_update (TLS, M->id); } if (q->callback) { - ((void (*)(void *, int, struct tgl_message *))q->callback) (q->callback_extra, 1, M); + ((void (*)(struct tgl_state *TLS, void *, int, struct tgl_message *))q->callback) (TLS, q->callback_extra, 1, M); } return 0; } -static int msg_send_on_answer (struct query *q UU) { +static int msg_send_on_answer (struct tgl_state *TLS, struct query *q UU) { unsigned x = fetch_int (); assert (x == CODE_messages_sent_message || x == CODE_messages_sent_message_link); int id = fetch_int (); // id long long y = *(long long *)q->extra; tfree (q->extra, 8); - struct tgl_message *M = tgl_message_get (y); + struct tgl_message *M = tgl_message_get (TLS, y); if (M && M->id != id) { - bl_do_set_msg_id (M, id); + bl_do_set_msg_id (TLS, M, id); } int date = fetch_int (); int pts = fetch_int (); //tglu_fetch_seq (); //bl_do_ int seq = fetch_int (); - if (seq == tgl_state.seq + 1 && !(tgl_state.locks & TGL_LOCK_DIFF)) { - bl_do_set_date (date); - bl_do_set_pts (pts); - bl_do_msg_seq_update (id); + if (seq == TLS->seq + 1 && !(TLS->locks & TGL_LOCK_DIFF)) { + bl_do_set_date (TLS, date); + bl_do_set_pts (TLS, pts); + bl_do_msg_seq_update (TLS, id); } else { - if (seq > tgl_state.seq + 1) { + if (seq > TLS->seq + 1) { vlogprintf (E_NOTICE, "Hole in seq\n"); - tgl_do_get_difference (0, 0, 0); + tgl_do_get_difference (TLS, 0, 0, 0); } } if (x == CODE_messages_sent_message_link) { @@ -806,7 +785,7 @@ static int msg_send_on_answer (struct query *q UU) { if (b == CODE_contacts_foreign_link_requested) { fetch_bool (); } - struct tgl_user *U = tglf_fetch_alloc_user (); + struct tgl_user *U = tglf_fetch_alloc_user (TLS); U->flags &= ~(FLAG_USER_IN_CONTACT | FLAG_USER_OUT_CONTACT); if (a == CODE_contacts_my_link_contact) { @@ -829,34 +808,33 @@ static int msg_send_on_answer (struct query *q UU) { } }*/ if (M->flags & FLAG_PENDING) { - bl_do_set_message_sent (M); + bl_do_set_message_sent (TLS, M); } if (q->callback) { - ((void (*)(void *, int, struct tgl_message *))q->callback) (q->callback_extra, 1, M); + ((void (*)(struct tgl_state *,void *, int, struct tgl_message *))q->callback) (TLS, q->callback_extra, 1, M); } return 0; } -static int msg_send_on_error (struct query *q, int error_code, int error_len, char *error) { +static int msg_send_on_error (struct tgl_state *TLS, struct query *q, int error_code, int error_len, char *error) { //vlogprintf (E_WARNING, "error for query #%lld: #%d :%.*s\n", q->msg_id, error_code, error_len, error); if (error_code == 420) { assert (!strncmp (error, "FLOOD_WAIT_", 11)); int wait = atoll (error + 11); q->flags &= ~QUERY_ACK_RECEIVED; - static struct timeval ptimeout; - ptimeout.tv_sec = wait; - event_add (q->ev, &ptimeout); + + TLS->timer_methods->insert (q->ev, wait); q->session_id = 0; return 1; } long long x = *(long long *)q->extra; tfree (q->extra, 8); - struct tgl_message *M = tgl_message_get (x); + struct tgl_message *M = tgl_message_get (TLS, x); if (q->callback) { - ((void (*)(void *, int, struct tgl_message *))q->callback) (q->callback_extra, 0, M); + ((void (*)(struct tgl_state *,void *, int, struct tgl_message *))q->callback) (TLS, q->callback_extra, 0, M); } if (M) { - bl_do_delete_msg (M); + bl_do_delete_msg (TLS, M); } return 0; } @@ -874,12 +852,12 @@ static struct query_methods msg_send_encr_methods = { //int out_message_num; -void tgl_do_send_encr_msg_action (struct tgl_message *M, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { - tgl_peer_t *P = tgl_peer_get (M->to_id); +void tgl_do_send_encr_msg_action (struct tgl_state *TLS, struct tgl_message *M, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { + tgl_peer_t *P = tgl_peer_get (TLS, M->to_id); if (!P || P->encr_chat.state != sc_ok) { vlogprintf (E_WARNING, "Unknown encrypted chat\n"); if (callback) { - ((void (*)(void *, int, struct tgl_message *))callback) (callback_extra, 0, M); + callback (TLS, callback_extra, 0, 0); } return; } @@ -897,8 +875,8 @@ void tgl_do_send_encr_msg_action (struct tgl_message *M, void (*callback)(void * out_int (CODE_decrypted_message_layer); out_random (15 + 4 * (lrand48 () % 3)); out_int (TGL_ENCRYPTED_LAYER); - out_int (2 * P->encr_chat.in_seq_no + (P->encr_chat.admin_id != tgl_state.our_id)); - out_int (2 * P->encr_chat.out_seq_no + (P->encr_chat.admin_id == tgl_state.our_id) - 2); + out_int (2 * P->encr_chat.in_seq_no + (P->encr_chat.admin_id != TLS->our_id)); + out_int (2 * P->encr_chat.out_seq_no + (P->encr_chat.admin_id == TLS->our_id) - 2); out_int (CODE_decrypted_message_service); } out_long (M->id); @@ -920,19 +898,19 @@ void tgl_do_send_encr_msg_action (struct tgl_message *M, void (*callback)(void * } encr_finish (&P->encr_chat); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M, callback, callback_extra); } -void tgl_do_send_encr_msg (struct tgl_message *M, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void tgl_do_send_encr_msg (struct tgl_state *TLS, struct tgl_message *M, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { if (M->service) { - tgl_do_send_encr_msg_action (M, callback, callback_extra); + tgl_do_send_encr_msg_action (TLS, M, callback, callback_extra); return; } - tgl_peer_t *P = tgl_peer_get (M->to_id); + tgl_peer_t *P = tgl_peer_get (TLS, M->to_id); if (!P || P->encr_chat.state != sc_ok) { vlogprintf (E_WARNING, "Unknown encrypted chat\n"); if (callback) { - ((void (*)(void *, int, struct tgl_message *))callback) (callback_extra, 0, M); + callback (TLS, callback_extra, 0, M); } return; } @@ -950,8 +928,8 @@ void tgl_do_send_encr_msg (struct tgl_message *M, void (*callback)(void *callbac out_int (CODE_decrypted_message_layer); out_random (15 + 4 * (lrand48 () % 3)); out_int (TGL_ENCRYPTED_LAYER); - out_int (2 * P->encr_chat.in_seq_no + (P->encr_chat.admin_id != tgl_state.our_id)); - out_int (2 * P->encr_chat.out_seq_no + (P->encr_chat.admin_id == tgl_state.our_id) - 2); + out_int (2 * P->encr_chat.in_seq_no + (P->encr_chat.admin_id != TLS->our_id)); + out_int (2 * P->encr_chat.out_seq_no + (P->encr_chat.admin_id == TLS->our_id) - 2); out_int (CODE_decrypted_message); } out_long (M->id); @@ -964,38 +942,38 @@ void tgl_do_send_encr_msg (struct tgl_message *M, void (*callback)(void *callbac out_int (CODE_decrypted_message_media_empty); encr_finish (&P->encr_chat); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M, callback, 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_state *TLS, struct tgl_message *M, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { if (tgl_get_peer_type (M->to_id) == TGL_PEER_ENCR_CHAT) { - tgl_do_send_encr_msg (M, callback, callback_extra); + tgl_do_send_encr_msg (TLS, M, callback, callback_extra); return; } clear_packet (); out_int (CODE_messages_send_message); - out_peer_id (M->to_id); + out_peer_id (TLS, M->to_id); out_cstring (M->message, M->message_len); out_long (M->id); long long *x = talloc (8); *x = M->id; - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_methods, x, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_methods, x, callback, 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 (struct tgl_state *TLS, tgl_peer_id_t id, const char *msg, int len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT) { - tgl_peer_t *P = tgl_peer_get (id); + tgl_peer_t *P = tgl_peer_get (TLS, id); if (!P) { vlogprintf (E_WARNING, "Unknown encrypted chat\n"); if (callback) { - ((void (*)(void *, int, struct tgl_message *))callback) (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } if (P->encr_chat.state != sc_ok) { vlogprintf (E_WARNING, "Chat is not yet initialized\n"); if (callback) { - ((void (*)(void *, int, struct tgl_message *))callback) (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } @@ -1003,21 +981,21 @@ void tgl_do_send_message (tgl_peer_id_t id, const char *msg, int len, void (*cal long long t; tglt_secure_random (&t, 8); vlogprintf (E_DEBUG, "t = %lld, len = %d\n", t, len); - bl_do_send_message_text (t, tgl_state.our_id, tgl_get_peer_type (id), tgl_get_peer_id (id), time (0), len, msg); - struct tgl_message *M = tgl_message_get (t); + bl_do_send_message_text (TLS, t, TLS->our_id, tgl_get_peer_type (id), tgl_get_peer_id (id), time (0), len, msg); + struct tgl_message *M = tgl_message_get (TLS, t); assert (M); - tgl_do_send_msg (M, callback, callback_extra); + tgl_do_send_msg (TLS, M, callback, callback_extra); //print_message (M); } /* }}} */ /* {{{ Send text file */ -void tgl_do_send_text (tgl_peer_id_t id, char *file_name, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void tgl_do_send_text (struct tgl_state *TLS, tgl_peer_id_t id, char *file_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { int fd = open (file_name, O_RDONLY); if (fd < 0) { vlogprintf (E_WARNING, "No such file '%s'\n", file_name); if (callback) { - ((void (*)(void *, int, struct tgl_message *))callback) (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } @@ -1028,11 +1006,11 @@ void tgl_do_send_text (tgl_peer_id_t id, char *file_name, void (*callback)(void vlogprintf (E_WARNING, "Too big file '%s'\n", file_name); close (fd); if (callback) { - ((void (*)(void *, int, struct tgl_message *))callback) (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } } else { buf[x] = 0; - tgl_do_send_message (id, buf, x, callback, callback_extra); + tgl_do_send_message (TLS, id, buf, x, callback, callback_extra); //tfree_str (file_name); close (fd); } @@ -1040,41 +1018,41 @@ void tgl_do_send_text (tgl_peer_id_t id, char *file_name, void (*callback)(void /* }}} */ /* {{{ Mark read */ -void tgl_do_messages_mark_read (tgl_peer_id_t id, int max_id, int offset, void (*callback)(void *callback_extra, int), void *callback_extra); -static int mark_read_on_receive (struct query *q UU) { +void tgl_do_messages_mark_read (struct tgl_state *TLS, tgl_peer_id_t id, int max_id, int offset, void (*callback)(struct tgl_state *TLS, void *callback_extra, int), void *callback_extra); +static int mark_read_on_receive (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == (int)CODE_messages_affected_history); //tglu_fetch_pts (); int pts = fetch_int (); //tglu_fetch_seq (); int seq = fetch_int (); // seq - if (seq == tgl_state.seq + 1 && !(tgl_state.locks & TGL_LOCK_DIFF)) { - bl_do_set_pts (pts); - bl_do_set_seq (seq); + if (seq == TLS->seq + 1 && !(TLS->locks & TGL_LOCK_DIFF)) { + bl_do_set_pts (TLS, pts); + bl_do_set_seq (TLS, seq); } else { - if (seq > tgl_state.seq + 1) { + if (seq > TLS->seq + 1) { vlogprintf (E_NOTICE, "Hole in seq\n"); - tgl_do_get_difference (0, 0, 0); + tgl_do_get_difference (TLS, 0, 0, 0); } } int offset = fetch_int (); // offset int *t = q->extra; if (offset > 0) { - tgl_do_messages_mark_read (tgl_set_peer_id (t[0], t[1]), t[2], offset, q->callback, q->callback_extra); + tgl_do_messages_mark_read (TLS, tgl_set_peer_id (t[0], t[1]), t[2], offset, q->callback, q->callback_extra); } else { if (q->callback) { - ((void (*)(void *, int))q->callback)(q->callback_extra, 1); + ((void (*)(struct tgl_state *, void *, int))q->callback)(TLS, q->callback_extra, 1); } } tfree (t, 12); return 0; } -static int mark_read_encr_on_receive (struct query *q UU) { +static int mark_read_encr_on_receive (struct tgl_state *TLS, struct query *q UU) { fetch_bool (); if (q->callback) { - ((void (*)(void *, int))q->callback)(q->callback_extra, 1); + ((void (*)(struct tgl_state *, void *, int))q->callback)(TLS, q->callback_extra, 1); } return 0; } @@ -1089,10 +1067,10 @@ static struct query_methods mark_read_encr_methods = { .type = TYPE_TO_PARAM(bool) }; -void tgl_do_messages_mark_read (tgl_peer_id_t id, int max_id, int offset, void (*callback)(void *callback_extra, int), void *callback_extra) { +void tgl_do_messages_mark_read (struct tgl_state *TLS, tgl_peer_id_t id, int max_id, int offset, void (*callback)(struct tgl_state *TLS, void *callback_extra, int), void *callback_extra) { clear_packet (); out_int (CODE_messages_read_history); - out_peer_id (id); + out_peer_id (TLS, id); out_int (max_id); out_int (offset); out_int (CODE_bool_true); @@ -1100,43 +1078,43 @@ void tgl_do_messages_mark_read (tgl_peer_id_t id, int max_id, int offset, void ( t[0] = tgl_get_peer_type (id); t[1] = tgl_get_peer_id (id); t[2] = max_id; - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &mark_read_methods, t, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &mark_read_methods, t, callback, callback_extra); } -void tgl_do_messages_mark_read_encr (tgl_peer_id_t id, long long access_hash, int last_time, void (*callback)(void *callback_extra, int), void *callback_extra) { +void tgl_do_messages_mark_read_encr (struct tgl_state *TLS, tgl_peer_id_t id, long long access_hash, int last_time, void (*callback)(struct tgl_state *TLS, void *callback_extra, int), void *callback_extra) { clear_packet (); out_int (CODE_messages_read_encrypted_history); out_int (CODE_input_encrypted_chat); out_int (tgl_get_peer_id (id)); out_long (access_hash); out_int (last_time); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &mark_read_encr_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &mark_read_encr_methods, 0, callback, callback_extra); } -void tgl_do_mark_read (tgl_peer_id_t id, void (*callback)(void *callback_extra, int success), void *callback_extra) { +void tgl_do_mark_read (struct tgl_state *TLS, tgl_peer_id_t id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra) { if (tgl_get_peer_type (id) == TGL_PEER_USER || tgl_get_peer_type (id) == TGL_PEER_CHAT) { - tgl_do_messages_mark_read (id, 0, 0, callback, callback_extra); + tgl_do_messages_mark_read (TLS, id, 0, 0, callback, callback_extra); return; } - tgl_peer_t *P = tgl_peer_get (id); + tgl_peer_t *P = tgl_peer_get (TLS, id); if (!P) { vlogprintf (E_WARNING, "Unknown peer\n"); - callback (callback_extra, 0); + callback (TLS, callback_extra, 0); return; } assert (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT); if (P->last) { - tgl_do_messages_mark_read_encr (id, P->encr_chat.access_hash, P->last->date, callback, callback_extra); + tgl_do_messages_mark_read_encr (TLS, id, P->encr_chat.access_hash, P->last->date, callback, callback_extra); } else { - tgl_do_messages_mark_read_encr (id, P->encr_chat.access_hash, time (0) - 10, callback, callback_extra); + tgl_do_messages_mark_read_encr (TLS, id, P->encr_chat.access_hash, time (0) - 10, callback, callback_extra); } } /* }}} */ /* {{{ Get history */ -void _tgl_do_get_history (tgl_peer_id_t id, int limit, int offset, int max_id, int list_offset, int list_size, struct tgl_message *ML[], void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra); -static int get_history_on_answer (struct query *q UU) { +void _tgl_do_get_history (struct tgl_state *TLS, tgl_peer_id_t id, int limit, int offset, int max_id, int list_offset, int list_size, struct tgl_message *ML[], void (*callback)(struct tgl_state *TLS,void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra); +static int get_history_on_answer (struct tgl_state *TLS, struct query *q UU) { int count = -1; int i; int x = fetch_int (); @@ -1168,7 +1146,7 @@ static int get_history_on_answer (struct query *q UU) { } //struct tgl_message **ML = talloc (sizeof (void *) * n); for (i = 0; i < n; i++) { - ML[i + list_offset] = tglf_fetch_alloc_message (); + ML[i + list_offset] = tglf_fetch_alloc_message (TLS); } list_offset += n; offset += n; @@ -1182,27 +1160,27 @@ static int get_history_on_answer (struct query *q UU) { assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_chat (); + tglf_fetch_alloc_chat (TLS); } assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_user (); + tglf_fetch_alloc_user (TLS); } if (limit <= 0 || x == (int)CODE_messages_messages) { if (q->callback) { - ((void (*)(void *, int, int, struct tgl_message **))q->callback) (q->callback_extra, 1, list_offset, ML); + ((void (*)(struct tgl_state *TLS, void *, int, int, struct tgl_message **))q->callback) (TLS, q->callback_extra, 1, list_offset, ML); } if (list_offset > 0) { - tgl_do_messages_mark_read (id, ML[0]->id, 0, 0, 0); + tgl_do_messages_mark_read (TLS, id, ML[0]->id, 0, 0, 0); } tfree (ML, sizeof (void *) * list_size); } else { - _tgl_do_get_history (id, limit, 0, ML[list_offset - 1]->id, list_offset, list_size, ML, q->callback, q->callback_extra); + _tgl_do_get_history (TLS, id, limit, 0, ML[list_offset - 1]->id, list_offset, list_size, ML, q->callback, q->callback_extra); } return 0; } @@ -1212,11 +1190,11 @@ static struct query_methods get_history_methods = { .type = TYPE_TO_PARAM(messages_messages) }; -void tgl_do_get_local_history (tgl_peer_id_t id, int limit, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { - tgl_peer_t *P = tgl_peer_get (id); +void tgl_do_get_local_history (struct tgl_state *TLS, tgl_peer_id_t id, int limit, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { + tgl_peer_t *P = tgl_peer_get (TLS, id); if (!P || !P->last) { if (callback) { - callback (callback_extra, 0, 0, 0); + callback (TLS, callback_extra, 0, 0, 0); } return; } @@ -1237,16 +1215,16 @@ void tgl_do_get_local_history (tgl_peer_id_t id, int limit, void (*callback)(voi } if (callback) { - callback (callback_extra, 1, count, ML); + callback (TLS, callback_extra, 1, count, ML); } tfree (ML, sizeof (void *) * count); } -void tgl_do_get_local_history_ext (tgl_peer_id_t id, int offset, int limit, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { - tgl_peer_t *P = tgl_peer_get (id); +void tgl_do_get_local_history_ext (struct tgl_state *TLS, tgl_peer_id_t id, int offset, int limit, void (*callback)(struct tgl_state *TLS,void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { + tgl_peer_t *P = tgl_peer_get (TLS, id); if (!P || !P->last) { if (callback) { - callback (callback_extra, 0, 0, 0); + callback (TLS, callback_extra, 0, 0, 0); } return; } @@ -1259,7 +1237,7 @@ void tgl_do_get_local_history_ext (tgl_peer_id_t id, int offset, int limit, void } if (count <= offset) { if (callback) { - callback (callback_extra, 1, 0, 0); + callback (TLS, callback_extra, 1, 0, 0); } return; } @@ -1276,14 +1254,14 @@ void tgl_do_get_local_history_ext (tgl_peer_id_t id, int offset, int limit, void } if (callback) { - callback (callback_extra, 1, count - offset, ML); + callback (TLS, callback_extra, 1, count - offset, ML); } tfree (ML, sizeof (void *) * (count) - offset); } -void _tgl_do_get_history (tgl_peer_id_t id, int limit, int offset, int max_id, int list_offset, int list_size, struct tgl_message *ML[], void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { +void _tgl_do_get_history (struct tgl_state *TLS, tgl_peer_id_t id, int limit, int offset, int max_id, int list_offset, int list_size, struct tgl_message *ML[], void (*callback)(struct tgl_state *TLS,void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { void **T = talloc (sizeof (void *) * 7); T[0] = ML; T[1] = (void *)(long)list_offset; @@ -1295,34 +1273,34 @@ void _tgl_do_get_history (tgl_peer_id_t id, int limit, int offset, int max_id, i clear_packet (); out_int (CODE_messages_get_history); - out_peer_id (id); + out_peer_id (TLS, id); out_int (offset); out_int (max_id); out_int (limit); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_history_methods, T, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &get_history_methods, T, callback, callback_extra); } -void tgl_do_get_history (tgl_peer_id_t id, int limit, int offline_mode, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { +void tgl_do_get_history (struct tgl_state *TLS, tgl_peer_id_t id, int limit, int offline_mode, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT || offline_mode) { - tgl_do_get_local_history (id, limit, callback, callback_extra); - tgl_do_mark_read (id, 0, 0); + tgl_do_get_local_history (TLS, id, limit, callback, callback_extra); + tgl_do_mark_read (TLS, id, 0, 0); return; } - _tgl_do_get_history (id, limit, 0, 0, 0, 0, 0, callback, callback_extra); + _tgl_do_get_history (TLS, id, limit, 0, 0, 0, 0, 0, callback, callback_extra); } -void tgl_do_get_history_ext (tgl_peer_id_t id, int offset, int limit, int offline_mode, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { +void tgl_do_get_history_ext (struct tgl_state *TLS, tgl_peer_id_t id, int offset, int limit, int offline_mode, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT || offline_mode) { - tgl_do_get_local_history (id, limit, callback, callback_extra); - tgl_do_mark_read (id, 0, 0); + tgl_do_get_local_history (TLS, id, limit, callback, callback_extra); + tgl_do_mark_read (TLS, id, 0, 0); return; } - _tgl_do_get_history (id, limit, offset, 0, 0, 0, 0, callback, callback_extra); + _tgl_do_get_history (TLS, id, limit, offset, 0, 0, 0, 0, callback, callback_extra); } /* }}} */ /* {{{ Get dialogs */ -static int get_dialogs_on_answer (struct query *q UU) { +static int get_dialogs_on_answer (struct tgl_state *TLS, struct query *q UU) { unsigned x = fetch_int (); assert (x == CODE_messages_dialogs || x == CODE_messages_dialogs_slice); if (x == CODE_messages_dialogs_slice) { @@ -1338,7 +1316,7 @@ static int get_dialogs_on_answer (struct query *q UU) { int *LM = talloc0 (4 * n); for (i = 0; i < n; i++) { assert (fetch_int () == (int)CODE_dialog); - PL[i] = tglf_fetch_peer_id (); + PL[i] = tglf_fetch_peer_id (TLS); LM[i] = fetch_int (); UC[i] = fetch_int (); assert (skip_type_any (TYPE_TO_PARAM (peer_notify_settings)) >= 0); @@ -1346,17 +1324,17 @@ static int get_dialogs_on_answer (struct query *q UU) { assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_message (); + tglf_fetch_alloc_message (TLS); } assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_chat (); + tglf_fetch_alloc_chat (TLS); } assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_user (); + tglf_fetch_alloc_user (TLS); } /*print_start (); push_color (COLOR_YELLOW); @@ -1364,13 +1342,13 @@ static int get_dialogs_on_answer (struct query *q UU) { tgl_peer_t *UC; switch (tgl_get_peer_type (plist[i])) { case TGL_PEER_USER: - UC = tgl_peer_get (plist[i]); + UC = tgl_peer_get (TLS, plist[i]); printf ("User "); print_user_name (plist[i], UC); printf (": %d unread\n", dlist[2 * i + 1]); break; case TGL_PEER_CHAT: - UC = tgl_peer_get (plist[i]); + UC = tgl_peer_get (TLS, plist[i]); printf ("Chat "); print_chat_name (plist[i], UC); printf (": %d unread\n", dlist[2 * i + 1]); @@ -1383,7 +1361,7 @@ static int get_dialogs_on_answer (struct query *q UU) { dialog_list_got = 1;*/ if (q->callback) { - ((void (*)(void *, int, int, tgl_peer_id_t *, int *, int *))q->callback) (q->callback_extra, 1, dl_size, PL, LM, UC); + ((void (*)(struct tgl_state *TLS, void *, int, int, tgl_peer_id_t *, int *, int *))q->callback) (TLS, q->callback_extra, 1, dl_size, PL, LM, UC); } tfree (PL, sizeof (tgl_peer_id_t) * dl_size); tfree (UC, 4 * dl_size); @@ -1398,13 +1376,13 @@ static struct query_methods get_dialogs_methods = { }; -void tgl_do_get_dialog_list (void (*callback)(void *callback_extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]), void *callback_extra) { +void tgl_do_get_dialog_list (struct tgl_state *TLS, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]), void *callback_extra) { clear_packet (); out_int (CODE_messages_get_dialogs); out_int (0); out_int (0); out_int (1000); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dialogs_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dialogs_methods, 0, callback, callback_extra); } /* }}} */ @@ -1429,7 +1407,7 @@ struct send_file { unsigned char *key; }; -static void out_peer_id (tgl_peer_id_t id) { +static void out_peer_id (struct tgl_state *TLS, tgl_peer_id_t id) { tgl_peer_t *U; switch (tgl_get_peer_type (id)) { case TGL_PEER_CHAT: @@ -1437,7 +1415,7 @@ static void out_peer_id (tgl_peer_id_t id) { out_int (tgl_get_peer_id (id)); break; case TGL_PEER_USER: - U = tgl_peer_get (id); + U = tgl_peer_get (TLS, id); if (U && U->user.access_hash) { out_int (CODE_input_peer_foreign); out_int (tgl_get_peer_id (id)); @@ -1452,50 +1430,50 @@ static void out_peer_id (tgl_peer_id_t id) { } } -static void send_part (struct send_file *f, void *callback, void *callback_extra); -static int send_file_part_on_answer (struct query *q) { +static void send_part (struct tgl_state *TLS, struct send_file *f, void *callback, void *callback_extra); +static int send_file_part_on_answer (struct tgl_state *TLS, struct query *q) { assert (fetch_int () == (int)CODE_bool_true); - send_part (q->extra, q->callback, q->callback_extra); + send_part (TLS, q->extra, q->callback, q->callback_extra); return 0; } -static int send_file_on_answer (struct query *q UU) { +static int send_file_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == (int)CODE_messages_stated_message); - struct tgl_message *M = tglf_fetch_alloc_message (); + struct tgl_message *M = tglf_fetch_alloc_message (TLS); assert (fetch_int () == CODE_vector); int n, i; n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_chat (); + tglf_fetch_alloc_chat (TLS); } assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_user (); + tglf_fetch_alloc_user (TLS); } //tglu_fetch_pts (); int pts = fetch_int (); //tglu_fetch_seq (); int seq = fetch_int (); - if (seq == tgl_state.seq + 1 && !(tgl_state.locks & TGL_LOCK_DIFF)) { - bl_do_set_pts (pts); - bl_do_msg_seq_update (M->id); + if (seq == TLS->seq + 1 && !(TLS->locks & TGL_LOCK_DIFF)) { + bl_do_set_pts (TLS, pts); + bl_do_msg_seq_update (TLS, M->id); } else { - if (seq > tgl_state.seq + 1) { + if (seq > TLS->seq + 1) { vlogprintf (E_NOTICE, "Hole in seq\n"); - tgl_do_get_difference (0, 0, 0); + tgl_do_get_difference (TLS, 0, 0, 0); } } if (q->callback) { - ((void (*)(void *, int, struct tgl_message *))q->callback)(q->callback_extra, 1, M); + ((void (*)(struct tgl_state *, void *, int, struct tgl_message *))q->callback)(TLS, q->callback_extra, 1, M); } //print_message (M); return 0; } -static int send_encr_file_on_answer (struct query *q UU) { +static int send_encr_file_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == (int)CODE_messages_sent_encrypted_file); struct tgl_message *M = q->extra; //M->date = fetch_int (); @@ -1506,21 +1484,21 @@ static int send_encr_file_on_answer (struct query *q UU) { //print_message (M); if (M->flags & FLAG_PENDING) { - bl_do_create_message_media_encr_sent (M->id, save, in_ptr - save); + bl_do_create_message_media_encr_sent (TLS, M->id, save, in_ptr - save); //bl_do_set_message_sent (M); - bl_do_msg_update (M->id); + bl_do_msg_update (TLS, M->id); } if (q->callback) { - ((void (*)(void *, int, struct tgl_message *))q->callback)(q->callback_extra, 1, M); + ((void (*)(struct tgl_state *, void *, int, struct tgl_message *))q->callback)(TLS, q->callback_extra, 1, M); } return 0; } -static int set_photo_on_answer (struct query *q) { +static int set_photo_on_answer (struct tgl_state *TLS, struct query *q) { assert (skip_type_any (TYPE_TO_PARAM(photos_photo)) >= 0); if (q->callback) { - ((void (*)(void *, int))q->callback)(q->callback_extra, 1); + ((void (*)(struct tgl_state *, void *, int))q->callback)(TLS, q->callback_extra, 1); } return 0; } @@ -1545,10 +1523,10 @@ static struct query_methods send_encr_file_methods = { .type = TYPE_TO_PARAM(messages_sent_encrypted_message) }; -static void send_part (struct send_file *f, void *callback, void *callback_extra) { +static void send_part (struct tgl_state *TLS, struct send_file *f, void *callback, void *callback_extra) { if (f->fd >= 0) { if (!f->part_num) { - tgl_state.cur_uploading_bytes += f->size; + TLS->cur_uploading_bytes += f->size; } clear_packet (); if (f->size < (16 << 20)) { @@ -1565,7 +1543,7 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra int x = read (f->fd, buf, f->part_size); assert (x > 0); f->offset += x; - tgl_state.cur_uploaded_bytes += x; + TLS->cur_uploaded_bytes += x; if (f->encr) { if (x & 15) { @@ -1588,10 +1566,10 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra assert (f->part_size == x); } //update_prompt (); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_part_methods, f, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_part_methods, f, callback, callback_extra); } else { - tgl_state.cur_uploaded_bytes -= f->size; - tgl_state.cur_uploading_bytes -= f->size; + TLS->cur_uploaded_bytes -= f->size; + TLS->cur_uploading_bytes -= f->size; //update_prompt (); clear_packet (); assert (f->media_type == CODE_input_media_uploaded_photo || f->media_type == CODE_input_media_uploaded_video || f->media_type == CODE_input_media_uploaded_thumb_video || f->media_type == CODE_input_media_uploaded_audio || f->media_type == CODE_input_media_uploaded_document || f->media_type == CODE_input_media_uploaded_thumb_document); @@ -1616,7 +1594,7 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra out_string (""); } out_int (CODE_input_photo_crop_auto); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_methods, 0, callback, callback_extra); } else { out_int (CODE_photos_upload_profile_photo); if (f->size < (16 << 20)) { @@ -1635,11 +1613,11 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra out_string ("profile photo"); out_int (CODE_input_geo_point_empty); out_int (CODE_input_photo_crop_auto); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &set_photo_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &set_photo_methods, 0, callback, callback_extra); } } else if (!f->encr) { out_int (CODE_messages_send_media); - out_peer_id (f->to_id); + out_peer_id (TLS, f->to_id); out_int (f->media_type); if (f->size < (16 << 20)) { out_int (CODE_input_file); @@ -1679,14 +1657,14 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra long long r; tglt_secure_random (&r, 8); out_long (r); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_methods, 0, callback, callback_extra); } else { //struct tgl_message *M = talloc0 (sizeof (*M)); out_int (CODE_messages_send_encrypted_file); out_int (CODE_input_encrypted_chat); out_int (tgl_get_peer_id (f->to_id)); - tgl_peer_t *P = tgl_peer_get (f->to_id); + tgl_peer_t *P = tgl_peer_get (TLS, f->to_id); assert (P); out_long (P->encr_chat.access_hash); long long r; @@ -1699,8 +1677,8 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra out_int (CODE_decrypted_message_layer); out_random (15 + 4 * (lrand48 () % 3)); out_int (TGL_ENCRYPTED_LAYER); - out_int (2 * P->encr_chat.in_seq_no + (P->encr_chat.admin_id != tgl_state.our_id)); - out_int (2 * P->encr_chat.out_seq_no + (P->encr_chat.admin_id == tgl_state.our_id)); + out_int (2 * P->encr_chat.in_seq_no + (P->encr_chat.admin_id != TLS->our_id)); + out_int (2 * P->encr_chat.out_seq_no + (P->encr_chat.admin_id == TLS->our_id)); out_int (CODE_decrypted_message); } out_long (r); @@ -1751,7 +1729,7 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra out_cstring ((void *)f->key, 32); out_cstring ((void *)f->init_iv, 32); - bl_do_create_message_media_encr_pending (r, tgl_state.our_id, tgl_get_peer_type (f->to_id), tgl_get_peer_id (f->to_id), time (0), 0, 0, save_ptr, packet_ptr - save_ptr); + bl_do_create_message_media_encr_pending (TLS, r, TLS->our_id, tgl_get_peer_type (f->to_id), tgl_get_peer_id (f->to_id), time (0), 0, 0, save_ptr, packet_ptr - save_ptr); encr_finish (&P->encr_chat); if (f->size < (16 << 20)) { @@ -1773,7 +1751,7 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra out_int ((*(int *)md5) ^ (*(int *)(md5 + 4))); tfree_secure (f->iv, 32); - struct tgl_message *M = tgl_message_get (r); + struct tgl_message *M = tgl_message_get (TLS, r); assert (M); //M->media.encr_photo.key = f->key; @@ -1782,7 +1760,7 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra //M->media.encr_photo.size = f->size; //M->flags = FLAG_ENCRYPTED; - //M->from_id = TGL_MK_USER (tgl_state.our_id); + //M->from_id = TGL_MK_USER (TLS->our_id); //M->to_id = f->to_id; //M->unread = 1; //M->message = tstrdup (""); @@ -1790,7 +1768,7 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra //M->id = r; //M->date = time (0); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_file_methods, M, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_file_methods, M, callback, callback_extra); } tfree_str (f->file_name); tfree (f, sizeof (*f)); @@ -1804,15 +1782,15 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra out_long (f->thumb_id); out_int (0); out_cstring ((void *)thumb_file, thumb_file_size); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_part_methods, f, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_part_methods, f, callback, callback_extra); }*/ -void _tgl_do_send_photo (enum tgl_message_media_type type, tgl_peer_id_t to_id, char *file_name, int avatar, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void _tgl_do_send_photo (struct tgl_state *TLS, enum tgl_message_media_type type, tgl_peer_id_t to_id, char *file_name, int avatar, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { int fd = open (file_name, O_RDONLY); if (fd < 0) { vlogprintf (E_WARNING, "No such file '%s'\n", file_name); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } @@ -1823,7 +1801,7 @@ void _tgl_do_send_photo (enum tgl_message_media_type type, tgl_peer_id_t to_id, vlogprintf (E_WARNING, "File has zero length\n"); close (fd); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } @@ -1844,7 +1822,7 @@ void _tgl_do_send_photo (enum tgl_message_media_type type, tgl_peer_id_t to_id, vlogprintf (E_WARNING, "Too big file. Maximal supported size is %d.\n", (512 << 10) * 1000); tfree (f, sizeof (*f)); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } @@ -1869,7 +1847,7 @@ void _tgl_do_send_photo (enum tgl_message_media_type type, tgl_peer_id_t to_id, vlogprintf (E_WARNING, "Unknown type %d.\n", type); tfree (f, sizeof (*f)); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } @@ -1892,29 +1870,29 @@ void _tgl_do_send_photo (enum tgl_message_media_type type, tgl_peer_id_t to_id, } else { send_part (f); }*/ - send_part (f, callback, callback_extra); + send_part (TLS, f, callback, callback_extra); } -void tgl_do_send_photo (enum tgl_message_media_type type, tgl_peer_id_t to_id, char *file_name, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { - _tgl_do_send_photo (type, to_id, file_name, 0, callback, callback_extra); +void tgl_do_send_photo (struct tgl_state *TLS, enum tgl_message_media_type type, tgl_peer_id_t to_id, char *file_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { + _tgl_do_send_photo (TLS, type, to_id, file_name, 0, callback, callback_extra); } -void tgl_do_set_chat_photo (tgl_peer_id_t chat_id, char *file_name, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void tgl_do_set_chat_photo (struct tgl_state *TLS, tgl_peer_id_t chat_id, char *file_name, void (*callback)(struct tgl_state *TLS,void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { assert (tgl_get_peer_type (chat_id) == TGL_PEER_CHAT); - _tgl_do_send_photo (tgl_message_media_photo, chat_id, file_name, tgl_get_peer_id (chat_id), callback, callback_extra); + _tgl_do_send_photo (TLS, tgl_message_media_photo, chat_id, file_name, tgl_get_peer_id (chat_id), callback, callback_extra); } -void tgl_do_set_profile_photo (char *file_name, void (*callback)(void *callback_extra, int success), void *callback_extra) { - _tgl_do_send_photo (tgl_message_media_photo, TGL_MK_USER(tgl_state.our_id), file_name, -1, (void *)callback, callback_extra); +void tgl_do_set_profile_photo (struct tgl_state *TLS, char *file_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra) { + _tgl_do_send_photo (TLS, tgl_message_media_photo, TGL_MK_USER(TLS->our_id), file_name, -1, (void *)callback, callback_extra); } /* }}} */ /* {{{ Profile name */ -int set_profile_name_on_answer (struct query *q) { - struct tgl_user *U = tglf_fetch_alloc_user (); +int set_profile_name_on_answer (struct tgl_state *TLS, struct query *q) { + struct tgl_user *U = tglf_fetch_alloc_user (TLS); if (q->callback) { - ((void (*)(void *, int, struct tgl_user *))q->callback) (q->callback_extra, 1, U); + ((void (*)(struct tgl_state *, void *, int, struct tgl_user *))q->callback) (TLS, q->callback_extra, 1, U); } return 0; } @@ -1924,27 +1902,27 @@ static struct query_methods set_profile_name_methods = { .type = TYPE_TO_PARAM(user) }; -void tgl_do_set_profile_name (char *first_name, char *last_name, void (*callback)(void *callback_extra, int success, struct tgl_user *U), void *callback_extra) { +void tgl_do_set_profile_name (struct tgl_state *TLS, char *first_name, char *last_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *U), void *callback_extra) { clear_packet (); out_int (CODE_account_update_profile); out_string (first_name); out_string (last_name); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &set_profile_name_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &set_profile_name_methods, 0, callback, callback_extra); } -void tgl_do_set_username (char *name, void (*callback)(void *callback_extra, int success, struct tgl_user *U), void *callback_extra) { +void tgl_do_set_username (struct tgl_state *TLS, char *name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *U), void *callback_extra) { clear_packet (); out_int (CODE_account_update_username); out_string (name); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &set_profile_name_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &set_profile_name_methods, 0, callback, callback_extra); } /* }}} */ /* {{{ Contacts search */ -int contact_search_on_answer (struct query *q) { +int contact_search_on_answer (struct tgl_state *TLS, struct query *q) { assert (fetch_int () == CODE_contacts_found); assert (fetch_int () == CODE_vector); int n = fetch_int (); @@ -1958,11 +1936,11 @@ int contact_search_on_answer (struct query *q) { struct tgl_user **UL = talloc (sizeof (void *) * n); for (i = 0; i < n; i++) { - UL[i] = tglf_fetch_alloc_user (); + UL[i] = tglf_fetch_alloc_user (TLS); } if (q->callback) { - ((void (*)(void *, int, int, struct tgl_user **))q->callback) (q->callback_extra, 1, n, UL); + ((void (*)(struct tgl_state *,void *, int, int, struct tgl_user **))q->callback) (TLS, q->callback_extra, 1, n, UL); } tfree (UL, sizeof (void *) * n); return 0; @@ -1973,47 +1951,47 @@ static struct query_methods contact_search_methods = { .type = TYPE_TO_PARAM(contacts_found) }; -void tgl_do_contact_search (char *name, int limit, void (*callback)(void *callback_extra, int success, int cnt, struct tgl_user *U[]), void *callback_extra) { +void tgl_do_contact_search (struct tgl_state *TLS, char *name, int limit, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int cnt, struct tgl_user *U[]), void *callback_extra) { clear_packet (); out_int (CODE_contacts_search); out_string (name); out_int (limit); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &contact_search_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &contact_search_methods, 0, callback, callback_extra); } /* }}} */ /* {{{ Forward */ -static int fwd_msg_on_answer (struct query *q UU) { +static int fwd_msg_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == (int)CODE_messages_stated_message); - struct tgl_message *M = tglf_fetch_alloc_message (); + struct tgl_message *M = tglf_fetch_alloc_message (TLS); assert (fetch_int () == CODE_vector); int n, i; n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_chat (); + tglf_fetch_alloc_chat (TLS); } assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_user (); + tglf_fetch_alloc_user (TLS); } //tglu_fetch_pts (); int pts = fetch_int (); int seq = fetch_int (); - if (seq == tgl_state.seq + 1 && !(tgl_state.locks & TGL_LOCK_DIFF)) { - bl_do_set_pts (pts); - bl_do_msg_seq_update (M->id); + if (seq == TLS->seq + 1 && !(TLS->locks & TGL_LOCK_DIFF)) { + bl_do_set_pts (TLS, pts); + bl_do_msg_seq_update (TLS, M->id); } else { - if (seq > tgl_state.seq + 1) { + if (seq > TLS->seq + 1) { vlogprintf (E_NOTICE, "Hole in seq\n"); - tgl_do_get_difference (0, 0, 0); + tgl_do_get_difference (TLS, 0, 0, 0); } } //print_message (M); if (q->callback) { - ((void (*)(void *, int, struct tgl_message *))q->callback) (q->callback_extra, 1, M); + ((void (*)(struct tgl_state *, void *, int, struct tgl_message *))q->callback) (TLS, q->callback_extra, 1, M); } return 0; } @@ -2023,28 +2001,28 @@ static struct query_methods fwd_msg_methods = { .type = TYPE_TO_PARAM(messages_stated_message) }; -void tgl_do_forward_message (tgl_peer_id_t id, int n, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void tgl_do_forward_message (struct tgl_state *TLS, tgl_peer_id_t id, int n, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT) { vlogprintf (E_WARNING, "Can not forward messages from secret chat\n"); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } clear_packet (); out_int (CODE_messages_forward_message); - out_peer_id (id); + out_peer_id (TLS, id); out_int (n); long long r; tglt_secure_random (&r, 8); out_long (r); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &fwd_msg_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &fwd_msg_methods, 0, callback, callback_extra); } -void tgl_do_send_contact (tgl_peer_id_t id, const char *phone, int phone_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void tgl_do_send_contact (struct tgl_state *TLS, tgl_peer_id_t id, const char *phone, int phone_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT) { if (callback) { - ((void (*)(void *, int, struct tgl_message *))callback) (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } @@ -2054,49 +2032,49 @@ void tgl_do_send_contact (tgl_peer_id_t id, const char *phone, int phone_len, co clear_packet (); out_int (CODE_messages_send_media); - out_peer_id (id); + out_peer_id (TLS, id); out_int (CODE_input_media_contact); out_cstring (phone, phone_len); out_cstring (first_name, first_name_len); out_cstring (last_name, last_name_len); out_long (t); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &fwd_msg_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &fwd_msg_methods, 0, callback, callback_extra); } -void tgl_do_forward_media (tgl_peer_id_t id, int n, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void tgl_do_forward_media (struct tgl_state *TLS, tgl_peer_id_t id, int n, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT) { vlogprintf (E_WARNING, "Can not forward messages from secret chat\n"); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } - struct tgl_message *M = tgl_message_get (n); + struct tgl_message *M = tgl_message_get (TLS, n); if (!M) { vlogprintf (E_WARNING, "No such message\n"); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } if (M->flags & FLAG_ENCRYPTED) { vlogprintf (E_WARNING, "Can not forward media from encrypted message\n"); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } if (M->media.type != tgl_message_media_photo && M->media.type != tgl_message_media_video && M->media.type != tgl_message_media_audio && M->media.type != tgl_message_media_document) { vlogprintf (E_WARNING, "Can only forward photo/audio/video/document\n"); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } clear_packet (); out_int (CODE_messages_send_media); - out_peer_id (id); + out_peer_id (TLS, id); switch (M->media.type) { case tgl_message_media_photo: out_int (CODE_input_media_photo); @@ -2129,19 +2107,19 @@ void tgl_do_forward_media (tgl_peer_id_t id, int n, void (*callback)(void *callb tglt_secure_random (&r, 8); out_long (r); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &fwd_msg_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &fwd_msg_methods, 0, callback, callback_extra); } /* }}} */ /* {{{ Send location */ -void tgl_do_send_location(tgl_peer_id_t id, double latitude, double longitude, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void tgl_do_send_location(struct tgl_state *TLS, tgl_peer_id_t id, double latitude, double longitude, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT) { clear_packet (); out_int (CODE_messages_send_encrypted); out_int (CODE_input_encrypted_chat); out_int (tgl_get_peer_id (id)); - tgl_peer_t *P = tgl_peer_get (id); + tgl_peer_t *P = tgl_peer_get (TLS, id); assert (P); out_long (P->encr_chat.access_hash); @@ -2155,8 +2133,8 @@ void tgl_do_send_location(tgl_peer_id_t id, double latitude, double longitude, v out_int (CODE_decrypted_message_layer); out_random (15 + 4 * (lrand48 () % 3)); out_int (TGL_ENCRYPTED_LAYER); - out_int (2 * P->encr_chat.in_seq_no + (P->encr_chat.admin_id != tgl_state.our_id)); - out_int (2 * P->encr_chat.out_seq_no + (P->encr_chat.admin_id == tgl_state.our_id)); + out_int (2 * P->encr_chat.in_seq_no + (P->encr_chat.admin_id != TLS->our_id)); + out_int (2 * P->encr_chat.out_seq_no + (P->encr_chat.admin_id == TLS->our_id)); out_int (CODE_decrypted_message); } out_long (r); @@ -2171,14 +2149,14 @@ void tgl_do_send_location(tgl_peer_id_t id, double latitude, double longitude, v out_double (latitude); out_double (longitude); - bl_do_create_message_media_encr_pending (r, tgl_state.our_id, tgl_get_peer_type (id), tgl_get_peer_id (id), time (0), 0, 0, save_ptr, packet_ptr - save_ptr); + bl_do_create_message_media_encr_pending (TLS, r, TLS->our_id, tgl_get_peer_type (id), tgl_get_peer_id (id), time (0), 0, 0, save_ptr, packet_ptr - save_ptr); encr_finish (&P->encr_chat); - struct tgl_message *M = tgl_message_get (r); + struct tgl_message *M = tgl_message_get (TLS, r); assert (M); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M, callback, callback_extra); } else { long long t; tglt_secure_random (&t, 8); @@ -2186,49 +2164,49 @@ void tgl_do_send_location(tgl_peer_id_t id, double latitude, double longitude, v clear_packet (); out_int (CODE_messages_send_media); - out_peer_id (id); + out_peer_id (TLS, id); out_int (CODE_input_media_geo_point); out_int (CODE_input_geo_point); out_double (latitude); out_double (longitude); out_long (t); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &fwd_msg_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &fwd_msg_methods, 0, callback, callback_extra); } } /* }}} */ /* {{{ Rename chat */ -static int rename_chat_on_answer (struct query *q UU) { +static int rename_chat_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == (int)CODE_messages_stated_message); - struct tgl_message *M = tglf_fetch_alloc_message (); + struct tgl_message *M = tglf_fetch_alloc_message (TLS); assert (fetch_int () == CODE_vector); int n, i; n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_chat (); + tglf_fetch_alloc_chat (TLS); } assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_user (); + tglf_fetch_alloc_user (TLS); } //tglu_fetch_pts (); int pts = fetch_int (); int seq = fetch_int (); - if (seq == tgl_state.seq + 1 && !(tgl_state.locks & TGL_LOCK_DIFF)) { - bl_do_set_pts (pts); - bl_do_msg_seq_update (M->id); + if (seq == TLS->seq + 1 && !(TLS->locks & TGL_LOCK_DIFF)) { + bl_do_set_pts (TLS, pts); + bl_do_msg_seq_update (TLS, M->id); } else { - if (seq > tgl_state.seq + 1) { + if (seq > TLS->seq + 1) { vlogprintf (E_NOTICE, "Hole in seq\n"); - tgl_do_get_difference (0, 0, 0); + tgl_do_get_difference (TLS, 0, 0, 0); } } //print_message (M); if (q->callback) { - ((void (*)(void *, int, struct tgl_message *))q->callback) (q->callback_extra, 1, M); + ((void (*)(struct tgl_state *, void *, int, struct tgl_message *))q->callback) (TLS, q->callback_extra, 1, M); } return 0; } @@ -2238,13 +2216,13 @@ static struct query_methods rename_chat_methods = { .type = TYPE_TO_PARAM(messages_stated_message) }; -void tgl_do_rename_chat (tgl_peer_id_t id, char *name UU, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void tgl_do_rename_chat (struct tgl_state *TLS, tgl_peer_id_t id, char *name UU, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { clear_packet (); out_int (CODE_messages_edit_chat_title); assert (tgl_get_peer_type (id) == TGL_PEER_CHAT); out_int (tgl_get_peer_id (id)); out_string (name); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &rename_chat_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &rename_chat_methods, 0, callback, callback_extra); } /* }}} */ @@ -2259,9 +2237,9 @@ void tgl_do_rename_chat (tgl_peer_id_t id, char *name UU, void (*callback)(void int i; for (i = 0; i < C->user_list_size; i++) { printf ("\t\t"); - print_user_name (TGL_MK_USER (C->user_list[i].user_id), tgl_peer_get (TGL_MK_USER (C->user_list[i].user_id))); + print_user_name (TGL_MK_USER (C->user_list[i].user_id), tgl_peer_get (TLS, TGL_MK_USER (C->user_list[i].user_id))); printf (" invited by "); - print_user_name (TGL_MK_USER (C->user_list[i].inviter_id), tgl_peer_get (TGL_MK_USER (C->user_list[i].inviter_id))); + print_user_name (TGL_MK_USER (C->user_list[i].inviter_id), tgl_peer_get (TLS, TGL_MK_USER (C->user_list[i].inviter_id))); printf (" at "); print_date_full (C->user_list[i].date); if (C->user_list[i].user_id == C->admin_id) { @@ -2273,11 +2251,11 @@ void tgl_do_rename_chat (tgl_peer_id_t id, char *name UU, void (*callback)(void print_end (); }*/ -static int chat_info_on_answer (struct query *q UU) { - struct tgl_chat *C = tglf_fetch_alloc_chat_full (); +static int chat_info_on_answer (struct tgl_state *TLS, struct query *q UU) { + struct tgl_chat *C = tglf_fetch_alloc_chat_full (TLS); //print_chat_info (C); if (q->callback) { - ((void (*)(void *, int, struct tgl_chat *))q->callback) (q->callback_extra, 1, C); + ((void (*)(struct tgl_state *, void *, int, struct tgl_chat *))q->callback) (TLS, q->callback_extra, 1, C); } return 0; } @@ -2287,18 +2265,18 @@ static struct query_methods chat_info_methods = { .type = TYPE_TO_PARAM(messages_chat_full) }; -void tgl_do_get_chat_info (tgl_peer_id_t id, int offline_mode, void (*callback)(void *callback_extra, int success, struct tgl_chat *C), void *callback_extra) { +void tgl_do_get_chat_info (struct tgl_state *TLS, tgl_peer_id_t id, int offline_mode, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_chat *C), void *callback_extra) { if (offline_mode) { - tgl_peer_t *C = tgl_peer_get (id); + tgl_peer_t *C = tgl_peer_get (TLS, id); if (!C) { vlogprintf (E_WARNING, "No such chat\n"); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } } else { //print_chat_info (&C->chat); if (callback) { - callback (callback_extra, 1, &C->chat); + callback (TLS, callback_extra, 1, &C->chat); } } return; @@ -2307,7 +2285,7 @@ void tgl_do_get_chat_info (tgl_peer_id_t id, int offline_mode, void (*callback)( out_int (CODE_messages_get_full_chat); assert (tgl_get_peer_type (id) == TGL_PEER_CHAT); out_int (tgl_get_peer_id (id)); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &chat_info_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &chat_info_methods, 0, callback, callback_extra); } /* }}} */ @@ -2333,10 +2311,10 @@ void tgl_do_get_chat_info (tgl_peer_id_t id, int offline_mode, void (*callback)( print_end (); }*/ -static int user_info_on_answer (struct query *q UU) { - struct tgl_user *U = tglf_fetch_alloc_user_full (); +static int user_info_on_answer (struct tgl_state *TLS, struct query *q UU) { + struct tgl_user *U = tglf_fetch_alloc_user_full (TLS); if (q->callback) { - ((void (*)(void *, int, struct tgl_user *))q->callback) (q->callback_extra, 1, U); + ((void (*)(struct tgl_state *, void *, int, struct tgl_user *))q->callback) (TLS, q->callback_extra, 1, U); } return 0; } @@ -2346,17 +2324,17 @@ static struct query_methods user_info_methods = { .type = TYPE_TO_PARAM(user_full) }; -void tgl_do_get_user_info (tgl_peer_id_t id, int offline_mode, void (*callback)(void *callback_extra, int success, struct tgl_user *U), void *callback_extra) { +void tgl_do_get_user_info (struct tgl_state *TLS, tgl_peer_id_t id, int offline_mode, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *U), void *callback_extra) { if (offline_mode) { - tgl_peer_t *C = tgl_peer_get (id); + tgl_peer_t *C = tgl_peer_get (TLS, id); if (!C) { vlogprintf (E_WARNING, "No such user\n"); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } } else { if (callback) { - callback (callback_extra, 1, &C->user); + callback (TLS, callback_extra, 1, &C->user); } } return; @@ -2364,7 +2342,7 @@ void tgl_do_get_user_info (tgl_peer_id_t id, int offline_mode, void (*callback)( clear_packet (); out_int (CODE_users_get_full_user); assert (tgl_get_peer_type (id) == TGL_PEER_USER); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); if (U && U->user.access_hash) { out_int (CODE_input_user_foreign); out_int (tgl_get_peer_id (id)); @@ -2373,7 +2351,7 @@ void tgl_do_get_user_info (tgl_peer_id_t id, int offline_mode, void (*callback)( out_int (CODE_input_user_contact); out_int (tgl_get_peer_id (id)); } - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &user_info_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &user_info_methods, 0, callback, callback_extra); } /* }}} */ @@ -2383,7 +2361,7 @@ void tgl_do_get_user_info (tgl_peer_id_t id, int offline_mode, void (*callback)( int n = fetch_int (); int i; for (i = 0; i < n; i++) { - tglf_fetch_alloc_user (); + tglf_fetch_alloc_user (TLS); } return 0; } @@ -2404,7 +2382,7 @@ void tgl_do_get_user_list_info_silent (int num, int *list) { out_int (list[i]); //out_long (0); } - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &user_list_info_silent_methods, 0); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &user_list_info_silent_methods, 0); }*/ /* }}} */ @@ -2428,9 +2406,9 @@ struct download { }; -static void end_load (struct download *D, void *callback, void *callback_extra) { - tgl_state.cur_downloading_bytes -= D->size; - tgl_state.cur_downloaded_bytes -= D->size; +static void end_load (struct tgl_state *TLS, struct download *D, void *callback, void *callback_extra) { + TLS->cur_downloading_bytes -= D->size; + TLS->cur_downloaded_bytes -= D->size; //update_prompt (); close (D->fd); /*if (D->next == 1) { @@ -2449,7 +2427,7 @@ static void end_load (struct download *D, void *callback, void *callback_extra) }*/ if (callback) { - ((void (*)(void *, int, char *))callback) (callback_extra, 1, D->name); + ((void (*)(struct tgl_state *, void *, int, char *))callback) (TLS, callback_extra, 1, D->name); } if (D->iv) { @@ -2459,8 +2437,8 @@ static void end_load (struct download *D, void *callback, void *callback_extra) tfree (D, sizeof (*D)); } -static void load_next_part (struct download *D, void *callback, void *callback_extra); -static int download_on_answer (struct query *q) { +static void load_next_part (struct tgl_state *TLS, struct download *D, void *callback, void *callback_extra); +static int download_on_answer (struct tgl_state *TLS, struct query *q) { assert (fetch_int () == (int)CODE_upload_file); unsigned x = fetch_int (); assert (x); @@ -2475,7 +2453,7 @@ static int download_on_answer (struct query *q) { fetch_int (); // mtime int len = prefetch_strlen (); assert (len >= 0); - tgl_state.cur_downloaded_bytes += len; + TLS->cur_downloaded_bytes += len; //update_prompt (); if (D->iv) { unsigned char *ptr = (void *)fetch_str (len); @@ -2494,11 +2472,11 @@ static int download_on_answer (struct query *q) { D->offset += len; D->refcnt --; if (D->offset < D->size) { - load_next_part (D, q->callback, q->callback_extra); + load_next_part (TLS, D, q->callback, q->callback_extra); return 0; } else { if (!D->refcnt) { - end_load (D, q->callback, q->callback_extra); + end_load (TLS, D, q->callback, q->callback_extra); } return 0; } @@ -2509,7 +2487,7 @@ static struct query_methods download_methods = { .type = TYPE_TO_PARAM(upload_file) }; -static void load_next_part (struct download *D, void *callback, void *callback_extra) { +static void load_next_part (struct tgl_state *TLS, struct download *D, void *callback, void *callback_extra) { if (!D->offset) { static char buf[PATH_MAX]; int l; @@ -2527,16 +2505,16 @@ static void load_next_part (struct download *D, void *callback, void *callback_e if (stat (buf, &st) >= 0) { D->offset = st.st_size; if (D->offset >= D->size) { - tgl_state.cur_downloading_bytes += D->size; - tgl_state.cur_downloaded_bytes += D->offset; + TLS->cur_downloading_bytes += D->size; + TLS->cur_downloaded_bytes += D->offset; vlogprintf (E_NOTICE, "Already downloaded\n"); - end_load (D, callback, callback_extra); + end_load (TLS, D, callback, callback_extra); return; } } - tgl_state.cur_downloading_bytes += D->size; - tgl_state.cur_downloaded_bytes += D->offset; + TLS->cur_downloading_bytes += D->size; + TLS->cur_downloaded_bytes += D->offset; //update_prompt (); } D->refcnt ++; @@ -2558,15 +2536,15 @@ static void load_next_part (struct download *D, void *callback, void *callback_e } out_int (D->offset); out_int (1 << 14); - tglq_send_query (tgl_state.DC_list[D->dc], packet_ptr - packet_buffer, packet_buffer, &download_methods, D, callback, callback_extra); - //tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &download_methods, D); + tglq_send_query (TLS, TLS->DC_list[D->dc], packet_ptr - packet_buffer, packet_buffer, &download_methods, D, callback, callback_extra); + //tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &download_methods, D); } -void tgl_do_load_photo_size (struct tgl_photo_size *P, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra) { +void tgl_do_load_photo_size (struct tgl_state *TLS, struct tgl_photo_size *P, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra) { if (!P->loc.dc) { vlogprintf (E_WARNING, "Bad video thumb\n"); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } @@ -2582,14 +2560,14 @@ void tgl_do_load_photo_size (struct tgl_photo_size *P, void (*callback)(void *ca D->secret = P->loc.secret; D->name = 0; D->fd = -1; - load_next_part (D, callback, callback_extra); + load_next_part (TLS, D, callback, callback_extra); } -void tgl_do_load_photo (struct tgl_photo *photo, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra) { +void tgl_do_load_photo (struct tgl_state *TLS, struct tgl_photo *photo, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra) { if (!photo->sizes_num) { vlogprintf (E_WARNING, "No sizes\n"); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } @@ -2602,18 +2580,18 @@ void tgl_do_load_photo (struct tgl_photo *photo, void (*callback)(void *callback maxi = i; } } - tgl_do_load_photo_size (&photo->sizes[maxi], callback, callback_extra); + tgl_do_load_photo_size (TLS, &photo->sizes[maxi], callback, callback_extra); } -void tgl_do_load_video_thumb (struct tgl_video *video, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra) { - tgl_do_load_photo_size (&video->thumb, callback, callback_extra); +void tgl_do_load_video_thumb (struct tgl_state *TLS, struct tgl_video *video, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra) { + tgl_do_load_photo_size (TLS, &video->thumb, callback, callback_extra); } -void tgl_do_load_document_thumb (struct tgl_document *video, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra) { - tgl_do_load_photo_size (&video->thumb, callback, callback_extra); +void tgl_do_load_document_thumb (struct tgl_state *TLS, struct tgl_document *video, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra) { + tgl_do_load_photo_size (TLS, &video->thumb, callback, callback_extra); } -void tgl_do_load_video (struct tgl_video *V, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra) { +void tgl_do_load_video (struct tgl_state *TLS, struct tgl_video *V, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra) { assert (V); struct download *D = talloc0 (sizeof (*D)); D->offset = 0; @@ -2624,10 +2602,10 @@ void tgl_do_load_video (struct tgl_video *V, void (*callback)(void *callback_ext D->name = 0; D->fd = -1; D->type = CODE_input_video_file_location; - load_next_part (D, callback, callback_extra); + load_next_part (TLS, D, callback, callback_extra); } -void tgl_do_load_audio (struct tgl_audio *V, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra) { +void tgl_do_load_audio (struct tgl_state *TLS, struct tgl_audio *V, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra) { assert (V); struct download *D = talloc0 (sizeof (*D)); D->offset = 0; @@ -2638,10 +2616,10 @@ void tgl_do_load_audio (struct tgl_audio *V, void (*callback)(void *callback_ext D->name = 0; D->fd = -1; D->type = CODE_input_audio_file_location; - load_next_part (D, callback, callback_extra); + load_next_part (TLS, D, callback, callback_extra); } -void tgl_do_load_document (struct tgl_document *V, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra) { +void tgl_do_load_document (struct tgl_state *TLS, struct tgl_document *V, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra) { assert (V); struct download *D = talloc0 (sizeof (*D)); D->offset = 0; @@ -2652,10 +2630,10 @@ void tgl_do_load_document (struct tgl_document *V, void (*callback)(void *callba D->name = 0; D->fd = -1; D->type = CODE_input_document_file_location; - load_next_part (D, callback, callback_extra); + load_next_part (TLS, D, callback, callback_extra); } -void tgl_do_load_encr_video (struct tgl_encr_video *V, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra) { +void tgl_do_load_encr_video (struct tgl_state *TLS, struct tgl_encr_video *V, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra) { assert (V); struct download *D = talloc0 (sizeof (*D)); D->offset = 0; @@ -2668,7 +2646,7 @@ void tgl_do_load_encr_video (struct tgl_encr_video *V, void (*callback)(void *ca D->key = V->key; D->iv = talloc (32); memcpy (D->iv, V->iv, 32); - load_next_part (D, callback, callback_extra); + load_next_part (TLS, D, callback, callback_extra); unsigned char md5[16]; unsigned char str[64]; @@ -2681,15 +2659,15 @@ void tgl_do_load_encr_video (struct tgl_encr_video *V, void (*callback)(void *ca /* {{{ Export auth */ -static int import_auth_on_answer (struct query *q UU) { +static int import_auth_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == (int)CODE_auth_authorization); fetch_int (); // expires - tglf_fetch_alloc_user (); + tglf_fetch_alloc_user (TLS); - bl_do_dc_signed (((struct tgl_dc *)q->extra)->id); + bl_do_dc_signed (TLS, ((struct tgl_dc *)q->extra)->id); if (q->callback) { - ((void (*)(void *, int))q->callback) (q->callback_extra, 1); + ((void (*)(struct tgl_state *, void *, int))q->callback) (TLS, q->callback_extra, 1); } return 0; } @@ -2700,9 +2678,9 @@ static struct query_methods import_auth_methods = { .type = TYPE_TO_PARAM(auth_authorization) }; -static int export_auth_on_answer (struct query *q UU) { +static int export_auth_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == (int)CODE_auth_exported_authorization); - bl_do_set_our_id (fetch_int ()); + bl_do_set_our_id (TLS, fetch_int ()); int l = prefetch_strlen (); char *s = talloc (l); memcpy (s, fetch_str (l), l); @@ -2710,9 +2688,9 @@ static int export_auth_on_answer (struct query *q UU) { clear_packet (); tgl_do_insert_header (); out_int (CODE_auth_import_authorization); - out_int (tgl_state.our_id); + out_int (TLS->our_id); out_cstring (s, l); - tglq_send_query (q->extra, packet_ptr - packet_buffer, packet_buffer, &import_auth_methods, q->extra, q->callback, q->callback_extra); + tglq_send_query (TLS, q->extra, packet_ptr - packet_buffer, packet_buffer, &import_auth_methods, q->extra, q->callback, q->callback_extra); tfree (s, l); return 0; } @@ -2723,16 +2701,16 @@ static struct query_methods export_auth_methods = { .type = TYPE_TO_PARAM(auth_exported_authorization) }; -void tgl_do_export_auth (int num, void (*callback) (void *callback_extra, int success), void *callback_extra) { +void tgl_do_export_auth (struct tgl_state *TLS, int num, void (*callback) (struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra) { clear_packet (); out_int (CODE_auth_export_authorization); out_int (num); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &export_auth_methods, tgl_state.DC_list[num], callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &export_auth_methods, TLS->DC_list[num], callback, callback_extra); } /* }}} */ /* {{{ Add contact */ -static int add_contact_on_answer (struct query *q UU) { +static int add_contact_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == (int)CODE_contacts_imported_contacts); assert (fetch_int () == CODE_vector); int n = fetch_int (); @@ -2758,11 +2736,11 @@ static int add_contact_on_answer (struct query *q UU) { struct tgl_user **UL = talloc (n * sizeof (void *)); for (i = 0; i < n; i++) { - UL[i] = tglf_fetch_alloc_user (); + UL[i] = tglf_fetch_alloc_user (TLS); } if (q->callback) { - ((void (*)(void *, int, int, struct tgl_user **))q->callback) (q->callback_extra, 1, n, UL); + ((void (*)(struct tgl_state *, void *, int, int, struct tgl_user **))q->callback) (TLS, q->callback_extra, 1, n, UL); } tfree (UL, n * sizeof (void *)); return 0; @@ -2773,7 +2751,7 @@ static struct query_methods add_contact_methods = { .type = TYPE_TO_PARAM(contacts_imported_contacts) }; -void tgl_do_add_contact (const char *phone, int phone_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, int force, void (*callback)(void *callback_extra, int success, int size, struct tgl_user *users[]), void *callback_extra) { +void tgl_do_add_contact (struct tgl_state *TLS, const char *phone, int phone_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, int force, void (*callback)(struct tgl_state *TLS,void *callback_extra, int success, int size, struct tgl_user *users[]), void *callback_extra) { clear_packet (); out_int (CODE_contacts_import_contacts); out_int (CODE_vector); @@ -2786,16 +2764,16 @@ void tgl_do_add_contact (const char *phone, int phone_len, const char *first_nam out_cstring (first_name, first_name_len); out_cstring (last_name, last_name_len); out_int (force ? CODE_bool_true : CODE_bool_false); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &add_contact_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &add_contact_methods, 0, callback, callback_extra); } /* }}} */ /* {{{ Add contact */ -static int del_contact_on_answer (struct query *q UU) { +static int del_contact_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (skip_type_contacts_link (TYPE_TO_PARAM(contacts_link)) >= 0); if (q->callback) { - ((void (*)(void *, int))q->callback) (q->callback_extra, 1); + ((void (*)(struct tgl_state *, void *, int))q->callback) (TLS, q->callback_extra, 1); } return 0; } @@ -2805,17 +2783,17 @@ static struct query_methods del_contact_methods = { .type = TYPE_TO_PARAM(contacts_link) }; -void tgl_do_del_contact (tgl_peer_id_t id, void (*callback)(void *callback_extra, int success), void *callback_extra) { +void tgl_do_del_contact (struct tgl_state *TLS, tgl_peer_id_t id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra) { if (tgl_get_peer_type (id) != TGL_PEER_USER) { if (callback) { - callback (callback_extra, 0); + callback (TLS, callback_extra, 0); } return; } clear_packet (); out_int (CODE_contacts_delete_contact); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); if (U && U->user.access_hash) { out_int (CODE_input_user_foreign); out_int (tgl_get_peer_id (id)); @@ -2824,13 +2802,13 @@ void tgl_do_del_contact (tgl_peer_id_t id, void (*callback)(void *callback_extra out_int (CODE_input_user_contact); out_int (tgl_get_peer_id (id)); } - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &del_contact_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &del_contact_methods, 0, callback, callback_extra); } /* }}} */ /* {{{ Msg search */ -void _tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, int offset, int max_id, char *s, int list_offset, int list_size, struct tgl_message **list, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra); -static int msg_search_on_answer (struct query *q UU) { +void _tgl_do_msg_search (struct tgl_state *TLS, tgl_peer_id_t id, int from, int to, int limit, int offset, int max_id, char *s, int list_offset, int list_size, struct tgl_message **list, void (*callback)(struct tgl_state *TLS,void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra); +static int msg_search_on_answer (struct tgl_state *TLS, struct query *q UU) { int count = -1; int i; int x = fetch_int (); @@ -2865,7 +2843,7 @@ static int msg_search_on_answer (struct query *q UU) { } //struct tgl_message **ML = talloc (sizeof (void *) * n); for (i = 0; i < n; i++) { - ML[i + list_offset] = tglf_fetch_alloc_message (); + ML[i + list_offset] = tglf_fetch_alloc_message (TLS); } list_offset += n; offset += n; @@ -2879,24 +2857,24 @@ static int msg_search_on_answer (struct query *q UU) { assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_chat (); + tglf_fetch_alloc_chat (TLS); } assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_user (); + tglf_fetch_alloc_user (TLS); } if (limit <= 0 || x == (int)CODE_messages_messages) { if (q->callback) { - ((void (*)(void *, int, int, struct tgl_message **))q->callback) (q->callback_extra, 1, list_offset, ML); + ((void (*)(struct tgl_state *, void *, int, int, struct tgl_message **))q->callback) (TLS, q->callback_extra, 1, list_offset, ML); } tfree_str (s); tfree (ML, sizeof (void *) * list_size); } else { - _tgl_do_msg_search (id, from, to, limit, 0, ML[list_offset - 1]->id, s, list_offset, list_size, ML, q->callback, q->callback_extra); + _tgl_do_msg_search (TLS, id, from, to, limit, 0, ML[list_offset - 1]->id, s, list_offset, list_size, ML, q->callback, q->callback_extra); } return 0; } @@ -2906,11 +2884,11 @@ static struct query_methods msg_search_methods = { .type = TYPE_TO_PARAM(messages_messages) }; -void _tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, int offset, int max_id, char *s, int list_offset, int list_size, struct tgl_message **list, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { +void _tgl_do_msg_search (struct tgl_state *TLS, tgl_peer_id_t id, int from, int to, int limit, int offset, int max_id, char *s, int list_offset, int list_size, struct tgl_message **list, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT) { vlogprintf (E_WARNING, "Can not search in secure chat\n"); if (callback) { - callback (callback_extra, 0, 0, 0); + callback (TLS, callback_extra, 0, 0, 0); } return; } @@ -2919,7 +2897,7 @@ void _tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, int offs if (tgl_get_peer_type (id) == TGL_PEER_UNKNOWN) { out_int (CODE_input_peer_empty); } else { - out_peer_id (id); + out_peer_id (TLS, id); } void **T = talloc (sizeof (void *) * 10); T[0] = list; @@ -2940,16 +2918,16 @@ void _tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, int offs out_int (offset); // offset out_int (max_id); // max_id out_int (limit); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_search_methods, T, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_search_methods, T, callback, callback_extra); } -void tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, int offset, const char *s, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { - _tgl_do_msg_search (id, from, to, limit, offset, 0, tstrdup (s), 0, 0, 0, callback, callback_extra); +void tgl_do_msg_search (struct tgl_state *TLS, tgl_peer_id_t id, int from, int to, int limit, int offset, const char *s, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) { + _tgl_do_msg_search (TLS, id, from, to, limit, offset, 0, tstrdup (s), 0, 0, 0, callback, callback_extra); } /* }}} */ /* {{{ Contacts search */ -static int contacts_search_on_answer (struct query *q UU) { +static int contacts_search_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == CODE_contacts_found); assert (fetch_int () == CODE_vector); int n = fetch_int (); @@ -2963,12 +2941,12 @@ static int contacts_search_on_answer (struct query *q UU) { struct tgl_user **UL = talloc (sizeof (void *) * n); for (i = 0; i < n; i++) { - UL[i] = tglf_fetch_alloc_user (); + UL[i] = tglf_fetch_alloc_user (TLS); } /*print_start (); push_color (COLOR_YELLOW); for (i = 0; i < n; i++) { - struct tgl_user *U = tglf_fetch_alloc_user (); + struct tgl_user *U = tglf_fetch_alloc_user (TLS); printf ("User "); push_color (COLOR_RED); printf ("%s %s", U->first_name, U->last_name); @@ -2978,7 +2956,7 @@ static int contacts_search_on_answer (struct query *q UU) { pop_color (); print_end ();*/ if (q->callback) { - ((void (*)(void *, int, int, struct tgl_user **))q->callback) (q->callback_extra, 1, n, UL); + ((void (*)(struct tgl_state *, void *, int, int, struct tgl_user **))q->callback) (TLS, q->callback_extra, 1, n, UL); } tfree (UL, sizeof (void *) * n); return 0; @@ -2989,18 +2967,18 @@ static struct query_methods contacts_search_methods = { .type = TYPE_TO_PARAM(contacts_found) }; -void tgl_do_contacts_search (int limit, const char *s, void (*callback) (void *callback_extra, int success, int size, struct tgl_user *users[]), void *callback_extra) { +void tgl_do_contacts_search (struct tgl_state *TLS, int limit, const char *s, void (*callback) (struct tgl_state *, void *callback_extra, int success, int size, struct tgl_user *users[]), void *callback_extra) { clear_packet (); out_int (CODE_contacts_search); out_string (s); out_int (limit); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &contacts_search_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &contacts_search_methods, 0, callback, callback_extra); } /* }}} */ /* {{{ Encr accept */ -static int send_encr_accept_on_answer (struct query *q UU) { - struct tgl_secret_chat *E = tglf_fetch_alloc_encrypted_chat (); +static int send_encr_accept_on_answer (struct tgl_state *TLS, struct query *q UU) { + struct tgl_secret_chat *E = tglf_fetch_alloc_encrypted_chat (TLS); /*if (E->state == sc_ok) { print_start (); @@ -3021,16 +2999,16 @@ static int send_encr_accept_on_answer (struct query *q UU) { }*/ if (E->state == sc_ok) { - tgl_do_send_encr_chat_layer (E); + tgl_do_send_encr_chat_layer (TLS, E); } if (q->callback) { - ((void (*)(void *, int, struct tgl_secret_chat *))q->callback) (q->callback_extra, E->state == sc_ok, E); + ((void (*)(struct tgl_state *, void *, int, struct tgl_secret_chat *))q->callback) (TLS, q->callback_extra, E->state == sc_ok, E); } return 0; } -static int send_encr_request_on_answer (struct query *q UU) { - struct tgl_secret_chat *E = tglf_fetch_alloc_encrypted_chat (); +static int send_encr_request_on_answer (struct tgl_state *TLS, struct query *q UU) { + struct tgl_secret_chat *E = tglf_fetch_alloc_encrypted_chat (TLS); /*if (E->state == sc_deleted) { print_start (); push_color (COLOR_YELLOW); @@ -3052,7 +3030,7 @@ static int send_encr_request_on_answer (struct query *q UU) { }*/ if (q->callback) { - ((void (*)(void *, int, struct tgl_secret_chat *))q->callback) (q->callback_extra, E->state != sc_deleted, E); + ((void (*)(struct tgl_state *, void *, int, struct tgl_secret_chat *))q->callback) (TLS, q->callback_extra, E->state != sc_deleted, E); } return 0; } @@ -3072,7 +3050,7 @@ static struct query_methods send_encr_request_methods = { //int encr_param_version; //static BN_CTX *ctx; -void tgl_do_send_accept_encr_chat (struct tgl_secret_chat *E, unsigned char *random, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra) { +void tgl_do_send_accept_encr_chat (struct tgl_state *TLS, struct tgl_secret_chat *E, unsigned char *random, void (*callback)(struct tgl_state *TLS,void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra) { int i; int ok = 0; for (i = 0; i < 64; i++) { @@ -3083,12 +3061,12 @@ void tgl_do_send_accept_encr_chat (struct tgl_secret_chat *E, unsigned char *ran } if (ok) { if (callback) { - callback (callback_extra, 1, E); + callback (TLS, callback_extra, 1, E); } return; } // Already generated key for this chat assert (E->g_key); - assert (tgl_state.BN_ctx); + assert (TLS->BN_ctx); unsigned char random_here[256]; tglt_secure_random (random_here, 256); for (i = 0; i < 256; i++) { @@ -3098,16 +3076,16 @@ void tgl_do_send_accept_encr_chat (struct tgl_secret_chat *E, unsigned char *ran ensure_ptr (b); BIGNUM *g_a = BN_bin2bn (E->g_key, 256, 0); ensure_ptr (g_a); - assert (tglmp_check_g (tgl_state.encr_prime, g_a) >= 0); + assert (tglmp_check_g (TLS, TLS->encr_prime, g_a) >= 0); //if (!ctx) { // ctx = BN_CTX_new (); // ensure_ptr (ctx); //} - BIGNUM *p = BN_bin2bn (tgl_state.encr_prime, 256, 0); + BIGNUM *p = BN_bin2bn (TLS->encr_prime, 256, 0); ensure_ptr (p); BIGNUM *r = BN_new (); ensure_ptr (r); - ensure (BN_mod_exp (r, g_a, b, p, tgl_state.BN_ctx)); + ensure (BN_mod_exp (r, g_a, b, p, TLS->BN_ctx)); static unsigned char kk[256]; memset (kk, 0, sizeof (kk)); BN_bn2bin (r, kk + (256 - BN_num_bytes (r))); @@ -3117,7 +3095,7 @@ void tgl_do_send_accept_encr_chat (struct tgl_secret_chat *E, unsigned char *ran static unsigned char sha_buffer[20]; sha1 (kk, 256, sha_buffer); - bl_do_encr_chat_set_key (E, kk, *(long long *)(sha_buffer + 12)); + bl_do_encr_chat_set_key (TLS, E, kk, *(long long *)(sha_buffer + 12)); clear_packet (); out_int (CODE_messages_accept_encryption); @@ -3125,8 +3103,8 @@ void tgl_do_send_accept_encr_chat (struct tgl_secret_chat *E, unsigned char *ran out_int (tgl_get_peer_id (E->id)); out_long (E->access_hash); - ensure (BN_set_word (g_a, tgl_state.encr_root)); - ensure (BN_mod_exp (r, g_a, b, p, tgl_state.BN_ctx)); + ensure (BN_set_word (g_a, TLS->encr_root)); + ensure (BN_mod_exp (r, g_a, b, p, TLS->BN_ctx)); static unsigned char buf[256]; memset (buf, 0, sizeof (buf)); BN_bn2bin (r, buf + (256 - BN_num_bytes (r))); @@ -3138,21 +3116,21 @@ void tgl_do_send_accept_encr_chat (struct tgl_secret_chat *E, unsigned char *ran BN_clear_free (p); BN_clear_free (r); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_accept_methods, E, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_accept_methods, E, callback, callback_extra); } -void tgl_do_create_keys_end (struct tgl_secret_chat *U) { - assert (tgl_state.encr_prime); +void tgl_do_create_keys_end (struct tgl_state *TLS, struct tgl_secret_chat *U) { + assert (TLS->encr_prime); BIGNUM *g_b = BN_bin2bn (U->g_key, 256, 0); ensure_ptr (g_b); - assert (tglmp_check_g (tgl_state.encr_prime, g_b) >= 0); - BIGNUM *p = BN_bin2bn (tgl_state.encr_prime, 256, 0); + assert (tglmp_check_g (TLS, TLS->encr_prime, g_b) >= 0); + BIGNUM *p = BN_bin2bn (TLS->encr_prime, 256, 0); ensure_ptr (p); BIGNUM *r = BN_new (); ensure_ptr (r); BIGNUM *a = BN_bin2bn ((void *)U->key, 256, 0); ensure_ptr (a); - ensure (BN_mod_exp (r, g_b, a, p, tgl_state.BN_ctx)); + ensure (BN_mod_exp (r, g_b, a, p, TLS->BN_ctx)); unsigned char *t = talloc (256); memcpy (t, U->key, 256); @@ -3180,7 +3158,7 @@ void tgl_do_create_keys_end (struct tgl_secret_chat *U) { BN_clear_free (a); } -void tgl_do_send_create_encr_chat (void *x, unsigned char *random, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra) { +void tgl_do_send_create_encr_chat (struct tgl_state *TLS, void *x, unsigned char *random, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra) { int user_id = (long)x; int i; unsigned char random_here[256]; @@ -3190,18 +3168,18 @@ void tgl_do_send_create_encr_chat (void *x, unsigned char *random, void (*callba } BIGNUM *a = BN_bin2bn (random, 256, 0); ensure_ptr (a); - BIGNUM *p = BN_bin2bn (tgl_state.encr_prime, 256, 0); + BIGNUM *p = BN_bin2bn (TLS->encr_prime, 256, 0); ensure_ptr (p); BIGNUM *g = BN_new (); ensure_ptr (g); - ensure (BN_set_word (g, tgl_state.encr_root)); + ensure (BN_set_word (g, TLS->encr_root)); BIGNUM *r = BN_new (); ensure_ptr (r); - ensure (BN_mod_exp (r, g, a, p, tgl_state.BN_ctx)); + ensure (BN_mod_exp (r, g, a, p, TLS->BN_ctx)); BN_clear_free (a); @@ -3211,18 +3189,18 @@ void tgl_do_send_create_encr_chat (void *x, unsigned char *random, void (*callba BN_bn2bin (r, (void *)(g_a + (256 - BN_num_bytes (r)))); int t = lrand48 (); - while (tgl_peer_get (TGL_MK_ENCR_CHAT (t))) { + while (tgl_peer_get (TLS, TGL_MK_ENCR_CHAT (t))) { t = lrand48 (); } - bl_do_encr_chat_init (t, user_id, (void *)random, (void *)g_a); - tgl_peer_t *_E = tgl_peer_get (TGL_MK_ENCR_CHAT (t)); + bl_do_encr_chat_init (TLS, t, user_id, (void *)random, (void *)g_a); + tgl_peer_t *_E = tgl_peer_get (TLS, TGL_MK_ENCR_CHAT (t)); assert (_E); struct tgl_secret_chat *E = &_E->encr_chat; clear_packet (); out_int (CODE_messages_request_encryption); - tgl_peer_t *U = tgl_peer_get (TGL_MK_USER (E->user_id)); + tgl_peer_t *U = tgl_peer_get (TLS, TGL_MK_USER (E->user_id)); assert (U); if (U && U->user.access_hash) { out_int (CODE_input_user_foreign); @@ -3240,10 +3218,10 @@ void tgl_do_send_create_encr_chat (void *x, unsigned char *random, void (*callba BN_clear_free (p); BN_clear_free (r); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_request_methods, E, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_request_methods, E, callback, callback_extra); } -static int get_dh_config_on_answer (struct query *q UU) { +static int get_dh_config_on_answer (struct tgl_state *TLS, struct query *q UU) { unsigned x = fetch_int (); assert (x == CODE_messages_dh_config || x == CODE_messages_dh_config_not_modified); if (x == CODE_messages_dh_config) { @@ -3252,14 +3230,14 @@ static int get_dh_config_on_answer (struct query *q UU) { assert (l == 256); char *s = fetch_str (l); int v = fetch_int (); - bl_do_set_dh_params (a, (void *)s, v); + bl_do_set_dh_params (TLS, a, (void *)s, v); BIGNUM *p = BN_bin2bn ((void *)s, 256, 0); ensure_ptr (p); - assert (tglmp_check_DH_params (p, a) >= 0); + assert (tglmp_check_DH_params (TLS, p, a) >= 0); BN_free (p); } else { - assert (tgl_state.encr_param_version); + assert (TLS->encr_param_version); } int l = prefetch_strlen (); assert (l == 256); @@ -3267,7 +3245,7 @@ static int get_dh_config_on_answer (struct query *q UU) { memcpy (random, fetch_str (256), 256); if (q->extra) { void **x = q->extra; - ((void (*)(void *, void *, void *, void *))(*x))(x[1], random, q->callback, q->callback_extra); + ((void (*)(struct tgl_state *, void *, void *, void *, void *))(*x))(TLS, x[1], random, q->callback, q->callback_extra); tfree (x, 2 * sizeof (void *)); tfree_secure (random, 256); } else { @@ -3281,10 +3259,10 @@ static struct query_methods get_dh_config_methods = { .type = TYPE_TO_PARAM(messages_dh_config) }; -void tgl_do_accept_encr_chat_request (struct tgl_secret_chat *E, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra) { +void tgl_do_accept_encr_chat_request (struct tgl_state *TLS, struct tgl_secret_chat *E, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra) { if (E->state != sc_request) { if (callback) { - callback (callback_extra, 0, E); + callback (TLS, callback_extra, 0, E); } return; } @@ -3292,23 +3270,23 @@ void tgl_do_accept_encr_chat_request (struct tgl_secret_chat *E, void (*callback clear_packet (); out_int (CODE_messages_get_dh_config); - out_int (tgl_state.encr_param_version); + out_int (TLS->encr_param_version); out_int (256); void **x = talloc (2 * sizeof (void *)); x[0] = tgl_do_send_accept_encr_chat; x[1] = E; - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dh_config_methods, x, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dh_config_methods, x, callback, callback_extra); } -void tgl_do_create_encr_chat_request (int user_id, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra) { +void tgl_do_create_encr_chat_request (struct tgl_state *TLS, int user_id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra) { clear_packet (); out_int (CODE_messages_get_dh_config); - out_int (tgl_state.encr_param_version); + out_int (TLS->encr_param_version); out_int (256); void **x = talloc (2 * sizeof (void *)); x[0] = tgl_do_send_create_encr_chat; x[1] = (void *)(long)(user_id); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dh_config_methods, x, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dh_config_methods, x, callback, callback_extra); } /* }}} */ @@ -3316,26 +3294,26 @@ void tgl_do_create_encr_chat_request (int user_id, void (*callback)(void *callba //int unread_messages; //int difference_got; //int seq, pts, qts, last_date; -static int get_state_on_answer (struct query *q UU) { - assert (tgl_state.locks & TGL_LOCK_DIFF); - tgl_state.locks ^= TGL_LOCK_DIFF; +static int get_state_on_answer (struct tgl_state *TLS, struct query *q UU) { + assert (TLS->locks & TGL_LOCK_DIFF); + TLS->locks ^= TGL_LOCK_DIFF; assert (fetch_int () == (int)CODE_updates_state); - bl_do_set_pts (fetch_int ()); - bl_do_set_qts (fetch_int ()); - bl_do_set_date (fetch_int ()); - bl_do_set_seq (fetch_int ()); + bl_do_set_pts (TLS, fetch_int ()); + bl_do_set_qts (TLS, fetch_int ()); + bl_do_set_date (TLS, fetch_int ()); + bl_do_set_seq (TLS, fetch_int ()); //unread_messages = fetch_int (); fetch_int (); //write_state_file (); //difference_got = 1; if (q->callback) { - ((void (*)(void *, int))q->callback) (q->callback_extra, 1); + ((void (*)(struct tgl_state *, void *, int))q->callback) (TLS, q->callback_extra, 1); } return 0; } -static int lookup_state_on_answer (struct query *q UU) { +static int lookup_state_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == (int)CODE_updates_state); int pts = fetch_int (); int qts = fetch_int (); @@ -3343,28 +3321,28 @@ static int lookup_state_on_answer (struct query *q UU) { int seq = fetch_int (); fetch_int (); - if (pts > tgl_state.pts || qts > tgl_state.qts || seq > tgl_state.seq) { - tgl_do_get_difference (0, 0, 0); + if (pts > TLS->pts || qts > TLS->qts || seq > TLS->seq) { + tgl_do_get_difference (TLS, 0, 0, 0); } return 0; } //int get_difference_active; -static int get_difference_on_answer (struct query *q UU) { +static int get_difference_on_answer (struct tgl_state *TLS, struct query *q UU) { //get_difference_active = 0; - assert (tgl_state.locks & TGL_LOCK_DIFF); - tgl_state.locks ^= TGL_LOCK_DIFF; + assert (TLS->locks & TGL_LOCK_DIFF); + TLS->locks ^= TGL_LOCK_DIFF; unsigned x = fetch_int (); if (x == CODE_updates_difference_empty) { - bl_do_set_date (fetch_int ()); - bl_do_set_seq (fetch_int ()); + bl_do_set_date (TLS, fetch_int ()); + bl_do_set_seq (TLS, fetch_int ()); //difference_got = 1; - vlogprintf (E_DEBUG, "Empty difference. Seq = %d\n", tgl_state.seq); + vlogprintf (E_DEBUG, "Empty difference. Seq = %d\n", TLS->seq); if (q->callback) { - ((void (*)(void *, int))q->callback) (q->callback_extra, 1); + ((void (*)(struct tgl_state *, void *, int))q->callback) (TLS, q->callback_extra, 1); } } else if (x == CODE_updates_difference || x == CODE_updates_difference_slice) { int n, i; @@ -3373,37 +3351,37 @@ static int get_difference_on_answer (struct query *q UU) { struct tgl_message **ML = talloc (n * sizeof (void *)); int ml_pos = 0; for (i = 0; i < n; i++) { - ML[ml_pos ++] = tglf_fetch_alloc_message (); + ML[ml_pos ++] = tglf_fetch_alloc_message (TLS); } assert (fetch_int () == CODE_vector); n = fetch_int (); struct tgl_message **EL = talloc (n * sizeof (void *)); int el_pos = 0; for (i = 0; i < n; i++) { - EL[el_pos ++] = tglf_fetch_alloc_encrypted_message (); + EL[el_pos ++] = tglf_fetch_alloc_encrypted_message (TLS); } assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglu_work_update (0, 0); + tglu_work_update (TLS, 0, 0); } assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_chat (); + tglf_fetch_alloc_chat (TLS); } assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_user (); + tglf_fetch_alloc_user (TLS); } assert (fetch_int () == (int)CODE_updates_state); - bl_do_set_pts (fetch_int ()); - bl_do_set_qts (fetch_int ()); - bl_do_set_date (fetch_int ()); + bl_do_set_pts (TLS, fetch_int ()); + bl_do_set_qts (TLS, fetch_int ()); + bl_do_set_date (TLS, fetch_int ()); if (x == CODE_updates_difference) { - bl_do_set_seq (fetch_int ()); - vlogprintf (E_DEBUG, "Difference end. New seq = %d\n", tgl_state.seq); + bl_do_set_seq (TLS, fetch_int ()); + vlogprintf (E_DEBUG, "Difference end. New seq = %d\n", TLS->seq); } else { fetch_int (); } @@ -3414,12 +3392,12 @@ static int get_difference_on_answer (struct query *q UU) { print_message (ML[i]); }*/ for (i = 0; i < ml_pos; i++) { - //tgl_state.callback.new_msg (ML[i]); - bl_do_msg_update (ML[i]->id); + //TLS->callback.new_msg (ML[i]); + bl_do_msg_update (TLS, ML[i]->id); } for (i = 0; i < el_pos; i++) { - //tgl_state.callback.new_msg (EL[i]); - bl_do_msg_update (EL[i]->id); + //TLS->callback.new_msg (EL[i]); + bl_do_msg_update (TLS, EL[i]->id); } tfree (ML, ml_pos * sizeof (void *)); tfree (EL, el_pos * sizeof (void *)); @@ -3428,11 +3406,11 @@ static int get_difference_on_answer (struct query *q UU) { //if (q->callback) { // ((void (*)(void *, int))q->callback) (q->callback_extra, 1); //} - tgl_do_get_difference (0, q->callback, q->callback_extra); + tgl_do_get_difference (TLS, 0, q->callback, q->callback_extra); } else { //difference_got = 1; if (q->callback) { - ((void (*)(void *, int))q->callback) (q->callback_extra, 1); + ((void (*)(struct tgl_state *, void *, int))q->callback) (TLS, q->callback_extra, 1); } } } else { @@ -3456,40 +3434,40 @@ static struct query_methods get_difference_methods = { .type = TYPE_TO_PARAM(updates_difference) }; -void tgl_do_lookup_state (void) { - if (tgl_state.locks & TGL_LOCK_DIFF) { +void tgl_do_lookup_state (struct tgl_state *TLS) { + if (TLS->locks & TGL_LOCK_DIFF) { return; } clear_packet (); tgl_do_insert_header (); out_int (CODE_updates_get_state); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &lookup_state_methods, 0, 0, 0); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &lookup_state_methods, 0, 0, 0); } -void tgl_do_get_difference (int sync_from_start, void (*callback)(void *callback_extra, int success), void *callback_extra) { +void tgl_do_get_difference (struct tgl_state *TLS, int sync_from_start, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra) { //get_difference_active = 1; //difference_got = 0; - if (tgl_state.locks & TGL_LOCK_DIFF) { + if (TLS->locks & TGL_LOCK_DIFF) { if (callback) { - callback (callback_extra, 0); + callback (TLS, callback_extra, 0); } return; } - tgl_state.locks |= TGL_LOCK_DIFF; + TLS->locks |= TGL_LOCK_DIFF; clear_packet (); tgl_do_insert_header (); - if (tgl_state.pts > 0 || sync_from_start) { - if (tgl_state.pts == 0) { tgl_state.pts = 1; } - //if (tgl_state.qts == 0) { tgl_state.qts = 1; } - if (tgl_state.date == 0) { tgl_state.date = 1; } + if (TLS->pts > 0 || sync_from_start) { + if (TLS->pts == 0) { TLS->pts = 1; } + //if (TLS->qts == 0) { TLS->qts = 1; } + if (TLS->date == 0) { TLS->date = 1; } out_int (CODE_updates_get_difference); - out_int (tgl_state.pts); - out_int (tgl_state.date); - out_int (tgl_state.qts); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_difference_methods, 0, callback, callback_extra); + out_int (TLS->pts); + out_int (TLS->date); + out_int (TLS->qts); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &get_difference_methods, 0, callback, callback_extra); } else { out_int (CODE_updates_get_state); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_state_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &get_state_methods, 0, callback, callback_extra); } } /* }}} */ @@ -3499,7 +3477,7 @@ void tgl_do_get_difference (int sync_from_start, void (*callback)(void *callback void tgl_do_visualize_key (tgl_peer_id_t id) { assert (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT); - tgl_peer_t *P = tgl_peer_get (id); + tgl_peer_t *P = tgl_peer_get (TLS, id); assert (P); if (P->encr_chat.state != sc_ok) { rprintf ("Chat is not initialized yet\n"); @@ -3525,9 +3503,9 @@ void tgl_do_visualize_key (tgl_peer_id_t id) { print_end (); }*/ -void tgl_do_visualize_key (tgl_peer_id_t id, unsigned char buf[16]) { +void tgl_do_visualize_key (struct tgl_state *TLS, tgl_peer_id_t id, unsigned char buf[16]) { assert (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT); - tgl_peer_t *P = tgl_peer_get (id); + tgl_peer_t *P = tgl_peer_get (TLS, id); assert (P); if (P->encr_chat.state != sc_ok) { vlogprintf (E_WARNING, "Chat is not initialized yet\n"); @@ -3559,7 +3537,7 @@ void tgl_do_visualize_key (tgl_peer_id_t id, unsigned char buf[16]) { print_start (); push_color (COLOR_YELLOW); for (i = 0; i < m; i++) { - tgl_peer_t *U = (void *)tglf_fetch_alloc_user (); + tgl_peer_t *U = (void *)tglf_fetch_alloc_user (TLS); assert (tgl_get_peer_id (U->id) == l[2 * i]); print_user_name (U->id, U); printf (" phone %s: %d mutual friends\n", U->user.phone, l[2 * i + 1]); @@ -3578,7 +3556,7 @@ void tgl_do_get_suggested (void) { clear_packet (); out_int (CODE_contacts_get_suggested); out_int (100); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_suggested_methods, 0); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &get_suggested_methods, 0); }*/ /* }}} */ @@ -3589,13 +3567,13 @@ static struct query_methods add_user_to_chat_methods = { .type = TYPE_TO_PARAM(messages_stated_message) }; -void tgl_do_add_user_to_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, int limit, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void tgl_do_add_user_to_chat (struct tgl_state *TLS, tgl_peer_id_t chat_id, tgl_peer_id_t id, int limit, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { clear_packet (); out_int (CODE_messages_add_chat_user); out_int (tgl_get_peer_id (chat_id)); assert (tgl_get_peer_type (id) == TGL_PEER_USER); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); if (U && U->user.access_hash) { out_int (CODE_input_user_foreign); out_int (tgl_get_peer_id (id)); @@ -3605,16 +3583,16 @@ void tgl_do_add_user_to_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, int limit out_int (tgl_get_peer_id (id)); } out_int (limit); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &add_user_to_chat_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &add_user_to_chat_methods, 0, callback, callback_extra); } -void tgl_do_del_user_from_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void tgl_do_del_user_from_chat (struct tgl_state *TLS, tgl_peer_id_t chat_id, tgl_peer_id_t id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { clear_packet (); out_int (CODE_messages_delete_chat_user); out_int (tgl_get_peer_id (chat_id)); assert (tgl_get_peer_type (id) == TGL_PEER_USER); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); if (U && U->user.access_hash) { out_int (CODE_input_user_foreign); out_int (tgl_get_peer_id (id)); @@ -3623,22 +3601,22 @@ void tgl_do_del_user_from_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, void (* out_int (CODE_input_user_contact); out_int (tgl_get_peer_id (id)); } - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &add_user_to_chat_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &add_user_to_chat_methods, 0, callback, callback_extra); } /* }}} */ /* {{{ Create secret chat */ //char *create_print_name (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4); -void tgl_do_create_secret_chat (tgl_peer_id_t id, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra) { +void tgl_do_create_secret_chat (struct tgl_state *TLS, tgl_peer_id_t id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra) { assert (tgl_get_peer_type (id) == TGL_PEER_USER); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); if (!U) { vlogprintf (E_WARNING, "Can not create chat with unknown user\n"); return; } - tgl_do_create_encr_chat_request (tgl_get_peer_id (id), callback, callback_extra); + tgl_do_create_encr_chat_request (TLS, tgl_get_peer_id (id), callback, callback_extra); } /* }}} */ @@ -3648,13 +3626,13 @@ static struct query_methods create_group_chat_methods = { .type = TYPE_TO_PARAM(messages_stated_message) }; -void tgl_do_create_group_chat (tgl_peer_id_t id, char *chat_topic, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void tgl_do_create_group_chat (struct tgl_state *TLS, tgl_peer_id_t id, char *chat_topic, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { assert (tgl_get_peer_type (id) == TGL_PEER_USER); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); if (!U) { vlogprintf (E_WARNING, "Can not create chat with unknown user\n"); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } @@ -3671,10 +3649,10 @@ void tgl_do_create_group_chat (tgl_peer_id_t id, char *chat_topic, void (*callba out_int (tgl_get_peer_id (id)); } out_string (chat_topic); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &create_group_chat_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &create_group_chat_methods, 0, callback, callback_extra); } -void tgl_do_create_group_chat_ex (int users_num, tgl_peer_id_t ids[], char *chat_topic, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { +void tgl_do_create_group_chat_ex (struct tgl_state *TLS, int users_num, tgl_peer_id_t ids[], char *chat_topic, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra) { clear_packet (); out_int (CODE_messages_create_chat); out_int (CODE_vector); @@ -3682,11 +3660,11 @@ void tgl_do_create_group_chat_ex (int users_num, tgl_peer_id_t ids[], char *chat int i; for (i = 0; i < users_num; i++) { tgl_peer_id_t id = ids[i]; - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); if (!U || tgl_get_peer_type (id) != TGL_PEER_USER) { vlogprintf (E_WARNING, "Can not create chat with unknown user\n"); if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } return; } @@ -3700,19 +3678,19 @@ void tgl_do_create_group_chat_ex (int users_num, tgl_peer_id_t ids[], char *chat } } out_string (chat_topic); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &create_group_chat_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &create_group_chat_methods, 0, callback, callback_extra); } /* }}} */ /* {{{ Delete msg */ -static int delete_msg_on_answer (struct query *q UU) { +static int delete_msg_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == CODE_vector); int n = fetch_int (); fetch_skip (n); if (q->callback) { - ((void (*)(void *, int))q->callback) (q->callback_extra, 1); + ((void (*)(struct tgl_state *, void *, int))q->callback) (TLS, q->callback_extra, 1); } return 0; } @@ -3722,26 +3700,26 @@ static struct query_methods delete_msg_methods = { .type = TYPE_TO_PARAM_1(vector, TYPE_TO_PARAM (bare_int)) }; -void tgl_do_delete_msg (long long id, void (*callback)(void *callback_extra, int success), void *callback_extra) { +void tgl_do_delete_msg (struct tgl_state *TLS, long long id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra) { clear_packet (); out_int (CODE_messages_delete_messages); out_int (CODE_vector); out_int (1); out_int (id); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &delete_msg_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &delete_msg_methods, 0, callback, callback_extra); } /* }}} */ /* {{{ Restore msg */ -static int restore_msg_on_answer (struct query *q UU) { +static int restore_msg_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == CODE_vector); int n = fetch_int (); fetch_skip (n); //logprintf ("Restored %d messages\n", n); if (q->callback) { - ((void (*)(void *, int))q->callback) (q->callback_extra, 1); + ((void (*)(struct tgl_state *, void *, int))q->callback) (TLS, q->callback_extra, 1); } return 0; } @@ -3751,19 +3729,19 @@ static struct query_methods restore_msg_methods = { .type = TYPE_TO_PARAM_1(vector, TYPE_TO_PARAM (bare_int)) }; -void tgl_do_restore_msg (long long id, void (*callback)(void *callback_extra, int success), void *callback_extra) { +void tgl_do_restore_msg (struct tgl_state *TLS, long long id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra) { clear_packet (); out_int (CODE_messages_restore_messages); out_int (CODE_vector); out_int (1); out_int (id); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &restore_msg_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &restore_msg_methods, 0, callback, callback_extra); } /* }}} */ /* {{{ Export card */ -static int export_card_on_answer (struct query *q UU) { +static int export_card_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == CODE_vector); int n = fetch_int (); //logprintf ("Restored %d messages\n", n); @@ -3771,7 +3749,7 @@ static int export_card_on_answer (struct query *q UU) { fetch_ints (r, n); if (q->callback) { - ((void (*)(void *, int, int, int *))q->callback) (q->callback_extra, 1, n, r); + ((void (*)(struct tgl_state *, void *, int, int, int *))q->callback) (TLS, q->callback_extra, 1, n, r); } free (r); return 0; @@ -3782,20 +3760,20 @@ static struct query_methods export_card_methods = { .type = TYPE_TO_PARAM_1(vector, TYPE_TO_PARAM (bare_int)) }; -void tgl_do_export_card (void (*callback)(void *callback_extra, int success, int size, int *card), void *callback_extra) { +void tgl_do_export_card (struct tgl_state *TLS, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, int *card), void *callback_extra) { clear_packet (); out_int (CODE_contacts_export_card); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &export_card_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &export_card_methods, 0, callback, callback_extra); } /* }}} */ /* {{{ Import card */ -static int import_card_on_answer (struct query *q UU) { - struct tgl_user *U = tglf_fetch_alloc_user (); +static int import_card_on_answer (struct tgl_state *TLS, struct query *q UU) { + struct tgl_user *U = tglf_fetch_alloc_user (TLS); if (q->callback) { - ((void (*)(void *, int, struct tgl_user *))q->callback) (q->callback_extra, 1, U); + ((void (*)(struct tgl_state *, void *, int, struct tgl_user *))q->callback) (TLS, q->callback_extra, 1, U); } return 0; } @@ -3805,21 +3783,21 @@ static struct query_methods import_card_methods = { .type = TYPE_TO_PARAM (user) }; -void tgl_do_import_card (int size, int *card, void (*callback)(void *callback_extra, int success, struct tgl_user *U), void *callback_extra) { +void tgl_do_import_card (struct tgl_state *TLS, int size, int *card, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *U), void *callback_extra) { clear_packet (); out_int (CODE_contacts_import_card); out_int (CODE_vector); out_int (size); out_ints (card, size); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &import_card_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &import_card_methods, 0, callback, callback_extra); } /* }}} */ #ifndef DISABLE_EXTF -static int ext_query_on_answer (struct query *q UU) { +static int ext_query_on_answer (struct tgl_state *TLS, struct query *q UU) { if (q->callback) { - char *buf = tglf_extf_fetch (q->type); - ((void (*)(void *, int, char *))q->callback) (q->callback_extra, 1, buf); + char *buf = tglf_extf_fetch (TLS, q->type); + ((void (*)(struct tgl_state *, void *, int, char *))q->callback) (TLS, q->callback_extra, 1, buf); } tgl_paramed_type_free (q->type); return 0; @@ -3829,38 +3807,36 @@ static struct query_methods ext_query_methods = { .on_answer = ext_query_on_answer, }; -void tgl_do_send_extf (char *data, int data_len, void (*callback)(void *callback_extra, int success, char *buf), void *callback_extra) { +void tgl_do_send_extf (struct tgl_state *TLS, char *data, int data_len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *buf), void *callback_extra) { clear_packet (); - ext_query_methods.type = tglf_extf_store (data, data_len); + ext_query_methods.type = tglf_extf_store (TLS, data, data_len); if (ext_query_methods.type) { - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &ext_query_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &ext_query_methods, 0, callback, callback_extra); } } #else -void tgl_do_send_extf (char *data, int data_len, void (*callback)(void *callback_extra, int success, char *buf), void *callback_extra) { +void tgl_do_send_extf (struct tgl_state *TLS, char *data, int data_len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *buf), void *callback_extra) { if (callback) { - callback (callback_extra, 0, 0); + callback (TLS, callback_extra, 0, 0); } } #endif -static void set_flag_4 (void *_D, int success) { +static void set_flag_4 (struct tgl_state *TLS, void *_D, int success) { struct tgl_dc *D = _D; assert (success); D->flags |= 4; - static struct timeval ptimeout; - ptimeout.tv_sec = tgl_state.temp_key_expire_time * 0.9; - event_add (D->ev, &ptimeout); + TLS->timer_methods->insert (D->ev, TLS->temp_key_expire_time * 0.9); } -static int send_bind_temp_on_answer (struct query *q UU) { +static int send_bind_temp_on_answer (struct tgl_state *TLS, struct query *q UU) { assert (fetch_int () == (int)CODE_bool_true); struct tgl_dc *D = q->extra; D->flags |= 2; - tgl_do_help_get_config_dc (D, set_flag_4, D); + tgl_do_help_get_config_dc (TLS, D, set_flag_4, D); vlogprintf (E_DEBUG, "Bind successful in dc %d\n", D->id); return 0; } @@ -3871,34 +3847,34 @@ static struct query_methods send_bind_temp_methods = { .type = TYPE_TO_PARAM (bool) }; -void tgl_do_send_bind_temp_key (struct tgl_dc *D, long long nonce, int expires_at, void *data, int len, long long msg_id) { +void tgl_do_send_bind_temp_key (struct tgl_state *TLS, struct tgl_dc *D, long long nonce, int expires_at, void *data, int len, long long msg_id) { clear_packet (); out_int (CODE_auth_bind_temp_auth_key); out_long (D->auth_key_id); out_long (nonce); out_int (expires_at); out_cstring (data, len); - struct query *q = tglq_send_query_ex (D, packet_ptr - packet_buffer, packet_buffer, &send_bind_temp_methods, D, 0, 0, 2); + struct query *q = tglq_send_query_ex (TLS, D, packet_ptr - packet_buffer, packet_buffer, &send_bind_temp_methods, D, 0, 0, 2); assert (q->msg_id == msg_id); } -static int update_status_on_answer (struct query *q UU) { +static int update_status_on_answer (struct tgl_state *TLS, struct query *q UU) { fetch_bool (); if (q->callback) { - ((void (*)(void *, int))q->callback) (q->callback_extra, 1); + ((void (*)(struct tgl_state *, void *, int))q->callback) (TLS, q->callback_extra, 1); } return 0; } -struct query_methods update_status_methods = { +static struct query_methods update_status_methods = { .on_answer = update_status_on_answer, .type = TYPE_TO_PARAM(bool) }; -void tgl_do_update_status (int online UU, void (*callback)(void *callback_extra, int success), void *callback_extra) { +void tgl_do_update_status (struct tgl_state *TLS, int online UU, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra) { clear_packet (); out_int (CODE_account_update_status); out_int (online ? CODE_bool_false : CODE_bool_true); - tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &update_status_methods, 0, callback, callback_extra); + tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &update_status_methods, 0, callback, callback_extra); } diff --git a/queries.h b/queries.h index b4a4448..51c61c2 100644 --- a/queries.h +++ b/queries.h @@ -24,16 +24,14 @@ #include "auto.h" #include "tgl-layout.h" -struct event; - #define QUERY_ACK_RECEIVED 1 #define QUERY_FORCE_SEND 2 struct query; struct query_methods { - int (*on_answer)(struct query *q); - int (*on_error)(struct query *q, int error_code, int len, char *error); - int (*on_timeout)(struct query *q); + int (*on_answer)(struct tgl_state *TLS, struct query *q); + int (*on_error)(struct tgl_state *TLS, struct query *q, int error_code, int len, char *error); + int (*on_timeout)(struct tgl_state *TLS, struct query *q); struct paramed_type *type; }; @@ -45,7 +43,7 @@ struct query { long long session_id; void *data; struct query_methods *methods; - struct event *ev; + struct tgl_timer *ev; struct tgl_dc *DC; struct tgl_session *session; struct paramed_type *type; @@ -55,20 +53,20 @@ struct query { }; -struct query *tglq_send_query (struct tgl_dc *DC, int len, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra); -void tglq_query_ack (long long id); -void tglq_query_error (long long id); -void tglq_query_result (long long id); -void tglq_query_restart (long long id); +struct query *tglq_send_query (struct tgl_state *TLS, struct tgl_dc *DC, int len, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra); +void tglq_query_ack (struct tgl_state *TLS, long long id); +void tglq_query_error (struct tgl_state *TLS, long long id); +void tglq_query_result (struct tgl_state *TLS, long long id); +void tglq_query_restart (struct tgl_state *TLS, long long id); -double next_timer_in (void); -void work_timers (void); +//double next_timer_in (void); +//void work_timers (void); //extern struct query_methods help_get_config_methods; double get_double_time (void); -void tgl_do_send_bind_temp_key (struct tgl_dc *D, long long nonce, int expires_at, void *data, int len, long long msg_id); +void tgl_do_send_bind_temp_key (struct tgl_state *TLS, struct tgl_dc *D, long long nonce, int expires_at, void *data, int len, long long msg_id); // For binlog diff --git a/structures.c b/structures.c index f995fcc..7d40a8f 100644 --- a/structures.c +++ b/structures.c @@ -53,7 +53,7 @@ DEFINE_TREE(message,struct tgl_message *,id_cmp,0) -char *tgls_default_create_print_name (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4) { +char *tgls_default_create_print_name (struct tgl_state *TLS, tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4) { const char *d[4]; d[0] = a1; d[1] = a2; d[2] = a3; d[3] = a4; static char buf[10000]; @@ -76,7 +76,7 @@ char *tgls_default_create_print_name (tgl_peer_id_t id, const char *a1, const ch int fl = strlen (s); int cc = 0; while (1) { - tgl_peer_t *P = tgl_peer_get_by_name (s); + tgl_peer_t *P = tgl_peer_get_by_name (TLS, s); if (!P || !tgl_cmp_peer_id (P->id, id)) { break; } @@ -117,7 +117,7 @@ enum tgl_typing_status tglf_fetch_typing (void) { /* {{{ Fetch */ -int tglf_fetch_file_location (struct tgl_file_location *loc) { +int tglf_fetch_file_location (struct tgl_state *TLS, struct tgl_file_location *loc) { int x = fetch_int (); assert (x == CODE_file_location_unavailable || x == CODE_file_location); @@ -135,7 +135,7 @@ int tglf_fetch_file_location (struct tgl_file_location *loc) { return 0; } -int tglf_fetch_user_status (struct tgl_user_status *S) { +int tglf_fetch_user_status (struct tgl_state *TLS, struct tgl_user_status *S) { unsigned x = fetch_int (); assert (x == CODE_user_status_empty || x == CODE_user_status_online || x == CODE_user_status_offline); switch (x) { @@ -157,11 +157,11 @@ int tglf_fetch_user_status (struct tgl_user_status *S) { return 0; } -long long tglf_fetch_user_photo (struct tgl_user *U) { +long long tglf_fetch_user_photo (struct tgl_state *TLS, struct tgl_user *U) { unsigned x = fetch_int (); assert (x == CODE_user_profile_photo || x == CODE_user_profile_photo_old || x == CODE_user_profile_photo_empty); if (x == CODE_user_profile_photo_empty) { - bl_do_set_user_profile_photo (U, 0, 0, 0); + bl_do_set_user_profile_photo (TLS, U, 0, 0, 0); return 0; } long long photo_id = 1; @@ -170,14 +170,14 @@ long long tglf_fetch_user_photo (struct tgl_user *U) { } static struct tgl_file_location big; static struct tgl_file_location small; - assert (tglf_fetch_file_location (&small) >= 0); - assert (tglf_fetch_file_location (&big) >= 0); + assert (tglf_fetch_file_location (TLS, &small) >= 0); + assert (tglf_fetch_file_location (TLS, &big) >= 0); - bl_do_set_user_profile_photo (U, photo_id, &big, &small); + bl_do_set_user_profile_photo (TLS, U, photo_id, &big, &small); return 0; } -int tglf_fetch_user (struct tgl_user *U) { +int tglf_fetch_user (struct tgl_state *TLS, struct tgl_user *U) { unsigned x = fetch_int (); assert (x == CODE_user_empty || x == CODE_user_self || x == CODE_user_contact || x == CODE_user_request || x == CODE_user_foreign || x == CODE_user_deleted); U->id = TGL_MK_USER (fetch_int ()); @@ -186,7 +186,7 @@ int tglf_fetch_user (struct tgl_user *U) { } if (x == CODE_user_self) { - bl_do_set_our_id (tgl_get_peer_id (U->id)); + bl_do_set_our_id (TLS, tgl_get_peer_id (U->id)); } int new = !(U->flags & FLAG_CREATED); @@ -202,9 +202,9 @@ int tglf_fetch_user (struct tgl_user *U) { char *s3 = fetch_str (l3); if (x == CODE_user_deleted && !(U->flags & FLAG_DELETED)) { - bl_do_user_add (tgl_get_peer_id (U->id), s1, l1, s2, l2, 0, 0, 0, 0); - bl_do_user_set_username (U, s3, l3); - bl_do_user_delete (U); + bl_do_user_add (TLS, tgl_get_peer_id (U->id), s1, l1, s2, l2, 0, 0, 0, 0); + bl_do_user_set_username (TLS, U, s3, l3); + bl_do_user_delete (TLS, U); } if (x != CODE_user_deleted) { long long access_hash = 0; @@ -218,10 +218,10 @@ int tglf_fetch_user (struct tgl_user *U) { assert (phone_len >= 0); phone = fetch_str (phone_len); } - bl_do_user_add (tgl_get_peer_id (U->id), s1, l1, s2, l2, access_hash, phone, phone_len, x == CODE_user_contact); - bl_do_user_set_username (U, s3, l3); - assert (tglf_fetch_user_photo (U) >= 0); - assert (tglf_fetch_user_status (&U->status) >= 0); + bl_do_user_add (TLS, tgl_get_peer_id (U->id), s1, l1, s2, l2, access_hash, phone, phone_len, x == CODE_user_contact); + bl_do_user_set_username (TLS, U, s3, l3); + assert (tglf_fetch_user_photo (TLS, U) >= 0); + assert (tglf_fetch_user_status (TLS, &U->status) >= 0); if (x == CODE_user_self) { fetch_bool (); @@ -233,63 +233,63 @@ int tglf_fetch_user (struct tgl_user *U) { int l2 = prefetch_strlen (); char *s2 = fetch_str (l2); - bl_do_user_set_name (U, s1, l1, s2, l2); + bl_do_user_set_name (TLS, U, s1, l1, s2, l2); int l3 = prefetch_strlen (); char *s3 = fetch_str (l3); - bl_do_user_set_username (U, s3, l3); + bl_do_user_set_username (TLS, U, s3, l3); if (x == CODE_user_deleted && !(U->flags & FLAG_DELETED)) { - bl_do_user_delete (U); + bl_do_user_delete (TLS, U); } if (x != CODE_user_deleted) { if (x != CODE_user_self) { - bl_do_user_set_access_hash (U, fetch_long ()); + bl_do_user_set_access_hash (TLS, U, fetch_long ()); } if (x != CODE_user_foreign) { int l = prefetch_strlen (); char *s = fetch_str (l); - bl_do_user_set_phone (U, s, l); + bl_do_user_set_phone (TLS, U, s, l); } - assert (tglf_fetch_user_photo (U) >= 0); + assert (tglf_fetch_user_photo (TLS, U) >= 0); - tglf_fetch_user_status (&U->status); + tglf_fetch_user_status (TLS, &U->status); if (x == CODE_user_self) { fetch_bool (); } if (x == CODE_user_contact) { - bl_do_user_set_friend (U, 1); + bl_do_user_set_friend (TLS, U, 1); } else { - bl_do_user_set_friend (U, 0); + bl_do_user_set_friend (TLS, U, 0); } } } return 0; } -void tglf_fetch_user_full (struct tgl_user *U) { +void tglf_fetch_user_full (struct tgl_state *TLS, struct tgl_user *U) { assert (fetch_int () == CODE_user_full); - tglf_fetch_alloc_user (); + tglf_fetch_alloc_user (TLS); assert (skip_type_any (TYPE_TO_PARAM (contacts_link)) >= 0); int *start = in_ptr; assert (skip_type_any (TYPE_TO_PARAM (photo)) >= 0); - bl_do_user_set_full_photo (U, start, 4 * (in_ptr - start)); + bl_do_user_set_full_photo (TLS, U, start, 4 * (in_ptr - start)); assert (skip_type_any (TYPE_TO_PARAM (peer_notify_settings)) >= 0); - bl_do_user_set_blocked (U, fetch_bool ()); + bl_do_user_set_blocked (TLS, U, fetch_bool ()); int l1 = prefetch_strlen (); char *s1 = fetch_str (l1); int l2 = prefetch_strlen (); char *s2 = fetch_str (l2); if (U && (U->flags & FLAG_CREATED)) { - bl_do_user_set_real_name (U, s1, l1, s2, l2); + bl_do_user_set_real_name (TLS, U, s1, l1, s2, l2); } } -void tglf_fetch_encrypted_chat (struct tgl_secret_chat *U) { +void tglf_fetch_encrypted_chat (struct tgl_state *TLS, struct tgl_secret_chat *U) { unsigned x = fetch_int (); assert (x == CODE_encrypted_chat_empty || x == CODE_encrypted_chat_waiting || x == CODE_encrypted_chat_requested || x == CODE_encrypted_chat || x == CODE_encrypted_chat_discarded); U->id = TGL_MK_ENCR_CHAT (fetch_int ()); @@ -303,7 +303,7 @@ void tglf_fetch_encrypted_chat (struct tgl_secret_chat *U) { vlogprintf (E_WARNING, "Unknown chat in deleted state. May be we forgot something...\n"); return; } - bl_do_encr_chat_delete (U); + bl_do_encr_chat_delete (TLS, U); //write_secret_chat_file (); return; } @@ -314,7 +314,7 @@ void tglf_fetch_encrypted_chat (struct tgl_secret_chat *U) { long long access_hash = fetch_long (); int date = fetch_int (); int admin_id = fetch_int (); - int user_id = fetch_int () + admin_id - tgl_state.our_id; + int user_id = fetch_int () + admin_id - TLS->our_id; if (x == CODE_encrypted_chat_waiting) { vlogprintf (E_WARNING, "Unknown chat in waiting state. May be we forgot something...\n"); @@ -335,21 +335,21 @@ void tglf_fetch_encrypted_chat (struct tgl_secret_chat *U) { return; } - bl_do_encr_chat_requested (U, access_hash, date, admin_id, user_id, (void *)g_key, (void *)nonce); + bl_do_encr_chat_requested (TLS, U, access_hash, date, admin_id, user_id, (void *)g_key, (void *)nonce); //write_secret_chat_file (); } else { - bl_do_encr_chat_set_access_hash (U, fetch_long ()); - bl_do_encr_chat_set_date (U, fetch_int ()); + bl_do_encr_chat_set_access_hash (TLS, U, fetch_long ()); + bl_do_encr_chat_set_date (TLS, U, fetch_int ()); if (fetch_int () != U->admin_id) { vlogprintf (E_WARNING, "Changed admin in secret chat. WTF?\n"); return; } - if (U->user_id != U->admin_id + fetch_int () - tgl_state.our_id) { + if (U->user_id != U->admin_id + fetch_int () - TLS->our_id) { vlogprintf (E_WARNING, "Changed partner in secret chat. WTF?\n"); return; } if (x == CODE_encrypted_chat_waiting) { - bl_do_encr_chat_set_state (U, sc_waiting); + bl_do_encr_chat_set_state (TLS, U, sc_waiting); //write_secret_chat_file (); return; // We needed only access hash from here } @@ -363,12 +363,12 @@ void tglf_fetch_encrypted_chat (struct tgl_secret_chat *U) { if (x == CODE_encrypted_chat_requested) { return; // Duplicate? } - bl_do_encr_chat_accepted (U, (void *)g_key, (void *)nonce, fetch_long ()); + bl_do_encr_chat_accepted (TLS, U, (void *)g_key, (void *)nonce, fetch_long ()); //write_secret_chat_file (); } } -void tglf_fetch_chat (struct tgl_chat *C) { +void tglf_fetch_chat (struct tgl_state *TLS, struct tgl_chat *C) { unsigned x = fetch_int (); assert (x == CODE_chat_empty || x == CODE_chat || x == CODE_chat_forbidden); C->id = TGL_MK_CHAT (fetch_int ()); @@ -399,8 +399,8 @@ void tglf_fetch_chat (struct tgl_chat *C) { big.dc = -2; } else { assert (z == CODE_chat_photo); - tglf_fetch_file_location (&small); - tglf_fetch_file_location (&big); + tglf_fetch_file_location (TLS, &small); + tglf_fetch_file_location (TLS, &big); } users_num = fetch_int (); date = fetch_int (); @@ -416,16 +416,16 @@ void tglf_fetch_chat (struct tgl_chat *C) { version = -1; } - bl_do_create_chat (C, y, s, l, users_num, date, version, &big, &small); + bl_do_create_chat (TLS, C, y, s, l, users_num, date, version, &big, &small); } else { if (x == CODE_chat_forbidden) { - bl_do_chat_forbid (C, 1); + bl_do_chat_forbid (TLS, C, 1); } else { - bl_do_chat_forbid (C, 0); + bl_do_chat_forbid (TLS, C, 0); } int l = prefetch_strlen (); char *s = fetch_str (l); - bl_do_chat_set_title (C, s, l); + bl_do_chat_set_title (TLS, C, s, l); struct tgl_file_location small; struct tgl_file_location big; @@ -439,21 +439,21 @@ void tglf_fetch_chat (struct tgl_chat *C) { big.dc = -2; } else { assert (y == CODE_chat_photo); - tglf_fetch_file_location (&small); - tglf_fetch_file_location (&big); + tglf_fetch_file_location (TLS, &small); + tglf_fetch_file_location (TLS, &big); } - bl_do_chat_set_photo (C, &big, &small); + bl_do_chat_set_photo (TLS, C, &big, &small); int users_num = fetch_int (); - bl_do_chat_set_date (C, fetch_int ()); - bl_do_chat_set_set_in_chat (C, fetch_bool ()); - bl_do_chat_set_version (C, users_num, fetch_int ()); + bl_do_chat_set_date (TLS, C, fetch_int ()); + bl_do_chat_set_set_in_chat (TLS, C, fetch_bool ()); + bl_do_chat_set_version (TLS, C, users_num, fetch_int ()); } else { - bl_do_chat_set_date (C, fetch_int ()); + bl_do_chat_set_date (TLS, C, fetch_int ()); } } } -void tglf_fetch_chat_full (struct tgl_chat *C) { +void tglf_fetch_chat_full (struct tgl_state *TLS, struct tgl_chat *C) { unsigned x = fetch_int (); assert (x == CODE_messages_chat_full); assert (fetch_int () == CODE_chat_full); @@ -490,30 +490,30 @@ void tglf_fetch_chat_full (struct tgl_chat *C) { assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_chat (); + tglf_fetch_alloc_chat (TLS); } assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_user (); + tglf_fetch_alloc_user (TLS); } if (admin_id) { - bl_do_chat_set_admin (C, admin_id); + bl_do_chat_set_admin (TLS, C, admin_id); } if (version > 0) { - bl_do_chat_set_participants (C, version, users_num, users); + bl_do_chat_set_participants (TLS, C, version, users_num, users); tfree (users, sizeof (struct tgl_chat_user) * users_num); } - bl_do_chat_set_full_photo (C, start, 4 * (end - start)); + bl_do_chat_set_full_photo (TLS, C, start, 4 * (end - start)); } -void tglf_fetch_photo_size (struct tgl_photo_size *S) { +void tglf_fetch_photo_size (struct tgl_state *TLS, struct tgl_photo_size *S) { memset (S, 0, sizeof (*S)); unsigned x = fetch_int (); assert (x == CODE_photo_size || x == CODE_photo_cached_size || x == CODE_photo_size_empty); S->type = fetch_str_dup (); if (x != CODE_photo_size_empty) { - tglf_fetch_file_location (&S->loc); + tglf_fetch_file_location (TLS, &S->loc); S->w = fetch_int (); S->h = fetch_int (); if (x == CODE_photo_size) { @@ -525,7 +525,7 @@ void tglf_fetch_photo_size (struct tgl_photo_size *S) { } } -void tglf_fetch_geo (struct tgl_geo *G) { +void tglf_fetch_geo (struct tgl_state *TLS, struct tgl_geo *G) { unsigned x = fetch_int (); if (x == CODE_geo_point) { G->longitude = fetch_double (); @@ -537,7 +537,7 @@ void tglf_fetch_geo (struct tgl_geo *G) { } } -void tglf_fetch_photo (struct tgl_photo *P) { +void tglf_fetch_photo (struct tgl_state *TLS, struct tgl_photo *P) { memset (P, 0, sizeof (*P)); unsigned x = fetch_int (); assert (x == CODE_photo_empty || x == CODE_photo); @@ -547,17 +547,17 @@ void tglf_fetch_photo (struct tgl_photo *P) { P->user_id = fetch_int (); P->date = fetch_int (); P->caption = fetch_str_dup (); - tglf_fetch_geo (&P->geo); + tglf_fetch_geo (TLS, &P->geo); assert (fetch_int () == CODE_vector); P->sizes_num = fetch_int (); P->sizes = talloc (sizeof (struct tgl_photo_size) * P->sizes_num); int i; for (i = 0; i < P->sizes_num; i++) { - tglf_fetch_photo_size (&P->sizes[i]); + tglf_fetch_photo_size (TLS, &P->sizes[i]); } } -void tglf_fetch_video (struct tgl_video *V) { +void tglf_fetch_video (struct tgl_state *TLS, struct tgl_video *V) { memset (V, 0, sizeof (*V)); unsigned x = fetch_int (); V->id = fetch_long (); @@ -569,13 +569,13 @@ void tglf_fetch_video (struct tgl_video *V) { V->duration = fetch_int (); V->mime_type = fetch_str_dup (); V->size = fetch_int (); - tglf_fetch_photo_size (&V->thumb); + tglf_fetch_photo_size (TLS, &V->thumb); V->dc_id = fetch_int (); V->w = fetch_int (); V->h = fetch_int (); } -void tglf_fetch_audio (struct tgl_audio *V) { +void tglf_fetch_audio (struct tgl_state *TLS, struct tgl_audio *V) { memset (V, 0, sizeof (*V)); unsigned x = fetch_int (); V->id = fetch_long (); @@ -589,7 +589,7 @@ void tglf_fetch_audio (struct tgl_audio *V) { V->dc_id = fetch_int (); } -void tglf_fetch_document (struct tgl_document *V) { +void tglf_fetch_document (struct tgl_state *TLS, struct tgl_document *V) { memset (V, 0, sizeof (*V)); unsigned x = fetch_int (); V->id = fetch_long (); @@ -600,11 +600,11 @@ void tglf_fetch_document (struct tgl_document *V) { V->caption = fetch_str_dup (); V->mime_type = fetch_str_dup (); V->size = fetch_int (); - tglf_fetch_photo_size (&V->thumb); + tglf_fetch_photo_size (TLS, &V->thumb); V->dc_id = fetch_int (); } -void tglf_fetch_message_action (struct tgl_message_action *M) { +void tglf_fetch_message_action (struct tgl_state *TLS, struct tgl_message_action *M) { memset (M, 0, sizeof (*M)); unsigned x = fetch_int (); switch (x) { @@ -638,7 +638,7 @@ void tglf_fetch_message_action (struct tgl_message_action *M) { break; case CODE_message_action_chat_edit_photo: M->type = tgl_message_action_chat_edit_photo; - tglf_fetch_photo (&M->photo); + tglf_fetch_photo (TLS, &M->photo); break; case CODE_message_action_chat_delete_photo: M->type = tgl_message_action_chat_delete_photo; @@ -657,13 +657,13 @@ void tglf_fetch_message_action (struct tgl_message_action *M) { } } -void tglf_fetch_message_short (struct tgl_message *M) { +void tglf_fetch_message_short (struct tgl_state *TLS, struct tgl_message *M) { int new = !(M->flags & FLAG_CREATED); if (new) { int id = fetch_int (); int from_id = fetch_int (); - int to_id = tgl_state.our_id; + int to_id = TLS->our_id; int l = prefetch_strlen (); char *s = fetch_str (l); @@ -672,16 +672,16 @@ void tglf_fetch_message_short (struct tgl_message *M) { int date = fetch_int (); //tglu_fetch_seq (); int seq = fetch_int (); - assert (seq == tgl_state.seq + 1); + assert (seq == TLS->seq + 1); - bl_do_create_message_text (id, from_id, TGL_PEER_USER, to_id, date, 1, l, s); + bl_do_create_message_text (TLS, id, from_id, TGL_PEER_USER, to_id, date, 1, l, s); - tgl_peer_t *P = tgl_peer_get (TGL_MK_USER (from_id)); + tgl_peer_t *P = tgl_peer_get (TLS, TGL_MK_USER (from_id)); if (!P || !(P->flags & FLAG_CREATED)) { - tgl_do_get_difference (0, 0, 0); + tgl_do_get_difference (TLS, 0, 0, 0); } else { - bl_do_set_pts (pts); - bl_do_set_date (date); + bl_do_set_pts (TLS, pts); + bl_do_set_date (TLS, date); } //bl_do_msg_seq_update (id); } else { @@ -690,16 +690,16 @@ void tglf_fetch_message_short (struct tgl_message *M) { int l = prefetch_strlen (); fetch_str (l); // text - tglu_fetch_pts (); + tglu_fetch_pts (TLS); fetch_int (); //tglu_fetch_seq (); int seq = fetch_int (); - assert (seq == tgl_state.seq + 1); + assert (seq == TLS->seq + 1); //bl_do_msg_seq_update (id); } } -void tglf_fetch_message_short_chat (struct tgl_message *M) { +void tglf_fetch_message_short_chat (struct tgl_state *TLS, struct tgl_message *M) { int new = !(M->flags & FLAG_CREATED); if (new) { @@ -714,19 +714,19 @@ void tglf_fetch_message_short_chat (struct tgl_message *M) { //tglu_fetch_seq (); int seq = fetch_int (); - assert (seq == tgl_state.seq + 1); - bl_do_create_message_text (id, from_id, TGL_PEER_CHAT, to_id, date, 1, l, s); + assert (seq == TLS->seq + 1); + bl_do_create_message_text (TLS, id, from_id, TGL_PEER_CHAT, to_id, date, 1, l, s); - tgl_peer_t *P = tgl_peer_get (TGL_MK_CHAT (to_id)); + tgl_peer_t *P = tgl_peer_get (TLS, TGL_MK_CHAT (to_id)); if (!P || !(P->flags & FLAG_CREATED)) { - tgl_do_get_difference (0, 0, 0); + tgl_do_get_difference (TLS, 0, 0, 0); } else { - P = tgl_peer_get (TGL_MK_USER (from_id)); + P = tgl_peer_get (TLS, TGL_MK_USER (from_id)); if (!P || !(P->flags & FLAG_CREATED)) { - tgl_do_get_difference (0, 0, 0); + tgl_do_get_difference (TLS, 0, 0, 0); } else { - bl_do_set_pts (pts); - bl_do_set_date (date); + bl_do_set_pts (TLS, pts); + bl_do_set_date (TLS, date); } } //bl_do_msg_seq_update (id); @@ -737,17 +737,17 @@ void tglf_fetch_message_short_chat (struct tgl_message *M) { int l = prefetch_strlen (); fetch_str (l); // text - tglu_fetch_pts (); + tglu_fetch_pts (TLS); fetch_int (); //tglu_fetch_seq (); int seq = fetch_int (); - assert (seq == tgl_state.seq + 1); + assert (seq == TLS->seq + 1); //bl_do_msg_seq_update (id); } } -void tglf_fetch_message_media (struct tgl_message_media *M) { +void tglf_fetch_message_media (struct tgl_state *TLS, struct tgl_message_media *M) { memset (M, 0, sizeof (*M)); //M->type = fetch_int (); int x = fetch_int (); @@ -757,23 +757,23 @@ void tglf_fetch_message_media (struct tgl_message_media *M) { break; case CODE_message_media_photo: M->type = tgl_message_media_photo; - tglf_fetch_photo (&M->photo); + tglf_fetch_photo (TLS, &M->photo); break; case CODE_message_media_video: M->type = tgl_message_media_video; - tglf_fetch_video (&M->video); + tglf_fetch_video (TLS, &M->video); break; case CODE_message_media_audio: M->type = tgl_message_media_audio; - tglf_fetch_audio (&M->audio); + tglf_fetch_audio (TLS, &M->audio); break; case CODE_message_media_document: M->type = tgl_message_media_document; - tglf_fetch_document (&M->document); + tglf_fetch_document (TLS, &M->document); break; case CODE_message_media_geo: M->type = tgl_message_media_geo; - tglf_fetch_geo (&M->geo); + tglf_fetch_geo (TLS, &M->geo); break; case CODE_message_media_contact: M->type = tgl_message_media_contact; @@ -794,7 +794,7 @@ void tglf_fetch_message_media (struct tgl_message_media *M) { } } -void tglf_fetch_message_media_encrypted (struct tgl_message_media *M) { +void tglf_fetch_message_media_encrypted (struct tgl_state *TLS, struct tgl_message_media *M) { memset (M, 0, sizeof (*M)); unsigned x = fetch_int (); int l; @@ -955,7 +955,7 @@ void tglf_fetch_message_media_encrypted (struct tgl_message_media *M) { } } -void tglf_fetch_message_action_encrypted (struct tgl_message_action *M) { +void tglf_fetch_message_action_encrypted (struct tgl_state *TLS, struct tgl_message_action *M) { unsigned x = fetch_int (); switch (x) { case CODE_decrypted_message_action_set_message_t_t_l: @@ -970,7 +970,7 @@ void tglf_fetch_message_action_encrypted (struct tgl_message_action *M) { M->read_cnt = n; while (n -- > 0) { long long id = fetch_long (); - struct tgl_message *N = tgl_message_get (id); + struct tgl_message *N = tgl_message_get (TLS, id); if (N) { N->unread = 0; } @@ -1016,7 +1016,7 @@ void tglf_fetch_message_action_encrypted (struct tgl_message_action *M) { } } -tgl_peer_id_t tglf_fetch_peer_id (void) { +tgl_peer_id_t tglf_fetch_peer_id (struct tgl_state *TLS) { unsigned x =fetch_int (); if (x == CODE_peer_user) { return TGL_MK_USER (fetch_int ()); @@ -1026,7 +1026,7 @@ tgl_peer_id_t tglf_fetch_peer_id (void) { } } -void tglf_fetch_message (struct tgl_message *M) { +void tglf_fetch_message (struct tgl_state *TLS, struct tgl_message *M) { unsigned x = fetch_int (); assert (x == CODE_message_empty || x == CODE_message || x == CODE_message_forwarded || x == CODE_message_service); int flags = 0; @@ -1046,7 +1046,7 @@ void tglf_fetch_message (struct tgl_message *M) { fwd_date = fetch_int (); } int from_id = fetch_int (); - tgl_peer_id_t to_id = tglf_fetch_peer_id (); + tgl_peer_id_t to_id = tglf_fetch_peer_id (TLS); //fetch_bool (); // out. @@ -1063,9 +1063,9 @@ void tglf_fetch_message (struct tgl_message *M) { if (new) { if (fwd_from_id) { - bl_do_create_message_service_fwd (id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, fwd_from_id, fwd_date, unread, start, (in_ptr - start)); + bl_do_create_message_service_fwd (TLS, id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, fwd_from_id, fwd_date, unread, start, (in_ptr - start)); } else { - bl_do_create_message_service (id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, unread, start, (in_ptr - start)); + bl_do_create_message_service (TLS, id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, unread, start, (in_ptr - start)); } } } else { @@ -1077,16 +1077,16 @@ void tglf_fetch_message (struct tgl_message *M) { if (new) { if (fwd_from_id) { - bl_do_create_message_media_fwd (id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, fwd_from_id, fwd_date, unread, l, s, start, in_ptr - start); + bl_do_create_message_media_fwd (TLS, id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, fwd_from_id, fwd_date, unread, l, s, start, in_ptr - start); } else { - bl_do_create_message_media (id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, unread, l, s, start, in_ptr - start); + bl_do_create_message_media (TLS, id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, unread, l, s, start, in_ptr - start); } } } - bl_do_set_unread (M, unread); + bl_do_set_unread (TLS, M, unread); } -void tglf_tglf_fetch_geo_message (struct tgl_message *M) { +void tglf_tglf_fetch_geo_message (struct tgl_state *TLS, struct tgl_message *M) { memset (M, 0, sizeof (*M)); unsigned x = fetch_int (); assert (x == CODE_geo_chat_message_empty || x == CODE_geo_chat_message || x == CODE_geo_chat_message_service); @@ -1100,11 +1100,11 @@ void tglf_tglf_fetch_geo_message (struct tgl_message *M) { M->date = fetch_int (); if (x == CODE_geo_chat_message_service) { M->service = 1; - tglf_fetch_message_action (&M->action); + tglf_fetch_message_action (TLS, &M->action); } else { M->message = fetch_str_dup (); M->message_len = strlen (M->message); - tglf_fetch_message_media (&M->media); + tglf_fetch_message_media (TLS, &M->media); } } @@ -1162,13 +1162,12 @@ static int decrypt_encrypted_message (struct tgl_secret_chat *E) { sha1 ((void *)decr_ptr, 4 + x, sha1a_buffer); if (memcmp (sha1a_buffer + 4, msg_key, 16)) { - vlogprintf (E_WARNING, "Sha1 mismatch\n"); return -1; } return 0; } -void tglf_fetch_encrypted_message (struct tgl_message *M) { +void tglf_fetch_encrypted_message (struct tgl_state *TLS, struct tgl_message *M) { unsigned x = fetch_int (); assert (x == CODE_encrypted_message || x == CODE_encrypted_message_service); unsigned sx = x; @@ -1178,7 +1177,7 @@ void tglf_fetch_encrypted_message (struct tgl_message *M) { tgl_peer_id_t chat = TGL_MK_ENCR_CHAT (to_id); int date = fetch_int (); - tgl_peer_t *P = tgl_peer_get (chat); + tgl_peer_t *P = tgl_peer_get (TLS, chat); if (!P) { vlogprintf (E_WARNING, "Encrypted message to unknown chat. Dropping\n"); M->flags |= FLAG_MESSAGE_EMPTY; @@ -1220,7 +1219,7 @@ void tglf_fetch_encrypted_message (struct tgl_message *M) { int layer = fetch_int (); assert (layer >= 0); if (P && ((P->flags) & FLAG_CREATED)) { - bl_do_encr_chat_set_layer ((void *)P, layer); + bl_do_encr_chat_set_layer (TLS, (void *)P, layer); } //x = fetch_int (); //assert (x == CODE_decrypted_message || x == CODE_decrypted_message_service); @@ -1232,8 +1231,8 @@ void tglf_fetch_encrypted_message (struct tgl_message *M) { vlogprintf (E_WARNING, "Hole in seq in secret chat. in_seq_no = %d, expect_seq_no = %d\n", in_seq_no / 2, P->encr_chat.in_seq_no); drop = 1; } - if ((in_seq_no & 1) != 1 - (P->encr_chat.admin_id == tgl_state.our_id) || - (out_seq_no & 1) != (P->encr_chat.admin_id == tgl_state.our_id)) { + if ((in_seq_no & 1) != 1 - (P->encr_chat.admin_id == TLS->our_id) || + (out_seq_no & 1) != (P->encr_chat.admin_id == TLS->our_id)) { vlogprintf (E_WARNING, "Bad msg admin\n"); drop = 1; } @@ -1286,11 +1285,11 @@ void tglf_fetch_encrypted_message (struct tgl_message *M) { assert (skip_type_any (TYPE_TO_PARAM (encrypted_file)) >= 0); if (x == CODE_decrypted_message || x == CODE_decrypted_message_l16) { if (!drop) { - bl_do_create_message_media_encr (id, P->encr_chat.user_id, TGL_PEER_ENCR_CHAT, to_id, date, l, s, start, end - start, start_file, in_ptr - start_file); + bl_do_create_message_media_encr (TLS, id, P->encr_chat.user_id, TGL_PEER_ENCR_CHAT, to_id, date, l, s, start, end - start, start_file, in_ptr - start_file); } } else if (x == CODE_decrypted_message_service || x == CODE_decrypted_message_service_l16) { if (!drop) { - bl_do_create_message_service_encr (id, P->encr_chat.user_id, TGL_PEER_ENCR_CHAT, to_id, date, start, end - start); + bl_do_create_message_service_encr (TLS, id, P->encr_chat.user_id, TGL_PEER_ENCR_CHAT, to_id, date, start, end - start); } } } else { @@ -1302,19 +1301,19 @@ void tglf_fetch_encrypted_message (struct tgl_message *M) { } else { if (ok && (x == CODE_decrypted_message_service || x == CODE_decrypted_message_service_l16)) { if (!drop) { - bl_do_create_message_service_encr (id, P->encr_chat.user_id, TGL_PEER_ENCR_CHAT, to_id, date, start, end - start); + bl_do_create_message_service_encr (TLS, id, P->encr_chat.user_id, TGL_PEER_ENCR_CHAT, to_id, date, start, end - start); } } } if (!drop) { if (in_seq_no >= 0 && out_seq_no >= 0) { - bl_do_encr_chat_update_seq ((void *)P, in_seq_no / 2 + 1, out_seq_no / 2); + bl_do_encr_chat_update_seq (TLS, (void *)P, in_seq_no / 2 + 1, out_seq_no / 2); assert (P->encr_chat.in_seq_no == in_seq_no / 2 + 1); } } } -void tglf_fetch_encrypted_message_file (struct tgl_message_media *M) { +void tglf_fetch_encrypted_message_file (struct tgl_state *TLS, struct tgl_message_media *M) { unsigned x = fetch_int (); assert (x == CODE_encrypted_file || x == CODE_encrypted_file_empty); if (x == CODE_encrypted_file_empty) { @@ -1340,275 +1339,275 @@ static int id_cmp (struct tgl_message *M1, struct tgl_message *M2) { else { return 0; } } -static void increase_peer_size (void) { - if (tgl_state.peer_num == tgl_state.peer_size) { - int new_size = tgl_state.peer_size ? 2 * tgl_state.peer_size : 10; - int old_size = tgl_state.peer_size; +static void increase_peer_size (struct tgl_state *TLS) { + if (TLS->peer_num == TLS->peer_size) { + int new_size = TLS->peer_size ? 2 * TLS->peer_size : 10; + int old_size = TLS->peer_size; if (old_size) { - tgl_state.Peers = trealloc (tgl_state.Peers, old_size * sizeof (void *), new_size * sizeof (void *)); + TLS->Peers = trealloc (TLS->Peers, old_size * sizeof (void *), new_size * sizeof (void *)); } else { - tgl_state.Peers = talloc (new_size * sizeof (void *)); + TLS->Peers = talloc (new_size * sizeof (void *)); } - tgl_state.peer_size = new_size; + TLS->peer_size = new_size; } } -struct tgl_user *tglf_fetch_alloc_user (void) { +struct tgl_user *tglf_fetch_alloc_user (struct tgl_state *TLS) { int data[2]; prefetch_data (data, 8); - tgl_peer_t *U = tgl_peer_get (TGL_MK_USER (data[1])); + tgl_peer_t *U = tgl_peer_get (TLS, TGL_MK_USER (data[1])); if (!U) { - tgl_state.users_allocated ++; + TLS->users_allocated ++; U = talloc0 (sizeof (*U)); U->id = TGL_MK_USER (data[1]); - tgl_state.peer_tree = tree_insert_peer (tgl_state.peer_tree, U, lrand48 ()); - increase_peer_size (); - tgl_state.Peers[tgl_state.peer_num ++] = U; + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, U, lrand48 ()); + increase_peer_size (TLS); + TLS->Peers[TLS->peer_num ++] = U; } - tglf_fetch_user (&U->user); + tglf_fetch_user (TLS, &U->user); return &U->user; } -struct tgl_secret_chat *tglf_fetch_alloc_encrypted_chat (void) { +struct tgl_secret_chat *tglf_fetch_alloc_encrypted_chat (struct tgl_state *TLS) { int data[2]; prefetch_data (data, 8); - tgl_peer_t *U = tgl_peer_get (TGL_MK_ENCR_CHAT (data[1])); + tgl_peer_t *U = tgl_peer_get (TLS, TGL_MK_ENCR_CHAT (data[1])); if (!U) { U = talloc0 (sizeof (*U)); U->id = TGL_MK_ENCR_CHAT (data[1]); - tgl_state.encr_chats_allocated ++; - tgl_state.peer_tree = tree_insert_peer (tgl_state.peer_tree, U, lrand48 ()); - increase_peer_size (); - tgl_state.Peers[tgl_state.peer_num ++] = U; + TLS->encr_chats_allocated ++; + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, U, lrand48 ()); + increase_peer_size (TLS); + TLS->Peers[TLS->peer_num ++] = U; } - tglf_fetch_encrypted_chat (&U->encr_chat); + tglf_fetch_encrypted_chat (TLS, &U->encr_chat); return &U->encr_chat; } -struct tgl_user *tglf_fetch_alloc_user_full (void) { +struct tgl_user *tglf_fetch_alloc_user_full (struct tgl_state *TLS) { int data[3]; prefetch_data (data, 12); - tgl_peer_t *U = tgl_peer_get (TGL_MK_USER (data[2])); + tgl_peer_t *U = tgl_peer_get (TLS, TGL_MK_USER (data[2])); if (U) { - tglf_fetch_user_full (&U->user); + tglf_fetch_user_full (TLS, &U->user); return &U->user; } else { - tgl_state.users_allocated ++; + TLS->users_allocated ++; U = talloc0 (sizeof (*U)); U->id = TGL_MK_USER (data[2]); - tgl_state.peer_tree = tree_insert_peer (tgl_state.peer_tree, U, lrand48 ()); - tglf_fetch_user_full (&U->user); - increase_peer_size (); - tgl_state.Peers[tgl_state.peer_num ++] = U; + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, U, lrand48 ()); + tglf_fetch_user_full (TLS, &U->user); + increase_peer_size (TLS); + TLS->Peers[TLS->peer_num ++] = U; return &U->user; } } -struct tgl_message *tglf_fetch_alloc_message (void) { +struct tgl_message *tglf_fetch_alloc_message (struct tgl_state *TLS) { int data[3]; prefetch_data (data, 12); - struct tgl_message *M = tgl_message_get (data[0] != (int)CODE_message_empty ? data[2] : data[1]); + struct tgl_message *M = tgl_message_get (TLS, data[0] != (int)CODE_message_empty ? data[2] : data[1]); if (!M) { - M = tglm_message_alloc (data[0] != (int)CODE_message_empty ? data[2] : data[1]); + M = tglm_message_alloc (TLS, data[0] != (int)CODE_message_empty ? data[2] : data[1]); } - tglf_fetch_message (M); + tglf_fetch_message (TLS, M); return M; } -struct tgl_message *tglf_fetch_alloc_geo_message (void) { +struct tgl_message *tglf_fetch_alloc_geo_message (struct tgl_state *TLS) { struct tgl_message *M = talloc (sizeof (*M)); - tglf_tglf_fetch_geo_message (M); - struct tgl_message *M1 = tree_lookup_message (tgl_state.message_tree, M); - tgl_state.messages_allocated ++; + tglf_tglf_fetch_geo_message (TLS, M); + struct tgl_message *M1 = tree_lookup_message (TLS->message_tree, M); + TLS->messages_allocated ++; if (M1) { - tglm_message_del_use (M1); - tglm_message_del_peer (M1); - tgls_clear_message (M1); + tglm_message_del_use (TLS, M1); + tglm_message_del_peer (TLS, M1); + tgls_clear_message (TLS, M1); memcpy (M1, M, sizeof (*M)); tfree (M, sizeof (*M)); - tglm_message_add_use (M1); - tglm_message_add_peer (M1); - tgl_state.messages_allocated --; + tglm_message_add_use (TLS, M1); + tglm_message_add_peer (TLS, M1); + TLS->messages_allocated --; return M1; } else { - tglm_message_add_use (M); - tglm_message_add_peer (M); - tgl_state.message_tree = tree_insert_message (tgl_state.message_tree, M, lrand48 ()); + tglm_message_add_use (TLS, M); + tglm_message_add_peer (TLS, M); + TLS->message_tree = tree_insert_message (TLS->message_tree, M, lrand48 ()); return M; } } -struct tgl_message *tglf_fetch_alloc_encrypted_message (void) { +struct tgl_message *tglf_fetch_alloc_encrypted_message (struct tgl_state *TLS) { int data[3]; prefetch_data (data, 12); - struct tgl_message *M = tgl_message_get (*(long long *)(data + 1)); + struct tgl_message *M = tgl_message_get (TLS, *(long long *)(data + 1)); if (!M) { M = talloc0 (sizeof (*M)); M->id = *(long long *)(data + 1); - tglm_message_insert_tree (M); - tgl_state.messages_allocated ++; - assert (tgl_message_get (M->id) == M); + tglm_message_insert_tree (TLS, M); + TLS->messages_allocated ++; + assert (tgl_message_get (TLS, M->id) == M); } - tglf_fetch_encrypted_message (M); + tglf_fetch_encrypted_message (TLS, M); return M; } -struct tgl_message *tglf_fetch_alloc_message_short (void) { +struct tgl_message *tglf_fetch_alloc_message_short (struct tgl_state *TLS) { int data[1]; prefetch_data (data, 4); - struct tgl_message *M = tgl_message_get (data[0]); + struct tgl_message *M = tgl_message_get (TLS, data[0]); if (!M) { M = talloc0 (sizeof (*M)); M->id = data[0]; - tglm_message_insert_tree (M); - tgl_state.messages_allocated ++; + tglm_message_insert_tree (TLS, M); + TLS->messages_allocated ++; } - tglf_fetch_message_short (M); + tglf_fetch_message_short (TLS, M); return M; } -struct tgl_message *tglf_fetch_alloc_message_short_chat (void) { +struct tgl_message *tglf_fetch_alloc_message_short_chat (struct tgl_state *TLS) { int data[1]; prefetch_data (data, 4); - struct tgl_message *M = tgl_message_get (data[0]); + struct tgl_message *M = tgl_message_get (TLS, data[0]); if (!M) { M = talloc0 (sizeof (*M)); M->id = data[0]; - tglm_message_insert_tree (M); - tgl_state.messages_allocated ++; + tglm_message_insert_tree (TLS, M); + TLS->messages_allocated ++; } - tglf_fetch_message_short_chat (M); + tglf_fetch_message_short_chat (TLS, M); return M; } -struct tgl_chat *tglf_fetch_alloc_chat (void) { +struct tgl_chat *tglf_fetch_alloc_chat (struct tgl_state *TLS) { int data[2]; prefetch_data (data, 8); - tgl_peer_t *U = tgl_peer_get (TGL_MK_CHAT (data[1])); + tgl_peer_t *U = tgl_peer_get (TLS, TGL_MK_CHAT (data[1])); if (!U) { - tgl_state.chats_allocated ++; + TLS->chats_allocated ++; U = talloc0 (sizeof (*U)); U->id = TGL_MK_CHAT (data[1]); - tgl_state.peer_tree = tree_insert_peer (tgl_state.peer_tree, U, lrand48 ()); - increase_peer_size (); - tgl_state.Peers[tgl_state.peer_num ++] = U; + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, U, lrand48 ()); + increase_peer_size (TLS); + TLS->Peers[TLS->peer_num ++] = U; } - tglf_fetch_chat (&U->chat); + tglf_fetch_chat (TLS, &U->chat); return &U->chat; } -struct tgl_chat *tglf_fetch_alloc_chat_full (void) { +struct tgl_chat *tglf_fetch_alloc_chat_full (struct tgl_state *TLS) { int data[3]; prefetch_data (data, 12); - tgl_peer_t *U = tgl_peer_get (TGL_MK_CHAT (data[2])); + tgl_peer_t *U = tgl_peer_get (TLS, TGL_MK_CHAT (data[2])); if (U) { - tglf_fetch_chat_full (&U->chat); + tglf_fetch_chat_full (TLS, &U->chat); return &U->chat; } else { - tgl_state.chats_allocated ++; + TLS->chats_allocated ++; U = talloc0 (sizeof (*U)); U->id = TGL_MK_CHAT (data[2]); - tgl_state.peer_tree = tree_insert_peer (tgl_state.peer_tree, U, lrand48 ()); - tglf_fetch_chat_full (&U->chat); - increase_peer_size (); - tgl_state.Peers[tgl_state.peer_num ++] = U; + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, U, lrand48 ()); + tglf_fetch_chat_full (TLS, &U->chat); + increase_peer_size (TLS); + TLS->Peers[TLS->peer_num ++] = U; return &U->chat; } } /* }}} */ -void tglp_insert_encrypted_chat (tgl_peer_t *P) { - tgl_state.encr_chats_allocated ++; - tgl_state.peer_tree = tree_insert_peer (tgl_state.peer_tree, P, lrand48 ()); - increase_peer_size (); - tgl_state.Peers[tgl_state.peer_num ++] = P; +void tglp_insert_encrypted_chat (struct tgl_state *TLS, tgl_peer_t *P) { + TLS->encr_chats_allocated ++; + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, lrand48 ()); + increase_peer_size (TLS); + TLS->Peers[TLS->peer_num ++] = P; } -void tglp_insert_user (tgl_peer_t *P) { - tgl_state.users_allocated ++; - tgl_state.peer_tree = tree_insert_peer (tgl_state.peer_tree, P, lrand48 ()); - increase_peer_size (); - tgl_state.Peers[tgl_state.peer_num ++] = P; +void tglp_insert_user (struct tgl_state *TLS, tgl_peer_t *P) { + TLS->users_allocated ++; + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, lrand48 ()); + increase_peer_size (TLS); + TLS->Peers[TLS->peer_num ++] = P; } -void tglp_insert_chat (tgl_peer_t *P) { - tgl_state.chats_allocated ++; - tgl_state.peer_tree = tree_insert_peer (tgl_state.peer_tree, P, lrand48 ()); - increase_peer_size (); - tgl_state.Peers[tgl_state.peer_num ++] = P; +void tglp_insert_chat (struct tgl_state *TLS, tgl_peer_t *P) { + TLS->chats_allocated ++; + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, lrand48 ()); + increase_peer_size (TLS); + TLS->Peers[TLS->peer_num ++] = P; } -void tgl_insert_empty_user (int uid) { +void tgl_insert_empty_user (struct tgl_state *TLS, int uid) { tgl_peer_id_t id = TGL_MK_USER (uid); - if (tgl_peer_get (id)) { return; } + if (tgl_peer_get (TLS, id)) { return; } tgl_peer_t *P = talloc0 (sizeof (*P)); P->id = id; - tglp_insert_user (P); + tglp_insert_user (TLS, P); } -void tgl_insert_empty_chat (int cid) { +void tgl_insert_empty_chat (struct tgl_state *TLS, int cid) { tgl_peer_id_t id = TGL_MK_CHAT (cid); - if (tgl_peer_get (id)) { return; } + if (tgl_peer_get (TLS, id)) { return; } tgl_peer_t *P = talloc0 (sizeof (*P)); P->id = id; - tglp_insert_chat (P); + tglp_insert_chat (TLS, P); } /* {{{ Free */ -void tgls_free_photo_size (struct tgl_photo_size *S) { +void tgls_free_photo_size (struct tgl_state *TLS, struct tgl_photo_size *S) { tfree_str (S->type); if (S->data) { tfree (S->data, S->size); } } -void tgls_free_photo (struct tgl_photo *P) { +void tgls_free_photo (struct tgl_state *TLS, struct tgl_photo *P) { if (P->caption) { tfree_str (P->caption); } if (P->sizes) { int i; for (i = 0; i < P->sizes_num; i++) { - tgls_free_photo_size (&P->sizes[i]); + tgls_free_photo_size (TLS, &P->sizes[i]); } tfree (P->sizes, sizeof (struct tgl_photo_size) * P->sizes_num); } } -void tgls_free_video (struct tgl_video *V) { +void tgls_free_video (struct tgl_state *TLS, struct tgl_video *V) { tfree_str (V->mime_type); if (!V->access_hash) { return; } tfree_str (V->caption); - tgls_free_photo_size (&V->thumb); + tgls_free_photo_size (TLS, &V->thumb); } -void tgls_free_audio (struct tgl_audio *A) { +void tgls_free_audio (struct tgl_state *TLS, struct tgl_audio *A) { tfree_str (A->mime_type); } -void tgls_free_document (struct tgl_document *D) { +void tgls_free_document (struct tgl_state *TLS, struct tgl_document *D) { if (!D->access_hash) { return; } if (D->mime_type) { tfree_str (D->mime_type);} if (D->caption) {tfree_str (D->caption);} - tgls_free_photo_size (&D->thumb); + tgls_free_photo_size (TLS, &D->thumb); } -void tgls_free_message_media (struct tgl_message_media *M) { +void tgls_free_message_media (struct tgl_state *TLS, struct tgl_message_media *M) { switch (M->type) { case tgl_message_media_none: case tgl_message_media_geo: return; case tgl_message_media_audio: - tgls_free_audio (&M->audio); + tgls_free_audio (TLS, &M->audio); return; case tgl_message_media_photo: - tgls_free_photo (&M->photo); + tgls_free_photo (TLS, &M->photo); return; case tgl_message_media_video: - tgls_free_video (&M->video); + tgls_free_video (TLS, &M->video); return; case tgl_message_media_contact: tfree_str (M->phone); @@ -1616,7 +1615,7 @@ void tgls_free_message_media (struct tgl_message_media *M) { tfree_str (M->last_name); return; case tgl_message_media_document: - tgls_free_document (&M->document); + tgls_free_document (TLS, &M->document); return; case tgl_message_media_unsupported: tfree (M->data, M->data_size); @@ -1634,7 +1633,7 @@ void tgls_free_message_media (struct tgl_message_media *M) { } } -void tgls_free_message_action (struct tgl_message_action *M) { +void tgls_free_message_action (struct tgl_state *TLS, struct tgl_message_action *M) { switch (M->type) { case tgl_message_action_none: break; @@ -1646,7 +1645,7 @@ void tgls_free_message_action (struct tgl_message_action *M) { tfree_str (M->new_title); break; case tgl_message_action_chat_edit_photo: - tgls_free_photo (&M->photo); + tgls_free_photo (TLS, &M->photo); break; case tgl_message_action_chat_delete_photo: case tgl_message_action_chat_add_user: @@ -1667,55 +1666,55 @@ void tgls_free_message_action (struct tgl_message_action *M) { } } -void tgls_clear_message (struct tgl_message *M) { +void tgls_clear_message (struct tgl_state *TLS, struct tgl_message *M) { if (!M->service) { if (M->message) { tfree (M->message, M->message_len + 1); } - tgls_free_message_media (&M->media); + tgls_free_message_media (TLS, &M->media); } else { - tgls_free_message_action (&M->action); + tgls_free_message_action (TLS, &M->action); } } -void tgls_free_message (struct tgl_message *M) { - tgls_clear_message (M); +void tgls_free_message (struct tgl_state *TLS, struct tgl_message *M) { + tgls_clear_message (TLS, M); tfree (M, sizeof (*M)); } -void tgls_free_chat (struct tgl_chat *U) { +void tgls_free_chat (struct tgl_state *TLS, struct tgl_chat *U) { if (U->title) { tfree_str (U->title); } if (U->print_title) { tfree_str (U->print_title); } if (U->user_list) { tfree (U->user_list, U->user_list_size * 12); } - tgls_free_photo (&U->photo); + tgls_free_photo (TLS, &U->photo); tfree (U, sizeof (*U)); } -void tgls_free_user (struct tgl_user *U) { +void tgls_free_user (struct tgl_state *TLS, struct tgl_user *U) { if (U->first_name) { tfree_str (U->first_name); } if (U->last_name) { tfree_str (U->last_name); } if (U->print_name) { tfree_str (U->print_name); } if (U->phone) { tfree_str (U->phone); } if (U->real_first_name) { tfree_str (U->real_first_name); } if (U->real_last_name) { tfree_str (U->real_last_name); } - tgls_free_photo (&U->photo); + tgls_free_photo (TLS, &U->photo); tfree (U, sizeof (*U)); } -void tgls_free_encr_chat (struct tgl_secret_chat *U) { +void tgls_free_encr_chat (struct tgl_state *TLS, struct tgl_secret_chat *U) { if (U->print_name) { tfree_str (U->print_name); } if (U->g_key) { tfree (U->g_key, 256); } if (U->nonce) { tfree (U->nonce, 256); } tfree (U, sizeof (*U)); } -void tgls_free_peer (tgl_peer_t *P) { +void tgls_free_peer (struct tgl_state *TLS, tgl_peer_t *P) { if (tgl_get_peer_type (P->id) == TGL_PEER_USER) { - tgls_free_user ((void *)P); + tgls_free_user (TLS, (void *)P); } else if (tgl_get_peer_type (P->id) == TGL_PEER_CHAT) { - tgls_free_chat ((void *)P); + tgls_free_chat (TLS, (void *)P); } else if (tgl_get_peer_type (P->id) == TGL_PEER_ENCR_CHAT) { - tgls_free_encr_chat ((void *)P); + tgls_free_encr_chat (TLS, (void *)P); } else { assert (0); } @@ -1724,46 +1723,46 @@ void tgls_free_peer (tgl_peer_t *P) { /* Messages {{{ */ -void tglm_message_del_use (struct tgl_message *M) { +void tglm_message_del_use (struct tgl_state *TLS, struct tgl_message *M) { M->next_use->prev_use = M->prev_use; M->prev_use->next_use = M->next_use; } -void tglm_message_add_use (struct tgl_message *M) { - M->next_use = tgl_state.message_list.next_use; - M->prev_use = &tgl_state.message_list; +void tglm_message_add_use (struct tgl_state *TLS, struct tgl_message *M) { + M->next_use = TLS->message_list.next_use; + M->prev_use = &TLS->message_list; M->next_use->prev_use = M; M->prev_use->next_use = M; } -void tglm_message_add_peer (struct tgl_message *M) { +void tglm_message_add_peer (struct tgl_state *TLS, struct tgl_message *M) { tgl_peer_id_t id; - if (!tgl_cmp_peer_id (M->to_id, TGL_MK_USER (tgl_state.our_id))) { + if (!tgl_cmp_peer_id (M->to_id, TGL_MK_USER (TLS->our_id))) { id = M->from_id; } else { id = M->to_id; } - tgl_peer_t *P = tgl_peer_get (id); + tgl_peer_t *P = tgl_peer_get (TLS, id); if (!P) { P = talloc0 (sizeof (*P)); P->id = id; switch (tgl_get_peer_type (id)) { case TGL_PEER_USER: - tgl_state.users_allocated ++; + TLS->users_allocated ++; break; case TGL_PEER_CHAT: - tgl_state.chats_allocated ++; + TLS->chats_allocated ++; break; case TGL_PEER_GEO_CHAT: - tgl_state.geo_chats_allocated ++; + TLS->geo_chats_allocated ++; break; case TGL_PEER_ENCR_CHAT: - tgl_state.encr_chats_allocated ++; + TLS->encr_chats_allocated ++; break; } - tgl_state.peer_tree = tree_insert_peer (tgl_state.peer_tree, P, lrand48 ()); - increase_peer_size (); - tgl_state.Peers[tgl_state.peer_num ++] = P; + TLS->peer_tree = tree_insert_peer (TLS->peer_tree, P, lrand48 ()); + increase_peer_size (TLS); + TLS->Peers[TLS->peer_num ++] = P; } if (!P->last) { P->last = M; @@ -1794,14 +1793,14 @@ void tglm_message_add_peer (struct tgl_message *M) { } } -void tglm_message_del_peer (struct tgl_message *M) { +void tglm_message_del_peer (struct tgl_state *TLS, struct tgl_message *M) { tgl_peer_id_t id; - if (!tgl_cmp_peer_id (M->to_id, TGL_MK_USER (tgl_state.our_id))) { + if (!tgl_cmp_peer_id (M->to_id, TGL_MK_USER (TLS->our_id))) { id = M->from_id; } else { id = M->to_id; } - tgl_peer_t *P = tgl_peer_get (id); + tgl_peer_t *P = tgl_peer_get (TLS, id); if (M->prev) { M->prev->next = M->next; } @@ -1813,98 +1812,99 @@ void tglm_message_del_peer (struct tgl_message *M) { } } -struct tgl_message *tglm_message_alloc (long long id) { +struct tgl_message *tglm_message_alloc (struct tgl_state *TLS, long long id) { struct tgl_message *M = talloc0 (sizeof (*M)); M->id = id; - tglm_message_insert_tree (M); - tgl_state.messages_allocated ++; + tglm_message_insert_tree (TLS, M); + TLS->messages_allocated ++; return M; } -void tglm_update_message_id (struct tgl_message *M, long long id) { - tgl_state.message_tree = tree_delete_message (tgl_state.message_tree, M); +void tglm_update_message_id (struct tgl_state *TLS, struct tgl_message *M, long long id) { + TLS->message_tree = tree_delete_message (TLS->message_tree, M); M->id = id; - tgl_state.message_tree = tree_insert_message (tgl_state.message_tree, M, lrand48 ()); + TLS->message_tree = tree_insert_message (TLS->message_tree, M, lrand48 ()); } -void tglm_message_insert_tree (struct tgl_message *M) { +void tglm_message_insert_tree (struct tgl_state *TLS, struct tgl_message *M) { assert (M->id); - tgl_state.message_tree = tree_insert_message (tgl_state.message_tree, M, lrand48 ()); + TLS->message_tree = tree_insert_message (TLS->message_tree, M, lrand48 ()); } -void tglm_message_remove_tree (struct tgl_message *M) { +void tglm_message_remove_tree (struct tgl_state *TLS, struct tgl_message *M) { assert (M->id); - tgl_state.message_tree = tree_delete_message (tgl_state.message_tree, M); + TLS->message_tree = tree_delete_message (TLS->message_tree, M); } -void tglm_message_insert (struct tgl_message *M) { - tglm_message_add_use (M); - tglm_message_add_peer (M); +void tglm_message_insert (struct tgl_state *TLS, struct tgl_message *M) { + tglm_message_add_use (TLS, M); + tglm_message_add_peer (TLS, M); } -void tglm_message_insert_unsent (struct tgl_message *M) { - tgl_state.message_unsent_tree = tree_insert_message (tgl_state.message_unsent_tree, M, lrand48 ()); +void tglm_message_insert_unsent (struct tgl_state *TLS, struct tgl_message *M) { + TLS->message_unsent_tree = tree_insert_message (TLS->message_unsent_tree, M, lrand48 ()); } -void tglm_message_remove_unsent (struct tgl_message *M) { - tgl_state.message_unsent_tree = tree_delete_message (tgl_state.message_unsent_tree, M); +void tglm_message_remove_unsent (struct tgl_state *TLS, struct tgl_message *M) { + TLS->message_unsent_tree = tree_delete_message (TLS->message_unsent_tree, M); } -static void __send_msg (struct tgl_message *M) { +static void __send_msg (struct tgl_message *M, void *_TLS) { + struct tgl_state *TLS = _TLS; vlogprintf (E_NOTICE, "Resending message...\n"); //print_message (M); if (M->media.type != tgl_message_media_none) { assert (M->flags & FLAG_ENCRYPTED); - bl_do_delete_msg (M); + bl_do_delete_msg (TLS, M); } else { - tgl_do_send_msg (M, 0, 0); + tgl_do_send_msg (TLS, M, 0, 0); } } -void tglm_send_all_unsent (void ) { - tree_act_message (tgl_state.message_unsent_tree, __send_msg); +void tglm_send_all_unsent (struct tgl_state *TLS) { + tree_act_ex_message (TLS->message_unsent_tree, __send_msg, TLS); } /* }}} */ -void tglp_peer_insert_name (tgl_peer_t *P) { - tgl_state.peer_by_name_tree = tree_insert_peer_by_name (tgl_state.peer_by_name_tree, P, lrand48 ()); +void tglp_peer_insert_name (struct tgl_state *TLS, tgl_peer_t *P) { + TLS->peer_by_name_tree = tree_insert_peer_by_name (TLS->peer_by_name_tree, P, lrand48 ()); } -void tglp_peer_delete_name (tgl_peer_t *P) { - tgl_state.peer_by_name_tree = tree_delete_peer_by_name (tgl_state.peer_by_name_tree, P); +void tglp_peer_delete_name (struct tgl_state *TLS, tgl_peer_t *P) { + TLS->peer_by_name_tree = tree_delete_peer_by_name (TLS->peer_by_name_tree, P); } -tgl_peer_t *tgl_peer_get (tgl_peer_id_t id) { +tgl_peer_t *tgl_peer_get (struct tgl_state *TLS, tgl_peer_id_t id) { static tgl_peer_t U; U.id = id; - return tree_lookup_peer (tgl_state.peer_tree, &U); + return tree_lookup_peer (TLS->peer_tree, &U); } -struct tgl_message *tgl_message_get (long long id) { +struct tgl_message *tgl_message_get (struct tgl_state *TLS, long long id) { struct tgl_message M; M.id = id; - return tree_lookup_message (tgl_state.message_tree, &M); + return tree_lookup_message (TLS->message_tree, &M); } -tgl_peer_t *tgl_peer_get_by_name (const char *s) { +tgl_peer_t *tgl_peer_get_by_name (struct tgl_state *TLS, const char *s) { static tgl_peer_t P; P.print_name = (void *)s; - tgl_peer_t *R = tree_lookup_peer_by_name (tgl_state.peer_by_name_tree, &P); + tgl_peer_t *R = tree_lookup_peer_by_name (TLS->peer_by_name_tree, &P); return R; } -void tgl_peer_iterator_ex (void (*it)(tgl_peer_t *P, void *extra), void *extra) { - tree_act_ex_peer (tgl_state.peer_tree, it, extra); +void tgl_peer_iterator_ex (struct tgl_state *TLS, void (*it)(tgl_peer_t *P, void *extra), void *extra) { + tree_act_ex_peer (TLS->peer_tree, it, extra); } -int tgl_complete_user_list (int index, const char *text, int len, char **R) { +int tgl_complete_user_list (struct tgl_state *TLS, int index, const char *text, int len, char **R) { index ++; - while (index < tgl_state.peer_num && (!tgl_state.Peers[index]->print_name || strncmp (tgl_state.Peers[index]->print_name, text, len) || tgl_get_peer_type (tgl_state.Peers[index]->id) != TGL_PEER_USER)) { + while (index < TLS->peer_num && (!TLS->Peers[index]->print_name || strncmp (TLS->Peers[index]->print_name, text, len) || tgl_get_peer_type (TLS->Peers[index]->id) != TGL_PEER_USER)) { index ++; } - if (index < tgl_state.peer_num) { - *R = strdup (tgl_state.Peers[index]->print_name); + if (index < TLS->peer_num) { + *R = strdup (TLS->Peers[index]->print_name); assert (*R); return index; } else { @@ -1912,13 +1912,13 @@ int tgl_complete_user_list (int index, const char *text, int len, char **R) { } } -int tgl_complete_chat_list (int index, const char *text, int len, char **R) { +int tgl_complete_chat_list (struct tgl_state *TLS, int index, const char *text, int len, char **R) { index ++; - while (index < tgl_state.peer_num && (!tgl_state.Peers[index]->print_name || strncmp (tgl_state.Peers[index]->print_name, text, len) || tgl_get_peer_type (tgl_state.Peers[index]->id) != TGL_PEER_CHAT)) { + while (index < TLS->peer_num && (!TLS->Peers[index]->print_name || strncmp (TLS->Peers[index]->print_name, text, len) || tgl_get_peer_type (TLS->Peers[index]->id) != TGL_PEER_CHAT)) { index ++; } - if (index < tgl_state.peer_num) { - *R = strdup (tgl_state.Peers[index]->print_name); + if (index < TLS->peer_num) { + *R = strdup (TLS->Peers[index]->print_name); assert (*R); return index; } else { @@ -1926,13 +1926,13 @@ int tgl_complete_chat_list (int index, const char *text, int len, char **R) { } } -int tgl_complete_encr_chat_list (int index, const char *text, int len, char **R) { +int tgl_complete_encr_chat_list (struct tgl_state *TLS, int index, const char *text, int len, char **R) { index ++; - while (index < tgl_state.peer_num && (!tgl_state.Peers[index]->print_name || strncmp (tgl_state.Peers[index]->print_name, text, len) || tgl_get_peer_type (tgl_state.Peers[index]->id) != TGL_PEER_ENCR_CHAT)) { + while (index < TLS->peer_num && (!TLS->Peers[index]->print_name || strncmp (TLS->Peers[index]->print_name, text, len) || tgl_get_peer_type (TLS->Peers[index]->id) != TGL_PEER_ENCR_CHAT)) { index ++; } - if (index < tgl_state.peer_num) { - *R = strdup (tgl_state.Peers[index]->print_name); + if (index < TLS->peer_num) { + *R = strdup (TLS->Peers[index]->print_name); assert (*R); return index; } else { @@ -1940,13 +1940,13 @@ int tgl_complete_encr_chat_list (int index, const char *text, int len, char **R) } } -int tgl_complete_peer_list (int index, const char *text, int len, char **R) { +int tgl_complete_peer_list (struct tgl_state *TLS, int index, const char *text, int len, char **R) { index ++; - while (index < tgl_state.peer_num && (!tgl_state.Peers[index]->print_name || strncmp (tgl_state.Peers[index]->print_name, text, len))) { + while (index < TLS->peer_num && (!TLS->Peers[index]->print_name || strncmp (TLS->Peers[index]->print_name, text, len))) { index ++; } - if (index < tgl_state.peer_num) { - *R = strdup (tgl_state.Peers[index]->print_name); + if (index < TLS->peer_num) { + *R = strdup (TLS->Peers[index]->print_name); assert (*R); return index; } else { @@ -1954,45 +1954,53 @@ int tgl_complete_peer_list (int index, const char *text, int len, char **R) { } } -void tgl_free_all (void) { - tree_act_peer (tgl_state.peer_tree, tgls_free_peer); - tgl_state.peer_tree = tree_clear_peer (tgl_state.peer_tree); - tgl_state.peer_by_name_tree = tree_clear_peer_by_name (tgl_state.peer_by_name_tree); - tree_act_message (tgl_state.message_tree, tgls_free_message); - tgl_state.message_tree = tree_clear_message (tgl_state.message_tree); - tree_act_message (tgl_state.message_unsent_tree, tgls_free_message); - tgl_state.message_unsent_tree = tree_clear_message (tgl_state.message_unsent_tree); +void tgls_free_peer_gw (tgl_peer_t *P, void *TLS) { + tgls_free_peer (TLS, P); +} - if (tgl_state.encr_prime) { tfree (tgl_state.encr_prime, 256); } +void tgls_free_message_gw (struct tgl_message *M, void *TLS) { + tgls_free_message (TLS, M); +} + +void tgl_free_all (struct tgl_state *TLS) { + tree_act_ex_peer (TLS->peer_tree, tgls_free_peer_gw, TLS); + TLS->peer_tree = tree_clear_peer (TLS->peer_tree); + TLS->peer_by_name_tree = tree_clear_peer_by_name (TLS->peer_by_name_tree); + tree_act_ex_message (TLS->message_tree, tgls_free_message_gw, TLS); + TLS->message_tree = tree_clear_message (TLS->message_tree); + tree_act_ex_message (TLS->message_unsent_tree, tgls_free_message_gw, TLS); + TLS->message_unsent_tree = tree_clear_message (TLS->message_unsent_tree); + + if (TLS->encr_prime) { tfree (TLS->encr_prime, 256); } - if (tgl_state.binlog_name) { tfree_str (tgl_state.binlog_name); } - if (tgl_state.auth_file) { tfree_str (tgl_state.auth_file); } - if (tgl_state.downloads_directory) { tfree_str (tgl_state.downloads_directory); } + if (TLS->binlog_name) { tfree_str (TLS->binlog_name); } + if (TLS->auth_file) { tfree_str (TLS->auth_file); } + if (TLS->downloads_directory) { tfree_str (TLS->downloads_directory); } int i; - for (i = 0; i < tgl_state.rsa_key_num; i++) { - tfree_str (tgl_state.rsa_key_list[i]); + for (i = 0; i < TLS->rsa_key_num; i++) { + tfree_str (TLS->rsa_key_list[i]); } - for (i = 0; i <= tgl_state.max_dc_num; i++) if (tgl_state.DC_list[i]) { - tgls_free_dc (tgl_state.DC_list[i]); + for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i]) { + tgls_free_dc (TLS, TLS->DC_list[i]); } - BN_CTX_free (tgl_state.BN_ctx); - tgls_free_pubkey (); + BN_CTX_free (TLS->BN_ctx); + tgls_free_pubkey (TLS); } -int tgl_print_stat (char *s, int len) { +int tgl_print_stat (struct tgl_state *TLS, char *s, int len) { return tsnprintf (s, len, "users_allocated\t%d\n" "chats_allocated\t%d\n" "encr_chats_allocated\t%d\n" "peer_num\t%d\n" "messages_allocated\t%d\n", - tgl_state.users_allocated, - tgl_state.chats_allocated, - tgl_state.encr_chats_allocated, - tgl_state.peer_num, - tgl_state.messages_allocated + TLS->users_allocated, + TLS->chats_allocated, + TLS->encr_chats_allocated, + TLS->peer_num, + TLS->messages_allocated ); } diff --git a/structures.h b/structures.h index 92e2f60..b7855bb 100644 --- a/structures.h +++ b/structures.h @@ -23,34 +23,35 @@ #include #include "tgl-layout.h" #include "tgl-fetch.h" +#include "tgl.h" -char *tgls_default_create_print_name (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4); +char *tgls_default_create_print_name (struct tgl_state *TLS, tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4); -void tgls_free_user (struct tgl_user *U); -void tgls_free_chat (struct tgl_chat *U); -void tgls_free_photo (struct tgl_photo *P); -void tgls_free_message (struct tgl_message *M); -void tgls_clear_message (struct tgl_message *M); +void tgls_free_user (struct tgl_state *TLS, struct tgl_user *U); +void tgls_free_chat (struct tgl_state *TLS, struct tgl_chat *U); +void tgls_free_photo (struct tgl_state *TLS, struct tgl_photo *P); +void tgls_free_message (struct tgl_state *TLS, struct tgl_message *M); +void tgls_clear_message (struct tgl_state *TLS, struct tgl_message *M); -struct tgl_message *tglm_message_alloc (long long id); -void tglm_message_insert_tree (struct tgl_message *M); -void tglm_update_message_id (struct tgl_message *M, long long id); -void tglm_message_insert (struct tgl_message *M); -void tglm_message_insert_unsent (struct tgl_message *M); -void tglm_message_remove_unsent (struct tgl_message *M); -void tglm_send_all_unsent (void); -void tglm_message_remove_tree (struct tgl_message *M); -void tglm_message_add_peer (struct tgl_message *M); -void tglm_message_del_peer (struct tgl_message *M); -void tglm_message_del_use (struct tgl_message *M); -void tglm_message_add_use (struct tgl_message *M); +struct tgl_message *tglm_message_alloc (struct tgl_state *TLS, long long id); +void tglm_message_insert_tree (struct tgl_state *TLS, struct tgl_message *M); +void tglm_update_message_id (struct tgl_state *TLS, struct tgl_message *M, long long id); +void tglm_message_insert (struct tgl_state *TLS, struct tgl_message *M); +void tglm_message_insert_unsent (struct tgl_state *TLS, struct tgl_message *M); +void tglm_message_remove_unsent (struct tgl_state *TLS, struct tgl_message *M); +void tglm_send_all_unsent (struct tgl_state *TLS); +void tglm_message_remove_tree (struct tgl_state *TLS, struct tgl_message *M); +void tglm_message_add_peer (struct tgl_state *TLS, struct tgl_message *M); +void tglm_message_del_peer (struct tgl_state *TLS, struct tgl_message *M); +void tglm_message_del_use (struct tgl_state *TLS, struct tgl_message *M); +void tglm_message_add_use (struct tgl_state *TLS, struct tgl_message *M); -void tglp_peer_insert_name (tgl_peer_t *P); -void tglp_peer_delete_name (tgl_peer_t *P); -void tglp_insert_encrypted_chat (tgl_peer_t *P); -void tglp_insert_user (tgl_peer_t *P); -void tglp_insert_chat (tgl_peer_t *P); +void tglp_peer_insert_name (struct tgl_state *TLS, tgl_peer_t *P); +void tglp_peer_delete_name (struct tgl_state *TLS, tgl_peer_t *P); +void tglp_insert_encrypted_chat (struct tgl_state *TLS, tgl_peer_t *P); +void tglp_insert_user (struct tgl_state *TLS, tgl_peer_t *P); +void tglp_insert_chat (struct tgl_state *TLS, tgl_peer_t *P); enum tgl_typing_status tglf_fetch_typing (void); #endif diff --git a/tgl-fetch.h b/tgl-fetch.h index 3cc5998..c83170a 100644 --- a/tgl-fetch.h +++ b/tgl-fetch.h @@ -19,35 +19,36 @@ */ #ifndef __TGL_FETCH_H__ #define __TGL_FETCH_H__ +#include "tgl.h" -int tglf_fetch_file_location (struct tgl_file_location *loc); -int tglf_fetch_user_status (struct tgl_user_status *S); -int tglf_fetch_user (struct tgl_user *U); -struct tgl_user *tglf_fetch_alloc_user (void); -struct tgl_user *tglf_fetch_alloc_user_full (void); -struct tgl_chat *tglf_fetch_alloc_chat (void); -struct tgl_chat *tglf_fetch_alloc_chat_full (void); -struct tgl_secret_chat *tglf_fetch_alloc_encrypted_chat (void); -struct tgl_message *tglf_fetch_alloc_message (void); -struct tgl_message *tglf_fetch_alloc_geo_message (void); -struct tgl_message *tglf_fetch_alloc_message_short (void); -struct tgl_message *tglf_fetch_alloc_message_short_chat (void); -struct tgl_message *tglf_fetch_alloc_encrypted_message (void); -void tglf_fetch_encrypted_message_file (struct tgl_message_media *M); -tgl_peer_id_t tglf_fetch_peer_id (void); +int tglf_fetch_file_location (struct tgl_state *TLS, struct tgl_file_location *loc); +int tglf_fetch_user_status (struct tgl_state *TLS, struct tgl_user_status *S); +int tglf_fetch_user (struct tgl_state *TLS, struct tgl_user *U); +struct tgl_user *tglf_fetch_alloc_user (struct tgl_state *TLS); +struct tgl_user *tglf_fetch_alloc_user_full (struct tgl_state *TLS); +struct tgl_chat *tglf_fetch_alloc_chat (struct tgl_state *TLS); +struct tgl_chat *tglf_fetch_alloc_chat_full (struct tgl_state *TLS); +struct tgl_secret_chat *tglf_fetch_alloc_encrypted_chat (struct tgl_state *TLS); +struct tgl_message *tglf_fetch_alloc_message (struct tgl_state *TLS); +struct tgl_message *tglf_fetch_alloc_geo_message (struct tgl_state *TLS); +struct tgl_message *tglf_fetch_alloc_message_short (struct tgl_state *TLS); +struct tgl_message *tglf_fetch_alloc_message_short_chat (struct tgl_state *TLS); +struct tgl_message *tglf_fetch_alloc_encrypted_message (struct tgl_state *TLS); +void tglf_fetch_encrypted_message_file (struct tgl_state *TLS, struct tgl_message_media *M); +tgl_peer_id_t tglf_fetch_peer_id (struct tgl_state *TLS); -void tglf_fetch_message_media (struct tgl_message_media *M); -void tglf_fetch_message_media_encrypted (struct tgl_message_media *M); -void tglf_fetch_message_action (struct tgl_message_action *M); -void tglf_fetch_message_action_encrypted (struct tgl_message_action *M); -void tglf_fetch_photo (struct tgl_photo *P); +void tglf_fetch_message_media (struct tgl_state *TLS, struct tgl_message_media *M); +void tglf_fetch_message_media_encrypted (struct tgl_state *TLS, struct tgl_message_media *M); +void tglf_fetch_message_action (struct tgl_state *TLS, struct tgl_message_action *M); +void tglf_fetch_message_action_encrypted (struct tgl_state *TLS, struct tgl_message_action *M); +void tglf_fetch_photo (struct tgl_state *TLS, struct tgl_photo *P); -void tglf_fetch_chat (struct tgl_chat *C); -void tglf_fetch_chat_full (struct tgl_chat *C); +void tglf_fetch_chat (struct tgl_state *TLS, struct tgl_chat *C); +void tglf_fetch_chat_full (struct tgl_state *TLS, struct tgl_chat *C); -void tglf_fetch_audio (struct tgl_audio *V); -void tglf_fetch_video (struct tgl_video *V); -void tglf_fetch_document (struct tgl_document *V); -void tglf_fetch_message (struct tgl_message *M); -void tglf_fetch_geo_message (struct tgl_message *M); +void tglf_fetch_audio (struct tgl_state *TLS, struct tgl_audio *V); +void tglf_fetch_video (struct tgl_state *TLS, struct tgl_video *V); +void tglf_fetch_document (struct tgl_state *TLS, struct tgl_document *V); +void tglf_fetch_message (struct tgl_state *TLS, struct tgl_message *M); +void tglf_fetch_geo_message (struct tgl_state *TLS, struct tgl_message *M); #endif diff --git a/tgl-inner.h b/tgl-inner.h index a5b4892..acb3855 100644 --- a/tgl-inner.h +++ b/tgl-inner.h @@ -23,8 +23,8 @@ #define vlogprintf(verbosity_level,...) \ do { \ - if (tgl_state.verbosity >= verbosity_level) { \ - tgl_state.callback.logprintf (__VA_ARGS__); \ + if (TLS->verbosity >= verbosity_level) { \ + TLS->callback.logprintf (__VA_ARGS__); \ } \ } while (0) diff --git a/tgl-layout.h b/tgl-layout.h index 089d1b1..a11c15f 100644 --- a/tgl-layout.h +++ b/tgl-layout.h @@ -62,8 +62,7 @@ struct tgl_session { int seq_no; struct connection *c; struct tree_long *ack_tree; - struct event *ev; - //struct event_timer ev; + struct tgl_timer *ev; }; struct tgl_dc { @@ -83,7 +82,7 @@ struct tgl_dc { long long temp_auth_key_id; long long server_salt; - struct event *ev; + struct tgl_timer *ev; int server_time_delta; double server_time_udelta; diff --git a/tgl-timers.c b/tgl-timers.c new file mode 100644 index 0000000..b78581a --- /dev/null +++ b/tgl-timers.c @@ -0,0 +1,69 @@ +/* + This file is part of tgl-library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + Copyright Vitaly Valtman 2013-2014 +*/ +#include "config.h" +#ifdef EVENT_V2 +#include +#include +#include +#else +#include +#include "event-old.h" +#endif + +#include "tgl.h" +#include + +static void timer_alarm (evutil_socket_t fd, short what, void *arg) { + void **p = arg; + ((void (*)(struct tgl_state *, void *))p[1]) (p[0], p[2]); +} + +struct tgl_timer *tgl_timer_alloc (struct tgl_state *TLS, void (*cb)(struct tgl_state *TLS, void *arg), void *arg) { + void **p = malloc (sizeof (void *) * 3); + p[0] = TLS; + p[1] = cb; + p[2] = arg; + return (void *)evtimer_new (TLS->ev_base, timer_alarm, p); +} + +void tgl_timer_insert (struct tgl_timer *t, double p) { + if (p < 0) { p = 0; } + double e = p - (int)p; + if (e < 0) { e = 0; } + struct timeval pv = { (int)p, (int)(e * 1e6)}; + event_add ((void *)t, &pv); +} + +void tgl_timer_delete (struct tgl_timer *t) { + event_del ((void *)t); +} + +void tgl_timer_free (struct tgl_timer *t) { + void *arg = event_get_callback_arg ((void *)t); + free (arg); + event_free ((void *)t); +} + +struct tgl_timer_methods tgl_libevent_timers = { + .alloc = tgl_timer_alloc, + .insert = tgl_timer_insert, + .delete = tgl_timer_delete, + .free = tgl_timer_free +}; diff --git a/tgl-timers.h b/tgl-timers.h new file mode 100644 index 0000000..052dd56 --- /dev/null +++ b/tgl-timers.h @@ -0,0 +1,27 @@ +/* + This file is part of tgl-library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + Copyright Vitaly Valtman 2013-2014 +*/ + +#ifndef __TGL_TIMERS_H__ +#define __TGL_TIMERS_H__ + +#include "tgl.h" +extern struct tgl_timer_methods tgl_libevent_timers; + +#endif diff --git a/tgl.c b/tgl.c index cca3076..c6a1d77 100644 --- a/tgl.c +++ b/tgl.c @@ -26,70 +26,60 @@ #include "tools.h" #include "mtproto-client.h" #include "structures.h" -#include "net.h" - -#ifdef EVENT_V2 -#include -#else -#include -#include "event-old.h" -#endif +//#include "net.h" #include struct tgl_state tgl_state; -void tgl_set_binlog_mode (int mode) { - tgl_state.binlog_enabled = mode; +void tgl_set_binlog_mode (struct tgl_state *TLS, int mode) { + TLS->binlog_enabled = mode; } -void tgl_set_binlog_path (const char *path) { - tgl_state.binlog_name = tstrdup (path); +void tgl_set_binlog_path (struct tgl_state *TLS, const char *path) { + TLS->binlog_name = tstrdup (path); } -void tgl_set_auth_file_path (const char *path) { - tgl_state.auth_file = tstrdup (path); +void tgl_set_auth_file_path (struct tgl_state *TLS, const char *path) { + TLS->auth_file = tstrdup (path); } -void tgl_set_download_directory (const char *path) { - tgl_state.downloads_directory = tstrdup (path); +void tgl_set_download_directory (struct tgl_state *TLS, const char *path) { + TLS->downloads_directory = tstrdup (path); } -void tgl_set_callback (struct tgl_update_callback *cb) { - tgl_state.callback = *cb; +void tgl_set_callback (struct tgl_state *TLS, struct tgl_update_callback *cb) { + TLS->callback = *cb; } -void tgl_set_rsa_key (const char *key) { - assert (tgl_state.rsa_key_num < TGL_MAX_RSA_KEYS_NUM); - tgl_state.rsa_key_list[tgl_state.rsa_key_num ++] = tstrdup (key); +void tgl_set_rsa_key (struct tgl_state *TLS, const char *key) { + assert (TLS->rsa_key_num < TGL_MAX_RSA_KEYS_NUM); + TLS->rsa_key_list[TLS->rsa_key_num ++] = tstrdup (key); } -void tgl_init (void) { - tgl_state.ev_base = event_base_new (); - - if (!tgl_state.net_methods) { - tgl_state.net_methods = &tgl_conn_methods; +void tgl_init (struct tgl_state *TLS) { + assert (TLS->timer_methods); + assert (TLS->net_methods); + if (!TLS->callback.create_print_name) { + TLS->callback.create_print_name = tgls_default_create_print_name; } - if (!tgl_state.callback.create_print_name) { - tgl_state.callback.create_print_name = tgls_default_create_print_name; - } - if (!tgl_state.temp_key_expire_time) { - tgl_state.temp_key_expire_time = 100000; + if (!TLS->temp_key_expire_time) { + TLS->temp_key_expire_time = 100000; } - tgl_state.message_list.next_use = &tgl_state.message_list; - tgl_state.message_list.prev_use = &tgl_state.message_list; + TLS->message_list.next_use = &TLS->message_list; + TLS->message_list.prev_use = &TLS->message_list; - tglmp_on_start (); + tglmp_on_start (TLS); } -int tgl_authorized_dc (struct tgl_dc *DC) { +int tgl_authorized_dc (struct tgl_state *TLS, struct tgl_dc *DC) { assert (DC); return (DC->flags & 4) != 0;//DC->auth_key_id; } -int tgl_signed_dc (struct tgl_dc *DC) { +int tgl_signed_dc (struct tgl_state *TLS, struct tgl_dc *DC) { assert (DC); return DC->has_auth; } diff --git a/tgl.h b/tgl.h index 13ccd99..897a13e 100644 --- a/tgl.h +++ b/tgl.h @@ -36,8 +36,8 @@ #define TG_SERVER_TEST_3 "174.140.142.5" // JUST RANDOM STRING -#define TGL_BUILD "2234" -#define TGL_VERSION "1.0.6" +#define TGL_BUILD "2590" +#define TGL_VERSION "1.1.0" #define TGL_ENCRYPTED_LAYER 18 @@ -72,36 +72,26 @@ struct tgl_allocator { }; extern struct tgl_allocator tgl_allocator_release; extern struct tgl_allocator tgl_allocator_debug; +struct tgl_state; struct tgl_update_callback { - void (*new_msg)(struct tgl_message *M); - void (*marked_read)(int num, struct tgl_message *list[]); + void (*new_msg)(struct tgl_state *TLS, struct tgl_message *M); + void (*marked_read)(struct tgl_state *TLS, int num, struct tgl_message *list[]); void (*logprintf)(const char *format, ...) __attribute__ ((format (printf, 1, 2))); - void (*type_notification)(struct tgl_user *U, enum tgl_typing_status status); - void (*type_in_chat_notification)(struct tgl_user *U, struct tgl_chat *C, enum tgl_typing_status status); - void (*type_in_secret_chat_notification)(struct tgl_secret_chat *E); - void (*status_notification)(struct tgl_user *U); - void (*user_registered)(struct tgl_user *U); - void (*user_activated)(struct tgl_user *U); - void (*new_authorization)(const char *device, const char *location); - //void (*secret_chat_created)(struct tgl_secret_chat *E); - //void (*secret_chat_request)(struct tgl_secret_chat *E); - //void (*secret_chat_established)(struct tgl_secret_chat *E); - //void (*secret_chat_deleted)(struct tgl_secret_chat *E); - //void (*secret_chat_accepted)(struct tgl_secret_chat *E); - //void (*new_user)(struct tgl_user *U); - //void (*delete_user)(struct tgl_user *U); - //void (*update_user_info)(struct tgl_user *U); - //void (*secret_chat_delete)(struct tgl_secret_chat *U); - //void (*secret_chat_requested)(struct tgl_secret_chat *U); - //void (*secret_chat_accepted)(struct tgl_secret_chat *U); - //void (*secret_chat_created)(struct tgl_secret_chat *U); - void (*chat_update)(struct tgl_chat *C, unsigned flags); - void (*user_update)(struct tgl_user *C, unsigned flags); - void (*secret_chat_update)(struct tgl_secret_chat *C, unsigned flags); - void (*msg_receive)(struct tgl_message *M); - void (*our_id)(int id); - char *(*create_print_name) (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4); + void (*type_notification)(struct tgl_state *TLS, struct tgl_user *U, enum tgl_typing_status status); + void (*type_in_chat_notification)(struct tgl_state *TLS, struct tgl_user *U, struct tgl_chat *C, enum tgl_typing_status status); + void (*type_in_secret_chat_notification)(struct tgl_state *TLS, struct tgl_secret_chat *E); + void (*status_notification)(struct tgl_state *TLS, struct tgl_user *U); + void (*user_registered)(struct tgl_state *TLS, struct tgl_user *U); + void (*user_activated)(struct tgl_state *TLS, struct tgl_user *U); + void (*new_authorization)(struct tgl_state *TLS, const char *device, const char *location); + void (*chat_update)(struct tgl_state *TLS, struct tgl_chat *C, unsigned flags); + void (*user_update)(struct tgl_state *TLS, struct tgl_user *C, unsigned flags); + void (*secret_chat_update)(struct tgl_state *TLS, struct tgl_secret_chat *C, unsigned flags); + void (*msg_receive)(struct tgl_state *TLS, struct tgl_message *M); + void (*our_id)(struct tgl_state *TLS, int id); + void (*notification)(struct tgl_state *TLS, char *type, char *message); + char *(*create_print_name) (struct tgl_state *TLS, tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4); }; struct tgl_net_methods { @@ -114,9 +104,23 @@ struct tgl_net_methods { struct tgl_dc *(*get_dc) (struct connection *c); struct tgl_session *(*get_session) (struct connection *c); - struct connection *(*create_connection) (const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods); + struct connection *(*create_connection) (struct tgl_state *TLS, const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods); }; +struct mtproto_methods { + int (*ready) (struct tgl_state *TLS, struct connection *c); + int (*close) (struct tgl_state *TLS, struct connection *c); + int (*execute) (struct tgl_state *TLS, struct connection *c, int op, int len); +}; + +struct tgl_timer; + +struct tgl_timer_methods { + struct tgl_timer *(*alloc) (struct tgl_state *TLS, void (*cb)(struct tgl_state *TLS, void *arg), void *arg); + void (*insert) (struct tgl_timer *t, double timeout); + void (*delete) (struct tgl_timer *t); + void (*free) (struct tgl_timer *t); +}; #define E_ERROR 0 #define E_WARNING 1 @@ -128,6 +132,7 @@ struct tgl_net_methods { #define TGL_MAX_RSA_KEYS_NUM 10 // Do not modify this structure, unless you know what you do +#pragma pack(push,4) struct tgl_state { int our_id; // ID of logged in user int encr_root; @@ -164,7 +169,7 @@ struct tgl_state { struct tgl_update_callback callback; struct tgl_net_methods *net_methods; - struct event_base *ev_base; + void *ev_base; char *rsa_key_list[TGL_MAX_RSA_KEYS_NUM]; int rsa_key_num; @@ -189,23 +194,30 @@ struct tgl_state { struct tgl_message message_list; int binlog_fd; + + struct tgl_timer_methods *timer_methods; + + void *pubKey; + + struct tree_query *queries_tree; }; -extern struct tgl_state tgl_state; +#pragma pack(pop) +//extern struct tgl_state tgl_state; -void tgl_reopen_binlog_for_writing (void); -void tgl_replay_log (void); +void tgl_reopen_binlog_for_writing (struct tgl_state *TLS); +void tgl_replay_log (struct tgl_state *TLS); -int tgl_print_stat (char *s, int len); -tgl_peer_t *tgl_peer_get (tgl_peer_id_t id); -tgl_peer_t *tgl_peer_get_by_name (const char *s); +int tgl_print_stat (struct tgl_state *TLS, char *s, int len); +tgl_peer_t *tgl_peer_get (struct tgl_state *TLS, tgl_peer_id_t id); +tgl_peer_t *tgl_peer_get_by_name (struct tgl_state *TLS, const char *s); -struct tgl_message *tgl_message_get (long long id); -void tgl_peer_iterator_ex (void (*it)(tgl_peer_t *P, void *extra), void *extra); +struct tgl_message *tgl_message_get (struct tgl_state *TLS, long long id); +void tgl_peer_iterator_ex (struct tgl_state *TLS, void (*it)(tgl_peer_t *P, void *extra), void *extra); -int tgl_complete_user_list (int index, const char *text, int len, char **R); -int tgl_complete_chat_list (int index, const char *text, int len, char **R); -int tgl_complete_encr_chat_list (int index, const char *text, int len, char **R); -int tgl_complete_peer_list (int index, const char *text, int len, char **R); +int tgl_complete_user_list (struct tgl_state *TLS, int index, const char *text, int len, char **R); +int tgl_complete_chat_list (struct tgl_state *TLS, int index, const char *text, int len, char **R); +int tgl_complete_encr_chat_list (struct tgl_state *TLS, int index, const char *text, int len, char **R); +int tgl_complete_peer_list (struct tgl_state *TLS, int index, const char *text, int len, char **R); #define TGL_PEER_USER 1 #define TGL_PEER_CHAT 2 @@ -218,12 +230,12 @@ int tgl_complete_peer_list (int index, const char *text, int len, char **R); #define TGL_MK_GEO_CHAT(id) tgl_set_peer_id (TGL_PEER_GEO_CHAT,id) #define TGL_MK_ENCR_CHAT(id) tgl_set_peer_id (TGL_PEER_ENCR_CHAT,id) -void tgl_set_binlog_mode (int mode); -void tgl_set_binlog_path (const char *path); -void tgl_set_auth_file_path (const char *path); -void tgl_set_download_directory (const char *path); -void tgl_set_callback (struct tgl_update_callback *cb); -void tgl_set_rsa_key (const char *key); +void tgl_set_binlog_mode (struct tgl_state *TLS, int mode); +void tgl_set_binlog_path (struct tgl_state *TLS, const char *path); +void tgl_set_auth_file_path (struct tgl_state *TLS, const char *path); +void tgl_set_download_directory (struct tgl_state *TLS, const char *path); +void tgl_set_callback (struct tgl_state *TLS, struct tgl_update_callback *cb); +void tgl_set_rsa_key (struct tgl_state *TLS, const char *key); static inline int tgl_get_peer_type (tgl_peer_id_t id) { @@ -245,116 +257,130 @@ static inline int tgl_cmp_peer_id (tgl_peer_id_t a, tgl_peer_id_t b) { return memcmp (&a, &b, sizeof (a)); } -static inline void tgl_incr_verbosity (void) { - tgl_state.verbosity ++; +static inline void tgl_incr_verbosity (struct tgl_state *TLS) { + TLS->verbosity ++; } -static inline void tgl_set_verbosity (int val) { - tgl_state.verbosity = val; +static inline void tgl_set_verbosity (struct tgl_state *TLS, int val) { + TLS->verbosity = val; } -static inline void tgl_enable_pfs (void) { - tgl_state.enable_pfs = 1; +static inline void tgl_enable_pfs (struct tgl_state *TLS) { + TLS->enable_pfs = 1; } -static inline void tgl_set_test_mode (void) { - tgl_state.test_mode ++; +static inline void tgl_set_test_mode (struct tgl_state *TLS) { + TLS->test_mode ++; } -struct pollfd; -int tgl_connections_make_poll_array (struct pollfd *fds, int max); -void tgl_connections_poll_result (struct pollfd *fds, int max); +static inline void tgl_set_net_methods (struct tgl_state *TLS, struct tgl_net_methods *methods) { + TLS->net_methods = methods; +} -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_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 *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 *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_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_text (tgl_peer_id_t id, char *file, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); -void tgl_do_mark_read (tgl_peer_id_t id, void (*callback)(void *callback_extra, int success), void *callback_extra); -void tgl_do_get_history (tgl_peer_id_t id, int limit, int offline_mode, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra); -void tgl_do_get_history_ext (tgl_peer_id_t id, int offset, int limit, int offline_mode, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra); -void tgl_do_get_dialog_list (void (*callback)(void *callback_extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]), void *callback_extra); -void tgl_do_send_photo (enum tgl_message_media_type type, tgl_peer_id_t to_id, char *file_name, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); -void tgl_do_set_chat_photo (tgl_peer_id_t chat_id, char *file_name, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); -void tgl_do_set_profile_photo (char *file_name, void (*callback)(void *callback_extra, int success), void *callback_extra); -void tgl_do_set_profile_name (char *first_name, char *last_name, void (*callback)(void *callback_extra, int success, struct tgl_user *U), void *callback_extra); -void tgl_do_set_username (char *name, void (*callback)(void *callback_extra, int success, struct tgl_user *U), void *callback_extra); -void tgl_do_forward_message (tgl_peer_id_t id, int n, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); -void tgl_do_rename_chat (tgl_peer_id_t id, char *name, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); -void tgl_do_get_chat_info (tgl_peer_id_t id, int offline_mode, void (*callback)(void *callback_extra, int success, struct tgl_chat *C), void *callback_extra); -void tgl_do_get_user_info (tgl_peer_id_t id, int offline_mode, void (*callback)(void *callback_extra, int success, struct tgl_user *U), void *callback_extra); -void tgl_do_load_photo (struct tgl_photo *photo, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra); -void tgl_do_load_video_thumb (struct tgl_video *video, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra); -void tgl_do_load_audio (struct tgl_audio *V, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra); -void tgl_do_load_video (struct tgl_video *V, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra); -void tgl_do_load_document (struct tgl_document *V, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra); -void tgl_do_load_document_thumb (struct tgl_document *video, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra); -void tgl_do_load_encr_video (struct tgl_encr_video *V, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra); -void tgl_do_export_auth (int num, void (*callback) (void *callback_extra, int success), void *callback_extra); -void tgl_do_add_contact (const char *phone, int phone_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, int force, void (*callback)(void *callback_extra, int success, int size, struct tgl_user *users[]), void *callback_extra); -void tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, int offset, const char *s, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra); -//void tgl_do_contacts_search (int limit, const char *s, void (*callback) (void *callback_extra, int success, int size, struct tgl_user *users[]), void *callback_extra); -void tgl_do_create_encr_chat_request (int user_id, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra); -void tgl_do_create_secret_chat (tgl_peer_id_t id, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra); -void tgl_do_accept_encr_chat_request (struct tgl_secret_chat *E, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra); -void tgl_do_get_difference (int sync_from_start, void (*callback)(void *callback_extra, int success), void *callback_extra); -void tgl_do_lookup_state (void); -void tgl_do_add_user_to_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, int limit, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); -void tgl_do_del_user_from_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); -void tgl_do_create_group_chat (tgl_peer_id_t id, char *chat_topic, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); -void tgl_do_create_group_chat_ex (int users_num, tgl_peer_id_t ids[], char *chat_topic, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); -void tgl_do_delete_msg (long long id, void (*callback)(void *callback_extra, int success), void *callback_extra); -void tgl_do_restore_msg (long long id, void (*callback)(void *callback_extra, int success), void *callback_extra); -void tgl_do_update_status (int online, void (*callback)(void *callback_extra, int success), void *callback_extra); -void tgl_do_help_get_config_dc (struct tgl_dc *D, void (*callback)(void *, int), void *callback_extra); -void tgl_do_export_card (void (*callback)(void *callback_extra, int success, int size, int *card), void *callback_extra); -void tgl_do_import_card (int size, int *card, void (*callback)(void *callback_extra, int success, struct tgl_user *U), void *callback_extra); -void tgl_do_send_contact (tgl_peer_id_t id, const char *phone, int phone_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); -void tgl_do_forward_media (tgl_peer_id_t id, int n, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); -void tgl_do_del_contact (tgl_peer_id_t id, void (*callback)(void *callback_extra, int success), void *callback_extra); -void tgl_do_set_encr_chat_ttl (struct tgl_secret_chat *E, int ttl, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); -void tgl_do_send_location(tgl_peer_id_t id, double latitude, double longitude, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra); -void tgl_do_contact_search (char *name, int limit, void (*callback)(void *callback_extra, int success, int cnt, struct tgl_user *U[]), void *callback_extra); +static inline void tgl_set_timer_methods (struct tgl_state *TLS, struct tgl_timer_methods *methods) { + TLS->timer_methods = methods; +} + +static inline void tgl_set_ev_base (struct tgl_state *TLS, void *ev_base) { + TLS->ev_base = ev_base; +} + +//struct pollfd; +//int tgl_connections_make_poll_array (struct tgl_state *TLS, struct pollfd *fds, int max); +//void tgl_connections_poll_result (struct tgl_state *TLS, struct pollfd *fds, int max); + +void tgl_do_help_get_config (struct tgl_state *TLS, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra); +void tgl_do_send_code (struct tgl_state *TLS, const char *user, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int registered, const char *hash), void *callback_extra); +void tgl_do_phone_call (struct tgl_state *TLS, const char *user, const char *hash, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra); +int tgl_do_send_code_result (struct tgl_state *TLS, const char *user, const char *hash, const char *code, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) ; +int tgl_do_send_code_result_auth (struct tgl_state *TLS, const char *user, const char *hash, const char *code, const char *first_name, const char *last_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *Self), void *callback_extra); +void tgl_do_update_contact_list (struct tgl_state *TLS, void (*callback) (struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_user *contacts[]), void *callback_extra); +void tgl_do_send_message (struct tgl_state *TLS, tgl_peer_id_t id, const char *msg, int len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_send_msg (struct tgl_state *TLS, struct tgl_message *M, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_send_text (struct tgl_state *TLS, tgl_peer_id_t id, char *file, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_mark_read (struct tgl_state *TLS, tgl_peer_id_t id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra); +void tgl_do_get_history (struct tgl_state *TLS, tgl_peer_id_t id, int limit, int offline_mode, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra); +void tgl_do_get_history_ext (struct tgl_state *TLS, tgl_peer_id_t id, int offset, int limit, int offline_mode, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra); +void tgl_do_get_dialog_list (struct tgl_state *TLS, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]), void *callback_extra); +void tgl_do_send_photo (struct tgl_state *TLS, enum tgl_message_media_type type, tgl_peer_id_t to_id, char *file_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_set_chat_photo (struct tgl_state *TLS, tgl_peer_id_t chat_id, char *file_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_set_profile_photo (struct tgl_state *TLS, char *file_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra); +void tgl_do_set_profile_name (struct tgl_state *TLS, char *first_name, char *last_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *U), void *callback_extra); +void tgl_do_set_username (struct tgl_state *TLS, char *name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *U), void *callback_extra); +void tgl_do_forward_message (struct tgl_state *TLS, tgl_peer_id_t id, int n, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_rename_chat (struct tgl_state *TLS, tgl_peer_id_t id, char *name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_get_chat_info (struct tgl_state *TLS, tgl_peer_id_t id, int offline_mode, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_chat *C), void *callback_extra); +void tgl_do_get_user_info (struct tgl_state *TLS, tgl_peer_id_t id, int offline_mode, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *U), void *callback_extra); +void tgl_do_load_photo (struct tgl_state *TLS, struct tgl_photo *photo, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra); +void tgl_do_load_video_thumb (struct tgl_state *TLS, struct tgl_video *video, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra); +void tgl_do_load_audio (struct tgl_state *TLS, struct tgl_audio *V, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra); +void tgl_do_load_video (struct tgl_state *TLS, struct tgl_video *V, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra); +void tgl_do_load_document (struct tgl_state *TLS, struct tgl_document *V, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra); +void tgl_do_load_document_thumb (struct tgl_state *TLS, struct tgl_document *video, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra); +void tgl_do_load_encr_video (struct tgl_state *TLS, struct tgl_encr_video *V, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra); +void tgl_do_export_auth (struct tgl_state *TLS, int num, void (*callback) (struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra); +void tgl_do_add_contact (struct tgl_state *TLS, const char *phone, int phone_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, int force, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_user *users[]), void *callback_extra); +void tgl_do_msg_search (struct tgl_state *TLS, tgl_peer_id_t id, int from, int to, int limit, int offset, const char *s, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra); +//void tgl_do_contacts_search (int limit, const char *s, void (*callback) (struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_user *users[]), void *callback_extra); +void tgl_do_create_encr_chat_request (struct tgl_state *TLS, int user_id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra); +void tgl_do_create_secret_chat (struct tgl_state *TLS, tgl_peer_id_t id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra); +void tgl_do_accept_encr_chat_request (struct tgl_state *TLS, struct tgl_secret_chat *E, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra); +void tgl_do_get_difference (struct tgl_state *TLS, int sync_from_start, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra); +void tgl_do_lookup_state (struct tgl_state *TLS); +void tgl_do_add_user_to_chat (struct tgl_state *TLS, tgl_peer_id_t chat_id, tgl_peer_id_t id, int limit, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_del_user_from_chat (struct tgl_state *TLS, tgl_peer_id_t chat_id, tgl_peer_id_t id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_create_group_chat (struct tgl_state *TLS, tgl_peer_id_t id, char *chat_topic, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_create_group_chat_ex (struct tgl_state *TLS, int users_num, tgl_peer_id_t ids[], char *chat_topic, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_delete_msg (struct tgl_state *TLS, long long id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra); +void tgl_do_restore_msg (struct tgl_state *TLS, long long id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra); +void tgl_do_update_status (struct tgl_state *TLS, int online, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra); +void tgl_do_help_get_config_dc (struct tgl_state *TLS, struct tgl_dc *D, void (*callback)(struct tgl_state *TLS, void *, int), void *callback_extra); +void tgl_do_export_card (struct tgl_state *TLS, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, int *card), void *callback_extra); +void tgl_do_import_card (struct tgl_state *TLS, int size, int *card, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *U), void *callback_extra); +void tgl_do_send_contact (struct tgl_state *TLS, tgl_peer_id_t id, const char *phone, int phone_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_forward_media (struct tgl_state *TLS, tgl_peer_id_t id, int n, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_del_contact (struct tgl_state *TLS, tgl_peer_id_t id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra); +void tgl_do_set_encr_chat_ttl (struct tgl_state *TLS, struct tgl_secret_chat *E, int ttl, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_send_location (struct tgl_state *TLS, tgl_peer_id_t id, double latitude, double longitude, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra); +void tgl_do_contact_search (struct tgl_state *TLS, char *name, int limit, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int cnt, struct tgl_user *U[]), void *callback_extra); -void tgl_do_visualize_key (tgl_peer_id_t id, unsigned char buf[16]); +void tgl_do_visualize_key (struct tgl_state *TLS, tgl_peer_id_t id, unsigned char buf[16]); -void tgl_do_send_ping (struct connection *c); +void tgl_do_send_ping (struct tgl_state *TLS, struct connection *c); -int tgl_authorized_dc (struct tgl_dc *DC); -int tgl_signed_dc (struct tgl_dc *DC); +void tgl_do_send_extf (struct tgl_state *TLS, char *data, int data_len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *data), void *callback_extra); + +int tgl_authorized_dc (struct tgl_state *TLS, struct tgl_dc *DC); +int tgl_signed_dc (struct tgl_state *TLS, struct tgl_dc *DC); //void tgl_do_get_suggested (void); -void tgl_do_create_keys_end (struct tgl_secret_chat *U); -void tgl_do_send_encr_chat_layer (struct tgl_secret_chat *E); +void tgl_do_create_keys_end (struct tgl_state *TLS, struct tgl_secret_chat *U); +void tgl_do_send_encr_chat_layer (struct tgl_state *TLS, struct tgl_secret_chat *E); -struct mtproto_methods { - int (*ready) (struct connection *c); - int (*close) (struct connection *c); - int (*execute) (struct connection *c, int op, int len); -}; +void tgl_init (struct tgl_state *TLS); +void tgl_dc_authorize (struct tgl_state *TLS, struct tgl_dc *DC); -void tgl_init (void); -void tgl_dc_authorize (struct tgl_dc *DC); - -void tgl_dc_iterator (void (*iterator)(struct tgl_dc *DC)); -void tgl_dc_iterator_ex (void (*iterator)(struct tgl_dc *DC, void *extra), void *extra); +void tgl_dc_iterator (struct tgl_state *TLS, void (*iterator)(struct tgl_dc *DC)); +void tgl_dc_iterator_ex (struct tgl_state *TLS, void (*iterator)(struct tgl_dc *DC, void *extra), void *extra); double tglt_get_double_time (void); -void tgl_insert_empty_user (int id); -void tgl_insert_empty_chat (int id); +void tgl_insert_empty_user (struct tgl_state *TLS, int id); +void tgl_insert_empty_chat (struct tgl_state *TLS, int id); -int tglf_extf_autocomplete (const char *text, int text_len, int index, char **R, char *data, int data_len); -struct paramed_type *tglf_extf_store (const char *data, int data_len); -void tgl_do_send_extf (char *data, int data_len, void (*callback)(void *callback_extra, int success, char *data), void *callback_extra); -char *tglf_extf_fetch (struct paramed_type *T); +int tglf_extf_autocomplete (struct tgl_state *TLS, const char *text, int text_len, int index, char **R, char *data, int data_len); +struct paramed_type *tglf_extf_store (struct tgl_state *TLS, const char *data, int data_len); +char *tglf_extf_fetch (struct tgl_state *TLS, struct paramed_type *T); -void tgl_free_all (void); +void tgl_free_all (struct tgl_state *TLS); + +static inline struct tgl_state *tgl_state_alloc (void) { + struct tgl_state *TLS = malloc (sizeof (*TLS)); + if (!TLS) { return NULL; } + memset (TLS, 0, sizeof (*TLS)); + return TLS; +} #endif diff --git a/updates.c b/updates.c index 514bf87..c7f2f9f 100644 --- a/updates.c +++ b/updates.c @@ -26,27 +26,27 @@ #include -void tglu_fetch_pts (void) { +void tglu_fetch_pts (struct tgl_state *TLS) { int p = fetch_int (); - if (p <= tgl_state.pts) { return; } - bl_do_set_pts (p); + if (p <= TLS->pts) { return; } + bl_do_set_pts (TLS, p); } -void tglu_fetch_qts (void) { +void tglu_fetch_qts (struct tgl_state *TLS) { int p = fetch_int (); - if (p <= tgl_state.qts) { return; } - bl_do_set_qts (p); + if (p <= TLS->qts) { return; } + bl_do_set_qts (TLS, p); } -void tglu_fetch_date (void) { +void tglu_fetch_date (struct tgl_state *TLS) { int p = fetch_int (); - if (p > tgl_state.date) { - //tgl_state.date = p; - bl_do_set_date (tgl_state.date); + if (p > TLS->date) { + //TLS->date = p; + bl_do_set_date (TLS, TLS->date); } } -static void fetch_dc_option (void) { +static void fetch_dc_option (struct tgl_state *TLS) { assert (fetch_int () == CODE_dc_option); int id = fetch_int (); int l1 = prefetch_strlen (); @@ -56,27 +56,27 @@ static void fetch_dc_option (void) { int port = fetch_int (); vlogprintf (E_DEBUG, "id = %d, name = %.*s ip = %.*s port = %d\n", id, l1, name, l2, ip, port); - bl_do_dc_option (id, l1, name, l2, ip, port); + bl_do_dc_option (TLS, id, l1, name, l2, ip, port); } -void tglu_work_update (struct connection *c, long long msg_id) { +void tglu_work_update (struct tgl_state *TLS, struct connection *c, long long msg_id) { unsigned op = fetch_int (); switch (op) { case CODE_update_new_message: { - struct tgl_message *M = tglf_fetch_alloc_message (); + struct tgl_message *M = tglf_fetch_alloc_message (TLS); assert (M); - tglu_fetch_pts (); - bl_do_msg_update (M->id); + tglu_fetch_pts (TLS); + bl_do_msg_update (TLS, M->id); break; }; case CODE_update_message_i_d: { int id = fetch_int (); // id int new = fetch_long (); // random_id - struct tgl_message *M = tgl_message_get (new); + struct tgl_message *M = tgl_message_get (TLS, new); if (M) { - bl_do_set_msg_id (M, id); + bl_do_set_msg_id (TLS, M, id); } } break; @@ -89,23 +89,23 @@ void tglu_work_update (struct connection *c, long long msg_id) { int i; for (i = 0; i < n; i++) { int id = fetch_int (); - struct tgl_message *M = tgl_message_get (id); + struct tgl_message *M = tgl_message_get (TLS, id); if (M) { - bl_do_set_unread (M, 0); + bl_do_set_unread (TLS, M, 0); } } - tglu_fetch_pts (); + tglu_fetch_pts (TLS); } break; case CODE_update_user_typing: { //vlogprintf (E_ERROR, "user typing\n"); tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *U = tgl_peer_get (TLS, id); enum tgl_typing_status status = tglf_fetch_typing (); - if (tgl_state.callback.type_notification && U) { - tgl_state.callback.type_notification ((void *)U, status); + if (TLS->callback.type_notification && U) { + TLS->callback.type_notification (TLS, (void *)U, status); } } break; @@ -114,13 +114,13 @@ void tglu_work_update (struct connection *c, long long msg_id) { //vlogprintf (E_ERROR, "chat typing\n"); tgl_peer_id_t chat_id = TGL_MK_CHAT (fetch_int ()); tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *C = tgl_peer_get (chat_id); - tgl_peer_t *U = tgl_peer_get (id); + tgl_peer_t *C = tgl_peer_get (TLS, chat_id); + tgl_peer_t *U = tgl_peer_get (TLS, id); enum tgl_typing_status status = tglf_fetch_typing (); if (U && C) { - if (tgl_state.callback.type_in_chat_notification) { - tgl_state.callback.type_in_chat_notification ((void *)U, (void *)C, status); + if (TLS->callback.type_in_chat_notification) { + TLS->callback.type_in_chat_notification (TLS, (void *)U, (void *)C, status); } } } @@ -128,33 +128,33 @@ void tglu_work_update (struct connection *c, long long msg_id) { case CODE_update_user_status: { tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (user_id); + tgl_peer_t *U = tgl_peer_get (TLS, user_id); if (U) { - tglf_fetch_user_status (&U->user.status); + tglf_fetch_user_status (TLS, &U->user.status); - if (tgl_state.callback.status_notification) { - tgl_state.callback.status_notification ((void *)U); + if (TLS->callback.status_notification) { + TLS->callback.status_notification (TLS, (void *)U); } } else { struct tgl_user_status t; - tglf_fetch_user_status (&t); + tglf_fetch_user_status (TLS, &t); } } break; case CODE_update_user_name: { tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *UC = tgl_peer_get (user_id); + tgl_peer_t *UC = tgl_peer_get (TLS, user_id); if (UC && (UC->flags & FLAG_CREATED)) { int l1 = prefetch_strlen (); char *f = fetch_str (l1); int l2 = prefetch_strlen (); char *l = fetch_str (l2); struct tgl_user *U = &UC->user; - bl_do_user_set_real_name (U, f, l1, l, l2); + bl_do_user_set_real_name (TLS, U, f, l1, l, l2); int l3 = prefetch_strlen (); f = fetch_str (l3); - bl_do_user_set_username (U, f, l3); + bl_do_user_set_username (TLS, U, f, l3); } else { fetch_skip_str (); fetch_skip_str (); @@ -165,8 +165,8 @@ void tglu_work_update (struct connection *c, long long msg_id) { case CODE_update_user_photo: { tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *UC = tgl_peer_get (user_id); - tglu_fetch_date (); + tgl_peer_t *UC = tgl_peer_get (TLS, user_id); + tglu_fetch_date (TLS); if (UC && (UC->flags & FLAG_CREATED)) { struct tgl_user *U = &UC->user; unsigned y = fetch_int (); @@ -182,10 +182,10 @@ void tglu_work_update (struct connection *c, long long msg_id) { } else { assert (y == CODE_user_profile_photo); photo_id = fetch_long (); - tglf_fetch_file_location (&small); - tglf_fetch_file_location (&big); + tglf_fetch_file_location (TLS, &small); + tglf_fetch_file_location (TLS, &big); } - bl_do_set_user_profile_photo (U, photo_id, &big, &small); + bl_do_set_user_profile_photo (TLS, U, photo_id, &big, &small); } else { struct tgl_file_location t; unsigned y = fetch_int (); @@ -193,8 +193,8 @@ void tglu_work_update (struct connection *c, long long msg_id) { } else { assert (y == CODE_user_profile_photo); fetch_long (); // photo_id - tglf_fetch_file_location (&t); - tglf_fetch_file_location (&t); + tglf_fetch_file_location (TLS, &t); + tglf_fetch_file_location (TLS, &t); } } fetch_bool (); @@ -205,7 +205,7 @@ void tglu_work_update (struct connection *c, long long msg_id) { assert (fetch_int () == CODE_vector); int n = fetch_int (); fetch_skip (n); - tglu_fetch_pts (); + tglu_fetch_pts (TLS); } break; case CODE_update_delete_messages: @@ -213,7 +213,7 @@ void tglu_work_update (struct connection *c, long long msg_id) { assert (fetch_int () == CODE_vector); int n = fetch_int (); fetch_skip (n); - tglu_fetch_pts (); + tglu_fetch_pts (TLS); } break; case CODE_update_chat_participants: @@ -222,10 +222,10 @@ void tglu_work_update (struct connection *c, long long msg_id) { assert (x == CODE_chat_participants || x == CODE_chat_participants_forbidden); tgl_peer_id_t chat_id = TGL_MK_CHAT (fetch_int ()); int n = 0; - tgl_peer_t *C = tgl_peer_get (chat_id); + tgl_peer_t *C = tgl_peer_get (TLS, chat_id); if (C && (C->flags & FLAG_CREATED)) { if (x == CODE_chat_participants) { - bl_do_chat_set_admin (&C->chat, fetch_int ()); + bl_do_chat_set_admin (TLS, &C->chat, fetch_int ()); assert (fetch_int () == CODE_vector); n = fetch_int (); struct tgl_chat_user *users = talloc (12 * n); @@ -237,7 +237,7 @@ void tglu_work_update (struct connection *c, long long msg_id) { users[i].date = fetch_int (); } int version = fetch_int (); - bl_do_chat_set_participants (&C->chat, version, n, users); + bl_do_chat_set_participants (TLS, &C->chat, version, n, users); } } else { if (x == CODE_chat_participants) { @@ -253,17 +253,17 @@ void tglu_work_update (struct connection *c, long long msg_id) { case CODE_update_contact_registered: { tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (user_id); + tgl_peer_t *U = tgl_peer_get (TLS, user_id); fetch_int (); // date - if (tgl_state.callback.user_registered && U) { - tgl_state.callback.user_registered ((void *)U); + if (TLS->callback.user_registered && U) { + TLS->callback.user_registered (TLS, (void *)U); } } break; case CODE_update_contact_link: { tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (user_id); + tgl_peer_t *U = tgl_peer_get (TLS, user_id); unsigned t = fetch_int (); assert (t == CODE_contacts_my_link_empty || t == CODE_contacts_my_link_requested || t == CODE_contacts_my_link_contact); if (t == CODE_contacts_my_link_requested) { @@ -280,10 +280,10 @@ void tglu_work_update (struct connection *c, long long msg_id) { case CODE_update_activation: { tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); - tgl_peer_t *U = tgl_peer_get (user_id); + tgl_peer_t *U = tgl_peer_get (TLS, user_id); - if (tgl_state.callback.user_activated && U) { - tgl_state.callback.user_activated ((void *)U); + if (TLS->callback.user_activated && U) { + TLS->callback.user_activated (TLS, (void *)U); } } break; @@ -293,8 +293,8 @@ void tglu_work_update (struct connection *c, long long msg_id) { fetch_int (); // date char *s = fetch_str_dup (); char *location = fetch_str_dup (); - if (tgl_state.callback.new_authorization) { - tgl_state.callback.new_authorization (s, location); + if (TLS->callback.new_authorization) { + TLS->callback.new_authorization (TLS, s, location); } tfree_str (s); tfree_str (location); @@ -302,25 +302,25 @@ void tglu_work_update (struct connection *c, long long msg_id) { break; case CODE_update_new_geo_chat_message: { - struct tgl_message *M = tglf_fetch_alloc_geo_message (); + struct tgl_message *M = tglf_fetch_alloc_geo_message (TLS); assert (M); - bl_do_msg_update (M->id); + bl_do_msg_update (TLS, M->id); } break; case CODE_update_new_encrypted_message: { - struct tgl_message *M = tglf_fetch_alloc_encrypted_message (); + struct tgl_message *M = tglf_fetch_alloc_encrypted_message (TLS); assert (M); - tglu_fetch_qts (); - bl_do_msg_update (M->id); + tglu_fetch_qts (TLS); + bl_do_msg_update (TLS, M->id); } break; case CODE_update_encryption: { - struct tgl_secret_chat *E = tglf_fetch_alloc_encrypted_chat (); + struct tgl_secret_chat *E = tglf_fetch_alloc_encrypted_chat (TLS); vlogprintf (E_DEBUG, "Secret chat state = %d\n", E->state); if (E->state == sc_ok) { - tgl_do_send_encr_chat_layer (E); + tgl_do_send_encr_chat_layer (TLS, E); } fetch_int (); // date } @@ -328,11 +328,11 @@ void tglu_work_update (struct connection *c, long long msg_id) { case CODE_update_encrypted_chat_typing: { tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); - tgl_peer_t *P = tgl_peer_get (id); + tgl_peer_t *P = tgl_peer_get (TLS, id); if (P) { - if (tgl_state.callback.type_in_secret_chat_notification) { - tgl_state.callback.type_in_secret_chat_notification ((void *)P); + if (TLS->callback.type_in_secret_chat_notification) { + TLS->callback.type_in_secret_chat_notification (TLS, (void *)P); } } } @@ -342,14 +342,14 @@ void tglu_work_update (struct connection *c, long long msg_id) { tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); // chat_id fetch_int (); // max_date fetch_int (); // date - tgl_peer_t *P = tgl_peer_get (id); + tgl_peer_t *P = tgl_peer_get (TLS, id); //int x = -1; if (P && P->last) { //x = 0; struct tgl_message *M = P->last; while (M && (!M->out || M->unread)) { if (M->out) { - bl_do_set_unread (M, 0); + bl_do_set_unread (TLS, M, 0); } M = M->next; } @@ -363,9 +363,9 @@ void tglu_work_update (struct connection *c, long long msg_id) { tgl_peer_id_t inviter_id = TGL_MK_USER (fetch_int ()); int version = fetch_int (); - tgl_peer_t *C = tgl_peer_get (chat_id); + tgl_peer_t *C = tgl_peer_get (TLS, chat_id); if (C && (C->flags & FLAG_CREATED)) { - bl_do_chat_add_user (&C->chat, version, tgl_get_peer_id (user_id), tgl_get_peer_id (inviter_id), time (0)); + bl_do_chat_add_user (TLS, &C->chat, version, tgl_get_peer_id (user_id), tgl_get_peer_id (inviter_id), time (0)); } } break; @@ -375,9 +375,9 @@ void tglu_work_update (struct connection *c, long long msg_id) { tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); int version = fetch_int (); - tgl_peer_t *C = tgl_peer_get (chat_id); + tgl_peer_t *C = tgl_peer_get (TLS, chat_id); if (C && (C->flags & FLAG_CREATED)) { - bl_do_chat_del_user (&C->chat, version, tgl_get_peer_id (user_id)); + bl_do_chat_del_user (TLS, &C->chat, version, tgl_get_peer_id (user_id)); } } break; @@ -388,7 +388,7 @@ void tglu_work_update (struct connection *c, long long msg_id) { assert (n >= 0); int i; for (i = 0; i < n; i++) { - fetch_dc_option (); + fetch_dc_option (TLS); } } break; @@ -396,9 +396,9 @@ void tglu_work_update (struct connection *c, long long msg_id) { { int id = fetch_int (); int blocked = fetch_bool (); - tgl_peer_t *P = tgl_peer_get (TGL_MK_USER (id)); + tgl_peer_t *P = tgl_peer_get (TLS, TGL_MK_USER (id)); if (P && (P->flags & FLAG_CREATED)) { - bl_do_user_set_blocked (&P->user, blocked); + bl_do_user_set_blocked (TLS, &P->user, blocked); } } break; @@ -417,6 +417,13 @@ void tglu_work_update (struct connection *c, long long msg_id) { skip_type_message_media (TYPE_TO_PARAM(message_media)); fetch_bool (); vlogprintf (E_ERROR, "Notification %.*s: %.*s\n", l1, type, l2, message); + if (TLS->callback.notification) { + char *t = tstrndup (type, l1); + char *m = tstrndup (message, l2); + TLS->callback.notification (TLS, t, m); + tfree_str (t); + tfree_str (m); + } } break; default: @@ -425,32 +432,32 @@ void tglu_work_update (struct connection *c, long long msg_id) { } } -void tglu_work_update_short (struct connection *c, long long msg_id) { +void tglu_work_update_short (struct tgl_state *TLS, struct connection *c, long long msg_id) { int *save = in_ptr; assert (!skip_type_any (TYPE_TO_PARAM (updates))); int *save_end = in_ptr; in_ptr = save; assert (fetch_int () == CODE_update_short); - tglu_work_update (c, msg_id); - tglu_fetch_date (); + tglu_work_update (TLS, c, msg_id); + tglu_fetch_date (TLS); assert (save_end == in_ptr); } -static int do_skip_seq (int seq) { - if (tgl_state.seq) { - if (seq <= tgl_state.seq) { +static int do_skip_seq (struct tgl_state *TLS, int seq) { + if (TLS->seq) { + if (seq <= TLS->seq) { vlogprintf (E_NOTICE, "Duplicate message with seq=%d\n", seq); return -1; } - if (seq > tgl_state.seq + 1) { - vlogprintf (E_NOTICE, "Hole in seq (seq = %d, cur_seq = %d)\n", seq, tgl_state.seq); - //vlogprintf (E_NOTICE, "lock_diff = %s\n", (tgl_state.locks & TGL_LOCK_DIFF) ? "true" : "false"); - tgl_do_get_difference (0, 0, 0); + if (seq > TLS->seq + 1) { + vlogprintf (E_NOTICE, "Hole in seq (seq = %d, cur_seq = %d)\n", seq, TLS->seq); + //vlogprintf (E_NOTICE, "lock_diff = %s\n", (TLS->locks & TGL_LOCK_DIFF) ? "true" : "false"); + tgl_do_get_difference (TLS, 0, 0, 0); return -1; } - if (tgl_state.locks & TGL_LOCK_DIFF) { + if (TLS->locks & TGL_LOCK_DIFF) { vlogprintf (E_DEBUG, "Update during get_difference. seq = %d\n", seq); return -1; } @@ -461,10 +468,10 @@ static int do_skip_seq (int seq) { } } -void tglu_work_updates (struct connection *c, long long msg_id) { +void tglu_work_updates (struct tgl_state *TLS, struct connection *c, long long msg_id) { int *save = in_ptr; assert (!skip_type_any (TYPE_TO_PARAM (updates))); - if (do_skip_seq (*(in_ptr - 1)) < 0) { + if (do_skip_seq (TLS, *(in_ptr - 1)) < 0) { return; } int *save_end = in_ptr; @@ -474,66 +481,66 @@ void tglu_work_updates (struct connection *c, long long msg_id) { int n = fetch_int (); int i; for (i = 0; i < n; i++) { - tglu_work_update (c, msg_id); + tglu_work_update (TLS, c, msg_id); } assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_user (); + tglf_fetch_alloc_user (TLS); } assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - tglf_fetch_alloc_chat (); + tglf_fetch_alloc_chat (TLS); } - bl_do_set_date (fetch_int ()); + bl_do_set_date (TLS, fetch_int ()); //bl_do_set_seq (fetch_int ()); int seq = fetch_int (); - assert (seq == tgl_state.seq + 1); - bl_do_set_seq (seq); + assert (seq == TLS->seq + 1); + bl_do_set_seq (TLS, seq); assert (save_end == in_ptr); } -void tglu_work_update_short_message (struct connection *c, long long msg_id) { +void tglu_work_update_short_message (struct tgl_state *TLS, struct connection *c, long long msg_id) { int *save = in_ptr; assert (!skip_type_any (TYPE_TO_PARAM (updates))); - if (do_skip_seq (*(in_ptr - 1)) < 0) { + if (do_skip_seq (TLS, *(in_ptr - 1)) < 0) { return; } int *save_end = in_ptr; in_ptr = save; assert (fetch_int () == (int)CODE_update_short_message); - struct tgl_message *M = tglf_fetch_alloc_message_short (); + struct tgl_message *M = tglf_fetch_alloc_message_short (TLS); assert (M); assert (save_end == in_ptr); - if (!(tgl_state.locks & TGL_LOCK_DIFF)) { - bl_do_msg_seq_update (M->id); + if (!(TLS->locks & TGL_LOCK_DIFF)) { + bl_do_msg_seq_update (TLS, M->id); } } -void tglu_work_update_short_chat_message (struct connection *c, long long msg_id) { +void tglu_work_update_short_chat_message (struct tgl_state *TLS, struct connection *c, long long msg_id) { int *save = in_ptr; assert (!skip_type_any (TYPE_TO_PARAM (updates))); - if (do_skip_seq (*(in_ptr - 1)) < 0) { + if (do_skip_seq (TLS, *(in_ptr - 1)) < 0) { return; } int *save_end = in_ptr; in_ptr = save; assert (fetch_int () == CODE_update_short_chat_message); - struct tgl_message *M = tglf_fetch_alloc_message_short_chat (); + struct tgl_message *M = tglf_fetch_alloc_message_short_chat (TLS); assert (M); assert (save_end == in_ptr); - if (!(tgl_state.locks & TGL_LOCK_DIFF)) { - bl_do_msg_seq_update (M->id); + if (!(TLS->locks & TGL_LOCK_DIFF)) { + bl_do_msg_seq_update (TLS, M->id); } } -void tglu_work_updates_to_long (struct connection *c, long long msg_id) { +void tglu_work_updates_to_long (struct tgl_state *TLS, struct connection *c, long long msg_id) { assert (fetch_int () == (int)CODE_updates_too_long); vlogprintf (E_NOTICE, "updates too long... Getting difference\n"); - tgl_do_get_difference (0, 0, 0); + tgl_do_get_difference (TLS, 0, 0, 0); } diff --git a/updates.h b/updates.h index 8b28be9..0716131 100644 --- a/updates.h +++ b/updates.h @@ -21,15 +21,15 @@ #ifndef __UPDATES_H__ #define __UPDATES_H__ struct connection; -void tglu_work_update (struct connection *c, long long msg_id); -void tglu_work_updates_to_long (struct connection *c, long long msg_id); -void tglu_work_update_short_chat_message (struct connection *c, long long msg_id); -void tglu_work_update_short_message (struct connection *c, long long msg_id); -void tglu_work_update_short (struct connection *c, long long msg_id); -void tglu_work_updates (struct connection *c, long long msg_id); +void tglu_work_update (struct tgl_state *TLS, struct connection *c, long long msg_id); +void tglu_work_updates_to_long (struct tgl_state *TLS, struct connection *c, long long msg_id); +void tglu_work_update_short_chat_message (struct tgl_state *TLS, struct connection *c, long long msg_id); +void tglu_work_update_short_message (struct tgl_state *TLS, struct connection *c, long long msg_id); +void tglu_work_update_short (struct tgl_state *TLS, struct connection *c, long long msg_id); +void tglu_work_updates (struct tgl_state *TLS, struct connection *c, long long msg_id); -void tglu_fetch_pts (void); -void tglu_fetch_qts (void); -void tglu_fetch_seq (void); -void tglu_fetch_date (void); +void tglu_fetch_pts (struct tgl_state *TLS); +void tglu_fetch_qts (struct tgl_state *TLS); +void tglu_fetch_seq (struct tgl_state *TLS); +void tglu_fetch_date (struct tgl_state *TLS); #endif