From 6cc1b3d3494e94c1867603202856156ba048f887 Mon Sep 17 00:00:00 2001 From: vysheng Date: Fri, 18 Oct 2013 23:30:24 +0400 Subject: [PATCH] Fixed small bug. Changed interface --- interface.c | 166 +++++++++++++++++++++++++++++++++++++++++++++------ interface.h | 1 + loop.c | 6 ++ queries.c | 47 +++++++++------ queries.h | 3 +- structures.c | 15 ++++- structures.h | 1 + 7 files changed, 201 insertions(+), 38 deletions(-) diff --git a/interface.c b/interface.c index 3dfeb1e..dbef70f 100644 --- a/interface.c +++ b/interface.c @@ -267,6 +267,32 @@ void rprintf (const char *format, ...) { } } +int saved_point; +char *saved_line; +int prompt_was; +void print_start (void) { + assert (!prompt_was); + if (readline_active) { + saved_point = rl_point; + saved_line = rl_copy_text(0, rl_end); + rl_save_prompt(); + rl_replace_line("", 0); + rl_redisplay(); + } + prompt_was = 1; +} +void print_end (void) { + assert (prompt_was); + if (readline_active) { + rl_restore_prompt(); + rl_replace_line(saved_line, 0); + rl_point = saved_point; + rl_redisplay(); + free(saved_line); + } + prompt_was = 0; +} + void hexdump (int *in_ptr, int *in_end) { int saved_point = 0; char *saved_line = 0; @@ -341,34 +367,138 @@ const char *message_media_type_str (struct message_media *M) { } } +int color_stack_pos; +const char *color_stack[10]; + +void push_color (const char *color) { + assert (color_stack_pos < 10); + color_stack[color_stack_pos ++] = color; + printf ("%s", color); +} + +void pop_color (void) { + assert (color_stack_pos > 0); + color_stack_pos --; + if (color_stack_pos >= 1) { + printf ("%s", color_stack[color_stack_pos] - 1); + } else { + printf ("%s", COLOR_NORMAL); + } +} + +void print_media (struct message_media *M) { + switch (M->type) { + case CODE_message_media_empty: + return; + case CODE_message_media_photo: + printf ("[photo]"); + return; + case CODE_message_media_video: + printf ("[video]"); + return; + case CODE_message_media_geo: + printf ("[geo] "); + printf ("%.6lf:%.6lf", M->geo.latitude, M->geo.longitude); + return; + case CODE_message_media_contact: + printf ("[contact] "); + push_color (COLOR_RED); + printf ("%s %s ", M->first_name, M->last_name); + pop_color (); + printf ("%s", M->phone); + return; + case CODE_message_media_unsupported: + printf ("[unsupported]"); + return; + default: + assert (0); + } +} + +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); + } else { + printf ("%s %s", U->user.first_name, U->user.last_name); + } + pop_color (); +} + +void print_chat_name (int id, union user_chat *C) { + push_color (COLOR_MAGENTA); + if (!C) { + printf ("chat#%d", -id); + } else { + printf ("%s", C->chat.title); + } + pop_color (); +} + +static char *monthes[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; +void print_date (long t) { + struct tm *tm = localtime (&t); + if (time (0) - t < 12 * 60 * 60) { + printf ("[%02d:%02d] ", tm->tm_hour, tm->tm_min); + } else { + printf ("[%02d %s]", tm->tm_mday, monthes[tm->tm_mon]); + } +} + +int our_id; void print_message (struct message *M) { if (M->service) { rprintf ("Service message\n"); return; } + + + print_start (); if (M->to_id >= 0) { if (M->out) { - union user_chat *U = user_chat_get (M->to_id); - assert (M->from_id >= 0); - if (U) { - rprintf (COLOR_RED "%s %s" COLOR_GREEN " <<< %s %s" COLOR_NORMAL "\n", U->user.first_name, U->user.last_name, M->message, message_media_type_str (&M->media)); - } else { - rprintf (COLOR_RED "User #%d" COLOR_GREEN " <<< %s %s" COLOR_NORMAL "\n", M->from_id, M->message, message_media_type_str (&M->media)); - } + push_color (COLOR_GREEN); + print_date (M->date); + pop_color (); + printf (" "); + print_user_name (M->to_id, user_chat_get (M->to_id)); + push_color (COLOR_GREEN); + printf (" <<< "); } else { - union user_chat *U = user_chat_get (M->from_id); - assert (M->from_id >= 0); - if (U) { - rprintf (COLOR_RED "%s %s" COLOR_BLUE " >>> %s %s" COLOR_NORMAL "\n", U->user.first_name, U->user.last_name, M->message, message_media_type_str (&M->media)); - } else { - rprintf (COLOR_RED "User #%d" COLOR_BLUE " >>> %s %s" COLOR_NORMAL "\n", M->from_id, M->message, message_media_type_str (&M->media)); - } + push_color (COLOR_BLUE); + print_date (M->date); + pop_color (); + printf (" "); + print_user_name (M->from_id, user_chat_get (M->from_id)); + push_color (COLOR_BLUE); + printf (" >>> "); } } else { - rprintf ("Message to chat %d\n", -M->to_id); - union user_chat *C = user_chat_get (M->to_id); - if (C) { - rprintf ("Chat %s\n", C->chat.title); + push_color (COLOR_MAGENTA); + print_date (M->date); + pop_color (); + printf (" "); + print_chat_name (M->to_id, user_chat_get (M->to_id)); + printf (" "); + print_user_name (M->from_id, user_chat_get (M->from_id)); + if (M->from_id == our_id) { + push_color (COLOR_GREEN); + } else { + push_color (COLOR_BLUE); } + printf (" >>> "); } + if (M->message && strlen (M->message)) { + printf ("%s", M->message); + } + if (M->media.type != CODE_message_media_empty) { + print_media (&M->media); + } + pop_color (); + assert (!color_stack_pos); + printf ("\n"); + print_end(); } diff --git a/interface.h b/interface.h index df1b1d3..eacf061 100644 --- a/interface.h +++ b/interface.h @@ -7,6 +7,7 @@ #define COLOR_GREY "\033[37;1m" #define COLOR_YELLOW "\033[33;1m" #define COLOR_BLUE "\033[34;1m" +#define COLOR_MAGENTA "\033[35;1m" char *get_default_prompt (void); diff --git a/loop.c b/loop.c index c91287b..55707fb 100644 --- a/loop.c +++ b/loop.c @@ -92,6 +92,7 @@ void write_dc (int auth_file_fd, struct dc *DC) { assert (write (auth_file_fd, &DC->server_salt, 8) == 8); } +int our_id; void write_auth_file (void) { int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, S_IRWXU); assert (auth_file_fd >= 0); @@ -112,6 +113,7 @@ void write_auth_file (void) { assert (write (auth_file_fd, &x, 4) == 4); } } + assert (write (auth_file_fd, &our_id, 4) == 4); close (auth_file_fd); } @@ -164,6 +166,10 @@ void read_auth_file (void) { read_dc (auth_file_fd, i); } } + int l = read (auth_file_fd, &our_id, 4); + if (l < 4) { + assert (!l); + } close (auth_file_fd); } diff --git a/queries.c b/queries.c index bf3cfc8..f3139c0 100644 --- a/queries.c +++ b/queries.c @@ -38,7 +38,7 @@ int alarm_query (struct query *q) { return 0; } -struct query *send_query (struct dc *DC, int ints, void *data, struct query_methods *methods) { +struct query *send_query (struct dc *DC, int ints, void *data, struct query_methods *methods, void *extra) { assert (DC); assert (DC->auth_key_id); if (!DC->sessions[0]) { @@ -66,6 +66,8 @@ struct query *send_query (struct dc *DC, int ints, void *data, struct query_meth q->ev.timeout = get_double_time () + QUERY_TIMEOUT; q->ev.self = (void *)q; insert_event_timer (&q->ev); + + q->extra = extra; return q; } @@ -307,7 +309,7 @@ void do_send_code (const char *user) { out_int (TG_APP_ID); out_string (TG_APP_HASH); - send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_code_methods); + send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_code_methods, 0); net_loop (0, code_is_sent); if (want_dc_num == -1) { return; } @@ -323,7 +325,7 @@ void do_send_code (const char *user) { } else { clear_packet (); out_int (CODE_help_get_config); - send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &help_get_config_methods); + send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &help_get_config_methods, 0); net_loop (0, config_got); DC_working = DC_list[want_dc_num]; if (!DC_working->auth_key_id) { @@ -342,7 +344,7 @@ void do_send_code (const char *user) { out_int (TG_APP_ID); out_string (TG_APP_HASH); - send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_code_methods); + send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_code_methods, 0); net_loop (0, code_is_sent); assert (want_dc_num == -1); } @@ -382,7 +384,7 @@ int do_send_code_result (const char *code) { out_string (suser); out_string (phone_code_hash); out_string (code); - send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods); + send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0); sign_in_ok = 0; net_loop (0, sign_in_is_ok); return sign_in_ok; @@ -418,17 +420,20 @@ void do_update_contact_list (void) { clear_packet (); out_int (CODE_contacts_get_contacts); out_string (""); - send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_contacts_methods); + send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_contacts_methods, 0); } int msg_send_on_answer (struct query *q UU) { assert (fetch_int () == (int)CODE_messages_sent_message); - int uid = fetch_int (); // uid + int id = fetch_int (); // id int date = fetch_int (); // date int ptr = fetch_int (); // ptr int seq = fetch_int (); // seq - logprintf ("Sent: uid = %d, date = %d, ptr = %d, seq = %d\n", uid, date, ptr, seq); + struct message *M = q->extra; + M->id = id; + message_insert (M); + logprintf ("Sent: id = %d, date = %d, ptr = %d, seq = %d\n", id, date, ptr, seq); return 0; } @@ -437,12 +442,17 @@ struct query_methods msg_send_methods = { }; int out_message_num; +int our_id; void do_send_message (union user_chat *U, const char *msg) { if (!out_message_num) { - out_message_num = lrand48 (); + out_message_num = -lrand48 (); } clear_packet (); out_int (CODE_messages_send_message); + struct message *M = malloc (sizeof (*M)); + memset (M, 0, sizeof (*M)); + M->from_id = our_id; + M->to_id = U->id; if (U->id < 0) { out_int (CODE_input_peer_chat); out_int (-U->id); @@ -456,14 +466,15 @@ void do_send_message (union user_chat *U, const char *msg) { out_int (U->id); } } + M->message = strdup (msg); + M->out = 1; + M->media.type = CODE_message_media_empty; + M->id = out_message_num; + M->date = time (0); out_string (msg); - out_long (out_message_num ++); - send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_methods); - if (U->id < 0) { - rprintf (COLOR_RED "%s" COLOR_GREEN " <<< " COLOR_NORMAL "%s\n", U->chat.title, msg); - } else { - rprintf (COLOR_RED "%s %s" COLOR_GREEN " <<< " COLOR_NORMAL "%s\n", U->user.first_name, U->user.last_name, msg); - } + out_long ((--out_message_num) - (1ll << 32)); + send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_methods, M); + print_message (M); } int get_history_on_answer (struct query *q UU) { @@ -525,7 +536,7 @@ void do_get_history (union user_chat *U, int limit) { out_int (0); out_int (0); out_int (limit); - send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_history_methods); + send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_history_methods, 0); } int get_dialogs_on_answer (struct query *q UU) { @@ -568,5 +579,5 @@ void do_get_dialog_list (void) { out_int (0); out_int (0); out_int (1000); - send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dialogs_methods); + send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dialogs_methods, 0); } diff --git a/queries.h b/queries.h index 67a5698..84012aa 100644 --- a/queries.h +++ b/queries.h @@ -24,10 +24,11 @@ struct query { void *data; struct query_methods *methods; struct event_timer ev; + void *extra; }; -struct query *send_query (struct dc *DC, int len, void *data, struct query_methods *methods); +struct query *send_query (struct dc *DC, int len, void *data, struct query_methods *methods, void *extra); void query_ack (long long id); void query_error (long long id); void query_result (long long id); diff --git a/structures.c b/structures.c index cf4a678..a70a5d1 100644 --- a/structures.c +++ b/structures.c @@ -3,7 +3,7 @@ #include "mtproto-common.h" #include "telegram.h" #include "tree.h" - +#include "loop.h" int verbosity; void fetch_file_location (struct file_location *loc) { @@ -41,6 +41,7 @@ void fetch_user_status (struct user_status *S) { } } +int our_id; void fetch_user (struct user *U) { memset (U, 0, sizeof (*U)); unsigned x = fetch_int (); @@ -50,6 +51,13 @@ void fetch_user (struct user *U) { U->flags = 1; return; } + if (x == CODE_user_self) { + assert (!our_id || (our_id == U->id)); + if (!our_id) { + our_id = U->id; + write_auth_file (); + } + } U->first_name = fetch_str_dup (); U->last_name = fetch_str_dup (); if (!strlen (U->first_name)) { @@ -586,3 +594,8 @@ void update_message_id (struct message *M, int id) { M->id = id; message_tree = tree_insert_message (message_tree, M, lrand48 ()); } + +void message_insert (struct message *M) { + message_add_use (M); + message_tree = tree_insert_message (message_tree, M, lrand48 ()); +} diff --git a/structures.h b/structures.h index 990fed6..02eb976 100644 --- a/structures.h +++ b/structures.h @@ -158,4 +158,5 @@ int print_stat (char *s, int len); union user_chat *user_chat_get (int id); struct message *message_get (int id); void update_message_id (struct message *M, int id); +void message_insert (struct message *M); #endif