Commit 73cdb880 authored by vysheng's avatar vysheng

Merge github.com:vysheng/tg

parents 121f0f1d 94d55325
...@@ -36,6 +36,7 @@ char *commands[] = { ...@@ -36,6 +36,7 @@ char *commands[] = {
"send_photo", "send_photo",
"send_video", "send_video",
"send_text", "send_text",
"chat_info",
0 }; 0 };
int commands_flags[] = { int commands_flags[] = {
...@@ -48,6 +49,7 @@ int commands_flags[] = { ...@@ -48,6 +49,7 @@ int commands_flags[] = {
0732, 0732,
0732, 0732,
0732, 0732,
074,
}; };
char *a = 0; char *a = 0;
...@@ -132,6 +134,19 @@ int complete_user_list (int index, const char *text, int len, char **R) { ...@@ -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) { int complete_user_chat_list (int index, const char *text, int len, char **R) {
index ++; index ++;
while (index < user_num + chat_num && (!Peers[index]->print_name || strncmp (Peers[index]->print_name, text, len))) { 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) { ...@@ -192,6 +207,9 @@ char *command_generator (const char *text, int state) {
return R; return R;
case 3: case 3:
return rl_filename_completion_function(text,state); return rl_filename_completion_function(text,state);
case 4:
index = complete_chat_list (index, text, len, &R);
return R;
default: default:
return 0; return 0;
} }
...@@ -272,6 +290,17 @@ void interpreter (char *line UU) { ...@@ -272,6 +290,17 @@ void interpreter (char *line UU) {
do_send_text (Peers[index], strndup (f, len)); 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)) { } else if (!memcmp (line, "history", 7)) {
char *q = line + 7; char *q = line + 7;
int len; int len;
...@@ -472,16 +501,38 @@ void print_media (struct message_media *M) { ...@@ -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) { void print_user_name (int id, union user_chat *U) {
push_color (COLOR_RED); push_color (COLOR_RED);
if (!U) { if (!U) {
printf ("user#%d", id); printf ("user#%d", id);
} else if (!U->user.first_name) { int i;
printf ("%s", U->user.last_name); for (i = 0; i < unknown_user_list_pos; i++) {
} else if (!U->user.last_name) { if (unknown_user_list[i] == id) {
printf ("%s", U->user.first_name); id = 0;
break;
}
}
if (id) {
assert (unknown_user_list_pos < 1000);
unknown_user_list[unknown_user_list_pos ++] = id;
}
} else { } 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 (); pop_color ();
} }
......
#ifndef __INTERFACE_H__ #ifndef __INTERFACE_H__
#define __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_NORMAL "\033[0m"
#define COLOR_GREEN "\033[32;1m" #define COLOR_GREEN "\033[32;1m"
#define COLOR_GREY "\033[37;1m" #define COLOR_GREY "\033[37;1m"
......
...@@ -27,6 +27,8 @@ extern char *auth_token; ...@@ -27,6 +27,8 @@ extern char *auth_token;
void set_default_username (const char *s); void set_default_username (const char *s);
int default_dc_num; int default_dc_num;
extern int unknown_user_list_pos;
extern int unknown_user_list[];
void net_loop (int flags, int (*is_end)(void)) { void net_loop (int flags, int (*is_end)(void)) {
while (!is_end ()) { while (!is_end ()) {
...@@ -58,6 +60,10 @@ void net_loop (int flags, int (*is_end)(void)) { ...@@ -58,6 +60,10 @@ void net_loop (int flags, int (*is_end)(void)) {
rl_callback_read_char (); rl_callback_read_char ();
} }
connections_poll_result (fds + cc, x - cc); 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;
}
} }
} }
......
...@@ -416,7 +416,17 @@ int get_contacts_on_answer (struct query *q UU) { ...@@ -416,7 +416,17 @@ int get_contacts_on_answer (struct query *q UU) {
n = fetch_int (); n = fetch_int ();
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
struct user *U = fetch_alloc_user (); 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; return 0;
} }
...@@ -782,3 +792,83 @@ void do_send_photo (int type, int to_id, char *file_name) { ...@@ -782,3 +792,83 @@ void do_send_photo (int type, int to_id, char *file_name) {
} }
send_part (f); 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);
}
...@@ -52,5 +52,7 @@ void do_send_text (union user_chat *U, char *file); ...@@ -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_history (union user_chat *U, int limit);
void do_get_dialog_list (void); void do_get_dialog_list (void);
void do_send_photo (int type, int to_id, char *file_name); 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 #endif
...@@ -37,6 +37,7 @@ struct chat { ...@@ -37,6 +37,7 @@ struct chat {
int user_num; int user_num;
int date; int date;
int version; int version;
int admin_id;
}; };
union user_chat { union user_chat {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment