forked from vysheng/tgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.c
5453 lines (4722 loc) · 179 KB
/
queries.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
#define _FILE_OFFSET_BITS 64
#include <errno.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef WIN32
#include <sys/utsname.h>
#endif
#include "mtproto-client.h"
#include "queries.h"
#include "tree.h"
#include "mtproto-common.h"
//#include "telegram.h"
#include "tgl-structures.h"
//#include "interface.h"
//#include "net.h"
#include "crypto/bn.h"
#include "crypto/rand.h"
#include "crypto/aes.h"
#include "crypto/sha.h"
#include "crypto/md5.h"
#include "no-preview.h"
#include "tgl-binlog.h"
#include "updates.h"
#include "auto.h"
#include "auto/auto-types.h"
#include "auto/auto-skip.h"
#include "auto/auto-free-ds.h"
#include "auto/auto-fetch-ds.h"
#include "auto/auto-print-ds.h"
#include "tgl.h"
#include "tg-mime-types.h"
#include "mtproto-utils.h"
#include "tgl-methods-in.h"
#include "tgl-queries.h"
#ifndef EPROTO
// BSD doesn't define EPROTO, even though it is POSIX:
// https://lists.freebsd.org/pipermail/freebsd-standards/2003-June/000124.html
#define EPROTO EIO
#endif
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
static void out_peer_id (struct tgl_state *TLS, tgl_peer_id_t id);
static struct query_methods send_msgs_methods;
struct messages_send_extra {
int multi;
int count;
tgl_message_id_t id;
tgl_message_id_t *list;
};
#define QUERY_TIMEOUT 6.0
struct send_file {
int fd;
long long size;
long long offset;
int part_num;
int part_size;
long long id;
long long thumb_id;
tgl_peer_id_t to_id;
int flags;
char *file_name;
int encr;
//int avatar;
tgl_peer_id_t avatar;
int reply;
unsigned char *iv;
unsigned char *init_iv;
unsigned char *key;
int w;
int h;
int duration;
char *caption;
int channel;
};
#define memcmp8(a,b) memcmp ((a), (b), 8)
DEFINE_TREE (query, struct query *, memcmp8, 0) ;
static int mystreq1 (const char *a, const char *b, int l) {
if ((int)strlen (a) != l) { return 1; }
return memcmp (a, b, l);
}
/* {{{ COMMON */
struct query *tglq_query_get (struct tgl_state *TLS, long long id) {
return tree_lookup_query (TLS->queries_tree, (void *)&id);
}
static int alarm_query (struct tgl_state *TLS, struct query *q) {
assert (q);
vlogprintf (E_DEBUG - 2, "Alarm query %" INT64_PRINTF_MODIFIER "d (type '%s')\n", q->msg_id, q->methods->name);
TLS->timer_methods->insert (q->ev, q->methods->timeout ? q->methods->timeout : QUERY_TIMEOUT);
if (q->session && q->session_id && q->DC && q->DC->sessions[0] == q->session && q->session->session_id == q->session_id) {
clear_packet ();
out_int (CODE_msg_container);
out_int (1);
out_long (q->msg_id);
out_int (q->seq_no);
out_int (4 * q->data_len);
out_ints (q->data, q->data_len);
tglmp_encrypt_send_message (TLS, q->session->c, packet_buffer, packet_ptr - packet_buffer, q->flags & QUERY_FORCE_SEND);
} else {
q->flags &= ~QUERY_ACK_RECEIVED;
if (tree_lookup_query (TLS->queries_tree, q)) {
TLS->queries_tree = tree_delete_query (TLS->queries_tree, q);
}
q->session = q->DC->sessions[0];
long long old_id = q->msg_id;
q->msg_id = tglmp_encrypt_send_message (TLS, q->session->c, q->data, q->data_len, (q->flags & QUERY_FORCE_SEND) | 1);
vlogprintf (E_NOTICE, "Resent query #%" INT64_PRINTF_MODIFIER "d as #%" INT64_PRINTF_MODIFIER "d of size %d to DC %d\n", old_id, q->msg_id, 4 * q->data_len, q->DC->id);
TLS->queries_tree = tree_insert_query (TLS->queries_tree, q, rand ());
q->session_id = q->session->session_id;
if (!(q->session->dc->flags & 4) && !(q->flags & QUERY_FORCE_SEND)) {
q->session_id = 0;
}
}
return 0;
}
void tglq_regen_query (struct tgl_state *TLS, long long id) {
struct query *q = tglq_query_get (TLS, id);
if (!q) { return; }
q->flags &= ~QUERY_ACK_RECEIVED;
if (!(q->session && q->session_id && q->DC && q->DC->sessions[0] == q->session && q->session->session_id == q->session_id)) {
q->session_id = 0;
} else {
if (!(q->session->dc->flags & 4) && !(q->flags & QUERY_FORCE_SEND)) {
q->session_id = 0;
}
}
vlogprintf (E_NOTICE, "regen query %" INT64_PRINTF_MODIFIER "d\n", id);
TLS->timer_methods->insert (q->ev, 0.001);
}
struct regen_tmp_struct {
struct tgl_state *TLS;
struct tgl_dc *DC;
struct tgl_session *S;
};
void tglq_regen_query_from_old_session (struct query *q, void *ex) {
struct regen_tmp_struct *T = ex;
struct tgl_state *TLS = T->TLS;
if (q->DC == T->DC) {
if (!q->session || q->session_id != T->S->session_id || q->session != T->S) {
q->session_id = 0;
vlogprintf (E_NOTICE, "regen query from old session %" INT64_PRINTF_MODIFIER "d\n", q->msg_id);
TLS->timer_methods->insert (q->ev, q->methods->timeout ? 0.001 : 0.1);
}
}
}
void tglq_regen_queries_from_old_session (struct tgl_state *TLS, struct tgl_dc *DC, struct tgl_session *S) {
struct regen_tmp_struct T;
T.TLS = TLS;
T.DC = DC;
T.S = S;
tree_act_ex_query (TLS->queries_tree, tglq_regen_query_from_old_session, &T);
}
void tglq_query_restart (struct tgl_state *TLS, long long id) {
struct query *q = tglq_query_get (TLS, id);
if (q) {
vlogprintf (E_NOTICE, "restarting query %" INT64_PRINTF_MODIFIER "d\n", id);
TLS->timer_methods->remove (q->ev);
alarm_query (TLS, q);
}
}
static void alarm_query_gateway (struct tgl_state *TLS, void *arg) {
alarm_query (TLS, arg);
}
struct query *tglq_send_query_ex (struct tgl_state *TLS, struct tgl_dc *DC, int ints, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra, int flags) {
assert (DC);
assert (DC->auth_key_id);
if (!DC->sessions[0]) {
tglmp_dc_create_session (TLS, DC);
}
vlogprintf (E_DEBUG, "Sending query of size %d to DC %d\n", 4 * ints, DC->id);
struct query *q = talloc0 (sizeof (*q));
q->data_len = ints;
q->data = talloc (4 * ints);
memcpy (q->data, data, 4 * ints);
q->msg_id = tglmp_encrypt_send_message (TLS, DC->sessions[0]->c, data, ints, 1 | (flags & QUERY_FORCE_SEND));
q->session = DC->sessions[0];
q->seq_no = q->session->seq_no - 1;
q->session_id = q->session->session_id;
if (!(DC->flags & 4) && !(flags & QUERY_FORCE_SEND)) {
q->session_id = 0;
}
vlogprintf (E_DEBUG, "Msg_id is %" INT64_PRINTF_MODIFIER "d %p\n", q->msg_id, q);
vlogprintf (E_NOTICE, "Sent query #%" INT64_PRINTF_MODIFIER "d of size %d to DC %d\n", q->msg_id, 4 * ints, DC->id);
q->methods = methods;
q->type = methods->type;
q->DC = DC;
q->flags = flags & QUERY_FORCE_SEND;
if (TLS->queries_tree) {
vlogprintf (E_DEBUG + 2, "%" INT64_PRINTF_MODIFIER "d %" INT64_PRINTF_MODIFIER "d\n", q->msg_id, TLS->queries_tree->x->msg_id);
}
TLS->queries_tree = tree_insert_query (TLS->queries_tree, q, rand ());
q->ev = TLS->timer_methods->alloc (TLS, alarm_query_gateway, q);
TLS->timer_methods->insert (q->ev, q->methods->timeout ? q->methods->timeout : QUERY_TIMEOUT);
q->extra = extra;
q->callback = callback;
q->callback_extra = callback_extra;
TLS->active_queries ++;
return q;
}
struct query *tglq_send_query (struct tgl_state *TLS, struct tgl_dc *DC, int ints, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra) {
return tglq_send_query_ex (TLS, DC, ints, data, methods, extra, callback, callback_extra, 0);
}
static int fail_on_error (struct tgl_state *TLS, struct query *q, int error_code, int l, const char *error) {
vlogprintf (E_ERROR, "error #%d: %.*s\n", error_code, l, error);
assert (0);
return 0;
}
void tglq_query_ack (struct tgl_state *TLS, long long id) {
struct query *q = tglq_query_get (TLS, id);
if (q && !(q->flags & QUERY_ACK_RECEIVED)) {
assert (q->msg_id == id);
q->flags |= QUERY_ACK_RECEIVED;
TLS->timer_methods->remove (q->ev);
}
}
void tglq_query_delete (struct tgl_state *TLS, long long id) {
struct query *q = tglq_query_get (TLS, id);
if (!q) {
return;
}
if (!(q->flags & QUERY_ACK_RECEIVED)) {
TLS->timer_methods->remove (q->ev);
}
TLS->queries_tree = tree_delete_query (TLS->queries_tree, q);
tfree (q->data, q->data_len * 4);
TLS->timer_methods->free (q->ev);
TLS->active_queries --;
}
static void resend_query_cb (struct tgl_state *TLS, void *_q, int success);
void tglq_free_query (struct query *q, void *extra) {
struct tgl_state *TLS = extra;
if (!(q->flags & QUERY_ACK_RECEIVED)) {
TLS->timer_methods->remove (q->ev);
}
tfree (q->data, q->data_len * 4);
TLS->timer_methods->free (q->ev);
}
void tglq_query_free_all (struct tgl_state *TLS) {
tree_act_ex_query (TLS->queries_tree, tglq_free_query, TLS);
TLS->queries_tree = tree_clear_query (TLS->queries_tree);
}
int tglq_query_error (struct tgl_state *TLS, long long id) {
assert (fetch_int () == CODE_rpc_error);
int error_code = fetch_int ();
int error_len = prefetch_strlen ();
char *error = fetch_str (error_len);
struct query *q = tglq_query_get (TLS, id);
if (!q) {
vlogprintf (E_WARNING, "error for query '%s' #%" INT64_PRINTF_MODIFIER "d: #%d :%.*s\n", q->methods->name, id, error_code, error_len, error);
vlogprintf (E_WARNING, "No such query\n");
} else {
if (!(q->flags & QUERY_ACK_RECEIVED)) {
TLS->timer_methods->remove (q->ev);
}
TLS->queries_tree = tree_delete_query (TLS->queries_tree, q);
int res = 0;
int error_handled = 0;
switch (error_code) {
case 303:
// migrate
{
int offset = -1;
if (error_len >= 15 && !memcmp (error, "PHONE_MIGRATE_", 14)) {
offset = 14;
}
if (error_len >= 17 && !memcmp (error, "NETWORK_MIGRATE_", 16)) {
offset = 16;
}
if (error_len >= 14 && !memcmp (error, "USER_MIGRATE_", 13)) {
offset = 13;
}
if (offset >= 0) {
int i = 0;
while (offset < error_len && error[offset] >= '0' && error[offset] <= '9') {
i = i * 10 + error[offset] - '0';
offset ++;
}
if (i > 0 && i < TGL_MAX_DC_NUM) {
bl_do_set_working_dc (TLS, i);
q->flags &= ~QUERY_ACK_RECEIVED;
//q->session_id = 0;
//struct tgl_dc *DC = q->DC;
//if (!(DC->flags & 4) && !(q->flags & QUERY_FORCE_SEND)) {
q->session_id = 0;
//}
q->DC = TLS->DC_working;
TLS->timer_methods->insert (q->ev, 0);
error_handled = 1;
res = 1;
}
}
}
break;
case 400:
// nothing to handle
// bad user input probably
break;
case 401:
if (!mystreq1 ("SESSION_PASSWORD_NEEDED", error, error_len)) {
if (!(TLS->locks & TGL_LOCK_PASSWORD)) {
TLS->locks |= TGL_LOCK_PASSWORD;
tgl_do_check_password (TLS, resend_query_cb, q);
}
res = 1;
error_handled = 1;
}
break;
case 403:
// privacy violation
break;
case 404:
// not found
break;
case 420:
// flood
case 500:
// internal error
default:
// anything else. Treated as internal error
{
int wait;
if (strncmp (error, "FLOOD_WAIT_", 11)) {
if (error_code == 420) {
vlogprintf (E_ERROR, "error = '%s'\n", error);
}
wait = 10;
} else {
wait = atoll (error + 11);
}
q->flags &= ~QUERY_ACK_RECEIVED;
TLS->timer_methods->insert (q->ev, wait);
struct tgl_dc *DC = q->DC;
if (!(DC->flags & 4) && !(q->flags & QUERY_FORCE_SEND)) {
q->session_id = 0;
}
error_handled = 1;
}
break;
}
if (error_handled) {
vlogprintf (E_DEBUG - 2, "error for query #%" INT64_PRINTF_MODIFIER "d: #%d %.*s (HANDLED)\n", id, error_code, error_len, error);
} else {
vlogprintf (E_WARNING, "error for query '%s' #%" INT64_PRINTF_MODIFIER "d: #%d %.*s\n", q->methods->name, id, error_code, error_len, error);
if (q->methods && q->methods->on_error) {
res = q->methods->on_error (TLS, q, error_code, error_len, error);
}
}
if (res <= 0) {
tfree (q->data, q->data_len * 4);
TLS->timer_methods->free (q->ev);
}
if (res == -11) {
TLS->active_queries --;
return -1;
}
}
TLS->active_queries --;
return 0;
}
#define MAX_PACKED_SIZE (1 << 24)
static int packed_buffer[MAX_PACKED_SIZE / 4];
int tglq_query_result (struct tgl_state *TLS, long long id) {
vlogprintf (E_DEBUG, "result for query #%" INT64_PRINTF_MODIFIER "d. Size %ld bytes\n", id, (long)4 * (in_end - in_ptr));
int op = prefetch_int ();
int *end = 0;
int *eend = 0;
if (op == CODE_gzip_packed) {
fetch_int ();
int l = prefetch_strlen ();
char *s = fetch_str (l);
int total_out = tgl_inflate (s, l, packed_buffer, MAX_PACKED_SIZE);
vlogprintf (E_DEBUG, "inflated %d bytes\n", total_out);
end = in_ptr;
eend = in_end;
in_ptr = packed_buffer;
in_end = in_ptr + total_out / 4;
}
struct query *q = tglq_query_get (TLS, id);
if (!q) {
vlogprintf (E_WARNING, "No such query\n");
in_ptr = in_end;
} else {
if (!(q->flags & QUERY_ACK_RECEIVED)) {
TLS->timer_methods->remove (q->ev);
}
TLS->queries_tree = tree_delete_query (TLS->queries_tree, q);
if (q->methods && q->methods->on_answer) {
assert (q->type);
int *save = in_ptr;
vlogprintf (E_DEBUG, "in_ptr = %p, end_ptr = %p\n", in_ptr, in_end);
if (skip_type_any (q->type) < 0) {
vlogprintf (E_ERROR, "Skipped %ld int out of %ld (type %s) (query type %s)\n", (long)(in_ptr - save), (long)(in_end - save), q->type->type->id, q->methods->name);
vlogprintf (E_ERROR, "0x%08x 0x%08x 0x%08x 0x%08x\n", *(save - 1), *(save), *(save + 1), *(save + 2));
assert (0);
}
assert (in_ptr == in_end);
in_ptr = save;
void *DS = fetch_ds_type_any (q->type);
assert (DS);
q->methods->on_answer (TLS, q, DS);
free_ds_type_any (DS, q->type);
assert (in_ptr == in_end);
}
tfree (q->data, 4 * q->data_len);
TLS->timer_methods->free (q->ev);
tfree (q, sizeof (*q));
}
if (end) {
in_ptr = end;
in_end = eend;
}
TLS->active_queries --;
return 0;
}
static void out_random (int n) {
assert (n <= 32);
static char buf[32];
tglt_secure_random (buf, n);
out_cstring (buf, n);
}
int allow_send_linux_version;
void tgl_do_insert_header (struct tgl_state *TLS) {
out_int (CODE_invoke_with_layer);
out_int (TGL_SCHEME_LAYER);
out_int (CODE_init_connection);
out_int (TLS->app_id);
#ifndef WIN32
if (allow_send_linux_version) {
struct utsname st;
uname (&st);
out_string (st.machine);
static char buf[4096];
tsnprintf (buf, sizeof (buf) - 1, "%.999s %.999s %.999s", st.sysname, st.release, st.version);
out_string (buf);
tsnprintf (buf, sizeof (buf) - 1, "%s (TGL %s)", TLS->app_version, TGL_VERSION);
out_string (buf);
out_string ("En");
} else {
out_string ("x86");
out_string ("Linux");
static char buf[4096];
tsnprintf (buf, sizeof (buf) - 1, "%s (TGL %s)", TLS->app_version, TGL_VERSION);
out_string (buf);
out_string ("en");
}
#else
out_string ("x86");
out_string ("Windows");
static char buf[4096];
tsnprintf (buf, sizeof (buf) - 1, "%s (TGL %s)", TLS->app_version, TGL_VERSION);
out_string (buf);
out_string ("en");
#endif
}
void tgl_set_query_error (struct tgl_state *TLS, int error_code, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
void tgl_set_query_error (struct tgl_state *TLS, int error_code, const char *format, ...) {
static char s[1001];
va_list ap;
va_start (ap, format);
vsnprintf (s, 1000, format, ap);
va_end (ap);
if (TLS->error) {
tfree_str (TLS->error);
}
TLS->error = tstrdup (s);
TLS->error_code = error_code;
}
/* }}} */
/* {{{ Default on error */
static int q_void_on_error (struct tgl_state *TLS, struct query *q, int error_code, int error_len, const char *error) {
tgl_set_query_error (TLS, EPROTO, "RPC_CALL_FAIL %d: %.*s", error_code, error_len, error);
if (q->callback) {
((void (*)(struct tgl_state *,void *, int))(q->callback))(TLS, q->callback_extra, 0);
}
return 0;
}
static int q_ptr_on_error (struct tgl_state *TLS, struct query *q, int error_code, int error_len, const char *error) {
tgl_set_query_error (TLS, EPROTO, "RPC_CALL_FAIL %d: %.*s", error_code, error_len, error);
if (q->callback) {
((void (*)(struct tgl_state *,void *, int, void *))(q->callback))(TLS, q->callback_extra, 0, NULL);
}
return 0;
}
static int q_list_on_error (struct tgl_state *TLS, struct query *q, int error_code, int error_len, const char *error) {
tgl_set_query_error (TLS, EPROTO, "RPC_CALL_FAIL %d: %.*s", error_code, error_len, error);
if (q->callback) {
((void (*)(struct tgl_state *,void *, int, int, void *))(q->callback))(TLS, q->callback_extra, 0, 0, NULL);
}
return 0;
}
/* }}} */
#include "queries-encrypted.c"
static void increase_ent (int *ent_size, int **ent, int s) {
*ent = trealloc (*ent, (*ent_size) * 4, (*ent_size) * 4 + 4 * s);
(*ent_size) +=s;
}
int utf8_len (const char *s, int len) {
int i;
int r = 0;
for (i = 0; i < len; i++) {
if ((s[i] & 0xc0) != 0x80) {
r ++;
}
}
return r;
}
static char *process_html_text (struct tgl_state *TLS, const char *text, int text_len, int *ent_size, int **ent) {
char *new_text = talloc (2 * text_len + 1);
int stpos[100];
int sttype[100];
int stp = 0;
int p;
int cur_p = 0;
*ent = talloc (8);
*ent_size = 2;
(*ent)[0] = CODE_vector;
(*ent)[1] = 0;
int total = 0;
for (p = 0; p < text_len; p++) {
assert (cur_p <= 2 * text_len);
if (text[p] == '<') {
if (stp == 99) {
tgl_set_query_error (TLS, EINVAL, "Too nested tags...");
tfree (new_text, 2 * text_len + 1);
return NULL;
}
int old_p = *ent_size;
if (text_len - p >= 3 && !memcmp (text + p, "<b>", 3)) {
increase_ent (ent_size, ent, 3);
total ++;
(*ent)[old_p] = CODE_message_entity_bold;
(*ent)[old_p + 1] = utf8_len (new_text, cur_p);
stpos[stp] = old_p + 2;
sttype[stp] = 0;
stp ++;
p += 2;
continue;
}
if (text_len - p >= 4 && !memcmp (text + p, "</b>", 4)) {
if (stp == 0 || sttype[stp - 1] != 0) {
tgl_set_query_error (TLS, EINVAL, "Invalid tag nest");
tfree (new_text, 2 * text_len + 1);
return NULL;
}
(*ent)[stpos[stp - 1]] = utf8_len (new_text, cur_p) - (*ent)[stpos[stp - 1] - 1];
stp --;
p += 3;
continue;
}
if (text_len - p >= 3 && !memcmp (text + p, "<i>", 3)) {
increase_ent (ent_size, ent, 3);
total ++;
(*ent)[old_p] = CODE_message_entity_italic;
(*ent)[old_p + 1] = utf8_len (new_text, cur_p);
stpos[stp] = old_p + 2;
sttype[stp] = 1;
stp ++;
p += 2;
continue;
}
if (text_len - p >= 4 && !memcmp (text + p, "</i>", 4)) {
if (stp == 0 || sttype[stp - 1] != 1) {
tgl_set_query_error (TLS, EINVAL, "Invalid tag nest");
tfree (new_text, 2 * text_len + 1);
return NULL;
}
(*ent)[stpos[stp - 1]] = utf8_len (new_text, cur_p) - (*ent)[stpos[stp - 1] - 1];
stp --;
p += 3;
continue;
}
if (text_len - p >= 6 && !memcmp (text + p, "<code>", 6)) {
increase_ent (ent_size, ent, 3);
total ++;
(*ent)[old_p] = CODE_message_entity_code;
(*ent)[old_p + 1] = utf8_len (new_text, cur_p);
stpos[stp] = old_p + 2;
sttype[stp] = 2;
stp ++;
p += 5;
continue;
}
if (text_len - p >= 7 && !memcmp (text + p, "</code>", 7)) {
if (stp == 0 || sttype[stp - 1] != 2) {
tgl_set_query_error (TLS, EINVAL, "Invalid tag nest");
tfree (new_text, 2 * text_len + 1);
return NULL;
}
(*ent)[stpos[stp - 1]] = utf8_len (new_text, cur_p) - (*ent)[stpos[stp - 1] - 1];
stp --;
p += 6;
continue;
}
if (text_len - p >= 9 && !memcmp (text + p, "<a href=\"", 9)) {
int pp = p + 9;
while (pp < text_len && text[pp] != '"') {
pp ++;
}
if (pp == text_len || pp == text_len - 1 || text[pp + 1] != '>') {
tgl_set_query_error (TLS, EINVAL, "<a> tag did not close");
tfree (new_text, 2 * text_len + 1);
return NULL;
}
int len = pp - p - 9;
assert (len >= 0);
if (len >= 250) {
tgl_set_query_error (TLS, EINVAL, "too long link");
tfree (new_text, 2 * text_len + 1);
return NULL;
}
increase_ent (ent_size, ent, 3 + (len + 1 + ((-len-1) & 3)) / 4);
total ++;
(*ent)[old_p] = CODE_message_entity_text_url;
(*ent)[old_p + 1] = utf8_len (new_text, cur_p);
stpos[stp] = old_p + 2;
sttype[stp] = 3;
stp ++;
unsigned char *r = (void *)((*ent) + old_p + 3);
r[0] = len;
memcpy (r + 1, text + p + 9, len);
memset (r + 1 + len, 0, (-len-1) & 3);
p = pp + 1;
continue;
}
if (text_len - p >= 4 && !memcmp (text + p, "</a>", 4)) {
if (stp == 0 || sttype[stp - 1] != 3) {
tgl_set_query_error (TLS, EINVAL, "Invalid tag nest");
tfree (new_text, 2 * text_len + 1);
return NULL;
}
(*ent)[stpos[stp - 1]] = utf8_len (new_text, cur_p) - (*ent)[stpos[stp - 1] - 1];
stp --;
p += 3;
continue;
}
if (text_len - p >= 4 && !memcmp (text + p, "<br>", 4)) {
new_text[cur_p ++] = '\n';
p += 3;
continue;
}
tgl_set_query_error (TLS, EINVAL, "Unknown tag");
tfree (new_text, 2 * text_len + 1);
return NULL;
} else if (text_len - p >= 4 && !memcmp (text + p, ">", 4)) {
p += 3;
new_text[cur_p ++] = '>';
} else if (text_len - p >= 4 && !memcmp (text + p, "<", 4)) {
p += 3;
new_text[cur_p ++] = '<';
} else if (text_len - p >= 5 && !memcmp (text + p, "&", 5)) {
p += 4;
new_text[cur_p ++] = '&';
} else if (text_len - p >= 6 && !memcmp (text + p, " ", 6)) {
p += 5;
new_text[cur_p ++] = 0xc2;
new_text[cur_p ++] = 0xa0;
} else if (text_len - p >= 3 && text[p] == '&' && text[p + 1] == '#') {
p += 2;
int num = 0;
int ok = 0;
while (p < text_len) {
if (text[p] >= '0' && text[p] <= '9') {
num = num * 10 + text[p] - '0';
if (num >= 127) {
tgl_set_query_error (TLS, EINVAL, "Too big number in &-sequence");
tfree (new_text, text_len + 1);
return NULL;
}
p ++;
} else if (text[p] == ';') {
new_text[cur_p ++] = num;
ok = 1;
break;
} else {
tgl_set_query_error (TLS, EINVAL, "Bad &-sequence");
tfree (new_text, text_len + 1);
return NULL;
}
}
if (ok) { continue; }
tgl_set_query_error (TLS, EINVAL, "Unterminated &-sequence");
tfree (new_text, text_len + 1);
return NULL;
} else {
new_text[cur_p ++] = text[p];
}
}
if (stp != 0) {
tgl_set_query_error (TLS, EINVAL, "Invalid tag nest");
tfree (new_text, text_len + 1);
return NULL;
}
(*ent)[1] = total;
char *n = talloc (cur_p + 1);
memcpy (n, new_text, cur_p);
n[cur_p] = 0;
tfree (new_text, 2 * text_len + 1);
return n;
}
/* {{{ Get config */
static void fetch_dc_option (struct tgl_state *TLS, struct tl_ds_dc_option *DS_DO) {
bl_do_dc_option (TLS, DS_LVAL (DS_DO->flags), DS_LVAL (DS_DO->id), NULL, 0, DS_STR (DS_DO->ip_address), DS_LVAL (DS_DO->port));
}
static int help_get_config_on_answer (struct tgl_state *TLS, struct query *q, void *DS) {
struct tl_ds_config *DS_C = DS;
int i;
for (i = 0; i < DS_LVAL (DS_C->dc_options->cnt); i++) {
fetch_dc_option (TLS, DS_C->dc_options->data[i]);
}
int max_chat_size = DS_LVAL (DS_C->chat_size_max);
int max_bcast_size = 0;//DS_LVAL (DS_C->broadcast_size_max);
vlogprintf (E_DEBUG, "chat_size = %d, bcast_size = %d\n", max_chat_size, max_bcast_size);
if (q->callback) {
((void (*)(struct tgl_state *,void *, int))(q->callback))(TLS, q->callback_extra, 1);
}
return 0;
}
static struct query_methods help_get_config_methods = {
.on_answer = help_get_config_on_answer,
.on_error = q_void_on_error,
.type = TYPE_TO_PARAM(config),
.name = "get config",
.timeout = 1
};
void tgl_do_help_get_config (struct tgl_state *TLS, void (*callback)(struct tgl_state *,void *, int), void *callback_extra) {
clear_packet ();
tgl_do_insert_header (TLS);
out_int (CODE_help_get_config);
tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &help_get_config_methods, 0, callback, callback_extra);
}
void tgl_do_help_get_config_dc (struct tgl_state *TLS, struct tgl_dc *D, void (*callback)(struct tgl_state *, void *, int), void *callback_extra) {
clear_packet ();
tgl_do_insert_header (TLS);
out_int (CODE_help_get_config);
tglq_send_query_ex (TLS, D, packet_ptr - packet_buffer, packet_buffer, &help_get_config_methods, 0, callback, callback_extra, 2);
}
/* }}} */
/* {{{ Send code */
static int send_code_on_answer (struct tgl_state *TLS, struct query *q, void *D) {
struct tl_ds_auth_sent_code *DS_ASC = D;
char *phone_code_hash = DS_STR_DUP (DS_ASC->phone_code_hash);
int registered = DS_BVAL (DS_ASC->phone_registered);;
if (q->callback) {
((void (*)(struct tgl_state *, void *, int, int, const char *))(q->callback)) (TLS, q->callback_extra, 1, registered, phone_code_hash);
}
tfree_str (phone_code_hash);
return 0;
}
static struct query_methods send_code_methods = {
.on_answer = send_code_on_answer,
.on_error = q_list_on_error,
.type = TYPE_TO_PARAM(auth_sent_code),
.name = "send code"
};
void tgl_do_send_code (struct tgl_state *TLS, const char *phone, int phone_len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int registered, const char *hash), void *callback_extra) {
vlogprintf (E_DEBUG, "sending code to dc %d\n", TLS->dc_working_num);
clear_packet ();
tgl_do_insert_header (TLS);
out_int (CODE_auth_send_code);
out_cstring (phone, phone_len);
out_int (0);
out_int (TLS->app_id);
out_string (TLS->app_hash);
out_string ("en");
tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &send_code_methods, NULL, callback, callback_extra);
}
static int phone_call_on_answer (struct tgl_state *TLS, struct query *q, void *D) {
if (q->callback) {
((void (*)(struct tgl_state *TLS, void *, int))(q->callback))(TLS, q->callback_extra, 1);
}
return 0;
}
static struct query_methods phone_call_methods = {
.on_answer = phone_call_on_answer,
.on_error = q_void_on_error,
.type = TYPE_TO_PARAM(bool),
.name = "phone call"
};
void tgl_do_phone_call (struct tgl_state *TLS, const char *phone, int phone_len, const char *hash, int hash_len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra) {
vlogprintf (E_DEBUG, "calling user\n");
clear_packet ();
tgl_do_insert_header (TLS);
out_int (CODE_auth_send_call);
out_cstring (phone, phone_len);
out_cstring (hash, hash_len);
tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &phone_call_methods, NULL, callback, callback_extra);
}
/* }}} */
/* {{{ Sign in / Sign up */
static int sign_in_on_answer (struct tgl_state *TLS, struct query *q, void *D) {
struct tl_ds_auth_authorization *DS_AA = D;
//vlogprintf (E_DEBUG, "Expires in %d\n", DS_LVAL (DS_AA->expires));
struct tgl_user *U = tglf_fetch_alloc_user (TLS, DS_AA->user);
bl_do_dc_signed (TLS, TLS->DC_working->id);
if (q->callback) {
((void (*)(struct tgl_state *TLS, void *, int, struct tgl_user *))q->callback) (TLS, q->callback_extra, 1, U);
}
return 0;
}
static int sign_in_on_error (struct tgl_state *TLS, struct query *q, int error_code, int error_len, const char *error) {
tgl_set_query_error (TLS, EPROTO, "RPC_CALL_FAIL %d: %.*s", error_code, error_len, error);
if (q->callback) {
((void (*)(struct tgl_state *, void *, int, struct tgl_user *))q->callback) (TLS, q->callback_extra, 0, NULL);
}
return 0;
}
static struct query_methods sign_in_methods = {
.on_answer = sign_in_on_answer,
.on_error = sign_in_on_error,
.type = TYPE_TO_PARAM(auth_authorization),
.name = "sign in"
};
int tgl_do_send_code_result (struct tgl_state *TLS, const char *phone, int phone_len, const char *hash, int hash_len, const char *code, int code_len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) {
clear_packet ();
out_int (CODE_auth_sign_in);
out_cstring (phone, phone_len);
out_cstring (hash, hash_len);
out_cstring (code, code_len);
tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra);
return 0;
}
int tgl_do_send_code_result_auth (struct tgl_state *TLS, const char *phone, int phone_len, const char *hash, int hash_len, const char *code, int code_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) {
clear_packet ();
out_int (CODE_auth_sign_up);
out_cstring (phone, phone_len);
out_cstring (hash, hash_len);
out_cstring (code, code_len);
out_cstring (first_name, first_name_len);
out_cstring (last_name, last_name_len);
tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra);
return 0;
}
int tgl_do_send_bot_auth (struct tgl_state *TLS, const char *code, int code_len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) {
clear_packet ();
out_int (CODE_auth_import_bot_authorization);
out_int (0);
out_int (TLS->app_id);
out_string (TLS->app_hash);
out_cstring (code, code_len);
tglq_send_query (TLS, TLS->DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra);
return 0;
}
/* }}} */
/* {{{ Get contacts */
static int get_contacts_on_answer (struct tgl_state *TLS, struct query *q, void *D) {
struct tl_ds_contacts_contacts *DS_CC = D;
int n = DS_CC->users ? DS_LVAL (DS_CC->users->cnt) : 0;
int i;
struct tgl_user **list = talloc (sizeof (void *) * n);
for (i = 0; i < n; i++) {
list[i] = tglf_fetch_alloc_user (TLS, DS_CC->users->data[i]);
}
if (q->callback) {
((void (*)(struct tgl_state *TLS, void *, int, int, struct tgl_user **))q->callback) (TLS, q->callback_extra, 1, n, list);
}
tfree (list, sizeof (void *) * n);
return 0;
}
static struct query_methods get_contacts_methods = {
.on_answer = get_contacts_on_answer,
.on_error = q_list_on_error,
.type = TYPE_TO_PARAM(contacts_contacts),
.name = "get contacts"
};
void tgl_do_update_contact_list (struct tgl_state *TLS, void (*callback) (struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_user *contacts[]), void *callback_extra) {