forked from vysheng/tgl
-
Notifications
You must be signed in to change notification settings - Fork 9
/
structures.c
2779 lines (2431 loc) · 79.1 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 tgl-library
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Copyright Vitaly Valtman 2013-2015
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <string.h>
#include <strings.h>
#include "tgl-structures.h"
#include "mtproto-common.h"
//#include "telegram.h"
#include "tree.h"
#include "crypto/aes.h"
#include "crypto/bn.h"
#include "crypto/sha.h"
#include "queries.h"
#include "tgl-binlog.h"
#include "tgl-methods-in.h"
#include "updates.h"
#include "mtproto-client.h"
#include "tgl.h"
#include "auto.h"
#include "auto/auto-types.h"
#include "auto/auto-skip.h"
#include "auto/auto-fetch-ds.h"
#include "auto/auto-free-ds.h"
static int id_cmp (struct tgl_message *M1, struct tgl_message *M2);
#define peer_cmp(a,b) (tgl_cmp_peer_id (a->id, b->id))
#define peer_cmp_name(a,b) (strcmp (a->print_name, b->print_name))
static int random_id_cmp (struct tgl_message *L, struct tgl_message *R) {
if (L->random_id < R->random_id) { return -1; }
if (L->random_id > R->random_id) { return 1; }
return 0;
}
static int temp_id_cmp (struct tgl_message *L, struct tgl_message *R) {
if (L->temp_id < R->temp_id) { return -1; }
if (L->temp_id > R->temp_id) { return 1; }
return 0;
}
static int photo_id_cmp (struct tgl_photo *L, struct tgl_photo *R) {
if (L->id < R->id) { return -1; }
if (L->id > R->id) { return 1; }
return 0;
}
static int document_id_cmp (struct tgl_document *L, struct tgl_document *R) {
if (L->id < R->id) { return -1; }
if (L->id > R->id) { return 1; }
return 0;
}
static int webpage_id_cmp (struct tgl_webpage *L, struct tgl_webpage *R) {
if (L->id < R->id) { return -1; }
if (L->id > R->id) { return 1; }
return 0;
}
DEFINE_TREE(peer,tgl_peer_t *,peer_cmp,0)
DEFINE_TREE(peer_by_name,tgl_peer_t *,peer_cmp_name,0)
DEFINE_TREE(message,struct tgl_message *,id_cmp,0)
DEFINE_TREE(random_id,struct tgl_message *, random_id_cmp,0)
DEFINE_TREE(temp_id,struct tgl_message *, temp_id_cmp,0)
DEFINE_TREE(photo,struct tgl_photo *,photo_id_cmp,0)
DEFINE_TREE(document,struct tgl_document *,document_id_cmp,0)
DEFINE_TREE(webpage,struct tgl_webpage *,webpage_id_cmp,0)
static void increase_peer_size (struct tgl_state *TLS);
char *tgls_default_create_print_name (struct tgl_state *TLS, tgl_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 (((unsigned char)*s) <= ' ') { *s = '_'; }
if (*s == '#') { *s = '_'; }
if (*s == '$') { *s = '_'; }
if (*s == '@') { *s = '_'; }
s++;
}
s = buf;
int fl = strlen (s);
int cc = 0;
while (1) {
tgl_peer_t *P = tgl_peer_get_by_name (TLS, s);
if (!P || !tgl_cmp_peer_id (P->id, id)) {
break;
}
cc ++;
assert (cc <= 9999);
tsnprintf (s + fl, 9999 - fl, "#%d", cc);
}
return tstrdup (s);
}
enum tgl_typing_status tglf_fetch_typing (struct tl_ds_send_message_action *DS_SMA) {
if (!DS_SMA) { return 0; }
switch (DS_SMA->magic) {
case CODE_send_message_typing_action:
return tgl_typing_typing;
case CODE_send_message_cancel_action:
return tgl_typing_cancel;
case CODE_send_message_record_video_action:
return tgl_typing_record_video;
case CODE_send_message_upload_video_action:
return tgl_typing_upload_video;
case CODE_send_message_record_audio_action:
return tgl_typing_record_audio;
case CODE_send_message_upload_audio_action:
return tgl_typing_upload_audio;
case CODE_send_message_upload_photo_action:
return tgl_typing_upload_photo;
case CODE_send_message_upload_document_action:
return tgl_typing_upload_document;
case CODE_send_message_geo_location_action:
return tgl_typing_geo;
case CODE_send_message_choose_contact_action:
return tgl_typing_choose_contact;
default:
assert (0);
return tgl_typing_none;
}
}
/*enum tgl_typing_status tglf_fetch_typing (void) {
struct tl_ds_send_message_action *DS_SMA = fetch_ds_type_send_message_action (TYPE_TO_PARAM (send_message_action));
enum tgl_typing_status res = tglf_fetch_typing (DS_SMA);
free_ds_type_send_message_action (DS_SMA, TYPE_TO_PARAM (send_message_action));
return res;
}*/
/* {{{ Fetch */
tgl_peer_id_t tglf_fetch_peer_id (struct tgl_state *TLS, struct tl_ds_peer *DS_P) {
switch (DS_P->magic) {
case CODE_peer_user:
return TGL_MK_USER (DS_LVAL (DS_P->user_id));
case CODE_peer_chat:
return TGL_MK_CHAT (DS_LVAL (DS_P->chat_id));
case CODE_peer_channel:
return TGL_MK_CHANNEL (DS_LVAL (DS_P->channel_id));
default:
assert (0);
exit (2);
}
}
int tglf_fetch_file_location (struct tgl_state *TLS, struct tgl_file_location *loc, struct tl_ds_file_location *DS_FL) {
if (!DS_FL) { return 0; }
loc->dc = DS_LVAL (DS_FL->dc_id);
loc->volume = DS_LVAL (DS_FL->volume_id);
loc->local_id = DS_LVAL (DS_FL->local_id);
loc->secret = DS_LVAL (DS_FL->secret);
return 0;
}
int tglf_fetch_user_status (struct tgl_state *TLS, struct tgl_user_status *S, struct tgl_user *U, struct tl_ds_user_status *DS_US) {
if (!DS_US) { return 0; }
switch (DS_US->magic) {
case CODE_user_status_empty:
if (S->online) {
tgl_insert_status_update (TLS, U);
if (S->online == 1) {
tgl_remove_status_expire (TLS, U);
}
}
S->online = 0;
S->when = 0;
break;
case CODE_user_status_online:
{
if (S->online != 1) {
S->when = DS_LVAL (DS_US->expires);
if (S->online) {
tgl_insert_status_update (TLS, U);
}
tgl_insert_status_expire (TLS, U);
S->online = 1;
} else {
if (DS_LVAL (DS_US->expires) != S->when) {
S->when = DS_LVAL (DS_US->expires);
tgl_remove_status_expire (TLS, U);
tgl_insert_status_expire (TLS, U);
}
}
}
break;
case CODE_user_status_offline:
if (S->online != -1) {
if (S->online) {
tgl_insert_status_update (TLS, U);
}
if (S->online == 1) {
tgl_remove_status_expire (TLS, U);
}
}
S->online = -1;
S->when = DS_LVAL (DS_US->was_online);
break;
case CODE_user_status_recently:
if (S->online != -2) {
if (S->online) {
tgl_insert_status_update (TLS, U);
}
if (S->online == 1) {
tgl_remove_status_expire (TLS, U);
}
}
S->online = -2;
break;
case CODE_user_status_last_week:
if (S->online != -3) {
if (S->online) {
tgl_insert_status_update (TLS, U);
}
if (S->online == 1) {
tgl_remove_status_expire (TLS, U);
}
}
S->online = -3;
break;
case CODE_user_status_last_month:
if (S->online != -4) {
if (S->online) {
tgl_insert_status_update (TLS, U);
}
if (S->online == 1) {
tgl_remove_status_expire (TLS, U);
}
}
S->online = -4;
break;
default:
assert (0);
}
return 0;
}
struct tgl_user *tglf_fetch_alloc_user (struct tgl_state *TLS, struct tl_ds_user *DS_U) {
if (!DS_U) { return 0; }
if (DS_U->magic == CODE_user_empty) {
return 0;
}
tgl_peer_id_t user_id = TGL_MK_USER (DS_LVAL (DS_U->id));
user_id.access_hash = DS_LVAL (DS_U->access_hash);
struct tgl_user *U = (struct tgl_user *)tgl_peer_get (TLS, user_id);
if (!U) {
TLS->users_allocated ++;
U = talloc0 (sizeof (tgl_peer_t));
U->id = user_id;
TLS->peer_tree = tree_insert_peer (TLS->peer_tree, (tgl_peer_t *)U, rand ());
increase_peer_size (TLS);
TLS->Peers[TLS->peer_num ++] = (tgl_peer_t *)U;
}
int flags = U->flags;
if (DS_LVAL (DS_U->flags) & (1 << 10)) {
bl_do_set_our_id (TLS, U->id);
flags |= TGLUF_SELF;
} else {
flags &= ~TGLUF_SELF;
}
if (DS_LVAL (DS_U->flags) & (1 << 11)) {
flags |= TGLUF_CONTACT;
} else {
flags &= ~TGLUF_CONTACT;
}
if (DS_LVAL (DS_U->flags) & (1 << 12)) {
flags |= TGLUF_MUTUAL_CONTACT;
} else {
flags &= ~TGLUF_MUTUAL_CONTACT;
}
if (DS_LVAL (DS_U->flags) & (1 << 14)) {
flags |= TGLUF_BOT;
U->bot = 1;
} else {
flags &= ~TGLUF_BOT;
U->bot = 0;
}
/*
if (DS_LVAL (DS_U->flags) & (1 << 15)) {
flags |= TGLUF_BOT_FULL_ACCESS;
}
if (DS_LVAL (DS_U->flags) & (1 << 16)) {
flags |= TGLUF_BOT_NO_GROUPS;
}*/
if (DS_LVAL (DS_U->flags) & (1 << 17)) {
flags |= TGLUF_OFFICIAL;
} else {
flags &= ~TGLUF_OFFICIAL;
}
if (!(flags & TGLUF_CREATED)) {
flags |= TGLUF_CREATE | TGLUF_CREATED;
}
bl_do_user (TLS, tgl_get_peer_id (U->id),
DS_U->access_hash,
DS_STR (DS_U->first_name),
DS_STR (DS_U->last_name),
DS_STR (DS_U->phone),
DS_STR (DS_U->username),
NULL,
DS_U->photo,
NULL, NULL,
NULL,
flags
);
if (DS_U->status) {
assert (tglf_fetch_user_status (TLS, &U->status, U, DS_U->status) >= 0);
}
if (DS_LVAL (DS_U->flags) & (1 << 13)) {
if (!(U->flags & TGLUF_DELETED)) {
bl_do_peer_delete (TLS, U->id);
}
}
return U;
}
struct tgl_user *tglf_fetch_alloc_user_full (struct tgl_state *TLS, struct tl_ds_user_full *DS_UF) {
if (!DS_UF) { return NULL; }
struct tgl_user *U = tglf_fetch_alloc_user (TLS, DS_UF->user);
if (!U) { return NULL; }
int flags = U->flags;
if (DS_BVAL (DS_UF->blocked)) {
flags |= TGLUF_BLOCKED;
} else {
flags &= ~TGLUF_BLOCKED;
}
bl_do_user (TLS, tgl_get_peer_id (U->id),
NULL,
NULL, 0,
NULL, 0,
NULL, 0,
NULL, 0,
DS_UF->profile_photo,
NULL,
NULL, NULL,
DS_UF->bot_info,
flags
);
return U;
}
void str_to_256 (unsigned char *dst, char *src, int src_len) {
if (src_len >= 256) {
memcpy (dst, src + src_len - 256, 256);
} else {
memset(dst, 0, 256 - src_len);
memcpy (dst + 256 - src_len, src, src_len);
}
}
void str_to_32 (unsigned char *dst, char *src, int src_len) {
if (src_len >= 32) {
memcpy (dst, src + src_len - 32, 32);
} else {
memset(dst, 0, 32 - src_len);
memcpy (dst + 32 - src_len, src, src_len);
}
}
struct tgl_secret_chat *tglf_fetch_alloc_encrypted_chat (struct tgl_state *TLS, struct tl_ds_encrypted_chat *DS_EC) {
if (!DS_EC) { return NULL; }
if (DS_EC->magic == CODE_encrypted_chat_empty) {
return NULL;
}
tgl_peer_id_t chat_id = TGL_MK_ENCR_CHAT (DS_LVAL (DS_EC->id));
chat_id.access_hash = DS_LVAL (DS_EC->access_hash);
struct tgl_secret_chat *U = (void *)tgl_peer_get (TLS, chat_id);
if (!U) {
TLS->encr_chats_allocated ++;
U = talloc0 (sizeof (tgl_peer_t));
U->id = chat_id;
TLS->peer_tree = tree_insert_peer (TLS->peer_tree, (tgl_peer_t *)U, rand ());
increase_peer_size (TLS);
TLS->Peers[TLS->peer_num ++] = (tgl_peer_t *)U;
}
int new = !(U->flags & TGLPF_CREATED);
if (DS_EC->magic == CODE_encrypted_chat_discarded) {
if (new) {
vlogprintf (E_WARNING, "Unknown chat in deleted state. May be we forgot something...\n");
return U;
}
bl_do_peer_delete (TLS, U->id);
//write_secret_chat_file ();
return U;
}
static unsigned char g_key[256];
if (new) {
if (DS_EC->magic != CODE_encrypted_chat_requested) {
vlogprintf (E_WARNING, "Unknown chat. May be we forgot something...\n");
return U;
}
str_to_256 (g_key, DS_STR (DS_EC->g_a));
int user_id = DS_LVAL (DS_EC->participant_id) + DS_LVAL (DS_EC->admin_id) - tgl_get_peer_id (TLS->our_id);
int r = sc_request;
bl_do_encr_chat (TLS, tgl_get_peer_id (U->id),
DS_EC->access_hash,
DS_EC->date,
DS_EC->admin_id,
&user_id,
NULL,
(void *)g_key,
NULL,
&r,
NULL, NULL, NULL, NULL, NULL,
NULL,
TGLECF_CREATE | TGLECF_CREATED,
NULL, 0
);
} else {
if (DS_EC->magic == CODE_encrypted_chat_waiting) {
int r = sc_waiting;
bl_do_encr_chat (TLS, tgl_get_peer_id (U->id),
DS_EC->access_hash,
DS_EC->date,
NULL,
NULL,
NULL,
NULL,
NULL,
&r,
NULL, NULL, NULL, NULL, NULL,
NULL,
TGL_FLAGS_UNCHANGED,
NULL, 0
);
return U; // We needed only access hash from here
}
str_to_256 (g_key, DS_STR (DS_EC->g_a_or_b));
//write_secret_chat_file ();
int r = sc_ok;
bl_do_encr_chat (TLS, tgl_get_peer_id (U->id),
DS_EC->access_hash,
DS_EC->date,
NULL,
NULL,
NULL,
g_key,
NULL,
&r,
NULL, NULL, NULL, NULL, NULL,
DS_EC->key_fingerprint,
TGL_FLAGS_UNCHANGED,
NULL, 0
);
}
return U;
}
struct tgl_chat *tglf_fetch_alloc_chat (struct tgl_state *TLS, struct tl_ds_chat *DS_C) {
if (!DS_C) { return NULL; }
if (DS_C->magic == CODE_chat_empty) {
return NULL;
}
if (DS_C->magic == CODE_channel || DS_C->magic == CODE_channel_forbidden) {
return (void *)tglf_fetch_alloc_channel (TLS, DS_C);
}
tgl_peer_id_t chat_id = TGL_MK_CHAT (DS_LVAL (DS_C->id));
chat_id.access_hash = 0; // chats don't have access hash
struct tgl_chat *C = (void *)tgl_peer_get (TLS, chat_id);
if (!C) {
TLS->chats_allocated ++;
C = talloc0 (sizeof (tgl_peer_t));
C->id = chat_id;
TLS->peer_tree = tree_insert_peer (TLS->peer_tree, (tgl_peer_t *)C, rand ());
increase_peer_size (TLS);
TLS->Peers[TLS->peer_num ++] = (tgl_peer_t *)C;
}
C->id = chat_id;
int flags = C->flags;
if (!(flags & TGLCF_CREATED)) {
flags |= TGLCF_CREATE | TGLCF_CREATED;
}
if (DS_LVAL (DS_C->flags) & 1) {
flags |= TGLCF_CREATOR;
} else {
flags &= ~TGLCF_CREATOR;
}
if (DS_LVAL (DS_C->flags) & 2) {
flags |= TGLCF_KICKED;
} else {
flags &= ~TGLCF_KICKED;
}
if (DS_LVAL (DS_C->flags) & 4) {
flags |= TGLCF_LEFT;
} else {
flags &= ~TGLCF_LEFT;
}
if (DS_LVAL (DS_C->flags) & 8) {
flags |= TGLCF_ADMINS_ENABLED;
} else {
flags &= ~TGLCF_ADMINS_ENABLED;
}
if (DS_LVAL (DS_C->flags) & 16) {
flags |= TGLCF_ADMIN;
} else {
flags &= ~TGLCF_ADMIN;
}
if (DS_LVAL (DS_C->flags) & 32) {
flags |= TGLCF_DEACTIVATED;
} else {
flags &= ~TGLCF_DEACTIVATED;
}
bl_do_chat (TLS, tgl_get_peer_id (C->id),
DS_STR (DS_C->title),
DS_C->participants_count,
DS_C->date,
NULL,
NULL,
DS_C->photo,
NULL,
NULL,
NULL, NULL,
flags
);
return C;
}
struct tgl_chat *tglf_fetch_alloc_chat_full (struct tgl_state *TLS, struct tl_ds_messages_chat_full *DS_MCF) {
if (!DS_MCF) { return NULL; }
if (DS_MCF->full_chat->magic == CODE_channel_full) {
return (void *)tglf_fetch_alloc_channel_full (TLS, DS_MCF);
}
if (DS_MCF->users) {
int i;
for (i = 0; i < DS_LVAL (DS_MCF->users->cnt); i++) {
tglf_fetch_alloc_user (TLS, DS_MCF->users->data[i]);
}
}
if (DS_MCF->chats) {
int i;
for (i = 0; i < DS_LVAL (DS_MCF->chats->cnt); i++) {
tglf_fetch_alloc_chat (TLS, DS_MCF->chats->data[i]);
}
}
struct tl_ds_chat_full *DS_CF = DS_MCF->full_chat;
if (DS_CF->bot_info) {
int n = DS_LVAL (DS_CF->bot_info->cnt);
int i;
for (i = 0; i < n; i++) {
struct tl_ds_bot_info *DS_BI = DS_CF->bot_info->data[i];
tgl_peer_t *P = tgl_peer_get (TLS, TGL_MK_USER (DS_LVAL (DS_BI->user_id)));
if (P && (P->flags & TGLCF_CREATED)) {
bl_do_user (TLS, tgl_get_peer_id (P->id),
NULL,
NULL, 0,
NULL, 0,
NULL, 0,
NULL, 0,
NULL,
NULL,
NULL, NULL,
DS_BI,
TGL_FLAGS_UNCHANGED
);
}
}
}
tgl_peer_id_t chat_id = TGL_MK_CHAT (DS_LVAL (DS_CF->id));
struct tgl_chat *C = (void *)tgl_peer_get (TLS, chat_id);
assert (C);
bl_do_chat (TLS, tgl_get_peer_id (C->id),
NULL, 0,
NULL,
NULL,
DS_CF->participants->version,
(struct tl_ds_vector *)DS_CF->participants->participants,
NULL,
DS_CF->chat_photo,
NULL,
//DS_CF->participants->admin_id,
NULL, NULL,
C->flags & 0xffff
);
return C;
}
struct tgl_channel *tglf_fetch_alloc_channel (struct tgl_state *TLS, struct tl_ds_chat *DS_C) {
if (!DS_C) { return NULL; }
tgl_peer_id_t chat_id = TGL_MK_CHANNEL (DS_LVAL (DS_C->id));
chat_id.access_hash = DS_LVAL (DS_C->access_hash);
struct tgl_channel *C = (void *)tgl_peer_get (TLS, chat_id);
if (!C) {
TLS->channels_allocated ++;
C = talloc0 (sizeof (tgl_peer_t));
C->id = chat_id;
TLS->peer_tree = tree_insert_peer (TLS->peer_tree, (tgl_peer_t *)C, rand ());
increase_peer_size (TLS);
TLS->Peers[TLS->peer_num ++] = (tgl_peer_t *)C;
}
C->id = chat_id;
int flags = C->flags;
if (!(flags & TGLCHF_CREATED)) {
flags |= TGLCHF_CREATE | TGLCHF_CREATED;
}
if (DS_LVAL (DS_C->flags) & 1) {
flags |= TGLCHF_CREATOR;
} else {
flags &= ~TGLCHF_CREATOR;
}
if (DS_LVAL (DS_C->flags) & 2) {
flags |= TGLCHF_KICKED;
} else {
flags &= ~TGLCHF_KICKED;
}
if (DS_LVAL (DS_C->flags) & 4) {
flags |= TGLCHF_LEFT;
} else {
flags &= ~TGLCHF_LEFT;
}
if (DS_LVAL (DS_C->flags) & 8) {
flags |= TGLCHF_EDITOR;
} else {
flags &= ~TGLCHF_EDITOR;
}
if (DS_LVAL (DS_C->flags) & 16) {
flags |= TGLCHF_MODERATOR;
} else {
flags &= ~TGLCHF_MODERATOR;
}
if (DS_LVAL (DS_C->flags) & 32) {
flags |= TGLCHF_BROADCAST;
} else {
flags &= ~TGLCHF_BROADCAST;
}
if (DS_LVAL (DS_C->flags) & 128) {
flags |= TGLCHF_OFFICIAL;
} else {
flags &= ~TGLCHF_OFFICIAL;
}
if (DS_LVAL (DS_C->flags) & 256) {
flags |= TGLCHF_MEGAGROUP;
} else {
flags &= ~TGLCHF_MEGAGROUP;
}
bl_do_channel (TLS, tgl_get_peer_id (C->id),
DS_C->access_hash,
DS_C->date,
DS_STR (DS_C->title),
DS_STR (DS_C->username),
DS_C->photo,
NULL,
NULL,
NULL, 0,
NULL, NULL, NULL, NULL,
flags
);
return C;
}
struct tgl_channel *tglf_fetch_alloc_channel_full (struct tgl_state *TLS, struct tl_ds_messages_chat_full *DS_MCF) {
if (!DS_MCF) { return NULL; }
if (DS_MCF->users) {
int i;
for (i = 0; i < DS_LVAL (DS_MCF->users->cnt); i++) {
tglf_fetch_alloc_user (TLS, DS_MCF->users->data[i]);
}
}
if (DS_MCF->chats) {
int i;
for (i = 0; i < DS_LVAL (DS_MCF->chats->cnt); i++) {
tglf_fetch_alloc_chat (TLS, DS_MCF->chats->data[i]);
}
}
struct tl_ds_chat_full *DS_CF = DS_MCF->full_chat;
tgl_peer_id_t chat_id = TGL_MK_CHANNEL (DS_LVAL (DS_CF->id));
struct tgl_channel *C = (void *)tgl_peer_get (TLS, chat_id);
assert (C);
bl_do_channel (TLS, tgl_get_peer_id (C->id),
NULL,
NULL,
NULL, 0,
NULL, 0,
NULL,
DS_CF->chat_photo,
NULL,
DS_STR (DS_CF->about),
DS_CF->participants_count,
DS_CF->admins_count,
DS_CF->kicked_count,
DS_CF->read_inbox_max_id,
TGL_FLAGS_UNCHANGED
);
return C;
}
void tglf_fetch_photo_size (struct tgl_state *TLS, struct tgl_photo_size *S, struct tl_ds_photo_size *DS_PS) {
memset (S, 0, sizeof (*S));
S->type = DS_STR_DUP (DS_PS->type);
S->w = DS_LVAL (DS_PS->w);
S->h = DS_LVAL (DS_PS->h);
S->size = DS_LVAL (DS_PS->size);
if (DS_PS->bytes) {
S->size = DS_PS->bytes->len;
}
tglf_fetch_file_location (TLS, &S->loc, DS_PS->location);
}
void tglf_fetch_geo (struct tgl_state *TLS, struct tgl_geo *G, struct tl_ds_geo_point *DS_GP) {
G->longitude = DS_LVAL (DS_GP->longitude);
G->latitude = DS_LVAL (DS_GP->latitude);
}
struct tgl_photo *tglf_fetch_alloc_photo (struct tgl_state *TLS, struct tl_ds_photo *DS_P) {
if (!DS_P) { return NULL; }
if (DS_P->magic == CODE_photo_empty) { return NULL; }
struct tgl_photo *P = tgl_photo_get (TLS, DS_LVAL (DS_P->id));
if (P) {
P->refcnt ++;
return P;
}
P = talloc0 (sizeof (*P));
P->id = DS_LVAL (DS_P->id);
P->refcnt = 1;
tgl_photo_insert (TLS, P);
P->access_hash = DS_LVAL (DS_P->access_hash);
//P->user_id = DS_LVAL (DS_P->user_id);
P->date = DS_LVAL (DS_P->date);
P->caption = NULL;//DS_STR_DUP (DS_P->caption);
/*if (DS_P->geo) {
tglf_fetch_geo (TLS, &P->geo, DS_P->geo);
}*/
P->sizes_num = DS_LVAL (DS_P->sizes->cnt);
P->sizes = talloc (sizeof (struct tgl_photo_size) * P->sizes_num);
int i;
for (i = 0; i < P->sizes_num; i++) {
tglf_fetch_photo_size (TLS, &P->sizes[i], DS_P->sizes->data[i]);
}
return P;
}
struct tgl_document *tglf_fetch_alloc_video (struct tgl_state *TLS, struct tl_ds_video *DS_V) {
if (!DS_V) { return NULL; }
if (DS_V->magic == CODE_video_empty) { return NULL; }
struct tgl_document *D = tgl_document_get (TLS, DS_LVAL (DS_V->id));
if (D) {
D->refcnt ++;
return D;
}
D = talloc0 (sizeof (*D));
D->id = DS_LVAL (DS_V->id);
D->refcnt = 1;
tgl_document_insert (TLS, D);
D->flags = TGLDF_VIDEO;
D->access_hash = DS_LVAL (DS_V->access_hash);
//D->user_id = DS_LVAL (DS_V->user_id);
D->date = DS_LVAL (DS_V->date);
D->caption = NULL;//DS_STR_DUP (DS_V->caption);
D->duration = DS_LVAL (DS_V->duration);
D->mime_type = tstrdup ("video/");//DS_STR_DUP (DS_V->mime_type);
D->size = DS_LVAL (DS_V->size);
tglf_fetch_photo_size (TLS, &D->thumb, DS_V->thumb);
D->dc_id = DS_LVAL (DS_V->dc_id);
D->w = DS_LVAL (DS_V->w);
D->h = DS_LVAL (DS_V->h);
return D;
}
struct tgl_document *tglf_fetch_alloc_audio (struct tgl_state *TLS, struct tl_ds_audio *DS_A) {
if (!DS_A) { return NULL; }
if (DS_A->magic == CODE_audio_empty) { return NULL; }
struct tgl_document *D = tgl_document_get (TLS, DS_LVAL (DS_A->id));
if (D) {
D->refcnt ++;
return D;
}
D = talloc0 (sizeof (*D));
D->id = DS_LVAL (DS_A->id);
D->refcnt = 1;
tgl_document_insert (TLS, D);
D->flags = TGLDF_AUDIO;
D->access_hash = DS_LVAL (DS_A->access_hash);
//D->user_id = DS_LVAL (DS_A->user_id);
D->date = DS_LVAL (DS_A->date);
D->duration = DS_LVAL (DS_A->duration);
D->mime_type = DS_STR_DUP (DS_A->mime_type);
D->size = DS_LVAL (DS_A->size);
D->dc_id = DS_LVAL (DS_A->dc_id);
return D;
}
void tglf_fetch_document_attribute (struct tgl_state *TLS, struct tgl_document *D, struct tl_ds_document_attribute *DS_DA) {
switch (DS_DA->magic) {
case CODE_document_attribute_image_size:
D->flags |= TGLDF_IMAGE;
D->w = DS_LVAL (DS_DA->w);
D->h = DS_LVAL (DS_DA->h);
return;
case CODE_document_attribute_animated:
D->flags |= TGLDF_ANIMATED;
return;
case CODE_document_attribute_sticker:
D->flags |= TGLDF_STICKER;
return;
case CODE_document_attribute_video:
D->flags |= TGLDF_VIDEO;
D->duration = DS_LVAL (DS_DA->duration);
D->w = DS_LVAL (DS_DA->w);
D->h = DS_LVAL (DS_DA->h);
return;
case CODE_document_attribute_audio:
D->flags |= TGLDF_AUDIO;
D->duration = DS_LVAL (DS_DA->duration);
return;
case CODE_document_attribute_filename:
D->caption = DS_STR_DUP (DS_DA->file_name);
return;
default:
assert (0);
}
}
struct tgl_document *tglf_fetch_alloc_document (struct tgl_state *TLS, struct tl_ds_document *DS_D) {
if (!DS_D) { return NULL; }
if (DS_D->magic == CODE_document_empty) { return NULL; }
struct tgl_document *D = tgl_document_get (TLS, DS_LVAL (DS_D->id));
if (D) {
D->refcnt ++;
return D;
}
D = talloc0 (sizeof (*D));
D->id = DS_LVAL (DS_D->id);
D->refcnt = 1;
tgl_document_insert (TLS, D);
D->access_hash = DS_LVAL (DS_D->access_hash);
//D->user_id = DS_LVAL (DS_D->user_id);
D->date = DS_LVAL (DS_D->date);
//D->caption = DS_STR_DUP (DS_D->file_name);
D->mime_type = DS_STR_DUP (DS_D->mime_type);
D->size = DS_LVAL (DS_D->size);
D->dc_id = DS_LVAL (DS_D->dc_id);
tglf_fetch_photo_size (TLS, &D->thumb, DS_D->thumb);
if (DS_D->attributes) {
int i;
for (i = 0; i < DS_LVAL (DS_D->attributes->cnt); i++) {
tglf_fetch_document_attribute (TLS, D, DS_D->attributes->data[i]);
}
}
return D;
}
struct tgl_webpage *tglf_fetch_alloc_webpage (struct tgl_state *TLS, struct tl_ds_web_page *DS_W) {
if (!DS_W) { return NULL; }
struct tgl_webpage *W = tgl_webpage_get (TLS, DS_LVAL (DS_W->id));
if (W) {
W->refcnt ++;
} else {
W = talloc0 (sizeof (*W));
W->id = DS_LVAL (DS_W->id);
W->refcnt = 1;
tgl_webpage_insert (TLS, W);
}
if (!W->url) {
W->url = DS_STR_DUP (DS_W->url);
}
if (!W->display_url) {
W->display_url = DS_STR_DUP (DS_W->display_url);
}
if (!W->type) {
W->type = DS_STR_DUP (DS_W->type);