Skip to content

Commit

Permalink
Fixed send_photo to encr chats
Browse files Browse the repository at this point in the history
  • Loading branch information
vvaltman committed Sep 11, 2014
1 parent 302f282 commit ce61ada
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 33 deletions.
70 changes: 70 additions & 0 deletions binlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions binlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions binlog.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions mtproto-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion mtproto-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
63 changes: 37 additions & 26 deletions queries.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion structures.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down

0 comments on commit ce61ada

Please sign in to comment.