-
Notifications
You must be signed in to change notification settings - Fork 149
/
tox.c
1862 lines (1556 loc) · 52.7 KB
/
tox.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
#include "main.h"
#include "tox_bootstrap.h"
struct Tox_Options options = {.proxy_host = proxy_address};
typedef struct {
uint8_t msg;
uint32_t param1, param2;
void *data;
} TOX_MSG;
static TOX_MSG tox_msg, audio_msg, video_msg, toxav_msg;
static volatile _Bool tox_thread_msg, audio_thread_msg, video_thread_msg, toxav_thread_msg;
static volatile _Bool save_needed = 1;
/* Writes log filename for fid to dest. returns length written */
static int log_file_name(uint8_t *dest, size_t size_dest, Tox *tox, int fid)
{
if (size_dest < TOX_PUBLIC_KEY_SIZE * 2 + sizeof(".txt"))
return -1;
uint8_t client_id[TOX_PUBLIC_KEY_SIZE];
tox_friend_get_public_key(tox, fid, client_id, 0);
cid_to_string(dest, client_id); dest += TOX_PUBLIC_KEY_SIZE * 2;
memcpy((char*)dest, ".txt", sizeof(".txt"));
return TOX_PUBLIC_KEY_SIZE * 2 + sizeof(".txt");
}
/* Writes friend meta data filename for fid to dest. returns length written */
static int friend_meta_data_file(uint8_t *dest, size_t size_dest, Tox *tox, int fid)
{
if (size_dest < TOX_PUBLIC_KEY_SIZE * 2 + sizeof(".fmetadata")){
return -1;
}
uint8_t client_id[TOX_PUBLIC_KEY_SIZE];
tox_friend_get_public_key(tox, fid, client_id, 0);
cid_to_string(dest, client_id); dest += TOX_PUBLIC_KEY_SIZE * 2;
memcpy((char*)dest, ".fmetadata", sizeof(".fmetadata"));
return TOX_PUBLIC_KEY_SIZE * 2 + sizeof(".fmetadata");
}
enum {
LOG_FILE_MSG_TYPE_TEXT = 0,
LOG_FILE_MSG_TYPE_ACTION = 1,
};
typedef struct {
uint64_t time;
uint16_t namelen, length;
uint8_t flags;
uint8_t msg_type;
uint8_t zeroes[2];
} LOG_FILE_MSG_HEADER;
void log_write(Tox *tox, int fid, const uint8_t *message, uint16_t length, _Bool author, uint8_t msg_type)
{
if(!logging_enabled) {
return;
}
uint8_t path[UTOX_FILE_NAME_LENGTH], *p;
uint8_t name[TOX_MAX_NAME_LENGTH];
size_t namelen;
FILE *file;
p = path + datapath(path);
int len = log_file_name(p, sizeof(path) - (p - path), tox, fid);
if (len == -1)
return;
p += len;
file = fopen((char*)path, "ab");
if(file) {
time_t rawtime;
time(&rawtime);
if(author) {
namelen = tox_self_get_name_size(tox);
tox_self_get_name(tox, name);
} else {
namelen = tox_friend_get_name_size(tox, fid, 0);
tox_friend_get_name(tox, fid, name, 0);
}
if (namelen > TOX_MAX_NAME_LENGTH) {
namelen = 0;
}
LOG_FILE_MSG_HEADER header = {
.time = rawtime,
.namelen = namelen,
.length = length,
.flags = author,
.msg_type = msg_type,
};
fwrite(&header, sizeof(header), 1, file);
fwrite(name, namelen, 1, file);
fwrite(message, length, 1, file);
fclose(file);
}
}
void log_read(Tox *tox, int fid)
{
uint8_t path[UTOX_FILE_NAME_LENGTH], *p;
FILE *file;
p = path + datapath(path);
int len = log_file_name(p, sizeof(path) - (p - path), tox, fid);
if (len == -1) {
debug("Error getting log file name for friend %d\n", fid);
return;
}
file = fopen((char*)path, "rb");
if(!file) {
debug("File not found (%s)\n", path);
p = path + datapath_old(path);
len = log_file_name(p, sizeof(path) - (p - path), tox, fid);
if (len == -1) {
debug("Error getting log file name for friend %d\n", fid);
return;
}
file = fopen((char*) path, "rb");
if (!file) {
debug("File not found (%s)\n", path);
return;
}
}
LOG_FILE_MSG_HEADER header;
off_t rewinds[MAX_BACKLOG_MESSAGES] = {};
size_t records_count = 0;
/* todo: some checks to avoid crashes with corrupted log files */
/* first find the last MAX_BACKLOG_MESSAGES messages in the log */
while(1 == fread(&header, sizeof(LOG_FILE_MSG_HEADER), 1, file)) {
fseeko(file, header.namelen + header.length, SEEK_CUR);
rewinds[records_count % countof(rewinds)] =
(off_t) sizeof(LOG_FILE_MSG_HEADER) + header.namelen + header.length;
records_count++;
}
if(ferror(file) || !feof(file)) {
// TODO: consider removing or truncating the log file.
// If !feof() this means that the file has an incomplete record,
// which would prevent it from loading forever, even though
// new records will keep being appended as usual.
debug("Log read error (%s)\n", path);
fclose(file);
return;
}
// Backtrack to read last MAX_BACKLOG_MESSAGES in full.
off_t rewind = 0;
MSG_IDX i;
for(i = 0; (i < records_count) && (i < countof(rewinds)); i++) {
rewind += rewinds[i];
}
fseeko(file, -rewind, SEEK_CUR);
MSG_DATA *m = &friend[fid].msg;
m->data = malloc(sizeof(void*) * i);
m->n = 0;
/* add the messages */
while((0 < i) && (1 == fread(&header, sizeof(LOG_FILE_MSG_HEADER), 1, file))) {
i--;
// Skip unused friend name recorded at the time.
fseeko(file, header.namelen, SEEK_CUR);
MESSAGE *msg = NULL;
switch(header.msg_type) {
case LOG_FILE_MSG_TYPE_ACTION: {
msg = malloc(sizeof(MESSAGE) + header.length);
msg->msg_type = MSG_TYPE_ACTION_TEXT;
break;
}
case LOG_FILE_MSG_TYPE_TEXT: {
msg = malloc(sizeof(MESSAGE) + header.length);
msg->msg_type = MSG_TYPE_TEXT;
break;
}
default: {
debug("Unknown backlog message type(%d), skipping.\n", (int)header.msg_type);
fseeko(file, header.length, SEEK_CUR);
continue;
}
}
// Read text message.
msg->author = header.flags & 1;
msg->length = header.length;
if(1 != fread(msg->msg, msg->length, 1, file)) {
debug("Log read error (%s)\n", path);
fclose(file);
return;
}
msg->length = utf8_validate(msg->msg, msg->length);
struct tm *ti;
time_t rawtime = header.time;
ti = localtime(&rawtime);
msg->time = ti->tm_hour * 60 + ti->tm_min;
m->data[m->n++] = msg;
// debug("loaded backlog: %d: %.*s\n", fid, msg->length, msg->msg);
}
fclose(file);
}
void friend_meta_data_read(Tox *tox, int friend_id)
{
/* Will need to be rewritten if anything is added to friend's meta data */
uint8_t path[UTOX_FILE_NAME_LENGTH], *p;
p = path + datapath(path);
int len = friend_meta_data_file(p, sizeof(path) - (p - path), tox, friend_id);
if (len == -1) {
debug("Error getting meta data file name for friend %d\n", friend_id);
return;
}
uint32_t size;
void *mdata = file_raw((char*)path, &size);
if (!mdata) {
debug("Meta Data not found (%s)\n", path);
return;
}
FRIEND_META_DATA *metadata = calloc(1, sizeof(*metadata));
if (size < sizeof(*metadata)) {
debug("Meta Data was incomplete\n");
return;
}
memcpy(metadata, mdata, sizeof(*metadata));
if (metadata->alias_length) {
friend_set_alias(&friend[friend_id], mdata + sizeof(size_t), metadata->alias_length);
}
}
static void tox_thread_message(Tox *tox, ToxAv *av, uint64_t time, uint8_t msg, uint32_t param1, uint32_t param2, void *data);
void tox_postmessage(uint8_t msg, uint32_t param1, uint32_t param2, void *data)
{
while(tox_thread_msg) {
yieldcpu(1);
}
tox_msg.msg = msg;
tox_msg.param1 = param1;
tox_msg.param2 = param2;
tox_msg.data = data;
tox_thread_msg = 1;
}
void toxvideo_postmessage(uint8_t msg, uint32_t param1, uint32_t param2, void *data)
{
while(video_thread_msg) {
yieldcpu(1);
}
video_msg.msg = msg;
video_msg.param1 = param1;
video_msg.param2 = param2;
video_msg.data = data;
video_thread_msg = 1;
}
void toxav_postmessage(uint8_t msg, uint32_t param1, uint32_t param2, void *data)
{
while(toxav_thread_msg) {
yieldcpu(1);
}
toxav_msg.msg = msg;
toxav_msg.param1 = param1;
toxav_msg.param2 = param2;
toxav_msg.data = data;
toxav_thread_msg = 1;
}
#include "tox_callbacks.h"
#include "tox_av.h"
/* bootstrap to dht with bootstrap_nodes */
static void do_bootstrap(Tox *tox)
{
static unsigned int j = 0;
if (j == 0)
j = rand();
int i = 0;
while(i < 4) {
struct bootstrap_node *d = &bootstrap_nodes[j % countof(bootstrap_nodes)];
tox_bootstrap(tox, d->address, d->port, d->key, 0);
tox_add_tcp_relay(tox, d->address, d->port, d->key, 0);
i++;
j++;
}
}
static void set_callbacks(Tox *tox)
{
tox_callback_friend_request(tox, callback_friend_request, NULL);
tox_callback_friend_message(tox, callback_friend_message, NULL);
tox_callback_friend_name(tox, callback_name_change, NULL);
tox_callback_friend_status_message(tox, callback_status_message, NULL);
tox_callback_friend_status(tox, callback_user_status, NULL);
tox_callback_friend_typing(tox, callback_typing_change, NULL);
tox_callback_friend_read_receipt(tox, callback_read_receipt, NULL);
tox_callback_friend_connection_status(tox, callback_connection_status, NULL);
tox_callback_group_invite(tox, callback_group_invite, NULL);
tox_callback_group_message(tox, callback_group_message, NULL);
tox_callback_group_action(tox, callback_group_action, NULL);
tox_callback_group_namelist_change(tox, callback_group_namelist_change, NULL);
tox_callback_group_title(tox, callback_group_title, NULL);
utox_set_callbacks_for_transfer(tox);
}
/* tries to load avatar from disk for given client id string and set avatar based on saved png data
* avatar is avatar to initialize. Will be unset if no file is found on disk or if file is corrupt or too large,
* otherwise will be set to avatar found on disk
* id is cid string of whose avatar to find(see also load_avatar in avatar.h)
* if png_data_out is not NULL, the png data loaded from disk will be copied to it.
* if it is not null, it should be at least UTOX_AVATAR_MAX_DATA_LENGTH bytes long
* if png_size_out is not null, the size of the png data will be stored in it
*
* returns: 1 on successful loading, 0 on failure
*/
static _Bool init_avatar(AVATAR *avatar, const char_t *id, uint8_t *png_data_out, uint32_t *png_size_out) {
unset_avatar(avatar);
uint8_t avatar_data[UTOX_AVATAR_MAX_DATA_LENGTH];
uint32_t size;
if (load_avatar(id, avatar_data, &size)) {
if (set_avatar(avatar, avatar_data, size)) {
if (png_data_out) {
memcpy(png_data_out, avatar_data, size);
}
if (png_size_out) {
*png_size_out = size;
}
return 1;
}
}
return 0;
}
static size_t load_save(uint8_t **out_data){
uint8_t path[UTOX_FILE_NAME_LENGTH], *p, *data;
uint32_t size;
do{ /* Try the STS compliant save location */
p = path + datapath(path);
strcpy((char*)p, "tox_save.tox");
data = file_raw((char*)path, &size);
if(data) break; /* We have data, were done here! */
/* Try filename missing the .tox extension */
p = path + datapath(path);
strcpy((char*)p, "tox_save");
data = file_raw((char*)path, &size);
if(data) break;
/* That didn't work, do we have a backup? */
p = path + datapath(path);
strcpy((char*)p, "tox_save.tmp");
data = file_raw((char*)path, &size);
if(data) break;
/* No backup huh? Is it in an old location we support? */
p = path + datapath_old(path);
strcpy((char*)p, "tox_save");
data = file_raw((char*)path, &size);
if(data) break;
/* Well, lets try the current directory... */
data = file_raw("tox_save", &size);
if(!data) return 0; /* F***it I give up! */
} while(0); /* Only once! */
*out_data = data;
return size;
}
static void tox_after_load(Tox *tox)
{
friends = tox_self_get_friend_list_size(tox);
uint32_t i = 0;
while(i != friends) {
int size;
FRIEND *f = &friend[i];
uint8_t name[TOX_MAX_NAME_LENGTH];
f->msg.scroll = 1.0;
tox_friend_get_public_key(tox, i, f->cid, 0);
size = tox_friend_get_name_size(tox, i, 0);
tox_friend_get_name(tox, i, name, 0);
friend_setname(f, name, size);
size = tox_friend_get_status_message_size(tox, i, 0);
f->status_message = malloc(size);
tox_friend_get_status_message(tox, i, f->status_message, 0);
f->status_length = size;
char_t cid[TOX_PUBLIC_KEY_SIZE * 2];
cid_to_string(cid, f->cid);
init_avatar(&f->avatar, cid, NULL, NULL);
log_read(tox, i);
friend_meta_data_read(tox, i);
i++;
}
self.name_length = tox_self_get_name_size(tox);
tox_self_get_name(tox, self.name);
self.statusmsg_length = tox_self_get_status_message_size(tox);
self.statusmsg = malloc(self.statusmsg_length);
tox_self_get_status_message(tox, self.statusmsg);
self.status = tox_self_get_status(tox);
}
static void load_defaults(Tox *tox)
{
uint8_t *name = (uint8_t*)DEFAULT_NAME, *status = (uint8_t*)DEFAULT_STATUS;
uint16_t name_len = sizeof(DEFAULT_NAME) - 1, status_len = sizeof(DEFAULT_STATUS) - 1;
tox_self_set_name(tox, name, name_len, 0);
tox_self_set_status_message(tox, status, status_len, 0);
self.name_length = name_len;
memcpy(self.name, name, name_len);
self.statusmsg_length = status_len;
self.statusmsg = malloc(status_len);
memcpy(self.statusmsg, status, status_len);
}
static void write_save(Tox *tox)
{
void *data;
uint32_t size;
uint8_t path_tmp[UTOX_FILE_NAME_LENGTH], path_real[UTOX_FILE_NAME_LENGTH], *p;
FILE *file;
size = tox_get_savedata_size(tox);
data = malloc(size);
tox_get_savedata(tox, data);
p = path_real + datapath(path_real);
memcpy(p, "tox_save.tox", sizeof("tox_save.tox"));
unsigned int path_len = (p - path_real) + sizeof("tox_save.tox");
memcpy(path_tmp, path_real, path_len);
memcpy(path_tmp + (path_len - 1), ".tmp", sizeof(".tmp"));
debug("Writing tox_save to: %s\n", (char*)path_tmp);
file = fopen((char*)path_tmp, "wb");
if(file) {
fwrite(data, size, 1, file);
flush_file(file);
fclose(file);
if (rename((char*)path_tmp, (char*)path_real) != 0) {
debug("Failed to rename file. %s to %s deleting and trying again\n", path_tmp, path_real);
remove((const char *)path_real);
if (rename((char*)path_tmp, (char*)path_real) != 0) {
debug("Saving Failed\n");
} else {
debug("Saved data\n");
}
} else {
debug("Saved data\n");
int ch = ch_mod(path_real);
if(!ch){
debug("CHMOD: success\n");
} else {
debug("CHMOD: failure\n");
}
}
} else {
debug("no data saved...\n");
}
save_needed = 0;
free(data);
}
void tox_settingschanged(void)
{
//free everything
tox_connected = 0;
list_freeall();
list_dropdown_clear(&dropdown_audio_in);
list_dropdown_clear(&dropdown_audio_out);
list_dropdown_clear(&dropdown_video);
tox_thread_init = 0;
toxaudio_postmessage(AUDIO_KILL, 0, 0, NULL);
toxvideo_postmessage(VIDEO_KILL, 0, 0, NULL);
toxav_postmessage(TOXAV_KILL, 0, 0, NULL);
// send the reconfig message!
tox_postmessage(0, 1, 0, NULL);
while(!tox_thread_init) {
yieldcpu(1);
}
list_start();
}
#define UTOX_TYPING_NOTIFICATION_TIMEOUT (1ul*1000*1000*1000)
static struct {
Tox *tox;
uint16_t friendnumber;
uint64_t time;
_Bool sent_value;
} typing_state = {
.tox = NULL,
.friendnumber = 0,
.time = 0,
.sent_value = 0,
};
static void utox_thread_work_for_typing_notifications(Tox *tox, uint64_t time)
{
if(typing_state.tox != tox) {
// Guard against Tox engine restarts.
return;
}
_Bool is_typing = (time < typing_state.time + UTOX_TYPING_NOTIFICATION_TIMEOUT);
if(typing_state.sent_value ^ is_typing) {
// Need to send an update.
if(tox_self_set_typing(tox, typing_state.friendnumber, is_typing, 0)){
// Successfully sent. Mark new state.
typing_state.sent_value = is_typing;
debug("Sent typing state to friend (%d): %d\n", typing_state.friendnumber, typing_state.sent_value);
}
}
}
/** void tox_thread(void)
*
* Main tox function, starts a new toxcore for utox to use, and then spawns its
* threads.
*
* Accepts and returns nothing.
*/
void tox_thread(void *UNUSED(args))
{
Tox *tox;
ToxAv *av;
uint8_t id[TOX_FRIEND_ADDRESS_SIZE];
_Bool reconfig;
do {
uint8_t *save_data = NULL;
size_t save_size = load_save(&save_data);
// Create main connection
TOX_ERR_NEW tox_new_err;
if (save_data && save_size) {
options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
options.savedata_data = save_data;
options.savedata_length = save_size;
}
debug("new tox object ipv6: %u udp: %u proxy: %u %s %u\n", options.ipv6_enabled, options.udp_enabled, options.proxy_type, options.proxy_host, options.proxy_port);
if((tox = tox_new(&options, &tox_new_err)) == NULL) {
debug("trying without proxy, err %u\n", tox_new_err);
if(!options.proxy_type || (options.proxy_type = TOX_PROXY_TYPE_NONE, (tox = tox_new(&options, &tox_new_err)) == NULL)) {
debug("trying without ipv6, err %u\n", tox_new_err);
if(!options.ipv6_enabled || (options.ipv6_enabled = 0, (tox = tox_new(&options, &tox_new_err)) == NULL)) {
debug("tox_new() failed %u\n", tox_new_err);
exit(1);
}
dropdown_ipv6.selected = dropdown_ipv6.over = 1;
}
dropdown_proxy.selected = dropdown_proxy.over = 0;
}
if (save_size) {
tox_after_load(tox);
free(save_data);
} else {
debug("No save file, using defaults\n");
load_defaults(tox);
}
// Set local info for self
edit_setstr(&edit_name, self.name, self.name_length);
edit_setstr(&edit_status, self.statusmsg, self.statusmsg_length);
// Get tox id, and gets the hex version for utox
tox_self_get_address(tox, id);
memcpy(self.id_binary, id, TOX_FRIEND_ADDRESS_SIZE);
id_to_string(self.id_buffer, id);
self.id_buffer_length = TOX_FRIEND_ADDRESS_SIZE * 2;
debug("Tox ID: %.*s\n", (int)self.id_buffer_length, self.id_buffer);
{
uint8_t avatar_data[UTOX_AVATAR_MAX_DATA_LENGTH];
uint32_t avatar_size;
uint8_t hex_id[TOX_FRIEND_ADDRESS_SIZE * 2];
id_to_string(hex_id, self.id_binary);
if (init_avatar(&self.avatar, hex_id, avatar_data, &avatar_size)) {
self.avatar_data = malloc(avatar_size);
if (self.avatar_data) {
memcpy(self.avatar_data, avatar_data, avatar_size);
self.avatar_size = avatar_size;
self.avatar_format = UTOX_AVATAR_FORMAT_PNG;
char_t hash_string[TOX_HASH_LENGTH * 2];
hash_to_string(hash_string, self.avatar.hash);
debug("Tox Avatar Hash: %.*s\n", (int)sizeof(hash_string), hash_string);
}
}
}
// Give toxcore the functions to call
set_callbacks(tox);
// Connect to bootstrapped nodes in "tox_bootstrap.h"
do_bootstrap(tox);
// Start the tox av session.
av = toxav_new(tox, MAX_CALLS);
// Give toxcore the av functions to call
set_av_callbacks(av);
global_av = av;
tox_thread_init = 1;
// Start the treads
thread(audio_thread, av);
thread(video_thread, av);
thread(toxav_thread, av);
//
_Bool connected = 0;
uint64_t last_save = get_time(), time;
while(1) {
// Put toxcore to work
tox_iterate(tox);
// Check currents connection
if(!!tox_self_get_connection_status(tox) != connected) {
connected = !connected;
postmessage(DHT_CONNECTED, connected, 0, NULL);
debug("Connected to DHT: %u\n", connected);
}
time = get_time();
// Wait 1million ticks then reconnect if needed and write save
if(time - last_save >= (uint64_t)10 * 1000 * 1000 * 1000) {
last_save = time;
if(!connected) {
do_bootstrap(tox);
}
//save every 10mill.
if (save_needed || (time - last_save >= (uint64_t)100 * 1000 * 1000 * 1000)){
// Save tox data
write_save(tox);
}
}
// If there's a message, load it, and send to the tox message thread
if (tox_thread_msg) {
TOX_MSG *msg = &tox_msg;
// If msg->msg is 0, reconfig if needed and break from tox_do
if (!msg->msg) {
reconfig = msg->param1;
tox_thread_msg = 0;
break;
}
tox_thread_message(tox, av, time, msg->msg, msg->param1, msg->param2, msg->data);
tox_thread_msg = 0;
}
if (!dont_send_typing_notes){
// Thread active transfers and check if friend is typing
utox_thread_work_for_typing_notifications(tox, time);
}
// Ask toxcore how many ms to wait, then wait at the most 20ms
uint32_t interval = tox_iteration_interval(tox);
yieldcpu((interval > 20) ? 20 : interval);
}
write_save(tox);
// Wait for all a/v threads to return 0
while(audio_thread_init || video_thread_init || toxav_thread_init) {
yieldcpu(1);
}
// Stop av threads, and toxcore.
debug("av_thread exit, tox thread ending\n");
toxav_kill(av);
tox_kill(tox);
} while(reconfig);
tox_thread_init = 0;
}
static void tox_thread_message(Tox *tox, ToxAv *av, uint64_t time, uint8_t msg, uint32_t param1, uint32_t param2, void *data)
{
switch(msg) {
case TOX_SETNAME: {
/* param1: name length
* data: name
*/
tox_self_set_name(tox, data, param1, 0);
break;
}
case TOX_SETAVATAR: {
/*
* param1: avatar format
* param2: length of avatar data
* data: raw avatar data (PNG)
*/
if (self.avatar_data) {
free(self.avatar_data);
}
self.avatar_data = data;
self.avatar_size = param2;
self.avatar_format = param1;
utox_avatar_update_friends(tox);
break;
}
case TOX_UNSETAVATAR: {
free(self.avatar_data);
self.avatar_data = NULL;
self.avatar_size = 0;
self.avatar_format = 0;
utox_avatar_update_friends(tox);
break;
}
case TOX_SETSTATUSMSG: {
/* param1: status length
* data: status message
*/
tox_self_set_status_message(tox, data, param1, 0);
break;
}
case TOX_SETSTATUS: {
/* param1: status
*/
tox_self_set_status(tox, param1);
break;
}
case TOX_ADDFRIEND: {
/* param1: length of message
* data: friend id + message
*/
uint32_t fid;
TOX_ERR_FRIEND_ADD f_err;
if(!param1) {
STRING* default_add_msg = SPTR(DEFAULT_FRIEND_REQUEST_MESSAGE);
fid = tox_friend_add(tox, data, default_add_msg->str, default_add_msg->length, &f_err);
} else {
fid = tox_friend_add(tox, data, data + TOX_FRIEND_ADDRESS_SIZE, param1, &f_err);
}
if(f_err != TOX_ERR_FRIEND_ADD_OK) {
uint8_t addf_error;
switch(f_err) {
case TOX_ERR_FRIEND_ADD_TOO_LONG:
addf_error = ADDF_TOOLONG; break;
case TOX_ERR_FRIEND_ADD_NO_MESSAGE:
addf_error = ADDF_NOMESSAGE; break;
case TOX_ERR_FRIEND_ADD_OWN_KEY:
addf_error = ADDF_OWNKEY; break;
case TOX_ERR_FRIEND_ADD_ALREADY_SENT:
addf_error = ADDF_ALREADYSENT; break;
case TOX_ERR_FRIEND_ADD_BAD_CHECKSUM:
addf_error = ADDF_BADCHECKSUM; break;
case TOX_ERR_FRIEND_ADD_SET_NEW_NOSPAM:
addf_error = ADDF_SETNEWNOSPAM; break;
case TOX_ERR_FRIEND_ADD_MALLOC:
addf_error = ADDF_NOMEM; break;
default:
addf_error = ADDF_UNKNOWN; break;
}
postmessage(FRIEND_ADD, 1, addf_error, data);
} else {
postmessage(FRIEND_ADD, 0, fid, data);
}
break;
}
case TOX_DELFRIEND: {
/* param1: friend #
*/
tox_friend_delete(tox, param1, 0);
postmessage(FRIEND_DEL, 0, 0, data);
break;
}
case TOX_ACCEPTFRIEND: {
/* data: FRIENDREQ
*/
FRIENDREQ *req = data;
TOX_ERR_FRIEND_ADD f_err;
uint32_t fid = tox_friend_add_norequest(tox, req->id, &f_err);
postmessage(FRIEND_ACCEPT, (f_err != TOX_ERR_FRIEND_ADD_OK), (f_err != TOX_ERR_FRIEND_ADD_OK) ? 0 : fid, req);
break;
}
case TOX_SENDMESSAGE:
case TOX_SENDACTION: {
/* param1: friend #
* param2: message length
* data: message
*/
void *p = data;
TOX_MESSAGE_TYPE type;
if(msg == TOX_SENDACTION){
type = TOX_MESSAGE_TYPE_ACTION;
} else {
type = TOX_MESSAGE_TYPE_NORMAL;
}
while(param2 > TOX_MAX_MESSAGE_LENGTH) {
uint16_t len = TOX_MAX_MESSAGE_LENGTH - utf8_unlen(p + TOX_MAX_MESSAGE_LENGTH);
tox_friend_send_message(tox, param1, type, p, len, 0);
param2 -= len;
p += len;
}
// Send last or only message
tox_friend_send_message(tox, param1, type, p, param2, 0);
/* write message to friend to logfile */
log_write(tox, param1, data, param2, 1, LOG_FILE_MSG_TYPE_TEXT);
free(data);
break;
}
case TOX_SENDMESSAGEGROUP: {
/* param1: group #
* param2: message length
* data: message
*/
tox_group_message_send(tox, param1, data, param2);
free(data);
break;
}
case TOX_SENDACTIONGROUP: {
/* param1: group #
* param2: message length
* data: message
*/
tox_group_action_send(tox, param1, data, param2);
free(data);
break;
}
case TOX_SET_TYPING: {
/* param1: friend #
*/
// Check if user has switched to another friend window chat.
// Take care not to react on obsolete data from old Tox instance.
_Bool need_resetting = (typing_state.tox == tox) &&
(typing_state.friendnumber != param1) &&
(typing_state.sent_value);
if(need_resetting) {
// Tell previous friend that he's betrayed.
tox_self_set_typing(tox, typing_state.friendnumber, 0, 0);
// Mark that new friend doesn't know that we're typing yet.
typing_state.sent_value = 0;
}
// Mark us as typing to this friend at the moment.
// utox_thread_work_for_typing_notifications() will
// send a notification if it deems necessary.
typing_state.tox = tox;
typing_state.friendnumber = param1;
typing_state.time = time;
//debug("Set typing state for friend (%d): %d\n", typing_state.friendnumber, typing_state.sent_value);
break;
}
case TOX_CALL: {
/* param1: friend #
*/
int32_t id;
toxav_call(av, &id, param1, &av_DefaultSettings, 10);
postmessage(FRIEND_CALL_STATUS, param1, id, (void*)CALL_RINGING);
break;
}
case TOX_CALL_VIDEO: {
/* param1: friend #
*/
ToxAvCSettings settings = av_DefaultSettings;
settings.call_type = av_TypeVideo;
settings.max_video_width = max_video_width;
settings.max_video_height = max_video_height;
int32_t id;
toxav_call(av, &id, param1, &settings, 10);
postmessage(FRIEND_CALL_STATUS, param1, id, (void*)CALL_RINGING_VIDEO);
break;
}
case TOX_CALL_VIDEO_ON: {
/* param1: friend #
* param2: call #
*/
ToxAvCSettings settings = av_DefaultSettings;
settings.call_type = av_TypeVideo;
settings.max_video_width = max_video_width;
settings.max_video_height = max_video_height;
toxav_change_settings(av, param2, &settings);
postmessage(FRIEND_CALL_START_VIDEO, param1, param2, NULL);
break;
}
case TOX_CALL_VIDEO_OFF: {
/* param1: friend #
* param2: call #
*/
toxav_change_settings(av, param2, &av_DefaultSettings);
postmessage(FRIEND_CALL_STOP_VIDEO, param1, param2, NULL);
break;
}
case TOX_ACCEPTCALL: {
/* param1: call #
*/
ToxAvCSettings settings = av_DefaultSettings;
if(param2) {
settings.call_type = av_TypeVideo;
settings.max_video_width = max_video_width;
settings.max_video_height = max_video_height;
}
toxav_answer(av, param1, &settings);
break;
}
case TOX_HANGUP: {
/* param1: call #
*/
toxav_hangup(av, param1);
break;
}
case TOX_CANCELCALL: {