-
Notifications
You must be signed in to change notification settings - Fork 20
/
minitox.c
1357 lines (1151 loc) · 40.2 KB
/
minitox.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
863
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
/*
* MiniTox - A minimal client for Tox
*/
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <tox/tox.h>
/*******************************************************************************
*
* Consts & Macros
*
******************************************************************************/
// where to save the tox data.
// if don't want to save, set it to NULL.
const char *savedata_filename = "./savedata.tox";
const char *savedata_tmp_filename = "./savedata.tox.tmp";
struct DHT_node {
const char *ip;
uint16_t port;
const char key_hex[TOX_PUBLIC_KEY_SIZE*2 1];
};
struct DHT_node bootstrap_nodes[] = {
// Setup tox bootrap nodes
{"node.tox.biribiri.org", 33445, "F404ABAA1C99A9D37D61AB54898F56793E1DEF8BD46B1038B9D822E8460FAB67"},
{"128.199.199.197", 33445, "B05C8869DBB4EDDD308F43C1A974A20A725A36EACCA123862FDE9945BF9D3E09"},
{"2400:6180:0:d0::17a:a001", 33445, "B05C8869DBB4EDDD308F43C1A974A20A725A36EACCA123862FDE9945BF9D3E09"},
};
#define LINE_MAX_SIZE 512 // If input line's length surpassed this value, it will be truncated.
#define PORT_RANGE_START 33445 // tox listen port range
#define PORT_RANGE_END 34445
#define AREPL_INTERVAL 30 // Async REPL iterate interval. unit: millisecond.
#define DEFAULT_CHAT_HIST_COUNT 20 // how many items of chat history to show by default;
#define SAVEDATA_AFTER_COMMAND true // whether save data after executing any command
/// Macros for terminal display
#define CODE_ERASE_LINE "\r\033[2K"
#define RESET_COLOR "\x01b[0m"
#define SELF_TALK_COLOR "\x01b[35m" // magenta
#define GUEST_TALK_COLOR "\x01b[90m" // bright black
#define CMD_PROMPT_COLOR "\x01b[34m" // blue
#define CMD_PROMPT CMD_PROMPT_COLOR "> " RESET_COLOR // green
#define FRIEND_TALK_PROMPT CMD_PROMPT_COLOR "%-.12s << " RESET_COLOR
#define GROUP_TALK_PROMPT CMD_PROMPT_COLOR "%-.12s <<< " RESET_COLOR
#define GUEST_MSG_PREFIX GUEST_TALK_COLOR "%s .12s | " RESET_COLOR
#define SELF_MSG_PREFIX SELF_TALK_COLOR "%s .12s | " RESET_COLOR
#define CMD_MSG_PREFIX CMD_PROMPT
#define PRINT(_fmt, ...) \
fputs(CODE_ERASE_LINE,stdout);\
printf(_fmt "\n", ##__VA_ARGS__);
#define COLOR_PRINT(_color, _fmt,...) PRINT(_color _fmt RESET_COLOR, ##__VA_ARGS__)
#define INFO(_fmt,...) COLOR_PRINT("\x01b[36m", _fmt, ##__VA_ARGS__) // cyran
#define WARN(_fmt,...) COLOR_PRINT("\x01b[33m", _fmt, ##__VA_ARGS__) // yellow
#define ERROR(_fmt,...) COLOR_PRINT("\x01b[31m", _fmt, ##__VA_ARGS__) // red
/*******************************************************************************
*
* Headers
*
******************************************************************************/
Tox *tox;
typedef void CommandHandler(int narg, char **args);
struct Command {
char* name;
char* desc;
int narg;
CommandHandler *handler;
};
struct GroupUserData {
uint32_t friend_num;
uint8_t *cookie;
size_t length;
};
struct FriendUserData {
uint8_t pubkey[TOX_PUBLIC_KEY_SIZE];
};
union RequestUserData {
struct GroupUserData group;
struct FriendUserData friend;
};
struct Request {
char *msg;
uint32_t id;
bool is_friend_request;
union RequestUserData userdata;
struct Request *next;
};
struct ChatHist {
char *msg;
struct ChatHist *next;
struct ChatHist *prev;
};
struct GroupPeer {
uint8_t pubkey[TOX_PUBLIC_KEY_SIZE];
char name[TOX_MAX_NAME_LENGTH 1];
};
struct Group {
uint32_t group_num;
char *title;
struct GroupPeer *peers;
size_t peers_count;
struct ChatHist *hist;
struct Group *next;
};
struct Friend {
uint32_t friend_num;
char *name;
char *status_message;
uint8_t pubkey[TOX_PUBLIC_KEY_SIZE];
TOX_CONNECTION connection;
struct ChatHist *hist;
struct Friend *next;
};
int NEW_STDIN_FILENO = STDIN_FILENO;
struct Request *requests = NULL;
struct Friend *friends = NULL;
struct Friend self;
struct Group *groups = NULL;
enum TALK_TYPE { TALK_TYPE_FRIEND, TALK_TYPE_GROUP, TALK_TYPE_COUNT, TALK_TYPE_NULL = UINT32_MAX };
uint32_t TalkingTo = TALK_TYPE_NULL;
/*******************************************************************************
*
* Utils
*
******************************************************************************/
#define RESIZE(key, size_key, length) \
if ((size_key) < (length 1)) { \
size_key = (length 1);\
key = calloc(1, size_key);\
}
#define LIST_FIND(_p, _condition) \
for (;*(_p) != NULL;_p = &((*_p)->next)) { \
if (_condition) { \
break;\
}\
}\
#define INDEX_TO_TYPE(idx) (idx % TALK_TYPE_COUNT)
#define INDEX_TO_NUM(idx) (idx / TALK_TYPE_COUNT)
#define GEN_INDEX(num,type) (num * TALK_TYPE_COUNT type)
bool str2uint(char *str, uint32_t *num) {
char *str_end;
long l = strtol(str,&str_end,10);
if (str_end == str || l < 0 ) return false;
*num = (uint32_t)l;
return true;
}
char* genmsg(struct ChatHist **pp, const char *fmt, ...) {
va_list va;
va_start(va, fmt);
va_list va2;
va_copy(va2, va);
size_t len = vsnprintf(NULL, 0, fmt, va2);
va_end(va2);
struct ChatHist *h = malloc(sizeof(struct ChatHist));
h->prev = NULL;
h->next = (*pp);
if (*pp) (*pp)->prev = h;
*pp = h;
h->msg = malloc(len 1);
vsnprintf(h->msg, len 1, fmt, va);
va_end(va);
return h->msg;
}
char* getftime(void) {
static char timebuf[64];
time_t tt = time(NULL);
struct tm *tm = localtime(&tt);
strftime(timebuf, sizeof(timebuf), "%H:%M:%S", tm);
return timebuf;
}
const char * connection_enum2text(TOX_CONNECTION conn) {
switch (conn) {
case TOX_CONNECTION_NONE:
return "Offline";
case TOX_CONNECTION_TCP:
return "Online(TCP)";
case TOX_CONNECTION_UDP:
return "Online(UDP)";
default:
return "UNKNOWN";
}
}
struct Friend *getfriend(uint32_t friend_num) {
struct Friend **p = &friends;
LIST_FIND(p, (*p)->friend_num == friend_num);
return *p;
}
struct Friend *addfriend(uint32_t friend_num) {
struct Friend *f = calloc(1, sizeof(struct Friend));
f->next = friends;
friends = f;
f->friend_num = friend_num;
f->connection = TOX_CONNECTION_NONE;
tox_friend_get_public_key(tox, friend_num, f->pubkey, NULL);
return f;
}
bool delfriend(uint32_t friend_num) {
struct Friend **p = &friends;
LIST_FIND(p, (*p)->friend_num == friend_num);
struct Friend *f = *p;
if (f) {
*p = f->next;
if (f->name) free(f->name);
if (f->status_message) free(f->status_message);
while (f->hist) {
struct ChatHist *tmp = f->hist;
f->hist = f->hist->next;
free(tmp);
}
free(f);
return 1;
}
return 0;
}
struct Group *addgroup(uint32_t group_num) {
struct Group *cf = calloc(1, sizeof(struct Group));
cf->next = groups;
groups = cf;
cf->group_num = group_num;
return cf;
}
bool delgroup(uint32_t group_num) {
struct Group **p = &groups;
LIST_FIND(p, (*p)->group_num == group_num);
struct Group *cf = *p;
if (cf) {
*p = cf->next;
if (cf->peers) free(cf->peers);
if (cf->title) free(cf->title);
while (cf->hist) {
struct ChatHist *tmp = cf->hist;
cf->hist = cf->hist->next;
free(tmp);
}
free(cf);
return 1;
}
return 0;
}
struct Group *getgroup(uint32_t group_num) {
struct Group **p = &groups;
LIST_FIND(p, (*p)->group_num == group_num);
return *p;
}
uint8_t *hex2bin(const char *hex)
{
size_t len = strlen(hex) / 2;
uint8_t *bin = malloc(len);
for (size_t i = 0; i < len; i, hex = 2) {
sscanf(hex, "%2hhx", &bin[i]);
}
return bin;
}
char *bin2hex(const uint8_t *bin, size_t length) {
char *hex = malloc(2*length 1);
char *saved = hex;
for (int i=0; i<length;i ,hex =2) {
sprintf(hex, "X",bin[i]);
}
return saved;
}
struct ChatHist ** get_current_histp(void) {
if (TalkingTo == TALK_TYPE_NULL) return NULL;
uint32_t num = INDEX_TO_NUM(TalkingTo);
switch (INDEX_TO_TYPE(TalkingTo)) {
case TALK_TYPE_FRIEND: {
struct Friend *f = getfriend(num);
if (f) return &f->hist;
break;
}
case TALK_TYPE_GROUP: {
struct Group *cf = getgroup(num);
if (cf) return &cf->hist;
break;
}
}
return NULL;
}
/*******************************************************************************
*
* Async REPL
*
******************************************************************************/
struct AsyncREPL {
char *line;
char *prompt;
size_t sz;
int nbuf;
int nstack;
};
struct termios saved_tattr;
struct AsyncREPL *async_repl;
void arepl_exit(void) {
tcsetattr(NEW_STDIN_FILENO, TCSAFLUSH, &saved_tattr);
}
void setup_arepl(void) {
if (!(isatty(STDIN_FILENO) && isatty(STDOUT_FILENO))) {
fputs("! stdout & stdin should be connected to tty", stderr);
exit(1);
}
async_repl = malloc(sizeof(struct AsyncREPL));
async_repl->nbuf = 0;
async_repl->nstack = 0;
async_repl->sz = LINE_MAX_SIZE;
async_repl->line = malloc(LINE_MAX_SIZE);
async_repl->prompt = malloc(LINE_MAX_SIZE);
strcpy(async_repl->prompt, CMD_PROMPT);
// stdin and stdout may share the same file obj,
// reopen stdin to avoid accidentally getting stdout modified.
char stdin_path[4080]; // 4080 is large enough for a path length for *nix system.
#ifdef F_GETPATH // macosx
if (fcntl(STDIN_FILENO, F_GETPATH, stdin_path) == -1) {
fputs("! fcntl get stdin filepath failed", stderr);
exit(1);
}
#else // linux
if (readlink("/proc/self/fd/0", stdin_path, sizeof(stdin_path)) == -1) {
fputs("! get stdin filename failed", stderr);
exit(1);
}
#endif
NEW_STDIN_FILENO = open(stdin_path, O_RDONLY);
if (NEW_STDIN_FILENO == -1) {
fputs("! reopen stdin failed",stderr);
exit(1);
}
close(STDIN_FILENO);
// Set stdin to Non-Blocking
int flags = fcntl(NEW_STDIN_FILENO, F_GETFL, 0);
fcntl(NEW_STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
/* Set stdin to Non-Canonical terminal mode. */
struct termios tattr;
tcgetattr(NEW_STDIN_FILENO, &tattr);
saved_tattr = tattr; // save it to restore when exit
tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON. */
tattr.c_cc[VMIN] = 1;
tattr.c_cc[VTIME] = 0;
tcsetattr(NEW_STDIN_FILENO, TCSAFLUSH, &tattr);
atexit(arepl_exit);
}
void arepl_reprint(struct AsyncREPL *arepl) {
fputs(CODE_ERASE_LINE, stdout);
if (arepl->prompt) fputs(arepl->prompt, stdout);
if (arepl->nbuf > 0) printf("%.*s", arepl->nbuf, arepl->line);
if (arepl->nstack > 0) {
printf("%.*s",(int)arepl->nstack, arepl->line arepl->sz - arepl->nstack);
printf("\033[�",arepl->nstack); // move cursor
}
fflush(stdout);
}
#define _AREPL_CURSOR_LEFT() arepl->line[arepl->sz - ( arepl->nstack)] = arepl->line[--arepl->nbuf]
#define _AREPL_CURSOR_RIGHT() arepl->line[arepl->nbuf ] = arepl->line[arepl->sz - (arepl->nstack--)]
int arepl_readline(struct AsyncREPL *arepl, char c, char *line, size_t sz){
static uint32_t escaped = 0;
if (c == '\033') { // mark escape code
escaped = 1;
return 0;
}
if (escaped>0) escaped ;
switch (c) {
case '\n': {
int ret = snprintf(line, sz, "%.*s%.*s\n",(int)arepl->nbuf, arepl->line, (int)arepl->nstack, arepl->line arepl->sz - arepl->nstack);
arepl->nbuf = 0;
arepl->nstack = 0;
return ret;
}
case '\010': // C-h
case '\177': // Backspace
if (arepl->nbuf > 0) arepl->nbuf--;
break;
case '\025': // C-u
arepl->nbuf = 0;
break;
case '\013': // C-k Vertical Tab
arepl->nstack = 0;
break;
case '\001': // C-a
while (arepl->nbuf > 0) _AREPL_CURSOR_LEFT();
break;
case '\005': // C-e
while (arepl->nstack > 0) _AREPL_CURSOR_RIGHT();
break;
case '\002': // C-b
if (arepl->nbuf > 0) _AREPL_CURSOR_LEFT();
break;
case '\006': // C-f
if (arepl->nstack > 0) _AREPL_CURSOR_RIGHT();
break;
case '\027': // C-w: backward delete a word
while (arepl->nbuf>0 && arepl->line[arepl->nbuf-1] == ' ') arepl->nbuf--;
while (arepl->nbuf>0 && arepl->line[arepl->nbuf-1] != ' ') arepl->nbuf--;
break;
case 'D':
case 'C':
if (escaped == 3 && arepl->nbuf >= 1 && arepl->line[arepl->nbuf-1] == '[') { // arrow keys
arepl->nbuf--;
if (c == 'D' && arepl->nbuf > 0) _AREPL_CURSOR_LEFT(); // left arrow: \033[D
if (c == 'C' && arepl->nstack > 0) _AREPL_CURSOR_RIGHT(); // right arrow: \033[C
break;
}
// fall through to default case
default:
arepl->line[arepl->nbuf ] = c;
}
return 0;
}
/*******************************************************************************
*
* Tox Callbacks
*
******************************************************************************/
void friend_message_cb(Tox *tox, uint32_t friend_num, TOX_MESSAGE_TYPE type, const uint8_t *message,
size_t length, void *user_data)
{
struct Friend *f = getfriend(friend_num);
if (!f) return;
if (type != TOX_MESSAGE_TYPE_NORMAL) {
INFO("* receive MESSAGE ACTION type from %s, no supported", f->name);
return;
}
char *msg = genmsg(&f->hist, GUEST_MSG_PREFIX "%.*s", getftime(), f->name, (int)length, (char*)message);
if (GEN_INDEX(friend_num, TALK_TYPE_FRIEND) == TalkingTo) {
PRINT("%s", msg);
} else {
INFO("* receive message from %s, use `/go <contact_index>` to talk\n",f->name);
}
}
void friend_name_cb(Tox *tox, uint32_t friend_num, const uint8_t *name, size_t length, void *user_data) {
struct Friend *f = getfriend(friend_num);
if (f) {
f->name = realloc(f->name, length 1);
sprintf(f->name, "%.*s", (int)length, (char*)name);
if (GEN_INDEX(friend_num, TALK_TYPE_FRIEND) == TalkingTo) {
INFO("* Opposite changed name to %.*s", (int)length, (char*)name)
sprintf(async_repl->prompt, FRIEND_TALK_PROMPT, f->name);
}
}
}
void friend_status_message_cb(Tox *tox, uint32_t friend_num, const uint8_t *message, size_t length, void *user_data) {
struct Friend *f = getfriend(friend_num);
if (f) {
f->status_message = realloc(f->status_message, length 1);
sprintf(f->status_message, "%.*s",(int)length, (char*)message);
}
}
void friend_connection_status_cb(Tox *tox, uint32_t friend_num, TOX_CONNECTION connection_status, void *user_data)
{
struct Friend *f = getfriend(friend_num);
if (f) {
f->connection = connection_status;
INFO("* %s is %s", f->name, connection_enum2text(connection_status));
}
}
void friend_request_cb(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, void *user_data) {
INFO("* receive friend request(use `/accept` to see).");
struct Request *req = malloc(sizeof(struct Request));
req->id = 1 ((requests != NULL) ? requests->id : 0);
req->is_friend_request = true;
memcpy(req->userdata.friend.pubkey, public_key, TOX_PUBLIC_KEY_SIZE);
req->msg = malloc(length 1);
sprintf(req->msg, "%.*s", (int)length, (char*)message);
req->next = requests;
requests = req;
}
void self_connection_status_cb(Tox *tox, TOX_CONNECTION connection_status, void *user_data)
{
self.connection = connection_status;
INFO("* You are %s", connection_enum2text(connection_status));
}
void group_invite_cb(Tox *tox, uint32_t friend_num, TOX_CONFERENCE_TYPE type, const uint8_t *cookie, size_t length, void *user_data) {
struct Friend *f = getfriend(friend_num);
if (f) {
if (type == TOX_CONFERENCE_TYPE_AV) {
WARN("* %s invites you to an AV group, which has not been supported.", f->name);
return;
}
INFO("* %s invites you to a group(try `/accept` to see)",f->name);
struct Request *req = malloc(sizeof(struct Request));
req->id = 1 ((requests != NULL) ? requests->id : 0);
req->next = requests;
requests = req;
req->is_friend_request = false;
req->userdata.group.cookie = malloc(length);
memcpy(req->userdata.group.cookie, cookie, length),
req->userdata.group.length = length;
req->userdata.group.friend_num = friend_num;
int sz = snprintf(NULL, 0, "%s%s", "From ", f->name);
req->msg = malloc(sz 1);
sprintf(req->msg, "%s%s", "From ", f->name);
}
}
void group_title_cb(Tox *tox, uint32_t group_num, uint32_t peer_number, const uint8_t *title, size_t length, void *user_data) {
struct Group *cf = getgroup(group_num);
if (cf) {
cf->title = realloc(cf->title, length 1);
sprintf(cf->title, "%.*s", (int)length, (char*)title);
if (GEN_INDEX(group_num, TALK_TYPE_GROUP) == TalkingTo) {
INFO("* Group title changed to %s", cf->title);
sprintf(async_repl->prompt, GROUP_TALK_PROMPT, cf->title);
}
}
}
void group_message_cb(Tox *tox, uint32_t group_num, uint32_t peer_number, TOX_MESSAGE_TYPE type, const uint8_t *message, size_t length, void *user_data) {
struct Group *cf = getgroup(group_num);
if (!cf) return;
if (tox_conference_peer_number_is_ours(tox, group_num, peer_number, NULL)) return;
if (type != TOX_MESSAGE_TYPE_NORMAL) {
INFO("* receive MESSAGE ACTION type from group %s, no supported", cf->title);
return;
}
if (peer_number >= cf->peers_count) {
ERROR("! Unknown peer_number, peer_count:%zu, peer_number:%u", cf->peers_count, peer_number);
return;
}
struct GroupPeer *peer = &cf->peers[peer_number];
char *msg = genmsg(&cf->hist, GUEST_MSG_PREFIX "%.*s", getftime(), peer->name, (int)length, (char*)message);
if (GEN_INDEX(group_num, TALK_TYPE_GROUP) == TalkingTo) {
PRINT("%s", msg);
} else {
INFO("* receive group message from %s, in group %s",peer->name, cf->title);
}
}
void group_peer_list_changed_cb(Tox *tox, uint32_t group_num, void *user_data) {
struct Group *cf = getgroup(group_num);
if (!cf) return;
TOX_ERR_CONFERENCE_PEER_QUERY err;
uint32_t count = tox_conference_peer_count(tox, group_num, &err);
if (err != TOX_ERR_CONFERENCE_PEER_QUERY_OK) {
ERROR("get group peer count failed, errcode:%d",err);
return;
}
if (cf->peers) free(cf->peers);
cf->peers = calloc(count, sizeof(struct GroupPeer));
cf->peers_count = count;
for (int i=0;i<count;i ) {
struct GroupPeer *p = cf->peers i;
tox_conference_peer_get_name(tox, group_num, i, (uint8_t*)p->name, NULL);
tox_conference_peer_get_public_key(tox, group_num, i, p->pubkey,NULL);
}
}
void group_peer_name_cb(Tox *tox, uint32_t group_num, uint32_t peer_num, const uint8_t *name, size_t length, void *user_data) {
struct Group *cf = getgroup(group_num);
if (!cf || peer_num >= cf->peers_count) {
ERROR("! Unexpected group_num/peer_num in group_peer_name_cb");
return;
}
struct GroupPeer *p = &cf->peers[peer_num];
sprintf(p->name, "%.*s", (int)length, (char*)name);
}
/*******************************************************************************
*
* Tox Setup
*
******************************************************************************/
void create_tox(void)
{
struct Tox_Options *options = tox_options_new(NULL);
tox_options_set_start_port(options, PORT_RANGE_START);
tox_options_set_end_port(options, PORT_RANGE_END);
if (savedata_filename) {
FILE *f = fopen(savedata_filename, "rb");
if (f) {
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char *savedata = malloc(fsize);
fread(savedata, fsize, 1, f);
fclose(f);
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(options, (uint8_t*)savedata, fsize);
tox = tox_new(options, NULL);
free(savedata);
}
}
if (!tox) tox = tox_new(options, NULL);
tox_options_free(options);
}
void init_friends(void) {
size_t sz = tox_self_get_friend_list_size(tox);
uint32_t *friend_list = malloc(sizeof(uint32_t) * sz);
tox_self_get_friend_list(tox, friend_list);
size_t len;
for (int i = 0;i<sz;i ) {
uint32_t friend_num = friend_list[i];
struct Friend *f = addfriend(friend_num);
len = tox_friend_get_name_size(tox, friend_num, NULL) 1;
f->name = calloc(1, len);
tox_friend_get_name(tox, friend_num, (uint8_t*)f->name, NULL);
len = tox_friend_get_status_message_size(tox, friend_num, NULL) 1;
f->status_message = calloc(1, len);
tox_friend_get_status_message(tox, friend_num, (uint8_t*)f->status_message, NULL);
tox_friend_get_public_key(tox, friend_num, f->pubkey, NULL);
}
free(friend_list);
// add self
self.friend_num = TALK_TYPE_NULL;
len = tox_self_get_name_size(tox) 1;
self.name = calloc(1, len);
tox_self_get_name(tox, (uint8_t*)self.name);
len = tox_self_get_status_message_size(tox) 1;
self.status_message = calloc(1, len);
tox_self_get_status_message(tox, (uint8_t*)self.status_message);
tox_self_get_public_key(tox, self.pubkey);
}
void update_savedata_file(void)
{
if (!(savedata_filename && savedata_tmp_filename)) return;
size_t size = tox_get_savedata_size(tox);
char *savedata = malloc(size);
tox_get_savedata(tox, (uint8_t*)savedata);
FILE *f = fopen(savedata_tmp_filename, "wb");
fwrite(savedata, size, 1, f);
fclose(f);
rename(savedata_tmp_filename, savedata_filename);
free(savedata);
}
void bootstrap(void)
{
for (size_t i = 0; i < sizeof(bootstrap_nodes)/sizeof(struct DHT_node); i ) {
uint8_t *bin = hex2bin(bootstrap_nodes[i].key_hex);
tox_bootstrap(tox, bootstrap_nodes[i].ip, bootstrap_nodes[i].port, bin, NULL);
free(bin);
}
}
void setup_tox(void)
{
create_tox();
init_friends();
bootstrap();
////// register callbacks
// self
tox_callback_self_connection_status(tox, self_connection_status_cb);
// friend
tox_callback_friend_request(tox, friend_request_cb);
tox_callback_friend_message(tox, friend_message_cb);
tox_callback_friend_name(tox, friend_name_cb);
tox_callback_friend_status_message(tox, friend_status_message_cb);
tox_callback_friend_connection_status(tox, friend_connection_status_cb);
// group
tox_callback_conference_invite(tox, group_invite_cb);
tox_callback_conference_title(tox, group_title_cb);
tox_callback_conference_message(tox, group_message_cb);
tox_callback_conference_peer_list_changed(tox, group_peer_list_changed_cb);
tox_callback_conference_peer_name(tox, group_peer_name_cb);
}
/*******************************************************************************
*
* Commands
*
******************************************************************************/
void command_help(int narg, char **args);
void command_guide(int narg, char **args) {
PRINT("This program is an minimal workable implementation of Tox client.");
PRINT("As it pursued simplicity at the cost of robustness and efficiency,");
PRINT("It should only be used for learning or playing with, instead of daily use.\n");
PRINT("Commands are any input lines with leading `/`,");
PRINT("Command args are seprated by blanks,");
PRINT("while some special commands may accept any-character string, like `/setname` and `/setstmsg`.\n");
PRINT("Use `/setname <YOUR NAME>` to set your name");
PRINT("Use `/info` to see your Name, Tox Id and Network Connection.");
PRINT("Use `/contacts` to list friends and groups, and use `/go <TARGET>` to talk to one of them.");
PRINT("Finally, use `/help` to get a list of available commands.\n");
PRINT("HAVE FUN!\n")
}
void _print_friend_info(struct Friend *f, bool is_self) {
PRINT("%-15s%s", "Name:", f->name);
if (is_self) {
uint8_t tox_id_bin[TOX_ADDRESS_SIZE];
tox_self_get_address(tox, tox_id_bin);
char *hex = bin2hex(tox_id_bin, sizeof(tox_id_bin));
PRINT("%-15s%s","Tox ID:", hex);
free(hex);
}
char *hex = bin2hex(f->pubkey, sizeof(f->pubkey));
PRINT("%-15s%s","Public Key:", hex);
free(hex);
PRINT("%-15s%s", "Status Msg:",f->status_message);
PRINT("%-15s%s", "Network:",connection_enum2text(f->connection));
}
void command_info(int narg, char **args) {
if (narg == 0) { // self
_print_friend_info(&self, true);
return;
}
uint32_t contact_idx;
if (!str2uint(args[0],&contact_idx)) goto FAIL;
uint32_t num = INDEX_TO_NUM(contact_idx);
switch (INDEX_TO_TYPE(contact_idx)) {
case TALK_TYPE_FRIEND: {
struct Friend *f = getfriend(num);
if (f) {
_print_friend_info(f, false);
return;
}
break;
}
case TALK_TYPE_GROUP: {
struct Group *cf = getgroup(num);
if (cf) {
PRINT("GROUP TITLE:\t%s",cf->title);
PRINT("PEER COUNT:\t%zu", cf->peers_count);
PRINT("Peers:");
for (int i=0;i<cf->peers_count;i ){
PRINT("\t%s",cf->peers[i].name);
}
return;
}
break;
}
}
FAIL:
WARN("^ Invalid contact index");
}
void command_setname(int narg, char **args) {
char *name = args[0];
size_t len = strlen(name);
TOX_ERR_SET_INFO err;
tox_self_set_name(tox, (uint8_t*)name, strlen(name), &err);
if (err != TOX_ERR_SET_INFO_OK) {
ERROR("! set name failed, errcode:%d", err);
return;
}
self.name = realloc(self.name, len 1);
strcpy(self.name, name);
}
void command_setstmsg(int narg, char **args) {
char *status = args[0];
size_t len = strlen(status);
TOX_ERR_SET_INFO err;
tox_self_set_status_message(tox, (uint8_t*)status, strlen(status), &err);
if (err != TOX_ERR_SET_INFO_OK) {
ERROR("! set status message failed, errcode:%d", err);
return;
}
self.status_message = realloc(self.status_message, len 1);
strcpy(self.status_message, status);
}
void command_add(int narg, char **args) {
char *hex_id = args[0];
char *msg = "";
if (narg > 1) msg = args[1];
uint8_t *bin_id = hex2bin(hex_id);
TOX_ERR_FRIEND_ADD err;
uint32_t friend_num = tox_friend_add(tox, bin_id, (uint8_t*)msg, strlen(msg), &err);
free(bin_id);
if (err != TOX_ERR_FRIEND_ADD_OK) {
ERROR("! add friend failed, errcode:%d",err);
return;
}
addfriend(friend_num);
}
void command_del(int narg, char **args) {
uint32_t contact_idx;
if (!str2uint(args[0], &contact_idx)) goto FAIL;
uint32_t num = INDEX_TO_NUM(contact_idx);
switch (INDEX_TO_TYPE(contact_idx)) {
case TALK_TYPE_FRIEND:
if (delfriend(num)) {
tox_friend_delete(tox, num, NULL);
return;
}
break;
case TALK_TYPE_GROUP:
if (delgroup(num)) {
tox_conference_delete(tox, num, NULL);
return;
}
break;
}
FAIL:
WARN("^ Invalid contact index");
}
void command_contacts(int narg, char **args) {
struct Friend *f = friends;
PRINT("#Friends(conctact_index|name|connection|status message):\n");
for (;f != NULL; f = f->next) {
PRINT("= .15s .12s %s",GEN_INDEX(f->friend_num, TALK_TYPE_FRIEND), f->name, connection_enum2text(f->connection), f->status_message);
}
struct Group *cf = groups;
PRINT("\n#Groups(contact_index|count of peers|name):\n");
for (;cf != NULL; cf = cf->next) {
PRINT("= d %s",GEN_INDEX(cf->group_num, TALK_TYPE_GROUP), tox_conference_peer_count(tox, cf->group_num, NULL), cf->title);
}
}
void command_save(int narg, char **args) {
update_savedata_file();
}
void command_go(int narg, char **args) {
if (narg == 0) {
TalkingTo = TALK_TYPE_NULL;
strcpy(async_repl->prompt, CMD_PROMPT);
return;
}
uint32_t contact_idx;
if (!str2uint(args[0], &contact_idx)) goto FAIL;
uint32_t num = INDEX_TO_NUM(contact_idx);
switch (INDEX_TO_TYPE(contact_idx)) {
case TALK_TYPE_FRIEND: {
struct Friend *f = getfriend(num);
if (f) {
TalkingTo = contact_idx;
sprintf(async_repl->prompt, FRIEND_TALK_PROMPT, f->name);
return;
}
break;
}
case TALK_TYPE_GROUP: {
struct Group *cf = getgroup(num);
if (cf) {
TalkingTo = contact_idx;
sprintf(async_repl->prompt, GROUP_TALK_PROMPT, cf->title);
return;
}
break;
}
}
FAIL:
WARN("^ Invalid contact index");
}
void command_history(int narg, char **args) {