Move binlog globals into a structure

This commit is contained in:
mjentsch 2014-08-22 17:05:29 +02:00
parent 331d631659
commit 32799a9086
8 changed files with 574 additions and 515 deletions

789
binlog.c

File diff suppressed because it is too large Load diff

111
binlog.h
View file

@ -84,69 +84,68 @@
#define CODE_binlog_create_message_service_encr 0x8b4b9395
#define CODE_binlog_delete_msg 0xa1d6ab6d
void *alloc_log_event (int l);
void *alloc_log_event (struct binlog *bl, int l);
void replay_log (struct telegram *instance);
void add_log_event (struct mtproto_connection *self, const int *data, int l);
void add_log_event_i (struct mtproto_connection *self, struct telegram *instance, const int *data, int l);
void write_binlog (void);
void add_log_event (struct binlog *bl, struct mtproto_connection *self, const int *data, int l);
void write_binlog (struct binlog *bl);
void bl_do_set_auth_key_id (struct telegram *instance, int num, unsigned char *buf);
void bl_do_dc_option (struct mtproto_connection *self, int id, int l1, const char *name, int l2, const char *ip, int port, struct telegram *instance);
void bl_do_dc_option (struct binlog *bl, struct mtproto_connection *self, int id, int l1, const char *name, int l2, const char *ip, int port, struct telegram *instance);
void bl_do_set_our_id (struct mtproto_connection *self, int id);
void bl_do_new_user (struct mtproto_connection *self, int id, const char *f, int fl, const char *l, int ll, long long access_token, const char *p, int pl, int contact);
void bl_do_user_delete (struct mtproto_connection *self, struct user *U);
void bl_do_set_user_profile_photo (struct mtproto_connection *self, struct user *U, long long photo_id, struct file_location *big, struct file_location *small);
void bl_do_set_user_name (struct mtproto_connection *self, struct user *U, const char *f, int fl, const char *l, int ll);
void bl_do_set_user_access_token (struct mtproto_connection *self, struct user *U, long long access_token);
void bl_do_set_user_phone (struct mtproto_connection *self, struct user *U, const char *p, int pl);
void bl_do_set_user_friend (struct mtproto_connection *self, struct user *U, int friend);
void bl_do_set_user_full_photo (struct mtproto_connection *self, struct user *U, const int *start, int len);
void bl_do_set_user_blocked (struct mtproto_connection *self, struct user *U, int blocked);
void bl_do_set_user_real_name (struct mtproto_connection *self, struct user *U, const char *f, int fl, const char *l, int ll);
void bl_do_set_our_id (struct binlog *bl, struct mtproto_connection *self, int id);
void bl_do_new_user (struct binlog *bl, struct mtproto_connection *self, int id, const char *f, int fl, const char *l, int ll, long long access_token, const char *p, int pl, int contact);
void bl_do_user_delete (struct binlog *bl, struct mtproto_connection *self, struct user *U);
void bl_do_set_user_profile_photo (struct binlog *bl, struct mtproto_connection *self, struct user *U, long long photo_id, struct file_location *big, struct file_location *small);
void bl_do_set_user_name (struct binlog *bl, struct mtproto_connection *self, struct user *U, const char *f, int fl, const char *l, int ll);
void bl_do_set_user_access_token (struct binlog *bl, struct mtproto_connection *self, struct user *U, long long access_token);
void bl_do_set_user_phone (struct binlog *bl, struct mtproto_connection *self, struct user *U, const char *p, int pl);
void bl_do_set_user_friend (struct binlog *bl, struct mtproto_connection *self, struct user *U, int friend);
void bl_do_set_user_full_photo (struct binlog *bl, struct mtproto_connection *self, struct user *U, const int *start, int len);
void bl_do_set_user_blocked (struct binlog *bl, struct mtproto_connection *self, struct user *U, int blocked);
void bl_do_set_user_real_name (struct binlog *bl, struct mtproto_connection *self, struct user *U, const char *f, int fl, const char *l, int ll);
void bl_do_encr_chat_delete (struct mtproto_connection *self, struct secret_chat *U);
void bl_do_encr_chat_requested (struct mtproto_connection *self, struct secret_chat *U, long long access_hash, int date, int admin_id, int user_id, unsigned char g_key[], unsigned char nonce[]);
void bl_do_set_encr_chat_access_hash (struct mtproto_connection *self, struct secret_chat *U, long long access_hash);
void bl_do_set_encr_chat_date (struct mtproto_connection *self, struct secret_chat *U, int date);
void bl_do_set_encr_chat_state (struct mtproto_connection *self, struct secret_chat *U, enum secret_chat_state state);
void bl_do_encr_chat_accepted (struct mtproto_connection *self, struct secret_chat *U, const unsigned char g_key[], const unsigned char nonce[], long long key_fingerprint);
void bl_do_set_encr_chat_key (struct mtproto_connection *self, struct secret_chat *E, unsigned char key[], long long key_fingerprint);
void bl_do_encr_chat_init (struct mtproto_connection *self, int id, int user_id, unsigned char random[], unsigned char g_a[]);
void bl_do_encr_chat_delete (struct binlog *bl, struct mtproto_connection *self, struct secret_chat *U);
void bl_do_encr_chat_requested (struct binlog *bl, struct mtproto_connection *self, struct secret_chat *U, long long access_hash, int date, int admin_id, int user_id, unsigned char g_key[], unsigned char nonce[]);
void bl_do_set_encr_chat_access_hash (struct binlog *bl, struct mtproto_connection *self, struct secret_chat *U, long long access_hash);
void bl_do_set_encr_chat_date (struct binlog *bl, struct mtproto_connection *self, struct secret_chat *U, int date);
void bl_do_set_encr_chat_state (struct binlog *bl, struct mtproto_connection *self, struct secret_chat *U, enum secret_chat_state state);
void bl_do_encr_chat_accepted (struct binlog *bl, struct mtproto_connection *self, struct secret_chat *U, const unsigned char g_key[], const unsigned char nonce[], long long key_fingerprint);
void bl_do_set_encr_chat_key (struct binlog *bl, struct mtproto_connection *self, struct secret_chat *E, unsigned char key[], long long key_fingerprint);
void bl_do_encr_chat_init (struct binlog *bl, struct mtproto_connection *self, int id, int user_id, unsigned char random[], unsigned char g_a[]);
void bl_do_dc_signed (struct mtproto_connection *self, int id);
void bl_do_set_working_dc (struct mtproto_connection *self, int num);
void bl_do_set_dh_params (struct mtproto_connection *self, int root, unsigned char prime[], int version);
void bl_do_dc_signed (struct binlog *bl, struct mtproto_connection *self, int id);
void bl_do_set_working_dc (struct binlog *bl, struct mtproto_connection *self, int num);
void bl_do_set_dh_params (struct binlog *bl, struct mtproto_connection *self, int root, unsigned char prime[], int version);
void bl_do_set_pts (struct mtproto_connection *self, int pts);
void bl_do_set_qts (struct mtproto_connection *self, int qts);
void bl_do_set_seq (struct mtproto_connection *self, int seq);
void bl_do_set_date (struct mtproto_connection *self, int date);
void bl_do_set_pts (struct binlog *bl, struct mtproto_connection *self, int pts);
void bl_do_set_qts (struct binlog *bl, struct mtproto_connection *self, int qts);
void bl_do_set_seq (struct binlog *bl, struct mtproto_connection *self, int seq);
void bl_do_set_date (struct binlog *bl, struct mtproto_connection *self, int date);
void bl_do_create_chat (struct mtproto_connection *self, struct chat *C, int y, const char *s, int l, int users_num, int date, int version, struct file_location *big, struct file_location *small);
void bl_do_chat_forbid (struct mtproto_connection *self, struct chat *C, int on);
void bl_do_set_chat_title (struct mtproto_connection *self, struct chat *C, const char *s, int l);
void bl_do_set_chat_photo (struct mtproto_connection *self, struct chat *C, struct file_location *big, struct file_location *small);
void bl_do_set_chat_date (struct mtproto_connection *self, struct chat *C, int date);
void bl_do_set_chat_set_in_chat (struct mtproto_connection *self, struct chat *C, int on);
void bl_do_set_chat_version (struct mtproto_connection *self, struct chat *C, int version, int user_num);
void bl_do_set_chat_admin (struct mtproto_connection *self, struct chat *C, int admin);
void bl_do_set_chat_participants (struct mtproto_connection *self, struct chat *C, int version, int user_num, struct chat_user *users);
void bl_do_set_chat_full_photo (struct mtproto_connection *self, struct chat *U, const int *start, int len);
void bl_do_chat_add_user (struct mtproto_connection *self, struct chat *C, int version, int user, int inviter, int date);
void bl_do_chat_del_user (struct mtproto_connection *self, struct chat *C, int version, int user);
void bl_do_create_chat (struct binlog *bl, struct mtproto_connection *self, struct chat *C, int y, const char *s, int l, int users_num, int date, int version, struct file_location *big, struct file_location *small);
void bl_do_chat_forbid (struct binlog *bl, struct mtproto_connection *self, struct chat *C, int on);
void bl_do_set_chat_title (struct binlog *bl, struct mtproto_connection *self, struct chat *C, const char *s, int l);
void bl_do_set_chat_photo (struct binlog *bl, struct mtproto_connection *self, struct chat *C, struct file_location *big, struct file_location *small);
void bl_do_set_chat_date (struct binlog *bl, struct mtproto_connection *self, struct chat *C, int date);
void bl_do_set_chat_set_in_chat (struct binlog *bl, struct mtproto_connection *self, struct chat *C, int on);
void bl_do_set_chat_version (struct binlog *bl, struct mtproto_connection *self, struct chat *C, int version, int user_num);
void bl_do_set_chat_admin (struct binlog *bl, struct mtproto_connection *self, struct chat *C, int admin);
void bl_do_set_chat_participants (struct binlog *bl, struct mtproto_connection *self, struct chat *C, int version, int user_num, struct chat_user *users);
void bl_do_set_chat_full_photo (struct binlog *bl, struct mtproto_connection *self, struct chat *U, const int *start, int len);
void bl_do_chat_add_user (struct binlog *bl, struct mtproto_connection *self, struct chat *C, int version, int user, int inviter, int date);
void bl_do_chat_del_user (struct binlog *bl, struct mtproto_connection *self, struct chat *C, int version, int user);
void bl_do_create_message_text (struct mtproto_connection *self, 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 (struct mtproto_connection *self, 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 (struct mtproto_connection *self, 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 (struct mtproto_connection *self, 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 (struct mtproto_connection *self, 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 (struct mtproto_connection *self, 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 (struct mtproto_connection *self, 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 (struct mtproto_connection *self, 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 (struct mtproto_connection *self, 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 mtproto_connection *self, struct message *M, int unread);
void bl_do_set_message_sent (struct mtproto_connection *self, struct message *M);
void bl_do_set_msg_id (struct mtproto_connection *self, struct message *M, int id);
void bl_do_delete_msg (struct mtproto_connection *self, struct message *M);
void bl_do_create_message_text (struct binlog *bl, struct mtproto_connection *self, 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 (struct binlog *bl, struct mtproto_connection *self, 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 (struct binlog *bl, struct mtproto_connection *self, 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 (struct binlog *bl, struct mtproto_connection *self, 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 (struct binlog *bl, struct mtproto_connection *self, 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 (struct binlog *bl, struct mtproto_connection *self, 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 (struct binlog *bl, struct mtproto_connection *self, 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 (struct binlog *bl, struct mtproto_connection *self, 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 (struct binlog *bl, struct mtproto_connection *self, 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 binlog *bl, struct mtproto_connection *self, struct message *M, int unread);
void bl_do_set_message_sent (struct binlog *bl, struct mtproto_connection *self, struct message *M);
void bl_do_set_msg_id (struct binlog *bl, struct mtproto_connection *self, struct message *M, int id);
void bl_do_delete_msg (struct binlog *bl, struct mtproto_connection *self, struct message *M);
#endif

View file

@ -781,7 +781,7 @@ void fetch_pts (struct mtproto_connection *self) {
} else {
self->pts ++;
}
bl_do_set_pts (self, self->pts);
bl_do_set_pts (self->bl, self, self->pts);
}
void fetch_qts (struct mtproto_connection *self) {
@ -798,14 +798,14 @@ void fetch_qts (struct mtproto_connection *self) {
} else {
self->qts ++;
}
bl_do_set_qts (self, self->qts);
bl_do_set_qts (self->bl, self, self->qts);
}
void fetch_date (struct mtproto_connection *self) {
int p = fetch_int (self);
if (p > self->last_date) {
self->last_date = p;
bl_do_set_date (self, self->last_date);
bl_do_set_date (self->bl, self, self->last_date);
}
}
@ -817,7 +817,7 @@ void fetch_seq (struct mtproto_connection *self) {
//seq = x;
} else if (x == self->seq + 1) {
self->seq = x;
bl_do_set_seq (self, self->seq);
bl_do_set_seq (self->bl, self, self->seq);
}
}
@ -908,7 +908,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
int new = fetch_long (self); // random_id
struct message *M = message_get (new);
if (M) {
bl_do_set_msg_id (self, M, id);
bl_do_set_msg_id (self->bl, self, M, id);
}
}
break;
@ -921,7 +921,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
int id = fetch_int (self);
struct message *M = message_get (id);
if (M) {
bl_do_set_unread (self, M, 0);
bl_do_set_unread (self->bl, self, M, 0);
}
}
fetch_pts (self);
@ -1004,7 +1004,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
int l2 = prefetch_strlen (self);
char *l = fetch_str (self, l2);
struct user *U = &UC->user;
bl_do_set_user_real_name (self, U, f, l1, l, l2);
bl_do_set_user_real_name (self->bl, self, U, f, l1, l, l2);
//print_start ();
//push_color (COLOR_YELLOW);
//print_date (time (0));
@ -1044,7 +1044,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
fetch_file_location (self, &small);
fetch_file_location (self, &big);
}
bl_do_set_user_profile_photo (self, U, photo_id, &big, &small);
bl_do_set_user_profile_photo (self->bl, self, U, photo_id, &big, &small);
//print_start ();
//push_color (COLOR_YELLOW);
@ -1105,7 +1105,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
peer_t *C = user_chat_get (chat_id);
if (C && (C->flags & FLAG_CREATED)) {
if (x == CODE_chat_participants) {
bl_do_set_chat_admin (self, &C->chat, fetch_int (self));
bl_do_set_chat_admin (self->bl, self, &C->chat, fetch_int (self));
assert (fetch_int (self) == CODE_vector);
n = fetch_int (self);
struct chat_user *users = talloc (12 * n);
@ -1117,7 +1117,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
users[i].date = fetch_int (self);
}
int version = fetch_int (self);
bl_do_set_chat_participants (self, &C->chat, version, n, users);
bl_do_set_chat_participants (self->bl, self, &C->chat, version, n, users);
}
} else {
if (x == CODE_chat_participants) {
@ -1330,7 +1330,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
peer_t *C = user_chat_get (chat_id);
if (C && (C->flags & FLAG_CREATED)) {
bl_do_chat_add_user (self, &C->chat, version, get_peer_id (user_id), get_peer_id (inviter_id), time (0));
bl_do_chat_add_user (self->bl, self, &C->chat, version, get_peer_id (user_id), get_peer_id (inviter_id), time (0));
}
//print_start ();
@ -1355,7 +1355,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
peer_t *C = user_chat_get (chat_id);
if (C && (C->flags & FLAG_CREATED)) {
bl_do_chat_del_user (self, &C->chat, version, get_peer_id (user_id));
bl_do_chat_del_user (self->bl, self, &C->chat, version, get_peer_id (user_id));
}
//print_start ();
@ -1415,8 +1415,8 @@ void work_updates (struct connection *c, long long msg_id) {
for (i = 0; i < n; i++) {
fetch_alloc_chat (self);
}
bl_do_set_date (self, fetch_int (c->mtconnection));
bl_do_set_seq (self, fetch_int (c->mtconnection));
bl_do_set_date (self->bl, self, fetch_int (c->mtconnection));
bl_do_set_seq (self->bl, self, fetch_int (c->mtconnection));
}
void work_update_short_message (struct connection *c UU, long long msg_id UU) {
@ -1870,6 +1870,10 @@ struct mtproto_connection *mtproto_new(struct dc *DC, int fd, struct telegram *t
memset(mtp, 0, sizeof(struct mtproto_connection));
mtp->packet_buffer = mtp->__packet_buffer + 16;
mtp->connection = fd_create_connection(DC, fd, tg, &mtproto_methods, mtp);
// binlog must exist
assert (tg->bl);
mtp->bl = tg->bl;
return mtp;
}

View file

@ -238,10 +238,18 @@ struct mtproto_connection {
// the amount of currently outgoing messages that
// have not yet received a response
int queries_num;
// binlog that consumes all updates and events of this connection
struct binlog *bl;
// marks this connection for destruction, so it
// will be freed once all queries received a response or timed out
int destroy;
};
void mtproto_connection_init (struct mtproto_connection *c);
struct mtproto_connection *mtproto_new(struct dc *DC, int fd, struct telegram *tg);
void mtproto_close(struct mtproto_connection *c);
void mtproto_connect(struct mtproto_connection *c);
void on_start (struct mtproto_connection *self);

View file

@ -355,7 +355,7 @@ void fetch_dc_option (struct telegram *instance) {
int port = fetch_int (mtp);
logprintf ( "id = %d, name = %.*s ip = %.*s port = %d\n", id, l1, name, l2, ip, port);
bl_do_dc_option (mtp, id, l1, name, l2, ip, port, instance);
bl_do_dc_option (mtp->bl, mtp, id, l1, name, l2, ip, port, instance);
}
int help_get_config_on_answer (struct query *q UU) {
@ -686,7 +686,7 @@ int sign_in_on_answer (struct query *q UU) {
fetch_user (mtp, &User);
if (!our_id) {
our_id = get_peer_id (User.id);
bl_do_set_our_id (mtp, our_id);
bl_do_set_our_id (mtp->bl, mtp, our_id);
}
sign_in_ok = 1;
if (verbosity) {
@ -694,7 +694,7 @@ int sign_in_on_answer (struct query *q UU) {
}
DC_working->has_auth = 1;
bl_do_dc_signed (mtp, DC_working->id);
bl_do_dc_signed (mtp->bl, mtp, DC_working->id);
return 0;
}
@ -914,7 +914,7 @@ int msg_send_encr_on_answer (struct query *q UU) {
struct message *M = q->extra;
//M->date = fetch_int (mtp);
fetch_int (mtp);
bl_do_set_message_sent (mtp, M);
bl_do_set_message_sent (mtp->bl, mtp, M);
return 0;
}
@ -925,7 +925,7 @@ int msg_send_on_answer (struct query *q UU) {
assert (x == CODE_messages_sent_message || x == CODE_messages_sent_message_link);
int id = fetch_int (mtp); // id
struct message *M = q->extra;
bl_do_set_msg_id (mtp, M, id);
bl_do_set_msg_id (mtp->bl, mtp, M, id);
fetch_date (mtp);
fetch_pts (mtp);
fetch_seq (mtp);
@ -969,7 +969,7 @@ int msg_send_on_answer (struct query *q UU) {
}
}
logprintf ("Sent: id = %d\n", id);
bl_do_set_message_sent (mtp, M);
bl_do_set_message_sent (mtp->bl, mtp, M);
return 0;
}
@ -978,7 +978,7 @@ int msg_send_on_error (struct query *q, int error_code, int error_len, char *err
logprintf ( "error for query #%lld: #%d :%.*s\n", q->msg_id, error_code, error_len, error);
struct message *M = q->extra;
bl_do_delete_msg (mtp, M);
bl_do_delete_msg (mtp->bl, mtp, M);
return 0;
}
@ -1053,7 +1053,7 @@ void do_send_message (struct telegram *instance, peer_id_t id, const char *msg,
long long t;
secure_random (&t, 8);
logprintf ("t = %lld, len = %d\n", t, len);
bl_do_send_message_text (mtp, t, our_id, get_peer_type (id), get_peer_id (id), time (0), len, msg);
bl_do_send_message_text (mtp->bl, mtp, 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 (instance, M);
@ -2570,7 +2570,7 @@ void do_send_accept_encr_chat (struct telegram *instance, struct secret_chat *E,
static unsigned char sha_buffer[20];
sha1 (kk, 256, sha_buffer);
bl_do_set_encr_chat_key (mtp, E, kk, *(long long *)(sha_buffer + 12));
bl_do_set_encr_chat_key (mtp->bl, mtp, E, kk, *(long long *)(sha_buffer + 12));
clear_packet (mtp);
out_int (mtp, CODE_messages_accept_encryption);
@ -2685,7 +2685,7 @@ void do_send_create_encr_chat (struct telegram *instance, void *x, unsigned char
t = lrand48 ();
}
bl_do_encr_chat_init (mtp, t, user_id, (void *)random, (void *)g_a);
bl_do_encr_chat_init (mtp->bl, mtp, t, user_id, (void *)random, (void *)g_a);
peer_t *_E = user_chat_get (MK_ENCR_CHAT (t));
assert (_E);
struct secret_chat *E = &_E->encr_chat;
@ -2731,7 +2731,7 @@ int get_dh_config_on_answer (struct query *q UU) {
assert (l == 256);
char *s = fetch_str (mtp, l);
int v = fetch_int (mtp);
bl_do_set_dh_params (mtp, a, (void *)s, v);
bl_do_set_dh_params (mtp->bl, mtp, a, (void *)s, v);
BIGNUM *p = BN_bin2bn ((void *)s, 256, 0);
ensure_ptr (p);
@ -2805,10 +2805,10 @@ int get_state_on_answer (struct query *q UU) {
logprintf("get_state_on_answer()\n");
assert (fetch_int (mtp) == (int)CODE_updates_state);
bl_do_set_pts (mtp, fetch_int (mtp));
bl_do_set_qts (mtp, fetch_int (mtp));
bl_do_set_date (mtp, fetch_int (mtp));
bl_do_set_seq (mtp, fetch_int (mtp));
bl_do_set_pts (mtp->bl, mtp, fetch_int (mtp));
bl_do_set_qts (mtp->bl, mtp, fetch_int (mtp));
bl_do_set_date (mtp->bl, mtp, fetch_int (mtp));
bl_do_set_seq (mtp->bl, mtp, fetch_int (mtp));
unread_messages = fetch_int (mtp);
//write_state_file ();
difference_got = 1;
@ -2824,8 +2824,8 @@ int get_difference_on_answer (struct query *q UU) {
get_difference_active = 0;
unsigned x = fetch_int (mtp);
if (x == CODE_updates_difference_empty) {
bl_do_set_date (mtp, fetch_int (mtp));
bl_do_set_seq (mtp, fetch_int (mtp));
bl_do_set_date (mtp->bl, mtp, fetch_int (mtp));
bl_do_set_seq (mtp->bl, mtp, fetch_int (mtp));
difference_got = 1;
} else if (x == CODE_updates_difference || x == CODE_updates_difference_slice) {
int n, i;
@ -2867,10 +2867,10 @@ int get_difference_on_answer (struct query *q UU) {
fetch_alloc_user (mtp);
}
assert (fetch_int (mtp) == (int)CODE_updates_state);
bl_do_set_pts (mtp, fetch_int (mtp));
bl_do_set_qts (mtp, fetch_int (mtp));
bl_do_set_date (mtp, fetch_int (mtp));
bl_do_set_seq (mtp, fetch_int (mtp));
bl_do_set_pts (mtp->bl, mtp, fetch_int (mtp));
bl_do_set_qts (mtp->bl, mtp, fetch_int (mtp));
bl_do_set_date (mtp->bl, mtp, fetch_int (mtp));
bl_do_set_seq (mtp->bl, mtp, fetch_int (mtp));
unread_messages = fetch_int (mtp);
//write_state_file ();
for (i = 0; i < ml_pos; i++) {

View file

@ -189,7 +189,7 @@ long long fetch_user_photo (struct mtproto_connection *mtp, struct user *U) {
unsigned x = fetch_int (mtp);
code_assert (x == CODE_user_profile_photo || x == CODE_user_profile_photo_old || x == CODE_user_profile_photo_empty);
if (x == CODE_user_profile_photo_empty) {
bl_do_set_user_profile_photo (mtp, U, 0, 0, 0);
bl_do_set_user_profile_photo (mtp->bl, mtp, U, 0, 0, 0);
return 0;
}
long long photo_id = 1;
@ -201,7 +201,7 @@ long long fetch_user_photo (struct mtproto_connection *mtp, struct user *U) {
code_try (fetch_file_location (mtp, &small));
code_try (fetch_file_location (mtp, &big));
bl_do_set_user_profile_photo (mtp, U, photo_id, &big, &small);
bl_do_set_user_profile_photo (mtp->bl, mtp, U, photo_id, &big, &small);
return 0;
}
@ -231,7 +231,7 @@ int fetch_user (struct mtproto_connection *mtp, struct user *U) {
if (x == CODE_user_self) {
assert (!our_id || (our_id == get_peer_id (U->id)));
if (!our_id) {
bl_do_set_our_id (mtp, get_peer_id (U->id));
bl_do_set_our_id (mtp->bl, mtp, get_peer_id (U->id));
// TODO: What to do here?
//write_auth_file ();
}
@ -250,8 +250,8 @@ int fetch_user (struct mtproto_connection *mtp, struct user *U) {
char *s2 = fetch_str (mtp, l2);
if (x == CODE_user_deleted && !(U->flags & FLAG_DELETED)) {
bl_do_new_user (mtp, get_peer_id (U->id), s1, l1, s2, l2, 0, 0, 0, 0);
bl_do_user_delete (mtp, U);
bl_do_new_user (mtp->bl, mtp, get_peer_id (U->id), s1, l1, s2, l2, 0, 0, 0, 0);
bl_do_user_delete (mtp->bl, mtp, U);
}
if (x != CODE_user_deleted) {
long long access_token = 0;
@ -265,7 +265,7 @@ int fetch_user (struct mtproto_connection *mtp, struct user *U) {
code_assert (phone_len >= 0);
phone = fetch_str (mtp, phone_len);
}
bl_do_new_user (mtp, get_peer_id (U->id), s1, l1, s2, l2, access_token, phone, phone_len, x == CODE_user_contact);
bl_do_new_user (mtp->bl, mtp, get_peer_id (U->id), s1, l1, s2, l2, access_token, phone, phone_len, x == CODE_user_contact);
if (fetch_user_photo (mtp, U) < 0) { return -1; }
if (fetch_user_status (mtp, &U->status) < 0) { return -1; }
@ -279,19 +279,19 @@ int fetch_user (struct mtproto_connection *mtp, struct user *U) {
int l2 = prefetch_strlen (mtp);
char *s2 = fetch_str (mtp, l2);
bl_do_set_user_name (mtp, U, s1, l1, s2, l2);
bl_do_set_user_name (mtp->bl, mtp, U, s1, l1, s2, l2);
if (x == CODE_user_deleted && !(U->flags & FLAG_DELETED)) {
bl_do_user_delete (mtp, U);
bl_do_user_delete (mtp->bl, mtp, U);
}
if (x != CODE_user_deleted) {
if (x != CODE_user_self) {
bl_do_set_user_access_token (mtp, U, fetch_long (mtp));
bl_do_set_user_access_token (mtp->bl, mtp, U, fetch_long (mtp));
}
if (x != CODE_user_foreign) {
int l = prefetch_strlen (mtp);
char *s = fetch_str (mtp, l);
bl_do_set_user_phone (mtp, U, s, l);
bl_do_set_user_phone (mtp->bl, mtp, U, s, l);
}
if (fetch_user_photo (mtp, U) < 0) { return -1; }
@ -301,9 +301,9 @@ int fetch_user (struct mtproto_connection *mtp, struct user *U) {
}
if (x == CODE_user_contact) {
bl_do_set_user_friend (mtp, U, 1);
bl_do_set_user_friend (mtp->bl, mtp, U, 1);
} else {
bl_do_set_user_friend (mtp, U, 0);
bl_do_set_user_friend (mtp->bl, mtp, U, 0);
}
}
}
@ -324,7 +324,7 @@ void fetch_encrypted_chat (struct mtproto_connection *mtp, struct secret_chat *U
logprintf ("Unknown chat in deleted state. May be we forgot something...\n");
return;
}
bl_do_encr_chat_delete (mtp, U);
bl_do_encr_chat_delete (mtp->bl, mtp, U);
// TODO: properly
write_secret_chat_file ("/home/dev-jessie/.telegram/+4915736384600/secret");
@ -375,11 +375,11 @@ void fetch_encrypted_chat (struct mtproto_connection *mtp, struct secret_chat *U
return;
}
bl_do_encr_chat_requested (mtp, U, access_hash, date, admin_id, user_id, (void *)g_key, (void *)nonce);
bl_do_encr_chat_requested (mtp->bl, mtp, U, access_hash, date, admin_id, user_id, (void *)g_key, (void *)nonce);
write_secret_chat_file ("/home/dev-jessie/.telegram/+4915736384600/secret");
} else {
bl_do_set_encr_chat_access_hash (mtp, U, fetch_long (mtp));
bl_do_set_encr_chat_date (mtp, U, fetch_int (mtp));
bl_do_set_encr_chat_access_hash (mtp->bl, mtp, U, fetch_long (mtp));
bl_do_set_encr_chat_date (mtp->bl, mtp, U, fetch_int (mtp));
if (fetch_int (mtp) != U->admin_id) {
logprintf ("Changed admin in secret chat. WTF?\n");
return;
@ -389,7 +389,7 @@ void fetch_encrypted_chat (struct mtproto_connection *mtp, struct secret_chat *U
return;
}
if (x == CODE_encrypted_chat_waiting) {
bl_do_set_encr_chat_state (mtp, U, sc_waiting);
bl_do_set_encr_chat_state (mtp->bl, mtp, U, sc_waiting);
write_secret_chat_file ("/home/dev-jessie/.telegram/+4915736384600/secret");
return; // We needed only access hash from here
}
@ -420,7 +420,7 @@ void fetch_encrypted_chat (struct mtproto_connection *mtp, struct secret_chat *U
if (x == CODE_encrypted_chat_requested) {
return; // Duplicate?
}
bl_do_encr_chat_accepted (mtp, U, (void *)g_key, (void *)nonce, fetch_long (mtp));
bl_do_encr_chat_accepted (mtp->bl, mtp, U, (void *)g_key, (void *)nonce, fetch_long (mtp));
}
write_secret_chat_file ("/home/dev-jessie/.telegram/+4915736384600/secret");
}
@ -445,16 +445,16 @@ void fetch_user_full (struct mtproto_connection *mtp, struct user *U) {
int *start = mtp->in_ptr;
fetch_skip_photo (mtp);
bl_do_set_user_full_photo (mtp, U, start, 4 * (mtp->in_ptr - start));
bl_do_set_user_full_photo (mtp->bl, mtp, U, start, 4 * (mtp->in_ptr - start));
fetch_notify_settings (mtp);
bl_do_set_user_blocked (mtp, U, fetch_bool (mtp));
bl_do_set_user_blocked (mtp->bl, mtp, U, fetch_bool (mtp));
int l1 = prefetch_strlen (mtp);
char *s1 = fetch_str (mtp, l1);
int l2 = prefetch_strlen (mtp);
char *s2 = fetch_str (mtp, l2);
bl_do_set_user_real_name (mtp, U, s1, l1, s2, l2);
bl_do_set_user_real_name (mtp->bl, mtp, U, s1, l1, s2, l2);
}
void fetch_chat (struct mtproto_connection *mtp, struct chat *C) {
@ -505,16 +505,16 @@ void fetch_chat (struct mtproto_connection *mtp, struct chat *C) {
version = -1;
}
bl_do_create_chat (mtp, C, y, s, l, users_num, date, version, &big, &small);
bl_do_create_chat (mtp->bl, mtp, C, y, s, l, users_num, date, version, &big, &small);
} else {
if (x == CODE_chat_forbidden) {
bl_do_chat_forbid (mtp, C, 1);
bl_do_chat_forbid (mtp->bl, mtp, C, 1);
} else {
bl_do_chat_forbid (mtp, C, 0);
bl_do_chat_forbid (mtp->bl, mtp, C, 0);
}
int l = prefetch_strlen (mtp);
char *s = fetch_str (mtp, l);
bl_do_set_chat_title (mtp, C, s, l);
bl_do_set_chat_title (mtp->bl, mtp, C, s, l);
struct file_location small;
struct file_location big;
@ -531,13 +531,13 @@ void fetch_chat (struct mtproto_connection *mtp, struct chat *C) {
fetch_file_location (mtp, &small);
fetch_file_location (mtp, &big);
}
bl_do_set_chat_photo (mtp, C, &big, &small);
bl_do_set_chat_photo (mtp->bl, mtp, C, &big, &small);
int users_num = fetch_int (mtp);
bl_do_set_chat_date (mtp, C, fetch_int (mtp));
bl_do_set_chat_set_in_chat (mtp, C, fetch_bool (mtp));
bl_do_set_chat_version (mtp, C, users_num, fetch_int (mtp));
bl_do_set_chat_date (mtp->bl, mtp, C, fetch_int (mtp));
bl_do_set_chat_set_in_chat (mtp->bl, mtp, C, fetch_bool (mtp));
bl_do_set_chat_version (mtp->bl, mtp, C, users_num, fetch_int (mtp));
} else {
bl_do_set_chat_date (mtp, C, fetch_int (mtp));
bl_do_set_chat_date (mtp->bl, mtp, C, fetch_int (mtp));
}
}
}
@ -606,13 +606,13 @@ void fetch_chat_full (struct mtproto_connection *mtp, struct chat *C) {
fetch_alloc_user (mtp);
}
if (admin_id) {
bl_do_set_chat_admin (mtp, C, admin_id);
bl_do_set_chat_admin (mtp->bl, mtp, C, admin_id);
}
if (version > 0) {
bl_do_set_chat_participants (mtp, C, version, users_num, users);
bl_do_set_chat_participants (mtp->bl, mtp, C, version, users_num, users);
tfree (users, sizeof (struct chat_user) * users_num);
}
bl_do_set_chat_full_photo (mtp, C, start, 4 * (end - start));
bl_do_set_chat_full_photo (mtp->bl, mtp, C, start, 4 * (end - start));
}
void fetch_photo_size (struct mtproto_connection *mtp, struct photo_size *S) {
@ -889,7 +889,7 @@ void fetch_message_short (struct mtproto_connection *mtp, struct message *M) {
int date = fetch_int (mtp);
fetch_seq (mtp);
bl_do_create_message_text (mtp, id, from_id, PEER_USER, to_id, date, l, s);
bl_do_create_message_text (mtp->bl, mtp, id, from_id, PEER_USER, to_id, date, l, s);
} else {
fetch_int (mtp); // id
fetch_int (mtp); // from_id
@ -917,7 +917,7 @@ void fetch_message_short_chat (struct mtproto_connection *mtp, struct message *M
int date = fetch_int (mtp);
fetch_seq (mtp);
bl_do_create_message_text (mtp, id, from_id, PEER_CHAT, to_id, date, l, s);
bl_do_create_message_text (mtp->bl, mtp, id, from_id, PEER_CHAT, to_id, date, l, s);
} else {
fetch_int (mtp); // id
fetch_int (mtp); // from_id
@ -1302,10 +1302,10 @@ void fetch_message (struct mtproto_connection *mtp, struct message *M) {
fetch_skip_message_action (mtp);
if (new) {
if (fwd_from_id) {
bl_do_create_message_service_fwd (mtp, id, from_id, get_peer_type (to_id), get_peer_id (to_id),
bl_do_create_message_service_fwd (mtp->bl, mtp, id, from_id, get_peer_type (to_id), get_peer_id (to_id),
date, fwd_from_id, fwd_date, start, (mtp->in_ptr - start));
} else {
bl_do_create_message_service (mtp, id, from_id, get_peer_type (to_id),
bl_do_create_message_service (mtp->bl, mtp, id, from_id, get_peer_type (to_id),
get_peer_id (to_id), date, start, (mtp->in_ptr - start));
}
}
@ -1316,15 +1316,15 @@ void fetch_message (struct mtproto_connection *mtp, struct message *M) {
fetch_skip_message_media (mtp);
if (new) {
if (fwd_from_id) {
bl_do_create_message_media_fwd (mtp, id, from_id, get_peer_type (to_id),
bl_do_create_message_media_fwd (mtp->bl, mtp, id, from_id, get_peer_type (to_id),
get_peer_id (to_id), date, fwd_from_id, fwd_date, l, s, start, mtp->in_ptr - start);
} else {
bl_do_create_message_media (mtp, id, from_id, get_peer_type (to_id),
bl_do_create_message_media (mtp->bl, mtp, id, from_id, get_peer_type (to_id),
get_peer_id (to_id), date, l, s, start, mtp->in_ptr - start);
}
}
}
bl_do_set_unread (mtp, M, unread);
bl_do_set_unread (mtp->bl, mtp, M, unread);
}
void fetch_geo_message (struct mtproto_connection *mtp, struct message *M) {
@ -1481,7 +1481,7 @@ void fetch_encrypted_message (struct mtproto_connection *mtp, struct message *M)
int *start_file = mtp->in_ptr;
fetch_skip_encrypted_message_file (mtp);
if (x == CODE_decrypted_message) {
bl_do_create_message_media_encr (mtp, id, P->encr_chat.user_id, PEER_ENCR_CHAT, to_id, date, l, s, start, end - start, start_file, mtp->in_ptr - start_file);
bl_do_create_message_media_encr (mtp->bl, mtp, id, P->encr_chat.user_id, PEER_ENCR_CHAT, to_id, date, l, s, start, end - start, start_file, mtp->in_ptr - start_file);
}
} else {
x = fetch_int (mtp);
@ -1494,7 +1494,7 @@ void fetch_encrypted_message (struct mtproto_connection *mtp, struct message *M)
}
} else {
if (ok && x == CODE_decrypted_message_service) {
bl_do_create_message_service_encr (mtp, id, P->encr_chat.user_id, PEER_ENCR_CHAT, to_id, date, start, end - start);
bl_do_create_message_service_encr (mtp->bl, mtp, id, P->encr_chat.user_id, PEER_ENCR_CHAT, to_id, date, start, end - start);
}
}
}

View file

@ -11,6 +11,9 @@
#include "glib.h"
#include "tools.h"
#include "mtproto-client.h"
#include "binlog.h"
/*
* New message received
@ -116,6 +119,7 @@ struct telegram *telegram_new(struct dc *DC, const char* login, const char *conf
//this->curr_dc = 0;
this->auth.DC_list[0] = DC;
this->change_state_listeners = NULL;
this->bl = talloc0 (sizeof(struct binlog));
this->login = g_strdup(login);
this->config_path = g_strdup_printf("%s/%s", config_path, login);
@ -145,6 +149,7 @@ void telegram_free(struct telegram *this)
g_free(this->state_path);
g_free(this->secret_path);
free(this);
tfree(this->bl, sizeof(struct binlog));
}
void assert_file_usable(const char *file)

View file

@ -65,6 +65,24 @@ struct authorization_state;
// Ready for sending and receiving messages
#define STATE_READY 22
/**
* Binary log
*/
#define BINLOG_BUFFER_SIZE (1 << 20)
struct binlog {
int binlog_buffer[BINLOG_BUFFER_SIZE];
int *rptr;
int *wptr;
int test_dc; // = 0
int in_replay_log;
int binlog_enabled; // = 0;
int binlog_fd;
long long binlog_pos;
int s[1000];
};
/**
* A telegram session
*