2013-10-23 10:26:17 -04:00
|
|
|
/*
|
|
|
|
This file is part of telegram-client.
|
|
|
|
|
|
|
|
Telegram-client is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Telegram-client is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this telegram-client. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright Vitaly Valtman 2013
|
|
|
|
*/
|
2013-11-09 17:47:19 -05:00
|
|
|
|
2014-01-13 08:05:25 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2013-11-09 17:47:19 -05:00
|
|
|
#include "config.h"
|
2014-01-13 08:05:25 -05:00
|
|
|
#endif
|
2013-11-09 17:47:19 -05:00
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
#include <assert.h>
|
2013-10-31 19:18:34 -04:00
|
|
|
#include <string.h>
|
2013-10-11 16:52:20 -04:00
|
|
|
#include "structures.h"
|
|
|
|
#include "mtproto-common.h"
|
2013-10-13 06:18:08 -04:00
|
|
|
#include "telegram.h"
|
|
|
|
#include "tree.h"
|
2013-10-18 15:30:24 -04:00
|
|
|
#include "loop.h"
|
2013-11-02 06:14:30 -04:00
|
|
|
#include <openssl/aes.h>
|
|
|
|
#include <openssl/sha.h>
|
2013-11-05 17:24:26 -05:00
|
|
|
#include "queries.h"
|
2013-11-14 19:08:24 -05:00
|
|
|
#include "binlog.h"
|
2013-11-02 06:14:30 -04:00
|
|
|
|
|
|
|
#define sha1 SHA1
|
|
|
|
|
2013-11-25 11:13:36 -05:00
|
|
|
static int id_cmp (struct message *M1, struct message *M2);
|
|
|
|
#define peer_cmp(a,b) (cmp_peer_id (a->id, b->id))
|
2014-01-31 11:03:40 -05:00
|
|
|
#define peer_cmp_name(a,b) (strcmp (a->print_name, b->print_name))
|
2013-11-25 11:13:36 -05:00
|
|
|
DEFINE_TREE(peer,peer_t *,peer_cmp,0)
|
2014-01-31 11:03:40 -05:00
|
|
|
DEFINE_TREE(peer_by_name,peer_t *,peer_cmp_name,0)
|
2013-11-25 11:13:36 -05:00
|
|
|
DEFINE_TREE(message,struct message *,id_cmp,0)
|
|
|
|
|
|
|
|
|
|
|
|
struct message message_list = {
|
|
|
|
.next_use = &message_list,
|
|
|
|
.prev_use = &message_list
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tree_peer *peer_tree;
|
2014-01-31 11:03:40 -05:00
|
|
|
struct tree_peer_by_name *peer_by_name_tree;
|
2013-11-25 11:13:36 -05:00
|
|
|
struct tree_message *message_tree;
|
2013-11-29 16:43:56 -05:00
|
|
|
struct tree_message *message_unsent_tree;
|
2013-11-25 11:13:36 -05:00
|
|
|
|
|
|
|
int users_allocated;
|
|
|
|
int chats_allocated;
|
|
|
|
int messages_allocated;
|
2013-11-04 12:34:27 -05:00
|
|
|
int peer_num;
|
|
|
|
int encr_chats_allocated;
|
|
|
|
int geo_chats_allocated;
|
2013-12-02 12:19:08 -05:00
|
|
|
|
|
|
|
int our_id;
|
|
|
|
int verbosity;
|
|
|
|
|
|
|
|
peer_t *Peers[MAX_PEER_NUM];
|
2013-11-14 19:08:24 -05:00
|
|
|
extern int binlog_enabled;
|
2013-11-25 11:13:36 -05:00
|
|
|
|
|
|
|
|
2013-11-21 14:35:49 -05:00
|
|
|
void fetch_skip_photo (void);
|
2013-11-14 19:08:24 -05:00
|
|
|
|
2013-11-21 14:35:49 -05:00
|
|
|
#define code_assert(x) if (!(x)) { logprintf ("Can not parse at line %d\n", __LINE__); assert (0); return -1; }
|
2013-12-02 12:19:08 -05:00
|
|
|
#define code_try(x) if ((x) == -1) { return -1; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Fetch simple structures (immediate fetch into buffer)
|
|
|
|
*
|
|
|
|
*/
|
2013-11-21 14:35:49 -05:00
|
|
|
|
|
|
|
int fetch_file_location (struct file_location *loc) {
|
|
|
|
int x = fetch_int ();
|
|
|
|
code_assert (x == CODE_file_location_unavailable || x == CODE_file_location);
|
|
|
|
|
|
|
|
if (x == CODE_file_location_unavailable) {
|
|
|
|
loc->dc = -1;
|
|
|
|
loc->volume = fetch_long ();
|
|
|
|
loc->local_id = fetch_int ();
|
|
|
|
loc->secret = fetch_long ();
|
|
|
|
} else {
|
|
|
|
loc->dc = fetch_int ();
|
|
|
|
loc->volume = fetch_long ();
|
|
|
|
loc->local_id = fetch_int ();
|
|
|
|
loc->secret = fetch_long ();
|
2013-11-14 19:08:24 -05:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
return 0;
|
2013-11-14 19:08:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int fetch_user_status (struct user_status *S) {
|
2013-11-21 14:35:49 -05:00
|
|
|
unsigned x = fetch_int ();
|
|
|
|
code_assert (x == CODE_user_status_empty || x == CODE_user_status_online || x == CODE_user_status_offline);
|
2013-10-11 16:52:20 -04:00
|
|
|
switch (x) {
|
|
|
|
case CODE_user_status_empty:
|
|
|
|
S->online = 0;
|
2013-11-21 14:35:49 -05:00
|
|
|
S->when = 0;
|
2013-10-11 16:52:20 -04:00
|
|
|
break;
|
|
|
|
case CODE_user_status_online:
|
|
|
|
S->online = 1;
|
|
|
|
S->when = fetch_int ();
|
|
|
|
break;
|
|
|
|
case CODE_user_status_offline:
|
|
|
|
S->online = -1;
|
|
|
|
S->when = fetch_int ();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert (0);
|
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
return 0;
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
|
2013-12-02 12:19:08 -05:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Skip simple structures
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
int fetch_skip_file_location (void) {
|
|
|
|
int x = fetch_int ();
|
|
|
|
code_assert (x == CODE_file_location_unavailable || x == CODE_file_location);
|
|
|
|
|
|
|
|
if (x == CODE_file_location_unavailable) {
|
|
|
|
in_ptr += 5;
|
|
|
|
} else {
|
|
|
|
in_ptr += 6;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fetch_skip_user_status (void) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
code_assert (x == CODE_user_status_empty || x == CODE_user_status_online || x == CODE_user_status_offline);
|
|
|
|
if (x != CODE_user_status_empty) {
|
|
|
|
fetch_int ();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
char *create_print_name (peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4) {
|
|
|
|
const char *d[4];
|
|
|
|
d[0] = a1; d[1] = a2; d[2] = a3; d[3] = a4;
|
|
|
|
static char buf[10000];
|
2013-11-29 16:43:56 -05:00
|
|
|
buf[0] = 0;
|
2013-11-04 12:34:27 -05:00
|
|
|
int i;
|
|
|
|
int p = 0;
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
if (d[i] && strlen (d[i])) {
|
2014-01-10 09:37:56 -05:00
|
|
|
p += tsnprintf (buf + p, 9999 - p, "%s%s", p ? "_" : "", d[i]);
|
2013-11-04 12:34:27 -05:00
|
|
|
assert (p < 9990);
|
2013-10-13 06:18:08 -04:00
|
|
|
}
|
|
|
|
}
|
2013-11-04 12:34:27 -05:00
|
|
|
char *s = buf;
|
2013-10-13 06:18:08 -04:00
|
|
|
while (*s) {
|
|
|
|
if (*s == ' ') { *s = '_'; }
|
|
|
|
s++;
|
|
|
|
}
|
2013-11-04 12:34:27 -05:00
|
|
|
s = buf;
|
2013-11-25 12:16:34 -05:00
|
|
|
int fl = strlen (s);
|
2013-10-24 18:56:11 -04:00
|
|
|
int cc = 0;
|
|
|
|
while (1) {
|
2014-01-31 11:03:40 -05:00
|
|
|
peer_t *P = peer_lookup_name (s);
|
2014-02-07 10:40:39 -05:00
|
|
|
if (!P || !cmp_peer_id (P->id, id)) {
|
2013-10-24 18:56:11 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
cc ++;
|
2013-11-25 12:16:34 -05:00
|
|
|
assert (cc <= 9999);
|
2014-01-10 09:37:56 -05:00
|
|
|
tsnprintf (s + fl, 9999 - fl, "#%d", cc);
|
2013-10-24 18:56:11 -04:00
|
|
|
}
|
2014-01-10 06:32:57 -05:00
|
|
|
return tstrdup (s);
|
2013-11-04 12:34:27 -05:00
|
|
|
}
|
|
|
|
|
2013-12-02 12:19:08 -05:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Fetch with log event
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-11-21 14:35:49 -05:00
|
|
|
long long fetch_user_photo (struct user *U) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
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 (U, 0, 0, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
long long photo_id = 1;
|
|
|
|
if (x == CODE_user_profile_photo) {
|
|
|
|
photo_id = fetch_long ();
|
|
|
|
}
|
|
|
|
struct file_location big;
|
|
|
|
struct file_location small;
|
2013-12-02 12:19:08 -05:00
|
|
|
code_try (fetch_file_location (&small));
|
|
|
|
code_try (fetch_file_location (&big));
|
2013-11-21 14:35:49 -05:00
|
|
|
|
|
|
|
bl_do_set_user_profile_photo (U, photo_id, &big, &small);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-14 19:08:24 -05:00
|
|
|
int fetch_user (struct user *U) {
|
2013-11-04 12:34:27 -05:00
|
|
|
unsigned x = fetch_int ();
|
2013-11-21 14:35:49 -05:00
|
|
|
code_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);
|
2013-11-04 12:34:27 -05:00
|
|
|
U->id = MK_USER (fetch_int ());
|
|
|
|
if (x == CODE_user_empty) {
|
2013-11-14 19:08:24 -05:00
|
|
|
return 0;
|
2013-11-04 12:34:27 -05:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
if (x == CODE_user_self) {
|
|
|
|
assert (!our_id || (our_id == get_peer_id (U->id)));
|
|
|
|
if (!our_id) {
|
2013-11-21 14:35:49 -05:00
|
|
|
bl_do_set_our_id (get_peer_id (U->id));
|
2013-11-04 12:34:27 -05:00
|
|
|
write_auth_file ();
|
|
|
|
}
|
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
|
|
|
|
int new = 0;
|
|
|
|
if (!(U->flags & FLAG_CREATED)) {
|
|
|
|
new = 1;
|
2013-11-14 19:08:24 -05:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
if (new) {
|
|
|
|
int l1 = prefetch_strlen ();
|
2013-12-02 12:19:08 -05:00
|
|
|
code_assert (l1 >= 0);
|
2013-11-21 14:35:49 -05:00
|
|
|
char *s1 = fetch_str (l1);
|
|
|
|
int l2 = prefetch_strlen ();
|
2013-12-02 12:19:08 -05:00
|
|
|
code_assert (l2 >= 0);
|
2013-11-21 14:35:49 -05:00
|
|
|
char *s2 = fetch_str (l2);
|
|
|
|
|
|
|
|
if (x == CODE_user_deleted && !(U->flags & FLAG_DELETED)) {
|
|
|
|
bl_do_new_user (get_peer_id (U->id), s1, l1, s2, l2, 0, 0, 0, 0);
|
|
|
|
bl_do_user_delete (U);
|
2013-11-14 19:08:24 -05:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
if (x != CODE_user_deleted) {
|
|
|
|
long long access_token = 0;
|
|
|
|
if (x != CODE_user_self) {
|
|
|
|
access_token = fetch_long ();
|
|
|
|
}
|
|
|
|
int phone_len = 0;
|
|
|
|
char *phone = 0;
|
|
|
|
if (x != CODE_user_foreign) {
|
|
|
|
phone_len = prefetch_strlen ();
|
2013-12-02 12:19:08 -05:00
|
|
|
code_assert (phone_len >= 0);
|
2013-11-21 14:35:49 -05:00
|
|
|
phone = fetch_str (phone_len);
|
|
|
|
}
|
|
|
|
bl_do_new_user (get_peer_id (U->id), s1, l1, s2, l2, access_token, phone, phone_len, x == CODE_user_contact);
|
|
|
|
if (fetch_user_photo (U) < 0) { return -1; }
|
|
|
|
|
|
|
|
if (fetch_user_status (&U->status) < 0) { return -1; }
|
|
|
|
if (x == CODE_user_self) {
|
|
|
|
fetch_bool ();
|
2013-11-16 08:04:53 -05:00
|
|
|
}
|
2013-11-14 19:08:24 -05:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
} else {
|
|
|
|
int l1 = prefetch_strlen ();
|
|
|
|
char *s1 = fetch_str (l1);
|
|
|
|
int l2 = prefetch_strlen ();
|
|
|
|
char *s2 = fetch_str (l2);
|
|
|
|
|
|
|
|
bl_do_set_user_name (U, s1, l1, s2, l2);
|
|
|
|
|
|
|
|
if (x == CODE_user_deleted && !(U->flags & FLAG_DELETED)) {
|
|
|
|
bl_do_user_delete (U);
|
2013-11-14 19:08:24 -05:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
if (x != CODE_user_deleted) {
|
|
|
|
if (x != CODE_user_self) {
|
|
|
|
bl_do_set_user_access_token (U, fetch_long ());
|
|
|
|
}
|
|
|
|
if (x != CODE_user_foreign) {
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
char *s = fetch_str (l);
|
|
|
|
bl_do_set_user_phone (U, s, l);
|
|
|
|
}
|
|
|
|
if (fetch_user_photo (U) < 0) { return -1; }
|
|
|
|
|
|
|
|
fetch_user_status (&U->status);
|
|
|
|
if (x == CODE_user_self) {
|
|
|
|
fetch_bool ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x == CODE_user_contact) {
|
|
|
|
bl_do_set_user_friend (U, 1);
|
|
|
|
} else {
|
|
|
|
bl_do_set_user_friend (U, 0);
|
|
|
|
}
|
2013-10-25 15:50:10 -04:00
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
return 0;
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
2013-10-13 06:18:08 -04:00
|
|
|
|
2013-11-02 13:01:22 -04:00
|
|
|
void fetch_encrypted_chat (struct secret_chat *U) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
assert (x == CODE_encrypted_chat_empty || x == CODE_encrypted_chat_waiting || x == CODE_encrypted_chat_requested || x == CODE_encrypted_chat || x == CODE_encrypted_chat_discarded);
|
|
|
|
U->id = MK_ENCR_CHAT (fetch_int ());
|
|
|
|
if (x == CODE_encrypted_chat_empty) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
int new = !(U->flags & FLAG_CREATED);
|
|
|
|
|
2013-11-02 13:01:22 -04:00
|
|
|
if (x == CODE_encrypted_chat_discarded) {
|
2013-11-21 14:35:49 -05:00
|
|
|
if (new) {
|
|
|
|
logprintf ("Unknown chat in deleted state. May be we forgot something...\n");
|
|
|
|
return;
|
2013-11-04 12:34:27 -05:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
bl_do_encr_chat_delete (U);
|
|
|
|
write_secret_chat_file ();
|
2013-11-02 13:01:22 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-11-04 12:34:27 -05:00
|
|
|
|
2013-11-21 14:35:49 -05:00
|
|
|
static char g_key[256];
|
|
|
|
static char nonce[256];
|
|
|
|
if (new) {
|
|
|
|
long long access_hash = fetch_long ();
|
|
|
|
int date = fetch_int ();
|
|
|
|
int admin_id = fetch_int ();
|
|
|
|
int user_id = fetch_int () + admin_id - our_id;
|
|
|
|
|
|
|
|
if (x == CODE_encrypted_chat_waiting) {
|
|
|
|
logprintf ("Unknown chat in waiting state. May be we forgot something...\n");
|
|
|
|
return;
|
2013-11-02 13:01:22 -04:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
if (x == CODE_encrypted_chat_requested || x == CODE_encrypted_chat) {
|
|
|
|
memset (g_key, 0, sizeof (g_key));
|
|
|
|
memset (nonce, 0, sizeof (nonce));
|
2013-11-02 13:01:22 -04:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
int l = prefetch_strlen ();
|
|
|
|
char *s = fetch_str (l);
|
2013-11-29 16:43:56 -05:00
|
|
|
if (l != 256) { logprintf ("l = %d\n", l); }
|
2013-11-04 12:34:27 -05:00
|
|
|
if (l < 256) {
|
2013-11-21 14:35:49 -05:00
|
|
|
memcpy (g_key + 256 - l, s, l);
|
2013-11-04 12:34:27 -05:00
|
|
|
} else {
|
2013-11-21 14:35:49 -05:00
|
|
|
memcpy (g_key, s + (l - 256), 256);
|
2013-11-04 12:34:27 -05:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
|
2013-12-23 19:57:01 -05:00
|
|
|
/*l = prefetch_strlen ();
|
2013-11-04 12:34:27 -05:00
|
|
|
s = fetch_str (l);
|
2013-11-29 16:43:56 -05:00
|
|
|
if (l != 256) { logprintf ("l = %d\n", l); }
|
2013-11-04 12:34:27 -05:00
|
|
|
if (l < 256) {
|
2013-11-21 14:35:49 -05:00
|
|
|
memcpy (nonce + 256 - l, s, l);
|
2013-11-04 12:34:27 -05:00
|
|
|
} else {
|
2013-11-21 14:35:49 -05:00
|
|
|
memcpy (nonce, s + (l - 256), 256);
|
2013-12-23 19:57:01 -05:00
|
|
|
}*/
|
2013-11-21 14:35:49 -05:00
|
|
|
|
|
|
|
if (x == CODE_encrypted_chat) {
|
|
|
|
fetch_long (); // fingerprint
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x == CODE_encrypted_chat) {
|
|
|
|
logprintf ("Unknown chat in ok state. May be we forgot something...\n");
|
|
|
|
return;
|
2013-11-14 19:08:24 -05:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
|
|
|
|
bl_do_encr_chat_requested (U, access_hash, date, admin_id, user_id, (void *)g_key, (void *)nonce);
|
|
|
|
write_secret_chat_file ();
|
2013-11-02 13:01:22 -04:00
|
|
|
} else {
|
2013-11-21 14:35:49 -05:00
|
|
|
bl_do_set_encr_chat_access_hash (U, fetch_long ());
|
|
|
|
bl_do_set_encr_chat_date (U, fetch_int ());
|
|
|
|
if (fetch_int () != U->admin_id) {
|
|
|
|
logprintf ("Changed admin in secret chat. WTF?\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (U->user_id != U->admin_id + fetch_int () - our_id) {
|
|
|
|
logprintf ("Changed partner in secret chat. WTF?\n");
|
|
|
|
return;
|
2013-11-02 13:01:22 -04:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
if (x == CODE_encrypted_chat_waiting) {
|
|
|
|
bl_do_set_encr_chat_state (U, sc_waiting);
|
|
|
|
write_secret_chat_file ();
|
|
|
|
return; // We needed only access hash from here
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x == CODE_encrypted_chat_requested || x == CODE_encrypted_chat) {
|
|
|
|
memset (g_key, 0, sizeof (g_key));
|
|
|
|
memset (nonce, 0, sizeof (nonce));
|
2013-11-02 13:01:22 -04:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
int l = prefetch_strlen ();
|
|
|
|
char *s = fetch_str (l);
|
2013-11-29 16:43:56 -05:00
|
|
|
if (l != 256) { logprintf ("l = %d\n", l); }
|
2013-11-04 12:34:27 -05:00
|
|
|
if (l < 256) {
|
2013-11-21 14:35:49 -05:00
|
|
|
memcpy (g_key + 256 - l, s, l);
|
2013-11-04 12:34:27 -05:00
|
|
|
} else {
|
2013-11-21 14:35:49 -05:00
|
|
|
memcpy (g_key, s + (l - 256), 256);
|
2013-11-04 12:34:27 -05:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
|
2013-12-23 19:57:01 -05:00
|
|
|
/*l = prefetch_strlen ();
|
2013-11-04 12:34:27 -05:00
|
|
|
s = fetch_str (l);
|
2013-11-29 16:43:56 -05:00
|
|
|
if (l != 256) { logprintf ("l = %d\n", l); }
|
2013-11-04 12:34:27 -05:00
|
|
|
if (l < 256) {
|
2013-11-21 14:35:49 -05:00
|
|
|
memcpy (nonce + 256 - l, s, l);
|
2013-11-04 12:34:27 -05:00
|
|
|
} else {
|
2013-11-21 14:35:49 -05:00
|
|
|
memcpy (nonce, s + (l - 256), 256);
|
2013-12-23 19:57:01 -05:00
|
|
|
}*/
|
2013-11-21 14:35:49 -05:00
|
|
|
|
|
|
|
if (x == CODE_encrypted_chat_requested) {
|
|
|
|
return; // Duplicate?
|
2013-11-05 17:24:26 -05:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
bl_do_encr_chat_accepted (U, (void *)g_key, (void *)nonce, fetch_long ());
|
2013-11-02 13:01:22 -04:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
write_secret_chat_file ();
|
2013-11-02 13:01:22 -04:00
|
|
|
}
|
|
|
|
|
2013-10-25 13:29:02 -04:00
|
|
|
void fetch_notify_settings (void);
|
|
|
|
void fetch_user_full (struct user *U) {
|
|
|
|
assert (fetch_int () == CODE_user_full);
|
|
|
|
fetch_alloc_user ();
|
|
|
|
unsigned x;
|
|
|
|
assert (fetch_int () == (int)CODE_contacts_link);
|
|
|
|
x = fetch_int ();
|
|
|
|
assert (x == CODE_contacts_my_link_empty || x == CODE_contacts_my_link_requested || x == CODE_contacts_my_link_contact);
|
|
|
|
if (x == CODE_contacts_my_link_requested) {
|
|
|
|
fetch_bool ();
|
|
|
|
}
|
|
|
|
x = fetch_int ();
|
|
|
|
assert (x == CODE_contacts_foreign_link_unknown || x == CODE_contacts_foreign_link_requested || x == CODE_contacts_foreign_link_mutual);
|
2013-10-25 13:33:18 -04:00
|
|
|
if (x == CODE_contacts_foreign_link_requested) {
|
2013-10-25 13:29:02 -04:00
|
|
|
fetch_bool ();
|
|
|
|
}
|
|
|
|
fetch_alloc_user ();
|
2013-11-21 14:35:49 -05:00
|
|
|
|
|
|
|
int *start = in_ptr;
|
|
|
|
fetch_skip_photo ();
|
|
|
|
bl_do_set_user_full_photo (U, start, 4 * (in_ptr - start));
|
|
|
|
|
2013-10-25 13:29:02 -04:00
|
|
|
fetch_notify_settings ();
|
2013-11-21 14:35:49 -05:00
|
|
|
|
|
|
|
bl_do_set_user_blocked (U, fetch_bool ());
|
|
|
|
int l1 = prefetch_strlen ();
|
|
|
|
char *s1 = fetch_str (l1);
|
|
|
|
int l2 = prefetch_strlen ();
|
|
|
|
char *s2 = fetch_str (l2);
|
|
|
|
bl_do_set_user_real_name (U, s1, l1, s2, l2);
|
2013-10-25 13:29:02 -04:00
|
|
|
}
|
|
|
|
|
2013-10-13 08:30:53 -04:00
|
|
|
void fetch_chat (struct chat *C) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
assert (x == CODE_chat_empty || x == CODE_chat || x == CODE_chat_forbidden);
|
2013-10-31 19:18:34 -04:00
|
|
|
C->id = MK_CHAT (fetch_int ());
|
2013-10-13 08:30:53 -04:00
|
|
|
if (x == CODE_chat_empty) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-22 18:26:35 -05:00
|
|
|
int new = !(C->flags & FLAG_CREATED);
|
|
|
|
if (new) {
|
|
|
|
int y = 0;
|
|
|
|
if (x == CODE_chat_forbidden) {
|
|
|
|
y |= FLAG_FORBIDDEN;
|
2013-10-13 08:30:53 -04:00
|
|
|
}
|
2013-11-22 18:26:35 -05:00
|
|
|
int l = prefetch_strlen ();
|
|
|
|
char *s = fetch_str (l);
|
|
|
|
|
|
|
|
struct file_location small;
|
|
|
|
struct file_location big;
|
|
|
|
memset (&small, 0, sizeof (small));
|
|
|
|
memset (&big, 0, sizeof (big));
|
|
|
|
int users_num = -1;
|
|
|
|
int date = 0;
|
|
|
|
int version = -1;
|
|
|
|
|
|
|
|
if (x == CODE_chat) {
|
2014-01-15 07:10:52 -05:00
|
|
|
unsigned z = fetch_int ();
|
|
|
|
if (z == CODE_chat_photo_empty) {
|
2013-11-22 18:26:35 -05:00
|
|
|
small.dc = -2;
|
|
|
|
big.dc = -2;
|
|
|
|
} else {
|
2014-01-15 07:10:52 -05:00
|
|
|
assert (z == CODE_chat_photo);
|
2013-11-22 18:26:35 -05:00
|
|
|
fetch_file_location (&small);
|
|
|
|
fetch_file_location (&big);
|
|
|
|
}
|
|
|
|
users_num = fetch_int ();
|
|
|
|
date = fetch_int ();
|
|
|
|
if (fetch_bool ()) {
|
|
|
|
y |= FLAG_CHAT_IN_CHAT;
|
|
|
|
}
|
|
|
|
version = fetch_int ();
|
|
|
|
} else {
|
|
|
|
small.dc = -2;
|
|
|
|
big.dc = -2;
|
|
|
|
users_num = -1;
|
|
|
|
date = fetch_int ();
|
|
|
|
version = -1;
|
2013-10-14 13:26:25 -04:00
|
|
|
}
|
2013-11-22 18:26:35 -05:00
|
|
|
|
|
|
|
bl_do_create_chat (C, y, s, l, users_num, date, version, &big, &small);
|
2013-10-14 13:26:25 -04:00
|
|
|
} else {
|
2013-11-22 18:26:35 -05:00
|
|
|
if (x == CODE_chat_forbidden) {
|
|
|
|
bl_do_chat_forbid (C, 1);
|
|
|
|
} else {
|
|
|
|
bl_do_chat_forbid (C, 0);
|
|
|
|
}
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
char *s = fetch_str (l);
|
|
|
|
bl_do_set_chat_title (C, s, l);
|
|
|
|
|
|
|
|
struct file_location small;
|
|
|
|
struct file_location big;
|
|
|
|
memset (&small, 0, sizeof (small));
|
|
|
|
memset (&big, 0, sizeof (big));
|
|
|
|
|
|
|
|
if (x == CODE_chat) {
|
|
|
|
unsigned y = fetch_int ();
|
|
|
|
if (y == CODE_chat_photo_empty) {
|
|
|
|
small.dc = -2;
|
|
|
|
big.dc = -2;
|
|
|
|
} else {
|
|
|
|
assert (y == CODE_chat_photo);
|
|
|
|
fetch_file_location (&small);
|
|
|
|
fetch_file_location (&big);
|
|
|
|
}
|
|
|
|
bl_do_set_chat_photo (C, &big, &small);
|
|
|
|
int users_num = fetch_int ();
|
|
|
|
bl_do_set_chat_date (C, fetch_int ());
|
|
|
|
bl_do_set_chat_set_in_chat (C, fetch_bool ());
|
|
|
|
bl_do_set_chat_version (C, users_num, fetch_int ());
|
|
|
|
} else {
|
|
|
|
bl_do_set_chat_date (C, fetch_int ());
|
|
|
|
}
|
2013-10-14 13:26:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 13:04:44 -04:00
|
|
|
void fetch_notify_settings (void) {
|
|
|
|
unsigned x = fetch_int ();
|
2013-10-25 18:03:36 -04:00
|
|
|
assert (x == CODE_peer_notify_settings || x == CODE_peer_notify_settings_empty || x == CODE_peer_notify_settings_old);
|
|
|
|
if (x == CODE_peer_notify_settings_old) {
|
2013-10-24 13:04:44 -04:00
|
|
|
fetch_int (); // mute_until
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
fetch_bool (); // show_previews
|
|
|
|
fetch_int (); // peer notify events
|
|
|
|
}
|
2013-10-25 18:03:36 -04:00
|
|
|
if (x == CODE_peer_notify_settings) {
|
|
|
|
fetch_int (); // mute_until
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
fetch_bool (); // show_previews
|
|
|
|
fetch_int (); // events_mask
|
|
|
|
}
|
2013-10-24 13:04:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void fetch_chat_full (struct chat *C) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
assert (x == CODE_messages_chat_full);
|
|
|
|
assert (fetch_int () == CODE_chat_full);
|
2013-10-31 19:18:34 -04:00
|
|
|
C->id = MK_CHAT (fetch_int ());
|
2013-11-25 11:13:36 -05:00
|
|
|
//C->flags &= ~(FLAG_DELETED | FLAG_FORBIDDEN | FLAG_CHAT_IN_CHAT);
|
|
|
|
//C->flags |= FLAG_CREATED;
|
2013-10-24 13:04:44 -04:00
|
|
|
x = fetch_int ();
|
2013-11-25 11:13:36 -05:00
|
|
|
int version = 0;
|
|
|
|
struct chat_user *users = 0;
|
|
|
|
int users_num = 0;
|
|
|
|
int admin_id = 0;
|
|
|
|
|
2013-10-24 13:04:44 -04:00
|
|
|
if (x == CODE_chat_participants) {
|
2013-10-31 19:18:34 -04:00
|
|
|
assert (fetch_int () == get_peer_id (C->id));
|
2013-11-25 11:13:36 -05:00
|
|
|
admin_id = fetch_int ();
|
2013-10-24 13:04:44 -04:00
|
|
|
assert (fetch_int () == CODE_vector);
|
2013-11-25 11:13:36 -05:00
|
|
|
users_num = fetch_int ();
|
2014-01-10 06:32:57 -05:00
|
|
|
users = talloc (sizeof (struct chat_user) * users_num);
|
2013-10-24 13:04:44 -04:00
|
|
|
int i;
|
2013-11-25 12:16:34 -05:00
|
|
|
for (i = 0; i < users_num; i++) {
|
2013-10-24 13:04:44 -04:00
|
|
|
assert (fetch_int () == (int)CODE_chat_participant);
|
2013-11-25 11:13:36 -05:00
|
|
|
users[i].user_id = fetch_int ();
|
|
|
|
users[i].inviter_id = fetch_int ();
|
|
|
|
users[i].date = fetch_int ();
|
2013-10-24 13:04:44 -04:00
|
|
|
}
|
2013-11-25 11:13:36 -05:00
|
|
|
version = fetch_int ();
|
2013-10-24 13:04:44 -04:00
|
|
|
}
|
2013-11-25 11:13:36 -05:00
|
|
|
int *start = in_ptr;
|
|
|
|
fetch_skip_photo ();
|
|
|
|
int *end = in_ptr;
|
2013-10-24 13:04:44 -04:00
|
|
|
fetch_notify_settings ();
|
|
|
|
|
|
|
|
int n, i;
|
|
|
|
assert (fetch_int () == CODE_vector);
|
|
|
|
n = fetch_int ();
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
fetch_alloc_chat ();
|
|
|
|
}
|
|
|
|
assert (fetch_int () == CODE_vector);
|
|
|
|
n = fetch_int ();
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
fetch_alloc_user ();
|
|
|
|
}
|
2013-11-25 11:13:36 -05:00
|
|
|
if (admin_id) {
|
|
|
|
bl_do_set_chat_admin (C, admin_id);
|
|
|
|
}
|
|
|
|
if (version > 0) {
|
|
|
|
bl_do_set_chat_participants (C, version, users_num, users);
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree (users, sizeof (struct chat_user) * users_num);
|
2013-11-25 11:13:36 -05:00
|
|
|
}
|
|
|
|
bl_do_set_chat_full_photo (C, start, 4 * (end - start));
|
2013-10-24 13:04:44 -04:00
|
|
|
}
|
|
|
|
|
2013-10-14 13:26:25 -04:00
|
|
|
void fetch_photo_size (struct photo_size *S) {
|
2013-10-18 12:00:47 -04:00
|
|
|
memset (S, 0, sizeof (*S));
|
2013-10-14 13:26:25 -04:00
|
|
|
unsigned x = fetch_int ();
|
2013-10-21 15:27:29 -04:00
|
|
|
assert (x == CODE_photo_size || x == CODE_photo_cached_size || x == CODE_photo_size_empty);
|
2013-10-14 13:26:25 -04:00
|
|
|
S->type = fetch_str_dup ();
|
2013-10-21 15:27:29 -04:00
|
|
|
if (x != CODE_photo_size_empty) {
|
|
|
|
fetch_file_location (&S->loc);
|
|
|
|
S->w = fetch_int ();
|
|
|
|
S->h = fetch_int ();
|
|
|
|
if (x == CODE_photo_size) {
|
|
|
|
S->size = fetch_int ();
|
|
|
|
} else {
|
2013-10-27 15:35:02 -04:00
|
|
|
S->size = prefetch_strlen ();
|
2014-01-10 06:32:57 -05:00
|
|
|
// S->data = talloc (S->size);
|
2013-11-11 13:35:31 -05:00
|
|
|
fetch_str (S->size);
|
|
|
|
// memcpy (S->data, fetch_str (S->size), S->size);
|
2013-10-21 15:27:29 -04:00
|
|
|
}
|
2013-10-14 13:26:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-21 14:35:49 -05:00
|
|
|
void fetch_skip_photo_size (void) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
assert (x == CODE_photo_size || x == CODE_photo_cached_size || x == CODE_photo_size_empty);
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // type
|
|
|
|
if (x != CODE_photo_size_empty) {
|
|
|
|
fetch_skip_file_location ();
|
|
|
|
in_ptr += 2; // w, h
|
|
|
|
if (x == CODE_photo_size) {
|
|
|
|
in_ptr ++;
|
|
|
|
} else {
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 13:26:25 -04:00
|
|
|
void fetch_geo (struct geo *G) {
|
2013-10-18 12:00:47 -04:00
|
|
|
unsigned x = fetch_int ();
|
|
|
|
if (x == CODE_geo_point) {
|
|
|
|
G->longitude = fetch_double ();
|
|
|
|
G->latitude = fetch_double ();
|
|
|
|
} else {
|
|
|
|
assert (x == CODE_geo_point_empty);
|
|
|
|
G->longitude = 0;
|
|
|
|
G->latitude = 0;
|
|
|
|
}
|
2013-10-14 13:26:25 -04:00
|
|
|
}
|
|
|
|
|
2013-11-21 14:35:49 -05:00
|
|
|
void fetch_skip_geo (void) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
assert (x == CODE_geo_point || x == CODE_geo_point_empty);
|
|
|
|
if (x == CODE_geo_point) {
|
2013-11-29 16:43:56 -05:00
|
|
|
in_ptr += 4;
|
2013-11-21 14:35:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 13:26:25 -04:00
|
|
|
void fetch_photo (struct photo *P) {
|
|
|
|
memset (P, 0, sizeof (*P));
|
|
|
|
unsigned x = fetch_int ();
|
2013-10-24 13:04:44 -04:00
|
|
|
assert (x == CODE_photo_empty || x == CODE_photo);
|
2013-10-14 13:26:25 -04:00
|
|
|
P->id = fetch_long ();
|
|
|
|
if (x == CODE_photo_empty) { return; }
|
|
|
|
P->access_hash = fetch_long ();
|
|
|
|
P->user_id = fetch_int ();
|
|
|
|
P->date = fetch_int ();
|
|
|
|
P->caption = fetch_str_dup ();
|
|
|
|
fetch_geo (&P->geo);
|
|
|
|
assert (fetch_int () == CODE_vector);
|
|
|
|
P->sizes_num = fetch_int ();
|
2014-01-10 06:32:57 -05:00
|
|
|
P->sizes = talloc (sizeof (struct photo_size) * P->sizes_num);
|
2013-10-14 13:26:25 -04:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < P->sizes_num; i++) {
|
|
|
|
fetch_photo_size (&P->sizes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-21 14:35:49 -05:00
|
|
|
void fetch_skip_photo (void) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
assert (x == CODE_photo_empty || x == CODE_photo);
|
|
|
|
in_ptr += 2; // id
|
|
|
|
if (x == CODE_photo_empty) { return; }
|
|
|
|
in_ptr += 2 +1 + 1; // access_hash, user_id, date
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // caption
|
|
|
|
fetch_skip_geo ();
|
|
|
|
assert (fetch_int () == CODE_vector);
|
|
|
|
int n = fetch_int ();
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
fetch_skip_photo_size ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 13:26:25 -04:00
|
|
|
void fetch_video (struct video *V) {
|
|
|
|
memset (V, 0, sizeof (*V));
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
V->id = fetch_long ();
|
|
|
|
if (x == CODE_video_empty) { return; }
|
|
|
|
V->access_hash = fetch_long ();
|
|
|
|
V->user_id = fetch_int ();
|
|
|
|
V->date = fetch_int ();
|
|
|
|
V->caption = fetch_str_dup ();
|
|
|
|
V->duration = fetch_int ();
|
|
|
|
V->size = fetch_int ();
|
|
|
|
fetch_photo_size (&V->thumb);
|
|
|
|
V->dc_id = fetch_int ();
|
|
|
|
V->w = fetch_int ();
|
|
|
|
V->h = fetch_int ();
|
|
|
|
}
|
|
|
|
|
2013-11-29 16:43:56 -05:00
|
|
|
void fetch_skip_video (void) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
fetch_long ();
|
|
|
|
if (x == CODE_video_empty) { return; }
|
|
|
|
fetch_skip (4);
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
fetch_skip (2);
|
|
|
|
fetch_skip_photo_size ();
|
|
|
|
fetch_skip (3);
|
|
|
|
}
|
|
|
|
|
2013-11-15 05:37:14 -05:00
|
|
|
void fetch_audio (struct audio *V) {
|
|
|
|
memset (V, 0, sizeof (*V));
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
V->id = fetch_long ();
|
|
|
|
if (x == CODE_audio_empty) { return; }
|
|
|
|
V->access_hash = fetch_long ();
|
|
|
|
V->user_id = fetch_int ();
|
|
|
|
V->date = fetch_int ();
|
|
|
|
V->duration = fetch_int ();
|
|
|
|
V->size = fetch_int ();
|
|
|
|
V->dc_id = fetch_int ();
|
|
|
|
}
|
|
|
|
|
2013-11-29 16:43:56 -05:00
|
|
|
void fetch_skip_audio (void) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
fetch_skip (2);
|
|
|
|
if (x == CODE_audio_empty) { return; }
|
|
|
|
fetch_skip (7);
|
|
|
|
}
|
|
|
|
|
2013-11-15 05:37:14 -05:00
|
|
|
void fetch_document (struct document *V) {
|
|
|
|
memset (V, 0, sizeof (*V));
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
V->id = fetch_long ();
|
|
|
|
if (x == CODE_document_empty) { return; }
|
|
|
|
V->access_hash = fetch_long ();
|
|
|
|
V->user_id = fetch_int ();
|
|
|
|
V->date = fetch_int ();
|
|
|
|
V->caption = fetch_str_dup ();
|
|
|
|
V->mime_type = fetch_str_dup ();
|
|
|
|
V->size = fetch_int ();
|
|
|
|
fetch_photo_size (&V->thumb);
|
|
|
|
V->dc_id = fetch_int ();
|
|
|
|
}
|
|
|
|
|
2013-11-29 16:43:56 -05:00
|
|
|
void fetch_skip_document (void) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
fetch_skip (2);
|
|
|
|
if (x == CODE_document_empty) { return; }
|
|
|
|
fetch_skip (4);
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
fetch_skip (1);
|
|
|
|
fetch_skip_photo_size ();
|
|
|
|
fetch_skip (1);
|
|
|
|
}
|
|
|
|
|
2013-10-14 13:26:25 -04:00
|
|
|
void fetch_message_action (struct message_action *M) {
|
|
|
|
memset (M, 0, sizeof (*M));
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
M->type = x;
|
|
|
|
switch (x) {
|
|
|
|
case CODE_message_action_empty:
|
|
|
|
break;
|
2013-11-22 18:26:35 -05:00
|
|
|
case CODE_message_action_geo_chat_create:
|
|
|
|
{
|
|
|
|
int l = prefetch_strlen (); // title
|
|
|
|
char *s = fetch_str (l);
|
|
|
|
int l2 = prefetch_strlen (); // checkin
|
|
|
|
char *s2 = fetch_str (l2);
|
|
|
|
logprintf ("Message action: Created geochat %.*s in address %.*s\n", l, s, l2, s2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_message_action_geo_chat_checkin:
|
|
|
|
break;
|
2013-10-14 13:26:25 -04:00
|
|
|
case CODE_message_action_chat_create:
|
|
|
|
M->title = fetch_str_dup ();
|
|
|
|
assert (fetch_int () == (int)CODE_vector);
|
|
|
|
M->user_num = fetch_int ();
|
2014-01-10 06:32:57 -05:00
|
|
|
M->users = talloc (M->user_num * 4);
|
2013-10-14 13:26:25 -04:00
|
|
|
fetch_ints (M->users, M->user_num);
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_edit_title:
|
|
|
|
M->new_title = fetch_str_dup ();
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_edit_photo:
|
|
|
|
fetch_photo (&M->photo);
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_delete_photo:
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_add_user:
|
|
|
|
M->user = fetch_int ();
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_delete_user:
|
|
|
|
M->user = fetch_int ();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-29 16:43:56 -05:00
|
|
|
void fetch_skip_message_action (void) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
int l;
|
|
|
|
switch (x) {
|
|
|
|
case CODE_message_action_empty:
|
|
|
|
break;
|
|
|
|
case CODE_message_action_geo_chat_create:
|
|
|
|
{
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_message_action_geo_chat_checkin:
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_create:
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
assert (fetch_int () == (int)CODE_vector);
|
|
|
|
l = fetch_int ();
|
|
|
|
fetch_skip (l);
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_edit_title:
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_edit_photo:
|
|
|
|
fetch_skip_photo ();
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_delete_photo:
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_add_user:
|
|
|
|
fetch_int ();
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_delete_user:
|
|
|
|
fetch_int ();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 15:19:39 -04:00
|
|
|
void fetch_message_short (struct message *M) {
|
2013-11-29 16:43:56 -05:00
|
|
|
int new = !(M->flags & FLAG_CREATED);
|
|
|
|
|
|
|
|
if (new) {
|
|
|
|
int id = fetch_int ();
|
|
|
|
int from_id = fetch_int ();
|
|
|
|
int to_id = our_id;
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
char *s = fetch_str (l);
|
|
|
|
|
|
|
|
fetch_pts ();
|
|
|
|
|
|
|
|
int date = fetch_int ();
|
|
|
|
fetch_seq ();
|
|
|
|
|
|
|
|
bl_do_create_message_text (id, from_id, PEER_USER, to_id, date, l, s);
|
|
|
|
} else {
|
|
|
|
fetch_int (); // id
|
|
|
|
fetch_int (); // from_id
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // text
|
|
|
|
|
|
|
|
fetch_pts ();
|
|
|
|
fetch_int ();
|
|
|
|
fetch_seq ();
|
|
|
|
}
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void fetch_message_short_chat (struct message *M) {
|
2013-11-29 16:43:56 -05:00
|
|
|
int new = !(M->flags & FLAG_CREATED);
|
|
|
|
|
|
|
|
if (new) {
|
|
|
|
int id = fetch_int ();
|
|
|
|
int from_id = fetch_int ();
|
|
|
|
int to_id = fetch_int ();
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
char *s = fetch_str (l);
|
|
|
|
|
|
|
|
fetch_pts ();
|
|
|
|
|
|
|
|
int date = fetch_int ();
|
|
|
|
fetch_seq ();
|
|
|
|
|
|
|
|
bl_do_create_message_text (id, from_id, PEER_CHAT, to_id, date, l, s);
|
|
|
|
} else {
|
|
|
|
fetch_int (); // id
|
|
|
|
fetch_int (); // from_id
|
|
|
|
fetch_int (); // to_id
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // text
|
|
|
|
|
|
|
|
fetch_pts ();
|
|
|
|
fetch_int ();
|
|
|
|
fetch_seq ();
|
|
|
|
}
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-14 13:26:25 -04:00
|
|
|
void fetch_message_media (struct message_media *M) {
|
|
|
|
memset (M, 0, sizeof (*M));
|
|
|
|
M->type = fetch_int ();
|
|
|
|
switch (M->type) {
|
|
|
|
case CODE_message_media_empty:
|
|
|
|
break;
|
|
|
|
case CODE_message_media_photo:
|
|
|
|
fetch_photo (&M->photo);
|
|
|
|
break;
|
|
|
|
case CODE_message_media_video:
|
|
|
|
fetch_video (&M->video);
|
|
|
|
break;
|
2013-11-15 05:37:14 -05:00
|
|
|
case CODE_message_media_audio:
|
|
|
|
fetch_audio (&M->audio);
|
|
|
|
break;
|
|
|
|
case CODE_message_media_document:
|
|
|
|
fetch_document (&M->document);
|
|
|
|
break;
|
2013-10-14 13:26:25 -04:00
|
|
|
case CODE_message_media_geo:
|
|
|
|
fetch_geo (&M->geo);
|
|
|
|
break;
|
|
|
|
case CODE_message_media_contact:
|
|
|
|
M->phone = fetch_str_dup ();
|
|
|
|
M->first_name = fetch_str_dup ();
|
|
|
|
M->last_name = fetch_str_dup ();
|
|
|
|
M->user_id = fetch_int ();
|
|
|
|
break;
|
|
|
|
case CODE_message_media_unsupported:
|
2014-01-10 09:37:56 -05:00
|
|
|
M->data_size = prefetch_strlen ();
|
|
|
|
M->data = talloc (M->data_size);
|
|
|
|
memcpy (M->data, fetch_str (M->data_size), M->data_size);
|
2013-10-14 13:26:25 -04:00
|
|
|
break;
|
|
|
|
default:
|
2013-10-18 12:00:47 -04:00
|
|
|
logprintf ("type = 0x%08x\n", M->type);
|
2013-10-14 13:26:25 -04:00
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-29 16:43:56 -05:00
|
|
|
void fetch_skip_message_media (void) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
switch (x) {
|
|
|
|
case CODE_message_media_empty:
|
|
|
|
break;
|
|
|
|
case CODE_message_media_photo:
|
|
|
|
fetch_skip_photo ();
|
|
|
|
break;
|
|
|
|
case CODE_message_media_video:
|
|
|
|
fetch_skip_video ();
|
|
|
|
break;
|
|
|
|
case CODE_message_media_audio:
|
|
|
|
fetch_skip_audio ();
|
|
|
|
break;
|
|
|
|
case CODE_message_media_document:
|
|
|
|
fetch_skip_document ();
|
|
|
|
break;
|
|
|
|
case CODE_message_media_geo:
|
|
|
|
fetch_skip_geo ();
|
|
|
|
break;
|
|
|
|
case CODE_message_media_contact:
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
fetch_int ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_message_media_unsupported:
|
|
|
|
{
|
|
|
|
int l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logprintf ("type = 0x%08x\n", x);
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fetch_skip_message_media_encrypted (void) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
int l;
|
|
|
|
switch (x) {
|
|
|
|
case CODE_decrypted_message_media_empty:
|
|
|
|
break;
|
|
|
|
case CODE_decrypted_message_media_photo:
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // thumb
|
|
|
|
fetch_skip (5);
|
|
|
|
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
break;
|
|
|
|
case CODE_decrypted_message_media_video:
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // thumb
|
|
|
|
|
|
|
|
fetch_skip (6);
|
|
|
|
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
break;
|
|
|
|
case CODE_decrypted_message_media_audio:
|
|
|
|
fetch_skip (2);
|
|
|
|
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
break;
|
|
|
|
case CODE_decrypted_message_media_document:
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // thumb
|
|
|
|
|
|
|
|
fetch_skip (2);
|
|
|
|
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // thumb
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // thumb
|
|
|
|
fetch_skip (1);
|
|
|
|
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l);
|
|
|
|
break;
|
|
|
|
case CODE_decrypted_message_media_geo_point:
|
|
|
|
fetch_skip (4);
|
|
|
|
break;
|
|
|
|
case CODE_decrypted_message_media_contact:
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // thumb
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // thumb
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // thumb
|
|
|
|
fetch_skip (1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logprintf ("type = 0x%08x\n", x);
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-02 13:01:22 -04:00
|
|
|
void fetch_message_media_encrypted (struct message_media *M) {
|
|
|
|
memset (M, 0, sizeof (*M));
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
int l;
|
2013-11-04 12:34:27 -05:00
|
|
|
switch (x) {
|
2013-11-02 13:01:22 -04:00
|
|
|
case CODE_decrypted_message_media_empty:
|
|
|
|
M->type = CODE_message_media_empty;
|
|
|
|
break;
|
|
|
|
case CODE_decrypted_message_media_photo:
|
|
|
|
M->type = x;
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // thumb
|
|
|
|
fetch_int (); // thumb_w
|
|
|
|
fetch_int (); // thumb_h
|
|
|
|
M->encr_photo.w = fetch_int ();
|
|
|
|
M->encr_photo.h = fetch_int ();
|
|
|
|
M->encr_photo.size = fetch_int ();
|
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
l = prefetch_strlen ();
|
2013-11-02 13:01:22 -04:00
|
|
|
assert (l > 0);
|
2014-01-10 06:32:57 -05:00
|
|
|
M->encr_photo.key = talloc (32);
|
2013-11-04 12:34:27 -05:00
|
|
|
memset (M->encr_photo.key, 0, 32);
|
|
|
|
if (l <= 32) {
|
|
|
|
memcpy (M->encr_photo.key + (32 - l), fetch_str (l), l);
|
|
|
|
} else {
|
|
|
|
memcpy (M->encr_photo.key, fetch_str (l) + (l - 32), 32);
|
|
|
|
}
|
2014-01-10 06:32:57 -05:00
|
|
|
M->encr_photo.iv = talloc (32);
|
2013-11-04 12:34:27 -05:00
|
|
|
l = prefetch_strlen ();
|
|
|
|
assert (l > 0);
|
|
|
|
memset (M->encr_photo.iv, 0, 32);
|
|
|
|
if (l <= 32) {
|
|
|
|
memcpy (M->encr_photo.iv + (32 - l), fetch_str (l), l);
|
|
|
|
} else {
|
|
|
|
memcpy (M->encr_photo.iv, fetch_str (l) + (l - 32), 32);
|
|
|
|
}
|
2013-11-02 13:01:22 -04:00
|
|
|
break;
|
|
|
|
case CODE_decrypted_message_media_video:
|
|
|
|
M->type = x;
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // thumb
|
|
|
|
fetch_int (); // thumb_w
|
|
|
|
fetch_int (); // thumb_h
|
2013-11-15 05:37:14 -05:00
|
|
|
M->encr_video.duration = fetch_int ();
|
2013-11-02 13:01:22 -04:00
|
|
|
M->encr_video.w = fetch_int ();
|
|
|
|
M->encr_video.h = fetch_int ();
|
|
|
|
M->encr_video.size = fetch_int ();
|
2013-11-15 05:37:14 -05:00
|
|
|
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
assert (l > 0);
|
2014-01-10 06:32:57 -05:00
|
|
|
M->encr_video.key = talloc0 (32);
|
2013-11-15 05:37:14 -05:00
|
|
|
if (l <= 32) {
|
|
|
|
memcpy (M->encr_video.key + (32 - l), fetch_str (l), l);
|
|
|
|
} else {
|
|
|
|
memcpy (M->encr_video.key, fetch_str (l) + (l - 32), 32);
|
|
|
|
}
|
2014-01-10 06:32:57 -05:00
|
|
|
M->encr_video.iv = talloc (32);
|
2013-11-15 05:37:14 -05:00
|
|
|
l = prefetch_strlen ();
|
|
|
|
assert (l > 0);
|
|
|
|
memset (M->encr_video.iv, 0, 32);
|
|
|
|
if (l <= 32) {
|
|
|
|
memcpy (M->encr_video.iv + (32 - l), fetch_str (l), l);
|
|
|
|
} else {
|
|
|
|
memcpy (M->encr_video.iv, fetch_str (l) + (l - 32), 32);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_decrypted_message_media_audio:
|
|
|
|
M->type = x;
|
|
|
|
M->encr_audio.duration = fetch_int ();
|
|
|
|
M->encr_audio.size = fetch_int ();
|
|
|
|
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
assert (l > 0);
|
2014-01-10 06:32:57 -05:00
|
|
|
M->encr_video.key = talloc0 (32);
|
2013-11-15 05:37:14 -05:00
|
|
|
if (l <= 32) {
|
|
|
|
memcpy (M->encr_video.key + (32 - l), fetch_str (l), l);
|
|
|
|
} else {
|
|
|
|
memcpy (M->encr_video.key, fetch_str (l) + (l - 32), 32);
|
|
|
|
}
|
2014-01-10 06:32:57 -05:00
|
|
|
M->encr_video.iv = talloc0 (32);
|
2013-11-15 05:37:14 -05:00
|
|
|
l = prefetch_strlen ();
|
|
|
|
assert (l > 0);
|
|
|
|
if (l <= 32) {
|
|
|
|
memcpy (M->encr_video.iv + (32 - l), fetch_str (l), l);
|
|
|
|
} else {
|
|
|
|
memcpy (M->encr_video.iv, fetch_str (l) + (l - 32), 32);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODE_decrypted_message_media_document:
|
|
|
|
M->type = x;
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // thumb
|
|
|
|
fetch_int (); // thumb_w
|
|
|
|
fetch_int (); // thumb_h
|
|
|
|
M->encr_document.file_name = fetch_str_dup ();
|
|
|
|
M->encr_document.mime_type = fetch_str_dup ();
|
|
|
|
M->encr_video.size = fetch_int ();
|
2013-11-02 13:01:22 -04:00
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
l = prefetch_strlen ();
|
2013-11-02 13:01:22 -04:00
|
|
|
assert (l > 0);
|
2014-01-10 06:32:57 -05:00
|
|
|
M->encr_video.key = talloc0 (32);
|
2013-11-04 12:34:27 -05:00
|
|
|
if (l <= 32) {
|
|
|
|
memcpy (M->encr_video.key + (32 - l), fetch_str (l), l);
|
|
|
|
} else {
|
|
|
|
memcpy (M->encr_video.key, fetch_str (l) + (l - 32), 32);
|
|
|
|
}
|
2014-01-10 06:32:57 -05:00
|
|
|
M->encr_video.iv = talloc0 (32);
|
2013-11-04 12:34:27 -05:00
|
|
|
l = prefetch_strlen ();
|
|
|
|
assert (l > 0);
|
|
|
|
if (l <= 32) {
|
|
|
|
memcpy (M->encr_video.iv + (32 - l), fetch_str (l), l);
|
|
|
|
} else {
|
|
|
|
memcpy (M->encr_video.iv, fetch_str (l) + (l - 32), 32);
|
|
|
|
}
|
2013-11-02 13:01:22 -04:00
|
|
|
break;
|
|
|
|
/* case CODE_decrypted_message_media_file:
|
|
|
|
M->type = x;
|
|
|
|
M->encr_file.filename = fetch_str_dup ();
|
|
|
|
l = prefetch_strlen ();
|
|
|
|
fetch_str (l); // thumb
|
|
|
|
l = fetch_int ();
|
|
|
|
assert (l > 0);
|
2014-01-10 06:32:57 -05:00
|
|
|
M->encr_file.key = talloc (l);
|
2013-11-02 13:01:22 -04:00
|
|
|
memcpy (M->encr_file.key, fetch_str (l), l);
|
|
|
|
|
|
|
|
l = fetch_int ();
|
|
|
|
assert (l > 0);
|
2014-01-10 06:32:57 -05:00
|
|
|
M->encr_file.iv = talloc (l);
|
2013-11-02 13:01:22 -04:00
|
|
|
memcpy (M->encr_file.iv, fetch_str (l), l);
|
|
|
|
break;
|
|
|
|
*/
|
|
|
|
case CODE_decrypted_message_media_geo_point:
|
|
|
|
M->geo.longitude = fetch_double ();
|
|
|
|
M->geo.latitude = fetch_double ();
|
|
|
|
M->type = CODE_message_media_geo;
|
|
|
|
break;
|
|
|
|
case CODE_decrypted_message_media_contact:
|
|
|
|
M->type = CODE_message_media_contact;
|
|
|
|
M->phone = fetch_str_dup ();
|
|
|
|
M->first_name = fetch_str_dup ();
|
|
|
|
M->last_name = fetch_str_dup ();
|
|
|
|
M->user_id = fetch_int ();
|
|
|
|
break;
|
|
|
|
default:
|
2013-11-29 16:43:56 -05:00
|
|
|
logprintf ("type = 0x%08x\n", x);
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fetch_skip_message_action_encrypted (void) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
switch (x) {
|
|
|
|
case CODE_decrypted_message_action_set_message_t_t_l:
|
|
|
|
fetch_skip (1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logprintf ("x = 0x%08x\n", x);
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fetch_message_action_encrypted (struct message_action *M) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
switch (x) {
|
|
|
|
case CODE_decrypted_message_action_set_message_t_t_l:
|
|
|
|
M->type = x;
|
|
|
|
M->ttl = fetch_int ();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logprintf ("x = 0x%08x\n", x);
|
2013-11-02 13:01:22 -04:00
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_id_t fetch_peer_id (void) {
|
2013-10-18 12:00:47 -04:00
|
|
|
unsigned x =fetch_int ();
|
|
|
|
if (x == CODE_peer_user) {
|
2013-10-31 19:18:34 -04:00
|
|
|
return MK_USER (fetch_int ());
|
2013-10-18 12:00:47 -04:00
|
|
|
} else {
|
|
|
|
assert (CODE_peer_chat);
|
2013-10-31 19:18:34 -04:00
|
|
|
return MK_CHAT (fetch_int ());
|
2013-10-18 12:00:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 13:26:25 -04:00
|
|
|
void fetch_message (struct message *M) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
assert (x == CODE_message_empty || x == CODE_message || x == CODE_message_forwarded || x == CODE_message_service);
|
2013-11-29 16:43:56 -05:00
|
|
|
int id = fetch_int ();
|
|
|
|
assert (M->id == id);
|
2013-10-14 13:26:25 -04:00
|
|
|
if (x == CODE_message_empty) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-29 16:43:56 -05:00
|
|
|
int fwd_from_id = 0;
|
|
|
|
int fwd_date = 0;
|
|
|
|
|
2013-10-14 13:26:25 -04:00
|
|
|
if (x == CODE_message_forwarded) {
|
2013-11-29 16:43:56 -05:00
|
|
|
fwd_from_id = fetch_int ();
|
|
|
|
fwd_date = fetch_int ();
|
2013-10-14 13:26:25 -04:00
|
|
|
}
|
2013-11-29 16:43:56 -05:00
|
|
|
int from_id = fetch_int ();
|
|
|
|
peer_id_t to_id = fetch_peer_id ();
|
|
|
|
|
|
|
|
fetch_bool (); // out.
|
|
|
|
|
|
|
|
int unread = fetch_bool ();
|
|
|
|
int date = fetch_int ();
|
|
|
|
|
|
|
|
int new = !(M->flags & FLAG_CREATED);
|
|
|
|
|
2013-10-14 13:26:25 -04:00
|
|
|
if (x == CODE_message_service) {
|
2013-11-29 16:43:56 -05:00
|
|
|
int *start = in_ptr;
|
|
|
|
fetch_skip_message_action ();
|
|
|
|
if (new) {
|
|
|
|
if (fwd_from_id) {
|
|
|
|
bl_do_create_message_service_fwd (id, from_id, get_peer_type (to_id), get_peer_id (to_id), date, fwd_from_id, fwd_date, start, (in_ptr - start));
|
|
|
|
} else {
|
|
|
|
bl_do_create_message_service (id, from_id, get_peer_type (to_id), get_peer_id (to_id), date, start, (in_ptr - start));
|
|
|
|
}
|
|
|
|
}
|
2013-10-14 13:26:25 -04:00
|
|
|
} else {
|
2013-11-29 16:43:56 -05:00
|
|
|
int l = prefetch_strlen ();
|
|
|
|
char *s = fetch_str (l);
|
|
|
|
int *start = in_ptr;
|
|
|
|
fetch_skip_message_media ();
|
|
|
|
if (new) {
|
|
|
|
if (fwd_from_id) {
|
|
|
|
bl_do_create_message_media_fwd (id, from_id, get_peer_type (to_id), get_peer_id (to_id), date, fwd_from_id, fwd_date, l, s, start, in_ptr - start);
|
|
|
|
} else {
|
|
|
|
bl_do_create_message_media (id, from_id, get_peer_type (to_id), get_peer_id (to_id), date, l, s, start, in_ptr - start);
|
|
|
|
}
|
|
|
|
}
|
2013-10-13 08:30:53 -04:00
|
|
|
}
|
2013-11-29 16:43:56 -05:00
|
|
|
bl_do_set_unread (M, unread);
|
2013-10-13 08:30:53 -04:00
|
|
|
}
|
2013-10-13 06:18:08 -04:00
|
|
|
|
2013-11-01 15:57:57 -04:00
|
|
|
void fetch_geo_message (struct message *M) {
|
|
|
|
memset (M, 0, sizeof (*M));
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
assert (x == CODE_geo_chat_message_empty || x == CODE_geo_chat_message || x == CODE_geo_chat_message_service);
|
|
|
|
M->to_id = MK_GEO_CHAT (fetch_int ());
|
|
|
|
M->id = fetch_int ();
|
|
|
|
if (x == CODE_geo_chat_message_empty) {
|
|
|
|
M->flags |= 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
M->from_id = MK_USER (fetch_int ());
|
|
|
|
M->date = fetch_int ();
|
|
|
|
if (x == CODE_geo_chat_message_service) {
|
|
|
|
M->service = 1;
|
|
|
|
fetch_message_action (&M->action);
|
|
|
|
} else {
|
|
|
|
M->message = fetch_str_dup ();
|
2014-01-21 09:36:46 -05:00
|
|
|
M->message_len = strlen (M->message);
|
2013-11-01 15:57:57 -04:00
|
|
|
fetch_message_media (&M->media);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 11:13:36 -05:00
|
|
|
static int *decr_ptr;
|
|
|
|
static int *decr_end;
|
2013-11-02 06:14:30 -04:00
|
|
|
|
|
|
|
int decrypt_encrypted_message (struct secret_chat *E) {
|
|
|
|
int *msg_key = decr_ptr;
|
|
|
|
decr_ptr += 4;
|
|
|
|
assert (decr_ptr < decr_end);
|
|
|
|
static unsigned char sha1a_buffer[20];
|
|
|
|
static unsigned char sha1b_buffer[20];
|
|
|
|
static unsigned char sha1c_buffer[20];
|
|
|
|
static unsigned char sha1d_buffer[20];
|
|
|
|
|
|
|
|
static unsigned char buf[64];
|
|
|
|
memcpy (buf, msg_key, 16);
|
|
|
|
memcpy (buf + 16, E->key, 32);
|
|
|
|
sha1 (buf, 48, sha1a_buffer);
|
|
|
|
|
|
|
|
memcpy (buf, E->key + 8, 16);
|
|
|
|
memcpy (buf + 16, msg_key, 16);
|
|
|
|
memcpy (buf + 32, E->key + 12, 16);
|
|
|
|
sha1 (buf, 48, sha1b_buffer);
|
|
|
|
|
|
|
|
memcpy (buf, E->key + 16, 32);
|
|
|
|
memcpy (buf + 32, msg_key, 16);
|
|
|
|
sha1 (buf, 48, sha1c_buffer);
|
|
|
|
|
|
|
|
memcpy (buf, msg_key, 16);
|
|
|
|
memcpy (buf + 16, E->key + 24, 32);
|
|
|
|
sha1 (buf, 48, sha1d_buffer);
|
|
|
|
|
|
|
|
static unsigned char key[32];
|
2013-11-04 12:34:27 -05:00
|
|
|
memcpy (key, sha1a_buffer + 0, 8);
|
|
|
|
memcpy (key + 8, sha1b_buffer + 8, 12);
|
|
|
|
memcpy (key + 20, sha1c_buffer + 4, 12);
|
|
|
|
|
|
|
|
static unsigned char iv[32];
|
|
|
|
memcpy (iv, sha1a_buffer + 8, 12);
|
|
|
|
memcpy (iv + 12, sha1b_buffer + 0, 8);
|
|
|
|
memcpy (iv + 20, sha1c_buffer + 16, 4);
|
|
|
|
memcpy (iv + 24, sha1d_buffer + 0, 8);
|
2013-11-02 06:14:30 -04:00
|
|
|
|
|
|
|
AES_KEY aes_key;
|
|
|
|
AES_set_decrypt_key (key, 256, &aes_key);
|
|
|
|
AES_ige_encrypt ((void *)decr_ptr, (void *)decr_ptr, 4 * (decr_end - decr_ptr), &aes_key, iv, 0);
|
2014-01-21 11:02:34 -05:00
|
|
|
memset (&aes_key, 0, sizeof (aes_key));
|
2013-11-02 06:14:30 -04:00
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
int x = *(decr_ptr);
|
2013-11-05 17:24:26 -05:00
|
|
|
if (x < 0 || (x & 3)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-04 12:34:27 -05:00
|
|
|
assert (x >= 0 && !(x & 3));
|
|
|
|
sha1 ((void *)decr_ptr, 4 + x, sha1a_buffer);
|
2013-11-02 06:14:30 -04:00
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
if (memcmp (sha1a_buffer + 4, msg_key, 16)) {
|
2013-11-02 06:14:30 -04:00
|
|
|
logprintf ("Sha1 mismatch\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fetch_encrypted_message (struct message *M) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
assert (x == CODE_encrypted_message || x == CODE_encrypted_message_service);
|
2013-11-04 12:34:27 -05:00
|
|
|
unsigned sx = x;
|
2013-11-29 16:43:56 -05:00
|
|
|
int new = !(M->flags & FLAG_CREATED);
|
|
|
|
long long id = fetch_long ();
|
|
|
|
int to_id = fetch_int ();
|
|
|
|
peer_id_t chat = MK_ENCR_CHAT (to_id);
|
|
|
|
int date = fetch_int ();
|
|
|
|
|
2013-11-02 06:14:30 -04:00
|
|
|
peer_t *P = user_chat_get (chat);
|
|
|
|
if (!P) {
|
|
|
|
logprintf ("Encrypted message to unknown chat. Dropping\n");
|
2013-11-21 14:35:49 -05:00
|
|
|
M->flags |= FLAG_MESSAGE_EMPTY;
|
2013-11-02 06:14:30 -04:00
|
|
|
}
|
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
|
2013-11-02 06:14:30 -04:00
|
|
|
int len = prefetch_strlen ();
|
2013-11-04 12:34:27 -05:00
|
|
|
assert ((len & 15) == 8);
|
2013-11-02 06:14:30 -04:00
|
|
|
decr_ptr = (void *)fetch_str (len);
|
2013-11-04 12:34:27 -05:00
|
|
|
decr_end = decr_ptr + (len / 4);
|
|
|
|
int ok = 0;
|
|
|
|
if (P) {
|
|
|
|
if (*(long long *)decr_ptr != P->encr_chat.key_fingerprint) {
|
|
|
|
logprintf ("Encrypted message with bad fingerprint to chat %s\n", P->print_name);
|
|
|
|
P = 0;
|
|
|
|
}
|
|
|
|
decr_ptr += 2;
|
|
|
|
}
|
2013-11-29 16:43:56 -05:00
|
|
|
int l = 0;
|
|
|
|
char *s = 0;
|
|
|
|
int *start = 0;
|
|
|
|
int *end = 0;
|
|
|
|
x = 0;
|
|
|
|
if (P && decrypt_encrypted_message (&P->encr_chat) >= 0 && new) {
|
2013-11-04 12:34:27 -05:00
|
|
|
ok = 1;
|
|
|
|
int *save_in_ptr = in_ptr;
|
|
|
|
int *save_in_end = in_end;
|
2013-11-02 06:14:30 -04:00
|
|
|
in_ptr = decr_ptr;
|
2013-11-29 16:43:56 -05:00
|
|
|
int ll = fetch_int ();
|
|
|
|
in_end = in_ptr + ll;
|
|
|
|
x = fetch_int ();
|
2013-11-02 06:14:30 -04:00
|
|
|
if (x == CODE_decrypted_message_layer) {
|
|
|
|
int layer = fetch_int ();
|
|
|
|
assert (layer >= 0);
|
|
|
|
x = fetch_int ();
|
|
|
|
}
|
2013-11-04 12:34:27 -05:00
|
|
|
assert (x == CODE_decrypted_message || x == CODE_decrypted_message_service);
|
2013-11-29 16:43:56 -05:00
|
|
|
//assert (id == fetch_long ());
|
|
|
|
fetch_long ();
|
|
|
|
ll = prefetch_strlen ();
|
|
|
|
fetch_str (ll); // random_bytes
|
2013-11-02 13:01:22 -04:00
|
|
|
if (x == CODE_decrypted_message) {
|
2013-11-29 16:43:56 -05:00
|
|
|
l = prefetch_strlen ();
|
|
|
|
s = fetch_str (l);
|
|
|
|
start = in_ptr;
|
|
|
|
fetch_skip_message_media_encrypted ();
|
|
|
|
end = in_ptr;
|
2013-11-02 13:01:22 -04:00
|
|
|
} else {
|
2013-11-29 16:43:56 -05:00
|
|
|
start = in_ptr;
|
|
|
|
fetch_skip_message_action_encrypted ();
|
|
|
|
end = in_ptr;
|
2013-11-02 13:01:22 -04:00
|
|
|
}
|
2013-11-04 12:34:27 -05:00
|
|
|
in_ptr = save_in_ptr;
|
|
|
|
in_end = save_in_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sx == CODE_encrypted_message) {
|
|
|
|
if (ok) {
|
2013-11-29 16:43:56 -05:00
|
|
|
int *start_file = in_ptr;
|
|
|
|
fetch_skip_encrypted_message_file ();
|
|
|
|
if (x == CODE_decrypted_message) {
|
|
|
|
bl_do_create_message_media_encr (id, P->encr_chat.user_id, PEER_ENCR_CHAT, to_id, date, l, s, start, end - start, start_file, in_ptr - start_file);
|
|
|
|
}
|
2013-11-04 12:34:27 -05:00
|
|
|
} else {
|
|
|
|
x = fetch_int ();
|
|
|
|
if (x == CODE_encrypted_file) {
|
|
|
|
fetch_skip (7);
|
|
|
|
} else {
|
|
|
|
assert (x == CODE_encrypted_file_empty);
|
|
|
|
}
|
|
|
|
M->media.type = CODE_message_media_empty;
|
|
|
|
}
|
2013-11-29 16:43:56 -05:00
|
|
|
} else {
|
|
|
|
if (ok && x == CODE_decrypted_message_service) {
|
|
|
|
bl_do_create_message_service_encr (id, P->encr_chat.user_id, PEER_ENCR_CHAT, to_id, date, start, end - start);
|
|
|
|
}
|
2013-11-02 13:01:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fetch_encrypted_message_file (struct message_media *M) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
assert (x == CODE_encrypted_file || x == CODE_encrypted_file_empty);
|
|
|
|
if (x == CODE_encrypted_file_empty) {
|
|
|
|
assert (M->type != CODE_decrypted_message_media_photo && M->type != CODE_decrypted_message_media_video);
|
|
|
|
} else {
|
|
|
|
assert (M->type == CODE_decrypted_message_media_photo || M->type == CODE_decrypted_message_media_video);
|
|
|
|
M->encr_photo.id = fetch_long ();
|
|
|
|
M->encr_photo.access_hash = fetch_long ();
|
2013-11-04 18:46:12 -05:00
|
|
|
fetch_int ();
|
2013-11-04 12:34:27 -05:00
|
|
|
//assert (M->encr_photo.size == fetch_int ());
|
2013-11-04 18:46:12 -05:00
|
|
|
//M->encr_photo.size = fetch_int (); // Why it is not the same?
|
2013-11-02 13:01:22 -04:00
|
|
|
M->encr_photo.dc_id = fetch_int ();
|
|
|
|
M->encr_photo.key_fingerprint = fetch_int ();
|
|
|
|
|
2013-11-02 06:14:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-29 16:43:56 -05:00
|
|
|
void fetch_skip_encrypted_message_file (void) {
|
|
|
|
unsigned x = fetch_int ();
|
|
|
|
assert (x == CODE_encrypted_file || x == CODE_encrypted_file_empty);
|
|
|
|
if (x == CODE_encrypted_file_empty) {
|
|
|
|
} else {
|
|
|
|
fetch_skip (7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-02 06:14:30 -04:00
|
|
|
static int id_cmp (struct message *M1, struct message *M2) {
|
|
|
|
if (M1->id < M2->id) { return -1; }
|
|
|
|
else if (M1->id > M2->id) { return 1; }
|
|
|
|
else { return 0; }
|
|
|
|
}
|
|
|
|
|
2013-10-13 06:18:08 -04:00
|
|
|
struct user *fetch_alloc_user (void) {
|
2013-10-24 13:04:44 -04:00
|
|
|
int data[2];
|
|
|
|
prefetch_data (data, 8);
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_t *U = user_chat_get (MK_USER (data[1]));
|
2013-11-21 14:35:49 -05:00
|
|
|
if (!U) {
|
2013-10-24 13:04:44 -04:00
|
|
|
users_allocated ++;
|
2014-01-10 06:32:57 -05:00
|
|
|
U = talloc0 (sizeof (*U));
|
2013-11-21 14:35:49 -05:00
|
|
|
U->id = MK_USER (data[1]);
|
2013-10-14 13:26:25 -04:00
|
|
|
peer_tree = tree_insert_peer (peer_tree, U, lrand48 ());
|
2013-12-02 12:19:08 -05:00
|
|
|
assert (peer_num < MAX_PEER_NUM);
|
2013-11-04 12:34:27 -05:00
|
|
|
Peers[peer_num ++] = U;
|
2013-10-13 06:18:08 -04:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
fetch_user (&U->user);
|
|
|
|
return &U->user;
|
2013-10-13 06:18:08 -04:00
|
|
|
}
|
|
|
|
|
2013-11-02 13:01:22 -04:00
|
|
|
struct secret_chat *fetch_alloc_encrypted_chat (void) {
|
|
|
|
int data[2];
|
|
|
|
prefetch_data (data, 8);
|
|
|
|
peer_t *U = user_chat_get (MK_ENCR_CHAT (data[1]));
|
2013-11-21 14:35:49 -05:00
|
|
|
if (!U) {
|
2014-01-10 06:32:57 -05:00
|
|
|
U = talloc0 (sizeof (*U));
|
2013-11-21 14:35:49 -05:00
|
|
|
U->id = MK_ENCR_CHAT (data[1]);
|
|
|
|
encr_chats_allocated ++;
|
2013-11-02 13:01:22 -04:00
|
|
|
peer_tree = tree_insert_peer (peer_tree, U, lrand48 ());
|
2013-12-02 12:19:08 -05:00
|
|
|
assert (peer_num < MAX_PEER_NUM);
|
2013-11-04 12:34:27 -05:00
|
|
|
Peers[peer_num ++] = U;
|
2013-11-02 13:01:22 -04:00
|
|
|
}
|
2013-11-21 14:35:49 -05:00
|
|
|
fetch_encrypted_chat (&U->encr_chat);
|
|
|
|
return &U->encr_chat;
|
2013-11-02 13:01:22 -04:00
|
|
|
}
|
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
void insert_encrypted_chat (peer_t *P) {
|
|
|
|
encr_chats_allocated ++;
|
|
|
|
peer_tree = tree_insert_peer (peer_tree, P, lrand48 ());
|
2013-12-02 12:19:08 -05:00
|
|
|
assert (peer_num < MAX_PEER_NUM);
|
2013-11-04 12:34:27 -05:00
|
|
|
Peers[peer_num ++] = P;
|
|
|
|
}
|
|
|
|
|
2013-11-21 14:35:49 -05:00
|
|
|
void insert_user (peer_t *P) {
|
|
|
|
users_allocated ++;
|
|
|
|
peer_tree = tree_insert_peer (peer_tree, P, lrand48 ());
|
2013-12-02 12:19:08 -05:00
|
|
|
assert (peer_num < MAX_PEER_NUM);
|
2013-11-21 14:35:49 -05:00
|
|
|
Peers[peer_num ++] = P;
|
|
|
|
}
|
|
|
|
|
2013-11-22 18:26:35 -05:00
|
|
|
void insert_chat (peer_t *P) {
|
|
|
|
chats_allocated ++;
|
|
|
|
peer_tree = tree_insert_peer (peer_tree, P, lrand48 ());
|
2013-12-02 12:19:08 -05:00
|
|
|
assert (peer_num < MAX_PEER_NUM);
|
2013-11-22 18:26:35 -05:00
|
|
|
Peers[peer_num ++] = P;
|
|
|
|
}
|
|
|
|
|
2013-10-25 13:29:02 -04:00
|
|
|
struct user *fetch_alloc_user_full (void) {
|
|
|
|
int data[3];
|
|
|
|
prefetch_data (data, 12);
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_t *U = user_chat_get (MK_USER (data[2]));
|
2013-10-25 13:29:02 -04:00
|
|
|
if (U) {
|
|
|
|
fetch_user_full (&U->user);
|
|
|
|
return &U->user;
|
|
|
|
} else {
|
|
|
|
users_allocated ++;
|
2014-01-10 06:32:57 -05:00
|
|
|
U = talloc0 (sizeof (*U));
|
2013-10-31 19:18:34 -04:00
|
|
|
U->id = MK_USER (data[2]);
|
2013-10-25 13:29:02 -04:00
|
|
|
peer_tree = tree_insert_peer (peer_tree, U, lrand48 ());
|
|
|
|
fetch_user_full (&U->user);
|
2013-12-02 12:19:08 -05:00
|
|
|
assert (peer_num < MAX_PEER_NUM);
|
2013-11-04 12:34:27 -05:00
|
|
|
Peers[peer_num ++] = U;
|
2013-10-25 13:29:02 -04:00
|
|
|
return &U->user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-13 06:18:08 -04:00
|
|
|
void free_user (struct user *U) {
|
2014-01-10 09:37:56 -05:00
|
|
|
if (U->first_name) { tfree_str (U->first_name); }
|
|
|
|
if (U->last_name) { tfree_str (U->last_name); }
|
|
|
|
if (U->print_name) { tfree_str (U->print_name); }
|
|
|
|
if (U->phone) { tfree_str (U->phone); }
|
2013-10-13 06:18:08 -04:00
|
|
|
}
|
|
|
|
|
2013-10-14 13:26:25 -04:00
|
|
|
void free_photo_size (struct photo_size *S) {
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree_str (S->type);
|
2013-10-18 12:00:47 -04:00
|
|
|
if (S->data) {
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree (S->data, S->size);
|
2013-10-18 12:00:47 -04:00
|
|
|
}
|
2013-10-14 13:26:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void free_photo (struct photo *P) {
|
2013-10-18 12:00:47 -04:00
|
|
|
if (!P->access_hash) { return; }
|
2014-01-10 09:37:56 -05:00
|
|
|
if (P->caption) { tfree_str (P->caption); }
|
2013-10-18 12:00:47 -04:00
|
|
|
if (P->sizes) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < P->sizes_num; i++) {
|
|
|
|
free_photo_size (&P->sizes[i]);
|
|
|
|
}
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree (P->sizes, sizeof (struct photo_size) * P->sizes_num);
|
2013-10-14 13:26:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_video (struct video *V) {
|
2013-10-18 12:00:47 -04:00
|
|
|
if (!V->access_hash) { return; }
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree_str (V->caption);
|
2013-10-18 12:00:47 -04:00
|
|
|
free_photo_size (&V->thumb);
|
2013-10-14 13:26:25 -04:00
|
|
|
}
|
|
|
|
|
2013-11-15 05:37:14 -05:00
|
|
|
void free_document (struct document *D) {
|
|
|
|
if (!D->access_hash) { return; }
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree_str (D->caption);
|
|
|
|
tfree_str (D->mime_type);
|
2013-11-15 05:37:14 -05:00
|
|
|
free_photo_size (&D->thumb);
|
|
|
|
}
|
|
|
|
|
2013-10-14 13:26:25 -04:00
|
|
|
void free_message_media (struct message_media *M) {
|
|
|
|
switch (M->type) {
|
|
|
|
case CODE_message_media_empty:
|
2013-10-19 07:46:41 -04:00
|
|
|
case CODE_message_media_geo:
|
2013-11-15 05:37:14 -05:00
|
|
|
case CODE_message_media_audio:
|
2013-10-14 13:26:25 -04:00
|
|
|
return;
|
|
|
|
case CODE_message_media_photo:
|
|
|
|
free_photo (&M->photo);
|
|
|
|
return;
|
|
|
|
case CODE_message_media_video:
|
|
|
|
free_video (&M->video);
|
|
|
|
return;
|
|
|
|
case CODE_message_media_contact:
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree_str (M->phone);
|
|
|
|
tfree_str (M->first_name);
|
|
|
|
tfree_str (M->last_name);
|
2013-10-14 13:26:25 -04:00
|
|
|
return;
|
2013-11-15 05:37:14 -05:00
|
|
|
case CODE_message_media_document:
|
|
|
|
free_document (&M->document);
|
|
|
|
return;
|
2013-10-14 13:26:25 -04:00
|
|
|
case CODE_message_media_unsupported:
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree (M->data, M->data_size);
|
2013-10-14 13:26:25 -04:00
|
|
|
return;
|
2013-11-02 13:01:22 -04:00
|
|
|
case CODE_decrypted_message_media_photo:
|
2013-11-15 05:37:14 -05:00
|
|
|
case CODE_decrypted_message_media_video:
|
|
|
|
case CODE_decrypted_message_media_audio:
|
|
|
|
case CODE_decrypted_message_media_document:
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree_secure (M->encr_photo.key, 32);
|
|
|
|
tfree_secure (M->encr_photo.iv, 32);
|
2013-11-02 13:01:22 -04:00
|
|
|
return;
|
2013-11-04 12:34:27 -05:00
|
|
|
case 0:
|
|
|
|
break;
|
2013-10-14 13:26:25 -04:00
|
|
|
default:
|
2013-10-19 07:46:41 -04:00
|
|
|
logprintf ("%08x\n", M->type);
|
2013-10-14 13:26:25 -04:00
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_message_action (struct message_action *M) {
|
|
|
|
switch (M->type) {
|
|
|
|
case CODE_message_action_empty:
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_create:
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree_str (M->title);
|
|
|
|
tfree (M->users, M->user_num * 4);
|
2013-10-14 13:26:25 -04:00
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_edit_title:
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree_str (M->new_title);
|
2013-10-14 13:26:25 -04:00
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_edit_photo:
|
|
|
|
free_photo (&M->photo);
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_delete_photo:
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_add_user:
|
|
|
|
break;
|
|
|
|
case CODE_message_action_chat_delete_user:
|
|
|
|
break;
|
2013-11-04 12:34:27 -05:00
|
|
|
case 0:
|
|
|
|
break;
|
2013-10-14 13:26:25 -04:00
|
|
|
default:
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_message (struct message *M) {
|
|
|
|
if (!M->service) {
|
2014-01-21 09:36:46 -05:00
|
|
|
if (M->message) { tfree (M->message, M->message_len + 1); }
|
2013-10-14 13:26:25 -04:00
|
|
|
free_message_media (&M->media);
|
|
|
|
} else {
|
|
|
|
free_message_action (&M->action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void message_del_use (struct message *M) {
|
|
|
|
M->next_use->prev_use = M->prev_use;
|
|
|
|
M->prev_use->next_use = M->next_use;
|
|
|
|
}
|
|
|
|
|
|
|
|
void message_add_use (struct message *M) {
|
|
|
|
M->next_use = message_list.next_use;
|
|
|
|
M->prev_use = &message_list;
|
|
|
|
M->next_use->prev_use = M;
|
|
|
|
M->prev_use->next_use = M;
|
|
|
|
}
|
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
void message_add_peer (struct message *M) {
|
|
|
|
peer_id_t id;
|
|
|
|
if (!cmp_peer_id (M->to_id, MK_USER (our_id))) {
|
|
|
|
id = M->from_id;
|
|
|
|
} else {
|
|
|
|
id = M->to_id;
|
|
|
|
}
|
|
|
|
peer_t *P = user_chat_get (id);
|
|
|
|
if (!P) {
|
2014-01-10 06:32:57 -05:00
|
|
|
P = talloc0 (sizeof (*P));
|
2013-11-04 12:34:27 -05:00
|
|
|
P->id = id;
|
|
|
|
switch (get_peer_type (id)) {
|
|
|
|
case PEER_USER:
|
|
|
|
users_allocated ++;
|
|
|
|
break;
|
|
|
|
case PEER_CHAT:
|
|
|
|
chats_allocated ++;
|
|
|
|
break;
|
|
|
|
case PEER_GEO_CHAT:
|
|
|
|
geo_chats_allocated ++;
|
|
|
|
break;
|
|
|
|
case PEER_ENCR_CHAT:
|
|
|
|
encr_chats_allocated ++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
peer_tree = tree_insert_peer (peer_tree, P, lrand48 ());
|
2013-12-02 12:19:08 -05:00
|
|
|
assert (peer_num < MAX_PEER_NUM);
|
2013-11-04 12:34:27 -05:00
|
|
|
Peers[peer_num ++] = P;
|
|
|
|
}
|
2013-11-29 16:43:56 -05:00
|
|
|
if (!P->last) {
|
|
|
|
P->last = M;
|
|
|
|
M->prev = M->next = 0;
|
|
|
|
} else {
|
|
|
|
if (get_peer_type (P->id) != PEER_ENCR_CHAT) {
|
|
|
|
struct message *N = P->last;
|
|
|
|
struct message *NP = 0;
|
|
|
|
while (N && N->id > M->id) {
|
|
|
|
NP = N;
|
|
|
|
N = N->next;
|
|
|
|
}
|
|
|
|
if (N) { assert (N->id < M->id); }
|
|
|
|
M->next = N;
|
|
|
|
M->prev = NP;
|
|
|
|
if (N) { N->prev = M; }
|
|
|
|
if (NP) { NP->next = M; }
|
|
|
|
else { P->last = M; }
|
|
|
|
} else {
|
|
|
|
struct message *N = P->last;
|
|
|
|
struct message *NP = 0;
|
|
|
|
M->next = N;
|
|
|
|
M->prev = NP;
|
|
|
|
if (N) { N->prev = M; }
|
|
|
|
if (NP) { NP->next = M; }
|
|
|
|
else { P->last = M; }
|
|
|
|
}
|
|
|
|
}
|
2013-11-04 12:34:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void message_del_peer (struct message *M) {
|
|
|
|
peer_id_t id;
|
|
|
|
if (!cmp_peer_id (M->to_id, MK_USER (our_id))) {
|
|
|
|
id = M->from_id;
|
|
|
|
} else {
|
|
|
|
id = M->to_id;
|
|
|
|
}
|
|
|
|
peer_t *P = user_chat_get (id);
|
|
|
|
if (M->prev) {
|
|
|
|
M->prev->next = M->next;
|
|
|
|
}
|
|
|
|
if (M->next) {
|
|
|
|
M->next->prev = M->prev;
|
|
|
|
}
|
|
|
|
if (P && P->last == M) {
|
|
|
|
P->last = M->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 13:26:25 -04:00
|
|
|
struct message *fetch_alloc_message (void) {
|
2013-11-29 16:43:56 -05:00
|
|
|
int data[2];
|
|
|
|
prefetch_data (data, 8);
|
|
|
|
struct message *M = message_get (data[1]);
|
|
|
|
|
|
|
|
if (!M) {
|
2014-01-10 06:32:57 -05:00
|
|
|
M = talloc0 (sizeof (*M));
|
2013-11-29 16:43:56 -05:00
|
|
|
M->id = data[1];
|
|
|
|
message_insert_tree (M);
|
|
|
|
messages_allocated ++;
|
2013-11-01 15:57:57 -04:00
|
|
|
}
|
2013-11-29 16:43:56 -05:00
|
|
|
fetch_message (M);
|
|
|
|
return M;
|
2013-11-01 15:57:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct message *fetch_alloc_geo_message (void) {
|
2014-01-10 06:32:57 -05:00
|
|
|
struct message *M = talloc (sizeof (*M));
|
2013-11-01 15:57:57 -04:00
|
|
|
fetch_geo_message (M);
|
|
|
|
struct message *M1 = tree_lookup_message (message_tree, M);
|
|
|
|
messages_allocated ++;
|
|
|
|
if (M1) {
|
|
|
|
message_del_use (M1);
|
2013-11-04 12:34:27 -05:00
|
|
|
message_del_peer (M1);
|
2013-11-01 15:57:57 -04:00
|
|
|
free_message (M1);
|
|
|
|
memcpy (M1, M, sizeof (*M));
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree (M, sizeof (*M));
|
2013-11-01 15:57:57 -04:00
|
|
|
message_add_use (M1);
|
2013-11-04 12:34:27 -05:00
|
|
|
message_add_peer (M1);
|
2013-11-01 15:57:57 -04:00
|
|
|
messages_allocated --;
|
|
|
|
return M1;
|
|
|
|
} else {
|
|
|
|
message_add_use (M);
|
2013-11-04 12:34:27 -05:00
|
|
|
message_add_peer (M);
|
2013-10-14 13:26:25 -04:00
|
|
|
message_tree = tree_insert_message (message_tree, M, lrand48 ());
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-02 06:14:30 -04:00
|
|
|
struct message *fetch_alloc_encrypted_message (void) {
|
2013-11-29 16:43:56 -05:00
|
|
|
int data[3];
|
|
|
|
prefetch_data (data, 12);
|
|
|
|
struct message *M = message_get (*(long long *)(data + 1));
|
|
|
|
|
|
|
|
if (!M) {
|
2014-01-10 06:32:57 -05:00
|
|
|
M = talloc0 (sizeof (*M));
|
2013-11-29 16:43:56 -05:00
|
|
|
M->id = *(long long *)(data + 1);
|
|
|
|
message_insert_tree (M);
|
|
|
|
messages_allocated ++;
|
|
|
|
assert (message_get (M->id) == M);
|
|
|
|
logprintf ("id = %lld\n", M->id);
|
2013-11-02 06:14:30 -04:00
|
|
|
}
|
2013-11-29 16:43:56 -05:00
|
|
|
fetch_encrypted_message (M);
|
|
|
|
return M;
|
2013-11-02 06:14:30 -04:00
|
|
|
}
|
|
|
|
|
2013-10-16 15:19:39 -04:00
|
|
|
struct message *fetch_alloc_message_short (void) {
|
2013-11-29 16:43:56 -05:00
|
|
|
int data[1];
|
|
|
|
prefetch_data (data, 4);
|
|
|
|
struct message *M = message_get (data[0]);
|
|
|
|
|
|
|
|
if (!M) {
|
2014-01-10 06:32:57 -05:00
|
|
|
M = talloc0 (sizeof (*M));
|
2013-11-29 16:43:56 -05:00
|
|
|
M->id = data[0];
|
|
|
|
message_insert_tree (M);
|
|
|
|
messages_allocated ++;
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
2013-11-29 16:43:56 -05:00
|
|
|
fetch_message_short (M);
|
|
|
|
return M;
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct message *fetch_alloc_message_short_chat (void) {
|
2013-11-29 16:43:56 -05:00
|
|
|
int data[1];
|
|
|
|
prefetch_data (data, 4);
|
|
|
|
struct message *M = message_get (data[0]);
|
|
|
|
|
|
|
|
if (!M) {
|
2014-01-10 06:32:57 -05:00
|
|
|
M = talloc0 (sizeof (*M));
|
2013-11-29 16:43:56 -05:00
|
|
|
M->id = data[0];
|
|
|
|
message_insert_tree (M);
|
|
|
|
messages_allocated ++;
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
2013-11-29 16:43:56 -05:00
|
|
|
fetch_message_short_chat (M);
|
|
|
|
return M;
|
2013-10-16 15:19:39 -04:00
|
|
|
}
|
|
|
|
|
2013-10-14 13:26:25 -04:00
|
|
|
struct chat *fetch_alloc_chat (void) {
|
2013-10-24 13:04:44 -04:00
|
|
|
int data[2];
|
|
|
|
prefetch_data (data, 8);
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_t *U = user_chat_get (MK_CHAT (data[1]));
|
2013-11-22 18:26:35 -05:00
|
|
|
if (!U) {
|
2013-10-24 13:04:44 -04:00
|
|
|
chats_allocated ++;
|
2014-01-10 06:32:57 -05:00
|
|
|
U = talloc0 (sizeof (*U));
|
2013-11-22 18:26:35 -05:00
|
|
|
U->id = MK_CHAT (data[1]);
|
2013-10-24 13:04:44 -04:00
|
|
|
peer_tree = tree_insert_peer (peer_tree, U, lrand48 ());
|
2013-12-02 12:19:08 -05:00
|
|
|
assert (peer_num < MAX_PEER_NUM);
|
2013-11-04 12:34:27 -05:00
|
|
|
Peers[peer_num ++] = U;
|
2013-10-24 13:04:44 -04:00
|
|
|
}
|
2013-11-22 18:26:35 -05:00
|
|
|
fetch_chat (&U->chat);
|
|
|
|
return &U->chat;
|
2013-10-24 13:04:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct chat *fetch_alloc_chat_full (void) {
|
|
|
|
int data[3];
|
|
|
|
prefetch_data (data, 12);
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_t *U = user_chat_get (MK_CHAT (data[2]));
|
2013-10-24 13:04:44 -04:00
|
|
|
if (U) {
|
|
|
|
fetch_chat_full (&U->chat);
|
|
|
|
return &U->chat;
|
2013-10-14 13:26:25 -04:00
|
|
|
} else {
|
2013-10-24 13:04:44 -04:00
|
|
|
chats_allocated ++;
|
2014-01-10 06:32:57 -05:00
|
|
|
U = talloc0 (sizeof (*U));
|
2013-10-31 19:18:34 -04:00
|
|
|
U->id = MK_CHAT (data[2]);
|
2013-10-14 13:26:25 -04:00
|
|
|
peer_tree = tree_insert_peer (peer_tree, U, lrand48 ());
|
2013-10-24 13:04:44 -04:00
|
|
|
fetch_chat_full (&U->chat);
|
2013-12-02 12:19:08 -05:00
|
|
|
assert (peer_num < MAX_PEER_NUM);
|
2013-11-04 12:34:27 -05:00
|
|
|
Peers[peer_num ++] = U;
|
2013-10-14 13:26:25 -04:00
|
|
|
return &U->chat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_chat (struct chat *U) {
|
2014-01-10 09:37:56 -05:00
|
|
|
if (U->title) { tfree_str (U->title); }
|
|
|
|
if (U->print_title) { tfree_str (U->print_title); }
|
2013-10-14 13:26:25 -04:00
|
|
|
}
|
|
|
|
|
2013-10-13 06:18:08 -04:00
|
|
|
int print_stat (char *s, int len) {
|
2014-01-10 09:37:56 -05:00
|
|
|
return tsnprintf (s, len,
|
2013-10-14 13:26:25 -04:00
|
|
|
"users_allocated\t%d\n"
|
|
|
|
"chats_allocated\t%d\n"
|
2013-11-04 12:34:27 -05:00
|
|
|
"secret_chats_allocated\t%d\n"
|
|
|
|
"peer_num\t%d\n"
|
2013-10-14 13:26:25 -04:00
|
|
|
"messages_allocated\t%d\n",
|
|
|
|
users_allocated,
|
|
|
|
chats_allocated,
|
2013-11-04 12:34:27 -05:00
|
|
|
encr_chats_allocated,
|
|
|
|
peer_num,
|
2013-10-14 13:26:25 -04:00
|
|
|
messages_allocated
|
|
|
|
);
|
2013-10-13 06:18:08 -04:00
|
|
|
}
|
2013-10-16 15:19:39 -04:00
|
|
|
|
2013-10-31 19:18:34 -04:00
|
|
|
peer_t *user_chat_get (peer_id_t id) {
|
2013-11-14 19:08:24 -05:00
|
|
|
static peer_t U;
|
2013-10-16 15:19:39 -04:00
|
|
|
U.id = id;
|
|
|
|
return tree_lookup_peer (peer_tree, &U);
|
|
|
|
}
|
|
|
|
|
2013-11-02 06:14:30 -04:00
|
|
|
struct message *message_get (long long id) {
|
2013-10-16 15:19:39 -04:00
|
|
|
struct message M;
|
|
|
|
M.id = id;
|
|
|
|
return tree_lookup_message (message_tree, &M);
|
|
|
|
}
|
|
|
|
|
2013-11-02 06:14:30 -04:00
|
|
|
void update_message_id (struct message *M, long long id) {
|
2013-10-16 15:19:39 -04:00
|
|
|
message_tree = tree_delete_message (message_tree, M);
|
|
|
|
M->id = id;
|
|
|
|
message_tree = tree_insert_message (message_tree, M, lrand48 ());
|
|
|
|
}
|
2013-10-18 15:30:24 -04:00
|
|
|
|
2013-11-29 16:43:56 -05:00
|
|
|
void message_insert_tree (struct message *M) {
|
|
|
|
assert (M->id);
|
|
|
|
message_tree = tree_insert_message (message_tree, M, lrand48 ());
|
|
|
|
}
|
|
|
|
|
|
|
|
void message_remove_tree (struct message *M) {
|
|
|
|
assert (M->id);
|
|
|
|
message_tree = tree_delete_message (message_tree, M);
|
|
|
|
}
|
|
|
|
|
2013-10-18 15:30:24 -04:00
|
|
|
void message_insert (struct message *M) {
|
|
|
|
message_add_use (M);
|
2013-11-04 12:34:27 -05:00
|
|
|
message_add_peer (M);
|
2013-11-29 16:43:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void message_insert_unsent (struct message *M) {
|
|
|
|
message_unsent_tree = tree_insert_message (message_unsent_tree, M, lrand48 ());
|
|
|
|
}
|
|
|
|
|
|
|
|
void message_remove_unsent (struct message *M) {
|
|
|
|
message_unsent_tree = tree_delete_message (message_unsent_tree, M);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __send_msg (struct message *M) {
|
|
|
|
logprintf ("Resending message...\n");
|
|
|
|
print_message (M);
|
|
|
|
|
|
|
|
do_send_msg (M);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_all_unsent (void ) {
|
|
|
|
tree_act_message (message_unsent_tree, __send_msg);
|
2013-10-18 15:30:24 -04:00
|
|
|
}
|
2014-01-31 11:03:40 -05:00
|
|
|
|
|
|
|
void peer_insert_name (peer_t *P) {
|
|
|
|
//if (!P->print_name || !strlen (P->print_name)) { return; }
|
|
|
|
//logprintf ("+%s\n", P->print_name);
|
|
|
|
peer_by_name_tree = tree_insert_peer_by_name (peer_by_name_tree, P, lrand48 ());
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_delete_name (peer_t *P) {
|
|
|
|
//if (!P->print_name || !strlen (P->print_name)) { return; }
|
|
|
|
//logprintf ("-%s\n", P->print_name);
|
|
|
|
peer_by_name_tree = tree_delete_peer_by_name (peer_by_name_tree, P);
|
|
|
|
}
|
|
|
|
|
|
|
|
peer_t *peer_lookup_name (const char *s) {
|
|
|
|
static peer_t P;
|
|
|
|
P.print_name = (void *)s;
|
|
|
|
peer_t *R = tree_lookup_peer_by_name (peer_by_name_tree, &P);
|
|
|
|
return R;
|
|
|
|
}
|