forked from vysheng/tg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structures.c
1982 lines (1798 loc) · 50.4 KB
/
structures.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <string.h>
#include "structures.h"
#include "mtproto-common.h"
#include "telegram.h"
#include "tree.h"
#include "loop.h"
#include <openssl/rand.h>
#include <openssl/aes.h>
#include <openssl/sha.h>
#include "queries.h"
#include "binlog.h"
#define sha1 SHA1
static int id_cmp (struct message *M1, struct message *M2);
#define peer_cmp(a,b) (cmp_peer_id (a->id, b->id))
DEFINE_TREE(peer,peer_t *,peer_cmp,0)
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;
struct tree_message *message_tree;
struct tree_message *message_unsent_tree;
int users_allocated;
int chats_allocated;
int messages_allocated;
int peer_num;
int encr_chats_allocated;
int geo_chats_allocated;
int our_id;
int verbosity;
peer_t *Peers[MAX_PEER_NUM];
extern int binlog_enabled;
void fetch_skip_photo (void);
#define code_assert(x) if (!(x)) { logprintf ("Can not parse at line %d\n", __LINE__); assert (0); return -1; }
#define code_try(x) if ((x) == -1) { return -1; }
/*
*
* Fetch simple structures (immediate fetch into buffer)
*
*/
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 ();
}
return 0;
}
int fetch_user_status (struct user_status *S) {
unsigned x = fetch_int ();
code_assert (x == CODE_user_status_empty || x == CODE_user_status_online || x == CODE_user_status_offline);
switch (x) {
case CODE_user_status_empty:
S->online = 0;
S->when = 0;
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);
}
return 0;
}
/*
*
* 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;
}
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];
buf[0] = 0;
int i;
int p = 0;
for (i = 0; i < 4; i++) {
if (d[i] && strlen (d[i])) {
p += tsnprintf (buf + p, 9999 - p, "%s%s", p ? "_" : "", d[i]);
assert (p < 9990);
}
}
char *s = buf;
while (*s) {
if (*s == ' ') { *s = '_'; }
s++;
}
s = buf;
int fl = strlen (s);
int cc = 0;
while (1) {
int ok = 1;
int i;
for (i = 0; i < peer_num; i++) {
assert (Peers[i]);
if (cmp_peer_id (Peers[i]->id, id) && Peers[i]->print_name && !strcmp (Peers[i]->print_name, s)) {
ok = 0;
break;
}
}
if (ok) {
break;
}
cc ++;
assert (cc <= 9999);
tsnprintf (s + fl, 9999 - fl, "#%d", cc);
}
return tstrdup (s);
}
/*
*
* Fetch with log event
*
*/
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;
code_try (fetch_file_location (&small));
code_try (fetch_file_location (&big));
bl_do_set_user_profile_photo (U, photo_id, &big, &small);
return 0;
}
int fetch_user (struct user *U) {
unsigned x = fetch_int ();
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);
U->id = MK_USER (fetch_int ());
if (x == CODE_user_empty) {
return 0;
}
if (x == CODE_user_self) {
assert (!our_id || (our_id == get_peer_id (U->id)));
if (!our_id) {
bl_do_set_our_id (get_peer_id (U->id));
write_auth_file ();
}
}
int new = 0;
if (!(U->flags & FLAG_CREATED)) {
new = 1;
}
if (new) {
int l1 = prefetch_strlen ();
code_assert (l1 >= 0);
char *s1 = fetch_str (l1);
int l2 = prefetch_strlen ();
code_assert (l2 >= 0);
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);
}
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 ();
code_assert (phone_len >= 0);
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 ();
}
}
} 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);
}
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);
}
}
}
return 0;
}
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;
}
int new = !(U->flags & FLAG_CREATED);
if (x == CODE_encrypted_chat_discarded) {
if (new) {
logprintf ("Unknown chat in deleted state. May be we forgot something...\n");
return;
}
bl_do_encr_chat_delete (U);
write_secret_chat_file ();
return;
}
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;
}
if (x == CODE_encrypted_chat_requested || x == CODE_encrypted_chat) {
memset (g_key, 0, sizeof (g_key));
memset (nonce, 0, sizeof (nonce));
}
int l = prefetch_strlen ();
char *s = fetch_str (l);
if (l != 256) { logprintf ("l = %d\n", l); }
if (l < 256) {
memcpy (g_key + 256 - l, s, l);
} else {
memcpy (g_key, s + (l - 256), 256);
}
/*l = prefetch_strlen ();
s = fetch_str (l);
if (l != 256) { logprintf ("l = %d\n", l); }
if (l < 256) {
memcpy (nonce + 256 - l, s, l);
} else {
memcpy (nonce, s + (l - 256), 256);
}*/
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;
}
bl_do_encr_chat_requested (U, access_hash, date, admin_id, user_id, (void *)g_key, (void *)nonce);
write_secret_chat_file ();
} else {
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;
}
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));
}
int l = prefetch_strlen ();
char *s = fetch_str (l);
if (l != 256) { logprintf ("l = %d\n", l); }
if (l < 256) {
memcpy (g_key + 256 - l, s, l);
} else {
memcpy (g_key, s + (l - 256), 256);
}
/*l = prefetch_strlen ();
s = fetch_str (l);
if (l != 256) { logprintf ("l = %d\n", l); }
if (l < 256) {
memcpy (nonce + 256 - l, s, l);
} else {
memcpy (nonce, s + (l - 256), 256);
}*/
if (x == CODE_encrypted_chat_requested) {
return; // Duplicate?
}
bl_do_encr_chat_accepted (U, (void *)g_key, (void *)nonce, fetch_long ());
}
write_secret_chat_file ();
}
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);
if (x == CODE_contacts_foreign_link_requested) {
fetch_bool ();
}
fetch_alloc_user ();
int *start = in_ptr;
fetch_skip_photo ();
bl_do_set_user_full_photo (U, start, 4 * (in_ptr - start));
fetch_notify_settings ();
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);
}
void fetch_chat (struct chat *C) {
unsigned x = fetch_int ();
assert (x == CODE_chat_empty || x == CODE_chat || x == CODE_chat_forbidden);
C->id = MK_CHAT (fetch_int ());
if (x == CODE_chat_empty) {
return;
}
int new = !(C->flags & FLAG_CREATED);
if (new) {
int y = 0;
if (x == CODE_chat_forbidden) {
y |= FLAG_FORBIDDEN;
}
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) {
unsigned z = fetch_int ();
if (z == CODE_chat_photo_empty) {
small.dc = -2;
big.dc = -2;
} else {
assert (z == CODE_chat_photo);
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;
}
bl_do_create_chat (C, y, s, l, users_num, date, version, &big, &small);
} else {
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 ());
}
}
}
void fetch_notify_settings (void) {
unsigned x = fetch_int ();
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) {
fetch_int (); // mute_until
int l = prefetch_strlen ();
fetch_str (l);
fetch_bool (); // show_previews
fetch_int (); // peer notify events
}
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
}
}
void fetch_chat_full (struct chat *C) {
unsigned x = fetch_int ();
assert (x == CODE_messages_chat_full);
assert (fetch_int () == CODE_chat_full);
C->id = MK_CHAT (fetch_int ());
//C->flags &= ~(FLAG_DELETED | FLAG_FORBIDDEN | FLAG_CHAT_IN_CHAT);
//C->flags |= FLAG_CREATED;
x = fetch_int ();
int version = 0;
struct chat_user *users = 0;
int users_num = 0;
int admin_id = 0;
if (x == CODE_chat_participants) {
assert (fetch_int () == get_peer_id (C->id));
admin_id = fetch_int ();
assert (fetch_int () == CODE_vector);
users_num = fetch_int ();
users = talloc (sizeof (struct chat_user) * users_num);
int i;
for (i = 0; i < users_num; i++) {
assert (fetch_int () == (int)CODE_chat_participant);
users[i].user_id = fetch_int ();
users[i].inviter_id = fetch_int ();
users[i].date = fetch_int ();
}
version = fetch_int ();
}
int *start = in_ptr;
fetch_skip_photo ();
int *end = in_ptr;
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 ();
}
if (admin_id) {
bl_do_set_chat_admin (C, admin_id);
}
if (version > 0) {
bl_do_set_chat_participants (C, version, users_num, users);
tfree (users, sizeof (struct chat_user) * users_num);
}
bl_do_set_chat_full_photo (C, start, 4 * (end - start));
}
void fetch_photo_size (struct photo_size *S) {
memset (S, 0, sizeof (*S));
unsigned x = fetch_int ();
assert (x == CODE_photo_size || x == CODE_photo_cached_size || x == CODE_photo_size_empty);
S->type = fetch_str_dup ();
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 {
S->size = prefetch_strlen ();
// S->data = talloc (S->size);
fetch_str (S->size);
// memcpy (S->data, fetch_str (S->size), S->size);
}
}
}
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);
}
}
}
void fetch_geo (struct geo *G) {
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;
}
}
void fetch_skip_geo (void) {
unsigned x = fetch_int ();
assert (x == CODE_geo_point || x == CODE_geo_point_empty);
if (x == CODE_geo_point) {
in_ptr += 4;
}
}
void fetch_photo (struct photo *P) {
memset (P, 0, sizeof (*P));
unsigned x = fetch_int ();
assert (x == CODE_photo_empty || x == CODE_photo);
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 ();
P->sizes = talloc (sizeof (struct photo_size) * P->sizes_num);
int i;
for (i = 0; i < P->sizes_num; i++) {
fetch_photo_size (&P->sizes[i]);
}
}
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 ();
}
}
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 ();
}
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);
}
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 ();
}
void fetch_skip_audio (void) {
unsigned x = fetch_int ();
fetch_skip (2);
if (x == CODE_audio_empty) { return; }
fetch_skip (7);
}
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 ();
}
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);
}
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;
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;
case CODE_message_action_chat_create:
M->title = fetch_str_dup ();
assert (fetch_int () == (int)CODE_vector);
M->user_num = fetch_int ();
M->users = talloc (M->user_num * 4);
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);
}
}
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);
}
}
void fetch_message_short (struct message *M) {
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 ();
}
}
void fetch_message_short_chat (struct message *M) {
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 ();
}
}
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;
case CODE_message_media_audio:
fetch_audio (&M->audio);
break;
case CODE_message_media_document:
fetch_document (&M->document);
break;
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:
M->data_size = prefetch_strlen ();
M->data = talloc (M->data_size);
memcpy (M->data, fetch_str (M->data_size), M->data_size);
break;
default:
logprintf ("type = 0x%08x\n", M->type);
assert (0);
}
}
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);