Commit cdea9ca8 authored by vysheng's avatar vysheng

id for users and chats renamed to peer_id_t. This will make implementing...

id for users and chats renamed to peer_id_t. This will make implementing secure chats and geochats easier
parent f4dcc509
This diff is collapsed.
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
*/ */
#ifndef __INTERFACE_H__ #ifndef __INTERFACE_H__
#define __INTERFACE_H__ #define __INTERFACE_H__
#include "structures.h"
#define COLOR_RED "\033[0;31m" #define COLOR_RED "\033[0;31m"
#define COLOR_REDB "\033[1;31m" #define COLOR_REDB "\033[1;31m"
...@@ -40,10 +41,10 @@ void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2))) ...@@ -40,10 +41,10 @@ void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2)))
void hexdump (int *in_ptr, int *in_end); void hexdump (int *in_ptr, int *in_end);
struct message; struct message;
union user_chat; union peer;
void print_message (struct message *M); void print_message (struct message *M);
void print_chat_name (int id, union user_chat *C); void print_chat_name (peer_id_t id, union peer *C);
void print_user_name (int id, union user_chat *U); void print_user_name (peer_id_t id, union peer *U);
//void print_media (struct message_media *M); //void print_media (struct message_media *M);
void pop_color (void); void pop_color (void);
void push_color (const char *color); void push_color (const char *color);
......
...@@ -635,13 +635,45 @@ void rpc_execute_answer (struct connection *c, long long msg_id UU); ...@@ -635,13 +635,45 @@ void rpc_execute_answer (struct connection *c, long long msg_id UU);
int unread_messages; int unread_messages;
int our_id; int our_id;
int pts;
int qts;
void fetch_pts (void) {
int p = fetch_int ();
if (p != pts + 1) {
if (pts) {
logprintf ("Hole in pts p = %d, pts = %d\n", p, pts);
// get difference should be here
} else {
pts = p;
}
} else {
pts ++;
}
}
void fetch_qts (void) {
int p = fetch_int ();
if (p != qts + 1) {
if (qts) {
logprintf ("Hole in qts\n");
// get difference should be here
} else {
qts = p;
}
} else {
qts ++;
}
}
void work_update (struct connection *c UU, long long msg_id UU) { void work_update (struct connection *c UU, long long msg_id UU) {
unsigned op = fetch_int (); unsigned op = fetch_int ();
switch (op) { switch (op) {
case CODE_update_new_message: case CODE_update_new_message:
{ {
struct message *M = fetch_alloc_message (); struct message *M = fetch_alloc_message ();
fetch_int (); //pts fetch_pts ();
unread_messages ++; unread_messages ++;
print_message (M); print_message (M);
update_prompt (); update_prompt ();
...@@ -667,7 +699,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { ...@@ -667,7 +699,7 @@ void work_update (struct connection *c UU, long long msg_id UU) {
M->unread = 0; M->unread = 0;
} }
} }
fetch_int (); //pts fetch_pts ();
print_start (); print_start ();
push_color (COLOR_YELLOW); push_color (COLOR_YELLOW);
printf ("%d messages marked as read\n", n); printf ("%d messages marked as read\n", n);
...@@ -677,8 +709,8 @@ void work_update (struct connection *c UU, long long msg_id UU) { ...@@ -677,8 +709,8 @@ void work_update (struct connection *c UU, long long msg_id UU) {
break; break;
case CODE_update_user_typing: case CODE_update_user_typing:
{ {
int id = fetch_int (); peer_id_t id = MK_USER (fetch_int ());
union user_chat *U = user_chat_get (id); peer_t *U = user_chat_get (id);
print_start (); print_start ();
push_color (COLOR_YELLOW); push_color (COLOR_YELLOW);
printf ("User "); printf ("User ");
...@@ -690,16 +722,16 @@ void work_update (struct connection *c UU, long long msg_id UU) { ...@@ -690,16 +722,16 @@ void work_update (struct connection *c UU, long long msg_id UU) {
break; break;
case CODE_update_chat_user_typing: case CODE_update_chat_user_typing:
{ {
int chat_id = fetch_int (); peer_id_t chat_id = MK_CHAT (fetch_int ());
int id = fetch_int (); peer_id_t id = MK_USER (fetch_int ());
union user_chat *C = user_chat_get (-chat_id); peer_t *C = user_chat_get (chat_id);
union user_chat *U = user_chat_get (id); peer_t *U = user_chat_get (id);
print_start (); print_start ();
push_color (COLOR_YELLOW); push_color (COLOR_YELLOW);
printf ("User "); printf ("User ");
print_user_name (id, U); print_user_name (id, U);
printf (" is typing in chat "); printf (" is typing in chat ");
print_chat_name (-chat_id, C); print_chat_name (chat_id, C);
printf ("....\n"); printf ("....\n");
pop_color (); pop_color ();
print_end (); print_end ();
...@@ -707,8 +739,8 @@ void work_update (struct connection *c UU, long long msg_id UU) { ...@@ -707,8 +739,8 @@ void work_update (struct connection *c UU, long long msg_id UU) {
break; break;
case CODE_update_user_status: case CODE_update_user_status:
{ {
int user_id = fetch_int (); peer_id_t user_id = MK_USER (fetch_int ());
union user_chat *U = user_chat_get (user_id); peer_t *U = user_chat_get (user_id);
if (U) { if (U) {
fetch_user_status (&U->user.status); fetch_user_status (&U->user.status);
print_start (); print_start ();
...@@ -727,8 +759,8 @@ void work_update (struct connection *c UU, long long msg_id UU) { ...@@ -727,8 +759,8 @@ void work_update (struct connection *c UU, long long msg_id UU) {
break; break;
case CODE_update_user_name: case CODE_update_user_name:
{ {
int user_id = fetch_int (); peer_id_t user_id = MK_USER (fetch_int ());
union user_chat *UC = user_chat_get (user_id); peer_t *UC = user_chat_get (user_id);
if (UC) { if (UC) {
struct user *U = &UC->user; struct user *U = &UC->user;
print_start (); print_start ();
...@@ -770,8 +802,8 @@ void work_update (struct connection *c UU, long long msg_id UU) { ...@@ -770,8 +802,8 @@ void work_update (struct connection *c UU, long long msg_id UU) {
break; break;
case CODE_update_user_photo: case CODE_update_user_photo:
{ {
int user_id = fetch_int (); peer_id_t user_id = MK_USER (fetch_int ());
union user_chat *UC = user_chat_get (user_id); peer_t *UC = user_chat_get (user_id);
if (UC) { if (UC) {
struct user *U = &UC->user; struct user *U = &UC->user;
...@@ -813,23 +845,36 @@ void work_update (struct connection *c UU, long long msg_id UU) { ...@@ -813,23 +845,36 @@ void work_update (struct connection *c UU, long long msg_id UU) {
pop_color (); pop_color ();
print_end (); print_end ();
fetch_skip (n); fetch_skip (n);
fetch_int (); // pts fetch_pts ();
}
break;
case CODE_update_delete_messages:
{
assert (fetch_int () == CODE_vector);
int n = fetch_int ();
print_start ();
push_color (COLOR_YELLOW);
printf ("Deleted %d messages\n", n);
pop_color ();
print_end ();
fetch_skip (n);
fetch_pts ();
} }
break; break;
case CODE_update_chat_participants: case CODE_update_chat_participants:
{ {
assert (fetch_int () == CODE_chat_participants); assert (fetch_int () == CODE_chat_participants);
int chat_id = fetch_int (); peer_id_t chat_id = MK_CHAT (fetch_int ());
fetch_int (); // admin_id fetch_int (); // admin_id
assert (fetch_int () == CODE_vector); assert (fetch_int () == CODE_vector);
int n = fetch_int (); int n = fetch_int ();
fetch_skip (n * 4); fetch_skip (n * 4);
fetch_int (); // version fetch_int (); // version
union user_chat *C = user_chat_get (-chat_id); peer_t *C = user_chat_get (chat_id);
print_start (); print_start ();
push_color (COLOR_YELLOW); push_color (COLOR_YELLOW);
printf ("Chat "); printf ("Chat ");
print_chat_name (-chat_id, C); print_chat_name (chat_id, C);
printf (" changed list: now %d members\n", n); printf (" changed list: now %d members\n", n);
pop_color (); pop_color ();
print_end (); print_end ();
...@@ -837,8 +882,8 @@ void work_update (struct connection *c UU, long long msg_id UU) { ...@@ -837,8 +882,8 @@ void work_update (struct connection *c UU, long long msg_id UU) {
break; break;
case CODE_update_contact_registered: case CODE_update_contact_registered:
{ {
int user_id = fetch_int (); peer_id_t user_id = MK_USER (fetch_int ());
union user_chat *U = user_chat_get (user_id); peer_t *U = user_chat_get (user_id);
fetch_int (); // date fetch_int (); // date
print_start (); print_start ();
push_color (COLOR_YELLOW); push_color (COLOR_YELLOW);
...@@ -851,8 +896,8 @@ void work_update (struct connection *c UU, long long msg_id UU) { ...@@ -851,8 +896,8 @@ void work_update (struct connection *c UU, long long msg_id UU) {
break; break;
case CODE_update_contact_link: case CODE_update_contact_link:
{ {
int user_id = fetch_int (); peer_id_t user_id = MK_USER (fetch_int ());
union user_chat *U = user_chat_get (user_id); peer_t *U = user_chat_get (user_id);
print_start (); print_start ();
push_color (COLOR_YELLOW); push_color (COLOR_YELLOW);
printf ("Updated link with user "); printf ("Updated link with user ");
...@@ -874,8 +919,8 @@ void work_update (struct connection *c UU, long long msg_id UU) { ...@@ -874,8 +919,8 @@ void work_update (struct connection *c UU, long long msg_id UU) {
break; break;
case CODE_update_activation: case CODE_update_activation:
{ {
int user_id = fetch_int (); peer_id_t user_id = MK_USER (fetch_int ());
union user_chat *U = user_chat_get (user_id); peer_t *U = user_chat_get (user_id);
print_start (); print_start ();
push_color (COLOR_YELLOW); push_color (COLOR_YELLOW);
printf ("User "); printf ("User ");
......
...@@ -562,8 +562,8 @@ int get_contacts_on_answer (struct query *q UU) { ...@@ -562,8 +562,8 @@ int get_contacts_on_answer (struct query *q UU) {
struct user *U = fetch_alloc_user (); struct user *U = fetch_alloc_user ();
print_start (); print_start ();
push_color (COLOR_YELLOW); push_color (COLOR_YELLOW);
printf ("User #%d: ", U->id); printf ("User #%d: ", get_peer_id (U->id));
print_user_name (U->id, (union user_chat *)U); print_user_name (U->id, (peer_t *)U);
push_color (COLOR_GREEN); push_color (COLOR_GREEN);
printf (" ("); printf (" (");
printf ("%s", U->print_name); printf ("%s", U->print_name);
...@@ -622,8 +622,8 @@ struct query_methods msg_send_methods = { ...@@ -622,8 +622,8 @@ struct query_methods msg_send_methods = {
int out_message_num; int out_message_num;
int our_id; int our_id;
void out_peer_id (int id); void out_peer_id (peer_id_t id);
void do_send_message (int id, const char *msg, int len) { void do_send_message (peer_id_t id, const char *msg, int len) {
if (!out_message_num) { if (!out_message_num) {
out_message_num = -lrand48 (); out_message_num = -lrand48 ();
} }
...@@ -631,7 +631,7 @@ void do_send_message (int id, const char *msg, int len) { ...@@ -631,7 +631,7 @@ void do_send_message (int id, const char *msg, int len) {
out_int (CODE_messages_send_message); out_int (CODE_messages_send_message);
struct message *M = malloc (sizeof (*M)); struct message *M = malloc (sizeof (*M));
memset (M, 0, sizeof (*M)); memset (M, 0, sizeof (*M));
M->from_id = our_id; M->from_id = MK_USER (our_id);
M->to_id = id; M->to_id = id;
M->unread = 1; M->unread = 1;
out_peer_id (id); out_peer_id (id);
...@@ -649,7 +649,7 @@ void do_send_message (int id, const char *msg, int len) { ...@@ -649,7 +649,7 @@ void do_send_message (int id, const char *msg, int len) {
print_message (M); print_message (M);
} }
void do_send_text (int id, char *file_name) { void do_send_text (peer_id_t id, char *file_name) {
int fd = open (file_name, O_RDONLY); int fd = open (file_name, O_RDONLY);
if (fd < 0) { if (fd < 0) {
rprintf ("No such file '%s'\n", file_name); rprintf ("No such file '%s'\n", file_name);
...@@ -683,7 +683,7 @@ struct query_methods mark_read_methods = { ...@@ -683,7 +683,7 @@ struct query_methods mark_read_methods = {
.on_answer = mark_read_on_receive .on_answer = mark_read_on_receive
}; };
void do_messages_mark_read (int id, int max_id) { void do_messages_mark_read (peer_id_t id, int max_id) {
clear_packet (); clear_packet ();
out_int (CODE_messages_read_history); out_int (CODE_messages_read_history);
out_peer_id (id); out_peer_id (id);
...@@ -726,7 +726,7 @@ int get_history_on_answer (struct query *q UU) { ...@@ -726,7 +726,7 @@ int get_history_on_answer (struct query *q UU) {
fetch_alloc_user (); fetch_alloc_user ();
} }
if (sn > 0) { if (sn > 0) {
do_messages_mark_read ((long)(q->extra), ML[0]->id); do_messages_mark_read (*(peer_id_t *)&(q->extra), ML[0]->id);
} }
return 0; return 0;
} }
...@@ -736,14 +736,14 @@ struct query_methods get_history_methods = { ...@@ -736,14 +736,14 @@ struct query_methods get_history_methods = {
}; };
void do_get_history (int id, int limit) { void do_get_history (peer_id_t id, int limit) {
clear_packet (); clear_packet ();
out_int (CODE_messages_get_history); out_int (CODE_messages_get_history);
out_peer_id (id); out_peer_id (id);
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, (void *)(long)id); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_history_methods, (void *)*(long *)&id);
} }
int get_dialogs_on_answer (struct query *q UU) { int get_dialogs_on_answer (struct query *q UU) {
...@@ -755,14 +755,15 @@ int get_dialogs_on_answer (struct query *q UU) { ...@@ -755,14 +755,15 @@ int get_dialogs_on_answer (struct query *q UU) {
assert (fetch_int () == CODE_vector); assert (fetch_int () == CODE_vector);
int n, i; int n, i;
n = fetch_int (); n = fetch_int ();
static int dlist[3 * 100]; static int dlist[2 * 100];
static peer_id_t plist[100];
int dl_size = n; int dl_size = n;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
assert (fetch_int () == CODE_dialog); assert (fetch_int () == CODE_dialog);
if (i < 100) { if (i < 100) {
dlist[3 * i + 0] = fetch_peer_id (); plist[i] = fetch_peer_id ();
dlist[3 * i + 1] = fetch_int (); dlist[2 * i + 0] = fetch_int ();
dlist[3 * i + 2] = fetch_int (); dlist[2 * i + 1] = fetch_int ();
} else { } else {
fetch_peer_id (); fetch_peer_id ();
fetch_int (); fetch_int ();
...@@ -787,16 +788,20 @@ int get_dialogs_on_answer (struct query *q UU) { ...@@ -787,16 +788,20 @@ int get_dialogs_on_answer (struct query *q UU) {
print_start (); print_start ();
push_color (COLOR_YELLOW); push_color (COLOR_YELLOW);
for (i = dl_size - 1; i >= 0; i--) { for (i = dl_size - 1; i >= 0; i--) {
if (dlist[3 * i] < 0) { peer_t *UC;
union user_chat *UC = user_chat_get (dlist[3 * i]); switch (get_peer_type (plist[i])) {
printf ("Chat "); case PEER_USER:
print_chat_name (dlist[3 * i], UC); UC = user_chat_get (plist[i]);
printf (": %d unread\n", dlist[3 * i + 2]);
} else {
union user_chat *UC = user_chat_get (dlist[3 * i]);
printf ("User "); printf ("User ");
print_user_name (dlist[3 * i], UC); print_user_name (plist[i], UC);
printf (": %d unread\n", dlist[3 * i + 2]); printf (": %d unread\n", dlist[2 * i + 1]);
break;
case PEER_CHAT:
UC = user_chat_get (plist[i]);
printf ("Chat ");
print_chat_name (plist[i], UC);
printf (": %d unread\n", dlist[2 * i + 1]);
break;
} }
} }
pop_color (); pop_color ();
...@@ -825,25 +830,31 @@ struct send_file { ...@@ -825,25 +830,31 @@ struct send_file {
int part_num; int part_num;
int part_size; int part_size;
long long id; long long id;
int to_id; peer_id_t to_id;
int media_type; int media_type;
char *file_name; char *file_name;
}; };
void out_peer_id (int id) { void out_peer_id (peer_id_t id) {
union user_chat *U = user_chat_get (id); peer_t *U;
if (id < 0) { switch (get_peer_type (id)) {
case PEER_CHAT:
out_int (CODE_input_peer_chat); out_int (CODE_input_peer_chat);
out_int (-id); out_int (get_peer_id (id));
} else { break;
case PEER_USER:
U = user_chat_get (id);
if (U && U->user.access_hash) { if (U && U->user.access_hash) {
out_int (CODE_input_peer_foreign); out_int (CODE_input_peer_foreign);
out_int (id); out_int (get_peer_id (id));
out_long (U->user.access_hash); out_long (U->user.access_hash);
} else { } else {
out_int (CODE_input_peer_contact); out_int (CODE_input_peer_contact);
out_int (id); out_int (get_peer_id (id));
} }
break;
default:
assert (0);
} }
} }
...@@ -932,7 +943,7 @@ void send_part (struct send_file *f) { ...@@ -932,7 +943,7 @@ void send_part (struct send_file *f) {
} }
} }
void do_send_photo (int type, int to_id, char *file_name) { void do_send_photo (int type, peer_id_t to_id, char *file_name) {
int fd = open (file_name, O_RDONLY); int fd = open (file_name, O_RDONLY);
if (fd < 0) { if (fd < 0) {
rprintf ("No such file '%s'\n", file_name); rprintf ("No such file '%s'\n", file_name);
...@@ -988,7 +999,7 @@ struct query_methods fwd_msg_methods = { ...@@ -988,7 +999,7 @@ struct query_methods fwd_msg_methods = {
.on_answer = fwd_msg_on_answer .on_answer = fwd_msg_on_answer
}; };
void do_forward_message (int id, int n) { void do_forward_message (peer_id_t id, int n) {
clear_packet (); clear_packet ();
out_int (CODE_invoke_with_layer3); out_int (CODE_invoke_with_layer3);
out_int (CODE_messages_forward_message); out_int (CODE_messages_forward_message);
...@@ -1022,17 +1033,18 @@ struct query_methods rename_chat_methods = { ...@@ -1022,17 +1033,18 @@ struct query_methods rename_chat_methods = {
.on_answer = rename_chat_on_answer .on_answer = rename_chat_on_answer
}; };
void do_rename_chat (int id, char *name) { void do_rename_chat (peer_id_t id, char *name) {
clear_packet (); clear_packet ();
out_int (CODE_messages_edit_chat_title); out_int (CODE_messages_edit_chat_title);
out_int (-id); assert (get_peer_type (id) == PEER_CHAT);
out_int (get_peer_id (id));
out_string (name); out_string (name);
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &rename_chat_methods, 0); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &rename_chat_methods, 0);
} }
int chat_info_on_answer (struct query *q UU) { int chat_info_on_answer (struct query *q UU) {
struct chat *C = fetch_alloc_chat_full (); struct chat *C = fetch_alloc_chat_full ();
union user_chat *U = (void *)C; peer_t *U = (void *)C;
print_start (); print_start ();
push_color (COLOR_YELLOW); push_color (COLOR_YELLOW);
printf ("Chat "); printf ("Chat ");
...@@ -1041,9 +1053,9 @@ int chat_info_on_answer (struct query *q UU) { ...@@ -1041,9 +1053,9 @@ int chat_info_on_answer (struct query *q UU) {
int i; int i;
for (i = 0; i < C->users_num; i++) { for (i = 0; i < C->users_num; i++) {
printf ("\t\t"); printf ("\t\t");
print_user_name (C->users[i].user_id, user_chat_get (C->users[i].user_id)); print_user_name (MK_USER (C->users[i].user_id), user_chat_get (MK_USER (C->users[i].user_id)));
printf (" invited by "); printf (" invited by ");
print_user_name (C->users[i].inviter_id, user_chat_get (C->users[i].inviter_id)); print_user_name (MK_USER (C->users[i].inviter_id), user_chat_get (MK_USER (C->users[i].inviter_id)));
printf (" at "); printf (" at ");
print_date_full (C->users[i].date); print_date_full (C->users[i].date);
if (C->users[i].user_id == C->admin_id) { if (C->users[i].user_id == C->admin_id) {
...@@ -1060,16 +1072,17 @@ struct query_methods chat_info_methods = { ...@@ -1060,16 +1072,17 @@ struct query_methods chat_info_methods = {
.on_answer = chat_info_on_answer .on_answer = chat_info_on_answer
}; };
void do_get_chat_info (int id) { void do_get_chat_info (peer_id_t id) {
clear_packet (); clear_packet ();
out_int (CODE_messages_get_full_chat); out_int (CODE_messages_get_full_chat);
out_int (-id); assert (get_peer_type (id) == PEER_CHAT);
out_int (get_peer_id (id));
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &chat_info_methods, 0); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &chat_info_methods, 0);
} }
int user_info_on_answer (struct query *q UU) { int user_info_on_answer (struct query *q UU) {
struct user *U = fetch_alloc_user_full (); struct user *U = fetch_alloc_user_full ();
union user_chat *C = (void *)U; peer_t *C = (void *)U;
print_start (); print_start ();
push_color (COLOR_YELLOW); push_color (COLOR_YELLOW);
printf ("User "); printf ("User ");
...@@ -1093,17 +1106,18 @@ struct query_methods user_info_methods = { ...@@ -1093,17 +1106,18 @@ struct query_methods user_info_methods = {
.on_answer = user_info_on_answer .on_answer = user_info_on_answer
}; };
void do_get_user_info (int id) { void do_get_user_info (peer_id_t id) {
clear_packet (); clear_packet ();
out_int (CODE_users_get_full_user); out_int (CODE_users_get_full_user);
union user_chat *U = user_chat_get (id); assert (get_peer_type (id) == PEER_USER);
peer_t *U = user_chat_get (id);
if (U && U->user.access_hash) { if (U && U->user.access_hash) {
out_int (CODE_input_user_foreign); out_int (CODE_input_user_foreign);
out_int (id); out_int (get_peer_id (id));
out_long (U->user.access_hash); out_long (U->user.access_hash);
} else { } else {
out_int (CODE_input_user_contact); out_int (CODE_input_user_contact);
out_int (id); out_int (get_peer_id (id));
} }
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &user_info_methods, 0); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &user_info_methods, 0);
} }
...@@ -1387,8 +1401,8 @@ int add_contact_on_answer (struct query *q UU) { ...@@ -1387,8 +1401,8 @@ int add_contact_on_answer (struct query *q UU) {
struct user *U = fetch_alloc_user (); struct user *U = fetch_alloc_user ();
print_start (); print_start ();
push_color (COLOR_YELLOW); push_color (COLOR_YELLOW);
printf ("User #%d: ", U->id); printf ("User #%d: ", get_peer_id (U->id));
print_user_name (U->id, (union user_chat *)U); print_user_name (U->id, (peer_t *)U);
push_color (COLOR_GREEN); push_color (COLOR_GREEN);
printf (" ("); printf (" (");
printf ("%s", U->print_name); printf ("%s", U->print_name);
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "net.h" #include "net.h"
#ifndef __QUERIES_H__ #ifndef __QUERIES_H__
#define __QUERIES_H__ #define __QUERIES_H__
#include "structures.h"
#define QUERY_ACK_RECEIVED 1 #define QUERY_ACK_RECEIVED 1
...@@ -68,16 +69,16 @@ double get_double_time (void); ...@@ -68,16 +69,16 @@ double get_double_time (void);
void do_update_contact_list (void); void do_update_contact_list (void);
union user_chat; union user_chat;
void do_send_message (int id, const char *msg, int len); void do_send_message (peer_id_t id, const char *msg, int len);
void do_send_text (int id, char *file); void do_send_text (peer_id_t id, char *file);
void do_get_history (int id, int limit); void do_get_history (peer_id_t id, 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, peer_id_t to_id, char *file_name);
void do_get_chat_info (int id); void do_get_chat_info (peer_id_t id);
void do_get_user_list_info_silent (int num, int *list); void do_get_user_list_info_silent (int num, int *list);
void do_get_user_info (int id); void do_get_user_info (peer_id_t id);
void do_forward_message (int id, int n); void do_forward_message (peer_id_t id, int n);
void do_rename_chat (int id, char *name); void do_rename_chat (peer_id_t id, char *name);
struct photo; struct photo;
struct video; struct video;
......
...@@ -17,13 +17,14 @@ ...@@ -17,13 +17,14 @@
Copyright Vitaly Valtman 2013 Copyright Vitaly Valtman 2013
*/ */
#include <assert.h> #include <assert.h>
#include <string.h>
#include "structures.h" #include "structures.h"
#include "mtproto-common.h" #include "mtproto-common.h"
#include "telegram.h" #include "telegram.h"
#include "tree.h" #include "tree.h"
#include "loop.h" #include "loop.h"
int verbosity; int verbosity;
union user_chat *Peers[MAX_USER_NUM]; peer_t *Peers[MAX_USER_NUM];
void fetch_file_location (struct file_location *loc) { void fetch_file_location (struct file_location *loc) {
int x = fetch_int (); int x = fetch_int ();
...@@ -67,16 +68,16 @@ int chat_num; ...@@ -67,16 +68,16 @@ int chat_num;
void fetch_user (struct user *U) { void fetch_user (struct user *U) {
unsigned x = fetch_int (); unsigned x = fetch_int ();
assert (x == CODE_user_empty || x == CODE_user_self || x == CODE_user_contact || x == CODE_user_request || x == CODE_user_foreign || x == CODE_user_deleted); assert (x == CODE_user_empty || x == CODE_user_self || x == CODE_user_contact || x == CODE_user_request || x == CODE_user_foreign || x == CODE_user_deleted);
U->id = fetch_int (); U->id = MK_USER (fetch_int ());
U->flags &= ~(FLAG_EMPTY | FLAG_DELETED | FLAG_USER_SELF | FLAG_USER_FOREIGN | FLAG_USER_CONTACT); U->flags &= ~(FLAG_EMPTY | FLAG_DELETED | FLAG_USER_SELF | FLAG_USER_FOREIGN | FLAG_USER_CONTACT);
if (x == CODE_user_empty) { if (x == CODE_user_empty) {
U->flags |= FLAG_EMPTY; U->flags |= FLAG_EMPTY;
return; return;
} }
if (x == CODE_user_self) { if (x == CODE_user_self) {
assert (!our_id || (our_id == U->id)); assert (!our_id || (our_id == get_peer_id (U->id)));
if (!our_id) { if (!our_id) {
our_id = U->id; our_id = get_peer_id (U->id);
write_auth_file (); write_auth_file ();
} }
} }
...@@ -226,7 +227,7 @@ void fetch_user_full (struct user *U) { ...@@ -226,7 +227,7 @@ void fetch_user_full (struct user *U) {
void fetch_chat (struct chat *C) { void fetch_chat (struct chat *C) {
unsigned x = fetch_int (); unsigned x = fetch_int ();
assert (x == CODE_chat_empty || x == CODE_chat || x == CODE_chat_forbidden); assert (x == CODE_chat_empty || x == CODE_chat || x == CODE_chat_forbidden);
C->id = -fetch_int (); C->id = MK_CHAT (fetch_int ());
C->flags &= ~(FLAG_EMPTY | FLAG_DELETED | FLAG_FORBIDDEN | FLAG_CHAT_IN_CHAT); C->flags &= ~(FLAG_EMPTY | FLAG_DELETED | FLAG_FORBIDDEN | FLAG_CHAT_IN_CHAT);
if (x == CODE_chat_empty) { if (x == CODE_chat_empty) {
C->flags |= FLAG_EMPTY; C->flags |= FLAG_EMPTY;
...@@ -292,11 +293,11 @@ void fetch_chat_full (struct chat *C) { ...@@ -292,11 +293,11 @@ void fetch_chat_full (struct chat *C) {
unsigned x = fetch_int (); unsigned x = fetch_int ();
assert (x == CODE_messages_chat_full); assert (x == CODE_messages_chat_full);
assert (fetch_int () == CODE_chat_full); assert (fetch_int () == CODE_chat_full);
C->id = -fetch_int (); C->id = MK_CHAT (fetch_int ());
C->flags &= ~(FLAG_EMPTY | FLAG_DELETED | FLAG_FORBIDDEN | FLAG_CHAT_IN_CHAT); C->flags &= ~(FLAG_EMPTY | FLAG_DELETED | FLAG_FORBIDDEN | FLAG_CHAT_IN_CHAT);
x = fetch_int (); x = fetch_int ();
if (x == CODE_chat_participants) { if (x == CODE_chat_participants) {
assert (fetch_int () == -C->id); assert (fetch_int () == get_peer_id (C->id));
C->admin_id = fetch_int (); C->admin_id = fetch_int ();
assert (fetch_int () == CODE_vector); assert (fetch_int () == CODE_vector);
if (C->users) { if (C->users) {
...@@ -440,7 +441,8 @@ void fetch_message_action (struct message_action *M) { ...@@ -440,7 +441,8 @@ void fetch_message_action (struct message_action *M) {
void fetch_message_short (struct message *M) { void fetch_message_short (struct message *M) {
memset (M, 0, sizeof (*M)); memset (M, 0, sizeof (*M));
M->id = fetch_int (); M->id = fetch_int ();
M->from_id = fetch_int (); M->to_id = MK_USER (our_id);
M->from_id = MK_USER (fetch_int ());
M->message = fetch_str_dup (); M->message = fetch_str_dup ();
fetch_int (); // pts fetch_int (); // pts
M->date = fetch_int (); M->date = fetch_int ();
...@@ -452,8 +454,8 @@ void fetch_message_short (struct message *M) { ...@@ -452,8 +454,8 @@ void fetch_message_short (struct message *M) {
void fetch_message_short_chat (struct message *M) { void fetch_message_short_chat (struct message *M) {
memset (M, 0, sizeof (*M)); memset (M, 0, sizeof (*M));
M->id = fetch_int (); M->id = fetch_int ();
M->from_id = fetch_int (); M->from_id = MK_USER (fetch_int ());
M->to_id = -fetch_int (); M->to_id = MK_CHAT (fetch_int ());
M->message = fetch_str_dup (); M->message = fetch_str_dup ();
fetch_int (); // pts fetch_int (); // pts
M->date = fetch_int (); M->date = fetch_int ();
...@@ -493,13 +495,13 @@ void fetch_message_media (struct message_media *M) { ...@@ -493,13 +495,13 @@ void fetch_message_media (struct message_media *M) {
} }
} }
int fetch_peer_id (void) { peer_id_t fetch_peer_id (void) {
unsigned x =fetch_int (); unsigned x =fetch_int ();
if (x == CODE_peer_user) { if (x == CODE_peer_user) {
return fetch_int (); return MK_USER (fetch_int ());
} else { } else {
assert (CODE_peer_chat); assert (CODE_peer_chat);
return -fetch_int (); return MK_CHAT (fetch_int ());
} }
} }
...@@ -513,10 +515,10 @@ void fetch_message (struct message *M) { ...@@ -513,10 +515,10 @@ void fetch_message (struct message *M) {
return; return;
} }
if (x == CODE_message_forwarded) { if (x == CODE_message_forwarded) {
M->fwd_from_id = fetch_int (); M->fwd_from_id = MK_USER (fetch_int ());
M->fwd_date = fetch_int (); M->fwd_date = fetch_int ();
} }
M->from_id = fetch_int (); M->from_id = MK_USER (fetch_int ());
M->to_id = fetch_peer_id (); M->to_id = fetch_peer_id ();
M->out = fetch_bool (); M->out = fetch_bool ();
M->unread = fetch_bool (); M->unread = fetch_bool ();
...@@ -530,10 +532,11 @@ void fetch_message (struct message *M) { ...@@ -530,10 +532,11 @@ void fetch_message (struct message *M) {
} }
} }
#define user_cmp(a,b) ((a)->id - (b)->id) #define id_cmp(a,b) ((a)->id - (b)->id)
#define peer_cmp(a,b) (cmp_peer_id (a->id, b->id))
DEFINE_TREE(peer,union user_chat *,user_cmp,0) DEFINE_TREE(peer,peer_t *,peer_cmp,0)
DEFINE_TREE(message,struct message *,user_cmp,0) DEFINE_TREE(message,struct message *,id_cmp,0)
struct tree_peer *peer_tree; struct tree_peer *peer_tree;
struct tree_message *message_tree; struct tree_message *message_tree;
...@@ -551,7 +554,7 @@ struct message message_list = { ...@@ -551,7 +554,7 @@ struct message message_list = {
struct user *fetch_alloc_user (void) { struct user *fetch_alloc_user (void) {
int data[2]; int data[2];
prefetch_data (data, 8); prefetch_data (data, 8);
union user_chat *U = user_chat_get (data[1]); peer_t *U = user_chat_get (MK_USER (data[1]));
if (U) { if (U) {
fetch_user (&U->user); fetch_user (&U->user);
return &U->user; return &U->user;
...@@ -569,7 +572,7 @@ struct user *fetch_alloc_user (void) { ...@@ -569,7 +572,7 @@ struct user *fetch_alloc_user (void) {
struct user *fetch_alloc_user_full (void) { struct user *fetch_alloc_user_full (void) {
int data[3]; int data[3];
prefetch_data (data, 12); prefetch_data (data, 12);
union user_chat *U = user_chat_get (data[2]); peer_t *U = user_chat_get (MK_USER (data[2]));
if (U) { if (U) {
fetch_user_full (&U->user); fetch_user_full (&U->user);
return &U->user; return &U->user;
...@@ -577,7 +580,7 @@ struct user *fetch_alloc_user_full (void) { ...@@ -577,7 +580,7 @@ struct user *fetch_alloc_user_full (void) {
users_allocated ++; users_allocated ++;
U = malloc (sizeof (*U)); U = malloc (sizeof (*U));
memset (U, 0, sizeof (*U)); memset (U, 0, sizeof (*U));
U->id = data[2]; U->id = MK_USER (data[2]);
peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); peer_tree = tree_insert_peer (peer_tree, U, lrand48 ());
fetch_user_full (&U->user); fetch_user_full (&U->user);
Peers[chat_num + (user_num ++ )] = U; Peers[chat_num + (user_num ++ )] = U;
...@@ -754,7 +757,7 @@ struct message *fetch_alloc_message_short_chat (void) { ...@@ -754,7 +757,7 @@ struct message *fetch_alloc_message_short_chat (void) {
struct chat *fetch_alloc_chat (void) { struct chat *fetch_alloc_chat (void) {
int data[2]; int data[2];
prefetch_data (data, 8); prefetch_data (data, 8);
union user_chat *U = user_chat_get (-data[1]); peer_t *U = user_chat_get (MK_CHAT (data[1]));
if (U) { if (U) {
fetch_chat (&U->chat); fetch_chat (&U->chat);
return &U->chat; return &U->chat;
...@@ -772,7 +775,7 @@ struct chat *fetch_alloc_chat (void) { ...@@ -772,7 +775,7 @@ struct chat *fetch_alloc_chat (void) {
struct chat *fetch_alloc_chat_full (void) { struct chat *fetch_alloc_chat_full (void) {
int data[3]; int data[3];
prefetch_data (data, 12); prefetch_data (data, 12);
union user_chat *U = user_chat_get (-data[2]); peer_t *U = user_chat_get (MK_CHAT (data[2]));
if (U) { if (U) {
fetch_chat_full (&U->chat); fetch_chat_full (&U->chat);
return &U->chat; return &U->chat;
...@@ -780,7 +783,7 @@ struct chat *fetch_alloc_chat_full (void) { ...@@ -780,7 +783,7 @@ struct chat *fetch_alloc_chat_full (void) {
chats_allocated ++; chats_allocated ++;
U = malloc (sizeof (*U)); U = malloc (sizeof (*U));
memset (U, 0, sizeof (*U)); memset (U, 0, sizeof (*U));
U->id = -data[2]; U->id = MK_CHAT (data[2]);
peer_tree = tree_insert_peer (peer_tree, U, lrand48 ()); peer_tree = tree_insert_peer (peer_tree, U, lrand48 ());
fetch_chat_full (&U->chat); fetch_chat_full (&U->chat);
Peers[(chat_num ++) + user_num] = U; Peers[(chat_num ++) + user_num] = U;
...@@ -804,8 +807,8 @@ int print_stat (char *s, int len) { ...@@ -804,8 +807,8 @@ int print_stat (char *s, int len) {
); );
} }
union user_chat *user_chat_get (int id) { peer_t *user_chat_get (peer_id_t id) {
union user_chat U; peer_t U;
U.id = id; U.id = id;
return tree_lookup_peer (peer_tree, &U); return tree_lookup_peer (peer_tree, &U);
} }
......
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
#ifndef __STRUCTURES_H__ #ifndef __STRUCTURES_H__
#define __STRUCTURES_H__ #define __STRUCTURES_H__
#include <assert.h>
typedef struct { int id; } peer_id_t;
#define FLAG_EMPTY 1 #define FLAG_EMPTY 1
#define FLAG_DELETED 2 #define FLAG_DELETED 2
#define FLAG_FORBIDDEN 4 #define FLAG_FORBIDDEN 4
...@@ -32,6 +35,7 @@ ...@@ -32,6 +35,7 @@
#define FLAG_CHAT_IN_CHAT 128 #define FLAG_CHAT_IN_CHAT 128
struct file_location { struct file_location {
int dc; int dc;
long long volume; long long volume;
...@@ -70,7 +74,7 @@ struct user_status { ...@@ -70,7 +74,7 @@ struct user_status {
}; };
struct user { struct user {
int id; peer_id_t id;
int flags; int flags;
char *print_name; char *print_name;
struct file_location photo_big; struct file_location photo_big;
...@@ -93,7 +97,7 @@ struct chat_user { ...@@ -93,7 +97,7 @@ struct chat_user {
}; };
struct chat { struct chat {
int id; peer_id_t id;
int flags; int flags;
char *print_title; char *print_title;
struct file_location photo_big; struct file_location photo_big;
...@@ -107,9 +111,9 @@ struct chat { ...@@ -107,9 +111,9 @@ struct chat {
int admin_id; int admin_id;
}; };
union user_chat { typedef union peer {
struct { struct {
int id; peer_id_t id;
int flags; int flags;
char *print_name; char *print_name;
struct file_location photo_big; struct file_location photo_big;
...@@ -118,7 +122,7 @@ union user_chat { ...@@ -118,7 +122,7 @@ union user_chat {
}; };
struct user user; struct user user;
struct chat chat; struct chat chat;
}; } peer_t;
struct video { struct video {
long long id; long long id;
...@@ -168,10 +172,10 @@ struct message { ...@@ -168,10 +172,10 @@ struct message {
struct message *next_use, *prev_use; struct message *next_use, *prev_use;
int id; int id;
int flags; int flags;
int fwd_from_id; peer_id_t fwd_from_id;
int fwd_date; int fwd_date;
int from_id; peer_id_t from_id;
int to_id; peer_id_t to_id;
int out; int out;
int unread; int unread;
int date; int date;
...@@ -196,16 +200,64 @@ struct chat *fetch_alloc_chat_full (void); ...@@ -196,16 +200,64 @@ struct chat *fetch_alloc_chat_full (void);
struct message *fetch_alloc_message (void); struct message *fetch_alloc_message (void);
struct message *fetch_alloc_message_short (void); struct message *fetch_alloc_message_short (void);
struct message *fetch_alloc_message_short_chat (void); struct message *fetch_alloc_message_short_chat (void);
int fetch_peer_id (void); peer_id_t fetch_peer_id (void);
void free_user (struct user *U); void free_user (struct user *U);
void free_chat (struct chat *U); void free_chat (struct chat *U);
int print_stat (char *s, int len); int print_stat (char *s, int len);
union user_chat *user_chat_get (int id); peer_t *user_chat_get (peer_id_t 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); void message_insert (struct message *M);
void free_photo (struct photo *P); void free_photo (struct photo *P);
void fetch_photo (struct photo *P); void fetch_photo (struct photo *P);
#define PEER_USER 1
#define PEER_CHAT 2
#define PEER_UNKNOWN 0
#define MK_USER(id) set_peer_id (PEER_USER,id)
#define MK_CHAT(id) set_peer_id (PEER_CHAT,id)
static inline int get_peer_type (peer_id_t id) {
if (id.id > 0) {
return PEER_USER;
}
if (id.id < 0) {
return PEER_CHAT;
}
return PEER_UNKNOWN;
}
static inline int get_peer_id (peer_id_t id) {
switch (get_peer_type (id)) {
case PEER_USER:
return id.id;
case PEER_CHAT:
return -id.id;
default:
return 0;
}
}
static inline peer_id_t set_peer_id (int type, int id) {
peer_id_t ID;
switch (type) {
case PEER_USER:
ID.id = id;
return ID;
case PEER_CHAT:
ID.id = -id;
return ID;
default:
assert (0);
return ID;
}
}
static inline int cmp_peer_id (peer_id_t a, peer_id_t b) {
return memcmp (&a, &b, sizeof (a));
}
#endif #endif
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