From ce61adac12fc18e2f5c8165e10f6ba91e1cb0928 Mon Sep 17 00:00:00 2001 From: vvaltman Date: Fri, 12 Sep 2014 00:57:59 +0400 Subject: [PATCH] Fixed send_photo to encr chats --- binlog.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ binlog.h | 2 ++ binlog.tl | 2 ++ interface.c | 5 ---- mtproto-client.c | 1 + mtproto-common.c | 2 +- queries.c | 63 +++++++++++++++++++++++++------------------ structures.c | 7 ++++- 8 files changed, 119 insertions(+), 33 deletions(-) diff --git a/binlog.c b/binlog.c index 7e7204d..0da050e 100644 --- a/binlog.c +++ b/binlog.c @@ -937,6 +937,46 @@ static int fetch_comb_binlog_create_message_media_encr (void *extra) { return 0; } +static int fetch_comb_binlog_create_message_media_encr_pending (void *extra) { + long long id = fetch_long (); + struct tgl_message *M = tgl_message_get (id); + if (!M) { + M = tglm_message_alloc (id); + } else { + assert (!(M->flags & FLAG_CREATED)); + } + M->flags |= FLAG_CREATED | FLAG_ENCRYPTED; + M->from_id = TGL_MK_USER (fetch_int ()); + int t = fetch_int (); + M->to_id = tgl_set_peer_id (t, fetch_int ()); + M->date = fetch_int (); + + int l = prefetch_strlen (); + M->message = talloc (l + 1); + memcpy (M->message, fetch_str (l), l); + M->message[l] = 0; + M->message_len = l; + + tglf_fetch_message_media_encrypted (&M->media); + M->unread = 1; + M->out = tgl_get_peer_id (M->from_id) == tgl_state.our_id; + + tglm_message_insert (M); + tglm_message_insert_unsent (M); + M->flags |= FLAG_PENDING; + return 0; +} + +static int fetch_comb_binlog_create_message_media_encr_sent (void *extra) { + long long id = fetch_long (); + struct tgl_message *M = tgl_message_get (id); + assert (M && (M->flags & FLAG_CREATED)); + tglf_fetch_encrypted_message_file (&M->media); + tglm_message_remove_unsent (M); + M->flags &= ~FLAG_PENDING; + return 0; +} + static int fetch_comb_binlog_create_message_media_fwd (void *extra) { int id = fetch_int (); struct tgl_message *M = tgl_message_get (id); @@ -1233,6 +1273,8 @@ static void replay_log_event (void) { FETCH_COMBINATOR_FUNCTION (binlog_create_message_text_fwd) FETCH_COMBINATOR_FUNCTION (binlog_create_message_media) FETCH_COMBINATOR_FUNCTION (binlog_create_message_media_encr) + FETCH_COMBINATOR_FUNCTION (binlog_create_message_media_encr_pending) + FETCH_COMBINATOR_FUNCTION (binlog_create_message_media_encr_sent) FETCH_COMBINATOR_FUNCTION (binlog_create_message_media_fwd) FETCH_COMBINATOR_FUNCTION (binlog_create_message_service) FETCH_COMBINATOR_FUNCTION (binlog_create_message_service_encr) @@ -1319,6 +1361,8 @@ void tgl_replay_log (void) { close (fd); } +static int b_packet_buffer[PACKET_BUFFER_SIZE]; + void tgl_reopen_binlog_for_writing (void) { binlog_fd = open (get_binlog_file_name (), O_WRONLY); if (binlog_fd < 0) { @@ -1916,6 +1960,32 @@ void bl_do_create_message_media_encr (long long msg_id, int from_id, int to_type add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); } +void bl_do_create_message_media_encr_pending (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) { + int *s_packet_buffer = packet_buffer; + int *s_packet_ptr = packet_ptr; + packet_buffer = b_packet_buffer; + clear_packet (); + out_int (CODE_binlog_create_message_media_encr_pending); + out_long (msg_id); + out_int (from_id); + out_int (to_type); + out_int (to_id); + out_int (date); + out_cstring (s, l); + out_ints (data, len); + add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); + packet_buffer = s_packet_buffer; + packet_ptr = s_packet_ptr; +} + +void bl_do_create_message_media_encr_sent (long long msg_id, const int *data, int len) { + clear_packet (); + out_int (CODE_binlog_create_message_media_encr_sent); + out_long (msg_id); + out_ints (data, len); + add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer)); +} + 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 unread, int l, const char *s, const int *data, int len) { clear_packet (); out_int (CODE_binlog_create_message_media_fwd); diff --git a/binlog.h b/binlog.h index 6f0732e..f0d10ce 100644 --- a/binlog.h +++ b/binlog.h @@ -78,6 +78,8 @@ void bl_do_create_message_text_fwd (int msg_id, int from_id, int to_type, int to void bl_do_create_message_service (int msg_id, int from_id, int to_type, int to_id, int date, int unread, 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, int unread, 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 unread, int l, const char *s, const int *data, int len); +void bl_do_create_message_media_encr_pending (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); +void bl_do_create_message_media_encr_sent (long long msg_id, 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 unread, 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); diff --git a/binlog.tl b/binlog.tl index 0460133..a5d6964 100644 --- a/binlog.tl +++ b/binlog.tl @@ -59,6 +59,8 @@ binlog.sendMessageActionEncr id:long from_id:int to_type:int to_id:int date:int binlog.createMessageTextFwd id:int from_id:int to_type:int to_id:int date:int fwd_from_id:int fwd_date:int unread:int text:string = binlog.Update; binlog.createMessageMedia id:int from_id:int to_type:int to_id:int date:int unread:int text:string media:MessageMedia = binlog.Update; binlog.createMessageMediaEncr id:long from_id:int to_type:int to_id:int date:int text:string media:DecryptedMessageMedia file:EncryptedFile = binlog.Update; +binlog.createMessageMediaEncrPending id:long from_id:int to_type:int to_id:int date:int text:string media:DecryptedMessageMedia = binlog.Update; +binlog.createMessageMediaEncrSent id:long file:EncryptedFile = binlog.Update; binlog.createMessageMediaFwd id:int from_id:int to_type:int to_id:int date:int fwd_from_id:int fwd_date:int unread:int text:string media:MessageMedia = binlog.Update; binlog.createMessageService id:int from_id:int to_type:int to_id:int date:int unread:int action:MessageAction = binlog.Update; binlog.createMessageServiceEncr id:long from_id:int to_type:int to_id:int date:int action:DecryptedMessageAction = binlog.Update; diff --git a/interface.c b/interface.c index 3d9dcc7..f1e5e2d 100644 --- a/interface.c +++ b/interface.c @@ -1894,11 +1894,6 @@ void print_media (struct tgl_message_media *M) { return; case tgl_message_media_photo_encr: printf ("[photo]"); - if (M->photo.caption && strlen (M->photo.caption)) { - printf ("[photo %s]", M->photo.caption); - } else { - printf ("[photo]"); - } return; case tgl_message_media_video_encr: if (M->encr_video.mime_type) { diff --git a/mtproto-client.c b/mtproto-client.c index 3027118..5e82d9a 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -457,6 +457,7 @@ static int check_prime (BIGNUM *p) { int tglmp_check_DH_params (BIGNUM *p, int g) { if (g < 2 || g > 7) { return -1; } + if (BN_num_bits (p) != 2048) { return -1; } BIGNUM t; BN_init (&t); diff --git a/mtproto-common.c b/mtproto-common.c index 53332f2..9071e7a 100644 --- a/mtproto-common.c +++ b/mtproto-common.c @@ -49,7 +49,7 @@ #endif -static int __packet_buffer[PACKET_BUFFER_SIZE]; +static int __packet_buffer[PACKET_BUFFER_SIZE + 16]; int *tgl_packet_ptr; int *tgl_packet_buffer = __packet_buffer + 16; diff --git a/queries.c b/queries.c index 1c595b4..a98eadd 100644 --- a/queries.c +++ b/queries.c @@ -1533,17 +1533,18 @@ static int send_file_on_answer (struct query *q UU) { static int send_encr_file_on_answer (struct query *q UU) { assert (fetch_int () == (int)CODE_messages_sent_encrypted_file); struct tgl_message *M = q->extra; - M->date = fetch_int (); - assert (fetch_int () == CODE_encrypted_file); - M->media.encr_photo.id = fetch_long (); - M->media.encr_photo.access_hash = fetch_long (); - //M->media.encr_photo.size = fetch_int (); + //M->date = fetch_int (); fetch_int (); - M->media.encr_photo.dc_id = fetch_int (); - assert (fetch_int () == M->media.encr_photo.key_fingerprint); + int *save = in_ptr; + + assert (skip_type_any (TYPE_TO_PARAM (encrypted_file)) >= 0); + //print_message (M); - tglm_message_insert (M); - bl_do_msg_update (M->id); + if (M->flags & FLAG_PENDING) { + bl_do_create_message_media_encr_sent (M->id, save, in_ptr - save); + //bl_do_set_message_sent (M); + bl_do_msg_update (M->id); + } if (q->callback) { ((void (*)(void *, int, struct tgl_message *))q->callback)(q->callback_extra, 1, M); @@ -1715,7 +1716,7 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra out_long (r); tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_methods, 0, callback, callback_extra); } else { - struct tgl_message *M = talloc0 (sizeof (*M)); + //struct tgl_message *M = talloc0 (sizeof (*M)); out_int (CODE_messages_send_encrypted_file); out_int (CODE_input_encrypted_chat); @@ -1731,18 +1732,19 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra out_long (r); out_random (15 + 4 * (lrand48 () % 3)); out_string (""); + int *save_ptr = packet_ptr; if (f->media_type == CODE_input_media_uploaded_photo) { out_int (CODE_decrypted_message_media_photo); - M->media.type = CODE_decrypted_message_media_photo; + //M->media.type = CODE_decrypted_message_media_photo; } else if (f->media_type == CODE_input_media_uploaded_video) { out_int (CODE_decrypted_message_media_video); - M->media.type = CODE_decrypted_message_media_video; + //M->media.type = CODE_decrypted_message_media_video; } else if (f->media_type == CODE_input_media_uploaded_audio) { out_int (CODE_decrypted_message_media_audio); - M->media.type = CODE_decrypted_message_media_audio; + //M->media.type = CODE_decrypted_message_media_audio; } else if (f->media_type == CODE_input_media_uploaded_document) { out_int (CODE_decrypted_message_media_document); - M->media.type = CODE_decrypted_message_media_document;; + //M->media.type = CODE_decrypted_message_media_document;; } else { assert (0); } @@ -1770,6 +1772,11 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra out_int (f->size); out_cstring ((void *)f->key, 32); out_cstring ((void *)f->init_iv, 32); + + long long msg_id; + tglt_secure_random (&msg_id, 8); + bl_do_create_message_media_encr_pending (msg_id, tgl_state.our_id, tgl_get_peer_type (f->to_id), tgl_get_peer_id (f->to_id), time (0), 0, 0, save_ptr, packet_ptr - save_ptr); + encr_finish (&P->encr_chat); if (f->size < (16 << 20)) { out_int (CODE_input_encrypted_file_uploaded); @@ -1790,20 +1797,22 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra out_int ((*(int *)md5) ^ (*(int *)(md5 + 4))); tfree_secure (f->iv, 32); + struct tgl_message *M = tgl_message_get (msg_id); + assert (M); - M->media.encr_photo.key = f->key; - M->media.encr_photo.iv = f->init_iv; - M->media.encr_photo.key_fingerprint = (*(int *)md5) ^ (*(int *)(md5 + 4)); - M->media.encr_photo.size = f->size; + //M->media.encr_photo.key = f->key; + //M->media.encr_photo.iv = f->init_iv; + //M->media.encr_photo.key_fingerprint = (*(int *)md5) ^ (*(int *)(md5 + 4)); + //M->media.encr_photo.size = f->size; - M->flags = FLAG_ENCRYPTED; - M->from_id = TGL_MK_USER (tgl_state.our_id); - M->to_id = f->to_id; - M->unread = 1; - M->message = tstrdup (""); - M->out = 1; - M->id = r; - M->date = time (0); + //M->flags = FLAG_ENCRYPTED; + //M->from_id = TGL_MK_USER (tgl_state.our_id); + //M->to_id = f->to_id; + //M->unread = 1; + //M->message = tstrdup (""); + //M->out = 1; + //M->id = r; + //M->date = time (0); tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_file_methods, M, callback, callback_extra); } @@ -3004,6 +3013,8 @@ static int get_dh_config_on_answer (struct query *q UU) { ensure_ptr (p); assert (tglmp_check_DH_params (p, a) >= 0); BN_free (p); + } else { + assert (tgl_state.encr_param_version); } int l = prefetch_strlen (); assert (l == 256); diff --git a/structures.c b/structures.c index 7a555db..2e3dec0 100644 --- a/structures.c +++ b/structures.c @@ -1708,7 +1708,12 @@ static void __send_msg (struct tgl_message *M) { vlogprintf (E_NOTICE, "Resending message...\n"); //print_message (M); - tgl_do_send_msg (M, 0, 0); + if (M->media.type != tgl_message_media_none) { + assert (M->flags & FLAG_ENCRYPTED); + bl_do_delete_msg (M); + } else { + tgl_do_send_msg (M, 0, 0); + } } void tglm_send_all_unsent (void ) {