From 94d55325b49bdaf375c0cbdaf34e4182c892c9d4 Mon Sep 17 00:00:00 2001 From: vysheng Date: Wed, 23 Oct 2013 14:24:59 +0400 Subject: [PATCH] Added chat_info query. Fixed interface --- interface.c | 61 +++++++++++++++++++++++++++++++--- interface.h | 3 +- loop.c | 6 ++++ queries.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++- queries.h | 2 ++ structures.h | 1 + 6 files changed, 158 insertions(+), 7 deletions(-) diff --git a/interface.c b/interface.c index 634ab6a..c9ac77c 100644 --- a/interface.c +++ b/interface.c @@ -36,6 +36,7 @@ char *commands[] = { "send_photo", "send_video", "send_text", + "chat_info", 0 }; int commands_flags[] = { @@ -48,6 +49,7 @@ int commands_flags[] = { 0732, 0732, 0732, + 074, }; char *a = 0; @@ -132,6 +134,19 @@ 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) { + index ++; + while (index < user_num + chat_num && (!Peers[index]->print_name || strncmp (Peers[index]->print_name, text, len) || Peers[index]->id > 0)) { + index ++; + } + if (index < user_num + chat_num) { + *R = strdup (Peers[index]->print_name); + return index; + } else { + return -1; + } +} + int complete_user_chat_list (int index, const char *text, int len, char **R) { index ++; while (index < user_num + chat_num && (!Peers[index]->print_name || strncmp (Peers[index]->print_name, text, len))) { @@ -192,6 +207,9 @@ char *command_generator (const char *text, int state) { return R; case 3: return rl_filename_completion_function(text,state); + case 4: + index = complete_chat_list (index, text, len, &R); + return R; default: return 0; } @@ -272,6 +290,17 @@ void interpreter (char *line UU) { do_send_text (Peers[index], strndup (f, len)); } } + } else if (!memcmp (line, "chat_info", 9)) { + char *q = line + 10; + int len; + char *text = get_token (&q, &len); + int index = 0; + while (index < user_num + chat_num && (!Peers[index]->print_name || strncmp (Peers[index]->print_name, text, len))) { + index ++; + } + if (index < user_num + chat_num && Peers[index]->id < 0) { + do_get_chat_info (Peers[index]); + } } else if (!memcmp (line, "history", 7)) { char *q = line + 7; int len; @@ -472,16 +501,38 @@ void print_media (struct message_media *M) { } } +int unknown_user_list_pos; +int unknown_user_list[1000]; + void print_user_name (int id, union user_chat *U) { push_color (COLOR_RED); if (!U) { printf ("user#%d", id); - } else if (!U->user.first_name) { - printf ("%s", U->user.last_name); - } else if (!U->user.last_name) { - printf ("%s", U->user.first_name); + int i; + for (i = 0; i < unknown_user_list_pos; i++) { + if (unknown_user_list[i] == id) { + id = 0; + break; + } + } + if (id) { + assert (unknown_user_list_pos < 1000); + unknown_user_list[unknown_user_list_pos ++] = id; + } } else { - printf ("%s %s", U->user.first_name, U->user.last_name); + if (U->flags & 20) { + push_color (COLOR_REDB); + } + if (!U->user.first_name) { + printf ("%s", U->user.last_name); + } else if (!U->user.last_name) { + printf ("%s", U->user.first_name); + } else { + printf ("%s %s", U->user.first_name, U->user.last_name); + } + if (U->flags & 20) { + pop_color (); + } } pop_color (); } diff --git a/interface.h b/interface.h index 2f9fb9b..e714725 100644 --- a/interface.h +++ b/interface.h @@ -1,7 +1,8 @@ #ifndef __INTERFACE_H__ #define __INTERFACE_H__ -#define COLOR_RED "\033[31;1m" +#define COLOR_RED "\033[0;31m" +#define COLOR_REDB "\033[1;31m" #define COLOR_NORMAL "\033[0m" #define COLOR_GREEN "\033[32;1m" #define COLOR_GREY "\033[37;1m" diff --git a/loop.c b/loop.c index 55707fb..ffe5646 100644 --- a/loop.c +++ b/loop.c @@ -27,6 +27,8 @@ extern char *auth_token; void set_default_username (const char *s); int default_dc_num; +extern int unknown_user_list_pos; +extern int unknown_user_list[]; void net_loop (int flags, int (*is_end)(void)) { while (!is_end ()) { @@ -58,6 +60,10 @@ void net_loop (int flags, int (*is_end)(void)) { rl_callback_read_char (); } connections_poll_result (fds + cc, x - cc); + if (unknown_user_list_pos) { + do_get_user_list_info_silent (unknown_user_list_pos, unknown_user_list); + unknown_user_list_pos = 0; + } } } diff --git a/queries.c b/queries.c index 92a5877..d609a57 100644 --- a/queries.c +++ b/queries.c @@ -416,7 +416,17 @@ int get_contacts_on_answer (struct query *q UU) { n = fetch_int (); for (i = 0; i < n; i++) { struct user *U = fetch_alloc_user (); - rprintf ("User #%d: " COLOR_RED "%s %s" COLOR_NORMAL " (" COLOR_GREEN "%s" COLOR_NORMAL ")\n", U->id, U->first_name, U->last_name, U->print_name); + print_start (); + push_color (COLOR_YELLOW); + printf ("User #%d: ", U->id); + print_user_name (U->id, (union user_chat *)U); + push_color (COLOR_GREEN); + printf (" ("); + printf ("%s", U->print_name); + printf (")\n"); + pop_color (); + pop_color (); + print_end (); } return 0; } @@ -757,3 +767,83 @@ void do_send_photo (int type, int to_id, char *file_name) { } send_part (f); } + +int chat_info_on_answer (struct query *q UU) { + assert (fetch_int () == (int)CODE_messages_chat_full); + + assert (fetch_int () == (int)CODE_chat_full); + int id = fetch_int (); // id + + assert (fetch_int () == (int)CODE_chat_participants); + assert (id == fetch_int ()); // id + + int admin_id = fetch_int (); + assert (fetch_int () == CODE_vector); + int n = fetch_int (); + assert (n <= 100); + int i; + static int a[300]; + for (i = 0; i < n; i ++) { + assert (fetch_int () == (int)CODE_chat_participant); + a[3 * i + 0] = fetch_int (); + a[3 * i + 1] = fetch_int (); + a[3 * i + 2] = fetch_int (); + } + fetch_int (); // version + + print_start (); + push_color (COLOR_YELLOW); + printf ("Chat "); + print_chat_name (-id, user_chat_get (-id)); + printf (" members:\n"); + for (i = 0; i < n; i++) { + printf ("\t\t"); + print_user_name (a[3 * i], user_chat_get (a[3 * i])); + if (a[3 * i] == admin_id) { + printf (" admin"); + } + printf ("\n"); + } + pop_color (); + print_end (); + return 0; +} + +struct query_methods chat_info_methods = { + .on_answer = chat_info_on_answer +}; + +void do_get_chat_info (union user_chat *chat) { + clear_packet (); + out_int (CODE_messages_get_full_chat); + out_int (-chat->id); + send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &chat_info_methods, 0); +} + +int user_list_info_silent_on_answer (struct query *q UU) { + assert (fetch_int () == CODE_vector); + int n = fetch_int (); + int i; + for (i = 0; i < n; i++) { + fetch_alloc_user (); + } + return 0; +} + +struct query_methods user_list_info_silent_methods = { + .on_answer = user_list_info_silent_on_answer +}; + +void do_get_user_list_info_silent (int num, int *list) { + clear_packet (); + out_int (CODE_users_get_users); + out_int (CODE_vector); + out_int (num); + int i; + for (i = 0; i < num; i++) { + out_int (CODE_input_user_contact); + out_int (list[i]); + //out_long (0); + } + send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &user_list_info_silent_methods, 0); +} diff --git a/queries.h b/queries.h index 0200438..ca10ba2 100644 --- a/queries.h +++ b/queries.h @@ -52,5 +52,7 @@ void do_send_text (union user_chat *U, char *file); void do_get_history (union user_chat *U, int limit); void do_get_dialog_list (void); void do_send_photo (int type, int to_id, char *file_name); +void do_get_chat_info (union user_chat *chat); +void do_get_user_list_info_silent (int num, int *list); #endif diff --git a/structures.h b/structures.h index b2a5fe2..bc6e3c3 100644 --- a/structures.h +++ b/structures.h @@ -37,6 +37,7 @@ struct chat { int user_num; int date; int version; + int admin_id; }; union user_chat {