forked from notsecure/uTox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tox.c
1879 lines (1552 loc) · 51.3 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"
typedef struct {
uint8_t msg;
uint16_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_get_client_id(tox, fid, client_id);
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");
}
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[512], *p;
uint8_t name[TOX_MAX_NAME_LENGTH];
int 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_get_self_name(tox, name);
} else if((namelen = tox_get_name(tox, fid, name)) == -1) {
//error reading name
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[512], *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);
}
static void tox_thread_message(Tox *tox, ToxAv *av, uint64_t time, uint8_t msg, uint16_t param1, uint16_t param2, void *data);
void tox_postmessage(uint8_t msg, uint16_t param1, uint16_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, uint16_t param1, uint16_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, uint16_t param1, uint16_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_from_address(tox, d->address, d->port, d->key);
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_action(tox, callback_friend_action, NULL);
tox_callback_name_change(tox, callback_name_change, NULL);
tox_callback_status_message(tox, callback_status_message, NULL);
tox_callback_user_status(tox, callback_user_status, NULL);
tox_callback_typing_change(tox, callback_typing_change, NULL);
tox_callback_read_receipt(tox, callback_read_receipt, NULL);
tox_callback_connection_status(tox, callback_connection_status, NULL);
tox_callback_avatar_info(tox, callback_avatar_info, NULL);
tox_callback_avatar_data(tox, callback_avatar_data, 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 TOX_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[TOX_AVATAR_MAX_DATA_LENGTH];
uint32_t size;
if (load_avatar(id, avatar_data, &size)) {
_Bool have_hash = load_avatar_hash(id, avatar->hash);
if (set_avatar(avatar, avatar_data, size, !have_hash)) {
if (png_data_out) {
memcpy(png_data_out, avatar_data, size);
}
if (png_size_out) {
*png_size_out = size;
}
if (!have_hash) { // save newly created hash if it wasn't found on disk
save_avatar_hash(id, avatar->hash);
}
return 1;
}
}
return 0;
}
static _Bool load_save(Tox *tox)
{
{
uint8_t path[512], *p;
uint32_t size;
/* Try the STS compliant save location */
p = path + datapath(path);
strcpy((char*)p, "tox_save.tox");
void *data = file_raw((char*)path, &size);
if(!data) {
/* Try filename missing the .tox extension */
p = path + datapath(path);
strcpy((char*)p, "tox_save");
data = file_raw((char*)path, &size);
if(!data) {
/* 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){
/* 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) {
/* Well, lets try the current directory... */
data = file_raw("tox_save", &size);
if(!data) {
/* F***it I give up! */
return 0;
}
}
}
}
}
tox_load(tox, data, size);
free(data);
}
friends = tox_count_friendlist(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_get_client_id(tox, i, f->cid);
size = tox_get_name(tox, i, name);
friend_setname(f, name, size);
size = tox_get_status_message_size(tox, i);
f->status_message = malloc(size);
tox_get_status_message(tox, i, f->status_message, size);
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);
i++;
}
self.name_length = tox_get_self_name(tox, self.name);
self.statusmsg_length = tox_get_self_status_message_size(tox);
self.statusmsg = malloc(self.statusmsg_length);
tox_get_self_status_message(tox, self.statusmsg, self.statusmsg_length);
self.status = tox_get_self_user_status(tox);
return 1;
}
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_set_name(tox, name, name_len);
tox_set_status_message(tox, status, status_len);
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[512], path_real[512], *p;
FILE *file;
size = tox_size(tox);
data = malloc(size);
tox_save(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"));
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");
}
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);
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_set_user_is_typing(tox, typing_state.friendnumber, is_typing)){
// 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 spwans it's
* threads.
*
* Accepts and retuns nothing.
*/
void tox_thread(void *UNUSED(args))
{
Tox *tox;
ToxAv *av;
uint8_t id[TOX_FRIEND_ADDRESS_SIZE];
_Bool reconfig;
do {
// Create main connection
debug("new tox object ipv6: %u no_udp: %u proxy: %u %s %u\n", options.ipv6enabled, options.udp_disabled, options.proxy_type, options.proxy_address, options.proxy_port);
if((tox = tox_new(&options)) == NULL) {
debug("trying without proxy\n");
if(!options.proxy_type || (options.proxy_type = TOX_PROXY_NONE, (tox = tox_new(&options)) == NULL)) {
debug("trying without ipv6\n");
if(!options.ipv6enabled || (options.ipv6enabled = 0, (tox = tox_new(&options)) == NULL)) {
debug("tox_new() failed\n");
exit(1);
}
dropdown_ipv6.selected = dropdown_ipv6.over = 1;
}
dropdown_proxy.selected = dropdown_proxy.over = 0;
}
// Load saved information, else use defaults
if(!load_save(tox)) {
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_get_address(tox, id);
id_to_string(self.id, id);
debug("Tox ID: %.*s\n", (int)sizeof(self.id), self.id);
uint8_t avatar_data[TOX_AVATAR_MAX_DATA_LENGTH];
uint32_t avatar_size;
if (init_avatar(&self.avatar, self.id, avatar_data, &avatar_size)) {
tox_set_avatar(tox, TOX_AVATAR_FORMAT_PNG, avatar_data, avatar_size); // set avatar before connecting
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 bootstraped 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_do(tox);
// Check currents connection
if(tox_isconnected(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)){
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;
}
// Thread active transfers and check if friend is typing
utox_thread_work_for_transfers(tox, time);
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_do_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, uint16_t param1, uint16_t param2, void *data)
{
switch(msg) {
case TOX_SETNAME: {
/* param1: name length
* data: name
*/
tox_set_name(tox, data, param1);
break;
}
case TOX_SETAVATAR: {
/*
* param1: avatar format
* param2: length of avatar data
* data: raw avatar data (PNG)
*/
tox_set_avatar(tox, param1, data, param2);
free(data);
break;
}
case TOX_UNSETAVATAR: {
tox_unset_avatar(tox);
break;
}
case TOX_SETSTATUSMSG: {
/* param1: status length
* data: status message
*/
tox_set_status_message(tox, data, param1);
break;
}
case TOX_SETSTATUS: {
/* param1: status
*/
tox_set_user_status(tox, param1);
break;
}
case TOX_ADDFRIEND: {
/* param1: length of message
* data: friend id + message
*/
int r;
if(!param1) {
STRING* default_add_msg = SPTR(DEFAULT_FRIEND_REQUEST_MESSAGE);
r = tox_add_friend(tox, data, default_add_msg->str, default_add_msg->length);
} else {
r = tox_add_friend(tox, data, data + TOX_FRIEND_ADDRESS_SIZE, param1);
}
if(r < 0) {
uint8_t addf_error;
switch(r) {
case TOX_FAERR_TOOLONG:
addf_error = ADDF_TOOLONG; break;
case TOX_FAERR_NOMESSAGE:
addf_error = ADDF_NOMESSAGE; break;
case TOX_FAERR_OWNKEY:
addf_error = ADDF_OWNKEY; break;
case TOX_FAERR_ALREADYSENT:
addf_error = ADDF_ALREADYSENT; break;
case TOX_FAERR_BADCHECKSUM:
addf_error = ADDF_BADCHECKSUM; break;
case TOX_FAERR_SETNEWNOSPAM:
addf_error = ADDF_SETNEWNOSPAM; break;
case TOX_FAERR_NOMEM:
addf_error = ADDF_NOMEM; break;
case TOX_FAERR_UNKNOWN:
default:
addf_error = ADDF_UNKNOWN; break;
}
postmessage(FRIEND_ADD, 1, addf_error, data);
} else {
postmessage(FRIEND_ADD, 0, r, data);
}
break;
}
case TOX_DELFRIEND: {
/* param1: friend #
*/
tox_del_friend(tox, param1);
postmessage(FRIEND_DEL, 0, 0, data);
break;
}
case TOX_ACCEPTFRIEND: {
/* data: FRIENDREQ
*/
FRIENDREQ *req = data;
int r = tox_add_friend_norequest(tox, req->id);
postmessage(FRIEND_ACCEPT, (r < 0), (r < 0) ? 0 : r, req);
break;
}
case TOX_SENDMESSAGE: {
/* param1: friend #
* param2: message length
* data: message
*/
/* write message to friend to logfile */
log_write(tox, param1, data, param2, 1, LOG_FILE_MSG_TYPE_TEXT);
void *p = data;
while(param2 > TOX_MAX_MESSAGE_LENGTH) {
uint16_t len = TOX_MAX_MESSAGE_LENGTH - utf8_unlen(p + TOX_MAX_MESSAGE_LENGTH);
tox_send_message(tox, param1, p, len);
param2 -= len;
p += len;
}
tox_send_message(tox, param1, p, param2);
free(data);
break;
}
case TOX_SENDACTION: {
/* param1: friend #
* param2: message length
* data: message
*/
/* write action/emote to friend to logfile */
log_write(tox, param1, data, param2, 1, LOG_FILE_MSG_TYPE_ACTION);
void *p = data;
while(param2 > TOX_MAX_MESSAGE_LENGTH) {
uint16_t len = TOX_MAX_MESSAGE_LENGTH - utf8_unlen(p + TOX_MAX_MESSAGE_LENGTH);
tox_send_action(tox, param1, p, len);
param2 -= len;
p += len;
}
tox_send_action(tox, param1, p, param2);
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);
}
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_set_user_is_typing(tox, typing_state.friendnumber, 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: {
/* param1: call #
* param2: friend #
*/
toxav_cancel(av, param1, param2, "Call canceled by friend");
postmessage(FRIEND_CALL_STATUS, param2, param1, (void*)(size_t)CALL_NONE);
break;
}
case TOX_NEWGROUP: {
/*
*/
int g = -1;
if (param1) {
g = toxav_add_av_groupchat(tox, &callback_av_group_audio, NULL);
} else {
g = tox_add_groupchat(tox);
}
if(g != -1) {
postmessage(GROUP_ADD, g, 0, tox);
}
break;
}
case TOX_LEAVEGROUP: {
/* param1: group #
*/
tox_del_groupchat(tox, param1);
break;
}
case TOX_GROUPINVITE: {
/* param1: group #
* param2: friend #
*/
tox_invite_friend(tox, param2, param1);
break;
}
case TOX_GROUPCHANGETOPIC: {
/* param1: group #
* param2: topic length
* data: topic
*/
tox_group_set_title(tox, param1, data, param2);
postmessage(GROUP_TITLE, param1, param2, data);
break;
}
case TOX_GROUP_AUDIO_START:{
/* param1: group #
*/
postmessage(GROUP_AUDIO_START, param1, 0, NULL);
break;
}
case TOX_GROUP_AUDIO_END:{
/* param1: group #
*/
postmessage(GROUP_AUDIO_END, param1, 0, NULL);
break;
}
case TOX_SENDFILES: {
/* param1: friend #
* param2: offset of first file name in data
* data: file names
*/
if(param2 == 0xFFFF) {
//paths with line breaks