Commit 725c6669 authored by Vysheng's avatar Vysheng

Manty fixes. Mostly binlog for messages

parent d095e607
This diff is collapsed.
...@@ -51,6 +51,18 @@ ...@@ -51,6 +51,18 @@
#define CODE_binlog_chat_full_photo 0x6cca6629 #define CODE_binlog_chat_full_photo 0x6cca6629
#define CODE_binlog_add_chat_participant 0x63345108 #define CODE_binlog_add_chat_participant 0x63345108
#define CODE_binlog_del_chat_participant 0x82d1f0ee #define CODE_binlog_del_chat_participant 0x82d1f0ee
#define CODE_binlog_create_message_text 0x269acd5b
#define CODE_binlog_create_message_text_fwd 0xa3d864cd
#define CODE_binlog_create_message_service 0xbbe5e94b
#define CODE_binlog_create_message_service_fwd 0xea9c57ae
#define CODE_binlog_create_message_media 0x62a92d19
#define CODE_binlog_create_message_media_fwd 0xbefdc462
#define CODE_binlog_send_message_text 0x31cfd652
#define CODE_binlog_set_unread 0x21d4c909
#define CODE_binlog_set_message_sent 0xc335282b
#define CODE_binlog_set_msg_id 0xf3285b6a
#define CODE_binlog_create_message_media_encr 0x19cd7c9d
#define CODE_binlog_create_message_service_encr 0x8b4b9395
void *alloc_log_event (int l); void *alloc_log_event (int l);
void replay_log (void); void replay_log (void);
...@@ -102,4 +114,17 @@ void bl_do_set_chat_participants (struct chat *C, int version, int user_num, str ...@@ -102,4 +114,17 @@ void bl_do_set_chat_participants (struct chat *C, int version, int user_num, str
void bl_do_set_chat_full_photo (struct chat *U, const int *start, int len); void bl_do_set_chat_full_photo (struct chat *U, const int *start, int len);
void bl_do_chat_add_user (struct chat *C, int version, int user, int inviter, int date); void bl_do_chat_add_user (struct chat *C, int version, int user, int inviter, int date);
void bl_do_chat_del_user (struct chat *C, int version, int user); void bl_do_chat_del_user (struct chat *C, int version, int user);
void bl_do_create_message_text (int msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s);
void bl_do_create_message_text_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int l, const char *s);
void bl_do_create_message_service (int msg_id, int from_id, int to_type, int to_id, int date, const int *data, int len);
void bl_do_create_message_service_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, const int *data, int len);
void bl_do_create_message_media (int msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len);
void bl_do_create_message_media_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int l, const char *s, const int *data, int len);
void bl_do_create_message_media_encr (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len, const int *data2, int len2);
void bl_do_create_message_service_encr (long long msg_id, int from_id, int to_type, int to_id, int date, const int *data, int len);
void bl_do_send_message_text (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s);
void bl_do_set_unread (struct message *M, int unread);
void bl_do_set_message_sent (struct message *M);
void bl_do_set_msg_id (struct message *M, int id);
#endif #endif
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
#include "mtproto-common.h" #include "mtproto-common.h"
//#define ALLOW_MULT 1 #define ALLOW_MULT 1
char *default_prompt = "> "; char *default_prompt = "> ";
int unread_messages; int unread_messages;
...@@ -1096,6 +1096,7 @@ void pop_color (void) { ...@@ -1096,6 +1096,7 @@ void pop_color (void) {
void print_media (struct message_media *M) { void print_media (struct message_media *M) {
switch (M->type) { switch (M->type) {
case CODE_message_media_empty: case CODE_message_media_empty:
case CODE_decrypted_message_media_empty:
return; return;
case CODE_message_media_photo: case CODE_message_media_photo:
if (M->photo.caption && strlen (M->photo.caption)) { if (M->photo.caption && strlen (M->photo.caption)) {
......
...@@ -436,7 +436,10 @@ int new_dc_num; ...@@ -436,7 +436,10 @@ int new_dc_num;
int loop (void) { int loop (void) {
on_start (); on_start ();
if (binlog_enabled) { if (binlog_enabled) {
double t = get_double_time ();
logprintf ("replay log start\n");
replay_log (); replay_log ();
logprintf ("replay log end in %lf seconds\n", get_double_time () - t);
write_binlog (); write_binlog ();
} else { } else {
read_auth_file (); read_auth_file ();
...@@ -571,6 +574,8 @@ int loop (void) { ...@@ -571,6 +574,8 @@ int loop (void) {
do_get_difference (); do_get_difference ();
net_loop (0, dgot); net_loop (0, dgot);
send_all_unsent ();
do_get_dialog_list (); do_get_dialog_list ();
return main_loop (); return main_loop ();
......
...@@ -789,7 +789,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { ...@@ -789,7 +789,7 @@ void work_update (struct connection *c UU, long long msg_id UU) {
int new = fetch_long (); // random_id int new = fetch_long (); // random_id
struct message *M = message_get (new); struct message *M = message_get (new);
if (M) { if (M) {
update_message_id (M, id); bl_do_set_msg_id (M, id);
} }
} }
break; break;
...@@ -802,7 +802,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { ...@@ -802,7 +802,7 @@ void work_update (struct connection *c UU, long long msg_id UU) {
int id = fetch_int (); int id = fetch_int ();
struct message *M = message_get (id); struct message *M = message_get (id);
if (M) { if (M) {
M->unread = 0; bl_do_set_unread (M, 0);
} }
} }
fetch_pts (); fetch_pts ();
...@@ -885,7 +885,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { ...@@ -885,7 +885,7 @@ void work_update (struct connection *c UU, long long msg_id UU) {
int l2 = prefetch_strlen (); int l2 = prefetch_strlen ();
char *l = fetch_str (l2); char *l = fetch_str (l2);
struct user *U = &UC->user; struct user *U = &UC->user;
bl_do_set_user_name (U, f, l1, l, l2); bl_do_set_user_real_name (U, f, l1, l, l2);
print_start (); print_start ();
push_color (COLOR_YELLOW); push_color (COLOR_YELLOW);
print_date (time (0)); print_date (time (0));
......
...@@ -226,7 +226,7 @@ long long compute_rsa_key_fingerprint (RSA *key); ...@@ -226,7 +226,7 @@ long long compute_rsa_key_fingerprint (RSA *key);
extern int *packet_buffer; extern int *packet_buffer;
extern int *packet_ptr; extern int *packet_ptr;
static inline void out_ints (int *what, int len) { static inline void out_ints (const int *what, int len) {
assert (packet_ptr + len <= packet_buffer + PACKET_BUFFER_SIZE); assert (packet_ptr + len <= packet_buffer + PACKET_BUFFER_SIZE);
memcpy (packet_ptr, what, len * 4); memcpy (packet_ptr, what, len * 4);
packet_ptr += len; packet_ptr += len;
......
...@@ -779,8 +779,9 @@ int msg_send_encr_on_answer (struct query *q UU) { ...@@ -779,8 +779,9 @@ int msg_send_encr_on_answer (struct query *q UU) {
assert (fetch_int () == CODE_messages_sent_encrypted_message); assert (fetch_int () == CODE_messages_sent_encrypted_message);
rprintf ("Sent\n"); rprintf ("Sent\n");
struct message *M = q->extra; struct message *M = q->extra;
M->date = fetch_int (); //M->date = fetch_int ();
message_insert (M); fetch_int ();
bl_do_set_message_sent (M);
return 0; return 0;
} }
...@@ -788,12 +789,11 @@ int msg_send_on_answer (struct query *q UU) { ...@@ -788,12 +789,11 @@ int msg_send_on_answer (struct query *q UU) {
unsigned x = fetch_int (); unsigned x = fetch_int ();
assert (x == CODE_messages_sent_message || x == CODE_messages_sent_message_link); assert (x == CODE_messages_sent_message || x == CODE_messages_sent_message_link);
int id = fetch_int (); // id int id = fetch_int (); // id
struct message *M = q->extra;
bl_do_set_msg_id (M, id);
fetch_date (); fetch_date ();
fetch_pts (); fetch_pts ();
fetch_seq (); fetch_seq ();
struct message *M = q->extra;
M->id = id;
message_insert (M);
if (x == CODE_messages_sent_message_link) { if (x == CODE_messages_sent_message_link) {
assert (fetch_int () == CODE_vector); assert (fetch_int () == CODE_vector);
int n = fetch_int (); int n = fetch_int ();
...@@ -834,6 +834,7 @@ int msg_send_on_answer (struct query *q UU) { ...@@ -834,6 +834,7 @@ int msg_send_on_answer (struct query *q UU) {
} }
} }
rprintf ("Sent: id = %d\n", id); rprintf ("Sent: id = %d\n", id);
bl_do_set_message_sent (M);
return 0; return 0;
} }
...@@ -848,86 +849,62 @@ struct query_methods msg_send_encr_methods = { ...@@ -848,86 +849,62 @@ struct query_methods msg_send_encr_methods = {
int out_message_num; int out_message_num;
int our_id; int our_id;
void do_send_encr_message (peer_id_t id, const char *msg, int len) { void do_send_encr_msg (struct message *M) {
peer_t *P = user_chat_get (id); peer_t *P = user_chat_get (M->to_id);
if (!P) { if (!P || P->encr_chat.state != sc_ok) { return; }
logprintf ("Can not send to unknown encrypted chat\n");
return;
}
if (P->encr_chat.state != sc_ok) {
logprintf ("Chat is not yet initialized\n");
return;
}
clear_packet (); clear_packet ();
out_int (CODE_messages_send_encrypted); out_int (CODE_messages_send_encrypted);
out_int (CODE_input_encrypted_chat); out_int (CODE_input_encrypted_chat);
out_int (get_peer_id (id)); out_int (get_peer_id (M->to_id));
out_long (P->encr_chat.access_hash); out_long (P->encr_chat.access_hash);
if (!out_message_num) { out_long (M->id);
out_message_num = -lrand48 ();
}
out_long ((--out_message_num) - (4ll << 32));
encr_start (); encr_start ();
//out_int (CODE_decrypted_message_layer);
//out_int (8);
out_int (CODE_decrypted_message); out_int (CODE_decrypted_message);
out_long ((out_message_num) - (4ll << 32)); out_long (M->id);
static int buf[4]; static int buf[4];
int i; int i;
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
buf[i] = mrand48 (); buf[i] = mrand48 ();
} }
out_cstring ((void *)buf, 16); out_cstring ((void *)buf, 16);
out_cstring ((void *)msg, len); out_cstring ((void *)M->message, M->message_len);
out_int (CODE_decrypted_message_media_empty); out_int (CODE_decrypted_message_media_empty);
encr_finish (&P->encr_chat); encr_finish (&P->encr_chat);
struct message *M = malloc (sizeof (*M));
memset (M, 0, sizeof (*M));
M->flags = FLAG_ENCRYPTED;
M->from_id = MK_USER (our_id);
M->to_id = id;
M->unread = 1;
M->message = malloc (len + 1);
memcpy (M->message, msg, len);
M->message[len] = 0;
M->message_len = len;
M->out = 1;
M->media.type = CODE_message_media_empty;
M->id = (out_message_num) - (4ll << 32);
M->date = time (0);
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M);
print_message (M);
} }
void do_send_message (peer_id_t id, const char *msg, int len) { void do_send_msg (struct message *M) {
if (get_peer_type (id) == PEER_ENCR_CHAT) { if (get_peer_type (M->to_id) == PEER_ENCR_CHAT) {
do_send_encr_message (id, msg, len); do_send_encr_msg (M);
return; return;
} }
if (!out_message_num) {
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)); out_peer_id (M->to_id);
memset (M, 0, sizeof (*M)); out_cstring (M->message, M->message_len);
M->from_id = MK_USER (our_id); out_long (M->id);
M->to_id = id;
M->unread = 1;
out_peer_id (id);
M->message = malloc (len + 1);
memcpy (M->message, msg, len);
M->message[len] = 0;
M->message_len = len;
M->out = 1;
M->media.type = CODE_message_media_empty;
M->id = out_message_num;
M->date = time (0);
out_cstring (msg, len);
out_long ((--out_message_num) - (1ll << 32));
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_methods, M); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_methods, M);
}
void do_send_message (peer_id_t id, const char *msg, int len) {
if (get_peer_type (id) == PEER_ENCR_CHAT) {
peer_t *P = user_chat_get (id);
if (!P) {
logprintf ("Can not send to unknown encrypted chat\n");
return;
}
if (P->encr_chat.state != sc_ok) {
logprintf ("Chat is not yet initialized\n");
return;
}
}
long long t = -lrand48 () * (1ll << 32) - lrand48 ();
bl_do_send_message_text (t, our_id, get_peer_type (id), get_peer_id (id), time (0), len, msg);
struct message *M = message_get (t);
assert (M);
do_send_msg (M);
print_message (M); print_message (M);
} }
/* }}} */ /* }}} */
...@@ -1070,6 +1047,7 @@ void do_get_local_history (peer_id_t id, int limit) { ...@@ -1070,6 +1047,7 @@ void do_get_local_history (peer_id_t id, int limit) {
if (!P || !P->last) { return; } if (!P || !P->last) { return; }
struct message *M = P->last; struct message *M = P->last;
int count = 1; int count = 1;
assert (!M->prev);
while (count < limit && M->next) { while (count < limit && M->next) {
M = M->next; M = M->next;
count ++; count ++;
...@@ -2295,6 +2273,9 @@ void do_create_keys_end (struct secret_chat *U) { ...@@ -2295,6 +2273,9 @@ void do_create_keys_end (struct secret_chat *U) {
BIGNUM *a = BN_bin2bn ((void *)U->key, 256, 0); BIGNUM *a = BN_bin2bn ((void *)U->key, 256, 0);
BN_init (r); BN_init (r);
BN_mod_exp (r, g_b, a, p, ctx); BN_mod_exp (r, g_b, a, p, ctx);
void *t = malloc (256);
memcpy (t, U->key, 256);
memset (U->key, 0, sizeof (U->key)); memset (U->key, 0, sizeof (U->key));
BN_bn2bin (r, (void *)U->key); BN_bn2bin (r, (void *)U->key);
...@@ -2306,7 +2287,15 @@ void do_create_keys_end (struct secret_chat *U) { ...@@ -2306,7 +2287,15 @@ void do_create_keys_end (struct secret_chat *U) {
static unsigned char sha_buffer[20]; static unsigned char sha_buffer[20];
sha1 ((void *)U->key, 256, sha_buffer); sha1 ((void *)U->key, 256, sha_buffer);
long long k = *(long long *)(sha_buffer + 12); long long k = *(long long *)(sha_buffer + 12);
assert (k == U->key_fingerprint); if (k != U->key_fingerprint) {
logprintf ("version = %d\n", encr_param_version);
hexdump ((void *)U->nonce, (void *)(U->nonce + 256));
hexdump ((void *)U->g_key, (void *)(U->g_key + 256));
hexdump ((void *)U->key, (void *)(U->key + 64));
hexdump ((void *)t, (void *)(t + 256));
logprintf ("!!Key fingerprint mismatch\n");
U->state = sc_deleted;
}
BN_clear_free (p); BN_clear_free (p);
BN_clear_free (g_b); BN_clear_free (g_b);
......
...@@ -107,6 +107,7 @@ void do_add_user_to_chat (peer_id_t chat_id, peer_id_t id, int limit); ...@@ -107,6 +107,7 @@ void do_add_user_to_chat (peer_id_t chat_id, peer_id_t id, int limit);
void do_del_user_from_chat (peer_id_t chat_id, peer_id_t id); void do_del_user_from_chat (peer_id_t chat_id, peer_id_t id);
void do_update_status (int online); void do_update_status (int online);
void do_contacts_search (int limit, const char *s); void do_contacts_search (int limit, const char *s);
void do_send_msg (struct message *M);
// For binlog // For binlog
......
This diff is collapsed.
...@@ -38,6 +38,7 @@ typedef struct { int type; int id; } peer_id_t; ...@@ -38,6 +38,7 @@ typedef struct { int type; int id; } peer_id_t;
#define FLAG_CHAT_IN_CHAT 128 #define FLAG_CHAT_IN_CHAT 128
#define FLAG_ENCRYPTED 4096 #define FLAG_ENCRYPTED 4096
#define FLAG_PENDING 8192
struct file_location { struct file_location {
int dc; int dc;
...@@ -337,8 +338,16 @@ struct message *fetch_alloc_message_short (void); ...@@ -337,8 +338,16 @@ struct message *fetch_alloc_message_short (void);
struct message *fetch_alloc_message_short_chat (void); struct message *fetch_alloc_message_short_chat (void);
struct message *fetch_alloc_encrypted_message (void); struct message *fetch_alloc_encrypted_message (void);
void fetch_encrypted_message_file (struct message_media *M); void fetch_encrypted_message_file (struct message_media *M);
void fetch_skip_encrypted_message_file (void);
void fetch_encrypted_message_file (struct message_media *M);
void fetch_message_action_encrypted (struct message_action *M);
peer_id_t fetch_peer_id (void); peer_id_t fetch_peer_id (void);
void fetch_message_media (struct message_media *M);
void fetch_message_media_encrypted (struct message_media *M);
void fetch_message_action (struct message_action *M);
void message_insert_tree (struct message *M);
void free_user (struct user *U); void free_user (struct user *U);
void free_chat (struct chat *U); void free_chat (struct chat *U);
...@@ -356,6 +365,12 @@ void insert_user (peer_t *P); ...@@ -356,6 +365,12 @@ void insert_user (peer_t *P);
void insert_chat (peer_t *P); void insert_chat (peer_t *P);
void fetch_photo (struct photo *P); void fetch_photo (struct photo *P);
void free_photo (struct photo *P); void free_photo (struct photo *P);
void message_insert_unsent (struct message *M);
void message_remove_unsent (struct message *M);
void send_all_unsent (void);
void message_remove_tree (struct message *M);
void message_add_peer (struct message *M);
void message_del_peer (struct message *M);
#define PEER_USER 1 #define PEER_USER 1
#define PEER_CHAT 2 #define PEER_CHAT 2
......
...@@ -17,5 +17,5 @@ ...@@ -17,5 +17,5 @@
Copyright Vitaly Valtman 2013 Copyright Vitaly Valtman 2013
*/ */
#define MAX_DC_NUM 9 #define MAX_DC_NUM 9
#define MAX_USER_NUM 1000 #define MAX_USER_NUM 100000
#define MAX_CHAT_NUM 1000 #define MAX_CHAT_NUM 100000
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