Fixed small bug. Changed interface

This commit is contained in:
vysheng 2013-10-18 23:30:24 +04:00
parent 0fef150a94
commit 6cc1b3d349
7 changed files with 201 additions and 38 deletions

View File

@ -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) { void hexdump (int *in_ptr, int *in_end) {
int saved_point = 0; int saved_point = 0;
char *saved_line = 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) { void print_message (struct message *M) {
if (M->service) { if (M->service) {
rprintf ("Service message\n"); rprintf ("Service message\n");
return; return;
} }
print_start ();
if (M->to_id >= 0) { if (M->to_id >= 0) {
if (M->out) { if (M->out) {
union user_chat *U = user_chat_get (M->to_id); push_color (COLOR_GREEN);
assert (M->from_id >= 0); print_date (M->date);
if (U) { pop_color ();
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)); printf (" ");
} else { print_user_name (M->to_id, user_chat_get (M->to_id));
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);
} printf (" <<< ");
} else { } else {
union user_chat *U = user_chat_get (M->from_id); push_color (COLOR_BLUE);
assert (M->from_id >= 0); print_date (M->date);
if (U) { pop_color ();
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)); printf (" ");
} else { print_user_name (M->from_id, user_chat_get (M->from_id));
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);
} printf (" >>> ");
} }
} else { } else {
rprintf ("Message to chat %d\n", -M->to_id); push_color (COLOR_MAGENTA);
union user_chat *C = user_chat_get (M->to_id); print_date (M->date);
if (C) { pop_color ();
rprintf ("Chat %s\n", C->chat.title); 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();
} }

View File

@ -7,6 +7,7 @@
#define COLOR_GREY "\033[37;1m" #define COLOR_GREY "\033[37;1m"
#define COLOR_YELLOW "\033[33;1m" #define COLOR_YELLOW "\033[33;1m"
#define COLOR_BLUE "\033[34;1m" #define COLOR_BLUE "\033[34;1m"
#define COLOR_MAGENTA "\033[35;1m"
char *get_default_prompt (void); char *get_default_prompt (void);

6
loop.c
View File

@ -92,6 +92,7 @@ void write_dc (int auth_file_fd, struct dc *DC) {
assert (write (auth_file_fd, &DC->server_salt, 8) == 8); assert (write (auth_file_fd, &DC->server_salt, 8) == 8);
} }
int our_id;
void write_auth_file (void) { void write_auth_file (void) {
int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, S_IRWXU); int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, S_IRWXU);
assert (auth_file_fd >= 0); 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, &x, 4) == 4);
} }
} }
assert (write (auth_file_fd, &our_id, 4) == 4);
close (auth_file_fd); close (auth_file_fd);
} }
@ -164,6 +166,10 @@ void read_auth_file (void) {
read_dc (auth_file_fd, i); read_dc (auth_file_fd, i);
} }
} }
int l = read (auth_file_fd, &our_id, 4);
if (l < 4) {
assert (!l);
}
close (auth_file_fd); close (auth_file_fd);
} }

View File

@ -38,7 +38,7 @@ int alarm_query (struct query *q) {
return 0; 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);
assert (DC->auth_key_id); assert (DC->auth_key_id);
if (!DC->sessions[0]) { 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.timeout = get_double_time () + QUERY_TIMEOUT;
q->ev.self = (void *)q; q->ev.self = (void *)q;
insert_event_timer (&q->ev); insert_event_timer (&q->ev);
q->extra = extra;
return q; return q;
} }
@ -307,7 +309,7 @@ void do_send_code (const char *user) {
out_int (TG_APP_ID); out_int (TG_APP_ID);
out_string (TG_APP_HASH); 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); net_loop (0, code_is_sent);
if (want_dc_num == -1) { return; } if (want_dc_num == -1) { return; }
@ -323,7 +325,7 @@ void do_send_code (const char *user) {
} else { } else {
clear_packet (); clear_packet ();
out_int (CODE_help_get_config); 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); net_loop (0, config_got);
DC_working = DC_list[want_dc_num]; DC_working = DC_list[want_dc_num];
if (!DC_working->auth_key_id) { if (!DC_working->auth_key_id) {
@ -342,7 +344,7 @@ void do_send_code (const char *user) {
out_int (TG_APP_ID); out_int (TG_APP_ID);
out_string (TG_APP_HASH); 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); net_loop (0, code_is_sent);
assert (want_dc_num == -1); assert (want_dc_num == -1);
} }
@ -382,7 +384,7 @@ int do_send_code_result (const char *code) {
out_string (suser); out_string (suser);
out_string (phone_code_hash); out_string (phone_code_hash);
out_string (code); 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; sign_in_ok = 0;
net_loop (0, sign_in_is_ok); net_loop (0, sign_in_is_ok);
return sign_in_ok; return sign_in_ok;
@ -418,17 +420,20 @@ void do_update_contact_list (void) {
clear_packet (); clear_packet ();
out_int (CODE_contacts_get_contacts); out_int (CODE_contacts_get_contacts);
out_string (""); 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) { int msg_send_on_answer (struct query *q UU) {
assert (fetch_int () == (int)CODE_messages_sent_message); assert (fetch_int () == (int)CODE_messages_sent_message);
int uid = fetch_int (); // uid int id = fetch_int (); // id
int date = fetch_int (); // date int date = fetch_int (); // date
int ptr = fetch_int (); // ptr int ptr = fetch_int (); // ptr
int seq = fetch_int (); // seq 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; return 0;
} }
@ -437,12 +442,17 @@ struct query_methods msg_send_methods = {
}; };
int out_message_num; int out_message_num;
int our_id;
void do_send_message (union user_chat *U, const char *msg) { void do_send_message (union user_chat *U, const char *msg) {
if (!out_message_num) { if (!out_message_num) {
out_message_num = lrand48 (); out_message_num = -lrand48 ();
} }
clear_packet (); clear_packet ();
out_int (CODE_messages_send_message); 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) { if (U->id < 0) {
out_int (CODE_input_peer_chat); out_int (CODE_input_peer_chat);
out_int (-U->id); out_int (-U->id);
@ -456,14 +466,15 @@ void do_send_message (union user_chat *U, const char *msg) {
out_int (U->id); 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_string (msg);
out_long (out_message_num ++); out_long ((--out_message_num) - (1ll << 32));
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_methods); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_methods, M);
if (U->id < 0) { print_message (M);
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);
}
} }
int get_history_on_answer (struct query *q UU) { 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 (0); out_int (0);
out_int (limit); 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) { 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 (0); out_int (0);
out_int (1000); 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);
} }

View File

@ -24,10 +24,11 @@ struct query {
void *data; void *data;
struct query_methods *methods; struct query_methods *methods;
struct event_timer ev; 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_ack (long long id);
void query_error (long long id); void query_error (long long id);
void query_result (long long id); void query_result (long long id);

View File

@ -3,7 +3,7 @@
#include "mtproto-common.h" #include "mtproto-common.h"
#include "telegram.h" #include "telegram.h"
#include "tree.h" #include "tree.h"
#include "loop.h"
int verbosity; int verbosity;
void fetch_file_location (struct file_location *loc) { 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) { void fetch_user (struct user *U) {
memset (U, 0, sizeof (*U)); memset (U, 0, sizeof (*U));
unsigned x = fetch_int (); unsigned x = fetch_int ();
@ -50,6 +51,13 @@ void fetch_user (struct user *U) {
U->flags = 1; U->flags = 1;
return; 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->first_name = fetch_str_dup ();
U->last_name = fetch_str_dup (); U->last_name = fetch_str_dup ();
if (!strlen (U->first_name)) { if (!strlen (U->first_name)) {
@ -586,3 +594,8 @@ void update_message_id (struct message *M, int id) {
M->id = id; M->id = id;
message_tree = tree_insert_message (message_tree, M, lrand48 ()); 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 ());
}

View File

@ -158,4 +158,5 @@ int print_stat (char *s, int len);
union user_chat *user_chat_get (int id); union user_chat *user_chat_get (int id);
struct message *message_get (int id); struct message *message_get (int id);
void update_message_id (struct message *M, int id); void update_message_id (struct message *M, int id);
void message_insert (struct message *M);
#endif #endif