diff --git a/binlog.c b/binlog.c index 6842fe1..f0144b9 100644 --- a/binlog.c +++ b/binlog.c @@ -21,9 +21,6 @@ # include "config.h" #endif -#ifdef USE_LUA -# include "lua-tg.h" -#endif #include #include #include @@ -107,9 +104,9 @@ static int fetch_comb_binlog_default_dc (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); - #endif + if (tgl_state.callback.our_id) { + tgl_state.callback.our_id (tgl_state.our_id); + } return 0; } @@ -194,9 +191,6 @@ static int fetch_comb_binlog_user_add (void *extra) { U->flags |= FLAG_USER_CONTACT; } - #ifdef USE_LUA - lua_user_update (U, TGL_UPDATE_CREATED); - #endif if (tgl_state.callback.user_update) { tgl_state.callback.user_update (U, TGL_UPDATE_CREATED); } @@ -209,9 +203,6 @@ static int fetch_comb_binlog_user_delete (void *extra) { assert (U); U->flags |= FLAG_DELETED; - #ifdef USE_LUA - lua_user_update (&U->user, TGL_UPDATE_DELETED); - #endif if (tgl_state.callback.user_update) { tgl_state.callback.user_update ((void *)U, TGL_UPDATE_DELETED); } @@ -223,6 +214,9 @@ static int fetch_comb_binlog_user_set_access_hash (void *extra) { tgl_peer_t *U = tgl_peer_get (id); assert (U); U->user.access_hash = fetch_long (); + if (tgl_state.callback.user_update) { + tgl_state.callback.user_update ((void *)U, TGL_UPDATE_ACCESS_HASH); + } return 0; } @@ -235,9 +229,6 @@ static int fetch_comb_binlog_user_set_phone (void *extra) { } U->user.phone = fetch_str_dup (); - #ifdef USE_LUA - lua_user_update (&U->user, TGL_UPDATE_PHONE); - #endif if (tgl_state.callback.user_update) { tgl_state.callback.user_update ((void *)U, TGL_UPDATE_PHONE); @@ -256,9 +247,6 @@ static int fetch_comb_binlog_user_set_friend (void *extra) { if (friend) { U->flags |= FLAG_USER_CONTACT; } else { U->flags &= ~FLAG_USER_CONTACT; } - #ifdef USE_LUA - lua_user_update (&U->user, TGL_UPDATE_CONTACT); - #endif if (tgl_state.callback.user_update) { tgl_state.callback.user_update ((void *)U, TGL_UPDATE_CONTACT); } @@ -274,9 +262,6 @@ static int fetch_comb_binlog_user_set_full_photo (void *extra) { } tglf_fetch_photo (&U->user.photo); - #ifdef USE_LUA - lua_user_update (&U->user, TGL_UPDATE_PHOTO); - #endif if (tgl_state.callback.user_update) { tgl_state.callback.user_update ((void *)U, TGL_UPDATE_PHOTO); } @@ -290,9 +275,6 @@ static int fetch_comb_binlog_user_set_blocked (void *extra) { U->user.blocked = fetch_int (); - #ifdef USE_LUA - lua_user_update (&U->user, TGL_UPDATE_BLOCKED); - #endif if (tgl_state.callback.user_update) { tgl_state.callback.user_update ((void *)U, TGL_UPDATE_BLOCKED); } @@ -303,15 +285,13 @@ 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); + assert (U->flags & FLAG_CREATED); if (U->user.real_first_name) { tfree_str (U->user.real_first_name); } if (U->user.real_last_name) { tfree_str (U->user.real_last_name); } U->user.real_first_name = fetch_str_dup (); U->user.real_last_name = fetch_str_dup (); - #ifdef USE_LUA - lua_user_update (&U->user, TGL_UPDATE_REAL_NAME); - #endif if (tgl_state.callback.user_update) { tgl_state.callback.user_update ((void *)U, TGL_UPDATE_REAL_NAME); } @@ -334,9 +314,6 @@ static int fetch_comb_binlog_user_set_name (void *extra) { U->print_name = create_print_name (U->id, U->user.first_name, U->user.last_name, 0, 0); tglp_peer_insert_name ((void *)U); - #ifdef USE_LUA - lua_user_update (&U->user, TGL_UPDATE_NAME); - #endif if (tgl_state.callback.user_update) { tgl_state.callback.user_update ((void *)U, TGL_UPDATE_NAME); } @@ -361,9 +338,6 @@ static int fetch_comb_binlog_user_set_photo (void *extra) { tglf_fetch_file_location (&U->user.photo_big); } - #ifdef USE_LUA - lua_user_update (&U->user); - #endif if (tgl_state.callback.user_update) { tgl_state.callback.user_update ((void *)U, TGL_UPDATE_PHOTO); } @@ -387,9 +361,6 @@ static int fetch_comb_binlog_encr_chat_delete (void *extra) { U->g_key = 0; } - #ifdef USE_LUA - lua_secret_chat_update (U, TGL_UPDATE_DELETED); - #endif if (tgl_state.callback.secret_chat_update) { tgl_state.callback.secret_chat_update (U, TGL_UPDATE_DELETED); } @@ -430,9 +401,6 @@ static int fetch_comb_binlog_encr_chat_requested (void *extra) { U->flags |= FLAG_CREATED; U->state = sc_request; - #ifdef USE_LUA - lua_secret_chat_update (U, TGL_UPDATE_REQUESTED); - #endif if (tgl_state.callback.secret_chat_update) { tgl_state.callback.secret_chat_update (U, TGL_UPDATE_REQUESTED); } @@ -444,6 +412,9 @@ static int fetch_comb_binlog_encr_chat_set_access_hash (void *extra) { tgl_peer_t *U = tgl_peer_get (id); assert (U); U->encr_chat.access_hash = fetch_long (); + if (tgl_state.callback.secret_chat_update) { + tgl_state.callback.secret_chat_update ((void *)U, TGL_UPDATE_ACCESS_HASH); + } return 0; } @@ -484,9 +455,6 @@ static int fetch_comb_binlog_encr_chat_accepted (void *extra) { } U->state = sc_ok; - #ifdef USE_LUA - lua_secret_chat_update (U, TGL_UPDATE_WORKING); - #endif if (tgl_state.callback.secret_chat_update) { tgl_state.callback.secret_chat_update (U, TGL_UPDATE_WORKING); } @@ -520,9 +488,6 @@ static int fetch_comb_binlog_encr_chat_init (void *extra) { fetch_ints (P->encr_chat.g_key, 64); P->flags |= FLAG_CREATED; - #ifdef USE_LUA - lua_secret_chat_update (U, TGL_UPDATE_CREATED); - #endif if (tgl_state.callback.secret_chat_update) { tgl_state.callback.secret_chat_update ((void *)P, TGL_UPDATE_CREATED); } @@ -552,9 +517,6 @@ static int fetch_comb_binlog_chat_create (void *extra) { fetch_data (&C->photo_big, sizeof (struct tgl_file_location)); fetch_data (&C->photo_small, sizeof (struct tgl_file_location)); - #ifdef USE_LUA - lua_chat_update (C, TGL_UPDATE_CREATED); - #endif if (tgl_state.callback.chat_update) { tgl_state.callback.chat_update (C, TGL_UPDATE_CREATED); } @@ -567,9 +529,6 @@ static int fetch_comb_binlog_chat_change_flags (void *extra) { C->chat.flags |= fetch_int (); C->chat.flags &= ~fetch_int (); - #ifdef USE_LUA - lua_chat_update (&C->chat, TGL_UPDATE_FLAGS); - #endif if (tgl_state.callback.chat_update) { tgl_state.callback.chat_update ((void *)C, TGL_UPDATE_FLAGS); } @@ -589,9 +548,6 @@ static int fetch_comb_binlog_chat_set_title (void *extra) { C->print_name = create_print_name (C->id, C->chat.title, 0, 0, 0); tglp_peer_insert_name ((void *)C); - #ifdef USE_LUA - lua_chat_update (&C->chat, TGL_UPDATE_TITLE); - #endif if (tgl_state.callback.chat_update) { tgl_state.callback.chat_update ((void *)C, TGL_UPDATE_TITLE); } @@ -604,9 +560,6 @@ static int fetch_comb_binlog_chat_set_photo (void *extra) { 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, TGL_UPDATE_PHOTO); - #endif if (tgl_state.callback.chat_update) { tgl_state.callback.chat_update ((void *)C, TGL_UPDATE_PHOTO); } @@ -633,9 +586,6 @@ static int fetch_comb_binlog_chat_set_admin (void *extra) { assert (C && (C->flags & FLAG_CREATED)); C->chat.admin_id = fetch_int (); - #ifdef USE_LUA - lua_chat_update (&C->chat, TGL_UPDATE_ADMIN); - #endif if (tgl_state.callback.chat_update) { tgl_state.callback.chat_update ((void *)C, TGL_UPDATE_ADMIN); } @@ -651,9 +601,6 @@ static int fetch_comb_binlog_chat_set_participants (void *extra) { C->chat.user_list = talloc (12 * C->chat.user_list_size); fetch_ints (C->chat.user_list, 3 * C->chat.user_list_size); - #ifdef USE_LUA - lua_chat_update (&C->chat, TGL_UPDATE_MEMBERS); - #endif if (tgl_state.callback.chat_update) { tgl_state.callback.chat_update ((void *)C, TGL_UPDATE_MEMBERS); } @@ -670,9 +617,6 @@ static int fetch_comb_binlog_chat_set_full_photo (void *extra) { } tglf_fetch_photo (&C->chat.photo); - #ifdef USE_LUA - lua_chat_update (&C->chat, TGL_UPDATE_PHOTO); - #endif if (tgl_state.callback.chat_update) { tgl_state.callback.chat_update ((void *)C, TGL_UPDATE_PHOTO); } @@ -703,9 +647,6 @@ static int fetch_comb_binlog_chat_add_participant (void *extra) { C->user_list[C->user_list_size - 1].date = date; C->user_list_version = version; - #ifdef USE_LUA - lua_chat_update (C, TGL_UPDATE_MEMBERS); - #endif if (tgl_state.callback.chat_update) { tgl_state.callback.chat_update (C, TGL_UPDATE_MEMBERS); } @@ -736,9 +677,6 @@ static int fetch_comb_binlog_chat_del_participant (void *extra) { C->user_list = trealloc (C->user_list, 12 * C->user_list_size + 12, 12 * C->user_list_size); C->user_list_version = version; - #ifdef USE_LUA - lua_chat_update (C, TGL_UPDATE_MEMBERS); - #endif if (tgl_state.callback.chat_update) { tgl_state.callback.chat_update (C, TGL_UPDATE_MEMBERS); } @@ -781,10 +719,6 @@ static int fetch_comb_binlog_create_message_text (void *extra) { M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; tglm_message_insert (M); - - #ifdef USE_LUA - lua_new_msg (M); - #endif return 0; } @@ -826,10 +760,6 @@ static int fetch_comb_binlog_send_message_text (void *extra) { tglm_message_insert (M); tglm_message_insert_unsent (M); M->flags |= FLAG_PENDING; - - #ifdef USE_LUA - lua_new_msg (M); - #endif return 0; } @@ -860,10 +790,6 @@ static int fetch_comb_binlog_send_message_action_encr (void *extra) { tglm_message_insert (M); tglm_message_insert_unsent (M); M->flags |= FLAG_PENDING; - - #ifdef USE_LUA - lua_new_msg (M); - #endif return 0; } @@ -907,9 +833,6 @@ static int fetch_comb_binlog_create_message_text_fwd (void *extra) { tglm_message_insert (M); - #ifdef USE_LUA - lua_new_msg (M); - #endif return 0; } @@ -938,9 +861,6 @@ static int fetch_comb_binlog_create_message_media (void *extra) { M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; tglm_message_insert (M); - #ifdef USE_LUA - lua_new_msg (M); - #endif return 0; } @@ -970,9 +890,6 @@ static int fetch_comb_binlog_create_message_media_encr (void *extra) { M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; tglm_message_insert (M); - #ifdef USE_LUA - lua_new_msg (M); - #endif return 0; } @@ -1004,9 +921,6 @@ static int fetch_comb_binlog_create_message_media_fwd (void *extra) { M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; tglm_message_insert (M); - #ifdef USE_LUA - lua_new_msg (M); - #endif return 0; } @@ -1030,9 +944,6 @@ static int fetch_comb_binlog_create_message_service (void *extra) { M->service = 1; tglm_message_insert (M); - #ifdef USE_LUA - lua_new_msg (M); - #endif return 0; } @@ -1064,9 +975,6 @@ static int fetch_comb_binlog_create_message_service_encr (void *extra) { } tglm_message_insert (M); - #ifdef USE_LUA - lua_new_msg (M); - #endif return 0; } @@ -1093,19 +1001,30 @@ static int fetch_comb_binlog_create_message_service_fwd (void *extra) { M->service = 1; tglm_message_insert (M); - #ifdef USE_LUA - lua_new_msg (M); - #endif + return 0; +} + +static int fetch_comb_binlog_message_set_unread_long (void *extra) { + struct tgl_message *M = tgl_message_get (fetch_long ()); + assert (M); + if (M->unread) { + M->unread = 0; + if (tgl_state.callback.marked_read) { + tgl_state.callback.marked_read (1, &M); + } + } return 0; } 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 - lua_update_msg (M); - #endif + if (M->unread) { + M->unread = 0; + if (tgl_state.callback.marked_read) { + tgl_state.callback.marked_read (1, &M); + } + } return 0; } @@ -1114,9 +1033,6 @@ static int fetch_comb_binlog_set_message_sent (void *extra) { assert (M); tglm_message_remove_unsent (M); M->flags &= ~FLAG_PENDING; - #ifdef USE_LUA - lua_update_msg (M); - #endif return 0; } @@ -1137,9 +1053,6 @@ static int fetch_comb_binlog_set_msg_id (void *extra) { tglm_message_insert_tree (M); tglm_message_add_peer (M); } - #ifdef USE_LUA - lua_update_msg (M); - #endif return 0; } @@ -1259,6 +1172,7 @@ static void replay_log_event (void) { FETCH_COMBINATOR_FUNCTION (binlog_create_message_service_encr) FETCH_COMBINATOR_FUNCTION (binlog_create_message_service_fwd) FETCH_COMBINATOR_FUNCTION (binlog_message_set_unread) + FETCH_COMBINATOR_FUNCTION (binlog_message_set_unread_long) FETCH_COMBINATOR_FUNCTION (binlog_set_message_sent) FETCH_COMBINATOR_FUNCTION (binlog_set_msg_id) FETCH_COMBINATOR_FUNCTION (binlog_delete_msg) @@ -1646,6 +1560,7 @@ void bl_do_encr_chat_init (int id, int user_id, unsigned char random[], unsigned } void bl_do_set_pts (int pts) { + if (tgl_state.locks & TGL_LOCK_DIFF) { return; } int *ev = alloc_log_event (8); ev[0] = CODE_binlog_set_pts; ev[1] = pts; @@ -1660,6 +1575,7 @@ void bl_do_set_qts (int qts) { } void bl_do_set_date (int date) { + if (tgl_state.locks & TGL_LOCK_DIFF) { return; } int *ev = alloc_log_event (8); ev[0] = CODE_binlog_set_date; ev[1] = date; @@ -1667,6 +1583,7 @@ void bl_do_set_date (int date) { } void bl_do_set_seq (int seq) { + if (tgl_state.locks & TGL_LOCK_DIFF) { return; } int *ev = alloc_log_event (8); ev[0] = CODE_binlog_set_seq; ev[1] = seq; @@ -1947,7 +1864,16 @@ 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_long (struct tgl_message *M, int unread) { + if (unread || !M->unread) { return; } + clear_packet (); + out_int (CODE_binlog_message_set_unread_long); + out_long (M->id); + add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); +} + void bl_do_set_unread (struct tgl_message *M, int unread) { + if (M->id != (int)M->id) { bl_do_set_unread_long (M, unread); } if (unread || !M->unread) { return; } clear_packet (); out_int (CODE_binlog_message_set_unread); @@ -1980,6 +1906,9 @@ void bl_do_delete_msg (struct tgl_message *M) { } void bl_do_msg_seq_update (long long id) { + if (tgl_state.locks & TGL_LOCK_DIFF) { + return; // We will receive this update in get_difference, that works now + } clear_packet (); out_int (CODE_binlog_msg_seq_update); out_long (id); diff --git a/binlog.tl b/binlog.tl index 4cc334a..a63b018 100644 --- a/binlog.tl +++ b/binlog.tl @@ -61,6 +61,7 @@ binlog.createMessageService id:int from_id:int to_type:int to_id:int date:int ac binlog.createMessageServiceEncr id:long from_id:int to_type:int to_id:int date:int action:DecryptedMessageAction = binlog.Update; binlog.createMessageServiceFwd id:int from_id:int to_type:int to_id:int date:int fwd_from_id:int fwd_date:int action:MessageAction = binlog.Update; binlog.messageSetUnread id:int = binlog.Update; +binlog.messageSetUnreadLong id:long = binlog.Update; binlog.setMessageSent id:long = binlog.Update; binlog.setMsgId old_id:long new_id:int = binlog.Update; binlog.deleteMsg id:long = binlog.Update; diff --git a/interface.c b/interface.c index 3442242..c1c2f4e 100644 --- a/interface.c +++ b/interface.c @@ -46,6 +46,10 @@ //#include "tools.h" //#include "structures.h" +#ifdef USE_LUA +# include "lua-tg.h" +#endif + //#include "mtproto-common.h" #include "tgl.h" @@ -65,6 +69,7 @@ char *default_prompt = "> "; int msg_num_mode; int alert_sound; +extern int binlog_read; int safe_quit; @@ -686,10 +691,39 @@ void interpreter_chat_mode (char *line) { } void mark_read_upd (int num, struct tgl_message *list[]) { + if (!binlog_read) { return; } if (log_level < 1) { return; } + + tgl_peer_id_t to_id = list[0]->from_id; + int ok = 1; + int i; + for (i = 1; i < num; i++) { + if (tgl_cmp_peer_id (to_id, list[i]->to_id)) { + ok = 0; + } + } print_start (); push_color (COLOR_YELLOW); - printf ("%d messages mark read\n", num); + if (!ok) { + printf ("%d messages mark read\n", num); + } else { + printf ("%d messages mark read in ", num); + switch (tgl_get_peer_type (to_id)) { + case TGL_PEER_USER: + printf (" user "); + print_user_name (to_id, tgl_peer_get (to_id)); + break; + case TGL_PEER_CHAT: + printf (" chat "); + print_chat_name (to_id, tgl_peer_get (to_id)); + break; + case TGL_PEER_ENCR_CHAT: + printf (" secret chat "); + print_chat_name (to_id, tgl_peer_get (to_id)); + break; + } + printf ("\n"); + } pop_color (); print_end (); } @@ -720,11 +754,135 @@ void type_in_chat_notification_upd (struct tgl_user *U, struct tgl_chat *C) { void print_message_gw (struct tgl_message *M) { + #ifdef USE_LUA + lua_new_msg (M); + #endif + if (!binlog_read) { return; } print_start (); print_message (M); print_end (); } +void our_id_gw (int id) { + #ifdef USE_LUA + lua_our_id (id); + #endif +} + +void print_peer_updates (int flags) { + if (flags & TGL_UPDATE_PHONE) { + printf (" phone"); + } + if (flags & TGL_UPDATE_CONTACT) { + printf (" contact"); + } + if (flags & TGL_UPDATE_PHOTO) { + printf (" photo"); + } + if (flags & TGL_UPDATE_BLOCKED) { + printf (" blocked"); + } + if (flags & TGL_UPDATE_REAL_NAME) { + printf (" name"); + } + if (flags & TGL_UPDATE_NAME) { + printf (" contact_name"); + } + if (flags & TGL_UPDATE_REQUESTED) { + printf (" status"); + } + if (flags & TGL_UPDATE_WORKING) { + printf (" status"); + } + if (flags & TGL_UPDATE_FLAGS) { + printf (" flags"); + } + if (flags & TGL_UPDATE_TITLE) { + printf (" title"); + } + if (flags & TGL_UPDATE_ADMIN) { + printf (" admin"); + } + if (flags & TGL_UPDATE_MEMBERS) { + printf (" members"); + } + if (flags & TGL_UPDATE_ACCESS_HASH) { + printf (" access_hash"); + } +} + +void user_update_gw (struct tgl_user *U, unsigned flags) { + #ifdef USE_LUA + lua_user_update (U, flags); + #endif + + if (!binlog_read) { return; } + + if (!(flags & TGL_UPDATE_CREATED)) { + print_start (); + push_color (COLOR_YELLOW); + printf ("User "); + print_user_name (U->id, (void *)U); + if (!(flags & TGL_UPDATE_DELETED)) { + printf (" updated"); + print_peer_updates (flags); + } else { + printf (" deleted"); + } + printf ("\n"); + pop_color (); + print_end (); + } +} + +void chat_update_gw (struct tgl_chat *U, unsigned flags) { + #ifdef USE_LUA + lua_chat_update (U, flags); + #endif + + if (!binlog_read) { return; } + + if (!(flags & TGL_UPDATE_CREATED)) { + print_start (); + push_color (COLOR_YELLOW); + printf ("Chat "); + print_chat_name (U->id, (void *)U); + if (!(flags & TGL_UPDATE_DELETED)) { + printf (" updated"); + print_peer_updates (flags); + } else { + printf (" deleted"); + } + printf ("\n"); + pop_color (); + print_end (); + } +} + +void secret_chat_update_gw (struct tgl_secret_chat *U, unsigned flags) { + #ifdef USE_LUA + lua_secret_chat_update (U, flags); + #endif + + if (!binlog_read) { return; } + + if (!(flags & TGL_UPDATE_CREATED)) { + print_start (); + push_color (COLOR_YELLOW); + printf ("Secret chat "); + print_encr_chat_name (U->id, (void *)U); + if (!(flags & TGL_UPDATE_DELETED)) { + printf (" updated"); + print_peer_updates (flags); + } else { + printf (" deleted"); + } + printf ("\n"); + pop_color (); + print_end (); + } +} + struct tgl_update_callback upd_cb = { .new_msg = print_message_gw, .marked_read = mark_read_upd, @@ -736,10 +894,11 @@ struct tgl_update_callback upd_cb = { .user_registered = 0, .user_activated = 0, .new_authorization = 0, - .user_update = 0, - .chat_update = 0, - .secret_chat_update = 0, - .msg_receive = print_message_gw + .user_update = user_update_gw, + .chat_update = chat_update_gw, + .secret_chat_update = secret_chat_update_gw, + .msg_receive = print_message_gw, + .our_id = our_id_gw }; diff --git a/loop.c b/loop.c index ae865ce..cfe71b9 100644 --- a/loop.c +++ b/loop.c @@ -53,6 +53,7 @@ int verbosity; +int binlog_read; extern char *default_username; extern char *auth_token; void set_default_username (const char *s); @@ -238,6 +239,8 @@ int loop (void) { tgl_replay_log (); logprintf ("replay log end in %lf seconds\n", tglt_get_double_time () - t); tgl_reopen_binlog_for_writing (); + binlog_read = 1; + //exit (0); #ifdef USE_LUA lua_binlog_end (); #endif diff --git a/lua-tg.c b/lua-tg.c index 77971a5..17abfc3 100644 --- a/lua-tg.c +++ b/lua-tg.c @@ -16,10 +16,12 @@ #include lua_State *luaState; -#include "interface.h" -#include "auto/constants.h" +//#include "interface.h" +//#include "auto/constants.h" #include "tgl.h" +#include "interface.h" +#include extern int verbosity; static int have_file; @@ -37,6 +39,14 @@ void lua_add_string_field (const char *name, const char *value) { lua_settable (luaState, -3); } +void lua_add_string_field_arr (int num, const char *value) { + if (!value || !strlen (value)) { return; } + my_lua_checkstack (luaState, 3); + lua_pushnumber (luaState, num); + lua_pushstring (luaState, value); + lua_settable (luaState, -3); +} + void lua_add_num_field (const char *name, double value) { assert (name && strlen (name)); my_lua_checkstack (luaState, 3); @@ -68,6 +78,9 @@ void push_user (tgl_peer_t *P UU) { lua_add_string_field ("real_first_name", P->user.real_first_name); lua_add_string_field ("real_last_name", P->user.real_last_name); lua_add_string_field ("phone", P->user.phone); + if (P->user.access_hash) { + lua_add_num_field ("access_hash", 1); + } } void push_chat (tgl_peer_t *P) { @@ -84,6 +97,60 @@ void push_encr_chat (tgl_peer_t *P) { lua_settable (luaState, -3); } +void push_update_types (unsigned flags) { + my_lua_checkstack (luaState, 4); + lua_newtable (luaState); + int cc = 0; + + + if (flags & TGL_UPDATE_CREATED) { + lua_add_string_field_arr (cc++, "created"); + } + if (flags & TGL_UPDATE_DELETED) { + lua_add_string_field_arr (cc++, "deleted"); + } + if (flags & TGL_UPDATE_PHONE) { + lua_add_string_field_arr (cc++, "phone"); + } + if (flags & TGL_UPDATE_CONTACT) { + lua_add_string_field_arr (cc++, "contact"); + } + if (flags & TGL_UPDATE_PHOTO) { + lua_add_string_field_arr (cc++, "photo"); + } + if (flags & TGL_UPDATE_BLOCKED) { + lua_add_string_field_arr (cc++, "blocked"); + } + if (flags & TGL_UPDATE_REAL_NAME) { + lua_add_string_field_arr (cc++, "real_name"); + } + if (flags & TGL_UPDATE_NAME) { + lua_add_string_field_arr (cc++, "name"); + } + if (flags & TGL_UPDATE_REQUESTED) { + lua_add_string_field_arr (cc++, "requested"); + } + if (flags & TGL_UPDATE_WORKING) { + lua_add_string_field_arr (cc++, "working"); + } + if (flags & TGL_UPDATE_FLAGS) { + lua_add_string_field_arr (cc++, "flags"); + } + if (flags & TGL_UPDATE_TITLE) { + lua_add_string_field_arr (cc++, "title"); + } + if (flags & TGL_UPDATE_ADMIN) { + lua_add_string_field_arr (cc++, "admin"); + } + if (flags & TGL_UPDATE_MEMBERS) { + lua_add_string_field_arr (cc++, "members"); + } + if (flags & TGL_UPDATE_ACCESS_HASH) { + lua_add_string_field_arr (cc++, "access_hash"); + } + +} + void push_peer (tgl_peer_id_t id, tgl_peer_t *P) { lua_newtable (luaState); @@ -137,32 +204,30 @@ void push_media (struct tgl_message_media *M) { my_lua_checkstack (luaState, 4); switch (M->type) { - case CODE_message_media_photo: - case CODE_decrypted_message_media_photo: + case tgl_message_media_photo: + case tgl_message_media_photo_encr: lua_pushstring (luaState, "photo"); break; - case CODE_message_media_video: - case CODE_decrypted_message_media_video: - lua_pushstring (luaState, "video"); + case tgl_message_media_video: + case tgl_message_media_video_encr: break; - case CODE_message_media_audio: - case CODE_decrypted_message_media_audio: + case tgl_message_media_audio: + case tgl_message_media_audio_encr: lua_pushstring (luaState, "audio"); break; - case CODE_message_media_document: - case CODE_decrypted_message_media_document: + case tgl_message_media_document: + case tgl_message_media_document_encr: lua_pushstring (luaState, "document"); break; - case CODE_message_media_unsupported: + case tgl_message_media_unsupported: lua_pushstring (luaState, "unsupported"); break; - case CODE_message_media_geo: + case tgl_message_media_geo: lua_newtable (luaState); lua_add_num_field ("longitude", M->geo.longitude); lua_add_num_field ("latitude", M->geo.latitude); break; - case CODE_message_media_contact: - case CODE_decrypted_message_media_contact: + case tgl_message_media_contact: lua_newtable (luaState); lua_add_string_field ("phone", M->phone); lua_add_string_field ("first_name", M->first_name); @@ -222,7 +287,7 @@ void push_message (struct tgl_message *M) { lua_pushlstring (luaState, M->message, M->message_len); lua_settable (luaState, -3); } - if (M->media.type && M->media.type != CODE_message_media_empty && M->media.type != CODE_decrypted_message_media_empty) { + if (M->media.type && M->media.type != tgl_message_media_none) { lua_pushstring (luaState, "media"); push_media (&M->media); lua_settable (luaState, -3); @@ -288,53 +353,56 @@ void lua_new_msg (struct tgl_message *M UU) { } } -void lua_secret_chat_created (struct tgl_secret_chat *C) { +void lua_secret_chat_update (struct tgl_secret_chat *C, unsigned flags) { if (!have_file) { return; } lua_settop (luaState, 0); //lua_checkstack (luaState, 20); my_lua_checkstack (luaState, 20); - lua_getglobal (luaState, "on_secret_chat_created"); + lua_getglobal (luaState, "on_secret_chat_update"); push_peer (C->id, (void *)C); - assert (lua_gettop (luaState) == 2); + push_update_types (flags); + assert (lua_gettop (luaState) == 3); - int r = lua_pcall (luaState, 1, 0, 0); + int r = lua_pcall (luaState, 2, 0, 0); if (r) { logprintf ("lua: %s\n", lua_tostring (luaState, -1)); } } -void lua_user_update (struct tgl_user *U) { +void lua_user_update (struct tgl_user *U, unsigned flags) { if (!have_file) { return; } lua_settop (luaState, 0); //lua_checkstack (luaState, 20); my_lua_checkstack (luaState, 20); lua_getglobal (luaState, "on_user_update"); push_peer (U->id, (void *)U); - assert (lua_gettop (luaState) == 2); + push_update_types (flags); + assert (lua_gettop (luaState) == 3); - int r = lua_pcall (luaState, 1, 0, 0); + int r = lua_pcall (luaState, 2, 0, 0); if (r) { logprintf ("lua: %s\n", lua_tostring (luaState, -1)); } } -void lua_chat_update (struct tgl_chat *C) { +void lua_chat_update (struct tgl_chat *C, unsigned flags) { if (!have_file) { return; } lua_settop (luaState, 0); //lua_checkstack (luaState, 20); my_lua_checkstack (luaState, 20); lua_getglobal (luaState, "on_chat_update"); push_peer (C->id, (void *)C); - assert (lua_gettop (luaState) == 2); + push_update_types (flags); + assert (lua_gettop (luaState) == 3); - int r = lua_pcall (luaState, 1, 0, 0); + int r = lua_pcall (luaState, 2, 0, 0); if (r) { logprintf ("lua: %s\n", lua_tostring (luaState, -1)); } } -extern tgl_peer_t *Peers[]; -extern int peer_num; +//extern tgl_peer_t *Peers[]; +//extern int peer_num; #define MAX_LUA_COMMANDS 1000 void *lua_ptr[MAX_LUA_COMMANDS]; @@ -511,7 +579,7 @@ static int postpone_from_lua (lua_State *L) { t[1] = a2; *(void **)(t + 2) = ev; - struct timeval ts= { timeout, 0}; + struct timeval ts= { timeout, 0}; event_add (ev, &ts); lua_pushboolean (L, 1); diff --git a/lua-tg.h b/lua-tg.h index fbc302b..353ceef 100644 --- a/lua-tg.h +++ b/lua-tg.h @@ -2,17 +2,16 @@ #define __LUA_TG_H__ #include -#include "structures.h" +#include "lua-tg.h" +#include "tgl.h" void lua_init (const char *file); void lua_new_msg (struct tgl_message *M); void lua_our_id (int id); -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_secret_chat_update (struct tgl_secret_chat *U, unsigned flags); +void lua_user_update (struct tgl_user *U, unsigned flags); +void lua_chat_update (struct tgl_chat *C, unsigned flags); void lua_binlog_end (void); void lua_diff_end (void); void lua_do_all (void); -#define lua_secret_chat_update(x) -#define lua_update_msg(x) #endif diff --git a/main.c b/main.c index 025ff77..308421c 100644 --- a/main.c +++ b/main.c @@ -91,6 +91,7 @@ int binlog_enabled; extern int log_level; int sync_from_start; int allow_weak_random; +char *lua_file; void set_default_username (const char *s) { if (default_username) { @@ -271,7 +272,7 @@ void parse_config_val (config_t *conf, char **s, char *param_name, const char *d if (path) { tasprintf (s, "%s/%s", path, default_name); } else { - *s = tstrdup (default_name); + *s = default_name ? tstrdup (default_name) : 0; } } } @@ -322,6 +323,10 @@ void parse_config (void) { parse_config_val (&conf, &downloads_directory, "downloads", DOWNLOADS_DIRECTORY, config_directory); parse_config_val (&conf, &binlog_file_name, "binlog", BINLOG_FILE, config_directory); + if (!lua_file) { + parse_config_val (&conf, &lua_file, "lua_script", 0, config_directory); + } + strcpy (buf + l, "binlog_enabled"); config_lookup_bool (&conf, buf, &binlog_enabled); @@ -396,7 +401,6 @@ int register_mode; int disable_auto_accept; int wait_dialog_list; -char *lua_file; void args_parse (int argc, char **argv) { int opt = 0; diff --git a/queries.c b/queries.c index 567f746..aa4729c 100644 --- a/queries.c +++ b/queries.c @@ -757,12 +757,13 @@ void tgl_do_send_encr_chat_layer (struct tgl_secret_chat *E) { long long t; tglt_secure_random (&t, 8); int action[2]; - action[0] = tgl_message_action_notify_layer; + action[0] = CODE_decrypted_message_action_notify_layer; action[1] = 15; 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 tgl_message *M = tgl_message_get (t); assert (M); + assert (M->action.type == tgl_message_action_notify_layer); tgl_do_send_msg (M, 0, 0); //print_message (M); } @@ -788,12 +789,14 @@ static int msg_send_on_answer (struct query *q UU) { int id = fetch_int (); // id struct tgl_message *M = q->extra; bl_do_set_msg_id (M, id); - tglu_fetch_date (); - tglu_fetch_pts (); + int date = fetch_int (); + int pts = fetch_int (); //tglu_fetch_seq (); //bl_do_ int seq = fetch_int (); if (seq == tgl_state.seq + 1) { + bl_do_set_date (date); + bl_do_set_pts (pts); bl_do_msg_seq_update (id); } else { tgl_do_get_difference (0, 0, 0); @@ -1012,7 +1015,8 @@ void tgl_do_send_text (tgl_peer_id_t id, char *file_name, void (*callback)(void /* {{{ Mark read */ static int mark_read_on_receive (struct query *q UU) { assert (fetch_int () == (int)CODE_messages_affected_history); - tglu_fetch_pts (); + //tglu_fetch_pts (); + fetch_int (); //tglu_fetch_seq (); fetch_int (); // seq fetch_int (); // offset @@ -1150,7 +1154,7 @@ void tgl_do_get_local_history (tgl_peer_id_t id, int limit, void (*callback)(voi count = 1; while (count < limit && M->next) { M = M->next; - ML[count ++] = M->next; + ML[count ++] = M; } callback (callback_extra, 1, count, ML); @@ -1324,11 +1328,13 @@ static int send_file_on_answer (struct query *q UU) { for (i = 0; i < n; i++) { tglf_fetch_alloc_user (); } - tglu_fetch_pts (); + //tglu_fetch_pts (); + int pts = fetch_int (); //tglu_fetch_seq (); int seq = fetch_int (); if (seq == tgl_state.seq + 1) { + bl_do_set_pts (pts); bl_do_msg_seq_update (M->id); } else { tgl_do_get_difference (0, 0, 0); @@ -1670,10 +1676,12 @@ static int fwd_msg_on_answer (struct query *q UU) { for (i = 0; i < n; i++) { tglf_fetch_alloc_user (); } - tglu_fetch_pts (); + //tglu_fetch_pts (); + int pts = fetch_int (); int seq = fetch_int (); if (seq == tgl_state.seq + 1) { + bl_do_set_pts (pts); bl_do_msg_seq_update (M->id); } else { tgl_do_get_difference (0, 0, 0); @@ -1720,9 +1728,12 @@ static int rename_chat_on_answer (struct query *q UU) { for (i = 0; i < n; i++) { tglf_fetch_alloc_user (); } - tglu_fetch_pts (); + //tglu_fetch_pts (); + int pts = fetch_int (); + int seq = fetch_int (); if (seq == tgl_state.seq + 1) { + bl_do_set_pts (pts); bl_do_msg_seq_update (M->id); } else { tgl_do_get_difference (0, 0, 0); @@ -2715,6 +2726,9 @@ static int get_state_on_answer (struct query *q UU) { //int get_difference_active; static int get_difference_on_answer (struct query *q UU) { //get_difference_active = 0; + assert (tgl_state.locks & TGL_LOCK_DIFF); + tgl_state.locks ^= TGL_LOCK_DIFF; + unsigned x = fetch_int (); if (x == CODE_updates_difference_empty) { bl_do_set_date (fetch_int ()); @@ -2778,9 +2792,9 @@ static int get_difference_on_answer (struct query *q UU) { tfree (EL, el_pos * sizeof (void *)); if (x == CODE_updates_difference_slice) { - if (q->callback) { - ((void (*)(void *, int))q->callback) (q->callback_extra, 1); - } + //if (q->callback) { + // ((void (*)(void *, int))q->callback) (q->callback_extra, 1); + //} tgl_do_get_difference (0, q->callback, q->callback_extra); } else { //difference_got = 1; @@ -2807,6 +2821,13 @@ static struct query_methods get_difference_methods = { void tgl_do_get_difference (int sync_from_start, void (*callback)(void *callback_extra, int success), void *callback_extra) { //get_difference_active = 1; //difference_got = 0; + if (tgl_state.locks & TGL_LOCK_DIFF) { + if (callback) { + callback (callback_extra, 0); + } + return; + } + tgl_state.locks |= TGL_LOCK_DIFF; clear_packet (); tgl_do_insert_header (); if (tgl_state.seq > 0 || sync_from_start) { diff --git a/tgl.h b/tgl.h index 7d9f45f..49adab8 100644 --- a/tgl.h +++ b/tgl.h @@ -29,6 +29,7 @@ struct dc; #define TGL_UPDATE_TITLE 2048 #define TGL_UPDATE_ADMIN 4096 #define TGL_UPDATE_MEMBERS 8192 +#define TGL_UPDATE_ACCESS_HASH 16384 struct tgl_update_callback { void (*new_msg)(struct tgl_message *M); @@ -57,6 +58,7 @@ struct tgl_update_callback { void (*user_update)(struct tgl_user *C, unsigned flags); void (*secret_chat_update)(struct tgl_secret_chat *C, unsigned flags); void (*msg_receive)(struct tgl_message *M); + void (*our_id)(int id); }; struct tgl_net_methods { @@ -77,6 +79,8 @@ struct tgl_net_methods { #define E_NOTICE 2 #define E_DEBUG 3 +#define TGL_LOCK_DIFF 1 + struct tgl_state { int our_id; // ID of logged in user int encr_root; @@ -91,6 +95,7 @@ struct tgl_state { int verbosity; int unread_messages; + long long locks; struct dc *DC_list[TGL_MAX_DC_NUM]; struct dc *DC_working; int max_dc_num; diff --git a/updates.c b/updates.c index 432aa2b..3c2de30 100644 --- a/updates.c +++ b/updates.c @@ -10,7 +10,7 @@ void tglu_fetch_pts (void) { int p = fetch_int (); if (p <= tgl_state.pts) { return; } - if (p != tgl_state.pts + 1) { + /*if (p != tgl_state.pts + 1) { if (tgl_state.pts) { //vlogprintf (E_NOTICE, "Hole in pts p = %d, pts = %d\n", p, tgl_state.pts); @@ -21,14 +21,14 @@ void tglu_fetch_pts (void) { } } else { tgl_state.pts ++; - } + }*/ bl_do_set_pts (tgl_state.pts); } void tglu_fetch_qts (void) { int p = fetch_int (); if (p <= tgl_state.qts) { return; } - if (p != tgl_state.qts + 1) { + /*if (p != tgl_state.qts + 1) { if (tgl_state.qts) { //logprintf ("Hole in qts\n"); // get difference should be here @@ -38,14 +38,14 @@ void tglu_fetch_qts (void) { } } else { tgl_state.qts ++; - } + }*/ bl_do_set_qts (tgl_state.qts); } void tglu_fetch_date (void) { int p = fetch_int (); if (p > tgl_state.date) { - tgl_state.date = p; + //tgl_state.date = p; bl_do_set_date (tgl_state.date); } } @@ -669,6 +669,9 @@ static int do_skip_seq (int seq) { tgl_do_get_difference (0, 0, 0); return -1; } + if (tgl_state.locks & TGL_LOCK_DIFF) { + return -1; + } return 0; } else { return -1;