forked from vysheng/tg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.c
2811 lines (2538 loc) · 72.7 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 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
#define _FILE_OFFSET_BITS 64
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/utsname.h>
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
#include "include.h"
#include "mtproto-client.h"
#include "queries.h"
#include "tree.h"
#include "mtproto-common.h"
#include "telegram.h"
#include "loop.h"
#include "structures.h"
#include "interface.h"
#include "net.h"
#include <openssl/bn.h>
#include <openssl/rand.h>
#include <openssl/aes.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
#include "no-preview.h"
#include "binlog.h"
#define sha1 SHA1
#ifdef __APPLE__
#define OPEN_BIN "open %s"
#else
#define OPEN_BIN "xdg-open %s"
#endif
char *get_downloads_directory (void);
int verbosity;
extern int offline_mode;
long long cur_uploading_bytes;
long long cur_uploaded_bytes;
long long cur_downloading_bytes;
long long cur_downloaded_bytes;
extern int binlog_enabled;
extern int sync_from_start;
int queries_num;
void out_peer_id (peer_id_t id);
#define QUERY_TIMEOUT 6.0
#define memcmp8(a,b) memcmp ((a), (b), 8)
DEFINE_TREE (query, struct query *, memcmp8, 0) ;
struct tree_query *queries_tree;
double get_double_time (void) {
struct timespec tv;
my_clock_gettime (CLOCK_REALTIME, &tv);
return tv.tv_sec + 1e-9 * tv.tv_nsec;
}
struct query *query_get (long long id) {
return tree_lookup_query (queries_tree, (void *)&id);
}
int alarm_query (struct query *q) {
assert (q);
if (verbosity >= 1) {
logprintf ("Alarm query %lld\n", q->msg_id);
}
q->ev.timeout = get_double_time () + QUERY_TIMEOUT;
insert_event_timer (&q->ev);
if (q->session->c->out_bytes >= 100000) {
return 0;
}
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);
encrypt_send_message (q->session->c, packet_buffer, packet_ptr - packet_buffer, 0);
return 0;
}
void query_restart (long long id) {
struct query *q = query_get (id);
if (q) {
remove_event_timer (&q->ev);
alarm_query (q);
}
}
struct query *send_query (struct dc *DC, int ints, void *data, struct query_methods *methods, void *extra) {
assert (DC);
assert (DC->auth_key_id);
if (!DC->sessions[0]) {
dc_create_session (DC);
}
if (verbosity) {
logprintf ( "Sending query of size %d to DC (%s:%d)\n", 4 * ints, DC->ip, DC->port);
}
struct query *q = talloc0 (sizeof (*q));
q->data_len = ints;
q->data = talloc (4 * ints);
memcpy (q->data, data, 4 * ints);
q->msg_id = encrypt_send_message (DC->sessions[0]->c, data, ints, 1);
q->session = DC->sessions[0];
q->seq_no = DC->sessions[0]->seq_no - 1;
if (verbosity) {
logprintf ( "Msg_id is %lld %p\n", q->msg_id, q);
}
q->methods = methods;
q->DC = DC;
if (queries_tree) {
if (verbosity >= 2) {
logprintf ( "%lld %lld\n", q->msg_id, queries_tree->x->msg_id);
}
}
queries_tree = tree_insert_query (queries_tree, q, lrand48 ());
q->ev.alarm = (void *)alarm_query;
q->ev.timeout = get_double_time () + QUERY_TIMEOUT;
q->ev.self = (void *)q;
insert_event_timer (&q->ev);
q->extra = extra;
queries_num ++;
return q;
}
void query_ack (long long id) {
struct query *q = query_get (id);
if (q && !(q->flags & QUERY_ACK_RECEIVED)) {
assert (q->msg_id == id);
q->flags |= QUERY_ACK_RECEIVED;
remove_event_timer (&q->ev);
}
}
void query_error (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);
if (verbosity) {
logprintf ( "error for query #%lld: #%d :%.*s\n", id, error_code, error_len, error);
}
struct query *q = query_get (id);
if (!q) {
if (verbosity) {
logprintf ( "No such query\n");
}
} else {
if (!(q->flags & QUERY_ACK_RECEIVED)) {
remove_event_timer (&q->ev);
}
queries_tree = tree_delete_query (queries_tree, q);
if (q->methods && q->methods->on_error) {
q->methods->on_error (q, error_code, error_len, error);
} else {
logprintf ( "error for query #%lld: #%d :%.*s\n", id, error_code, error_len, error);
}
tfree (q->data, q->data_len * 4);
tfree (q, sizeof (*q));
}
queries_num --;
}
#define MAX_PACKED_SIZE (1 << 24)
static int packed_buffer[MAX_PACKED_SIZE / 4];
void query_result (long long id UU) {
if (verbosity) {
logprintf ( "result for query #%lld\n", id);
}
if (verbosity >= 4) {
logprintf ( "result: ");
hexdump_in ();
}
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 = tinflate (s, l, packed_buffer, MAX_PACKED_SIZE);
end = in_ptr;
eend = in_end;
//assert (total_out % 4 == 0);
in_ptr = packed_buffer;
in_end = in_ptr + total_out / 4;
if (verbosity >= 4) {
logprintf ( "Unzipped data: ");
hexdump_in ();
}
}
struct query *q = query_get (id);
if (!q) {
if (verbosity) {
logprintf ( "No such query\n");
}
in_ptr = in_end;
} else {
if (!(q->flags & QUERY_ACK_RECEIVED)) {
remove_event_timer (&q->ev);
}
queries_tree = tree_delete_query (queries_tree, q);
if (q->methods && q->methods->on_answer) {
q->methods->on_answer (q);
assert (in_ptr == in_end);
}
tfree (q->data, 4 * q->data_len);
tfree (q, sizeof (*q));
}
if (end) {
in_ptr = end;
in_end = eend;
}
queries_num --;
}
#define event_timer_cmp(a,b) ((a)->timeout > (b)->timeout ? 1 : ((a)->timeout < (b)->timeout ? -1 : (memcmp (a, b, sizeof (struct event_timer)))))
DEFINE_TREE (timer, struct event_timer *, event_timer_cmp, 0)
struct tree_timer *timer_tree;
void insert_event_timer (struct event_timer *ev) {
if (verbosity > 2) {
logprintf ( "INSERT: %lf %p %p\n", ev->timeout, ev->self, ev->alarm);
}
timer_tree = tree_insert_timer (timer_tree, ev, lrand48 ());
}
void remove_event_timer (struct event_timer *ev) {
if (verbosity > 2) {
logprintf ( "REMOVE: %lf %p %p\n", ev->timeout, ev->self, ev->alarm);
}
timer_tree = tree_delete_timer (timer_tree, ev);
}
double next_timer_in (void) {
if (!timer_tree) { return 1e100; }
return tree_get_min_timer (timer_tree)->timeout;
}
void work_timers (void) {
double t = get_double_time ();
while (timer_tree) {
struct event_timer *ev = tree_get_min_timer (timer_tree);
assert (ev);
if (ev->timeout > t) { break; }
remove_event_timer (ev);
assert (ev->alarm);
if (verbosity) {
logprintf ("Alarm\n");
}
ev->alarm (ev->self);
}
}
int max_chat_size;
int want_dc_num;
int new_dc_num;
extern struct dc *DC_list[];
extern struct dc *DC_working;
void out_random (int n) {
assert (n <= 32);
static char buf[32];
secure_random (buf, n);
out_cstring (buf, n);
}
int allow_send_linux_version;
void do_insert_header (void) {
out_int (CODE_invoke_with_layer11);
out_int (CODE_init_connection);
out_int (TG_APP_ID);
if (allow_send_linux_version) {
struct utsname st;
uname (&st);
out_string (st.machine);
static char buf[4096];
tsnprintf (buf, sizeof (buf), "%.999s %.999s %.999s\n", st.sysname, st.release, st.version);
out_string (buf);
out_string (TG_VERSION " (build " TG_BUILD ")");
out_string ("En");
} else {
out_string ("x86");
out_string ("Linux");
out_string (TG_VERSION);
out_string ("en");
}
}
/* {{{ Get config */
void fetch_dc_option (void) {
assert (fetch_int () == CODE_dc_option);
int id = fetch_int ();
int l1 = prefetch_strlen ();
char *name = fetch_str (l1);
int l2 = prefetch_strlen ();
char *ip = fetch_str (l2);
int port = fetch_int ();
if (verbosity) {
logprintf ( "id = %d, name = %.*s ip = %.*s port = %d\n", id, l1, name, l2, ip, port);
}
bl_do_dc_option (id, l1, name, l2, ip, port);
}
int help_get_config_on_answer (struct query *q UU) {
assert (fetch_int () == CODE_config);
fetch_int ();
unsigned test_mode = fetch_int ();
assert (test_mode == CODE_bool_true || test_mode == CODE_bool_false);
assert (test_mode == CODE_bool_false || test_mode == CODE_bool_true);
int this_dc = fetch_int ();
if (verbosity) {
logprintf ( "this_dc = %d\n", this_dc);
}
assert (fetch_int () == CODE_vector);
int n = fetch_int ();
assert (n <= 10);
int i;
for (i = 0; i < n; i++) {
fetch_dc_option ();
}
max_chat_size = fetch_int ();
if (verbosity >= 2) {
logprintf ( "chat_size = %d\n", max_chat_size);
}
return 0;
}
struct query_methods help_get_config_methods = {
.on_answer = help_get_config_on_answer
};
void do_help_get_config (void) {
clear_packet ();
out_int (CODE_help_get_config);
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &help_get_config_methods, 0);
}
/* }}} */
/* {{{ Send code */
char *phone_code_hash;
int send_code_on_answer (struct query *q UU) {
assert (fetch_int () == CODE_auth_sent_code);
fetch_bool ();
int l = prefetch_strlen ();
char *s = fetch_str (l);
if (phone_code_hash) {
tfree_str (phone_code_hash);
}
phone_code_hash = tstrndup (s, l);
want_dc_num = -1;
return 0;
}
int send_code_on_error (struct query *q UU, int error_code, int l, char *error) {
int s = strlen ("PHONE_MIGRATE_");
int s2 = strlen ("NETWORK_MIGRATE_");
if (l >= s && !memcmp (error, "PHONE_MIGRATE_", s)) {
int i = error[s] - '0';
want_dc_num = i;
} else if (l >= s2 && !memcmp (error, "NETWORK_MIGRATE_", s2)) {
int i = error[s2] - '0';
want_dc_num = i;
} else {
logprintf ( "error_code = %d, error = %.*s\n", error_code, l, error);
assert (0);
}
return 0;
}
struct query_methods send_code_methods = {
.on_answer = send_code_on_answer,
.on_error = send_code_on_error
};
int code_is_sent (void) {
return want_dc_num;
}
int config_got (void) {
return DC_list[want_dc_num] != 0;
}
char *suser;
extern int dc_working_num;
void do_send_code (const char *user) {
logprintf ("sending code\n");
suser = tstrdup (user);
want_dc_num = 0;
clear_packet ();
do_insert_header ();
out_int (CODE_auth_send_code);
out_string (user);
out_int (0);
out_int (TG_APP_ID);
out_string (TG_APP_HASH);
out_string ("en");
logprintf ("send_code: dc_num = %d\n", dc_working_num);
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_code_methods, 0);
net_loop (0, code_is_sent);
if (want_dc_num == -1) { return; }
DC_working = DC_list[want_dc_num];
if (!DC_working->sessions[0]) {
dc_create_session (DC_working);
}
dc_working_num = want_dc_num;
bl_do_set_working_dc (dc_working_num);
logprintf ("send_code: dc_num = %d\n", dc_working_num);
want_dc_num = 0;
clear_packet ();
do_insert_header ();
out_int (CODE_auth_send_code);
out_string (user);
out_int (0);
out_int (TG_APP_ID);
out_string (TG_APP_HASH);
out_string ("en");
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_code_methods, 0);
net_loop (0, code_is_sent);
assert (want_dc_num == -1);
}
/* }}} */
/* {{{ Check phone */
int check_phone_result;
int cr_f (void) {
return check_phone_result >= 0;
}
int check_phone_on_answer (struct query *q UU) {
assert (fetch_int () == (int)CODE_auth_checked_phone);
check_phone_result = fetch_bool ();
fetch_bool ();
return 0;
}
int check_phone_on_error (struct query *q UU, int error_code, int l, char *error) {
int s = strlen ("PHONE_MIGRATE_");
int s2 = strlen ("NETWORK_MIGRATE_");
if (l >= s && !memcmp (error, "PHONE_MIGRATE_", s)) {
int i = error[s] - '0';
assert (DC_list[i]);
dc_working_num = i;
DC_working = DC_list[i];
write_auth_file ();
bl_do_set_working_dc (i);
check_phone_result = 1;
} else if (l >= s2 && !memcmp (error, "NETWORK_MIGRATE_", s2)) {
int i = error[s2] - '0';
assert (DC_list[i]);
dc_working_num = i;
bl_do_set_working_dc (i);
DC_working = DC_list[i];
write_auth_file ();
check_phone_result = 1;
} else {
logprintf ( "error_code = %d, error = %.*s\n", error_code, l, error);
assert (0);
}
return 0;
}
struct query_methods check_phone_methods = {
.on_answer = check_phone_on_answer,
.on_error = check_phone_on_error
};
int do_auth_check_phone (const char *user) {
suser = tstrdup (user);
clear_packet ();
out_int (CODE_auth_check_phone);
out_string (user);
check_phone_result = -1;
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &check_phone_methods, 0);
net_loop (0, cr_f);
return check_phone_result;
}
/* }}} */
/* {{{ Nearest DC */
int nearest_dc_num;
int nr_f (void) {
return nearest_dc_num >= 0;
}
int nearest_dc_on_answer (struct query *q UU) {
assert (fetch_int () == (int)CODE_nearest_dc);
char *country = fetch_str_dup ();
if (verbosity > 0) {
logprintf ("Server thinks that you are in %s\n", country);
}
fetch_int (); // this_dc
nearest_dc_num = fetch_int ();
assert (nearest_dc_num >= 0);
return 0;
}
int fail_on_error (struct query *q UU, int error_code UU, int l UU, char *error UU) {
fprintf (stderr, "error #%d: %.*s\n", error_code, l, error);
assert (0);
return 0;
}
struct query_methods nearest_dc_methods = {
.on_answer = nearest_dc_on_answer,
.on_error = fail_on_error
};
int do_get_nearest_dc (void) {
clear_packet ();
out_int (CODE_help_get_nearest_dc);
nearest_dc_num = -1;
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &nearest_dc_methods, 0);
net_loop (0, nr_f);
return nearest_dc_num;
}
/* }}} */
/* {{{ Sign in / Sign up */
int sign_in_ok;
int our_id;
int sign_in_is_ok (void) {
return sign_in_ok;
}
struct user User;
int sign_in_on_answer (struct query *q UU) {
assert (fetch_int () == (int)CODE_auth_authorization);
int expires = fetch_int ();
fetch_user (&User);
if (!our_id) {
our_id = get_peer_id (User.id);
bl_do_set_our_id (our_id);
}
sign_in_ok = 1;
if (verbosity) {
logprintf ( "authorized successfully: name = '%s %s', phone = '%s', expires = %d\n", User.first_name, User.last_name, User.phone, (int)(expires - get_double_time ()));
}
DC_working->has_auth = 1;
bl_do_dc_signed (DC_working->id);
return 0;
}
int sign_in_on_error (struct query *q UU, int error_code, int l, char *error) {
logprintf ( "error_code = %d, error = %.*s\n", error_code, l, error);
sign_in_ok = -1;
assert (0);
return 0;
}
struct query_methods sign_in_methods = {
.on_answer = sign_in_on_answer,
.on_error = sign_in_on_error
};
int do_send_code_result (const char *code) {
clear_packet ();
out_int (CODE_auth_sign_in);
out_string (suser);
out_string (phone_code_hash);
out_string (code);
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0);
sign_in_ok = 0;
net_loop (0, sign_in_is_ok);
return sign_in_ok;
}
int do_send_code_result_auth (const char *code, const char *first_name, const char *last_name) {
clear_packet ();
out_int (CODE_auth_sign_up);
out_string (suser);
out_string (phone_code_hash);
out_string (code);
out_string (first_name);
out_string (last_name);
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0);
sign_in_ok = 0;
net_loop (0, sign_in_is_ok);
return sign_in_ok;
}
/* }}} */
/* {{{ Get contacts */
extern char *user_list[];
int get_contacts_on_answer (struct query *q UU) {
int i;
assert (fetch_int () == (int)CODE_contacts_contacts);
assert (fetch_int () == CODE_vector);
int n = fetch_int ();
for (i = 0; i < n; i++) {
assert (fetch_int () == (int)CODE_contact);
fetch_int (); // id
fetch_int (); // mutual
}
assert (fetch_int () == CODE_vector);
n = fetch_int ();
for (i = 0; i < n; i++) {
struct user *U = fetch_alloc_user ();
print_start ();
push_color (COLOR_YELLOW);
printf ("User #%d: ", get_peer_id (U->id));
print_user_name (U->id, (peer_t *)U);
push_color (COLOR_GREEN);
printf (" (");
printf ("%s", U->print_name);
if (U->phone) {
printf (" ");
printf ("%s", U->phone);
}
printf (") ");
pop_color ();
if (U->status.online > 0) {
printf ("online\n");
} else {
if (U->status.online < 0) {
printf ("offline. Was online ");
print_date_full (U->status.when);
} else {
printf ("offline permanent");
}
printf ("\n");
}
pop_color ();
print_end ();
}
return 0;
}
struct query_methods get_contacts_methods = {
.on_answer = get_contacts_on_answer,
};
void do_update_contact_list (void) {
clear_packet ();
out_int (CODE_contacts_get_contacts);
out_string ("");
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_contacts_methods, 0);
}
/* }}} */
/* {{{ Encrypt decrypted */
int *encr_extra;
int *encr_ptr;
int *encr_end;
char *encrypt_decrypted_message (struct secret_chat *E) {
static int msg_key[4];
static unsigned char sha1a_buffer[20];
static unsigned char sha1b_buffer[20];
static unsigned char sha1c_buffer[20];
static unsigned char sha1d_buffer[20];
int x = *(encr_ptr);
assert (x >= 0 && !(x & 3));
sha1 ((void *)encr_ptr, 4 + x, sha1a_buffer);
memcpy (msg_key, sha1a_buffer + 4, 16);
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];
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);
AES_KEY aes_key;
AES_set_encrypt_key (key, 256, &aes_key);
AES_ige_encrypt ((void *)encr_ptr, (void *)encr_ptr, 4 * (encr_end - encr_ptr), &aes_key, iv, 1);
memset (&aes_key, 0, sizeof (aes_key));
return (void *)msg_key;
}
void encr_start (void) {
encr_extra = packet_ptr;
packet_ptr += 1; // str len
packet_ptr += 2; // fingerprint
packet_ptr += 4; // msg_key
packet_ptr += 1; // len
}
void encr_finish (struct secret_chat *E) {
int l = packet_ptr - (encr_extra + 8);
while (((packet_ptr - encr_extra) - 3) & 3) {
int t;
secure_random (&t, 4);
out_int (t);
}
*encr_extra = ((packet_ptr - encr_extra) - 1) * 4 * 256 + 0xfe;
encr_extra ++;
*(long long *)encr_extra = E->key_fingerprint;
encr_extra += 2;
encr_extra[4] = l * 4;
encr_ptr = encr_extra + 4;
encr_end = packet_ptr;
memcpy (encr_extra, encrypt_decrypted_message (E), 16);
}
/* }}} */
/* {{{ Seng msg (plain text) */
int msg_send_encr_on_answer (struct query *q UU) {
assert (fetch_int () == CODE_messages_sent_encrypted_message);
rprintf ("Sent\n");
struct message *M = q->extra;
//M->date = fetch_int ();
fetch_int ();
bl_do_set_message_sent (M);
return 0;
}
int msg_send_on_answer (struct query *q UU) {
unsigned x = fetch_int ();
assert (x == CODE_messages_sent_message || x == CODE_messages_sent_message_link);
int id = fetch_int (); // id
struct message *M = q->extra;
bl_do_set_msg_id (M, id);
fetch_date ();
fetch_pts ();
fetch_seq ();
if (x == CODE_messages_sent_message_link) {
assert (fetch_int () == CODE_vector);
int n = fetch_int ();
int i;
unsigned a, b;
for (i = 0; i < n; i++) {
assert (fetch_int () == (int)CODE_contacts_link);
a = fetch_int ();
assert (a == CODE_contacts_my_link_empty || a == CODE_contacts_my_link_requested || a == CODE_contacts_my_link_contact);
if (a == CODE_contacts_my_link_requested) {
fetch_bool ();
}
b = fetch_int ();
assert (b == CODE_contacts_foreign_link_unknown || b == CODE_contacts_foreign_link_requested || b == CODE_contacts_foreign_link_mutual);
if (b == CODE_contacts_foreign_link_requested) {
fetch_bool ();
}
struct user *U = fetch_alloc_user ();
U->flags &= ~(FLAG_USER_IN_CONTACT | FLAG_USER_OUT_CONTACT);
if (a == CODE_contacts_my_link_contact) {
U->flags |= FLAG_USER_IN_CONTACT;
}
U->flags &= ~(FLAG_USER_IN_CONTACT | FLAG_USER_OUT_CONTACT);
if (b == CODE_contacts_foreign_link_mutual) {
U->flags |= FLAG_USER_IN_CONTACT | FLAG_USER_OUT_CONTACT;
}
if (b == CODE_contacts_foreign_link_requested) {
U->flags |= FLAG_USER_OUT_CONTACT;
}
print_start ();
push_color (COLOR_YELLOW);
printf ("Link with user ");
print_user_name (U->id, (void *)U);
printf (" changed\n");
pop_color ();
print_end ();
}
}
rprintf ("Sent: id = %d\n", id);
bl_do_set_message_sent (M);
return 0;
}
int msg_send_on_error (struct query *q, int error_code, int error_len, char *error) {
logprintf ( "error for query #%lld: #%d :%.*s\n", q->msg_id, error_code, error_len, error);
struct message *M = q->extra;
bl_do_delete_msg (M);
return 0;
}
struct query_methods msg_send_methods = {
.on_answer = msg_send_on_answer,
.on_error = msg_send_on_error
};
struct query_methods msg_send_encr_methods = {
.on_answer = msg_send_encr_on_answer
};
int out_message_num;
int our_id;
void do_send_encr_msg (struct message *M) {
peer_t *P = user_chat_get (M->to_id);
if (!P || P->encr_chat.state != sc_ok) { return; }
clear_packet ();
out_int (CODE_messages_send_encrypted);
out_int (CODE_input_encrypted_chat);
out_int (get_peer_id (M->to_id));
out_long (P->encr_chat.access_hash);
out_long (M->id);
encr_start ();
out_int (CODE_decrypted_message);
out_long (M->id);
static int buf[4];
secure_random (buf, 16);
out_cstring ((void *)buf, 16);
out_cstring ((void *)M->message, M->message_len);
out_int (CODE_decrypted_message_media_empty);
encr_finish (&P->encr_chat);
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M);
}
void do_send_msg (struct message *M) {
if (get_peer_type (M->to_id) == PEER_ENCR_CHAT) {
do_send_encr_msg (M);
return;
}
clear_packet ();
out_int (CODE_messages_send_message);
out_peer_id (M->to_id);
out_cstring (M->message, M->message_len);
out_long (M->id);
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_methods, M);
}
void do_send_message (peer_id_t id, const char *msg, int len) {
if (get_peer_type (id) == PEER_ENCR_CHAT) {
peer_t *P = user_chat_get (id);
if (!P) {
logprintf ("Can not send to unknown encrypted chat\n");
return;
}
if (P->encr_chat.state != sc_ok) {
logprintf ("Chat is not yet initialized\n");
return;
}
}
long long t;
secure_random (&t, 8);
logprintf ("t = %lld, len = %d\n", t, len);
bl_do_send_message_text (t, our_id, get_peer_type (id), get_peer_id (id), time (0), len, msg);
struct message *M = message_get (t);
assert (M);
do_send_msg (M);
print_message (M);
}
/* }}} */
/* {{{ Send text file */
void do_send_text (peer_id_t id, char *file_name) {
int fd = open (file_name, O_RDONLY);
if (fd < 0) {
rprintf ("No such file '%s'\n", file_name);
tfree_str (file_name);
return;
}
static char buf[(1 << 20) + 1];
int x = read (fd, buf, (1 << 20) + 1);
assert (x >= 0);
if (x == (1 << 20) + 1) {
rprintf ("Too big file '%s'\n", file_name);
tfree_str (file_name);
close (fd);
} else {
buf[x] = 0;
do_send_message (id, buf, x);
tfree_str (file_name);
close (fd);
}
}
/* }}} */
/* {{{ Mark read */
int mark_read_on_receive (struct query *q UU) {
assert (fetch_int () == (int)CODE_messages_affected_history);
fetch_pts ();
fetch_seq ();
fetch_int (); // offset
return 0;
}
int mark_read_encr_on_receive (struct query *q UU) {
fetch_bool ();
return 0;
}
struct query_methods mark_read_methods = {
.on_answer = mark_read_on_receive
};
struct query_methods mark_read_encr_methods = {
.on_answer = mark_read_encr_on_receive
};
void do_messages_mark_read (peer_id_t id, int max_id) {
clear_packet ();
out_int (CODE_messages_read_history);
out_peer_id (id);
out_int (max_id);
out_int (0);
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &mark_read_methods, 0);
}
void do_messages_mark_read_encr (peer_id_t id, long long access_hash, int last_time) {
clear_packet ();
out_int (CODE_messages_read_encrypted_history);
out_int (CODE_input_encrypted_chat);
out_int (get_peer_id (id));
out_long (access_hash);
out_int (last_time);
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &mark_read_encr_methods, 0);
}
void do_mark_read (peer_id_t id) {
peer_t *P = user_chat_get (id);
if (!P) {
rprintf ("Unknown peer\n");
return;
}
if (get_peer_type (id) == PEER_USER || get_peer_type (id) == PEER_CHAT) {
if (!P->last) {
rprintf ("Unknown last peer message\n");
return;
}
do_messages_mark_read (id, P->last->id);