From bf86f75604d8ebe3e78f95c5971c7b7cc11a7232 Mon Sep 17 00:00:00 2001 From: Vysheng Date: Wed, 13 Aug 2014 19:55:16 +0400 Subject: [PATCH] fixes --- binlog.c | 677 ++++++++++++++++++++-------------------- binlog.h | 68 ++-- interface.c | 336 ++++++++++---------- interface.h | 18 +- loop.c | 72 ++--- lua-tg.c | 89 +++--- lua-tg.h | 8 +- main.c | 9 +- mtproto-client.c | 143 ++++----- queries.c | 388 +++++++++++------------ queries.h | 52 +--- structures.c | 790 +++++++++++++++++++++++------------------------ structures.h | 85 ++--- telegram.h | 3 +- tgl-fetch.h | 49 +-- tgl-layout.h | 146 ++++----- tgl.c | 1 - tgl.h | 135 +++++++- 18 files changed, 1533 insertions(+), 1536 deletions(-) diff --git a/binlog.c b/binlog.c index 66d6527..756e866 100644 --- a/binlog.c +++ b/binlog.c @@ -51,39 +51,24 @@ static int binlog_buffer[BINLOG_BUFFER_SIZE]; static int *rptr; static int *wptr; +static int binlog_fd; -extern int test_dc; - -extern int pts; -extern int qts; -extern int last_date; -extern int seq; #define MAX_LOG_EVENT_SIZE (1 << 17) char *get_binlog_file_name (void); -extern struct dc *DC_list[]; -extern struct dc *DC_working; -extern int dc_working_num; -extern int binlog_enabled; -extern int encr_root; -extern unsigned char *encr_prime; -extern int encr_param_version; -extern int messages_allocated; -int in_replay_log; - -void *alloc_log_event (int l UU) { +static void *alloc_log_event (int l UU) { return binlog_buffer; } static long long binlog_pos; -int fetch_comb_binlog_start (void *extra) { +static int fetch_comb_binlog_start (void *extra) { return 0; } -int fetch_comb_binlog_dc_option (void *extra) { +static int fetch_comb_binlog_dc_option (void *extra) { int id = fetch_int (); int l1 = prefetch_strlen (); assert (l1 >= 0); @@ -93,31 +78,31 @@ int fetch_comb_binlog_dc_option (void *extra) { char *ip = fetch_str (l2); int port = fetch_int (); - vlogprintf (E_NOTICE, "DC%d '%s' update: %s:%d\n", id, name, ip, port); + vlogprintf (E_NOTICE, "DC%d '%.*s' update: %.*s:%d\n", id, l1, name, l2, ip, port); alloc_dc (id, tstrndup (ip, l2), port); return 0; } -int fetch_comb_binlog_auth_key (void *extra) { +static int fetch_comb_binlog_auth_key (void *extra) { int num = fetch_int (); assert (num >= 0 && num <= MAX_DC_ID); - assert (DC_list[num]); - DC_list[num]->auth_key_id = fetch_long (); - fetch_ints (DC_list[num]->auth_key, 64); - DC_list[num]->flags |= 1; + 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; return 0; } -int fetch_comb_binlog_default_dc (void *extra) { +static int fetch_comb_binlog_default_dc (void *extra) { int num = fetch_int (); assert (num >= 0 && num <= MAX_DC_ID); - DC_working = DC_list[num]; - dc_working_num = num; + tgl_state.DC_working = tgl_state.DC_list[num]; + tgl_state.dc_working_num = num; return 0; } -int fetch_comb_binlog_our_id (void *extra) { +static int fetch_comb_binlog_our_id (void *extra) { tgl_state.our_id = fetch_int (); #ifdef USE_LUA lua_our_id (tgl_state.our_id); @@ -125,73 +110,73 @@ int fetch_comb_binlog_our_id (void *extra) { return 0; } -int fetch_comb_binlog_dc_signed (void *extra) { +static int fetch_comb_binlog_dc_signed (void *extra) { int num = fetch_int (); assert (num >= 0 && num <= MAX_DC_ID); - assert (DC_list[num]); - DC_list[num]->has_auth = 1; + assert (tgl_state.DC_list[num]); + tgl_state.DC_list[num]->has_auth = 1; return 0; } -int fetch_comb_binlog_dc_salt (void *extra) { +static int fetch_comb_binlog_dc_salt (void *extra) { int num = fetch_int (); assert (num >= 0 && num <= MAX_DC_ID); - assert (DC_list[num]); - DC_list[num]->server_salt = fetch_long (); + assert (tgl_state.DC_list[num]); + tgl_state.DC_list[num]->server_salt = fetch_long (); return 0; } -int fetch_comb_binlog_set_dh_params (void *extra) { - if (encr_prime) { tfree (encr_prime, 256); } - encr_root = fetch_int (); - encr_prime = talloc (256); - fetch_ints (encr_prime, 64); - encr_param_version = fetch_int (); +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 (); return 0; } -int fetch_comb_binlog_set_pts (void *extra) { +static int fetch_comb_binlog_set_pts (void *extra) { int new_pts = fetch_int (); - assert (new_pts >= pts); - pts = new_pts; + assert (new_pts >= tgl_state.pts); + tgl_state.pts = new_pts; return 0; } -int fetch_comb_binlog_set_qts (void *extra) { +static int fetch_comb_binlog_set_qts (void *extra) { int new_qts = fetch_int (); //assert (new_qts >= qts); - qts = new_qts; + tgl_state.qts = new_qts; return 0; } -int fetch_comb_binlog_set_date (void *extra) { +static int fetch_comb_binlog_set_date (void *extra) { int new_date = fetch_int (); - assert (new_date >= last_date); - last_date = new_date; + assert (new_date >= tgl_state.date); + tgl_state.date = new_date; return 0; } -int fetch_comb_binlog_set_seq (void *extra) { +static int fetch_comb_binlog_set_seq (void *extra) { int new_seq = fetch_int (); - assert (new_seq >= seq); - seq = new_seq; + assert (new_seq >= tgl_state.seq); + tgl_state.seq = new_seq; return 0; } -int fetch_comb_binlog_user_add (void *extra) { - peer_id_t id = MK_USER (fetch_int ()); - peer_t *_U = peer_get (id); +static int fetch_comb_binlog_user_add (void *extra) { + tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *_U = tgl_peer_get (id); if (!_U) { _U = talloc0 (sizeof (*_U)); _U->id = id; - insert_user (_U); + tglp_insert_user (_U); } else { assert (!(_U->flags & FLAG_CREATED)); } - struct user *U = (void *)_U; + struct tgl_user *U = (void *)_U; U->flags |= FLAG_CREATED; - if (get_peer_id (id) == tgl_state.our_id) { + if (tgl_get_peer_id (id) == tgl_state.our_id) { U->flags |= FLAG_USER_SELF; } U->first_name = fetch_str_dup (); @@ -199,7 +184,7 @@ int fetch_comb_binlog_user_add (void *extra) { assert (!U->print_name); U->print_name = create_print_name (U->id, U->first_name, U->last_name, 0, 0); - peer_insert_name ((void *)U); + tglp_peer_insert_name ((void *)U); U->access_hash = fetch_long (); U->phone = fetch_str_dup (); if (fetch_int ()) { @@ -212,9 +197,9 @@ int fetch_comb_binlog_user_add (void *extra) { return 0; } -int fetch_comb_binlog_user_delete (void *extra) { - peer_id_t id = MK_USER (fetch_int ()); - peer_t *U = peer_get (id); +static int fetch_comb_binlog_user_delete (void *extra) { + tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (id); assert (U); U->flags |= FLAG_DELETED; @@ -224,9 +209,9 @@ int fetch_comb_binlog_user_delete (void *extra) { return 0; } -int fetch_comb_binlog_user_set_access_hash (void *extra) { - peer_id_t id = MK_USER (fetch_int ()); - peer_t *U = peer_get (id); +static int fetch_comb_binlog_user_set_access_hash (void *extra) { + tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (id); assert (U); U->user.access_hash = fetch_long (); @@ -236,9 +221,9 @@ int fetch_comb_binlog_user_set_access_hash (void *extra) { return 0; } -int fetch_comb_binlog_user_set_phone (void *extra) { - peer_id_t id = MK_USER (fetch_int ()); - peer_t *U = peer_get (id); +static int fetch_comb_binlog_user_set_phone (void *extra) { + tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (id); assert (U); if (U->user.phone) { tfree_str (U->user.phone); @@ -251,9 +236,9 @@ int fetch_comb_binlog_user_set_phone (void *extra) { return 0; } -int fetch_comb_binlog_user_set_friend (void *extra) { - peer_id_t id = MK_USER (fetch_int ()); - peer_t *U = peer_get (id); +static int fetch_comb_binlog_user_set_friend (void *extra) { + tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (id); assert (U); if (U->user.phone) { tfree_str (U->user.phone); @@ -268,12 +253,12 @@ int fetch_comb_binlog_user_set_friend (void *extra) { return 0; } -int fetch_comb_binlog_user_set_full_photo (void *extra) { - peer_id_t id = MK_USER (fetch_int ()); - peer_t *U = peer_get (id); +static int fetch_comb_binlog_user_set_full_photo (void *extra) { + tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (id); assert (U); if (U->flags & FLAG_HAS_PHOTO) { - free_photo (&U->user.photo); + tgls_free_photo (&U->user.photo); } tglf_fetch_photo (&U->user.photo); @@ -283,9 +268,9 @@ int fetch_comb_binlog_user_set_full_photo (void *extra) { return 0; } -int fetch_comb_binlog_user_set_blocked (void *extra) { - peer_id_t id = MK_USER (fetch_int ()); - peer_t *U = peer_get (id); +static int fetch_comb_binlog_user_set_blocked (void *extra) { + tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (id); assert (U); U->user.blocked = fetch_int (); @@ -296,9 +281,9 @@ int fetch_comb_binlog_user_set_blocked (void *extra) { return 0; } -int fetch_comb_binlog_user_set_real_name (void *extra) { - peer_id_t id = MK_USER (fetch_int ()); - peer_t *U = peer_get (id); +static int fetch_comb_binlog_user_set_real_name (void *extra) { + tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (id); assert (U); if (U->user.real_first_name) { tfree_str (U->user.real_first_name); } @@ -312,9 +297,9 @@ int fetch_comb_binlog_user_set_real_name (void *extra) { return 0; } -int fetch_comb_binlog_user_set_name (void *extra) { - peer_id_t id = MK_USER (fetch_int ()); - peer_t *U = peer_get (id); +static int fetch_comb_binlog_user_set_name (void *extra) { + tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (id); assert (U); if (U->user.first_name) { tfree_str (U->user.first_name); } @@ -322,11 +307,11 @@ 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) { - peer_delete_name (U); + tglp_peer_delete_name (U); tfree_str (U->print_name); } U->print_name = create_print_name (U->id, U->user.first_name, U->user.last_name, 0, 0); - peer_insert_name ((void *)U); + tglp_peer_insert_name ((void *)U); #ifdef USE_LUA lua_user_update (&U->user); @@ -334,9 +319,9 @@ int fetch_comb_binlog_user_set_name (void *extra) { return 0; } -int fetch_comb_binlog_user_set_photo (void *extra) { - peer_id_t id = MK_USER (fetch_int ()); - peer_t *U = peer_get (id); +static int fetch_comb_binlog_user_set_photo (void *extra) { + tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (id); assert (U); @@ -358,11 +343,11 @@ int fetch_comb_binlog_user_set_photo (void *extra) { return 0; } -int fetch_comb_binlog_encr_chat_delete (void *extra) { - peer_id_t id = MK_ENCR_CHAT (fetch_int ()); - peer_t *_U = peer_get (id); +static int fetch_comb_binlog_encr_chat_delete (void *extra) { + tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); + tgl_peer_t *_U = tgl_peer_get (id); assert (_U); - struct secret_chat *U = &_U->encr_chat; + struct tgl_secret_chat *U = &_U->encr_chat; memset (U->key, 0, sizeof (U->key)); U->flags |= FLAG_DELETED; U->state = sc_deleted; @@ -381,23 +366,23 @@ int fetch_comb_binlog_encr_chat_delete (void *extra) { return 0; } -int fetch_comb_binlog_encr_chat_requested (void *extra) { - peer_id_t id = MK_ENCR_CHAT (fetch_int ()); - peer_t *_U = peer_get (id); +static int fetch_comb_binlog_encr_chat_requested (void *extra) { + tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); + tgl_peer_t *_U = tgl_peer_get (id); if (!_U) { _U = talloc0 (sizeof (*_U)); _U->id = id; - insert_encrypted_chat (_U); + tglp_insert_encrypted_chat (_U); } else { assert (!(_U->flags & FLAG_CREATED)); } - struct secret_chat *U = (void *)_U; + struct tgl_secret_chat *U = (void *)_U; U->access_hash = fetch_long (); U->date = fetch_int (); U->admin_id = fetch_int (); U->user_id = fetch_int (); - peer_t *Us = peer_get (MK_USER (U->user_id)); + tgl_peer_t *Us = tgl_peer_get (TGL_MK_USER (U->user_id)); assert (!U->print_name); if (Us) { U->print_name = create_print_name (id, "!", Us->user.first_name, Us->user.last_name, 0); @@ -406,7 +391,7 @@ int fetch_comb_binlog_encr_chat_requested (void *extra) { tsnprintf (buf, 99, "user#%d", U->user_id); U->print_name = create_print_name (id, "!", buf, 0, 0); } - peer_insert_name ((void *)U); + tglp_peer_insert_name ((void *)U); U->g_key = talloc (256); U->nonce = talloc (256); fetch_ints (U->g_key, 64); @@ -421,9 +406,9 @@ int fetch_comb_binlog_encr_chat_requested (void *extra) { return 0; } -int fetch_comb_binlog_encr_chat_set_access_hash (void *extra) { - peer_id_t id = MK_ENCR_CHAT (fetch_int ()); - peer_t *U = peer_get (id); +static int fetch_comb_binlog_encr_chat_set_access_hash (void *extra) { + tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (id); assert (U); U->encr_chat.access_hash = fetch_long (); @@ -433,9 +418,9 @@ int fetch_comb_binlog_encr_chat_set_access_hash (void *extra) { return 0; } -int fetch_comb_binlog_encr_chat_set_date (void *extra) { - peer_id_t id = MK_ENCR_CHAT (fetch_int ()); - peer_t *U = peer_get (id); +static int fetch_comb_binlog_encr_chat_set_date (void *extra) { + tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (id); assert (U); U->encr_chat.date = fetch_int (); @@ -445,9 +430,9 @@ int fetch_comb_binlog_encr_chat_set_date (void *extra) { return 0; } -int fetch_comb_binlog_encr_chat_set_state (void *extra) { - peer_id_t id = MK_ENCR_CHAT (fetch_int ()); - peer_t *U = peer_get (id); +static int fetch_comb_binlog_encr_chat_set_state (void *extra) { + tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (id); assert (U); U->encr_chat.state = fetch_int (); @@ -457,11 +442,11 @@ int fetch_comb_binlog_encr_chat_set_state (void *extra) { return 0; } -int fetch_comb_binlog_encr_chat_accepted (void *extra) { - peer_id_t id = MK_ENCR_CHAT (fetch_int ()); - peer_t *_U = peer_get (id); +static int fetch_comb_binlog_encr_chat_accepted (void *extra) { + tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); + tgl_peer_t *_U = tgl_peer_get (id); assert (_U); - struct secret_chat *U = &_U->encr_chat; + struct tgl_secret_chat *U = &_U->encr_chat; if (!U->g_key) { U->g_key = talloc (256); } @@ -474,7 +459,7 @@ int fetch_comb_binlog_encr_chat_accepted (void *extra) { U->key_fingerprint = fetch_long (); if (U->state == sc_waiting) { - do_create_keys_end (U); + tgl_do_create_keys_end (U); } U->state = sc_ok; @@ -484,11 +469,11 @@ int fetch_comb_binlog_encr_chat_accepted (void *extra) { return 0; } -int fetch_comb_binlog_encr_chat_set_key (void *extra) { - peer_id_t id = MK_ENCR_CHAT (fetch_int ()); - peer_t *_U = peer_get (id); +static int fetch_comb_binlog_encr_chat_set_key (void *extra) { + tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); + tgl_peer_t *_U = tgl_peer_get (id); assert (_U); - struct secret_chat *U = &_U->encr_chat; + struct tgl_secret_chat *U = &_U->encr_chat; fetch_ints (U->key, 64); U->key_fingerprint = fetch_long (); @@ -498,17 +483,17 @@ int fetch_comb_binlog_encr_chat_set_key (void *extra) { return 0; } -int fetch_comb_binlog_encr_chat_init (void *extra) { - peer_t *P = talloc0 (sizeof (*P)); - P->id = MK_ENCR_CHAT (fetch_int ()); - assert (!peer_get (P->id)); +static int fetch_comb_binlog_encr_chat_init (void *extra) { + tgl_peer_t *P = talloc0 (sizeof (*P)); + P->id = TGL_MK_ENCR_CHAT (fetch_int ()); + assert (!tgl_peer_get (P->id)); P->encr_chat.user_id = fetch_int (); P->encr_chat.admin_id = tgl_state.our_id; - insert_encrypted_chat (P); - peer_t *Us = peer_get (MK_USER (P->encr_chat.user_id)); + tglp_insert_encrypted_chat (P); + tgl_peer_t *Us = tgl_peer_get (TGL_MK_USER (P->encr_chat.user_id)); assert (Us); P->print_name = create_print_name (P->id, "!", Us->user.first_name, Us->user.last_name, 0); - peer_insert_name (P); + tglp_peer_insert_name (P); P->encr_chat.g_key = talloc (256); fetch_ints (P->encr_chat.key, 64); @@ -521,28 +506,28 @@ int fetch_comb_binlog_encr_chat_init (void *extra) { return 0; } -int fetch_comb_binlog_chat_create (void *extra) { - peer_id_t id = MK_CHAT (fetch_int ()); - peer_t *_C = peer_get (id); +static int fetch_comb_binlog_chat_create (void *extra) { + tgl_peer_id_t id = TGL_MK_CHAT (fetch_int ()); + tgl_peer_t *_C = tgl_peer_get (id); if (!_C) { _C = talloc0 (sizeof (*_C)); _C->id = id; - insert_chat (_C); + tglp_insert_chat (_C); } else { assert (!(_C->flags & FLAG_CREATED)); } - struct chat *C = &_C->chat; + struct tgl_chat *C = &_C->chat; C->flags = FLAG_CREATED | fetch_int (); C->title = fetch_str_dup (); assert (!C->print_title); C->print_title = create_print_name (id, C->title, 0, 0, 0); - peer_insert_name ((void *)C); + tglp_peer_insert_name ((void *)C); C->users_num = fetch_int (); C->date = fetch_int (); C->version = fetch_int (); - fetch_data (&C->photo_big, sizeof (struct file_location)); - fetch_data (&C->photo_small, sizeof (struct file_location)); + fetch_data (&C->photo_big, sizeof (struct tgl_file_location)); + fetch_data (&C->photo_small, sizeof (struct tgl_file_location)); #ifdef USE_LUA lua_chat_update (C); @@ -550,8 +535,8 @@ int fetch_comb_binlog_chat_create (void *extra) { return 0; } -int fetch_comb_binlog_chat_change_flags (void *extra) { - peer_t *C = peer_get (MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_change_flags (void *extra) { + tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); C->chat.flags |= fetch_int (); C->chat.flags &= ~fetch_int (); @@ -562,18 +547,18 @@ int fetch_comb_binlog_chat_change_flags (void *extra) { return 0; } -int fetch_comb_binlog_chat_set_title (void *extra) { - peer_t *C = peer_get (MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_title (void *extra) { + tgl_peer_t *C = tgl_peer_get (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) { - peer_delete_name ((void *)C); + tglp_peer_delete_name ((void *)C); tfree_str (C->print_name); } C->print_name = create_print_name (C->id, C->chat.title, 0, 0, 0); - peer_insert_name ((void *)C); + tglp_peer_insert_name ((void *)C); #ifdef USE_LUA lua_chat_update (&C->chat); @@ -581,11 +566,11 @@ int fetch_comb_binlog_chat_set_title (void *extra) { return 0; } -int fetch_comb_binlog_chat_set_photo (void *extra) { - peer_t *C = peer_get (MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_photo (void *extra) { + tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); - fetch_data (&C->photo_big, sizeof (struct file_location)); - fetch_data (&C->photo_small, sizeof (struct file_location)); + fetch_data (&C->photo_big, sizeof (struct tgl_file_location)); + fetch_data (&C->photo_small, sizeof (struct tgl_file_location)); #ifdef USE_LUA lua_chat_update (&C->chat); @@ -593,8 +578,8 @@ int fetch_comb_binlog_chat_set_photo (void *extra) { return 0; } -int fetch_comb_binlog_chat_set_date (void *extra) { - peer_t *C = peer_get (MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_date (void *extra) { + tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); C->chat.date = fetch_int (); @@ -604,8 +589,8 @@ int fetch_comb_binlog_chat_set_date (void *extra) { return 0; } -int fetch_comb_binlog_chat_set_version (void *extra) { - peer_t *C = peer_get (MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_version (void *extra) { + tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); C->chat.version = fetch_int (); C->chat.users_num = fetch_int (); @@ -616,8 +601,8 @@ int fetch_comb_binlog_chat_set_version (void *extra) { return 0; } -int fetch_comb_binlog_chat_set_admin (void *extra) { - peer_t *C = peer_get (MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_admin (void *extra) { + tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); C->chat.admin_id = fetch_int (); @@ -627,8 +612,8 @@ int fetch_comb_binlog_chat_set_admin (void *extra) { return 0; } -int fetch_comb_binlog_chat_set_participants (void *extra) { - peer_t *C = peer_get (MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_participants (void *extra) { + tgl_peer_t *C = tgl_peer_get (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); } @@ -642,13 +627,13 @@ int fetch_comb_binlog_chat_set_participants (void *extra) { return 0; } -int fetch_comb_binlog_chat_set_full_photo (void *extra) { - peer_t *C = peer_get (MK_CHAT (fetch_int ())); +static int fetch_comb_binlog_chat_set_full_photo (void *extra) { + tgl_peer_t *C = tgl_peer_get (TGL_MK_CHAT (fetch_int ())); assert (C && (C->flags & FLAG_CREATED)); assert (C && (C->flags & FLAG_CREATED)); if (C->flags & FLAG_HAS_PHOTO) { - free_photo (&C->chat.photo); + tgls_free_photo (&C->chat.photo); } tglf_fetch_photo (&C->chat.photo); @@ -658,11 +643,11 @@ int fetch_comb_binlog_chat_set_full_photo (void *extra) { return 0; } -int fetch_comb_binlog_chat_add_participant (void *extra) { - peer_id_t id = MK_CHAT (fetch_int ()); - peer_t *_C = peer_get (id); +static int fetch_comb_binlog_chat_add_participant (void *extra) { + tgl_peer_id_t id = TGL_MK_CHAT (fetch_int ()); + tgl_peer_t *_C = tgl_peer_get (id); assert (_C && (_C->flags & FLAG_CREATED)); - struct chat *C = &_C->chat; + struct tgl_chat *C = &_C->chat; int version = fetch_int (); int user = fetch_int (); @@ -688,11 +673,11 @@ int fetch_comb_binlog_chat_add_participant (void *extra) { return 0; } -int fetch_comb_binlog_chat_del_participant (void *extra) { - peer_id_t id = MK_CHAT (fetch_int ()); - peer_t *_C = peer_get (id); +static int fetch_comb_binlog_chat_del_participant (void *extra) { + tgl_peer_id_t id = TGL_MK_CHAT (fetch_int ()); + tgl_peer_t *_C = tgl_peer_get (id); assert (_C && (_C->flags & FLAG_CREATED)); - struct chat *C = &_C->chat; + struct tgl_chat *C = &_C->chat; int version = fetch_int (); int user = fetch_int (); @@ -701,7 +686,7 @@ int fetch_comb_binlog_chat_del_participant (void *extra) { int i; for (i = 0; i < C->user_list_size; i++) { if (C->user_list[i].user_id == user) { - struct chat_user t; + struct tgl_chat_user t; t = C->user_list[i]; C->user_list[i] = C->user_list[C->user_list_size - 1]; C->user_list[C->user_list_size - 1] = t; @@ -718,24 +703,24 @@ int fetch_comb_binlog_chat_del_participant (void *extra) { return 0; } -int fetch_comb_binlog_create_message_text (void *extra) { +static int fetch_comb_binlog_create_message_text (void *extra) { long long id = fetch_int (); - struct message *M = message_get (id); + struct tgl_message *M = tgl_message_get (id); if (!M) { - M = message_alloc (id); + M = tglm_message_alloc (id); } else { assert (!(M->flags & FLAG_CREATED)); } M->flags |= FLAG_CREATED; - M->from_id = MK_USER (fetch_int ()); + M->from_id = TGL_MK_USER (fetch_int ()); int t = fetch_int (); - if (t == PEER_ENCR_CHAT) { + if (t == TGL_PEER_ENCR_CHAT) { M->flags |= FLAG_ENCRYPTED; } - M->to_id = set_peer_id (t, fetch_int ()); + M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); int l = prefetch_strlen (); @@ -744,16 +729,16 @@ int fetch_comb_binlog_create_message_text (void *extra) { M->message[l] = 0; M->message_len = l; - if (t == PEER_ENCR_CHAT) { + if (t == TGL_PEER_ENCR_CHAT) { M->media.type = CODE_decrypted_message_media_empty; } else { M->media.type = CODE_message_media_empty; } M->unread = 1; - M->out = get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; - message_insert (M); + tglm_message_insert (M); #ifdef USE_LUA lua_new_msg (M); @@ -761,24 +746,24 @@ int fetch_comb_binlog_create_message_text (void *extra) { return 0; } -int fetch_comb_binlog_send_message_text (void *extra) { +static int fetch_comb_binlog_send_message_text (void *extra) { long long id = fetch_long (); - struct message *M = message_get (id); + struct tgl_message *M = tgl_message_get (id); if (!M) { - M = message_alloc (id); + M = tglm_message_alloc (id); } else { assert (!(M->flags & FLAG_CREATED)); } M->flags |= FLAG_CREATED; - M->from_id = MK_USER (fetch_int ()); + M->from_id = TGL_MK_USER (fetch_int ()); int t = fetch_int (); - if (t == PEER_ENCR_CHAT) { + if (t == TGL_PEER_ENCR_CHAT) { M->flags |= FLAG_ENCRYPTED; } - M->to_id = set_peer_id (t, fetch_int ()); + M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); int l = prefetch_strlen (); @@ -787,17 +772,17 @@ int fetch_comb_binlog_send_message_text (void *extra) { M->message[l] = 0; M->message_len = l; - if (t == PEER_ENCR_CHAT) { + if (t == TGL_PEER_ENCR_CHAT) { M->media.type = CODE_decrypted_message_media_empty; } else { M->media.type = CODE_message_media_empty; } M->unread = 1; - M->out = get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; - message_insert (M); - message_insert_unsent (M); + tglm_message_insert (M); + tglm_message_insert_unsent (M); M->flags |= FLAG_PENDING; #ifdef USE_LUA @@ -806,32 +791,32 @@ int fetch_comb_binlog_send_message_text (void *extra) { return 0; } -int fetch_comb_binlog_send_message_action_encr (void *extra) { +static int fetch_comb_binlog_send_message_action_encr (void *extra) { long long id = fetch_long (); - struct message *M = message_get (id); + struct tgl_message *M = tgl_message_get (id); if (!M) { - M = message_alloc (id); + M = tglm_message_alloc (id); } else { assert (!(M->flags & FLAG_CREATED)); } M->flags |= FLAG_CREATED | FLAG_ENCRYPTED; - M->from_id = MK_USER (fetch_int ()); + M->from_id = TGL_MK_USER (fetch_int ()); int t = fetch_int (); - M->to_id = set_peer_id (t, fetch_int ()); + M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); M->media.type = CODE_decrypted_message_media_empty; tglf_fetch_message_action_encrypted (&M->action); M->unread = 1; - M->out = get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; M->service = 1; - message_insert (M); - message_insert_unsent (M); + tglm_message_insert (M); + tglm_message_insert_unsent (M); M->flags |= FLAG_PENDING; #ifdef USE_LUA @@ -840,27 +825,27 @@ int fetch_comb_binlog_send_message_action_encr (void *extra) { return 0; } -int fetch_comb_binlog_create_message_text_fwd (void *extra) { +static int fetch_comb_binlog_create_message_text_fwd (void *extra) { long long id = fetch_int (); - struct message *M = message_get (id); + struct tgl_message *M = tgl_message_get (id); if (!M) { - M = message_alloc (id); + M = tglm_message_alloc (id); } else { assert (!(M->flags & FLAG_CREATED)); } M->flags |= FLAG_CREATED; - M->from_id = MK_USER (fetch_int ()); + M->from_id = TGL_MK_USER (fetch_int ()); int t = fetch_int (); - if (t == PEER_ENCR_CHAT) { + if (t == TGL_PEER_ENCR_CHAT) { M->flags |= FLAG_ENCRYPTED; } - M->to_id = set_peer_id (t, fetch_int ()); + M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); - M->fwd_from_id = MK_USER (fetch_int ()); + M->fwd_from_id = TGL_MK_USER (fetch_int ()); M->fwd_date = fetch_int (); int l = prefetch_strlen (); @@ -869,16 +854,16 @@ int fetch_comb_binlog_create_message_text_fwd (void *extra) { M->message[l] = 0; M->message_len = l; - if (t == PEER_ENCR_CHAT) { + if (t == TGL_PEER_ENCR_CHAT) { M->media.type = CODE_decrypted_message_media_empty; } else { M->media.type = CODE_message_media_empty; } M->unread = 1; - M->out = get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; - message_insert (M); + tglm_message_insert (M); #ifdef USE_LUA lua_new_msg (M); @@ -886,18 +871,18 @@ int fetch_comb_binlog_create_message_text_fwd (void *extra) { return 0; } -int fetch_comb_binlog_create_message_media (void *extra) { +static int fetch_comb_binlog_create_message_media (void *extra) { int id = fetch_int (); - struct message *M = message_get (id); + struct tgl_message *M = tgl_message_get (id); if (!M) { - M = message_alloc (id); + M = tglm_message_alloc (id); } else { assert (!(M->flags & FLAG_CREATED)); } M->flags |= FLAG_CREATED; - M->from_id = MK_USER (fetch_int ()); + M->from_id = TGL_MK_USER (fetch_int ()); int t = fetch_int (); - M->to_id = set_peer_id (t, fetch_int ()); + M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); int l = prefetch_strlen (); @@ -908,27 +893,27 @@ int fetch_comb_binlog_create_message_media (void *extra) { tglf_fetch_message_media (&M->media); M->unread = 1; - M->out = get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; - message_insert (M); + tglm_message_insert (M); #ifdef USE_LUA lua_new_msg (M); #endif return 0; } -int fetch_comb_binlog_create_message_media_encr (void *extra) { +static int fetch_comb_binlog_create_message_media_encr (void *extra) { long long id = fetch_long (); - struct message *M = message_get (id); + struct tgl_message *M = tgl_message_get (id); if (!M) { - M = message_alloc (id); + M = tglm_message_alloc (id); } else { assert (!(M->flags & FLAG_CREATED)); } M->flags |= FLAG_CREATED | FLAG_ENCRYPTED; - M->from_id = MK_USER (fetch_int ()); + M->from_id = TGL_MK_USER (fetch_int ()); int t = fetch_int (); - M->to_id = set_peer_id (t, fetch_int ()); + M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); int l = prefetch_strlen (); @@ -940,30 +925,30 @@ int fetch_comb_binlog_create_message_media_encr (void *extra) { tglf_fetch_message_media_encrypted (&M->media); tglf_fetch_encrypted_message_file (&M->media); M->unread = 1; - M->out = get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; - message_insert (M); + tglm_message_insert (M); #ifdef USE_LUA lua_new_msg (M); #endif return 0; } -int fetch_comb_binlog_create_message_media_fwd (void *extra) { +static int fetch_comb_binlog_create_message_media_fwd (void *extra) { int id = fetch_int (); - struct message *M = message_get (id); + struct tgl_message *M = tgl_message_get (id); if (!M) { - M = message_alloc (id); + M = tglm_message_alloc (id); } else { assert (!(M->flags & FLAG_CREATED)); } M->flags |= FLAG_CREATED; - M->from_id = MK_USER (fetch_int ()); + M->from_id = TGL_MK_USER (fetch_int ()); int t = fetch_int (); - M->to_id = set_peer_id (t, fetch_int ()); + M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); - M->fwd_from_id = MK_USER (fetch_int ()); + M->fwd_from_id = TGL_MK_USER (fetch_int ()); M->fwd_date = fetch_int (); int l = prefetch_strlen (); @@ -974,106 +959,106 @@ int fetch_comb_binlog_create_message_media_fwd (void *extra) { tglf_fetch_message_media (&M->media); M->unread = 1; - M->out = get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; - message_insert (M); + tglm_message_insert (M); #ifdef USE_LUA lua_new_msg (M); #endif return 0; } -int fetch_comb_binlog_create_message_service (void *extra) { +static int fetch_comb_binlog_create_message_service (void *extra) { int id = fetch_int (); - struct message *M = message_get (id); + struct tgl_message *M = tgl_message_get (id); if (!M) { - M = message_alloc (id); + M = tglm_message_alloc (id); } else { assert (!(M->flags & FLAG_CREATED)); } M->flags |= FLAG_CREATED; - M->from_id = MK_USER (fetch_int ()); + M->from_id = TGL_MK_USER (fetch_int ()); int t = fetch_int (); - M->to_id = set_peer_id (t, fetch_int ()); + M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); tglf_fetch_message_action (&M->action); M->unread = 1; - M->out = get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; M->service = 1; - message_insert (M); + tglm_message_insert (M); #ifdef USE_LUA lua_new_msg (M); #endif return 0; } -int fetch_comb_binlog_create_message_service_encr (void *extra) { +static int fetch_comb_binlog_create_message_service_encr (void *extra) { long long id = fetch_long (); - struct message *M = message_get (id); + struct tgl_message *M = tgl_message_get (id); if (!M) { - M = message_alloc (id); + M = tglm_message_alloc (id); } else { assert (!(M->flags & FLAG_CREATED)); } M->flags |= FLAG_CREATED | FLAG_ENCRYPTED; - M->from_id = MK_USER (fetch_int ()); + M->from_id = TGL_MK_USER (fetch_int ()); int t = fetch_int (); - assert (t == PEER_ENCR_CHAT); - M->to_id = set_peer_id (t, fetch_int ()); + assert (t == TGL_PEER_ENCR_CHAT); + M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); - struct secret_chat *E = (void *)peer_get (M->to_id); + struct tgl_secret_chat *E = (void *)tgl_peer_get (M->to_id); assert (E); tglf_fetch_message_action_encrypted (&M->action); M->unread = 1; - M->out = get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; M->service = 1; if (!M->out && M->action.type == CODE_decrypted_message_action_notify_layer) { E->layer = M->action.layer; } - message_insert (M); + tglm_message_insert (M); #ifdef USE_LUA lua_new_msg (M); #endif return 0; } -int fetch_comb_binlog_create_message_service_fwd (void *extra) { +static int fetch_comb_binlog_create_message_service_fwd (void *extra) { int id = fetch_int (); - struct message *M = message_get (id); + struct tgl_message *M = tgl_message_get (id); if (!M) { - M = message_alloc (id); + M = tglm_message_alloc (id); } else { assert (!(M->flags & FLAG_CREATED)); } M->flags |= FLAG_CREATED; - M->from_id = MK_USER (fetch_int ()); + M->from_id = TGL_MK_USER (fetch_int ()); int t = fetch_int (); - M->to_id = set_peer_id (t, fetch_int ()); + M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); - M->fwd_from_id = MK_USER (fetch_int ()); + M->fwd_from_id = TGL_MK_USER (fetch_int ()); M->fwd_date = fetch_int (); tglf_fetch_message_action (&M->action); M->unread = 1; - M->out = get_peer_id (M->from_id) == tgl_state.our_id; + M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; M->service = 1; - message_insert (M); + tglm_message_insert (M); #ifdef USE_LUA lua_new_msg (M); #endif return 0; } -int fetch_comb_binlog_message_set_unread (void *extra) { - struct message *M = message_get (fetch_int ()); +static int fetch_comb_binlog_message_set_unread (void *extra) { + struct tgl_message *M = tgl_message_get (fetch_int ()); assert (M); M->unread = 0; #ifdef USE_LUA @@ -1082,10 +1067,10 @@ int fetch_comb_binlog_message_set_unread (void *extra) { return 0; } -int fetch_comb_binlog_set_message_sent (void *extra) { - struct message *M = message_get (fetch_long ()); +static int fetch_comb_binlog_set_message_sent (void *extra) { + struct tgl_message *M = tgl_message_get (fetch_long ()); assert (M); - message_remove_unsent (M); + tglm_message_remove_unsent (M); M->flags &= ~FLAG_PENDING; #ifdef USE_LUA lua_update_msg (M); @@ -1093,22 +1078,22 @@ int fetch_comb_binlog_set_message_sent (void *extra) { return 0; } -int fetch_comb_binlog_set_msg_id (void *extra) { - struct message *M = message_get (fetch_long ()); +static int fetch_comb_binlog_set_msg_id (void *extra) { + struct tgl_message *M = tgl_message_get (fetch_long ()); assert (M); if (M->flags & FLAG_PENDING) { - message_remove_unsent (M); + tglm_message_remove_unsent (M); M->flags &= ~FLAG_PENDING; } - message_remove_tree (M); - message_del_peer (M); + tglm_message_remove_tree (M); + tglm_message_del_peer (M); M->id = fetch_int (); - if (message_get (M->id)) { - free_message (M); + if (tgl_message_get (M->id)) { + tgls_free_message (M); tfree (M, sizeof (*M)); } else { - message_insert_tree (M); - message_add_peer (M); + tglm_message_insert_tree (M); + tglm_message_add_peer (M); } #ifdef USE_LUA lua_update_msg (M); @@ -1116,17 +1101,17 @@ int fetch_comb_binlog_set_msg_id (void *extra) { return 0; } -int fetch_comb_binlog_delete_msg (void *extra) { - struct message *M = message_get (fetch_long ()); +static int fetch_comb_binlog_delete_msg (void *extra) { + struct tgl_message *M = tgl_message_get (fetch_long ()); assert (M); if (M->flags & FLAG_PENDING) { - message_remove_unsent (M); + tglm_message_remove_unsent (M); M->flags &= ~FLAG_PENDING; } - message_remove_tree (M); - message_del_peer (M); - message_del_use (M); - free_message (M); + tglm_message_remove_tree (M); + tglm_message_del_peer (M); + tglm_message_del_use (M); + tgls_free_message (M); tfree (M, sizeof (*M)); return 0; } @@ -1137,8 +1122,7 @@ int fetch_comb_binlog_delete_msg (void *extra) { break; \ -void replay_log_event (void) { - in_replay_log = 1; +static void replay_log_event (void) { assert (rptr < wptr); int op = *rptr; @@ -1223,19 +1207,18 @@ void replay_log_event (void) { assert (ok >= 0); assert (in_ptr == in_end); - in_replay_log = 0; binlog_pos += (in_ptr - rptr) * 4; rptr = in_ptr; } -void create_new_binlog (void) { +static void create_new_binlog (void) { static int s[1000]; packet_ptr = s; out_int (CODE_binlog_start); out_int (CODE_binlog_dc_option); out_int (1); out_string (""); - out_string (tgl_params.test_mode ? TG_SERVER_TEST : TG_SERVER); + out_string (tgl_state.test_mode ? TG_SERVER_TEST : TG_SERVER); out_int (443); out_int (CODE_binlog_default_dc); out_int (1); @@ -1250,7 +1233,7 @@ void create_new_binlog (void) { } -void replay_log (void) { +void tgl_replay_log (void) { if (access (get_binlog_file_name (), F_OK) < 0) { printf ("No binlog found. Creating new one\n"); create_new_binlog (); @@ -1289,8 +1272,7 @@ void replay_log (void) { close (fd); } -int binlog_fd; -void write_binlog (void) { +void tgl_reopen_binlog_for_writing (void) { binlog_fd = open (get_binlog_file_name (), O_WRONLY); if (binlog_fd < 0) { perror ("binlog open"); @@ -1304,12 +1286,11 @@ void write_binlog (void) { } } -void add_log_event (const int *data, int len) { +static void add_log_event (const int *data, int len) { if (verbosity) { logprintf ("Add log event: magic = 0x%08x, len = %d\n", data[0], len); } assert (!(len & 3)); - if (in_replay_log) { return; } rptr = (void *)data; wptr = rptr + (len / 4); int *in = in_ptr; @@ -1319,7 +1300,7 @@ void add_log_event (const int *data, int len) { logprintf ("Unread %lld ints. Len = %d\n", (long long)(wptr - rptr), len); assert (rptr == wptr); } - if (binlog_enabled) { + if (tgl_state.binlog_enabled) { assert (binlog_fd > 0); assert (write (binlog_fd, data, len) == len); } @@ -1363,26 +1344,26 @@ void bl_do_user_add (int id, const char *f, int fl, const char *l, int ll, long add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_user_delete (struct user *U) { +void bl_do_user_delete (struct tgl_user *U) { if (U->flags & FLAG_DELETED) { return; } int *ev = alloc_log_event (8); ev[0] = CODE_binlog_user_delete; - ev[1] = get_peer_id (U->id); + ev[1] = tgl_get_peer_id (U->id); add_log_event (ev, 8); } -void bl_do_set_user_profile_photo (struct user *U, long long photo_id, struct file_location *big, struct file_location *small) { +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) { 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] = get_peer_id (U->id); + ev[1] = tgl_get_peer_id (U->id); ev[2] = CODE_user_profile_photo_empty; add_log_event (ev, 12); } else { clear_packet (); out_int (CODE_binlog_user_set_photo); - out_int (get_peer_id (U->id)); + out_int (tgl_get_peer_id (U->id)); out_int (CODE_user_profile_photo); out_long (photo_id); if (small->dc >= 0) { @@ -1413,50 +1394,50 @@ void bl_do_set_user_profile_photo (struct user *U, long long photo_id, struct fi } } -void bl_do_user_set_name (struct user *U, const char *f, int fl, const char *l, int ll) { +void bl_do_user_set_name (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; } clear_packet (); out_int (CODE_binlog_user_set_name); - out_int (get_peer_id (U->id)); + 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)); } -void bl_do_user_set_access_hash (struct user *U, long long access_token) { +void bl_do_user_set_access_hash (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] = get_peer_id (U->id); + ev[1] = tgl_get_peer_id (U->id); *(long long *)(ev + 2) = access_token; add_log_event (ev, 16); } -void bl_do_user_set_phone (struct user *U, const char *p, int pl) { +void bl_do_user_set_phone (struct tgl_user *U, const char *p, int pl) { if (U->phone && (int)strlen (U->phone) == pl && !strncmp (U->phone, p, pl)) { return; } clear_packet (); out_int (CODE_binlog_user_set_phone); - out_int (get_peer_id (U->id)); + out_int (tgl_get_peer_id (U->id)); out_cstring (p, pl); add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_user_set_friend (struct user *U, int friend) { +void bl_do_user_set_friend (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] = get_peer_id (U->id); + ev[1] = tgl_get_peer_id (U->id); ev[2] = friend; add_log_event (ev, 12); } void bl_do_dc_option (int id, int l1, const char *name, int l2, const char *ip, int port) { - struct dc *DC = DC_list[id]; + struct dc *DC = tgl_state.DC_list[id]; if (DC) { return; } clear_packet (); @@ -1483,50 +1464,50 @@ void bl_do_set_working_dc (int num) { add_log_event (ev, 8); } -void bl_do_user_set_full_photo (struct user *U, const int *start, int len) { +void bl_do_user_set_full_photo (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] = get_peer_id (U->id); + ev[1] = tgl_get_peer_id (U->id); memcpy (ev + 2, start, len); add_log_event (ev, len + 8); } -void bl_do_user_set_blocked (struct user *U, int blocked) { +void bl_do_user_set_blocked (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] = get_peer_id (U->id); + ev[1] = tgl_get_peer_id (U->id); ev[2] = blocked; add_log_event (ev, 12); } -void bl_do_user_set_real_name (struct user *U, const char *f, int fl, const char *l, int ll) { +void bl_do_user_set_real_name (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; } clear_packet (); out_int (CODE_binlog_user_set_real_name); - out_int (get_peer_id (U->id)); + 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)); } -void bl_do_encr_chat_delete (struct secret_chat *U) { +void bl_do_encr_chat_delete (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] = get_peer_id (U->id); + ev[1] = tgl_get_peer_id (U->id); add_log_event (ev, 8); } -void bl_do_encr_chat_requested (struct 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_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; - ev[1] = get_peer_id (U->id); + ev[1] = tgl_get_peer_id (U->id); *(long long *)(ev + 2) = access_hash; ev[4] = date; ev[5] = admin_id; @@ -1536,48 +1517,48 @@ void bl_do_encr_chat_requested (struct secret_chat *U, long long access_hash, in add_log_event (ev, 540); } -void bl_do_encr_chat_set_access_hash (struct secret_chat *U, long long access_hash) { +void bl_do_encr_chat_set_access_hash (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] = get_peer_id (U->id); + ev[1] = tgl_get_peer_id (U->id); *(long long *)(ev + 2) = access_hash; add_log_event (ev, 16); } -void bl_do_encr_chat_set_date (struct secret_chat *U, int date) { +void bl_do_encr_chat_set_date (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] = get_peer_id (U->id); + ev[1] = tgl_get_peer_id (U->id); ev[2] = date; add_log_event (ev, 12); } -void bl_do_encr_chat_set_state (struct secret_chat *U, enum secret_chat_state state) { +void bl_do_encr_chat_set_state (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] = get_peer_id (U->id); + ev[1] = tgl_get_peer_id (U->id); ev[2] = state; add_log_event (ev, 12); } -void bl_do_encr_chat_accepted (struct secret_chat *U, const unsigned char g_key[], const unsigned char nonce[], long long key_fingerprint) { +void bl_do_encr_chat_accepted (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; - ev[1] = get_peer_id (U->id); + ev[1] = tgl_get_peer_id (U->id); memcpy (ev + 2, g_key, 256); memcpy (ev + 66, nonce, 256); *(long long *)(ev + 130) = key_fingerprint; add_log_event (ev, 528); } -void bl_do_encr_chat_set_key (struct secret_chat *E, unsigned char key[], long long key_fingerprint) { +void bl_do_encr_chat_set_key (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] = get_peer_id (E->id); + ev[1] = tgl_get_peer_id (E->id); memcpy (ev + 2, key, 256); *(long long *)(ev + 66) = key_fingerprint; add_log_event (ev, 272); @@ -1630,26 +1611,26 @@ void bl_do_set_seq (int seq) { add_log_event (ev, 8); } -void bl_do_create_chat (struct chat *C, int y, const char *s, int l, int users_num, int date, int version, struct file_location *big, struct file_location *small) { +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) { clear_packet (); out_int (CODE_binlog_chat_create); - out_int (get_peer_id (C->id)); + out_int (tgl_get_peer_id (C->id)); out_int (y); out_cstring (s, l); out_int (users_num); out_int (date); out_int (version); - out_data (big, sizeof (struct file_location)); - out_data (small, sizeof (struct file_location)); + 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)); } -void bl_do_chat_forbid (struct chat *C, int on) { +void bl_do_chat_forbid (struct tgl_chat *C, int on) { if (on) { if (C->flags & FLAG_FORBIDDEN) { return; } int *ev = alloc_log_event (16); ev[0] = CODE_binlog_chat_change_flags; - ev[1] = get_peer_id (C->id); + ev[1] = tgl_get_peer_id (C->id); ev[2] = FLAG_FORBIDDEN; ev[3] = 0; add_log_event (ev, 16); @@ -1657,48 +1638,48 @@ void bl_do_chat_forbid (struct chat *C, int on) { if (!(C->flags & FLAG_FORBIDDEN)) { return; } int *ev = alloc_log_event (16); ev[0] = CODE_binlog_chat_change_flags; - ev[1] = get_peer_id (C->id); + ev[1] = tgl_get_peer_id (C->id); ev[2] = 0; ev[3] = FLAG_FORBIDDEN; add_log_event (ev, 16); } } -void bl_do_chat_set_title (struct chat *C, const char *s, int l) { +void bl_do_chat_set_title (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 (get_peer_id (C->id)); + out_int (tgl_get_peer_id (C->id)); out_cstring (s, l); add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_chat_set_photo (struct chat *C, struct file_location *big, struct file_location *small) { - if (!memcmp (&C->photo_small, small, sizeof (struct file_location)) && - !memcmp (&C->photo_big, big, sizeof (struct file_location))) { return; } +void bl_do_chat_set_photo (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 (); out_int (CODE_binlog_chat_set_photo); - out_int (get_peer_id (C->id)); - out_data (big, sizeof (struct file_location)); - out_data (small, sizeof (struct file_location)); + 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)); } -void bl_do_chat_set_date (struct chat *C, int date) { +void bl_do_chat_set_date (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] = get_peer_id (C->id); + ev[1] = tgl_get_peer_id (C->id); ev[2] = date; add_log_event (ev, 12); } -void bl_do_chat_set_set_in_chat (struct chat *C, int on) { +void bl_do_chat_set_set_in_chat (struct tgl_chat *C, int on) { if (on) { if (C->flags & FLAG_CHAT_IN_CHAT) { return; } int *ev = alloc_log_event (16); ev[0] = CODE_binlog_chat_change_flags; - ev[1] = get_peer_id (C->id); + ev[1] = tgl_get_peer_id (C->id); ev[2] = FLAG_CHAT_IN_CHAT; ev[3] = 0; add_log_event (ev, 16); @@ -1706,57 +1687,57 @@ void bl_do_chat_set_set_in_chat (struct chat *C, int on) { if (!(C->flags & FLAG_CHAT_IN_CHAT)) { return; } int *ev = alloc_log_event (16); ev[0] = CODE_binlog_chat_change_flags; - ev[1] = get_peer_id (C->id); + ev[1] = tgl_get_peer_id (C->id); ev[2] = 0; ev[3] = FLAG_CHAT_IN_CHAT; add_log_event (ev, 16); } } -void bl_do_chat_set_version (struct chat *C, int version, int user_num) { +void bl_do_chat_set_version (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] = get_peer_id (C->id); + ev[1] = tgl_get_peer_id (C->id); ev[2] = version; ev[3] = user_num; add_log_event (ev, 16); } -void bl_do_chat_set_admin (struct chat *C, int admin) { +void bl_do_chat_set_admin (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] = get_peer_id (C->id); + ev[1] = tgl_get_peer_id (C->id); ev[2] = admin; add_log_event (ev, 12); } -void bl_do_chat_set_participants (struct chat *C, int version, int user_num, struct chat_user *users) { +void bl_do_chat_set_participants (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; - ev[1] = get_peer_id (C->id); + ev[1] = tgl_get_peer_id (C->id); ev[2] = version; ev[3] = user_num; memcpy (ev + 4, users, 12 * user_num); add_log_event (ev, 12 * user_num + 16); } -void bl_do_chat_set_full_photo (struct chat *U, const int *start, int len) { +void bl_do_chat_set_full_photo (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] = get_peer_id (U->id); + ev[1] = tgl_get_peer_id (U->id); memcpy (ev + 2, start, len); add_log_event (ev, len + 8); } -void bl_do_chat_add_user (struct chat *C, int version, int user, int inviter, int date) { +void bl_do_chat_add_user (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; - ev[1] = get_peer_id (C->id); + ev[1] = tgl_get_peer_id (C->id); ev[2] = version; ev[3] = user; ev[4] = inviter; @@ -1764,11 +1745,11 @@ void bl_do_chat_add_user (struct chat *C, int version, int user, int inviter, in add_log_event (ev, 24); } -void bl_do_chat_del_user (struct chat *C, int version, int user) { +void bl_do_chat_del_user (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] = get_peer_id (C->id); + ev[1] = tgl_get_peer_id (C->id); ev[2] = version; ev[3] = user; add_log_event (ev, 16); @@ -1904,7 +1885,7 @@ void bl_do_create_message_service_fwd (int msg_id, int from_id, int to_type, int add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_set_unread (struct message *M, int unread) { +void bl_do_set_unread (struct tgl_message *M, int unread) { if (unread || !M->unread) { return; } clear_packet (); out_int (CODE_binlog_message_set_unread); @@ -1912,7 +1893,7 @@ void bl_do_set_unread (struct message *M, int unread) { add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_set_message_sent (struct message *M) { +void bl_do_set_message_sent (struct tgl_message *M) { if (!(M->flags & FLAG_PENDING)) { return; } clear_packet (); out_int (CODE_binlog_set_message_sent); @@ -1920,7 +1901,7 @@ void bl_do_set_message_sent (struct message *M) { add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_set_msg_id (struct message *M, int id) { +void bl_do_set_msg_id (struct tgl_message *M, int id) { if (M->id == id) { return; } clear_packet (); out_int (CODE_binlog_set_msg_id); @@ -1929,7 +1910,7 @@ void bl_do_set_msg_id (struct message *M, int id) { add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_delete_msg (struct message *M) { +void bl_do_delete_msg (struct tgl_message *M) { clear_packet (); out_int (CODE_binlog_delete_msg); out_long (M->id); diff --git a/binlog.h b/binlog.h index 44b3873..af22f6e 100644 --- a/binlog.h +++ b/binlog.h @@ -21,33 +21,29 @@ #include "structures.h" -void *alloc_log_event (int l); -void replay_log (void); -void add_log_event (const int *data, int l); -void write_binlog (void); void bl_do_set_auth_key_id (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_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 user *U); -void bl_do_set_user_profile_photo (struct user *U, long long photo_id, struct file_location *big, struct file_location *small); -void bl_do_user_set_name (struct user *U, const char *f, int fl, const char *l, int ll); -void bl_do_user_set_access_hash (struct user *U, long long access_token); -void bl_do_user_set_phone (struct user *U, const char *p, int pl); -void bl_do_user_set_friend (struct user *U, int friend); -void bl_do_user_set_full_photo (struct user *U, const int *start, int len); -void bl_do_user_set_blocked (struct user *U, int blocked); -void bl_do_user_set_real_name (struct user *U, const char *f, int fl, const char *l, int ll); +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_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_encr_chat_delete (struct secret_chat *U); -void bl_do_encr_chat_requested (struct 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_set_access_hash (struct secret_chat *U, long long access_hash); -void bl_do_encr_chat_set_date (struct secret_chat *U, int date); -void bl_do_encr_chat_set_state (struct secret_chat *U, enum secret_chat_state state); -void bl_do_encr_chat_accepted (struct secret_chat *U, const unsigned char g_key[], const unsigned char nonce[], long long key_fingerprint); -void bl_do_encr_chat_set_key (struct secret_chat *E, unsigned char key[], long long key_fingerprint); +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_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_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_dc_signed (int id); @@ -59,18 +55,18 @@ void bl_do_set_qts (int qts); void bl_do_set_seq (int seq); void bl_do_set_date (int date); -void bl_do_create_chat (struct chat *C, int y, const char *s, int l, int users_num, int date, int version, struct file_location *big, struct file_location *small); -void bl_do_chat_forbid (struct chat *C, int on); -void bl_do_chat_set_title (struct chat *C, const char *s, int l); -void bl_do_chat_set_photo (struct chat *C, struct file_location *big, struct file_location *small); -void bl_do_chat_set_date (struct chat *C, int date); -void bl_do_chat_set_set_in_chat (struct chat *C, int on); -void bl_do_chat_set_version (struct chat *C, int version, int user_num); -void bl_do_chat_set_admin (struct chat *C, int admin); -void bl_do_chat_set_participants (struct chat *C, int version, int user_num, struct chat_user *users); -void bl_do_chat_set_full_photo (struct chat *U, const int *start, int len); -void bl_do_chat_add_user (struct chat *C, int version, int user, int inviter, int date); -void bl_do_chat_del_user (struct chat *C, int version, int user); +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_message_text (int msg_id, int from_id, int to_type, int to_id, int date, 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 l, const char *s); @@ -82,8 +78,8 @@ void bl_do_create_message_media_encr (long long msg_id, int from_id, int to_type 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 message *M, int unread); -void bl_do_set_message_sent (struct message *M); -void bl_do_set_msg_id (struct message *M, int id); -void bl_do_delete_msg (struct message *M); +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); #endif diff --git a/interface.c b/interface.c index a5f4106..7c7269e 100644 --- a/interface.c +++ b/interface.c @@ -38,20 +38,21 @@ #endif #include "include.h" -#include "queries.h" +//#include "queries.h" #include "interface.h" #include "telegram.h" -#include "structures.h" +#include "auto/constants.h" +#include "tools.h" +//#include "structures.h" -#include "mtproto-common.h" +//#include "mtproto-common.h" #include "tgl.h" #define ALLOW_MULT 1 char *default_prompt = "> "; -int unread_messages; int msg_num_mode; int alert_sound; @@ -62,17 +63,11 @@ int readline_active; int log_level; -long long cur_uploading_bytes; -long long cur_uploaded_bytes; -long long cur_downloading_bytes; -long long cur_downloaded_bytes; char *line_ptr; -extern peer_t *Peers[]; -extern int peer_num; int in_chat_mode; -peer_id_t chat_mode_id; +tgl_peer_id_t chat_mode_id; int is_same_word (const char *s, size_t l, const char *word) { @@ -93,6 +88,7 @@ char *end_string_token (int *l) { *l = line_ptr - s; return s; } + char *next_token (int *l) { while (*line_ptr == ' ') { line_ptr ++; } if (!*line_ptr) { @@ -103,14 +99,6 @@ char *next_token (int *l) { char *s = line_ptr; int in_str = 0; while (*line_ptr && (*line_ptr != ' ' || neg || in_str)) { -/* if (*line_ptr == '\\') { - neg = 1 - neg; - } else { - if (*line_ptr == '"' && !neg) { - in_str = !in_str; - } - neg = 0; - }*/ line_ptr++; } *l = line_ptr - s; @@ -118,7 +106,7 @@ char *next_token (int *l) { } #define NOT_FOUND (int)0x80000000 -peer_id_t PEER_NOT_FOUND = {.id = NOT_FOUND}; +tgl_peer_id_t TGL_PEER_NOT_FOUND = {.id = NOT_FOUND}; long long next_token_int (void) { int l; @@ -133,98 +121,98 @@ long long next_token_int (void) { } } -peer_id_t next_token_user (void) { +tgl_peer_id_t next_token_user (void) { int l; char *s = next_token (&l); - if (!s) { return PEER_NOT_FOUND; } + if (!s) { return TGL_PEER_NOT_FOUND; } if (l >= 6 && !memcmp (s, "user#", 5)) { s += 5; l -= 5; int r = atoi (s); - if (r >= 0) { return set_peer_id (PEER_USER, r); } - else { return PEER_NOT_FOUND; } + if (r >= 0) { return tgl_set_peer_id (TGL_PEER_USER, r); } + else { return TGL_PEER_NOT_FOUND; } } char c = s[l]; - peer_t *P = peer_lookup_name (s); + tgl_peer_t *P = tgl_peer_get_by_name (s); s[l] = c; - if (P && get_peer_type (P->id) == PEER_USER) { + if (P && tgl_get_peer_type (P->id) == TGL_PEER_USER) { return P->id; } else { - return PEER_NOT_FOUND; + return TGL_PEER_NOT_FOUND; } } -peer_id_t next_token_chat (void) { +tgl_peer_id_t next_token_chat (void) { int l; char *s = next_token (&l); - if (!s) { return PEER_NOT_FOUND; } + if (!s) { return TGL_PEER_NOT_FOUND; } if (l >= 6 && !memcmp (s, "chat#", 5)) { s += 5; l -= 5; int r = atoi (s); - if (r >= 0) { return set_peer_id (PEER_CHAT, r); } - else { return PEER_NOT_FOUND; } + if (r >= 0) { return tgl_set_peer_id (TGL_PEER_CHAT, r); } + else { return TGL_PEER_NOT_FOUND; } } char c = s[l]; - peer_t *P = peer_lookup_name (s); + tgl_peer_t *P = tgl_peer_get_by_name (s); s[l] = c; - if (P && get_peer_type (P->id) == PEER_CHAT) { + if (P && tgl_get_peer_type (P->id) == TGL_PEER_CHAT) { return P->id; } else { - return PEER_NOT_FOUND; + return TGL_PEER_NOT_FOUND; } } -peer_id_t next_token_encr_chat (void) { +tgl_peer_id_t next_token_encr_chat (void) { int l; char *s = next_token (&l); - if (!s) { return PEER_NOT_FOUND; } + if (!s) { return TGL_PEER_NOT_FOUND; } char c = s[l]; - peer_t *P = peer_lookup_name (s); + tgl_peer_t *P = tgl_peer_get_by_name (s); s[l] = c; - if (P && get_peer_type (P->id) == PEER_ENCR_CHAT) { + if (P && tgl_get_peer_type (P->id) == TGL_PEER_ENCR_CHAT) { return P->id; } else { - return PEER_NOT_FOUND; + return TGL_PEER_NOT_FOUND; } } -peer_id_t next_token_peer (void) { +tgl_peer_id_t next_token_peer (void) { int l; char *s = next_token (&l); - if (!s) { return PEER_NOT_FOUND; } + if (!s) { return TGL_PEER_NOT_FOUND; } if (l >= 6 && !memcmp (s, "user#", 5)) { s += 5; l -= 5; int r = atoi (s); - if (r >= 0) { return set_peer_id (PEER_USER, r); } - else { return PEER_NOT_FOUND; } + if (r >= 0) { return tgl_set_peer_id (TGL_PEER_USER, r); } + else { return TGL_PEER_NOT_FOUND; } } if (l >= 6 && !memcmp (s, "chat#", 5)) { s += 5; l -= 5; int r = atoi (s); - if (r >= 0) { return set_peer_id (PEER_CHAT, r); } - else { return PEER_NOT_FOUND; } + if (r >= 0) { return tgl_set_peer_id (TGL_PEER_CHAT, r); } + else { return TGL_PEER_NOT_FOUND; } } char c = s[l]; - peer_t *P = peer_lookup_name (s); + tgl_peer_t *P = tgl_peer_get_by_name (s); s[l] = c; if (P) { return P->id; } else { - return PEER_NOT_FOUND; + return TGL_PEER_NOT_FOUND; } } @@ -232,26 +220,26 @@ char *get_default_prompt (void) { static char buf[1000]; int l = 0; if (in_chat_mode) { - peer_t *U = peer_get (chat_mode_id); + tgl_peer_t *U = tgl_peer_get (chat_mode_id); assert (U && U->print_name); l += tsnprintf (buf + l, 999 - l, COLOR_RED "%.*s " COLOR_NORMAL, 100, U->print_name); } - if (unread_messages || cur_uploading_bytes || cur_downloading_bytes) { + if (tgl_state.unread_messages || tgl_state.cur_uploading_bytes || tgl_state.cur_downloading_bytes) { l += tsnprintf (buf + l, 999 - l, COLOR_RED "["); int ok = 0; - if (unread_messages) { - l += tsnprintf (buf + l, 999 - l, "%d unread", unread_messages); + if (tgl_state.unread_messages) { + l += tsnprintf (buf + l, 999 - l, "%d unread", tgl_state.unread_messages); ok = 1; } - if (cur_uploading_bytes) { + if (tgl_state.cur_uploading_bytes) { if (ok) { *(buf + l) = ' '; l ++; } ok = 1; - l += tsnprintf (buf + l, 999 - l, "%lld%%Up", 100 * cur_uploaded_bytes / cur_uploading_bytes); + l += tsnprintf (buf + l, 999 - l, "%lld%%Up", 100 * tgl_state.cur_uploaded_bytes / tgl_state.cur_uploading_bytes); } - if (cur_downloading_bytes) { + if (tgl_state.cur_downloading_bytes) { if (ok) { *(buf + l) = ' '; l ++; } ok = 1; - l += tsnprintf (buf + l, 999 - l, "%lld%%Down", 100 * cur_downloaded_bytes / cur_downloading_bytes); + l += tsnprintf (buf + l, 999 - l, "%lld%%Down", 100 * tgl_state.cur_downloaded_bytes / tgl_state.cur_downloading_bytes); } l += tsnprintf (buf + l, 999 - l, "]" COLOR_NORMAL); return buf; @@ -479,11 +467,11 @@ char *command_generator (const char *text, int state) { if (c) { rl_line_buffer[rl_point] = c; } return R; case 1: - index = complete_user_list (index, text, len, &R); + index = tgl_complete_user_list (index, text, len, &R); if (c) { rl_line_buffer[rl_point] = c; } return R; case 2: - index = complete_peer_list (index, text, len, &R); + index = tgl_complete_peer_list (index, text, len, &R); if (c) { rl_line_buffer[rl_point] = c; } return R; case 3: @@ -491,11 +479,11 @@ char *command_generator (const char *text, int state) { if (c) { rl_line_buffer[rl_point] = c; } return R; case 4: - index = complete_chat_list (index, text, len, &R); + index = tgl_complete_chat_list (index, text, len, &R); if (c) { rl_line_buffer[rl_point] = c; } return R; case 5: - index = complete_encr_chat_list (index, text, len, &R); + index = tgl_complete_encr_chat_list (index, text, len, &R); if (c) { rl_line_buffer[rl_point] = c; } return R; case 6: @@ -537,15 +525,15 @@ void interpreter_chat_mode (char *line) { int limit = 40; sscanf (line, "/history %99d", &limit); if (limit < 0 || limit > 1000) { limit = 40; } - do_get_history (chat_mode_id, limit); + tgl_do_get_history (chat_mode_id, limit); return; } if (!strncmp (line, "/read", 5)) { - do_mark_read (chat_mode_id); + tgl_do_mark_read (chat_mode_id); return; } if (strlen (line)>0) { - do_send_message (chat_mode_id, line, strlen (line)); + tgl_do_send_message (chat_mode_id, line, strlen (line)); } } @@ -592,36 +580,36 @@ void interpreter (char *line UU) { #define IS_WORD(s) is_same_word (command, l, (s)) #define RET in_readline = 0; return; - peer_id_t id; + tgl_peer_id_t id; #define GET_PEER \ id = next_token_peer (); \ - if (!cmp_peer_id (id, PEER_NOT_FOUND)) { \ + if (!tgl_cmp_peer_id (id, TGL_PEER_NOT_FOUND)) { \ printf ("Bad user/chat id\n"); \ RET; \ } #define GET_PEER_USER \ id = next_token_user (); \ - if (!cmp_peer_id (id, PEER_NOT_FOUND)) { \ + if (!tgl_cmp_peer_id (id, TGL_PEER_NOT_FOUND)) { \ printf ("Bad user id\n"); \ RET; \ } #define GET_PEER_CHAT \ id = next_token_chat (); \ - if (!cmp_peer_id (id, PEER_NOT_FOUND)) { \ + if (!tgl_cmp_peer_id (id, TGL_PEER_NOT_FOUND)) { \ printf ("Bad chat id\n"); \ RET; \ } #define GET_PEER_ENCR_CHAT \ id = next_token_encr_chat (); \ - if (!cmp_peer_id (id, PEER_NOT_FOUND)) { \ + if (!tgl_cmp_peer_id (id, TGL_PEER_NOT_FOUND)) { \ printf ("Bad encr_chat id\n"); \ RET; \ } if (IS_WORD ("contact_list")) { - do_update_contact_list (); + tgl_do_update_contact_list (); } else if (IS_WORD ("dialog_list")) { - do_get_dialog_list (); + tgl_do_get_dialog_list (); } else if (IS_WORD ("stats")) { static char stat_buf[1 << 15]; tgl_print_stat (stat_buf, (1 << 15) - 1); @@ -634,7 +622,7 @@ void interpreter (char *line UU) { printf ("Empty message\n"); RET; } - do_send_message (id, s, strlen (s)); + tgl_do_send_message (id, s, strlen (s)); } else if (IS_WORD ("rename_chat")) { GET_PEER_CHAT; int t; @@ -643,7 +631,7 @@ void interpreter (char *line UU) { printf ("Empty new name\n"); RET; } - do_rename_chat (id, s); + tgl_do_rename_chat (id, s); } else if (IS_WORD ("send_photo")) { GET_PEER; int t; @@ -652,7 +640,7 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - do_send_photo (CODE_input_media_uploaded_photo, id, tstrndup (s, t)); + tgl_do_send_photo (CODE_input_media_uploaded_photo, id, tstrndup (s, t)); } else if (IS_WORD("send_video")) { GET_PEER; int t; @@ -661,7 +649,7 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - do_send_photo (CODE_input_media_uploaded_video, id, tstrndup (s, t)); + tgl_do_send_photo (CODE_input_media_uploaded_video, id, tstrndup (s, t)); } else if (IS_WORD ("send_text")) { GET_PEER; int t; @@ -670,7 +658,7 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - do_send_text (id, tstrndup (s, t)); + tgl_do_send_text (id, tstrndup (s, t)); } else if (IS_WORD ("fwd")) { GET_PEER; int num = next_token_int (); @@ -678,18 +666,18 @@ void interpreter (char *line UU) { printf ("Bad msg id\n"); RET; } - do_forward_message (id, num); + tgl_do_forward_message (id, num); } else if (IS_WORD ("load_photo")) { long long num = next_token_int (); if (num == NOT_FOUND) { printf ("Bad msg id\n"); RET; } - struct message *M = message_get (num); + struct tgl_message *M = tgl_message_get (num); if (M && !M->service && M->media.type == CODE_message_media_photo) { - do_load_photo (&M->media.photo, 1); + tgl_do_load_photo (&M->media.photo, 1); } else if (M && !M->service && M->media.type == CODE_decrypted_message_media_photo) { - do_load_encr_video (&M->media.encr_video, 1); // this is not a bug. + tgl_do_load_encr_video (&M->media.encr_video, 1); // this is not a bug. } else { printf ("Bad msg id\n"); RET; @@ -700,11 +688,11 @@ void interpreter (char *line UU) { printf ("Bad msg id\n"); RET; } - struct message *M = message_get (num); + struct tgl_message *M = tgl_message_get (num); if (M && !M->service && M->media.type == CODE_message_media_photo) { - do_load_photo (&M->media.photo, 2); + tgl_do_load_photo (&M->media.photo, 2); } else if (M && !M->service && M->media.type == CODE_decrypted_message_media_photo) { - do_load_encr_video (&M->media.encr_video, 2); // this is not a bug. + tgl_do_load_encr_video (&M->media.encr_video, 2); // this is not a bug. } else { printf ("Bad msg id\n"); RET; @@ -715,9 +703,9 @@ void interpreter (char *line UU) { printf ("Bad msg id\n"); RET; } - struct message *M = message_get (num); + struct tgl_message *M = tgl_message_get (num); if (M && !M->service && M->media.type == CODE_message_media_video) { - do_load_video_thumb (&M->media.video, 1); + tgl_do_load_video_thumb (&M->media.video, 1); } else { printf ("Bad msg id\n"); RET; @@ -728,9 +716,9 @@ void interpreter (char *line UU) { printf ("Bad msg id\n"); RET; } - struct message *M = message_get (num); + struct tgl_message *M = tgl_message_get (num); if (M && !M->service && M->media.type == CODE_message_media_video) { - do_load_video_thumb (&M->media.video, 2); + tgl_do_load_video_thumb (&M->media.video, 2); } else { printf ("Bad msg id\n"); RET; @@ -741,11 +729,11 @@ void interpreter (char *line UU) { printf ("Bad msg id\n"); RET; } - struct message *M = message_get (num); + struct tgl_message *M = tgl_message_get (num); if (M && !M->service && M->media.type == CODE_message_media_video) { - do_load_video (&M->media.video, 1); + tgl_do_load_video (&M->media.video, 1); } else if (M && !M->service && M->media.type == CODE_decrypted_message_media_video) { - do_load_encr_video (&M->media.encr_video, 1); + tgl_do_load_encr_video (&M->media.encr_video, 1); } else { printf ("Bad msg id\n"); RET; @@ -756,35 +744,35 @@ void interpreter (char *line UU) { printf ("Bad msg id\n"); RET; } - struct message *M = message_get (num); + struct tgl_message *M = tgl_message_get (num); if (M && !M->service && M->media.type == CODE_message_media_video) { - do_load_video (&M->media.video, 2); + tgl_do_load_video (&M->media.video, 2); } else if (M && !M->service && M->media.type == CODE_decrypted_message_media_video) { - do_load_encr_video (&M->media.encr_video, 2); + tgl_do_load_encr_video (&M->media.encr_video, 2); } else { printf ("Bad msg id\n"); RET; } } else if (IS_WORD ("chat_info")) { GET_PEER_CHAT; - do_get_chat_info (id); + tgl_do_get_chat_info (id); } else if (IS_WORD ("user_info")) { GET_PEER_USER; - do_get_user_info (id); + tgl_do_get_user_info (id); } else if (IS_WORD ("history")) { GET_PEER; int limit = next_token_int (); - do_get_history (id, limit > 0 ? limit : 40); + tgl_do_get_history (id, limit > 0 ? limit : 40); } else if (IS_WORD ("chat_add_user")) { GET_PEER_CHAT; - peer_id_t chat_id = id; + tgl_peer_id_t chat_id = id; GET_PEER_USER; - do_add_user_to_chat (chat_id, id, 100); + tgl_do_add_user_to_chat (chat_id, id, 100); } else if (IS_WORD ("chat_del_user")) { GET_PEER_CHAT; - peer_id_t chat_id = id; + tgl_peer_id_t chat_id = id; GET_PEER_USER; - do_del_user_from_chat (chat_id, id); + tgl_do_del_user_from_chat (chat_id, id); } else if (IS_WORD ("add_contact")) { int phone_len, first_name_len, last_name_len; char *phone, *first_name, *last_name; @@ -803,10 +791,10 @@ void interpreter (char *line UU) { printf ("No last name found\n"); RET; } - do_add_contact (phone, phone_len, first_name, first_name_len, last_name, last_name_len, 0); + tgl_do_add_contact (phone, phone_len, first_name, first_name_len, last_name, last_name_len, 0); } else if (IS_WORD ("rename_contact")) { GET_PEER_USER; - peer_t *U = peer_get (id); + tgl_peer_t *U = tgl_peer_get (id); if (!U) { printf ("No such user\n"); RET; @@ -829,7 +817,7 @@ void interpreter (char *line UU) { printf ("No last name found\n"); RET; } - do_add_contact (phone, phone_len, first_name, first_name_len, last_name, last_name_len, 1); + tgl_do_add_contact (phone, phone_len, first_name, first_name_len, last_name, last_name_len, 1); } else if (IS_WORD ("help")) { //print_start (); push_color (COLOR_YELLOW); @@ -886,7 +874,7 @@ void interpreter (char *line UU) { printf ("Empty message\n"); RET; } - do_msg_search (id, from, to, limit, s); + tgl_do_msg_search (id, from, to, limit, s); } else if (IS_WORD ("global_search")) { int from = 0; int to = 0; @@ -897,16 +885,16 @@ void interpreter (char *line UU) { printf ("Empty message\n"); RET; } - do_msg_search (PEER_NOT_FOUND, from, to, limit, s); + tgl_do_msg_search (TGL_PEER_NOT_FOUND, from, to, limit, s); } else if (IS_WORD ("mark_read")) { GET_PEER; - do_mark_read (id); + tgl_do_mark_read (id); } else if (IS_WORD ("visualize_key")) { GET_PEER_ENCR_CHAT; - do_visualize_key (id); + tgl_do_visualize_key (id); } else if (IS_WORD ("create_secret_chat")) { GET_PEER; - do_create_secret_chat (id); + tgl_do_create_secret_chat (id); } else if (IS_WORD ("create_group_chat")) { GET_PEER; int t; @@ -915,13 +903,13 @@ void interpreter (char *line UU) { printf ("Empty chat topic\n"); RET; } - do_create_group_chat (id, s); + tgl_do_create_group_chat (id, s); } else if (IS_WORD ("suggested_contacts")) { - do_get_suggested (); + tgl_do_get_suggested (); } else if (IS_WORD ("status_online")) { - do_update_status (1); + tgl_do_update_status (1); } else if (IS_WORD ("status_offline")) { - do_update_status (0); + tgl_do_update_status (0); } else if (IS_WORD ("contacts_search")) { int t; char *s = next_token (&t); @@ -929,7 +917,7 @@ void interpreter (char *line UU) { printf ("Empty search query\n"); RET; } - do_contacts_search (100, s); + tgl_do_contacts_search (100, s); } else if (IS_WORD("send_audio")) { GET_PEER; int t; @@ -938,7 +926,7 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - do_send_photo (CODE_input_media_uploaded_audio, id, tstrndup (s, t)); + tgl_do_send_photo (CODE_input_media_uploaded_audio, id, tstrndup (s, t)); } else if (IS_WORD("send_document")) { GET_PEER; int t; @@ -947,18 +935,18 @@ void interpreter (char *line UU) { printf ("Empty file name\n"); RET; } - do_send_photo (CODE_input_media_uploaded_document, id, tstrndup (s, t)); + tgl_do_send_photo (CODE_input_media_uploaded_document, id, tstrndup (s, t)); } else if (IS_WORD ("load_audio")) { long long num = next_token_int (); if (num == NOT_FOUND) { printf ("Bad msg id\n"); RET; } - struct message *M = message_get (num); + struct tgl_message *M = tgl_message_get (num); if (M && !M->service && M->media.type == CODE_message_media_audio) { - do_load_audio (&M->media.video, 1); + tgl_do_load_audio (&M->media.video, 1); } else if (M && !M->service && M->media.type == CODE_decrypted_message_media_audio) { - do_load_encr_video (&M->media.encr_video, 1); + tgl_do_load_encr_video (&M->media.encr_video, 1); } else { printf ("Bad msg id\n"); RET; @@ -969,11 +957,11 @@ void interpreter (char *line UU) { printf ("Bad msg id\n"); RET; } - struct message *M = message_get (num); + struct tgl_message *M = tgl_message_get (num); if (M && !M->service && M->media.type == CODE_message_media_audio) { - do_load_audio (&M->media.video, 2); + tgl_do_load_audio (&M->media.video, 2); } else if (M && !M->service && M->media.type == CODE_decrypted_message_media_audio) { - do_load_encr_video (&M->media.encr_video, 2); + tgl_do_load_encr_video (&M->media.encr_video, 2); } else { printf ("Bad msg id\n"); RET; @@ -984,9 +972,9 @@ void interpreter (char *line UU) { printf ("Bad msg id\n"); RET; } - struct message *M = message_get (num); + struct tgl_message *M = tgl_message_get (num); if (M && !M->service && M->media.type == (int)CODE_message_media_document) { - do_load_document_thumb (&M->media.document, 1); + tgl_do_load_document_thumb (&M->media.document, 1); } else { printf ("Bad msg id\n"); RET; @@ -997,9 +985,9 @@ void interpreter (char *line UU) { printf ("Bad msg id\n"); RET; } - struct message *M = message_get (num); + struct tgl_message *M = tgl_message_get (num); if (M && !M->service && M->media.type == (int)CODE_message_media_document) { - do_load_document_thumb (&M->media.document, 2); + tgl_do_load_document_thumb (&M->media.document, 2); } else { printf ("Bad msg id\n"); RET; @@ -1010,11 +998,11 @@ void interpreter (char *line UU) { printf ("Bad msg id\n"); RET; } - struct message *M = message_get (num); + struct tgl_message *M = tgl_message_get (num); if (M && !M->service && M->media.type == CODE_message_media_document) { - do_load_document (&M->media.document, 1); + tgl_do_load_document (&M->media.document, 1); } else if (M && !M->service && M->media.type == CODE_decrypted_message_media_document) { - do_load_encr_video (&M->media.encr_video, 1); + tgl_do_load_encr_video (&M->media.encr_video, 1); } else { printf ("Bad msg id\n"); RET; @@ -1025,11 +1013,11 @@ void interpreter (char *line UU) { printf ("Bad msg id\n"); RET; } - struct message *M = message_get (num); + struct tgl_message *M = tgl_message_get (num); if (M && !M->service && M->media.type == CODE_message_media_document) { - do_load_document (&M->media.document, 2); + tgl_do_load_document (&M->media.document, 2); } else if (M && !M->service && M->media.type == CODE_decrypted_message_media_document) { - do_load_encr_video (&M->media.encr_video, 2); + tgl_do_load_encr_video (&M->media.encr_video, 2); } else { printf ("Bad msg id\n"); RET; @@ -1042,7 +1030,7 @@ void interpreter (char *line UU) { RET; } if (IS_WORD ("debug_verbosity")) { - verbosity = num; + tgl_set_verbosity (num); } else if (IS_WORD ("log_level")) { log_level = num; } else if (IS_WORD ("msg_num")) { @@ -1060,22 +1048,22 @@ void interpreter (char *line UU) { printf ("Bad msg id\n"); RET; } - do_delete_msg (num); + tgl_do_delete_msg (num); } else if (IS_WORD ("restore_msg")) { long long num = next_token_int (); if (num == NOT_FOUND) { printf ("Bad msg id\n"); RET; } - do_restore_msg (num); + tgl_do_restore_msg (num); } else if (IS_WORD ("delete_restore_msg")) { long long num = next_token_int (); if (num == NOT_FOUND) { printf ("Bad msg id\n"); RET; } - do_delete_msg (num); - do_restore_msg (num); + tgl_do_delete_msg (num); + tgl_do_restore_msg (num); } else if (IS_WORD ("quit")) { exit (0); } else if (IS_WORD ("safe_quit")) { @@ -1186,7 +1174,7 @@ void pop_color (void) { } } -void print_media (struct message_media *M) { +void print_media (struct tgl_message_media *M) { assert (M); switch (M->type) { case CODE_message_media_empty: @@ -1255,31 +1243,31 @@ void print_media (struct message_media *M) { int unknown_user_list_pos; int unknown_user_list[1000]; -void print_user_name (peer_id_t id, peer_t *U) { - assert (get_peer_type (id) == PEER_USER); +void print_user_name (tgl_peer_id_t id, tgl_peer_t *U) { + assert (tgl_get_peer_type (id) == TGL_PEER_USER); push_color (COLOR_RED); if (!U) { - printf ("user#%d", get_peer_id (id)); + printf ("user#%d", tgl_get_peer_id (id)); int i; int ok = 1; for (i = 0; i < unknown_user_list_pos; i++) { - if (unknown_user_list[i] == get_peer_id (id)) { + if (unknown_user_list[i] == tgl_get_peer_id (id)) { ok = 0; break; } } if (ok) { assert (unknown_user_list_pos < 1000); - unknown_user_list[unknown_user_list_pos ++] = get_peer_id (id); + unknown_user_list[unknown_user_list_pos ++] = tgl_get_peer_id (id); } } else { if (U->flags & (FLAG_USER_SELF | FLAG_USER_CONTACT)) { push_color (COLOR_REDB); } if ((U->flags & FLAG_DELETED)) { - printf ("deleted user#%d", get_peer_id (id)); + printf ("deleted user#%d", tgl_get_peer_id (id)); } else if (!(U->flags & FLAG_CREATED)) { - printf ("empty user#%d", get_peer_id (id)); + printf ("empty user#%d", tgl_get_peer_id (id)); } else if (!U->user.first_name || !strlen (U->user.first_name)) { printf ("%s", U->user.last_name); } else if (!U->user.last_name || !strlen (U->user.last_name)) { @@ -1294,33 +1282,33 @@ void print_user_name (peer_id_t id, peer_t *U) { pop_color (); } -void print_chat_name (peer_id_t id, peer_t *C) { - assert (get_peer_type (id) == PEER_CHAT); +void print_chat_name (tgl_peer_id_t id, tgl_peer_t *C) { + assert (tgl_get_peer_type (id) == TGL_PEER_CHAT); push_color (COLOR_MAGENTA); if (!C) { - printf ("chat#%d", get_peer_id (id)); + printf ("chat#%d", tgl_get_peer_id (id)); } else { printf ("%s", C->chat.title); } pop_color (); } -void print_encr_chat_name (peer_id_t id, peer_t *C) { - assert (get_peer_type (id) == PEER_ENCR_CHAT); +void print_encr_chat_name (tgl_peer_id_t id, tgl_peer_t *C) { + assert (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT); push_color (COLOR_MAGENTA); if (!C) { - printf ("encr_chat#%d", get_peer_id (id)); + printf ("encr_chat#%d", tgl_get_peer_id (id)); } else { printf ("%s", C->print_name); } pop_color (); } -void print_encr_chat_name_full (peer_id_t id, peer_t *C) { - assert (get_peer_type (id) == PEER_ENCR_CHAT); +void print_encr_chat_name_full (tgl_peer_id_t id, tgl_peer_t *C) { + assert (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT); push_color (COLOR_MAGENTA); if (!C) { - printf ("encr_chat#%d", get_peer_id (id)); + printf ("encr_chat#%d", tgl_get_peer_id (id)); } else { printf ("%s", C->print_name); } @@ -1342,7 +1330,7 @@ void print_date_full (long t) { printf ("[%04d/%02d/%02d %02d:%02d:%02d]", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); } -void print_service_message (struct message *M) { +void print_service_message (struct tgl_message *M) { assert (M); print_start (); push_color (COLOR_GREY); @@ -1354,14 +1342,14 @@ void print_service_message (struct message *M) { print_date (M->date); pop_color (); printf (" "); - if (get_peer_type (M->to_id) == PEER_CHAT) { - print_chat_name (M->to_id, peer_get (M->to_id)); + if (tgl_get_peer_type (M->to_id) == TGL_PEER_CHAT) { + print_chat_name (M->to_id, tgl_peer_get (M->to_id)); } else { - assert (get_peer_type (M->to_id) == PEER_ENCR_CHAT); - print_encr_chat_name (M->to_id, peer_get (M->to_id)); + assert (tgl_get_peer_type (M->to_id) == TGL_PEER_ENCR_CHAT); + print_encr_chat_name (M->to_id, tgl_peer_get (M->to_id)); } printf (" "); - print_user_name (M->from_id, peer_get (M->from_id)); + print_user_name (M->from_id, tgl_peer_get (M->from_id)); switch (M->action.type) { case CODE_message_action_empty: @@ -1388,12 +1376,12 @@ void print_service_message (struct message *M) { break; case CODE_message_action_chat_add_user: printf (" added user "); - print_user_name (set_peer_id (PEER_USER, M->action.user), peer_get (set_peer_id (PEER_USER, M->action.user))); + print_user_name (tgl_set_peer_id (TGL_PEER_USER, M->action.user), tgl_peer_get (tgl_set_peer_id (TGL_PEER_USER, M->action.user))); printf ("\n"); break; case CODE_message_action_chat_delete_user: printf (" deleted user "); - print_user_name (set_peer_id (PEER_USER, M->action.user), peer_get (set_peer_id (PEER_USER, M->action.user))); + print_user_name (tgl_set_peer_id (TGL_PEER_USER, M->action.user), tgl_peer_get (tgl_set_peer_id (TGL_PEER_USER, M->action.user))); printf ("\n"); break; case CODE_decrypted_message_action_set_message_t_t_l: @@ -1421,10 +1409,10 @@ void print_service_message (struct message *M) { print_end (); } -peer_id_t last_from_id; -peer_id_t last_to_id; +tgl_peer_id_t last_from_id; +tgl_peer_id_t last_to_id; -void print_message (struct message *M) { +void print_message (struct tgl_message *M) { assert (M); if (M->flags & (FLAG_MESSAGE_EMPTY | FLAG_DELETED)) { return; @@ -1434,7 +1422,7 @@ void print_message (struct message *M) { print_service_message (M); return; } - if (!get_peer_type (M->to_id)) { + if (!tgl_get_peer_type (M->to_id)) { logprintf ("Bad msg\n"); return; } @@ -1443,7 +1431,7 @@ void print_message (struct message *M) { last_to_id = M->to_id; print_start (); - if (get_peer_type (M->to_id) == PEER_USER) { + if (tgl_get_peer_type (M->to_id) == TGL_PEER_USER) { if (M->out) { push_color (COLOR_GREEN); if (msg_num_mode) { @@ -1452,7 +1440,7 @@ void print_message (struct message *M) { print_date (M->date); pop_color (); printf (" "); - print_user_name (M->to_id, peer_get (M->to_id)); + print_user_name (M->to_id, tgl_peer_get (M->to_id)); push_color (COLOR_GREEN); if (M->unread) { printf (" <<< "); @@ -1467,7 +1455,7 @@ void print_message (struct message *M) { print_date (M->date); pop_color (); printf (" "); - print_user_name (M->from_id, peer_get (M->from_id)); + print_user_name (M->from_id, tgl_peer_get (M->from_id)); push_color (COLOR_BLUE); if (M->unread) { printf (" >>> "); @@ -1478,8 +1466,8 @@ void print_message (struct message *M) { play_sound(); } } - } else if (get_peer_type (M->to_id) == PEER_ENCR_CHAT) { - peer_t *P = peer_get (M->to_id); + } else if (tgl_get_peer_type (M->to_id) == TGL_PEER_ENCR_CHAT) { + tgl_peer_t *P = tgl_peer_get (M->to_id); assert (P); if (M->out) { push_color (COLOR_GREEN); @@ -1515,7 +1503,7 @@ void print_message (struct message *M) { } } } else { - assert (get_peer_type (M->to_id) == PEER_CHAT); + assert (tgl_get_peer_type (M->to_id) == TGL_PEER_CHAT); push_color (COLOR_MAGENTA); if (msg_num_mode) { printf ("%lld ", M->id); @@ -1523,10 +1511,10 @@ void print_message (struct message *M) { print_date (M->date); pop_color (); printf (" "); - print_chat_name (M->to_id, peer_get (M->to_id)); + print_chat_name (M->to_id, tgl_peer_get (M->to_id)); printf (" "); - print_user_name (M->from_id, peer_get (M->from_id)); - if ((get_peer_type (M->from_id) == PEER_USER) && (get_peer_id (M->from_id) == tgl_state.our_id)) { + print_user_name (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)) { push_color (COLOR_GREEN); } else { push_color (COLOR_BLUE); @@ -1537,9 +1525,9 @@ void print_message (struct message *M) { printf (" »»» "); } } - if (get_peer_type (M->fwd_from_id) == PEER_USER) { + if (tgl_get_peer_type (M->fwd_from_id) == TGL_PEER_USER) { printf ("[fwd from "); - print_user_name (M->fwd_from_id, peer_get (M->fwd_from_id)); + print_user_name (M->fwd_from_id, tgl_peer_get (M->fwd_from_id)); printf ("] "); } if (M->message && strlen (M->message)) { diff --git a/interface.h b/interface.h index 19c2fc7..df4906b 100644 --- a/interface.h +++ b/interface.h @@ -19,6 +19,7 @@ #ifndef __INTERFACE_H__ #define __INTERFACE_H__ #include "structures.h" +#include "tgl-layout.h" #define COLOR_RED "\033[0;31m" #define COLOR_REDB "\033[1;31m" @@ -48,7 +49,7 @@ void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2))) #define vlogprintf(v,...) \ do { \ - if (tgl_params.verbosity >= (v)) {\ + if (tgl_state.verbosity >= (v)) {\ logprintf (__VA_ARGS__);\ }\ } while (0);\ @@ -56,14 +57,13 @@ void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2))) void hexdump (int *in_ptr, int *in_end); -struct message; -union peer; -void print_message (struct message *M); -void print_chat_name (peer_id_t id, union peer *C); -void print_user_name (peer_id_t id, union peer *U); -void print_encr_chat_name_full (peer_id_t id, peer_t *C); -void print_encr_chat_name (peer_id_t id, peer_t *C); -//void print_media (struct message_media *M); +struct tgl_message; +void print_message (struct tgl_message *M); +void print_chat_name (tgl_peer_id_t id, tgl_peer_t *C); +void print_user_name (tgl_peer_id_t id, tgl_peer_t *U); +void print_encr_chat_name_full (tgl_peer_id_t id, tgl_peer_t *C); +void print_encr_chat_name (tgl_peer_id_t id, tgl_peer_t *C); +//void print_media (struct tgl_message_media *M); void pop_color (void); void push_color (const char *color); void print_start (void); diff --git a/loop.c b/loop.c index d65ba80..ceec635 100644 --- a/loop.c +++ b/loop.c @@ -60,9 +60,7 @@ extern char *default_username; extern char *auth_token; -extern int test_dc; void set_default_username (const char *s); -int default_dc_num; extern int binlog_enabled; extern int unknown_user_list_pos; @@ -71,7 +69,6 @@ int register_mode; extern int safe_quit; extern int queries_num; -int unread_messages; void got_it (char *line, int len); void net_loop (int flags, int (*is_end)(void)) { while (!is_end ()) { @@ -93,7 +90,7 @@ void net_loop (int flags, int (*is_end)(void)) { } work_timers (); if ((flags & 3) && (fds[0].revents & POLLIN)) { - unread_messages = 0; + tgl_state.unread_messages = 0; if (flags & 1) { rl_callback_read_char (); } else { @@ -113,7 +110,7 @@ void net_loop (int flags, int (*is_end)(void)) { exit (0); } if (unknown_user_list_pos) { - do_get_user_list_info_silent (unknown_user_list_pos, unknown_user_list); + tgl_do_get_user_list_info_silent (unknown_user_list_pos, unknown_user_list); unknown_user_list_pos = 0; } } @@ -153,11 +150,6 @@ int main_loop (void) { return 0; } - -struct dc *DC_list[MAX_DC_ID + 1]; -struct dc *DC_working; -int dc_working_num; -int auth_state; char *get_auth_key_filename (void); char *get_state_filename (void); char *get_secret_chat_filename (void); @@ -229,7 +221,7 @@ void read_dc (int auth_file_fd, int id, unsigned ver) { } void empty_auth_file (void) { - alloc_dc (1, tstrdup (tgl_params.test_mode ? TG_SERVER_TEST : TG_SERVER), 443); + alloc_dc (1, tstrdup (tgl_state.test_mode ? TG_SERVER_TEST : TG_SERVER), 443); dc_working_num = 1; auth_state = 0; write_auth_file (); @@ -324,7 +316,7 @@ void write_state_file (void) { wseq = seq; wpts = pts; wqts = qts; wdate = last_date; } -extern peer_t *Peers[]; +extern tgl_peer_t *Peers[]; extern int peer_num; extern int encr_root; @@ -349,18 +341,18 @@ void read_secret_chat_file (void) { assert (read (fd, &cc, 4) == 4); int i; for (i = 0; i < cc; i++) { - peer_t *P = talloc0 (sizeof (*P)); - struct secret_chat *E = &P->encr_chat; + tgl_peer_t *P = talloc0 (sizeof (*P)); + struct tgl_secret_chat *E = &P->encr_chat; int t; assert (read (fd, &t, 4) == 4); - P->id = MK_ENCR_CHAT (t); + P->id = TGL_MK_ENCR_CHAT (t); assert (read (fd, &P->flags, 4) == 4); assert (read (fd, &t, 4) == 4); assert (t > 0); P->print_name = talloc (t + 1); assert (read (fd, P->print_name, t) == t); P->print_name[t] = 0; - peer_insert_name (P); + tglp_peer_insert_name (P); assert (read (fd, &E->state, 4) == 4); assert (read (fd, &E->user_id, 4) == 4); @@ -376,7 +368,7 @@ void read_secret_chat_file (void) { } assert (read (fd, E->key, 256) == 256); assert (read (fd, &E->key_fingerprint, 8) == 8); - insert_encrypted_chat (P); + tglp_insert_encrypted_chat (P); } if (version >= 1) { assert (read (fd, &encr_root, 4) == 4); @@ -389,16 +381,16 @@ void read_secret_chat_file (void) { close (fd); } -void count_encr_peer (peer_t *P, void *cc) { - if (get_peer_type (P->id) == PEER_ENCR_CHAT && P->encr_chat.state != sc_none && P->encr_chat.state != sc_deleted) { +void count_encr_peer (tgl_peer_t *P, void *cc) { + if (tgl_get_peer_type (P->id) == TGL_PEER_ENCR_CHAT && P->encr_chat.state != sc_none && P->encr_chat.state != sc_deleted) { (*(int *)cc) ++; } } -void write_encr_peer (peer_t *P, void *pfd) { +void write_encr_peer (tgl_peer_t *P, void *pfd) { int fd = *(int *)pfd; - if (get_peer_type (P->id) == PEER_ENCR_CHAT && P->encr_chat.state != sc_none && P->encr_chat.state != sc_deleted) { - int t = get_peer_id (P->id); + if (tgl_get_peer_type (P->id) == TGL_PEER_ENCR_CHAT && P->encr_chat.state != sc_none && P->encr_chat.state != sc_deleted) { + int t = tgl_get_peer_id (P->id); assert (write (fd, &t, 4) == 4); t = P->flags; assert (write (fd, &t, 4) == 4); @@ -435,8 +427,8 @@ void write_secret_chat_file (void) { assert (write (fd, x, 8) == 8); int cc = 0; - peer_iterator_ex (count_encr_peer, &cc); - peer_iterator_ex (write_encr_peer, &fd); + tgl_peer_iterator_ex (count_encr_peer, &cc); + tgl_peer_iterator_ex (write_encr_peer, &fd); assert (write (fd, &encr_root, 4) == 4); if (encr_root) { @@ -468,9 +460,9 @@ int loop (void) { if (binlog_enabled) { double t = get_double_time (); logprintf ("replay log start\n"); - replay_log (); + tgl_replay_log (); logprintf ("replay log end in %lf seconds\n", get_double_time () - t); - write_binlog (); + tgl_reopen_binlog_for_writing (); #ifdef USE_LUA lua_binlog_end (); #endif @@ -493,7 +485,7 @@ int loop (void) { if (verbosity) { logprintf ("Requesting info about DC...\n"); } - do_help_get_config (); + tgl_do_help_get_config (); net_loop (0, mcs); if (verbosity) { logprintf ("DC_info: %d new DC got\n", new_dc_num); @@ -519,11 +511,11 @@ int loop (void) { set_default_username (user); } } - int res = do_auth_check_phone (default_username); + int res = tgl_do_auth_check_phone (default_username); assert (res >= 0); logprintf ("%s\n", res > 0 ? "phone registered" : "phone not registered"); if (res > 0 && !register_mode) { - do_send_code (default_username); + tgl_do_send_code (default_username); char *code = 0; size_t size = 0; printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): "); @@ -534,11 +526,11 @@ int loop (void) { } if (!strcmp (code, "call")) { printf ("You typed \"call\", switching to phone system.\n"); - do_phone_call (default_username); + tgl_do_phone_call (default_username); printf ("Calling you! Code: "); continue; } - if (do_send_code_result (code) >= 0) { + if (tgl_do_send_code_result (code) >= 0) { break; } printf ("Invalid code. Try again: "); @@ -572,12 +564,12 @@ int loop (void) { exit (EXIT_FAILURE); } - int dc_num = do_get_nearest_dc (); + int dc_num = tgl_do_get_nearest_dc (); assert (dc_num >= 0 && dc_num <= MAX_DC_NUM && DC_list[dc_num]); dc_working_num = dc_num; DC_working = DC_list[dc_working_num]; - do_send_code (default_username); + tgl_do_send_code (default_username); printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): "); while (1) { if (net_getline (&code, &size) == -1) { @@ -586,11 +578,11 @@ int loop (void) { } if (!strcmp (code, "call")) { printf ("You typed \"call\", switching to phone system.\n"); - do_phone_call (default_username); + tgl_do_phone_call (default_username); printf ("Calling you! Code: "); continue; } - if (do_send_code_result_auth (code, first_name, last_name) >= 0) { + if (tgl_do_send_code_result_auth (code, first_name, last_name) >= 0) { break; } printf ("Invalid code. Try again: "); @@ -601,8 +593,8 @@ int loop (void) { } for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->has_auth) { - do_export_auth (i); - do_import_auth (i); + tgl_do_export_auth (i); + tgl_do_import_auth (i); bl_do_dc_signed (i); write_auth_file (); } @@ -616,15 +608,15 @@ int loop (void) { set_interface_callbacks (); - do_get_difference (); + tgl_do_get_difference (); net_loop (0, dgot); #ifdef USE_LUA lua_diff_end (); #endif - send_all_unsent (); + tglm_send_all_unsent (); - do_get_dialog_list (); + tgl_do_get_dialog_list (); if (wait_dialog_list) { dialog_list_got = 0; net_loop (0, dlgot); diff --git a/lua-tg.c b/lua-tg.c index e57fcbc..009ccd4 100644 --- a/lua-tg.c +++ b/lua-tg.c @@ -21,14 +21,15 @@ lua_State *luaState; #include "tools.h" #include "queries.h" #include "net.h" +#include "tgl.h" extern int verbosity; static int have_file; #define my_lua_checkstack(L,x) assert (lua_checkstack (L, x)) -void push_user (peer_t *P UU); -void push_peer (peer_id_t id, peer_t *P); +void push_user (tgl_peer_t *P UU); +void push_peer (tgl_peer_id_t id, tgl_peer_t *P); void lua_add_string_field (const char *name, const char *value) { assert (name && strlen (name)); @@ -47,15 +48,15 @@ void lua_add_num_field (const char *name, double value) { lua_settable (luaState, -3); } -void push_peer_type (int x) { +void push_tgl_peer_type (int x) { switch (x) { - case PEER_USER: + case TGL_PEER_USER: lua_pushstring (luaState, "user"); break; - case PEER_CHAT: + case TGL_PEER_CHAT: lua_pushstring (luaState, "chat"); break; - case PEER_ENCR_CHAT: + case TGL_PEER_ENCR_CHAT: lua_pushstring (luaState, "encr_chat"); break; default: @@ -63,7 +64,7 @@ void push_peer_type (int x) { } } -void push_user (peer_t *P UU) { +void push_user (tgl_peer_t *P UU) { my_lua_checkstack (luaState, 4); lua_add_string_field ("first_name", P->user.first_name); lua_add_string_field ("last_name", P->user.last_name); @@ -72,41 +73,41 @@ void push_user (peer_t *P UU) { lua_add_string_field ("phone", P->user.phone); } -void push_chat (peer_t *P) { +void push_chat (tgl_peer_t *P) { my_lua_checkstack (luaState, 4); assert (P->chat.title); lua_add_string_field ("title", P->chat.title); lua_add_num_field ("members_num", P->chat.users_num); } -void push_encr_chat (peer_t *P) { +void push_encr_chat (tgl_peer_t *P) { my_lua_checkstack (luaState, 4); lua_pushstring (luaState, "user"); - push_peer (MK_USER (P->encr_chat.user_id), peer_get (MK_USER (P->encr_chat.user_id))); + push_peer (TGL_MK_USER (P->encr_chat.user_id), tgl_peer_get (TGL_MK_USER (P->encr_chat.user_id))); lua_settable (luaState, -3); } -void push_peer (peer_id_t id, peer_t *P) { +void push_peer (tgl_peer_id_t id, tgl_peer_t *P) { lua_newtable (luaState); - lua_add_num_field ("id", get_peer_id (id)); + lua_add_num_field ("id", tgl_get_peer_id (id)); lua_pushstring (luaState, "type"); - push_peer_type (get_peer_type (id)); + push_tgl_peer_type (tgl_get_peer_type (id)); lua_settable (luaState, -3); if (!P || !(P->flags & FLAG_CREATED)) { lua_pushstring (luaState, "print_name"); static char s[100]; - switch (get_peer_type (id)) { - case PEER_USER: - sprintf (s, "user#%d", get_peer_id (id)); + switch (tgl_get_peer_type (id)) { + case TGL_PEER_USER: + sprintf (s, "user#%d", tgl_get_peer_id (id)); break; - case PEER_CHAT: - sprintf (s, "chat#%d", get_peer_id (id)); + case TGL_PEER_CHAT: + sprintf (s, "chat#%d", tgl_get_peer_id (id)); break; - case PEER_ENCR_CHAT: - sprintf (s, "encr_chat#%d", get_peer_id (id)); + case TGL_PEER_ENCR_CHAT: + sprintf (s, "encr_chat#%d", tgl_get_peer_id (id)); break; default: assert (0); @@ -120,14 +121,14 @@ void push_peer (peer_id_t id, peer_t *P) { lua_add_string_field ("print_name", P->print_name); lua_add_num_field ("flags", P->flags); - switch (get_peer_type (id)) { - case PEER_USER: + switch (tgl_get_peer_type (id)) { + case TGL_PEER_USER: push_user (P); break; - case PEER_CHAT: + case TGL_PEER_CHAT: push_chat (P); break; - case PEER_ENCR_CHAT: + case TGL_PEER_ENCR_CHAT: push_encr_chat (P); break; default: @@ -135,7 +136,7 @@ void push_peer (peer_id_t id, peer_t *P) { } } -void push_media (struct message_media *M) { +void push_media (struct tgl_message_media *M) { my_lua_checkstack (luaState, 4); switch (M->type) { @@ -176,7 +177,7 @@ void push_media (struct message_media *M) { } } -void push_message (struct message *M) { +void push_message (struct tgl_message *M) { assert (M); my_lua_checkstack (luaState, 10); lua_newtable (luaState); @@ -186,20 +187,20 @@ void push_message (struct message *M) { lua_add_string_field ("id", s); lua_add_num_field ("flags", M->flags); - if (get_peer_type (M->fwd_from_id)) { + if (tgl_get_peer_type (M->fwd_from_id)) { lua_pushstring (luaState, "fwd_from"); - push_peer (M->fwd_from_id, peer_get (M->fwd_from_id)); + push_peer (M->fwd_from_id, tgl_peer_get (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, peer_get (M->from_id)); + push_peer (M->from_id, tgl_peer_get (M->from_id)); lua_settable (luaState, -3); lua_pushstring (luaState, "to"); - push_peer (M->to_id, peer_get (M->to_id)); + push_peer (M->to_id, tgl_peer_get (M->to_id)); lua_settable (luaState, -3); lua_pushstring (luaState, "out"); @@ -275,7 +276,7 @@ void lua_our_id (int id) { } } -void lua_new_msg (struct message *M UU) { +void lua_new_msg (struct tgl_message *M UU) { if (!have_file) { return; } lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -290,7 +291,7 @@ void lua_new_msg (struct message *M UU) { } } -void lua_secret_chat_created (struct secret_chat *C) { +void lua_secret_chat_created (struct tgl_secret_chat *C) { if (!have_file) { return; } lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -305,7 +306,7 @@ void lua_secret_chat_created (struct secret_chat *C) { } } -void lua_user_update (struct user *U) { +void lua_user_update (struct tgl_user *U) { if (!have_file) { return; } lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -320,7 +321,7 @@ void lua_user_update (struct user *U) { } } -void lua_chat_update (struct chat *C) { +void lua_chat_update (struct tgl_chat *C) { if (!have_file) { return; } lua_settop (luaState, 0); //lua_checkstack (luaState, 20); @@ -335,15 +336,15 @@ void lua_chat_update (struct chat *C) { } } -extern peer_t *Peers[]; +extern tgl_peer_t *Peers[]; extern int peer_num; #define MAX_LUA_COMMANDS 1000 void *lua_ptr[MAX_LUA_COMMANDS]; static int pos; -static peer_t *get_peer (const char *s) { - return peer_lookup_name (s); +static tgl_peer_t *get_peer (const char *s) { + return tgl_peer_get_by_name (s); } void lua_do_all (void) { @@ -354,19 +355,19 @@ void lua_do_all (void) { int f = (long)lua_ptr[p ++]; switch (f) { case 0: - do_send_message (((peer_t *)lua_ptr[p])->id, lua_ptr[p + 1], strlen (lua_ptr[p + 1])); + tgl_do_send_message (((tgl_peer_t *)lua_ptr[p])->id, lua_ptr[p + 1], strlen (lua_ptr[p + 1])); tfree_str (lua_ptr[p + 1]); p += 2; break; case 1: - do_forward_message (((peer_t *)lua_ptr[p])->id, (long)lua_ptr[p + 1]); + tgl_do_forward_message (((tgl_peer_t *)lua_ptr[p])->id, (long)lua_ptr[p + 1]); p += 2; break; case 2: #ifdef DEBUG - texists (lua_ptr[p], sizeof (peer_t)); + texists (lua_ptr[p], sizeof (tgl_peer_t)); #endif - do_mark_read (((peer_t *)lua_ptr[p])->id); + tgl_do_mark_read (((tgl_peer_t *)lua_ptr[p])->id); p += 1; break; default: @@ -394,7 +395,7 @@ static int send_msg_from_lua (lua_State *L) { } const char *msg = lua_tostring (L, -1); - peer_t *P = get_peer (s); + tgl_peer_t *P = get_peer (s); if (!P) { lua_pushboolean (L, 0); return 1; @@ -426,7 +427,7 @@ static int fwd_msg_from_lua (lua_State *L) { lua_pushboolean (L, 0); return 1; } - peer_t *P = get_peer (s); + tgl_peer_t *P = get_peer (s); if (!P) { lua_pushboolean (L, 0); return 1; @@ -455,7 +456,7 @@ static int mark_read_from_lua (lua_State *L) { lua_pushboolean (L, 0); return 1; } - peer_t *P = get_peer (s); + tgl_peer_t *P = get_peer (s); if (!P) { lua_pushboolean (L, 0); return 1; diff --git a/lua-tg.h b/lua-tg.h index 0878860..fbc302b 100644 --- a/lua-tg.h +++ b/lua-tg.h @@ -5,11 +5,11 @@ #include "structures.h" void lua_init (const char *file); -void lua_new_msg (struct message *M); +void lua_new_msg (struct tgl_message *M); void lua_our_id (int id); -void lua_secret_chat_created (struct secret_chat *U); -void lua_user_update (struct user *U); -void lua_chat_update (struct chat *C); +void lua_secret_chat_created (struct tgl_secret_chat *U); +void lua_user_update (struct tgl_user *U); +void lua_chat_update (struct tgl_chat *C); void lua_binlog_end (void); void lua_diff_end (void); void lua_do_all (void); diff --git a/main.c b/main.c index d6c56bc..3ea11a2 100644 --- a/main.c +++ b/main.c @@ -298,9 +298,12 @@ void parse_config (void) { buf[l ++] = '.'; } - tgl_params.test_mode = 0; + int test_mode = 0; strcpy (buf + l, "test"); - config_lookup_bool (&conf, buf, &tgl_params.test_mode); + config_lookup_bool (&conf, buf, &test_mode); + if (test_mode) { + tgl_set_test_mode (); + } strcpy (buf + l, "log_level"); long long t = log_level; @@ -393,7 +396,7 @@ void args_parse (int argc, char **argv) { rsa_public_key_name = tstrdup (optarg); break; case 'v': - tgl_params.verbosity ++; + tgl_incr_verbosity (); break; case 'N': msg_num_mode ++; diff --git a/mtproto-client.c b/mtproto-client.c index f98eaac..de390c3 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -54,6 +54,7 @@ #include "structures.h" #include "binlog.h" #include "auto.h" +#include "tgl.h" #if defined(__FreeBSD__) #define __builtin_bswap32(x) bswap32(x) @@ -831,7 +832,7 @@ void fetch_seq (void) { int x = fetch_int (); if (x > seq + 1) { logprintf ("Hole in seq: seq = %d, x = %d\n", seq, x); - //do_get_difference (); + //tgl_do_get_difference (); //seq = x; } else if (x == seq + 1) { seq = x; @@ -844,20 +845,20 @@ void work_update_binlog (void) { switch (op) { case CODE_update_user_name: { - peer_id_t user_id = MK_USER (fetch_int ()); - peer_t *UC = peer_get (user_id); + tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *UC = tgl_peer_get (user_id); if (UC) { - struct user *U = &UC->user; + struct tgl_user *U = &UC->user; if (U->first_name) { tfree_str (U->first_name); } if (U->last_name) { tfree_str (U->last_name); } if (U->print_name) { - peer_delete_name (UC); + tglp_peer_delete_name (UC); tfree_str (U->print_name); } U->first_name = fetch_str_dup (); U->last_name = fetch_str_dup (); U->print_name = create_print_name (U->id, U->first_name, U->last_name, 0, 0); - peer_insert_name ((void *)U); + tglp_peer_insert_name ((void *)U); } else { fetch_skip_str (); fetch_skip_str (); @@ -866,11 +867,11 @@ void work_update_binlog (void) { break; case CODE_update_user_photo: { - peer_id_t user_id = MK_USER (fetch_int ()); - peer_t *UC = peer_get (user_id); + tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *UC = tgl_peer_get (user_id); fetch_date (); if (UC) { - struct user *U = &UC->user; + struct tgl_user *U = &UC->user; unsigned y = fetch_int (); if (y == CODE_user_profile_photo_empty) { @@ -884,7 +885,7 @@ void work_update_binlog (void) { tglf_fetch_file_location (&U->photo_big); } } else { - struct file_location t; + struct tgl_file_location t; unsigned y = fetch_int (); if (y == CODE_user_profile_photo_empty) { } else { @@ -907,7 +908,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { switch (op) { case CODE_update_new_message: { - struct message *M = tglf_fetch_alloc_message (); + struct tgl_message *M = tglf_fetch_alloc_message (); assert (M); fetch_pts (); unread_messages ++; @@ -919,7 +920,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { { int id = fetch_int (); // id int new = fetch_long (); // random_id - struct message *M = message_get (new); + struct tgl_message *M = tgl_message_get (new); if (M) { bl_do_set_msg_id (M, id); } @@ -932,7 +933,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { int i; for (i = 0; i < n; i++) { int id = fetch_int (); - struct message *M = message_get (id); + struct tgl_message *M = tgl_message_get (id); if (M) { bl_do_set_unread (M, 0); } @@ -950,8 +951,8 @@ void work_update (struct connection *c UU, long long msg_id UU) { break; case CODE_update_user_typing: { - peer_id_t id = MK_USER (fetch_int ()); - peer_t *U = peer_get (id); + tgl_peer_id_t id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (id); if (log_level >= 2) { print_start (); push_color (COLOR_YELLOW); @@ -966,10 +967,10 @@ void work_update (struct connection *c UU, long long msg_id UU) { break; case CODE_update_chat_user_typing: { - peer_id_t chat_id = MK_CHAT (fetch_int ()); - peer_id_t id = MK_USER (fetch_int ()); - peer_t *C = peer_get (chat_id); - peer_t *U = peer_get (id); + 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); if (log_level >= 2) { print_start (); push_color (COLOR_YELLOW); @@ -986,8 +987,8 @@ void work_update (struct connection *c UU, long long msg_id UU) { break; case CODE_update_user_status: { - peer_id_t user_id = MK_USER (fetch_int ()); - peer_t *U = peer_get (user_id); + tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (user_id); if (U) { tglf_fetch_user_status (&U->user.status); if (log_level >= 3) { @@ -1002,21 +1003,21 @@ void work_update (struct connection *c UU, long long msg_id UU) { print_end (); } } else { - struct user_status t; + struct tgl_user_status t; tglf_fetch_user_status (&t); } } break; case CODE_update_user_name: { - peer_id_t user_id = MK_USER (fetch_int ()); - peer_t *UC = peer_get (user_id); + tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *UC = tgl_peer_get (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 user *U = &UC->user; + struct tgl_user *U = &UC->user; bl_do_user_set_real_name (U, f, l1, l, l2); print_start (); push_color (COLOR_YELLOW); @@ -1036,15 +1037,15 @@ void work_update (struct connection *c UU, long long msg_id UU) { break; case CODE_update_user_photo: { - peer_id_t user_id = MK_USER (fetch_int ()); - peer_t *UC = peer_get (user_id); + tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *UC = tgl_peer_get (user_id); fetch_date (); if (UC && (UC->flags & FLAG_CREATED)) { - struct user *U = &UC->user; + struct tgl_user *U = &UC->user; unsigned y = fetch_int (); long long photo_id; - struct file_location big; - struct file_location small; + struct tgl_file_location big; + struct tgl_file_location small; memset (&big, 0, sizeof (big)); memset (&small, 0, sizeof (small)); if (y == CODE_user_profile_photo_empty) { @@ -1068,7 +1069,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { pop_color (); print_end (); } else { - struct file_location t; + struct tgl_file_location t; unsigned y = fetch_int (); if (y == CODE_user_profile_photo_empty) { } else { @@ -1113,15 +1114,15 @@ void work_update (struct connection *c UU, long long msg_id UU) { { unsigned x = fetch_int (); assert (x == CODE_chat_participants || x == CODE_chat_participants_forbidden); - peer_id_t chat_id = MK_CHAT (fetch_int ()); + tgl_peer_id_t chat_id = TGL_MK_CHAT (fetch_int ()); int n = 0; - peer_t *C = peer_get (chat_id); + tgl_peer_t *C = tgl_peer_get (chat_id); if (C && (C->flags & FLAG_CREATED)) { if (x == CODE_chat_participants) { bl_do_chat_set_admin (&C->chat, fetch_int ()); assert (fetch_int () == CODE_vector); n = fetch_int (); - struct chat_user *users = talloc (12 * n); + struct tgl_chat_user *users = talloc (12 * n); int i; for (i = 0; i < n; i++) { assert (fetch_int () == (int)CODE_chat_participant); @@ -1157,8 +1158,8 @@ void work_update (struct connection *c UU, long long msg_id UU) { break; case CODE_update_contact_registered: { - peer_id_t user_id = MK_USER (fetch_int ()); - peer_t *U = peer_get (user_id); + tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (user_id); fetch_int (); // date print_start (); push_color (COLOR_YELLOW); @@ -1172,8 +1173,8 @@ void work_update (struct connection *c UU, long long msg_id UU) { break; case CODE_update_contact_link: { - peer_id_t user_id = MK_USER (fetch_int ()); - peer_t *U = peer_get (user_id); + tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (user_id); print_start (); push_color (COLOR_YELLOW); print_date (time (0)); @@ -1196,8 +1197,8 @@ void work_update (struct connection *c UU, long long msg_id UU) { break; case CODE_update_activation: { - peer_id_t user_id = MK_USER (fetch_int ()); - peer_t *U = peer_get (user_id); + tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); + tgl_peer_t *U = tgl_peer_get (user_id); print_start (); push_color (COLOR_YELLOW); print_date (time (0)); @@ -1227,7 +1228,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { break; case CODE_update_new_geo_chat_message: { - struct message *M = tglf_fetch_alloc_geo_message (); + struct tgl_message *M = tglf_fetch_alloc_geo_message (); unread_messages ++; print_message (M); update_prompt (); @@ -1235,7 +1236,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { break; case CODE_update_new_encrypted_message: { - struct message *M = tglf_fetch_alloc_encrypted_message (); + struct tgl_message *M = tglf_fetch_alloc_encrypted_message (); unread_messages ++; print_message (M); update_prompt (); @@ -1244,7 +1245,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { break; case CODE_update_encryption: { - struct secret_chat *E = tglf_fetch_alloc_encrypted_chat (); + struct tgl_secret_chat *E = tglf_fetch_alloc_encrypted_chat (); if (verbosity >= 2) { logprintf ("Secret chat state = %d\n", E->state); } @@ -1278,25 +1279,25 @@ void work_update (struct connection *c UU, long long msg_id UU) { pop_color (); print_end (); if (E->state == sc_request && !disable_auto_accept) { - do_accept_encr_chat_request (E); + tgl_do_accept_encr_chat_request (E); } if (E->state == sc_ok) { - do_send_encr_chat_layer (E); + tgl_do_send_encr_chat_layer (E); } fetch_int (); // date } break; case CODE_update_encrypted_chat_typing: { - peer_id_t id = MK_ENCR_CHAT (fetch_int ()); - peer_t *P = peer_get (id); + tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); + tgl_peer_t *P = tgl_peer_get (id); print_start (); push_color (COLOR_YELLOW); print_date (time (0)); if (P) { printf (" User "); - peer_id_t user_id = MK_USER (P->encr_chat.user_id); - print_user_name (user_id, peer_get (user_id)); + tgl_peer_id_t user_id = TGL_MK_USER (P->encr_chat.user_id); + print_user_name (user_id, tgl_peer_get (user_id)); printf (" typing in secret chat "); print_encr_chat_name (id, P); printf ("\n"); @@ -1309,14 +1310,14 @@ void work_update (struct connection *c UU, long long msg_id UU) { break; case CODE_update_encrypted_messages_read: { - peer_id_t id = MK_ENCR_CHAT (fetch_int ()); // chat_id + tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); // chat_id fetch_int (); // max_date fetch_int (); // date - peer_t *P = peer_get (id); + tgl_peer_t *P = tgl_peer_get (id); int x = -1; if (P && P->last) { x = 0; - struct message *M = P->last; + struct tgl_message *M = P->last; while (M && (!M->out || M->unread)) { if (M->out) { M->unread = 0; @@ -1330,7 +1331,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { push_color (COLOR_YELLOW); print_date (time (0)); printf (" Encrypted chat "); - print_encr_chat_name_full (id, peer_get (id)); + print_encr_chat_name_full (id, tgl_peer_get (id)); printf (": %d messages marked read \n", x); pop_color (); print_end (); @@ -1339,25 +1340,25 @@ void work_update (struct connection *c UU, long long msg_id UU) { break; case CODE_update_chat_participant_add: { - peer_id_t chat_id = MK_CHAT (fetch_int ()); - peer_id_t user_id = MK_USER (fetch_int ()); - peer_id_t inviter_id = MK_USER (fetch_int ()); + tgl_peer_id_t chat_id = TGL_MK_CHAT (fetch_int ()); + tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); + tgl_peer_id_t inviter_id = TGL_MK_USER (fetch_int ()); int version = fetch_int (); - peer_t *C = peer_get (chat_id); + tgl_peer_t *C = tgl_peer_get (chat_id); if (C && (C->flags & FLAG_CREATED)) { - bl_do_chat_add_user (&C->chat, version, get_peer_id (user_id), get_peer_id (inviter_id), time (0)); + bl_do_chat_add_user (&C->chat, version, tgl_get_peer_id (user_id), tgl_get_peer_id (inviter_id), time (0)); } print_start (); push_color (COLOR_YELLOW); print_date (time (0)); printf (" Chat "); - print_chat_name (chat_id, peer_get (chat_id)); + print_chat_name (chat_id, tgl_peer_get (chat_id)); printf (": user "); - print_user_name (user_id, peer_get (user_id)); + print_user_name (user_id, tgl_peer_get (user_id)); printf (" added by user "); - print_user_name (inviter_id, peer_get (inviter_id)); + print_user_name (inviter_id, tgl_peer_get (inviter_id)); printf ("\n"); pop_color (); print_end (); @@ -1365,22 +1366,22 @@ void work_update (struct connection *c UU, long long msg_id UU) { break; case CODE_update_chat_participant_delete: { - peer_id_t chat_id = MK_CHAT (fetch_int ()); - peer_id_t user_id = MK_USER (fetch_int ()); + tgl_peer_id_t chat_id = TGL_MK_CHAT (fetch_int ()); + tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ()); int version = fetch_int (); - peer_t *C = peer_get (chat_id); + tgl_peer_t *C = tgl_peer_get (chat_id); if (C && (C->flags & FLAG_CREATED)) { - bl_do_chat_del_user (&C->chat, version, get_peer_id (user_id)); + bl_do_chat_del_user (&C->chat, version, tgl_get_peer_id (user_id)); } print_start (); push_color (COLOR_YELLOW); print_date (time (0)); printf (" Chat "); - print_chat_name (chat_id, peer_get (chat_id)); + print_chat_name (chat_id, tgl_peer_get (chat_id)); printf (": user "); - print_user_name (user_id, peer_get (user_id)); + print_user_name (user_id, tgl_peer_get (user_id)); printf (" deleted\n"); pop_color (); print_end (); @@ -1401,7 +1402,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { { int id = fetch_int (); int blocked = fetch_bool (); - peer_t *P = peer_get (MK_USER (id)); + tgl_peer_t *P = tgl_peer_get (TGL_MK_USER (id)); if (P && (P->flags & FLAG_CREATED)) { bl_do_user_set_blocked (&P->user, blocked); } @@ -1454,7 +1455,7 @@ void work_updates (struct connection *c, long long msg_id) { void work_update_short_message (struct connection *c UU, long long msg_id UU) { assert (fetch_int () == (int)CODE_update_short_message); - struct message *M = tglf_fetch_alloc_message_short (); + struct tgl_message *M = tglf_fetch_alloc_message_short (); unread_messages ++; print_message (M); update_prompt (); @@ -1465,7 +1466,7 @@ void work_update_short_message (struct connection *c UU, long long msg_id UU) { void work_update_short_chat_message (struct connection *c UU, long long msg_id UU) { assert (fetch_int () == CODE_update_short_chat_message); - struct message *M = tglf_fetch_alloc_message_short_chat (); + struct tgl_message *M = tglf_fetch_alloc_message_short_chat (); unread_messages ++; print_message (M); update_prompt (); @@ -1601,7 +1602,7 @@ void work_new_detailed_info (struct connection *c UU, long long msg_id UU) { void work_updates_to_long (struct connection *c UU, long long msg_id UU) { assert (fetch_int () == (int)CODE_updates_too_long); logprintf ("updates to long... Getting difference\n"); - do_get_difference (); + tgl_do_get_difference (); } void work_bad_msg_notification (struct connection *c UU, long long msg_id UU) { diff --git a/queries.c b/queries.c index 06fdd70..23d0ee7 100644 --- a/queries.c +++ b/queries.c @@ -78,7 +78,7 @@ extern int sync_from_start; int queries_num; -void out_peer_id (peer_id_t id); +void out_peer_id (tgl_peer_id_t id); #define QUERY_TIMEOUT 6.0 #define memcmp8(a,b) memcmp ((a), (b), 8) @@ -320,7 +320,7 @@ void out_random (int n) { } int allow_send_linux_version; -void do_insert_header (void) { +void tgl_do_insert_header (void) { out_int (CODE_invoke_with_layer15); out_int (CODE_init_connection); out_int (TG_APP_ID); @@ -392,9 +392,9 @@ struct query_methods help_get_config_methods = { .type = TYPE_TO_PARAM(config) }; -void do_help_get_config (void) { +void tgl_do_help_get_config (void) { clear_packet (); - do_insert_header (); + tgl_do_insert_header (); out_int (CODE_help_get_config); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &help_get_config_methods, 0); } @@ -449,12 +449,12 @@ int config_got (void) { char *suser; extern int dc_working_num; -void do_send_code (const char *user) { +void tgl_do_send_code (const char *user) { logprintf ("sending code\n"); suser = tstrdup (user); want_dc_num = 0; clear_packet (); - do_insert_header (); + tgl_do_insert_header (); out_int (CODE_auth_send_code); out_string (user); out_int (0); @@ -478,7 +478,7 @@ void do_send_code (const char *user) { logprintf ("send_code: dc_num = %d\n", dc_working_num); want_dc_num = 0; clear_packet (); - do_insert_header (); + tgl_do_insert_header (); out_int (CODE_auth_send_code); out_string (user); out_int (0); @@ -509,17 +509,17 @@ struct query_methods phone_call_methods = { .type = TYPE_TO_PARAM(bool) }; -void do_phone_call (const char *user) { +void tgl_do_phone_call (const char *user) { logprintf ("calling user\n"); suser = tstrdup (user); want_dc_num = 0; clear_packet (); - do_insert_header (); + tgl_do_insert_header (); out_int (CODE_auth_send_call); out_string (user); out_string (phone_code_hash); - logprintf ("do_phone_call: dc_num = %d\n", dc_working_num); + logprintf ("tgl_do_phone_call: dc_num = %d\n", dc_working_num); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &phone_call_methods, 0); } /* }}} */ @@ -574,7 +574,7 @@ struct query_methods check_phone_methods = { .type = TYPE_TO_PARAM(auth_checked_phone) }; -int do_auth_check_phone (const char *user) { +int tgl_do_auth_check_phone (const char *user) { suser = tstrdup (user); clear_packet (); out_int (CODE_auth_check_phone); @@ -619,7 +619,7 @@ struct query_methods nearest_dc_methods = { .type = TYPE_TO_PARAM(nearest_dc) }; -int do_get_nearest_dc (void) { +int tgl_do_get_nearest_dc (void) { clear_packet (); out_int (CODE_help_get_nearest_dc); nearest_dc_num = -1; @@ -635,14 +635,14 @@ int sign_in_is_ok (void) { return sign_in_ok; } -struct user User; +struct tgl_user User; int sign_in_on_answer (struct query *q UU) { assert (fetch_int () == (int)CODE_auth_authorization); int expires = fetch_int (); tglf_fetch_user (&User); if (!tgl_state.our_id) { - bl_do_set_our_id (get_peer_id (User.id)); + bl_do_set_our_id (tgl_get_peer_id (User.id)); } sign_in_ok = 1; if (verbosity) { @@ -668,7 +668,7 @@ struct query_methods sign_in_methods = { .type = TYPE_TO_PARAM(auth_authorization) }; -int do_send_code_result (const char *code) { +int tgl_do_send_code_result (const char *code) { clear_packet (); out_int (CODE_auth_sign_in); out_string (suser); @@ -680,7 +680,7 @@ int do_send_code_result (const char *code) { return sign_in_ok; } -int do_send_code_result_auth (const char *code, const char *first_name, const char *last_name) { +int tgl_do_send_code_result_auth (const char *code, const char *first_name, const char *last_name) { clear_packet (); out_int (CODE_auth_sign_up); out_string (suser); @@ -712,11 +712,11 @@ int get_contacts_on_answer (struct query *q UU) { assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n; i++) { - struct user *U = tglf_fetch_alloc_user (); + struct tgl_user *U = tglf_fetch_alloc_user (); print_start (); push_color (COLOR_YELLOW); - printf ("User #%d: ", get_peer_id (U->id)); - print_user_name (U->id, (peer_t *)U); + printf ("User #%d: ", tgl_get_peer_id (U->id)); + print_user_name (U->id, (tgl_peer_t *)U); push_color (COLOR_GREEN); printf (" ("); printf ("%s", U->print_name); @@ -749,7 +749,7 @@ struct query_methods get_contacts_methods = { }; -void do_update_contact_list (void) { +void tgl_do_update_contact_list (void) { clear_packet (); out_int (CODE_contacts_get_contacts); out_string (""); @@ -762,7 +762,7 @@ int *encr_extra; int *encr_ptr; int *encr_end; -char *encrypt_decrypted_message (struct secret_chat *E) { +char *encrypt_decrypted_message (struct tgl_secret_chat *E) { static int msg_key[4]; static unsigned char sha1a_buffer[20]; static unsigned char sha1b_buffer[20]; @@ -819,7 +819,7 @@ void encr_start (void) { } -void encr_finish (struct secret_chat *E) { +void encr_finish (struct tgl_secret_chat *E) { int l = packet_ptr - (encr_extra + 8); while (((packet_ptr - encr_extra) - 3) & 3) { int t; @@ -838,17 +838,17 @@ void encr_finish (struct secret_chat *E) { } /* }}} */ -void do_send_encr_chat_layer (struct secret_chat *E) { +void tgl_do_send_encr_chat_layer (struct tgl_secret_chat *E) { long long t; secure_random (&t, 8); int action[2]; action[0] = CODE_decrypted_message_action_notify_layer; action[1] = 15; - bl_do_send_message_action_encr (t, tgl_state.our_id, get_peer_type (E->id), get_peer_id (E->id), time (0), 2, action); + 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); - struct message *M = message_get (t); + struct tgl_message *M = tgl_message_get (t); assert (M); - do_send_msg (M); + tgl_do_send_msg (M); print_message (M); } @@ -856,7 +856,7 @@ void do_send_encr_chat_layer (struct secret_chat *E) { int msg_send_encr_on_answer (struct query *q UU) { assert (fetch_int () == CODE_messages_sent_encrypted_message); rprintf ("Sent\n"); - struct message *M = q->extra; + struct tgl_message *M = q->extra; //M->date = fetch_int (); fetch_int (); bl_do_set_message_sent (M); @@ -867,7 +867,7 @@ int msg_send_on_answer (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 - struct message *M = q->extra; + struct tgl_message *M = q->extra; bl_do_set_msg_id (M, id); fetch_date (); fetch_pts (); @@ -889,7 +889,7 @@ int msg_send_on_answer (struct query *q UU) { if (b == CODE_contacts_foreign_link_requested) { fetch_bool (); } - struct user *U = tglf_fetch_alloc_user (); + struct tgl_user *U = tglf_fetch_alloc_user (); U->flags &= ~(FLAG_USER_IN_CONTACT | FLAG_USER_OUT_CONTACT); if (a == CODE_contacts_my_link_contact) { @@ -918,7 +918,7 @@ int msg_send_on_answer (struct query *q UU) { int msg_send_on_error (struct query *q, int error_code, int error_len, char *error) { logprintf ( "error for query #%lld: #%d :%.*s\n", q->msg_id, error_code, error_len, error); - struct message *M = q->extra; + struct tgl_message *M = q->extra; bl_do_delete_msg (M); return 0; } @@ -936,14 +936,14 @@ struct query_methods msg_send_encr_methods = { int out_message_num; -void do_send_encr_msg_action (struct message *M) { - peer_t *P = peer_get (M->to_id); +void tgl_do_send_encr_msg_action (struct tgl_message *M) { + tgl_peer_t *P = tgl_peer_get (M->to_id); if (!P || P->encr_chat.state != sc_ok) { return; } clear_packet (); out_int (CODE_messages_send_encrypted_service); out_int (CODE_input_encrypted_chat); - out_int (get_peer_id (M->to_id)); + out_int (tgl_get_peer_id (M->to_id)); out_long (P->encr_chat.access_hash); out_long (M->id); encr_start (); @@ -966,18 +966,18 @@ void do_send_encr_msg_action (struct message *M) { send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M); } -void do_send_encr_msg (struct message *M) { +void tgl_do_send_encr_msg (struct tgl_message *M) { if (M->service) { - do_send_encr_msg_action (M); + tgl_do_send_encr_msg_action (M); return; } - peer_t *P = peer_get (M->to_id); + tgl_peer_t *P = tgl_peer_get (M->to_id); if (!P || P->encr_chat.state != sc_ok) { return; } clear_packet (); out_int (CODE_messages_send_encrypted); out_int (CODE_input_encrypted_chat); - out_int (get_peer_id (M->to_id)); + out_int (tgl_get_peer_id (M->to_id)); out_long (P->encr_chat.access_hash); out_long (M->id); encr_start (); @@ -993,9 +993,9 @@ void do_send_encr_msg (struct message *M) { send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M); } -void do_send_msg (struct message *M) { - if (get_peer_type (M->to_id) == PEER_ENCR_CHAT) { - do_send_encr_msg (M); +void tgl_do_send_msg (struct tgl_message *M) { + if (tgl_get_peer_type (M->to_id) == TGL_PEER_ENCR_CHAT) { + tgl_do_send_encr_msg (M); return; } clear_packet (); @@ -1006,9 +1006,9 @@ void do_send_msg (struct message *M) { send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_methods, M); } -void do_send_message (peer_id_t id, const char *msg, int len) { - if (get_peer_type (id) == PEER_ENCR_CHAT) { - peer_t *P = peer_get (id); +void tgl_do_send_message (tgl_peer_id_t id, const char *msg, int len) { + if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT) { + tgl_peer_t *P = tgl_peer_get (id); if (!P) { logprintf ("Can not send to unknown encrypted chat\n"); return; @@ -1021,16 +1021,16 @@ void do_send_message (peer_id_t id, const char *msg, int len) { long long t; secure_random (&t, 8); logprintf ("t = %lld, len = %d\n", t, len); - bl_do_send_message_text (t, tgl_state.our_id, get_peer_type (id), get_peer_id (id), time (0), len, msg); - struct message *M = message_get (t); + 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); assert (M); - do_send_msg (M); + tgl_do_send_msg (M); print_message (M); } /* }}} */ /* {{{ Send text file */ -void do_send_text (peer_id_t id, char *file_name) { +void tgl_do_send_text (tgl_peer_id_t id, char *file_name) { int fd = open (file_name, O_RDONLY); if (fd < 0) { rprintf ("No such file '%s'\n", file_name); @@ -1046,7 +1046,7 @@ void do_send_text (peer_id_t id, char *file_name) { close (fd); } else { buf[x] = 0; - do_send_message (id, buf, x); + tgl_do_send_message (id, buf, x); tfree_str (file_name); close (fd); } @@ -1077,7 +1077,7 @@ struct query_methods mark_read_encr_methods = { .type = TYPE_TO_PARAM(bool) }; -void do_messages_mark_read (peer_id_t id, int max_id) { +void tgl_do_messages_mark_read (tgl_peer_id_t id, int max_id) { clear_packet (); out_int (CODE_messages_read_history); out_peer_id (id); @@ -1086,35 +1086,35 @@ void do_messages_mark_read (peer_id_t id, int max_id) { send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &mark_read_methods, 0); } -void do_messages_mark_read_encr (peer_id_t id, long long access_hash, int last_time) { +void tgl_do_messages_mark_read_encr (tgl_peer_id_t id, long long access_hash, int last_time) { clear_packet (); out_int (CODE_messages_read_encrypted_history); out_int (CODE_input_encrypted_chat); - out_int (get_peer_id (id)); + out_int (tgl_get_peer_id (id)); out_long (access_hash); out_int (last_time); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &mark_read_encr_methods, 0); } -void do_mark_read (peer_id_t id) { - peer_t *P = peer_get (id); +void tgl_do_mark_read (tgl_peer_id_t id) { + tgl_peer_t *P = tgl_peer_get (id); if (!P) { rprintf ("Unknown peer\n"); return; } - if (get_peer_type (id) == PEER_USER || get_peer_type (id) == PEER_CHAT) { + if (tgl_get_peer_type (id) == TGL_PEER_USER || tgl_get_peer_type (id) == TGL_PEER_CHAT) { if (!P->last) { rprintf ("Unknown last peer message\n"); return; } - do_messages_mark_read (id, P->last->id); + tgl_do_messages_mark_read (id, P->last->id); return; } - assert (get_peer_type (id) == PEER_ENCR_CHAT); + assert (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT); if (P->last) { - do_messages_mark_read_encr (id, P->encr_chat.access_hash, P->last->date); + tgl_do_messages_mark_read_encr (id, P->encr_chat.access_hash, P->last->date); } else { - do_messages_mark_read_encr (id, P->encr_chat.access_hash, time (0) - 10); + tgl_do_messages_mark_read_encr (id, P->encr_chat.access_hash, time (0) - 10); } } @@ -1122,7 +1122,7 @@ void do_mark_read (peer_id_t id) { /* {{{ Get history */ int get_history_on_answer (struct query *q UU) { - static struct message *ML[10000]; + static struct tgl_message *ML[10000]; int i; int x = fetch_int (); if (x == (int)CODE_messages_messages_slice) { @@ -1134,7 +1134,7 @@ int get_history_on_answer (struct query *q UU) { assert (fetch_int () == CODE_vector); int n = fetch_int (); for (i = 0; i < n; i++) { - struct message *M = tglf_fetch_alloc_message (); + struct tgl_message *M = tglf_fetch_alloc_message (); if (i <= 9999) { ML[i] = M; } @@ -1155,7 +1155,7 @@ int get_history_on_answer (struct query *q UU) { tglf_fetch_alloc_user (); } if (sn > 0 && q->extra) { - do_messages_mark_read (*(peer_id_t *)&(q->extra), ML[0]->id); + tgl_do_messages_mark_read (*(tgl_peer_id_t *)&(q->extra), ML[0]->id); } return 0; } @@ -1165,10 +1165,10 @@ struct query_methods get_history_methods = { .type = TYPE_TO_PARAM(messages_messages) }; -void do_get_local_history (peer_id_t id, int limit) { - peer_t *P = peer_get (id); +void tgl_do_get_local_history (tgl_peer_id_t id, int limit) { + tgl_peer_t *P = tgl_peer_get (id); if (!P || !P->last) { return; } - struct message *M = P->last; + struct tgl_message *M = P->last; int count = 1; assert (!M->prev); while (count < limit && M->next) { @@ -1181,10 +1181,10 @@ void do_get_local_history (peer_id_t id, int limit) { } } -void do_get_history (peer_id_t id, int limit) { - if (get_peer_type (id) == PEER_ENCR_CHAT || offline_mode) { - do_get_local_history (id, limit); - do_mark_read (id); +void tgl_do_get_history (tgl_peer_id_t id, int limit) { + if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT || offline_mode) { + tgl_do_get_local_history (id, limit); + tgl_do_mark_read (id); return; } clear_packet (); @@ -1209,7 +1209,7 @@ int get_dialogs_on_answer (struct query *q UU) { int n, i; n = fetch_int (); static int dlist[2 * 100]; - static peer_id_t plist[100]; + static tgl_peer_id_t plist[100]; int dl_size = n; for (i = 0; i < n; i++) { assert (fetch_int () == (int)CODE_dialog); @@ -1242,16 +1242,16 @@ int get_dialogs_on_answer (struct query *q UU) { print_start (); push_color (COLOR_YELLOW); for (i = dl_size - 1; i >= 0; i--) { - peer_t *UC; - switch (get_peer_type (plist[i])) { - case PEER_USER: - UC = peer_get (plist[i]); + tgl_peer_t *UC; + switch (tgl_get_peer_type (plist[i])) { + case TGL_PEER_USER: + UC = tgl_peer_get (plist[i]); printf ("User "); print_user_name (plist[i], UC); printf (": %d unread\n", dlist[2 * i + 1]); break; - case PEER_CHAT: - UC = peer_get (plist[i]); + case TGL_PEER_CHAT: + UC = tgl_peer_get (plist[i]); printf ("Chat "); print_chat_name (plist[i], UC); printf (": %d unread\n", dlist[2 * i + 1]); @@ -1271,7 +1271,7 @@ struct query_methods get_dialogs_methods = { }; -void do_get_dialog_list (void) { +void tgl_do_get_dialog_list (void) { clear_packet (); out_int (CODE_messages_get_dialogs); out_int (0); @@ -1292,7 +1292,7 @@ struct send_file { int part_size; long long id; long long thumb_id; - peer_id_t to_id; + tgl_peer_id_t to_id; unsigned media_type; char *file_name; int encr; @@ -1301,22 +1301,22 @@ struct send_file { unsigned char *key; }; -void out_peer_id (peer_id_t id) { - peer_t *U; - switch (get_peer_type (id)) { - case PEER_CHAT: +void out_peer_id (tgl_peer_id_t id) { + tgl_peer_t *U; + switch (tgl_get_peer_type (id)) { + case TGL_PEER_CHAT: out_int (CODE_input_peer_chat); - out_int (get_peer_id (id)); + out_int (tgl_get_peer_id (id)); break; - case PEER_USER: - U = peer_get (id); + case TGL_PEER_USER: + U = tgl_peer_get (id); if (U && U->user.access_hash) { out_int (CODE_input_peer_foreign); - out_int (get_peer_id (id)); + out_int (tgl_get_peer_id (id)); out_long (U->user.access_hash); } else { out_int (CODE_input_peer_contact); - out_int (get_peer_id (id)); + out_int (tgl_get_peer_id (id)); } break; default: @@ -1333,7 +1333,7 @@ int send_file_part_on_answer (struct query *q) { int send_file_on_answer (struct query *q UU) { assert (fetch_int () == (int)CODE_messages_stated_message); - struct message *M = tglf_fetch_alloc_message (); + struct tgl_message *M = tglf_fetch_alloc_message (); assert (fetch_int () == CODE_vector); int n, i; n = fetch_int (); @@ -1356,7 +1356,7 @@ int send_encr_file_on_answer (struct query *q UU) { hexdump_in (); } assert (fetch_int () == (int)CODE_messages_sent_encrypted_file); - struct message *M = q->extra; + struct tgl_message *M = q->extra; M->date = fetch_int (); assert (fetch_int () == CODE_encrypted_file); M->media.encr_photo.id = fetch_long (); @@ -1366,7 +1366,7 @@ int send_encr_file_on_answer (struct query *q UU) { M->media.encr_photo.dc_id = fetch_int (); assert (fetch_int () == M->media.encr_photo.key_fingerprint); print_message (M); - message_insert (M); + tglm_message_insert (M); return 0; } @@ -1479,12 +1479,12 @@ void send_part (struct send_file *f) { out_long (-lrand48 () * (1ll << 32) - lrand48 ()); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_methods, 0); } else { - struct message *M = talloc0 (sizeof (*M)); + struct tgl_message *M = talloc0 (sizeof (*M)); out_int (CODE_messages_send_encrypted_file); out_int (CODE_input_encrypted_chat); - out_int (get_peer_id (f->to_id)); - peer_t *P = peer_get (f->to_id); + out_int (tgl_get_peer_id (f->to_id)); + tgl_peer_t *P = tgl_peer_get (f->to_id); assert (P); out_long (P->encr_chat.access_hash); long long r = -lrand48 () * (1ll << 32) - lrand48 (); @@ -1560,7 +1560,7 @@ void send_part (struct send_file *f) { M->media.encr_photo.size = f->size; M->flags = FLAG_ENCRYPTED; - M->from_id = MK_USER (tgl_state.our_id); + M->from_id = TGL_MK_USER (tgl_state.our_id); M->to_id = f->to_id; M->unread = 1; M->message = tstrdup (""); @@ -1586,7 +1586,7 @@ void send_file_thumb (struct send_file *f) { send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_part_methods, f); } -void do_send_photo (int type, peer_id_t to_id, char *file_name) { +void tgl_do_send_photo (int type, tgl_peer_id_t to_id, char *file_name) { int fd = open (file_name, O_RDONLY); if (fd < 0) { rprintf ("No such file '%s'\n", file_name); @@ -1625,7 +1625,7 @@ void do_send_photo (int type, peer_id_t to_id, char *file_name) { f->to_id = to_id; f->media_type = type; f->file_name = file_name; - if (get_peer_type (f->to_id) == PEER_ENCR_CHAT) { + if (tgl_get_peer_type (f->to_id) == TGL_PEER_ENCR_CHAT) { f->encr = 1; f->iv = talloc (32); secure_random (f->iv, 32); @@ -1650,7 +1650,7 @@ void do_send_photo (int type, peer_id_t to_id, char *file_name) { /* {{{ Forward */ int fwd_msg_on_answer (struct query *q UU) { assert (fetch_int () == (int)CODE_messages_stated_message); - struct message *M = tglf_fetch_alloc_message (); + struct tgl_message *M = tglf_fetch_alloc_message (); assert (fetch_int () == CODE_vector); int n, i; n = fetch_int (); @@ -1673,8 +1673,8 @@ struct query_methods fwd_msg_methods = { .type = TYPE_TO_PARAM(messages_stated_message) }; -void do_forward_message (peer_id_t id, int n) { - if (get_peer_type (id) == PEER_ENCR_CHAT) { +void tgl_do_forward_message (tgl_peer_id_t id, int n) { + if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT) { rprintf ("Can not forward messages from secret chat\n"); return; } @@ -1690,7 +1690,7 @@ void do_forward_message (peer_id_t id, int n) { /* {{{ Rename chat */ int rename_chat_on_answer (struct query *q UU) { assert (fetch_int () == (int)CODE_messages_stated_message); - struct message *M = tglf_fetch_alloc_message (); + struct tgl_message *M = tglf_fetch_alloc_message (); assert (fetch_int () == CODE_vector); int n, i; n = fetch_int (); @@ -1713,19 +1713,19 @@ struct query_methods rename_chat_methods = { .type = TYPE_TO_PARAM(messages_stated_message) }; -void do_rename_chat (peer_id_t id, char *name UU) { +void tgl_do_rename_chat (tgl_peer_id_t id, char *name UU) { clear_packet (); out_int (CODE_messages_edit_chat_title); - assert (get_peer_type (id) == PEER_CHAT); - out_int (get_peer_id (id)); + assert (tgl_get_peer_type (id) == TGL_PEER_CHAT); + out_int (tgl_get_peer_id (id)); out_string (name); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &rename_chat_methods, 0); } /* }}} */ /* {{{ Chat info */ -void print_chat_info (struct chat *C) { - peer_t *U = (void *)C; +void print_chat_info (struct tgl_chat *C) { + tgl_peer_t *U = (void *)C; print_start (); push_color (COLOR_YELLOW); printf ("Chat "); @@ -1734,9 +1734,9 @@ void print_chat_info (struct chat *C) { int i; for (i = 0; i < C->user_list_size; i++) { printf ("\t\t"); - print_user_name (MK_USER (C->user_list[i].user_id), peer_get (MK_USER (C->user_list[i].user_id))); + print_user_name (TGL_MK_USER (C->user_list[i].user_id), tgl_peer_get (TGL_MK_USER (C->user_list[i].user_id))); printf (" invited by "); - print_user_name (MK_USER (C->user_list[i].inviter_id), peer_get (MK_USER (C->user_list[i].inviter_id))); + print_user_name (TGL_MK_USER (C->user_list[i].inviter_id), tgl_peer_get (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) { @@ -1749,7 +1749,7 @@ void print_chat_info (struct chat *C) { } int chat_info_on_answer (struct query *q UU) { - struct chat *C = tglf_fetch_alloc_chat_full (); + struct tgl_chat *C = tglf_fetch_alloc_chat_full (); print_chat_info (C); return 0; } @@ -1759,9 +1759,9 @@ struct query_methods chat_info_methods = { .type = TYPE_TO_PARAM(messages_chat_full) }; -void do_get_chat_info (peer_id_t id) { +void tgl_do_get_chat_info (tgl_peer_id_t id) { if (offline_mode) { - peer_t *C = peer_get (id); + tgl_peer_t *C = tgl_peer_get (id); if (!C) { rprintf ("No such chat\n"); } else { @@ -1771,16 +1771,16 @@ void do_get_chat_info (peer_id_t id) { } clear_packet (); out_int (CODE_messages_get_full_chat); - assert (get_peer_type (id) == PEER_CHAT); - out_int (get_peer_id (id)); + assert (tgl_get_peer_type (id) == TGL_PEER_CHAT); + out_int (tgl_get_peer_id (id)); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &chat_info_methods, 0); } /* }}} */ /* {{{ User info */ -void print_user_info (struct user *U) { - peer_t *C = (void *)U; +void print_user_info (struct tgl_user *U) { + tgl_peer_t *C = (void *)U; print_start (); push_color (COLOR_YELLOW); printf ("User "); @@ -1800,7 +1800,7 @@ void print_user_info (struct user *U) { } int user_info_on_answer (struct query *q UU) { - struct user *U = tglf_fetch_alloc_user_full (); + struct tgl_user *U = tglf_fetch_alloc_user_full (); print_user_info (U); return 0; } @@ -1810,9 +1810,9 @@ struct query_methods user_info_methods = { .type = TYPE_TO_PARAM(user_full) }; -void do_get_user_info (peer_id_t id) { +void tgl_do_get_user_info (tgl_peer_id_t id) { if (offline_mode) { - peer_t *C = peer_get (id); + tgl_peer_t *C = tgl_peer_get (id); if (!C) { rprintf ("No such user\n"); } else { @@ -1822,15 +1822,15 @@ void do_get_user_info (peer_id_t id) { } clear_packet (); out_int (CODE_users_get_full_user); - assert (get_peer_type (id) == PEER_USER); - peer_t *U = peer_get (id); + assert (tgl_get_peer_type (id) == TGL_PEER_USER); + tgl_peer_t *U = tgl_peer_get (id); if (U && U->user.access_hash) { out_int (CODE_input_user_foreign); - out_int (get_peer_id (id)); + out_int (tgl_get_peer_id (id)); out_long (U->user.access_hash); } else { out_int (CODE_input_user_contact); - out_int (get_peer_id (id)); + out_int (tgl_get_peer_id (id)); } send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &user_info_methods, 0); } @@ -1852,7 +1852,7 @@ struct query_methods user_list_info_silent_methods = { .type = TYPE_TO_PARAM_1(vector, TYPE_TO_PARAM(user)) }; -void do_get_user_list_info_silent (int num, int *list) { +void tgl_do_get_user_list_info_silent (int num, int *list) { clear_packet (); out_int (CODE_users_get_users); out_int (CODE_vector); @@ -2007,7 +2007,7 @@ void load_next_part (struct download *D) { //send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &download_methods, D); } -void do_load_photo_size (struct photo_size *P, int next) { +void tgl_do_load_photo_size (struct tgl_photo_size *P, int next) { if (!P->loc.dc) { rprintf ("Bad video thumb\n"); return; @@ -2029,7 +2029,7 @@ void do_load_photo_size (struct photo_size *P, int next) { load_next_part (D); } -void do_load_photo (struct photo *photo, int next) { +void tgl_do_load_photo (struct tgl_photo *photo, int next) { if (!photo->sizes_num) { return; } int max = -1; int maxi = 0; @@ -2040,18 +2040,18 @@ void do_load_photo (struct photo *photo, int next) { maxi = i; } } - do_load_photo_size (&photo->sizes[maxi], next); + tgl_do_load_photo_size (&photo->sizes[maxi], next); } -void do_load_video_thumb (struct video *video, int next) { - do_load_photo_size (&video->thumb, next); +void tgl_do_load_video_thumb (struct tgl_video *video, int next) { + tgl_do_load_photo_size (&video->thumb, next); } -void do_load_document_thumb (struct document *video, int next) { - do_load_photo_size (&video->thumb, next); +void tgl_do_load_document_thumb (struct tgl_document *video, int next) { + tgl_do_load_photo_size (&video->thumb, next); } -void do_load_video (struct video *V, int next) { +void tgl_do_load_video (struct tgl_video *V, int next) { assert (V); assert (next); struct download *D = talloc0 (sizeof (*D)); @@ -2067,7 +2067,7 @@ void do_load_video (struct video *V, int next) { load_next_part (D); } -void do_load_audio (struct video *V, int next) { +void tgl_do_load_audio (struct tgl_video *V, int next) { assert (V); assert (next); struct download *D = talloc0 (sizeof (*D)); @@ -2083,7 +2083,7 @@ void do_load_audio (struct video *V, int next) { load_next_part (D); } -void do_load_document (struct document *V, int next) { +void tgl_do_load_document (struct tgl_document *V, int next) { assert (V); assert (next); struct download *D = talloc0 (sizeof (*D)); @@ -2099,7 +2099,7 @@ void do_load_document (struct document *V, int next) { load_next_part (D); } -void do_load_encr_video (struct encr_video *V, int next) { +void tgl_do_load_encr_video (struct tgl_encr_video *V, int next) { assert (V); assert (next); struct download *D = talloc0 (sizeof (*D)); @@ -2152,7 +2152,7 @@ struct query_methods export_auth_methods = { .type = TYPE_TO_PARAM(auth_exported_authorization) }; -void do_export_auth (int num) { +void tgl_do_export_auth (int num) { export_auth_str = 0; clear_packet (); out_int (CODE_auth_export_authorization); @@ -2178,9 +2178,9 @@ struct query_methods import_auth_methods = { .type = TYPE_TO_PARAM(auth_authorization) }; -void do_import_auth (int num) { +void tgl_do_import_auth (int num) { clear_packet (); - do_insert_header (); + tgl_do_insert_header (); out_int (CODE_auth_import_authorization); out_int (tgl_state.our_id); out_cstring (export_auth_str, export_auth_str_len); @@ -2214,11 +2214,11 @@ int add_contact_on_answer (struct query *q UU) { assert (fetch_int () == CODE_vector); n = fetch_int (); for (i = 0; i < n ; i++) { - struct user *U = tglf_fetch_alloc_user (); + struct tgl_user *U = tglf_fetch_alloc_user (); print_start (); push_color (COLOR_YELLOW); - printf ("User #%d: ", get_peer_id (U->id)); - print_user_name (U->id, (peer_t *)U); + printf ("User #%d: ", tgl_get_peer_id (U->id)); + print_user_name (U->id, (tgl_peer_t *)U); push_color (COLOR_GREEN); printf (" ("); printf ("%s", U->print_name); @@ -2251,7 +2251,7 @@ struct query_methods add_contact_methods = { .type = TYPE_TO_PARAM(contacts_imported_contacts) }; -void 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 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) { clear_packet (); out_int (CODE_contacts_import_contacts); out_int (CODE_vector); @@ -2276,14 +2276,14 @@ struct query_methods msg_search_methods = { .type = TYPE_TO_PARAM(messages_messages) }; -void do_msg_search (peer_id_t id, int from, int to, int limit, const char *s) { - if (get_peer_type (id) == PEER_ENCR_CHAT) { +void tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, const char *s) { + if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT) { rprintf ("Can not search in secure chat\n"); return; } clear_packet (); out_int (CODE_messages_search); - if (get_peer_type (id) == PEER_UNKNOWN) { + if (tgl_get_peer_type (id) == TGL_PEER_UNKNOWN) { out_int (CODE_input_peer_empty); } else { out_peer_id (id); @@ -2314,7 +2314,7 @@ int contacts_search_on_answer (struct query *q UU) { print_start (); push_color (COLOR_YELLOW); for (i = 0; i < n; i++) { - struct user *U = tglf_fetch_alloc_user (); + struct tgl_user *U = tglf_fetch_alloc_user (); printf ("User "); push_color (COLOR_RED); printf ("%s %s", U->first_name, U->last_name); @@ -2331,7 +2331,7 @@ struct query_methods contacts_search_methods = { .type = TYPE_TO_PARAM(contacts_found) }; -void do_contacts_search (int limit, const char *s) { +void tgl_do_contacts_search (int limit, const char *s) { clear_packet (); out_int (CODE_contacts_search); out_string (s); @@ -2342,7 +2342,7 @@ void do_contacts_search (int limit, const char *s) { /* {{{ Encr accept */ int send_encr_accept_on_answer (struct query *q UU) { - struct secret_chat *E = tglf_fetch_alloc_encrypted_chat (); + struct tgl_secret_chat *E = tglf_fetch_alloc_encrypted_chat (); if (E->state == sc_ok) { print_start (); @@ -2365,7 +2365,7 @@ int send_encr_accept_on_answer (struct query *q UU) { } int send_encr_request_on_answer (struct query *q UU) { - struct secret_chat *E = tglf_fetch_alloc_encrypted_chat (); + struct tgl_secret_chat *E = tglf_fetch_alloc_encrypted_chat (); if (E->state == sc_deleted) { print_start (); push_color (COLOR_YELLOW); @@ -2403,7 +2403,7 @@ unsigned char *encr_prime; int encr_param_version; BN_CTX *ctx; -void do_send_accept_encr_chat (struct secret_chat *E, unsigned char *random) { +void tgl_do_send_accept_encr_chat (struct tgl_secret_chat *E, unsigned char *random) { int i; int ok = 0; for (i = 0; i < 64; i++) { @@ -2446,7 +2446,7 @@ void do_send_accept_encr_chat (struct secret_chat *E, unsigned char *random) { clear_packet (); out_int (CODE_messages_accept_encryption); out_int (CODE_input_encrypted_chat); - out_int (get_peer_id (E->id)); + out_int (tgl_get_peer_id (E->id)); out_long (E->access_hash); ensure (BN_set_word (g_a, encr_root)); @@ -2465,7 +2465,7 @@ void do_send_accept_encr_chat (struct secret_chat *E, unsigned char *random) { send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_accept_methods, E); } -void do_create_keys_end (struct secret_chat *U) { +void tgl_do_create_keys_end (struct tgl_secret_chat *U) { assert (encr_prime); BIGNUM *g_b = BN_bin2bn (U->g_key, 256, 0); ensure_ptr (g_b); @@ -2514,7 +2514,7 @@ void do_create_keys_end (struct secret_chat *U) { BN_clear_free (a); } -void do_send_create_encr_chat (void *x, unsigned char *random) { +void tgl_do_send_create_encr_chat (void *x, unsigned char *random) { int user_id = (long)x; int i; unsigned char random_here[256]; @@ -2549,18 +2549,18 @@ void do_send_create_encr_chat (void *x, unsigned char *random) { BN_bn2bin (r, (void *)g_a); int t = lrand48 (); - while (peer_get (MK_ENCR_CHAT (t))) { + while (tgl_peer_get (TGL_MK_ENCR_CHAT (t))) { t = lrand48 (); } bl_do_encr_chat_init (t, user_id, (void *)random, (void *)g_a); - peer_t *_E = peer_get (MK_ENCR_CHAT (t)); + tgl_peer_t *_E = tgl_peer_get (TGL_MK_ENCR_CHAT (t)); assert (_E); - struct secret_chat *E = &_E->encr_chat; + struct tgl_secret_chat *E = &_E->encr_chat; clear_packet (); out_int (CODE_messages_request_encryption); - peer_t *U = peer_get (MK_USER (E->user_id)); + tgl_peer_t *U = tgl_peer_get (TGL_MK_USER (E->user_id)); assert (U); if (U && U->user.access_hash) { out_int (CODE_input_user_foreign); @@ -2570,7 +2570,7 @@ void do_send_create_encr_chat (void *x, unsigned char *random) { out_int (CODE_input_user_contact); out_int (E->user_id); } - out_int (get_peer_id (E->id)); + out_int (tgl_get_peer_id (E->id)); out_cstring (g_a, 256); write_secret_chat_file (); @@ -2617,7 +2617,7 @@ struct query_methods get_dh_config_methods = { .type = TYPE_TO_PARAM(messages_dh_config) }; -void do_accept_encr_chat_request (struct secret_chat *E) { +void tgl_do_accept_encr_chat_request (struct tgl_secret_chat *E) { assert (E->state == sc_request); clear_packet (); @@ -2625,18 +2625,18 @@ void do_accept_encr_chat_request (struct secret_chat *E) { out_int (encr_param_version); out_int (256); void **x = talloc (2 * sizeof (void *)); - x[0] = do_send_accept_encr_chat; + x[0] = tgl_do_send_accept_encr_chat; x[1] = E; send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dh_config_methods, x); } -void do_create_encr_chat_request (int user_id) { +void tgl_do_create_encr_chat_request (int user_id) { clear_packet (); out_int (CODE_messages_get_dh_config); out_int (encr_param_version); out_int (256); void **x = talloc (2 * sizeof (void *)); - x[0] = do_send_create_encr_chat; + x[0] = tgl_do_send_create_encr_chat; x[1] = (void *)(long)(user_id); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dh_config_methods, x); } @@ -2670,7 +2670,7 @@ int get_difference_on_answer (struct query *q UU) { int n, i; assert (fetch_int () == CODE_vector); n = fetch_int (); - static struct message *ML[10000]; + static struct tgl_message *ML[10000]; int ml_pos = 0; for (i = 0; i < n; i++) { if (ml_pos < 10000) { @@ -2714,7 +2714,7 @@ int get_difference_on_answer (struct query *q UU) { print_message (ML[i]); } if (x == CODE_updates_difference_slice) { - do_get_difference (); + tgl_do_get_difference (); } else { difference_got = 1; } @@ -2734,11 +2734,11 @@ struct query_methods get_difference_methods = { .type = TYPE_TO_PARAM(updates_difference) }; -void do_get_difference (void) { +void tgl_do_get_difference (void) { get_difference_active = 1; difference_got = 0; clear_packet (); - do_insert_header (); + tgl_do_insert_header (); if (seq > 0 || sync_from_start) { if (pts == 0) { pts = 1; } if (qts == 0) { qts = 1; } @@ -2758,9 +2758,9 @@ void do_get_difference (void) { /* {{{ Visualize key */ char *colors[4] = {COLOR_GREY, COLOR_CYAN, COLOR_BLUE, COLOR_GREEN}; -void do_visualize_key (peer_id_t id) { - assert (get_peer_type (id) == PEER_ENCR_CHAT); - peer_t *P = peer_get (id); +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); assert (P); if (P->encr_chat.state != sc_ok) { rprintf ("Chat is not initialized yet\n"); @@ -2807,8 +2807,8 @@ int get_suggested_on_answer (struct query *q UU) { print_start (); push_color (COLOR_YELLOW); for (i = 0; i < m; i++) { - peer_t *U = (void *)tglf_fetch_alloc_user (); - assert (get_peer_id (U->id) == l[2 * i]); + tgl_peer_t *U = (void *)tglf_fetch_alloc_user (); + 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]); } @@ -2822,7 +2822,7 @@ struct query_methods get_suggested_methods = { .type = TYPE_TO_PARAM(contacts_suggested) }; -void do_get_suggested (void) { +void tgl_do_get_suggested (void) { clear_packet (); out_int (CODE_contacts_get_suggested); out_int (100); @@ -2837,56 +2837,56 @@ struct query_methods add_user_to_chat_methods = { .type = TYPE_TO_PARAM(message_action) }; -void do_add_user_to_chat (peer_id_t chat_id, peer_id_t id, int limit) { +void tgl_do_add_user_to_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, int limit) { clear_packet (); out_int (CODE_messages_add_chat_user); - out_int (get_peer_id (chat_id)); + out_int (tgl_get_peer_id (chat_id)); - assert (get_peer_type (id) == PEER_USER); - peer_t *U = peer_get (id); + assert (tgl_get_peer_type (id) == TGL_PEER_USER); + tgl_peer_t *U = tgl_peer_get (id); if (U && U->user.access_hash) { out_int (CODE_input_user_foreign); - out_int (get_peer_id (id)); + out_int (tgl_get_peer_id (id)); out_long (U->user.access_hash); } else { out_int (CODE_input_user_contact); - out_int (get_peer_id (id)); + out_int (tgl_get_peer_id (id)); } out_int (limit); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &add_user_to_chat_methods, 0); } -void do_del_user_from_chat (peer_id_t chat_id, peer_id_t id) { +void tgl_do_del_user_from_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id) { clear_packet (); out_int (CODE_messages_delete_chat_user); - out_int (get_peer_id (chat_id)); + out_int (tgl_get_peer_id (chat_id)); - assert (get_peer_type (id) == PEER_USER); - peer_t *U = peer_get (id); + assert (tgl_get_peer_type (id) == TGL_PEER_USER); + tgl_peer_t *U = tgl_peer_get (id); if (U && U->user.access_hash) { out_int (CODE_input_user_foreign); - out_int (get_peer_id (id)); + out_int (tgl_get_peer_id (id)); out_long (U->user.access_hash); } else { out_int (CODE_input_user_contact); - out_int (get_peer_id (id)); + out_int (tgl_get_peer_id (id)); } send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &add_user_to_chat_methods, 0); } /* }}} */ /* {{{ Create secret chat */ -char *create_print_name (peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4); +char *create_print_name (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4); -void do_create_secret_chat (peer_id_t id) { - assert (get_peer_type (id) == PEER_USER); - peer_t *U = peer_get (id); +void tgl_do_create_secret_chat (tgl_peer_id_t id) { + assert (tgl_get_peer_type (id) == TGL_PEER_USER); + tgl_peer_t *U = tgl_peer_get (id); if (!U) { rprintf ("Can not create chat with unknown user\n"); return; } - do_create_encr_chat_request (get_peer_id (id)); + tgl_do_create_encr_chat_request (tgl_get_peer_id (id)); } /* }}} */ @@ -2896,9 +2896,9 @@ struct query_methods create_group_chat_methods = { .type = TYPE_TO_PARAM(message_action) }; -void do_create_group_chat (peer_id_t id, char *chat_topic) { - assert (get_peer_type (id) == PEER_USER); - peer_t *U = peer_get (id); +void tgl_do_create_group_chat (tgl_peer_id_t id, char *chat_topic) { + assert (tgl_get_peer_type (id) == TGL_PEER_USER); + tgl_peer_t *U = tgl_peer_get (id); if (!U) { rprintf ("Can not create chat with unknown user\n"); return; @@ -2909,11 +2909,11 @@ void do_create_group_chat (peer_id_t id, char *chat_topic) { out_int (1); // Number of users, currently we support only 1 user. if (U && U->user.access_hash) { out_int (CODE_input_user_foreign); - out_int (get_peer_id (id)); + out_int (tgl_get_peer_id (id)); out_long (U->user.access_hash); } else { out_int (CODE_input_user_contact); - out_int (get_peer_id (id)); + out_int (tgl_get_peer_id (id)); } out_string (chat_topic); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &create_group_chat_methods, 0); @@ -2936,7 +2936,7 @@ struct query_methods delete_msg_methods = { .type = TYPE_TO_PARAM_1(vector, TYPE_TO_PARAM (bare_int)) }; -void do_delete_msg (long long id) { +void tgl_do_delete_msg (long long id) { clear_packet (); out_int (CODE_messages_delete_messages); out_int (CODE_vector); @@ -2961,7 +2961,7 @@ struct query_methods restore_msg_methods = { .type = TYPE_TO_PARAM_1(vector, TYPE_TO_PARAM (bare_int)) }; -void do_restore_msg (long long id) { +void tgl_do_restore_msg (long long id) { clear_packet (); out_int (CODE_messages_restore_messages); out_int (CODE_vector); @@ -2980,7 +2980,7 @@ struct query_methods update_status_methods = { .type = TYPE_TO_PARAM(bool) }; -void do_update_status (int online UU) { +void tgl_do_update_status (int online UU) { clear_packet (); out_int (CODE_account_update_status); out_int (online ? CODE_bool_false : CODE_bool_true); diff --git a/queries.h b/queries.h index 84a9546..db45111 100644 --- a/queries.h +++ b/queries.h @@ -21,6 +21,7 @@ #define __QUERIES_H__ #include "structures.h" #include "auto.h" +#include "tgl-layout.h" #define QUERY_ACK_RECEIVED 1 @@ -65,59 +66,8 @@ void work_timers (void); extern struct query_methods help_get_config_methods; -void do_send_code (const char *user); -void do_phone_call (const char *user); -int do_send_code_result (const char *code); double get_double_time (void); -void do_update_contact_list (void); -union user_chat; -void do_send_message (peer_id_t id, const char *msg, int len); -void do_send_text (peer_id_t id, char *file); -void do_get_history (peer_id_t id, int limit); -void do_get_dialog_list (void); -void do_get_dialog_list_ex (void); -void do_send_photo (int type, peer_id_t to_id, char *file_name); -void do_get_chat_info (peer_id_t id); -void do_get_user_list_info_silent (int num, int *list); -void do_get_user_info (peer_id_t id); -void do_forward_message (peer_id_t id, int n); -void do_rename_chat (peer_id_t id, char *name); -void do_load_encr_video (struct encr_video *V, int next); -void do_create_encr_chat_request (int user_id); -void do_create_secret_chat (peer_id_t id); -void do_create_group_chat (peer_id_t id, char *chat_topic); -void do_get_suggested (void); - -struct photo; -struct video; -void do_load_photo (struct photo *photo, int next); -void do_load_video_thumb (struct video *video, int next); -void do_load_audio (struct video *V, int next); -void do_load_video (struct video *V, int next); -void do_load_document (struct document *V, int next); -void do_load_document_thumb (struct document *video, int next); -void do_help_get_config (void); -int do_auth_check_phone (const char *user); -int do_get_nearest_dc (void); -int do_send_code_result_auth (const char *code, const char *first_name, const char *last_name); -void do_import_auth (int num); -void do_export_auth (int num); -void 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 do_msg_search (peer_id_t id, int from, int to, int limit, const char *s); -void do_accept_encr_chat_request (struct secret_chat *E); -void do_get_difference (void); -void do_mark_read (peer_id_t id); -void do_visualize_key (peer_id_t id); -void do_create_keys_end (struct secret_chat *U); -void do_add_user_to_chat (peer_id_t chat_id, peer_id_t id, int limit); -void do_del_user_from_chat (peer_id_t chat_id, peer_id_t id); -void do_update_status (int online); -void do_contacts_search (int limit, const char *s); -void do_send_msg (struct message *M); -void do_delete_msg (long long id); -void do_restore_msg (long long id); -void do_send_encr_chat_layer (struct secret_chat *E); // For binlog diff --git a/structures.c b/structures.c index 5554b42..921ff5b 100644 --- a/structures.c +++ b/structures.c @@ -38,20 +38,20 @@ #define sha1 SHA1 -static int id_cmp (struct message *M1, struct message *M2); -#define peer_cmp(a,b) (cmp_peer_id (a->id, b->id)) +static int id_cmp (struct tgl_message *M1, struct tgl_message *M2); +#define peer_cmp(a,b) (tgl_cmp_peer_id (a->id, b->id)) #define peer_cmp_name(a,b) (strcmp (a->print_name, b->print_name)) -DEFINE_TREE(peer,peer_t *,peer_cmp,0) -DEFINE_TREE(peer_by_name,peer_t *,peer_cmp_name,0) -DEFINE_TREE(message,struct message *,id_cmp,0) +DEFINE_TREE(peer,tgl_peer_t *,peer_cmp,0) +DEFINE_TREE(peer_by_name,tgl_peer_t *,peer_cmp_name,0) +DEFINE_TREE(message,struct tgl_message *,id_cmp,0) -static struct message message_list = { +static struct tgl_message message_list = { .next_use = &message_list, .prev_use = &message_list }; -static struct tree_peer *peer_tree; +static struct tree_peer *tgl_peer_tree; static struct tree_peer_by_name *peer_by_name_tree; static struct tree_message *message_tree; static struct tree_message *message_unsent_tree; @@ -63,11 +63,11 @@ static int peer_num; static int encr_chats_allocated; static int geo_chats_allocated; -static peer_t *Peers[MAX_PEER_NUM]; +static tgl_peer_t *Peers[TGL_MAX_PEER_NUM]; -char *create_print_name (peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4) { +char *create_print_name (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]; @@ -89,8 +89,8 @@ char *create_print_name (peer_id_t id, const char *a1, const char *a2, const cha int fl = strlen (s); int cc = 0; while (1) { - peer_t *P = peer_lookup_name (s); - if (!P || !cmp_peer_id (P->id, id)) { + tgl_peer_t *P = tgl_peer_get_by_name (s); + if (!P || !tgl_cmp_peer_id (P->id, id)) { break; } cc ++; @@ -100,13 +100,9 @@ char *create_print_name (peer_id_t id, const char *a1, const char *a2, const cha return tstrdup (s); } -/* - * - * Fetch simple structures (immediate fetch into buffer) - * - */ +/* {{{ Fetch */ -int tglf_fetch_file_location (struct file_location *loc) { +int tglf_fetch_file_location (struct tgl_file_location *loc) { int x = fetch_int (); assert (x == CODE_file_location_unavailable || x == CODE_file_location); @@ -124,7 +120,7 @@ int tglf_fetch_file_location (struct file_location *loc) { return 0; } -int tglf_fetch_user_status (struct user_status *S) { +int tglf_fetch_user_status (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) { @@ -146,13 +142,7 @@ int tglf_fetch_user_status (struct user_status *S) { return 0; } -/* - * - * Fetch with log event - * - */ - -long long tglf_fetch_user_photo (struct user *U) { +long long tglf_fetch_user_photo (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) { @@ -163,8 +153,8 @@ long long tglf_fetch_user_photo (struct user *U) { if (x == CODE_user_profile_photo) { photo_id = fetch_long (); } - static struct file_location big; - static struct file_location small; + 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); @@ -172,16 +162,16 @@ long long tglf_fetch_user_photo (struct user *U) { return 0; } -int tglf_fetch_user (struct user *U) { +int tglf_fetch_user (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 = MK_USER (fetch_int ()); + U->id = TGL_MK_USER (fetch_int ()); if (x == CODE_user_empty) { return 0; } if (x == CODE_user_self) { - bl_do_set_our_id (get_peer_id (U->id)); + bl_do_set_our_id (tgl_get_peer_id (U->id)); } int new = !(U->flags & FLAG_CREATED); @@ -194,7 +184,7 @@ int tglf_fetch_user (struct user *U) { char *s2 = fetch_str (l2); if (x == CODE_user_deleted && !(U->flags & FLAG_DELETED)) { - bl_do_user_add (get_peer_id (U->id), s1, l1, s2, l2, 0, 0, 0, 0); + bl_do_user_add (tgl_get_peer_id (U->id), s1, l1, s2, l2, 0, 0, 0, 0); bl_do_user_delete (U); } if (x != CODE_user_deleted) { @@ -209,7 +199,7 @@ int tglf_fetch_user (struct user *U) { assert (phone_len >= 0); phone = fetch_str (phone_len); } - bl_do_user_add (get_peer_id (U->id), s1, l1, s2, l2, access_hash, phone, phone_len, x == CODE_user_contact); + bl_do_user_add (tgl_get_peer_id (U->id), s1, l1, s2, l2, access_hash, phone, phone_len, x == CODE_user_contact); assert (tglf_fetch_user_photo (U) >= 0); assert (tglf_fetch_user_status (&U->status) >= 0); @@ -254,7 +244,7 @@ int tglf_fetch_user (struct user *U) { return 0; } -void tglf_fetch_user_full (struct user *U) { +void tglf_fetch_user_full (struct tgl_user *U) { assert (fetch_int () == CODE_user_full); tglf_fetch_alloc_user (); assert (skip_type_any (TYPE_TO_PARAM (contacts_link)) >= 0); @@ -273,10 +263,10 @@ void tglf_fetch_user_full (struct user *U) { bl_do_user_set_real_name (U, s1, l1, s2, l2); } -void tglf_fetch_encrypted_chat (struct secret_chat *U) { +void tglf_fetch_encrypted_chat (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 = MK_ENCR_CHAT (fetch_int ()); + U->id = TGL_MK_ENCR_CHAT (fetch_int ()); if (x == CODE_encrypted_chat_empty) { return; } @@ -352,10 +342,10 @@ void tglf_fetch_encrypted_chat (struct secret_chat *U) { } } -void fetch_chat (struct chat *C) { +void tglf_fetch_chat (struct tgl_chat *C) { unsigned x = fetch_int (); assert (x == CODE_chat_empty || x == CODE_chat || x == CODE_chat_forbidden); - C->id = MK_CHAT (fetch_int ()); + C->id = TGL_MK_CHAT (fetch_int ()); if (x == CODE_chat_empty) { return; } @@ -368,8 +358,8 @@ void fetch_chat (struct chat *C) { int l = prefetch_strlen (); char *s = fetch_str (l); - struct file_location small; - struct file_location big; + struct tgl_file_location small; + struct tgl_file_location big; memset (&small, 0, sizeof (small)); memset (&big, 0, sizeof (big)); int users_num = -1; @@ -411,8 +401,8 @@ void fetch_chat (struct chat *C) { char *s = fetch_str (l); bl_do_chat_set_title (C, s, l); - struct file_location small; - struct file_location big; + struct tgl_file_location small; + struct tgl_file_location big; memset (&small, 0, sizeof (small)); memset (&big, 0, sizeof (big)); @@ -437,23 +427,23 @@ void fetch_chat (struct chat *C) { } } -void fetch_chat_full (struct chat *C) { +void tglf_fetch_chat_full (struct tgl_chat *C) { unsigned x = fetch_int (); assert (x == CODE_messages_chat_full); assert (fetch_int () == CODE_chat_full); - C->id = MK_CHAT (fetch_int ()); + C->id = TGL_MK_CHAT (fetch_int ()); x = fetch_int (); int version = 0; - struct chat_user *users = 0; + struct tgl_chat_user *users = 0; int users_num = 0; int admin_id = 0; if (x == CODE_chat_participants) { - assert (fetch_int () == get_peer_id (C->id)); + assert (fetch_int () == tgl_get_peer_id (C->id)); admin_id = fetch_int (); assert (fetch_int () == CODE_vector); users_num = fetch_int (); - users = talloc (sizeof (struct chat_user) * users_num); + users = talloc (sizeof (struct tgl_chat_user) * users_num); int i; for (i = 0; i < users_num; i++) { assert (fetch_int () == (int)CODE_chat_participant); @@ -484,12 +474,12 @@ void fetch_chat_full (struct chat *C) { } if (version > 0) { bl_do_chat_set_participants (C, version, users_num, users); - tfree (users, sizeof (struct chat_user) * users_num); + tfree (users, sizeof (struct tgl_chat_user) * users_num); } bl_do_chat_set_full_photo (C, start, 4 * (end - start)); } -void tglf_fetch_photo_size (struct photo_size *S) { +void tglf_fetch_photo_size (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); @@ -507,7 +497,7 @@ void tglf_fetch_photo_size (struct photo_size *S) { } } -void fetch_geo (struct geo *G) { +void fetch_geo (struct tgl_geo *G) { unsigned x = fetch_int (); if (x == CODE_geo_point) { G->longitude = fetch_double (); @@ -519,7 +509,7 @@ void fetch_geo (struct geo *G) { } } -void tglf_fetch_photo (struct photo *P) { +void tglf_fetch_photo (struct tgl_photo *P) { memset (P, 0, sizeof (*P)); unsigned x = fetch_int (); assert (x == CODE_photo_empty || x == CODE_photo); @@ -532,14 +522,14 @@ void tglf_fetch_photo (struct photo *P) { fetch_geo (&P->geo); assert (fetch_int () == CODE_vector); P->sizes_num = fetch_int (); - P->sizes = talloc (sizeof (struct photo_size) * P->sizes_num); + 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]); } } -void fetch_video (struct video *V) { +void tglf_fetch_video (struct tgl_video *V) { memset (V, 0, sizeof (*V)); unsigned x = fetch_int (); V->id = fetch_long (); @@ -557,7 +547,7 @@ void fetch_video (struct video *V) { V->h = fetch_int (); } -void fetch_audio (struct audio *V) { +void tglf_fetch_audio (struct tgl_audio *V) { memset (V, 0, sizeof (*V)); unsigned x = fetch_int (); V->id = fetch_long (); @@ -571,7 +561,7 @@ void fetch_audio (struct audio *V) { V->dc_id = fetch_int (); } -void fetch_document (struct document *V) { +void tglf_fetch_document (struct tgl_document *V) { memset (V, 0, sizeof (*V)); unsigned x = fetch_int (); V->id = fetch_long (); @@ -586,7 +576,7 @@ void fetch_document (struct document *V) { V->dc_id = fetch_int (); } -void tglf_fetch_message_action (struct message_action *M) { +void tglf_fetch_message_action (struct tgl_message_action *M) { memset (M, 0, sizeof (*M)); unsigned x = fetch_int (); M->type = x; @@ -630,7 +620,7 @@ void tglf_fetch_message_action (struct message_action *M) { } } -void tglf_fetch_message_short (struct message *M) { +void tglf_fetch_message_short (struct tgl_message *M) { int new = !(M->flags & FLAG_CREATED); if (new) { @@ -645,7 +635,7 @@ void tglf_fetch_message_short (struct message *M) { int date = fetch_int (); fetch_seq (); - bl_do_create_message_text (id, from_id, PEER_USER, to_id, date, l, s); + bl_do_create_message_text (id, from_id, TGL_PEER_USER, to_id, date, l, s); } else { fetch_int (); // id fetch_int (); // from_id @@ -658,7 +648,7 @@ void tglf_fetch_message_short (struct message *M) { } } -void tglf_fetch_message_short_chat (struct message *M) { +void tglf_fetch_message_short_chat (struct tgl_message *M) { int new = !(M->flags & FLAG_CREATED); if (new) { @@ -673,7 +663,7 @@ void tglf_fetch_message_short_chat (struct message *M) { int date = fetch_int (); fetch_seq (); - bl_do_create_message_text (id, from_id, PEER_CHAT, to_id, date, l, s); + bl_do_create_message_text (id, from_id, TGL_PEER_CHAT, to_id, date, l, s); } else { fetch_int (); // id fetch_int (); // from_id @@ -688,7 +678,7 @@ void tglf_fetch_message_short_chat (struct message *M) { } -void tglf_fetch_message_media (struct message_media *M) { +void tglf_fetch_message_media (struct tgl_message_media *M) { memset (M, 0, sizeof (*M)); M->type = fetch_int (); switch (M->type) { @@ -698,13 +688,13 @@ void tglf_fetch_message_media (struct message_media *M) { tglf_fetch_photo (&M->photo); break; case CODE_message_media_video: - fetch_video (&M->video); + tglf_fetch_video (&M->video); break; case CODE_message_media_audio: - fetch_audio (&M->audio); + tglf_fetch_audio (&M->audio); break; case CODE_message_media_document: - fetch_document (&M->document); + tglf_fetch_document (&M->document); break; case CODE_message_media_geo: fetch_geo (&M->geo); @@ -726,7 +716,7 @@ void tglf_fetch_message_media (struct message_media *M) { } } -void tglf_fetch_message_media_encrypted (struct message_media *M) { +void tglf_fetch_message_media_encrypted (struct tgl_message_media *M) { memset (M, 0, sizeof (*M)); unsigned x = fetch_int (); int l; @@ -883,7 +873,7 @@ void tglf_fetch_message_media_encrypted (struct message_media *M) { } } -void tglf_fetch_message_action_encrypted (struct message_action *M) { +void tglf_fetch_message_action_encrypted (struct tgl_message_action *M) { unsigned x = fetch_int (); switch (x) { case CODE_decrypted_message_action_set_message_t_t_l: @@ -898,7 +888,7 @@ void tglf_fetch_message_action_encrypted (struct message_action *M) { M->read_cnt = n; while (n -- > 0) { long long id = fetch_long (); - struct message *N = message_get (id); + struct tgl_message *N = tgl_message_get (id); if (N) { N->unread = 0; } @@ -940,17 +930,17 @@ void tglf_fetch_message_action_encrypted (struct message_action *M) { } } -peer_id_t tglf_fetch_peer_id (void) { +tgl_peer_id_t tglf_fetch_peer_id (void) { unsigned x =fetch_int (); if (x == CODE_peer_user) { - return MK_USER (fetch_int ()); + return TGL_MK_USER (fetch_int ()); } else { assert (CODE_peer_chat); - return MK_CHAT (fetch_int ()); + return TGL_MK_CHAT (fetch_int ()); } } -void fetch_message (struct message *M) { +void tglf_fetch_message (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 id = fetch_int (); @@ -966,7 +956,7 @@ void fetch_message (struct message *M) { fwd_date = fetch_int (); } int from_id = fetch_int (); - peer_id_t to_id = tglf_fetch_peer_id (); + tgl_peer_id_t to_id = tglf_fetch_peer_id (); fetch_bool (); // out. @@ -982,9 +972,9 @@ void fetch_message (struct message *M) { if (new) { if (fwd_from_id) { - bl_do_create_message_service_fwd (id, from_id, get_peer_type (to_id), get_peer_id (to_id), date, fwd_from_id, fwd_date, start, (in_ptr - start)); + 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, start, (in_ptr - start)); } else { - bl_do_create_message_service (id, from_id, get_peer_type (to_id), get_peer_id (to_id), date, start, (in_ptr - start)); + bl_do_create_message_service (id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, start, (in_ptr - start)); } } } else { @@ -996,26 +986,26 @@ void fetch_message (struct message *M) { if (new) { if (fwd_from_id) { - bl_do_create_message_media_fwd (id, from_id, get_peer_type (to_id), get_peer_id (to_id), date, fwd_from_id, fwd_date, l, s, start, in_ptr - start); + 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, l, s, start, in_ptr - start); } else { - bl_do_create_message_media (id, from_id, get_peer_type (to_id), get_peer_id (to_id), date, l, s, start, in_ptr - start); + bl_do_create_message_media (id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, l, s, start, in_ptr - start); } } } bl_do_set_unread (M, unread); } -void fetch_geo_message (struct message *M) { +void tglf_fetch_geo_message (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); - M->to_id = MK_GEO_CHAT (fetch_int ()); + M->to_id = TGL_MK_GEO_CHAT (fetch_int ()); M->id = fetch_int (); if (x == CODE_geo_chat_message_empty) { M->flags |= 1; return; } - M->from_id = MK_USER (fetch_int ()); + M->from_id = TGL_MK_USER (fetch_int ()); M->date = fetch_int (); if (x == CODE_geo_chat_message_service) { M->service = 1; @@ -1030,7 +1020,7 @@ void fetch_geo_message (struct message *M) { static int *decr_ptr; static int *decr_end; -int decrypt_encrypted_message (struct secret_chat *E) { +static int decrypt_encrypted_message (struct tgl_secret_chat *E) { int *msg_key = decr_ptr; decr_ptr += 4; assert (decr_ptr < decr_end); @@ -1087,17 +1077,17 @@ int decrypt_encrypted_message (struct secret_chat *E) { return 0; } -void tglf_fetch_encrypted_message (struct message *M) { +void tglf_fetch_encrypted_message (struct tgl_message *M) { unsigned x = fetch_int (); assert (x == CODE_encrypted_message || x == CODE_encrypted_message_service); unsigned sx = x; int new = !(M->flags & FLAG_CREATED); long long id = fetch_long (); int to_id = fetch_int (); - peer_id_t chat = MK_ENCR_CHAT (to_id); + tgl_peer_id_t chat = TGL_MK_ENCR_CHAT (to_id); int date = fetch_int (); - peer_t *P = peer_get (chat); + tgl_peer_t *P = tgl_peer_get (chat); if (!P) { vlogprintf (E_WARNING, "Encrypted message to unknown chat. Dropping\n"); M->flags |= FLAG_MESSAGE_EMPTY; @@ -1158,9 +1148,9 @@ void tglf_fetch_encrypted_message (struct message *M) { int *start_file = in_ptr; assert (skip_type_any (TYPE_TO_PARAM (encrypted_file)) >= 0); if (x == CODE_decrypted_message) { - bl_do_create_message_media_encr (id, P->encr_chat.user_id, PEER_ENCR_CHAT, to_id, date, l, s, start, end - start, start_file, in_ptr - start_file); + 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); } else if (x == CODE_decrypted_message_service) { - bl_do_create_message_service_encr (id, P->encr_chat.user_id, PEER_ENCR_CHAT, to_id, date, start, end - start); + bl_do_create_message_service_encr (id, P->encr_chat.user_id, TGL_PEER_ENCR_CHAT, to_id, date, start, end - start); } } else { assert (skip_type_any (TYPE_TO_PARAM (encrypted_file)) >= 0); @@ -1168,12 +1158,12 @@ void tglf_fetch_encrypted_message (struct message *M) { } } else { if (ok && x == CODE_decrypted_message_service) { - bl_do_create_message_service_encr (id, P->encr_chat.user_id, PEER_ENCR_CHAT, to_id, date, start, end - start); + bl_do_create_message_service_encr (id, P->encr_chat.user_id, TGL_PEER_ENCR_CHAT, to_id, date, start, end - start); } } } -void tglf_fetch_encrypted_message_file (struct message_media *M) { +void tglf_fetch_encrypted_message_file (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) { @@ -1193,134 +1183,252 @@ void tglf_fetch_encrypted_message_file (struct message_media *M) { } } -static int id_cmp (struct message *M1, struct message *M2) { +static int id_cmp (struct tgl_message *M1, struct tgl_message *M2) { if (M1->id < M2->id) { return -1; } else if (M1->id > M2->id) { return 1; } else { return 0; } } -struct user *tglf_fetch_alloc_user (void) { +struct tgl_user *tglf_fetch_alloc_user (void) { int data[2]; prefetch_data (data, 8); - peer_t *U = peer_get (MK_USER (data[1])); + tgl_peer_t *U = tgl_peer_get (TGL_MK_USER (data[1])); if (!U) { users_allocated ++; U = talloc0 (sizeof (*U)); - U->id = MK_USER (data[1]); - peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); - assert (peer_num < MAX_PEER_NUM); + U->id = TGL_MK_USER (data[1]); + tgl_peer_tree = tree_insert_peer (tgl_peer_tree, U, lrand48 ()); + assert (peer_num < TGL_MAX_PEER_NUM); Peers[peer_num ++] = U; } tglf_fetch_user (&U->user); return &U->user; } -struct secret_chat *tglf_fetch_alloc_encrypted_chat (void) { +struct tgl_secret_chat *tglf_fetch_alloc_encrypted_chat (void) { int data[2]; prefetch_data (data, 8); - peer_t *U = peer_get (MK_ENCR_CHAT (data[1])); + tgl_peer_t *U = tgl_peer_get (TGL_MK_ENCR_CHAT (data[1])); if (!U) { U = talloc0 (sizeof (*U)); - U->id = MK_ENCR_CHAT (data[1]); + U->id = TGL_MK_ENCR_CHAT (data[1]); encr_chats_allocated ++; - peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); - assert (peer_num < MAX_PEER_NUM); + tgl_peer_tree = tree_insert_peer (tgl_peer_tree, U, lrand48 ()); + assert (peer_num < TGL_MAX_PEER_NUM); Peers[peer_num ++] = U; } tglf_fetch_encrypted_chat (&U->encr_chat); return &U->encr_chat; } -void insert_encrypted_chat (peer_t *P) { - encr_chats_allocated ++; - peer_tree = tree_insert_peer (peer_tree, P, lrand48 ()); - assert (peer_num < MAX_PEER_NUM); - Peers[peer_num ++] = P; -} - -void insert_user (peer_t *P) { - users_allocated ++; - peer_tree = tree_insert_peer (peer_tree, P, lrand48 ()); - assert (peer_num < MAX_PEER_NUM); - Peers[peer_num ++] = P; -} - -void insert_chat (peer_t *P) { - chats_allocated ++; - peer_tree = tree_insert_peer (peer_tree, P, lrand48 ()); - assert (peer_num < MAX_PEER_NUM); - Peers[peer_num ++] = P; -} - -struct user *tglf_fetch_alloc_user_full (void) { +struct tgl_user *tglf_fetch_alloc_user_full (void) { int data[3]; prefetch_data (data, 12); - peer_t *U = peer_get (MK_USER (data[2])); + tgl_peer_t *U = tgl_peer_get (TGL_MK_USER (data[2])); if (U) { tglf_fetch_user_full (&U->user); return &U->user; } else { users_allocated ++; U = talloc0 (sizeof (*U)); - U->id = MK_USER (data[2]); - peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); + U->id = TGL_MK_USER (data[2]); + tgl_peer_tree = tree_insert_peer (tgl_peer_tree, U, lrand48 ()); tglf_fetch_user_full (&U->user); - assert (peer_num < MAX_PEER_NUM); + assert (peer_num < TGL_MAX_PEER_NUM); Peers[peer_num ++] = U; return &U->user; } } -void free_user (struct user *U) { +struct tgl_message *tglf_fetch_alloc_message (void) { + int data[2]; + prefetch_data (data, 8); + struct tgl_message *M = tgl_message_get (data[1]); + + if (!M) { + M = tglm_message_alloc (data[1]); + } + tglf_fetch_message (M); + return M; +} + +struct tgl_message *tglf_fetch_alloc_geo_message (void) { + struct tgl_message *M = talloc (sizeof (*M)); + tglf_fetch_geo_message (M); + struct tgl_message *M1 = tree_lookup_message (message_tree, M); + messages_allocated ++; + if (M1) { + tglm_message_del_use (M1); + tglm_message_del_peer (M1); + tgls_free_message (M1); + memcpy (M1, M, sizeof (*M)); + tfree (M, sizeof (*M)); + tglm_message_add_use (M1); + tglm_message_add_peer (M1); + messages_allocated --; + return M1; + } else { + tglm_message_add_use (M); + tglm_message_add_peer (M); + message_tree = tree_insert_message (message_tree, M, lrand48 ()); + return M; + } +} + +struct tgl_message *tglf_fetch_alloc_encrypted_message (void) { + int data[3]; + prefetch_data (data, 12); + struct tgl_message *M = tgl_message_get (*(long long *)(data + 1)); + + if (!M) { + M = talloc0 (sizeof (*M)); + M->id = *(long long *)(data + 1); + tglm_message_insert_tree (M); + messages_allocated ++; + assert (tgl_message_get (M->id) == M); + } + tglf_fetch_encrypted_message (M); + return M; +} + +struct tgl_message *tglf_fetch_alloc_message_short (void) { + int data[1]; + prefetch_data (data, 4); + struct tgl_message *M = tgl_message_get (data[0]); + + if (!M) { + M = talloc0 (sizeof (*M)); + M->id = data[0]; + tglm_message_insert_tree (M); + messages_allocated ++; + } + tglf_fetch_message_short (M); + return M; +} + +struct tgl_message *tglf_fetch_alloc_message_short_chat (void) { + int data[1]; + prefetch_data (data, 4); + struct tgl_message *M = tgl_message_get (data[0]); + + if (!M) { + M = talloc0 (sizeof (*M)); + M->id = data[0]; + tglm_message_insert_tree (M); + messages_allocated ++; + } + tglf_fetch_message_short_chat (M); + return M; +} + +struct tgl_chat *tglf_fetch_alloc_chat (void) { + int data[2]; + prefetch_data (data, 8); + tgl_peer_t *U = tgl_peer_get (TGL_MK_CHAT (data[1])); + if (!U) { + chats_allocated ++; + U = talloc0 (sizeof (*U)); + U->id = TGL_MK_CHAT (data[1]); + tgl_peer_tree = tree_insert_peer (tgl_peer_tree, U, lrand48 ()); + assert (peer_num < TGL_MAX_PEER_NUM); + Peers[peer_num ++] = U; + } + tglf_fetch_chat (&U->chat); + return &U->chat; +} + +struct tgl_chat *tglf_fetch_alloc_chat_full (void) { + int data[3]; + prefetch_data (data, 12); + tgl_peer_t *U = tgl_peer_get (TGL_MK_CHAT (data[2])); + if (U) { + tglf_fetch_chat_full (&U->chat); + return &U->chat; + } else { + chats_allocated ++; + U = talloc0 (sizeof (*U)); + U->id = TGL_MK_CHAT (data[2]); + tgl_peer_tree = tree_insert_peer (tgl_peer_tree, U, lrand48 ()); + tglf_fetch_chat_full (&U->chat); + assert (peer_num < TGL_MAX_PEER_NUM); + Peers[peer_num ++] = U; + return &U->chat; + } +} +/* }}} */ + +void tglp_insert_encrypted_chat (tgl_peer_t *P) { + encr_chats_allocated ++; + tgl_peer_tree = tree_insert_peer (tgl_peer_tree, P, lrand48 ()); + assert (peer_num < TGL_MAX_PEER_NUM); + Peers[peer_num ++] = P; +} + +void tglp_insert_user (tgl_peer_t *P) { + users_allocated ++; + tgl_peer_tree = tree_insert_peer (tgl_peer_tree, P, lrand48 ()); + assert (peer_num < TGL_MAX_PEER_NUM); + Peers[peer_num ++] = P; +} + +void tglp_insert_chat (tgl_peer_t *P) { + chats_allocated ++; + tgl_peer_tree = tree_insert_peer (tgl_peer_tree, P, lrand48 ()); + assert (peer_num < TGL_MAX_PEER_NUM); + Peers[peer_num ++] = P; +} + +/* {{{ Free */ +void tgls_free_user (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); } } -void free_photo_size (struct photo_size *S) { +void tgls_free_photo_size (struct tgl_photo_size *S) { tfree_str (S->type); if (S->data) { tfree (S->data, S->size); } } -void free_photo (struct photo *P) { +void tgls_free_photo (struct tgl_photo *P) { if (!P->access_hash) { return; } if (P->caption) { tfree_str (P->caption); } if (P->sizes) { int i; for (i = 0; i < P->sizes_num; i++) { - free_photo_size (&P->sizes[i]); + tgls_free_photo_size (&P->sizes[i]); } - tfree (P->sizes, sizeof (struct photo_size) * P->sizes_num); + tfree (P->sizes, sizeof (struct tgl_photo_size) * P->sizes_num); } } -void free_video (struct video *V) { +void tgls_free_video (struct tgl_video *V) { if (!V->access_hash) { return; } tfree_str (V->caption); - free_photo_size (&V->thumb); + tgls_free_photo_size (&V->thumb); } -void free_document (struct document *D) { +void tgls_free_document (struct tgl_document *D) { if (!D->access_hash) { return; } tfree_str (D->caption); tfree_str (D->mime_type); - free_photo_size (&D->thumb); + tgls_free_photo_size (&D->thumb); } -void free_message_media (struct message_media *M) { +void tgls_free_message_media (struct tgl_message_media *M) { switch (M->type) { case CODE_message_media_empty: case CODE_message_media_geo: case CODE_message_media_audio: return; case CODE_message_media_photo: - free_photo (&M->photo); + tgls_free_photo (&M->photo); return; case CODE_message_media_video: - free_video (&M->video); + tgls_free_video (&M->video); return; case CODE_message_media_contact: tfree_str (M->phone); @@ -1328,7 +1436,7 @@ void free_message_media (struct message_media *M) { tfree_str (M->last_name); return; case CODE_message_media_document: - free_document (&M->document); + tgls_free_document (&M->document); return; case CODE_message_media_unsupported: tfree (M->data, M->data_size); @@ -1348,7 +1456,7 @@ void free_message_media (struct message_media *M) { } } -void free_message_action (struct message_action *M) { +void tgls_free_message_action (struct tgl_message_action *M) { switch (M->type) { case CODE_message_action_empty: break; @@ -1360,7 +1468,7 @@ void free_message_action (struct message_action *M) { tfree_str (M->new_title); break; case CODE_message_action_chat_edit_photo: - free_photo (&M->photo); + tgls_free_photo (&M->photo); break; case CODE_message_action_chat_delete_photo: break; @@ -1375,63 +1483,71 @@ void free_message_action (struct message_action *M) { } } -void free_message (struct message *M) { +void tgls_free_message (struct tgl_message *M) { if (!M->service) { if (M->message) { tfree (M->message, M->message_len + 1); } - free_message_media (&M->media); + tgls_free_message_media (&M->media); } else { - free_message_action (&M->action); + tgls_free_message_action (&M->action); } } -void message_del_use (struct message *M) { +void tgls_free_chat (struct tgl_chat *U) { + if (U->title) { tfree_str (U->title); } + if (U->print_title) { tfree_str (U->print_title); } +} +/* }}} */ + +/* Messages {{{ */ + +void tglm_message_del_use (struct tgl_message *M) { M->next_use->prev_use = M->prev_use; M->prev_use->next_use = M->next_use; } -void message_add_use (struct message *M) { +void tglm_message_add_use (struct tgl_message *M) { M->next_use = message_list.next_use; M->prev_use = &message_list; M->next_use->prev_use = M; M->prev_use->next_use = M; } -void message_add_peer (struct message *M) { - peer_id_t id; - if (!cmp_peer_id (M->to_id, MK_USER (tgl_state.our_id))) { +void tglm_message_add_peer (struct tgl_message *M) { + tgl_peer_id_t id; + if (!tgl_cmp_peer_id (M->to_id, TGL_MK_USER (tgl_state.our_id))) { id = M->from_id; } else { id = M->to_id; } - peer_t *P = peer_get (id); + tgl_peer_t *P = tgl_peer_get (id); if (!P) { P = talloc0 (sizeof (*P)); P->id = id; - switch (get_peer_type (id)) { - case PEER_USER: + switch (tgl_get_peer_type (id)) { + case TGL_PEER_USER: users_allocated ++; break; - case PEER_CHAT: + case TGL_PEER_CHAT: chats_allocated ++; break; - case PEER_GEO_CHAT: + case TGL_PEER_GEO_CHAT: geo_chats_allocated ++; break; - case PEER_ENCR_CHAT: + case TGL_PEER_ENCR_CHAT: encr_chats_allocated ++; break; } - peer_tree = tree_insert_peer (peer_tree, P, lrand48 ()); - assert (peer_num < MAX_PEER_NUM); + tgl_peer_tree = tree_insert_peer (tgl_peer_tree, P, lrand48 ()); + assert (peer_num < TGL_MAX_PEER_NUM); Peers[peer_num ++] = P; } if (!P->last) { P->last = M; M->prev = M->next = 0; } else { - if (get_peer_type (P->id) != PEER_ENCR_CHAT) { - struct message *N = P->last; - struct message *NP = 0; + if (tgl_get_peer_type (P->id) != TGL_PEER_ENCR_CHAT) { + struct tgl_message *N = P->last; + struct tgl_message *NP = 0; while (N && N->id > M->id) { NP = N; N = N->next; @@ -1443,8 +1559,8 @@ void message_add_peer (struct message *M) { if (NP) { NP->next = M; } else { P->last = M; } } else { - struct message *N = P->last; - struct message *NP = 0; + struct tgl_message *N = P->last; + struct tgl_message *NP = 0; M->next = N; M->prev = NP; if (N) { N->prev = M; } @@ -1454,14 +1570,14 @@ void message_add_peer (struct message *M) { } } -void message_del_peer (struct message *M) { - peer_id_t id; - if (!cmp_peer_id (M->to_id, MK_USER (tgl_state.our_id))) { +void tglm_message_del_peer (struct tgl_message *M) { + tgl_peer_id_t id; + if (!tgl_cmp_peer_id (M->to_id, TGL_MK_USER (tgl_state.our_id))) { id = M->from_id; } else { id = M->to_id; } - peer_t *P = peer_get (id); + tgl_peer_t *P = tgl_peer_get (id); if (M->prev) { M->prev->next = M->next; } @@ -1473,133 +1589,136 @@ void message_del_peer (struct message *M) { } } -struct message *message_alloc (long long id) { - struct message *M = talloc0 (sizeof (*M)); +struct tgl_message *tglm_message_alloc (long long id) { + struct tgl_message *M = talloc0 (sizeof (*M)); M->id = id; - message_insert_tree (M); + tglm_message_insert_tree (M); messages_allocated ++; return M; } -struct message *tglf_fetch_alloc_message (void) { - int data[2]; - prefetch_data (data, 8); - struct message *M = message_get (data[1]); - - if (!M) { - M = message_alloc (data[1]); - } - fetch_message (M); - return M; +void tglm_update_message_id (struct tgl_message *M, long long id) { + message_tree = tree_delete_message (message_tree, M); + M->id = id; + message_tree = tree_insert_message (message_tree, M, lrand48 ()); } -struct message *tglf_fetch_alloc_geo_message (void) { - struct message *M = talloc (sizeof (*M)); - fetch_geo_message (M); - struct message *M1 = tree_lookup_message (message_tree, M); - messages_allocated ++; - if (M1) { - message_del_use (M1); - message_del_peer (M1); - free_message (M1); - memcpy (M1, M, sizeof (*M)); - tfree (M, sizeof (*M)); - message_add_use (M1); - message_add_peer (M1); - messages_allocated --; - return M1; +void tglm_message_insert_tree (struct tgl_message *M) { + assert (M->id); + message_tree = tree_insert_message (message_tree, M, lrand48 ()); +} + +void tglm_message_remove_tree (struct tgl_message *M) { + assert (M->id); + message_tree = tree_delete_message (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_unsent (struct tgl_message *M) { + message_unsent_tree = tree_insert_message (message_unsent_tree, M, lrand48 ()); +} + +void tglm_message_remove_unsent (struct tgl_message *M) { + message_unsent_tree = tree_delete_message (message_unsent_tree, M); +} + +static void __send_msg (struct tgl_message *M) { + vlogprintf (E_NOTICE, "Resending message...\n"); + print_message (M); + + tgl_do_send_msg (M); +} + +void tglm_send_all_unsent (void ) { + tree_act_message (message_unsent_tree, __send_msg); +} +/* }}} */ + +void tglp_peer_insert_name (tgl_peer_t *P) { + peer_by_name_tree = tree_insert_peer_by_name (peer_by_name_tree, P, lrand48 ()); +} + +void tglp_peer_delete_name (tgl_peer_t *P) { + peer_by_name_tree = tree_delete_peer_by_name (peer_by_name_tree, P); +} + +tgl_peer_t *tgl_peer_get (tgl_peer_id_t id) { + static tgl_peer_t U; + U.id = id; + return tree_lookup_peer (tgl_peer_tree, &U); +} + +struct tgl_message *tgl_message_get (long long id) { + struct tgl_message M; + M.id = id; + return tree_lookup_message (message_tree, &M); +} + +tgl_peer_t *tgl_peer_get_by_name (const char *s) { + static tgl_peer_t P; + P.print_name = (void *)s; + tgl_peer_t *R = tree_lookup_peer_by_name (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_peer_tree, it, extra); +} + +int tgl_complete_user_list (int index, const char *text, int len, char **R) { + index ++; + while (index < peer_num && (!Peers[index]->print_name || strncmp (Peers[index]->print_name, text, len) || tgl_get_peer_type (Peers[index]->id) != TGL_PEER_USER)) { + index ++; + } + if (index < peer_num) { + *R = strdup (Peers[index]->print_name); + return index; } else { - message_add_use (M); - message_add_peer (M); - message_tree = tree_insert_message (message_tree, M, lrand48 ()); - return M; + return -1; } } -struct message *tglf_fetch_alloc_encrypted_message (void) { - int data[3]; - prefetch_data (data, 12); - struct message *M = message_get (*(long long *)(data + 1)); - - if (!M) { - M = talloc0 (sizeof (*M)); - M->id = *(long long *)(data + 1); - message_insert_tree (M); - messages_allocated ++; - assert (message_get (M->id) == M); +int tgl_complete_chat_list (int index, const char *text, int len, char **R) { + index ++; + while (index < peer_num && (!Peers[index]->print_name || strncmp (Peers[index]->print_name, text, len) || tgl_get_peer_type (Peers[index]->id) != TGL_PEER_CHAT)) { + index ++; } - tglf_fetch_encrypted_message (M); - return M; -} - -struct message *tglf_fetch_alloc_message_short (void) { - int data[1]; - prefetch_data (data, 4); - struct message *M = message_get (data[0]); - - if (!M) { - M = talloc0 (sizeof (*M)); - M->id = data[0]; - message_insert_tree (M); - messages_allocated ++; - } - tglf_fetch_message_short (M); - return M; -} - -struct message *tglf_fetch_alloc_message_short_chat (void) { - int data[1]; - prefetch_data (data, 4); - struct message *M = message_get (data[0]); - - if (!M) { - M = talloc0 (sizeof (*M)); - M->id = data[0]; - message_insert_tree (M); - messages_allocated ++; - } - tglf_fetch_message_short_chat (M); - return M; -} - -struct chat *tglf_fetch_alloc_chat (void) { - int data[2]; - prefetch_data (data, 8); - peer_t *U = peer_get (MK_CHAT (data[1])); - if (!U) { - chats_allocated ++; - U = talloc0 (sizeof (*U)); - U->id = MK_CHAT (data[1]); - peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); - assert (peer_num < MAX_PEER_NUM); - Peers[peer_num ++] = U; - } - fetch_chat (&U->chat); - return &U->chat; -} - -struct chat *tglf_fetch_alloc_chat_full (void) { - int data[3]; - prefetch_data (data, 12); - peer_t *U = peer_get (MK_CHAT (data[2])); - if (U) { - fetch_chat_full (&U->chat); - return &U->chat; + if (index < peer_num) { + *R = strdup (Peers[index]->print_name); + return index; } else { - chats_allocated ++; - U = talloc0 (sizeof (*U)); - U->id = MK_CHAT (data[2]); - peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); - fetch_chat_full (&U->chat); - assert (peer_num < MAX_PEER_NUM); - Peers[peer_num ++] = U; - return &U->chat; + return -1; } } -void free_chat (struct chat *U) { - if (U->title) { tfree_str (U->title); } - if (U->print_title) { tfree_str (U->print_title); } +int tgl_complete_encr_chat_list (int index, const char *text, int len, char **R) { + index ++; + while (index < peer_num && (!Peers[index]->print_name || strncmp (Peers[index]->print_name, text, len) || tgl_get_peer_type (Peers[index]->id) != TGL_PEER_ENCR_CHAT)) { + index ++; + } + if (index < peer_num) { + *R = strdup (Peers[index]->print_name); + return index; + } else { + return -1; + } +} + +int tgl_complete_peer_list (int index, const char *text, int len, char **R) { + index ++; + while (index < peer_num && (!Peers[index]->print_name || strncmp (Peers[index]->print_name, text, len))) { + index ++; + } + if (index < peer_num) { + *R = strdup (Peers[index]->print_name); + return index; + } else { + return -1; + } } int tgl_print_stat (char *s, int len) { @@ -1616,126 +1735,3 @@ int tgl_print_stat (char *s, int len) { messages_allocated ); } - -peer_t *peer_get (peer_id_t id) { - static peer_t U; - U.id = id; - return tree_lookup_peer (peer_tree, &U); -} - -struct message *message_get (long long id) { - struct message M; - M.id = id; - return tree_lookup_message (message_tree, &M); -} - -void update_message_id (struct message *M, long long id) { - message_tree = tree_delete_message (message_tree, M); - M->id = id; - message_tree = tree_insert_message (message_tree, M, lrand48 ()); -} - -void message_insert_tree (struct message *M) { - assert (M->id); - message_tree = tree_insert_message (message_tree, M, lrand48 ()); -} - -void message_remove_tree (struct message *M) { - assert (M->id); - message_tree = tree_delete_message (message_tree, M); -} - -void message_insert (struct message *M) { - message_add_use (M); - message_add_peer (M); -} - -void message_insert_unsent (struct message *M) { - message_unsent_tree = tree_insert_message (message_unsent_tree, M, lrand48 ()); -} - -void message_remove_unsent (struct message *M) { - message_unsent_tree = tree_delete_message (message_unsent_tree, M); -} - -static void __send_msg (struct message *M) { - vlogprintf (E_NOTICE, "Resending message...\n"); - print_message (M); - - do_send_msg (M); -} - -void send_all_unsent (void ) { - tree_act_message (message_unsent_tree, __send_msg); -} - -void peer_insert_name (peer_t *P) { - peer_by_name_tree = tree_insert_peer_by_name (peer_by_name_tree, P, lrand48 ()); -} - -void peer_delete_name (peer_t *P) { - peer_by_name_tree = tree_delete_peer_by_name (peer_by_name_tree, P); -} - -peer_t *peer_lookup_name (const char *s) { - static peer_t P; - P.print_name = (void *)s; - peer_t *R = tree_lookup_peer_by_name (peer_by_name_tree, &P); - return R; -} - -void peer_iterator_ex (void (*it)(peer_t *P, void *extra), void *extra) { - tree_act_ex_peer (peer_tree, it, extra); -} - -int complete_user_list (int index, const char *text, int len, char **R) { - index ++; - while (index < peer_num && (!Peers[index]->print_name || strncmp (Peers[index]->print_name, text, len) || get_peer_type (Peers[index]->id) != PEER_USER)) { - index ++; - } - if (index < peer_num) { - *R = strdup (Peers[index]->print_name); - return index; - } else { - return -1; - } -} - -int complete_chat_list (int index, const char *text, int len, char **R) { - index ++; - while (index < peer_num && (!Peers[index]->print_name || strncmp (Peers[index]->print_name, text, len) || get_peer_type (Peers[index]->id) != PEER_CHAT)) { - index ++; - } - if (index < peer_num) { - *R = strdup (Peers[index]->print_name); - return index; - } else { - return -1; - } -} - -int complete_encr_chat_list (int index, const char *text, int len, char **R) { - index ++; - while (index < peer_num && (!Peers[index]->print_name || strncmp (Peers[index]->print_name, text, len) || get_peer_type (Peers[index]->id) != PEER_ENCR_CHAT)) { - index ++; - } - if (index < peer_num) { - *R = strdup (Peers[index]->print_name); - return index; - } else { - return -1; - } -} - -int complete_peer_list (int index, const char *text, int len, char **R) { - index ++; - while (index < peer_num && (!Peers[index]->print_name || strncmp (Peers[index]->print_name, text, len))) { - index ++; - } - if (index < peer_num) { - *R = strdup (Peers[index]->print_name); - return index; - } else { - return -1; - } -} diff --git a/structures.h b/structures.h index b1e76d4..18cca06 100644 --- a/structures.h +++ b/structures.h @@ -23,72 +23,31 @@ #include "tgl-layout.h" #include "tgl-fetch.h" -char *create_print_name (peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4); - -struct message *message_alloc (long long id); - -void free_user (struct user *U); -void free_chat (struct chat *U); +char *create_print_name (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4); -int tgl_print_stat (char *s, int len); -peer_t *peer_get (peer_id_t id); -peer_t *peer_lookup_name (const char *s); -struct message *message_get (long long id); +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); +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); - -void message_insert_tree (struct message *M); -void update_message_id (struct message *M, long long id); -void message_insert (struct message *M); -void free_photo (struct photo *P); -void insert_encrypted_chat (peer_t *P); -void insert_user (peer_t *P); -void insert_chat (peer_t *P); -void message_insert_unsent (struct message *M); -void message_remove_unsent (struct message *M); -void send_all_unsent (void); -void message_remove_tree (struct message *M); -void message_add_peer (struct message *M); -void message_del_peer (struct message *M); -void free_message (struct message *M); -void message_del_use (struct message *M); -void peer_insert_name (peer_t *P); -void peer_delete_name (peer_t *P); -void peer_iterator_ex (void (*it)(peer_t *P, void *extra), void *extra); - -int complete_user_list (int index, const char *text, int len, char **R); -int complete_chat_list (int index, const char *text, int len, char **R); -int complete_encr_chat_list (int index, const char *text, int len, char **R); -int complete_peer_list (int index, const char *text, int len, char **R); -#define PEER_USER 1 -#define PEER_CHAT 2 -#define PEER_GEO_CHAT 3 -#define PEER_ENCR_CHAT 4 -#define PEER_UNKNOWN 0 - -#define MK_USER(id) set_peer_id (PEER_USER,id) -#define MK_CHAT(id) set_peer_id (PEER_CHAT,id) -#define MK_GEO_CHAT(id) set_peer_id (PEER_GEO_CHAT,id) -#define MK_ENCR_CHAT(id) set_peer_id (PEER_ENCR_CHAT,id) - -static inline int get_peer_type (peer_id_t id) { - return id.type; -} - -static inline int get_peer_id (peer_id_t id) { - return id.id; -} - -static inline peer_id_t set_peer_id (int type, int id) { - peer_id_t ID; - ID.id = id; - ID.type = type; - return ID; -} - -static inline int cmp_peer_id (peer_id_t a, peer_id_t b) { - return memcmp (&a, &b, sizeof (a)); -} +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); #endif diff --git a/telegram.h b/telegram.h index f8645c6..5f78cd4 100644 --- a/telegram.h +++ b/telegram.h @@ -16,8 +16,7 @@ Copyright Vitaly Valtman 2013 */ -#define MAX_DC_NUM 9 -#define MAX_PEER_NUM 100000 +#define TGL_MAX_PEER_NUM 100000 #ifndef PROG_NAME #define PROG_NAME "telegram-cli" diff --git a/tgl-fetch.h b/tgl-fetch.h index 9d42f45..18e8ed1 100644 --- a/tgl-fetch.h +++ b/tgl-fetch.h @@ -1,25 +1,34 @@ #ifndef __TGL_FETCH_H__ #define __TGL_FETCH_H__ -int tglf_fetch_file_location (struct file_location *loc); -int tglf_fetch_user_status (struct user_status *S); -int tglf_fetch_user (struct user *U); -struct user *tglf_fetch_alloc_user (void); -struct user *tglf_fetch_alloc_user_full (void); -struct chat *tglf_fetch_alloc_chat (void); -struct chat *tglf_fetch_alloc_chat_full (void); -struct secret_chat *tglf_fetch_alloc_encrypted_chat (void); -struct message *tglf_fetch_alloc_message (void); -struct message *tglf_fetch_alloc_geo_message (void); -struct message *tglf_fetch_alloc_message_short (void); -struct message *tglf_fetch_alloc_message_short_chat (void); -struct message *tglf_fetch_alloc_encrypted_message (void); -void tglf_fetch_encrypted_message_file (struct message_media *M); -peer_id_t tglf_fetch_peer_id (void); +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); -void tglf_fetch_message_media (struct message_media *M); -void tglf_fetch_message_media_encrypted (struct message_media *M); -void tglf_fetch_message_action (struct message_action *M); -void tglf_fetch_message_action_encrypted (struct message_action *M); -void tglf_fetch_photo (struct photo *P); +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_chat (struct tgl_chat *C); +void tglf_fetch_chat_full (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); #endif diff --git a/tgl-layout.h b/tgl-layout.h index faee2a4..71bf3c0 100644 --- a/tgl-layout.h +++ b/tgl-layout.h @@ -20,41 +20,41 @@ #pragma pack(push,4) -typedef struct { int type; int id; } peer_id_t; +typedef struct { int type; int id; } tgl_peer_id_t; -struct file_location { +struct tgl_file_location { int dc; long long volume; int local_id; long long secret; }; -struct photo_size { +struct tgl_photo_size { char *type; - struct file_location loc; + struct tgl_file_location loc; int w; int h; int size; char *data; }; -struct geo { +struct tgl_geo { double longitude; double latitude; }; -struct photo { +struct tgl_photo { long long id; long long access_hash; int user_id; int date; char *caption; - struct geo geo; + struct tgl_geo geo; int sizes_num; - struct photo_size *sizes; + struct tgl_photo_size *sizes; }; -struct encr_photo { +struct tgl_encr_photo { long long id; long long access_hash; int dc_id; @@ -67,7 +67,7 @@ struct encr_photo { int h; }; -struct encr_video { +struct tgl_encr_video { long long id; long long access_hash; int dc_id; @@ -82,7 +82,7 @@ struct encr_video { char *mime_type; }; -struct encr_audio { +struct tgl_encr_audio { long long id; long long access_hash; int dc_id; @@ -95,7 +95,7 @@ struct encr_audio { char *mime_type; }; -struct encr_document { +struct tgl_encr_document { long long id; long long access_hash; int dc_id; @@ -108,64 +108,64 @@ struct encr_document { char *mime_type; }; -struct encr_file { +struct tgl_encr_file { char *filename; unsigned char *key; unsigned char *iv; }; -struct user_status { +struct tgl_user_status { int online; int when; }; -struct user { - peer_id_t id; +struct tgl_user { + tgl_peer_id_t id; int flags; - struct message *last; + struct tgl_message *last; char *print_name; int structure_version; - struct file_location photo_big; - struct file_location photo_small; + struct tgl_file_location photo_big; + struct tgl_file_location photo_small; long long photo_id; - struct photo photo; + struct tgl_photo photo; char *first_name; char *last_name; char *phone; long long access_hash; - struct user_status status; + struct tgl_user_status status; int blocked; char *real_first_name; char *real_last_name; }; -struct chat_user { +struct tgl_chat_user { int user_id; int inviter_id; int date; }; -struct chat { - peer_id_t id; +struct tgl_chat { + tgl_peer_id_t id; int flags; - struct message *last; + struct tgl_message *last; char *print_title; int structure_version; - struct file_location photo_big; - struct file_location photo_small; - struct photo photo; + struct tgl_file_location photo_big; + struct tgl_file_location photo_small; + struct tgl_photo photo; char *title; int users_num; int user_list_size; int user_list_version; - struct chat_user *user_list; + struct tgl_chat_user *user_list; int date; int version; int admin_id; }; -enum secret_chat_state { +enum tgl_secret_chat_state { sc_none, sc_waiting, sc_request, @@ -173,15 +173,15 @@ enum secret_chat_state { sc_deleted }; -struct secret_chat { - peer_id_t id; +struct tgl_secret_chat { + tgl_peer_id_t id; int flags; - struct message *last; + struct tgl_message *last; char *print_name; int structure_version; - struct file_location photo_big; - struct file_location photo_small; - struct photo photo; + struct tgl_file_location photo_big; + struct tgl_file_location photo_small; + struct tgl_photo photo; int user_id; int admin_id; int date; @@ -191,35 +191,35 @@ struct secret_chat { unsigned char *g_key; unsigned char *nonce; - enum secret_chat_state state; + enum tgl_secret_chat_state state; int key[64]; long long key_fingerprint; }; -typedef union peer { +typedef union tgl_peer { struct { - peer_id_t id; + tgl_peer_id_t id; int flags; - struct message *last; + struct tgl_message *last; char *print_name; int structure_version; - struct file_location photo_big; - struct file_location photo_small; - struct photo photo; + struct tgl_file_location photo_big; + struct tgl_file_location photo_small; + struct tgl_photo photo; }; - struct user user; - struct chat chat; - struct secret_chat encr_chat; -} peer_t; + struct tgl_user user; + struct tgl_chat chat; + struct tgl_secret_chat encr_chat; +} tgl_peer_t; -struct video { +struct tgl_video { long long id; long long access_hash; int user_id; int date; int size; int dc_id; - struct photo_size thumb; + struct tgl_photo_size thumb; char *caption; int duration; int w; @@ -227,7 +227,7 @@ struct video { char *mime_type; }; -struct audio { +struct tgl_audio { long long id; long long access_hash; int user_id; @@ -238,19 +238,19 @@ struct audio { char *mime_type; }; -struct document { +struct tgl_document { long long id; long long access_hash; int user_id; int date; int size; int dc_id; - struct photo_size thumb; + struct tgl_photo_size thumb; char *caption; char *mime_type; }; -struct message_action { +struct tgl_message_action { unsigned type; union { struct { @@ -259,7 +259,7 @@ struct message_action { int *users; }; char *new_title; - struct photo photo; + struct tgl_photo photo; int user; int ttl; int layer; @@ -269,25 +269,25 @@ struct message_action { }; }; -struct message_media { +struct tgl_message_media { unsigned type; union { - struct photo photo; - struct video video; - struct audio audio; - struct document document; - struct geo geo; + struct tgl_photo photo; + struct tgl_video video; + struct tgl_audio audio; + struct tgl_document document; + struct tgl_geo geo; struct { char *phone; char *first_name; char *last_name; int user_id; }; - struct encr_photo encr_photo; - struct encr_video encr_video; - struct encr_audio encr_audio; - struct encr_document encr_document; - struct encr_file encr_file; + struct tgl_encr_photo encr_photo; + struct tgl_encr_video encr_video; + struct tgl_encr_audio encr_audio; + struct tgl_encr_document encr_document; + struct tgl_encr_file encr_file; struct { void *data; int data_size; @@ -295,25 +295,25 @@ struct message_media { }; }; -struct message { - struct message *next_use, *prev_use; - struct message *next, *prev; +struct tgl_message { + struct tgl_message *next_use, *prev_use; + struct tgl_message *next, *prev; long long id; int flags; - peer_id_t fwd_from_id; + tgl_peer_id_t fwd_from_id; int fwd_date; - peer_id_t from_id; - peer_id_t to_id; + tgl_peer_id_t from_id; + tgl_peer_id_t to_id; int out; int unread; int date; int service; union { - struct message_action action; + struct tgl_message_action action; struct { char *message; int message_len; - struct message_media media; + struct tgl_message_media media; }; }; }; diff --git a/tgl.c b/tgl.c index e8a5c7c..6412a21 100644 --- a/tgl.c +++ b/tgl.c @@ -3,6 +3,5 @@ #endif #include "tgl.h" -struct tgl_params tgl_params; struct tgl_state tgl_state; diff --git a/tgl.h b/tgl.h index 29cd91f..e821493 100644 --- a/tgl.h +++ b/tgl.h @@ -1,17 +1,140 @@ #ifndef __TGL_H__ #define __TGL_H__ +#include +#include + +#define TGL_MAX_DC_NUM 100 + // Do not modify this structure, unless you know what you do struct tgl_state { int our_id; // ID of logged in user + int encr_root; + unsigned char *encr_prime; + int encr_param_version; + int pts; + int qts; + int date; + int seq; + int binlog_enabled; + int test_mode; + int verbosity; + int unread_messages; + + struct dc *DC_list[TGL_MAX_DC_NUM]; + struct dc *DC_working; + int dc_working_num; + + long long cur_uploading_bytes; + long long cur_uploaded_bytes; + long long cur_downloading_bytes; + long long cur_downloaded_bytes; }; extern struct tgl_state tgl_state; -// Should be set before first use of lib -struct tgl_params { - int test_mode; // Connect to test DC - int verbosity; // May be modified any moment -}; -extern struct tgl_params tgl_params; +void tgl_reopen_binlog_for_writing (void); +void tgl_replay_log (void); +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); + +struct tgl_message *tgl_message_get (long long id); +void tgl_peer_iterator_ex (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); + +#define TGL_PEER_USER 1 +#define TGL_PEER_CHAT 2 +#define TGL_PEER_GEO_CHAT 3 +#define TGL_PEER_ENCR_CHAT 4 +#define TGL_PEER_UNKNOWN 0 + +#define TGL_MK_USER(id) tgl_set_peer_id (TGL_PEER_USER,id) +#define TGL_MK_CHAT(id) tgl_set_peer_id (TGL_PEER_CHAT,id) +#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) + +static inline int tgl_get_peer_type (tgl_peer_id_t id) { + return id.type; +} + +static inline int tgl_get_peer_id (tgl_peer_id_t id) { + return id.id; +} + +static inline tgl_peer_id_t tgl_set_peer_id (int type, int id) { + tgl_peer_id_t ID; + ID.id = id; + ID.type = type; + return ID; +} + +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_set_verbosity (int val) { + tgl_state.verbosity = val; +} + +static inline void tgl_set_test_mode (void) { + tgl_state.test_mode ++; +} + +void tgl_do_send_code (const char *user); +void tgl_do_phone_call (const char *user); +int tgl_do_send_code_result (const char *code); +void tgl_do_update_contact_list (void); +void tgl_do_send_message (tgl_peer_id_t id, const char *msg, int len); +void tgl_do_send_text (tgl_peer_id_t id, char *file); +void tgl_do_get_history (tgl_peer_id_t id, int limit); +void tgl_do_get_dialog_list (void); +void tgl_do_get_dialog_list_ex (void); +void tgl_do_send_photo (int type, tgl_peer_id_t to_id, char *file_name); +void tgl_do_get_chat_info (tgl_peer_id_t id); +void tgl_do_get_user_list_info_silent (int num, int *list); +void tgl_do_get_user_info (tgl_peer_id_t id); +void tgl_do_forward_message (tgl_peer_id_t id, int n); +void tgl_do_rename_chat (tgl_peer_id_t id, char *name); +void tgl_do_load_encr_video (struct tgl_encr_video *V, int next); +void tgl_do_create_encr_chat_request (int user_id); +void tgl_do_create_secret_chat (tgl_peer_id_t id); +void tgl_do_create_group_chat (tgl_peer_id_t id, char *chat_topic); +void tgl_do_get_suggested (void); + +void tgl_do_load_photo (struct tgl_photo *photo, int next); +void tgl_do_load_video_thumb (struct tgl_video *video, int next); +void tgl_do_load_audio (struct tgl_video *V, int next); +void tgl_do_load_video (struct tgl_video *V, int next); +void tgl_do_load_document (struct tgl_document *V, int next); +void tgl_do_load_document_thumb (struct tgl_document *video, int next); +void tgl_do_help_get_config (void); +int tgl_do_auth_check_phone (const char *user); +int tgl_do_get_nearest_dc (void); +int tgl_do_send_code_result_auth (const char *code, const char *first_name, const char *last_name); +void tgl_do_import_auth (int num); +void tgl_do_export_auth (int num); +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 tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, const char *s); +void tgl_do_accept_encr_chat_request (struct tgl_secret_chat *E); +void tgl_do_get_difference (void); +void tgl_do_mark_read (tgl_peer_id_t id); +void tgl_do_visualize_key (tgl_peer_id_t id); +void tgl_do_create_keys_end (struct tgl_secret_chat *U); +void tgl_do_add_user_to_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, int limit); +void tgl_do_del_user_from_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id); +void tgl_do_update_status (int online); +void tgl_do_contacts_search (int limit, const char *s); +void tgl_do_send_msg (struct tgl_message *M); +void tgl_do_delete_msg (long long id); +void tgl_do_restore_msg (long long id); +void tgl_do_send_encr_chat_layer (struct tgl_secret_chat *E); #endif