diff --git a/binlog.c b/binlog.c index 567471a..16b47f4 100644 --- a/binlog.c +++ b/binlog.c @@ -702,6 +702,7 @@ static int fetch_comb_binlog_create_message_text (void *extra) { M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); + M->unread = fetch_int (); int l = prefetch_strlen (); M->message = talloc (l + 1); @@ -715,7 +716,7 @@ static int fetch_comb_binlog_create_message_text (void *extra) { M->media.type = tgl_message_media_none; } - M->unread = 1; + //M->unread = 1; M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; tglm_message_insert (M); @@ -815,6 +816,8 @@ static int fetch_comb_binlog_create_message_text_fwd (void *extra) { M->fwd_from_id = TGL_MK_USER (fetch_int ()); M->fwd_date = fetch_int (); + + M->unread = fetch_int (); int l = prefetch_strlen (); M->message = talloc (l + 1); @@ -828,7 +831,7 @@ static int fetch_comb_binlog_create_message_text_fwd (void *extra) { M->media.type = tgl_message_media_none; } - M->unread = 1; + //M->unread = 1; M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; tglm_message_insert (M); @@ -849,6 +852,8 @@ static int fetch_comb_binlog_create_message_media (void *extra) { int t = fetch_int (); M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); + + M->unread = fetch_int (); int l = prefetch_strlen (); M->message = talloc (l + 1); @@ -857,7 +862,7 @@ static int fetch_comb_binlog_create_message_media (void *extra) { M->message_len = l; tglf_fetch_message_media (&M->media); - M->unread = 1; + //M->unread = 1; M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; tglm_message_insert (M); @@ -909,6 +914,8 @@ static int fetch_comb_binlog_create_message_media_fwd (void *extra) { M->fwd_from_id = TGL_MK_USER (fetch_int ()); M->fwd_date = fetch_int (); + + M->unread = fetch_int (); int l = prefetch_strlen (); M->message = talloc (l + 1); @@ -917,7 +924,7 @@ static int fetch_comb_binlog_create_message_media_fwd (void *extra) { M->message_len = l; tglf_fetch_message_media (&M->media); - M->unread = 1; + //M->unread = 1; M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; tglm_message_insert (M); @@ -937,9 +944,11 @@ static int fetch_comb_binlog_create_message_service (void *extra) { int t = fetch_int (); M->to_id = tgl_set_peer_id (t, fetch_int ()); M->date = fetch_int (); + + M->unread = fetch_int (); tglf_fetch_message_action (&M->action); - M->unread = 1; + //M->unread = 1; M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; M->service = 1; @@ -994,9 +1003,11 @@ static int fetch_comb_binlog_create_message_service_fwd (void *extra) { M->fwd_from_id = TGL_MK_USER (fetch_int ()); M->fwd_date = fetch_int (); + + M->unread = fetch_int (); tglf_fetch_message_action (&M->action); - M->unread = 1; + //M->unread = 1; M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; M->service = 1; @@ -1735,7 +1746,7 @@ void bl_do_chat_del_user (struct tgl_chat *C, int version, int user) { add_log_event (ev, 16); } -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 (int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s) { clear_packet (); out_int (CODE_binlog_create_message_text); out_int (msg_id); @@ -1743,6 +1754,7 @@ void bl_do_create_message_text (int msg_id, int from_id, int to_type, int to_id, out_int (to_type); out_int (to_id); out_int (date); + out_int (unread); out_cstring (s, l); add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } @@ -1771,7 +1783,7 @@ void bl_do_send_message_action_encr (long long msg_id, int from_id, int to_type, add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_create_message_text_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int l, const char *s) { +void bl_do_create_message_text_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s) { clear_packet (); out_int (CODE_binlog_create_message_text_fwd); out_int (msg_id); @@ -1781,11 +1793,12 @@ void bl_do_create_message_text_fwd (int msg_id, int from_id, int to_type, int to out_int (date); out_int (fwd); out_int (fwd_date); + out_int (unread); out_cstring (s, l); add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_create_message_media (int msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len) { +void bl_do_create_message_media (int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s, const int *data, int len) { clear_packet (); out_int (CODE_binlog_create_message_media); out_int (msg_id); @@ -1793,6 +1806,7 @@ void bl_do_create_message_media (int msg_id, int from_id, int to_type, int to_id out_int (to_type); out_int (to_id); out_int (date); + out_int (unread); out_cstring (s, l); out_ints (data, len); add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); @@ -1812,7 +1826,7 @@ void bl_do_create_message_media_encr (long long msg_id, int from_id, int to_type add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_create_message_media_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int l, const char *s, const int *data, int len) { +void bl_do_create_message_media_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s, const int *data, int len) { clear_packet (); out_int (CODE_binlog_create_message_media_fwd); out_int (msg_id); @@ -1822,12 +1836,13 @@ void bl_do_create_message_media_fwd (int msg_id, int from_id, int to_type, int t out_int (date); out_int (fwd); out_int (fwd_date); + out_int (unread); out_cstring (s, l); out_ints (data, len); add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_create_message_service (int msg_id, int from_id, int to_type, int to_id, int date, const int *data, int len) { +void bl_do_create_message_service (int msg_id, int from_id, int to_type, int to_id, int date, int unread, const int *data, int len) { clear_packet (); out_int (CODE_binlog_create_message_service); out_int (msg_id); @@ -1835,6 +1850,7 @@ void bl_do_create_message_service (int msg_id, int from_id, int to_type, int to_ out_int (to_type); out_int (to_id); out_int (date); + out_int (unread); out_ints (data, len); add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } @@ -1851,7 +1867,7 @@ void bl_do_create_message_service_encr (long long msg_id, int from_id, int to_ty add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } -void bl_do_create_message_service_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, const int *data, int len) { +void bl_do_create_message_service_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, const int *data, int len) { clear_packet (); out_int (CODE_binlog_create_message_service_fwd); out_int (msg_id); @@ -1861,6 +1877,7 @@ void bl_do_create_message_service_fwd (int msg_id, int from_id, int to_type, int out_int (date); out_int (fwd); out_int (fwd_date); + out_int (unread); out_ints (data, len); add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } diff --git a/binlog.h b/binlog.h index 9393c2c..0e60a95 100644 --- a/binlog.h +++ b/binlog.h @@ -69,12 +69,12 @@ 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); -void bl_do_create_message_service (int msg_id, int from_id, int to_type, int to_id, int date, const int *data, int len); -void bl_do_create_message_service_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, const int *data, int len); -void bl_do_create_message_media (int msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len); -void bl_do_create_message_media_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int l, const char *s, const int *data, int len); +void bl_do_create_message_text (int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s); +void bl_do_create_message_text_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s); +void bl_do_create_message_service (int msg_id, int from_id, int to_type, int to_id, int date, int unread, const int *data, int len); +void bl_do_create_message_service_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, const int *data, int len); +void bl_do_create_message_media (int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s, const int *data, int len); +void bl_do_create_message_media_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s, const int *data, int len); void bl_do_create_message_media_encr (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len, const int *data2, int len2); void bl_do_create_message_service_encr (long long msg_id, int from_id, int to_type, int to_id, int date, const int *data, int len); void bl_do_send_message_text (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s); diff --git a/binlog.tl b/binlog.tl index 0c0c71e..56d2cad 100644 --- a/binlog.tl +++ b/binlog.tl @@ -50,16 +50,16 @@ binlog.chatSetFullPhoto id:int photo:Photo = binlog.Update; binlog.chatAddParticipant id:int version:int user:%binlog.ChatParticipant = binlog.Update; binlog.chatDelParticipant id:int version:int user:int = binlog.Update; -binlog.createMessageText id:int from_id:int to_type:int to_id:int date:int text:string = binlog.Update; +binlog.createMessageText id:int from_id:int to_type:int to_id:int date:int unread:int text:string = binlog.Update; binlog.sendMessageText id:long from_id:int to_type:int to_id:int date:int text:string = binlog.Update; binlog.sendMessageActionEncr id:long from_id:int to_type:int to_id:int date:int action:DecryptedMessageAction = binlog.Update; -binlog.createMessageTextFwd id:int from_id:int to_type:int to_id:int date:int fwd_from_id:int fwd_date:int text:string = binlog.Update; -binlog.createMessageMedia id:int from_id:int to_type:int to_id:int date:int text:string media:MessageMedia = binlog.Update; +binlog.createMessageTextFwd id:int from_id:int to_type:int to_id:int date:int fwd_from_id:int fwd_date:int unread:int text:string = binlog.Update; +binlog.createMessageMedia id:int from_id:int to_type:int to_id:int date:int unread:int text:string media:MessageMedia = binlog.Update; binlog.createMessageMediaEncr id:long from_id:int to_type:int to_id:int date:int text:string media:DecryptedMessageMedia file:EncryptedFile = binlog.Update; -binlog.createMessageMediaFwd id:int from_id:int to_type:int to_id:int date:int fwd_from_id:int fwd_date:int text:string media:MessageMedia = binlog.Update; -binlog.createMessageService id:int from_id:int to_type:int to_id:int date:int action:MessageAction = binlog.Update; +binlog.createMessageMediaFwd id:int from_id:int to_type:int to_id:int date:int fwd_from_id:int fwd_date:int unread:int text:string media:MessageMedia = binlog.Update; +binlog.createMessageService id:int from_id:int to_type:int to_id:int date:int unread:int action:MessageAction = binlog.Update; 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.createMessageServiceFwd id:int from_id:int to_type:int to_id:int date:int fwd_from_id:int fwd_date:int unread:int action:MessageAction = binlog.Update; binlog.messageSetUnread id:int = binlog.Update; binlog.messageSetUnreadLong id:long = binlog.Update; binlog.setMessageSent id:long = binlog.Update; diff --git a/interface.c b/interface.c index bbcd098..78c1b21 100644 --- a/interface.c +++ b/interface.c @@ -42,6 +42,7 @@ #include "interface.h" #include "telegram.h" +#include //#include "auto/constants.h" //#include "tools.h" //#include "structures.h" @@ -260,6 +261,7 @@ char *get_default_prompt (void) { l += snprintf (buf + l, 999 - l, "%lld%%Down", 100 * tgl_state.cur_downloaded_bytes / tgl_state.cur_downloading_bytes); } l += snprintf (buf + l, 999 - l, "]" COLOR_NORMAL); + l += snprintf (buf + l, 999 - l, "%s", default_prompt); return buf; } l += snprintf (buf + l, 999 - l, "%s", default_prompt); @@ -689,11 +691,96 @@ void interpreter_chat_mode (char *line) { } } +#define MAX_UNREAD_MESSAGE_COUNT 10000 +struct tgl_message *unread_message_list[MAX_UNREAD_MESSAGE_COUNT]; +int unread_message_count; +struct event *unread_message_event; + +void print_read_list (int num, struct tgl_message *list[]) { + int i; + print_start (); + for (i = 0; i < num; i++) if (list[i]) { + tgl_peer_id_t to_id; + if (tgl_get_peer_type (list[i]->to_id) == TGL_PEER_USER && tgl_get_peer_id (list[i]->to_id) == tgl_state.our_id) { + to_id = list[i]->from_id; + } else { + to_id = list[i]->to_id; + } + int j; + int c1 = 0; + int c2 = 0; + for (j = i; j < num; j++) if (list[j]) { + tgl_peer_id_t end_id; + if (tgl_get_peer_type (list[j]->to_id) == TGL_PEER_USER && tgl_get_peer_id (list[j]->to_id) == tgl_state.our_id) { + end_id = list[j]->from_id; + } else { + end_id = list[j]->to_id; + } + if (!tgl_cmp_peer_id (to_id, end_id)) { + if (list[j]->out) { + c1 ++; + } else { + c2 ++; + } + list[j] = 0; + } + } + + assert (c1 + c2 > 0); + push_color (COLOR_YELLOW); + 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; + default: + assert (0); + } + printf (" marked read %d outbox and %d inbox messages\n", c1, c2); + pop_color (); + } + print_end (); +} + +void unread_message_alarm (evutil_socket_t fd, short what, void *arg) { + print_read_list (unread_message_count, unread_message_list); + unread_message_count = 0; + event_free (unread_message_event); + unread_message_event = 0; +} + 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; + if (unread_message_count + num <= MAX_UNREAD_MESSAGE_COUNT) { + memcpy (unread_message_list + unread_message_count, list, num * sizeof (void *)); + unread_message_count += num; + + if (!unread_message_event) { + unread_message_event = evtimer_new (tgl_state.ev_base, unread_message_alarm, 0); + static struct timeval ptimeout = { 1, 0}; + event_add (unread_message_event, &ptimeout); + } + } else { + print_read_list (unread_message_count, unread_message_list); + print_read_list (num, list); + unread_message_count = 0; + if (unread_message_event) { + event_free (unread_message_event); + unread_message_event = 0; + } + } + /* + tgl_peer_id_t to_id = list[0]->to_id; int ok = 1; int i; for (i = 1; i < num; i++) { @@ -724,7 +811,7 @@ void mark_read_upd (int num, struct tgl_message *list[]) { printf ("\n"); } pop_color (); - print_end (); + print_end ();*/ } void type_notification_upd (struct tgl_user *U) { diff --git a/loop.c b/loop.c index 0f4e819..fb38823 100644 --- a/loop.c +++ b/loop.c @@ -101,6 +101,7 @@ void net_loop (int flags, int (*is_end)(void)) { exit (0); } write_state_file (); + update_prompt (); if (unknown_user_list_pos) { int i; for (i = 0; i < unknown_user_list_pos; i++) { diff --git a/structures.c b/structures.c index 60f65c8..d70ba3c 100644 --- a/structures.c +++ b/structures.c @@ -645,7 +645,7 @@ void tglf_fetch_message_short (struct tgl_message *M) { //tglu_fetch_seq (); fetch_int (); - bl_do_create_message_text (id, from_id, TGL_PEER_USER, to_id, date, l, s); + bl_do_create_message_text (id, from_id, TGL_PEER_USER, to_id, date, 0, l, s); } else { fetch_int (); // id fetch_int (); // from_id @@ -675,7 +675,7 @@ void tglf_fetch_message_short_chat (struct tgl_message *M) { //tglu_fetch_seq (); fetch_int (); - bl_do_create_message_text (id, from_id, TGL_PEER_CHAT, to_id, date, l, s); + bl_do_create_message_text (id, from_id, TGL_PEER_CHAT, to_id, date, 0, l, s); } else { fetch_int (); // id fetch_int (); // from_id @@ -998,9 +998,9 @@ void tglf_fetch_message (struct tgl_message *M) { if (new) { if (fwd_from_id) { - bl_do_create_message_service_fwd (id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, fwd_from_id, fwd_date, 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, unread, start, (in_ptr - start)); } else { - bl_do_create_message_service (id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, 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, unread, start, (in_ptr - start)); } } } else { @@ -1012,9 +1012,9 @@ void tglf_fetch_message (struct tgl_message *M) { if (new) { if (fwd_from_id) { - bl_do_create_message_media_fwd (id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, fwd_from_id, fwd_date, 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, unread, l, s, start, in_ptr - start); } else { - bl_do_create_message_media (id, from_id, tgl_get_peer_type (to_id), tgl_get_peer_id (to_id), date, 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, unread, l, s, start, in_ptr - start); } } }