forked from JFreegman/toxic
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgroupchats.c
More file actions
2379 lines (1823 loc) · 68.6 KB
/
groupchats.c
File metadata and controls
2379 lines (1823 loc) · 68.6 KB
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
/* groupchats.c
*
* Copyright (C) 2020-2024 Toxic All Rights Reserved.
*
* This file is part of Toxic. Toxic is free software licensed
* under the GNU General Public License 3.0.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* needed for strcasestr() and wcswidth() */
#endif
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
#include <unistd.h>
#include <inttypes.h>
#ifdef AUDIO
#ifdef __APPLE__
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#else
#include <AL/al.h>
#include <AL/alc.h>
/* compatibility with older versions of OpenAL */
#ifndef ALC_ALL_DEVICES_SPECIFIER
#include <AL/alext.h>
#endif /* ALC_ALL_DEVICES_SPECIFIER */
#endif /* __APPLE__ */
#endif /* AUDIO */
#include "windows.h"
#include "toxic.h"
#include "execute.h"
#include "misc_tools.h"
#include "groupchats.h"
#include "toxic_strings.h"
#include "log.h"
#include "line_info.h"
#include "settings.h"
#include "input.h"
#include "help.h"
#include "notify.h"
#include "autocomplete.h"
#include "audio_device.h"
extern char *DATA_FILE;
static int max_groupchat_index = 0;
extern struct Winthread Winthread;
#define GROUP_SIDEBAR_OFFSET 3 /* Offset for the peer number box at the top of the statusbar */
static_assert(TOX_GROUP_CHAT_ID_SIZE == TOX_PUBLIC_KEY_SIZE, "TOX_GROUP_CHAT_ID_SIZE != TOX_PUBLIC_KEY_SIZE");
/* groupchat command names used for tab completion. */
static const char *const group_cmd_list[] = {
"/accept",
"/add",
"/avatar",
"/chatid",
"/clear",
"/close",
"/color",
"/conference",
"/connect",
"/disconnect",
"/decline",
"/exit",
"/group",
"/help",
"/ignore",
"/join",
"/kick",
"/list",
"/locktopic",
"/log",
"/mod",
"/myid",
#ifdef QRCODE
"/myqr",
#endif /* QRCODE */
"/nick",
"/note",
"/passwd",
"/nospam",
"/peerlimit",
"/privacy",
"/quit",
"/rejoin",
"/requests",
#ifdef PYTHON
"/run",
#endif /* PYTHON */
"/silence",
"/status",
"/topic",
"/unignore",
"/unmod",
"/unsilence",
"/voice",
"/whisper",
"/whois",
};
static GroupChat groupchats[MAX_GROUPCHAT_NUM];
static ToxWindow *new_group_chat(Tox *tox, uint32_t groupnumber, const char *groupname, int length);
static void groupchat_set_group_name(ToxWindow *self, Toxic *toxic, uint32_t groupnumber);
static void groupchat_update_name_list(uint32_t groupnumber);
static void groupchat_onGroupPeerJoin(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, uint32_t peer_id);
static int realloc_peer_list(uint32_t groupnumber, uint32_t n);
static void groupchat_onGroupNickChange(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, uint32_t peer_id,
const char *new_nick, size_t len);
static void groupchat_onGroupStatusChange(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, uint32_t peer_id,
TOX_USER_STATUS status);
static void groupchat_onGroupSelfNickChange(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, const char *old_nick,
size_t old_length, const char *new_nick, size_t length);
static void ignore_list_cleanup(GroupChat *chat);
/*
* Return a GroupChat pointer associated with groupnumber.
* Return NULL if groupnumber is invalid.
*/
GroupChat *get_groupchat(uint32_t groupnumber)
{
for (size_t i = 0; i < max_groupchat_index; ++i) {
if (!groupchats[i].active) {
continue;
}
if (groupchats[i].groupnumber == groupnumber) {
return &groupchats[i];
}
}
return NULL;
}
/*
* Return the groupnumber associated with `public_key`.
* Return -1 if public_key does not designate a valid group.
*
* `public_key` must be a string of at least TOX_PUBLIC_KEY_SIZE * 2 chars in length.
*/
static int get_groupnumber_by_public_key_string(const char *public_key)
{
char pk_bin[TOX_PUBLIC_KEY_SIZE];
if (tox_pk_string_to_bytes(public_key, strlen(public_key), pk_bin, sizeof(pk_bin)) != 0) {
return -1;
}
for (size_t i = 0; i < max_groupchat_index; ++i) {
const GroupChat *chat = &groupchats[i];
if (!chat->active) {
continue;
}
if (memcmp(pk_bin, chat->chat_id, TOX_PUBLIC_KEY_SIZE) == 0) {
return chat->groupnumber;
}
}
return -1;
}
static const char *get_group_exit_string(Tox_Group_Exit_Type exit_type)
{
switch (exit_type) {
case TOX_GROUP_EXIT_TYPE_QUIT:
return "Quit";
case TOX_GROUP_EXIT_TYPE_TIMEOUT:
return "Connection timed out";
case TOX_GROUP_EXIT_TYPE_DISCONNECTED:
return "Disconnected";
case TOX_GROUP_EXIT_TYPE_KICK:
return "Kicked";
case TOX_GROUP_EXIT_TYPE_SYNC_ERROR:
return "Sync error";
default:
return "Unknown error";
}
}
static void clear_peer(GroupPeer *peer)
{
*peer = (GroupPeer) {
0
};
}
void groupchat_rejoin(ToxWindow *self, Toxic *toxic)
{
const Client_Config *c_config = toxic->c_config;
GroupChat *chat = get_groupchat(self->num);
if (chat == NULL) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch GroupChat object.");
return;
}
Tox_Err_Group_Self_Query s_err;
const uint32_t self_peer_id = tox_group_self_get_peer_id(toxic->tox, self->num, &s_err);
if (s_err != TOX_ERR_GROUP_SELF_QUERY_OK) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0,
"Failed to fetch self peer_id in groupchat_rejoin()");
return;
}
for (uint32_t i = 0; i < chat->max_idx; ++i) {
clear_peer(&chat->peer_list[i]);
}
chat->num_peers = 0;
chat->max_idx = 0;
realloc_peer_list(self->num, 0);
groupchat_onGroupPeerJoin(self, toxic, self->num, self_peer_id);
}
static void kill_groupchat_window(ToxWindow *self, Windows *windows, const Client_Config *c_config)
{
if (self == NULL) {
return;
}
ChatContext *ctx = self->chatwin;
StatusBar *statusbar = self->stb;
if (ctx != NULL) {
log_disable(ctx->log);
line_info_cleanup(ctx->hst);
delwin(ctx->linewin);
delwin(ctx->history);
delwin(ctx->sidebar);
free(ctx->log);
free(ctx);
}
delwin(statusbar->topline);
free(statusbar);
free(self->help);
kill_notifs(self->active_box);
del_window(self, windows, c_config);
}
/* Closes groupchat window and cleans up. */
static void close_groupchat(ToxWindow *self, Toxic *toxic, uint32_t groupnumber)
{
GroupChat *chat = get_groupchat(groupnumber);
if (chat == NULL) {
return;
}
ignore_list_cleanup(chat);
realloc_peer_list(groupnumber, 0);
free_ptr_array((void **) chat->name_list);
*chat = (GroupChat) {
0
};
int i;
for (i = max_groupchat_index; i > 0; --i) {
if (groupchats[i - 1].active) {
break;
}
}
max_groupchat_index = i;
kill_groupchat_window(self, toxic->windows, toxic->c_config);
}
void exit_groupchat(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, const char *partmessage, size_t length)
{
if (length > TOX_GROUP_MAX_PART_LENGTH) {
length = TOX_GROUP_MAX_PART_LENGTH;
}
tox_group_leave(toxic->tox, groupnumber, (const uint8_t *) partmessage, length, NULL);
if (self != NULL) {
close_groupchat(self, toxic, groupnumber);
}
}
/*
* Initializes groupchat log. This should only be called after we have the group name.
*/
static void init_groupchat_log(ToxWindow *self, Toxic *toxic, uint32_t groupnumber)
{
if (toxic == NULL || self == NULL) {
return;
}
Tox *tox = toxic->tox;
const Client_Config *c_config = toxic->c_config;
GroupChat *chat = get_groupchat(groupnumber);
if (chat == NULL) {
return;
}
ChatContext *ctx = self->chatwin;
char my_id[TOX_ADDRESS_SIZE];
tox_self_get_address(tox, (uint8_t *) my_id);
char chat_id[TOX_GROUP_CHAT_ID_SIZE];
Tox_Err_Group_State_Query err;
if (!tox_group_get_chat_id(tox, groupnumber, (uint8_t *)chat_id, &err)) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0,
"Failed to fetch chat id. Logging disabled. (error: %d)", err);
return;
}
if (log_init(ctx->log, c_config, chat->group_name, my_id, chat_id, LOG_TYPE_CHAT) != 0) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize.");
return;
}
if (load_chat_history(ctx->log, self, c_config) != 0) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to load chat history.");
}
if (c_config->autolog) {
if (log_enable(ctx->log) != 0) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to enable chat log.");
}
}
}
/* Creates a new toxic groupchat window associated with groupnumber.
*
* Returns 0 on success.
* Returns -1 on general failure.
* Returns -2 if the groupnumber is already in use. This usually means that the client has
* been kicked and needs to close the chat window before opening a new one.
*/
int init_groupchat_win(Toxic *toxic, uint32_t groupnumber, const char *groupname, size_t length,
Group_Join_Type join_type)
{
if (toxic == NULL) {
return -1;
}
Tox *tox = toxic->tox;
ToxWindow *self = new_group_chat(tox, groupnumber, groupname, length);
for (int i = 0; i <= max_groupchat_index; ++i) {
if (!groupchats[i].active) {
if (i == max_groupchat_index) {
++max_groupchat_index;
}
groupchats[i].active = true;
groupchats[i].groupnumber = groupnumber;
groupchats[i].num_peers = 0;
groupchats[i].time_connected = get_unix_time();
const int window_id = add_window(toxic, self);
if (window_id < 0) {
fprintf(stderr, "Failed to create new groupchat window\n");
close_groupchat(self, toxic, groupnumber);
return -1;
}
groupchats[i].window_id = window_id;
if (!tox_group_get_chat_id(tox, groupnumber, (uint8_t *) groupchats[i].chat_id, NULL)) {
fprintf(stderr, "Failed to fetch new groupchat ID\n");
close_groupchat(self, toxic, groupnumber);
return -1;
}
set_active_window_by_id(toxic->windows, groupchats[i].window_id);
store_data(toxic);
Tox_Err_Group_Self_Query err;
const uint32_t peer_id = tox_group_self_get_peer_id(tox, groupnumber, &err);
if (err != TOX_ERR_GROUP_SELF_QUERY_OK) {
close_groupchat(self, toxic, groupnumber);
return -1;
}
if (join_type == Group_Join_Type_Create || join_type == Group_Join_Type_Load) {
groupchat_set_group_name(self, toxic, groupnumber);
}
groupchat_onGroupPeerJoin(self, toxic, groupnumber, peer_id);
return 0;
}
}
kill_groupchat_window(self, toxic->windows, toxic->c_config);
return -1;
}
void groupchat_update_statusbar_topic(ToxWindow *self, const Tox *tox)
{
StatusBar *statusbar = self->stb;
char topic[TOX_GROUP_MAX_TOPIC_LENGTH + 1] = {0};
const size_t t_len = tox_group_get_topic_size(tox, self->num, NULL);
if (t_len > TOX_GROUP_MAX_TOPIC_LENGTH) {
return;
}
tox_group_get_topic(tox, self->num, (uint8_t *)topic, NULL);
topic[t_len] = '\0';
filter_string(topic, t_len, false);
snprintf(statusbar->topic, sizeof(statusbar->topic), "%s", topic);
statusbar->topic_len = strlen(statusbar->topic);
}
void set_nick_this_group(ToxWindow *self, Toxic *toxic, const char *new_nick, size_t length)
{
if (self == NULL) {
return;
}
Tox *tox = toxic->tox;
const Client_Config *c_config = toxic->c_config;
char old_nick[TOX_MAX_NAME_LENGTH + 1];
size_t old_length = get_group_self_nick_truncate(tox, old_nick, self->num);
Tox_Err_Group_Self_Name_Set err;
tox_group_self_set_name(tox, self->num, (const uint8_t *) new_nick, length, &err);
GroupChat *chat = get_groupchat(self->num);
if (chat == NULL) {
line_info_add(self, c_config, false, NULL, 0, SYS_MSG, 0, RED, "-!- Failed to set nick: invalid groupnumber");
return;
}
switch (err) {
case TOX_ERR_GROUP_SELF_NAME_SET_OK: {
groupchat_onGroupSelfNickChange(self, toxic, self->num, old_nick, old_length, new_nick, length);
break;
}
default: {
if (chat->time_connected > 0) {
line_info_add(self, c_config, false, NULL, 0, SYS_MSG, 0, RED, "-!- Failed to set nick (error %d).", err);
}
break;
}
}
}
/* void set_nick_all_groups(Tox *tox, const char *new_nick, size_t length) */
/* { */
/* for (int i = 0; i < max_groupchat_index; ++i) { */
/* if (groupchats[i].active) { */
/* ToxWindow *self = get_window_pointer_by_id(groupchats[i].window_id); */
/* if (!self) { */
/* continue; */
/* } */
/* char old_nick[TOX_MAX_NAME_LENGTH + 1]; */
/* size_t old_length = get_group_self_nick_truncate(tox, old_nick, self->num); */
/* Tox_Err_Group_Self_Name_Set err; */
/* tox_group_self_set_name(tox, groupchats[i].groupnumber, (uint8_t *) new_nick, length, &err); */
/* switch (err) { */
/* case TOX_ERR_GROUP_SELF_NAME_SET_OK: { */
/* groupchat_onGroupSelfNickChange(self, tox, self->num, old_nick, old_length, new_nick, length); */
/* break; */
/* } */
/* default: { */
/* if (groupchats[i].time_connected > 0) { */
/* line_info_add(self, c_config, false, NULL, 0, SYS_MSG, 0, RED, "-!- Failed to set nick (error %d).", err); */
/* } */
/* break; */
/* } */
/* } */
/* } */
/* } */
/* } */
void set_status_all_groups(Toxic *toxic, uint8_t status)
{
for (int i = 0; i < max_groupchat_index; ++i) {
if (groupchats[i].active) {
ToxWindow *self = get_window_pointer_by_id(toxic->windows, groupchats[i].window_id);
if (self == NULL) {
continue;
}
Tox_Err_Group_Self_Query s_err;
const uint32_t self_peer_id = tox_group_self_get_peer_id(toxic->tox, self->num, &s_err);
if (s_err != TOX_ERR_GROUP_SELF_QUERY_OK) {
line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0,
"Failed to fetch self peer_id.");
continue;
}
if (tox_group_self_set_status(toxic->tox, self->num, (TOX_USER_STATUS) status, NULL)) {
groupchat_onGroupStatusChange(self, toxic, self->num, self_peer_id, (TOX_USER_STATUS) status);
}
}
}
}
/* Returns a weight for peer_sort_cmp based on the peer's role. */
#define PEER_CMP_BASE_WEIGHT 100000
static int peer_sort_cmp_weight(const struct GroupPeer *peer)
{
int w = PEER_CMP_BASE_WEIGHT;
if (peer->role == TOX_GROUP_ROLE_FOUNDER) {
w <<= 2;
} else if (peer->role == TOX_GROUP_ROLE_MODERATOR) {
w <<= 1;
} else if (peer->role == TOX_GROUP_ROLE_OBSERVER) {
w >>= 1;
}
return w;
}
static int peer_sort_cmp(const void *n1, const void *n2)
{
const struct GroupPeer *peer1 = (const struct GroupPeer *) n1;
const struct GroupPeer *peer2 = (const struct GroupPeer *) n2;
int res = qsort_strcasecmp_hlpr(peer1->name, peer2->name);
return res - peer_sort_cmp_weight(peer1) + peer_sort_cmp_weight(peer2);
}
/* Sorts the peer list, first by role, then by name. */
static void sort_peerlist(uint32_t groupnumber)
{
GroupChat *chat = get_groupchat(groupnumber);
if (!chat) {
return;
}
qsort(chat->peer_list, chat->max_idx, sizeof(struct GroupPeer), peer_sort_cmp);
}
/* Puts the peer_id associated with nick in `peer_id`.
*
* Returns 0 on success.
* Returns -1 if peer is not found.
* Returns -2 if there are more than one peers with nick.
*/
static int group_get_nick_peer_id(uint32_t groupnumber, const char *nick, uint32_t *peer_id)
{
GroupChat *chat = get_groupchat(groupnumber);
if (!chat) {
return -1;
}
if (nick == NULL) {
return -1;
}
size_t count = 0;
for (uint32_t i = 0; i < chat->max_idx; ++i) {
GroupPeer *peer = &chat->peer_list[i];
if (!peer->active) {
continue;
}
if (strcmp(nick, peer->name) == 0) {
if (++count > 1) {
return -2;
}
*peer_id = peer->peer_id;
}
}
return count > 0 ? 0 : -1;
}
/* Gets the peer_id associated with `public_key`.
*
* Returns 0 on success.
* Returns -1 on failure or if `public_key` is invalid.
*/
int group_get_public_key_peer_id(uint32_t groupnumber, const char *public_key, uint32_t *peer_id)
{
GroupChat *chat = get_groupchat(groupnumber);
if (!chat) {
return -1;
}
if (public_key == NULL) {
return -1;
}
if (strlen(public_key) < TOX_GROUP_PEER_PUBLIC_KEY_SIZE * 2) {
return -1;
}
char key_bin[TOX_GROUP_PEER_PUBLIC_KEY_SIZE];
if (tox_pk_string_to_bytes(public_key, TOX_GROUP_PEER_PUBLIC_KEY_SIZE * 2, key_bin, sizeof(key_bin)) == -1) {
return -1;
}
for (uint32_t i = 0; i < chat->max_idx; ++i) {
GroupPeer *peer = &chat->peer_list[i];
if (!peer->active) {
continue;
}
if (memcmp(key_bin, peer->public_key, TOX_GROUP_PEER_PUBLIC_KEY_SIZE) == 0) {
*peer_id = peer->peer_id;
return 0;
}
}
return -1;
}
/* Puts the peer_id associated with `identifier` in `peer_id`. The string may be
* either a nick or a public key.
*
* On failure, `peer_id` is set to (uint32_t)-1.
*
* This function is intended to be a helper for groupchat_commands.c and will print
* error messages to `self`.
* Return 0 on success.
* Return -1 if the identifier does not correspond with a peer in the group, or if
* the identifier is a nick which is being used by multiple peers.
*/
int group_get_peer_id_of_identifier(ToxWindow *self, const Client_Config *c_config,
const char *identifier, uint32_t *peer_id)
{
*peer_id = (uint32_t) -1;
if (group_get_public_key_peer_id(self->num, identifier, peer_id) == 0) {
return 0;
}
int ret = group_get_nick_peer_id(self->num, identifier, peer_id);
switch (ret) {
case 0: {
return 0;
}
case -1: {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name or public key.");
return -1;
}
case -2: {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0,
"More than one peer is using this name; specify the target's public key.");
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0,
"Use the /whois or /list command to determine the key.");
*peer_id = (uint32_t) -1;
return -1;
}
default:
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Unspecified error.");
return -1;
}
}
static void groupchat_update_last_seen(uint32_t groupnumber, uint32_t peer_id)
{
GroupChat *chat = get_groupchat(groupnumber);
if (!chat) {
return;
}
const int peer_index = get_peer_index(groupnumber, peer_id);
if (peer_index >= 0) {
chat->peer_list[peer_index].last_active = get_unix_time();
}
}
/* Returns the peerlist index of peer_id for groupnumber's group chat.
* Returns -1 on failure.
*/
int get_peer_index(uint32_t groupnumber, uint32_t peer_id)
{
GroupChat *chat = get_groupchat(groupnumber);
if (!chat) {
return -1;
}
for (uint32_t i = 0; i < chat->max_idx; ++i) {
if (!chat->peer_list[i].active) {
continue;
}
if (chat->peer_list[i].peer_id == peer_id) {
return i;
}
}
return -1;
}
/**
* Return true if `key` is in the ignored list.
*/
static bool peer_is_ignored(const GroupChat *chat, const uint8_t *key)
{
for (uint16_t i = 0; i < chat->num_ignored; ++i) {
if (memcmp(chat->ignored_list[i], key, TOX_GROUP_PEER_PUBLIC_KEY_SIZE) == 0) {
return true;
}
}
return false;
}
static bool ignore_list_add_key(GroupChat *chat, const uint8_t *key)
{
uint8_t **tmp_list = (uint8_t **)realloc(chat->ignored_list, (chat->num_ignored + 1) * sizeof(uint8_t *));
if (tmp_list == NULL) {
return false;
}
chat->ignored_list = tmp_list;
tmp_list[chat->num_ignored] = (uint8_t *)malloc(sizeof(uint8_t) * TOX_GROUP_PEER_PUBLIC_KEY_SIZE);
if (tmp_list[chat->num_ignored] == NULL) {
return false;
}
memcpy(tmp_list[chat->num_ignored], key, TOX_GROUP_PEER_PUBLIC_KEY_SIZE);
++chat->num_ignored;
return true;
}
static void ignore_list_cleanup(GroupChat *chat)
{
for (uint16_t i = 0; i < chat->num_ignored; ++i) {
if (chat->ignored_list[i] != NULL) {
free(chat->ignored_list[i]);
}
}
free(chat->ignored_list);
chat->ignored_list = NULL;
chat->num_ignored = 0;
}
static bool ignore_list_rm_key(GroupChat *chat, const uint8_t *key)
{
if (chat->num_ignored == 0) {
return false;
}
int32_t idx = -1;
for (uint16_t i = 0; i < chat->num_ignored; ++i) {
if (memcmp(chat->ignored_list[i], key, TOX_GROUP_PEER_PUBLIC_KEY_SIZE) == 0) {
idx = i;
break;
}
}
if (idx == -1) {
fprintf(stderr, "Key not found in ignore list\n");
return false;
}
if ((chat->num_ignored - 1) == 0) {
ignore_list_cleanup(chat);
return true;
}
--chat->num_ignored;
if (idx != chat->num_ignored) {
memcpy(chat->ignored_list[idx], chat->ignored_list[chat->num_ignored], TOX_GROUP_PEER_PUBLIC_KEY_SIZE);
}
free(chat->ignored_list[chat->num_ignored]);
uint8_t **tmp_list = realloc(chat->ignored_list, chat->num_ignored * sizeof(uint8_t *));
if (tmp_list == NULL) {
return false;
}
chat->ignored_list = tmp_list;
return true;
}
void group_toggle_peer_ignore(uint32_t groupnumber, int peer_id, bool ignore)
{
int peer_index = get_peer_index(groupnumber, peer_id);
if (peer_index < 0) {
fprintf(stderr, "Failed to find peer index (group_toggle_peer_ignore())\n");
return;
}
GroupChat *chat = get_groupchat(groupnumber);
if (!chat) {
return;
}
GroupPeer *peer = &chat->peer_list[peer_index];
peer->is_ignored = ignore;
bool ret;
if (ignore) {
ret = ignore_list_add_key(chat, peer->public_key);
} else {
ret = ignore_list_rm_key(chat, peer->public_key);
}
if (!ret) {
fprintf(stderr, "Client failed to modify ignore list\n");
}
}
static void groupchat_update_name_list(uint32_t groupnumber)
{
GroupChat *chat = get_groupchat(groupnumber);
if (!chat) {
return;
}
free_ptr_array((void **) chat->name_list);
chat->name_list = (char **) malloc_ptr_array(chat->num_peers, TOX_MAX_NAME_LENGTH + 1);
if (!chat->name_list) {
fprintf(stderr, "WARNING: Out of memory in groupchat_update_name_list()\n");
return;
}
uint32_t count = 0;
for (uint32_t i = 0; i < chat->max_idx; ++i) {
if (chat->peer_list[i].active) {
size_t length = chat->peer_list[i].name_length;
memcpy(chat->name_list[count], chat->peer_list[i].name, length);
chat->name_list[count][length] = 0;
++count;
}
}
sort_peerlist(groupnumber);
}
/* destroys and re-creates groupchat window */
void redraw_groupchat_win(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
StatusBar *statusbar = self->stb;
endwin();
refresh();
clear();
int x2;
int y2;
getmaxyx(self->window, y2, x2);
if (y2 <= 0 || x2 <= 0) {
return;
}
if (ctx->sidebar) {
delwin(ctx->sidebar);
ctx->sidebar = NULL;
}
delwin(ctx->linewin);
delwin(ctx->history);
delwin(self->window_bar);
delwin(self->window);
delwin(statusbar->topline);
self->window = newwin(y2, x2, 0, 0);
ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2 - CHATBOX_HEIGHT, 0);
self->window_bar = subwin(self->window, WINDOW_BAR_HEIGHT, x2, y2 - (CHATBOX_HEIGHT + WINDOW_BAR_HEIGHT), 0);
statusbar->topline = subwin(self->window, TOP_BAR_HEIGHT, x2, 0, 0);
if (self->show_peerlist) {
ctx->history = subwin(self->window, y2 - CHATBOX_HEIGHT - WINDOW_BAR_HEIGHT, x2 - SIDEBAR_WIDTH - 1, 0, 0);
ctx->sidebar = subwin(self->window, y2 - CHATBOX_HEIGHT - WINDOW_BAR_HEIGHT, SIDEBAR_WIDTH, 0, x2 - SIDEBAR_WIDTH);
} else {
ctx->history = subwin(self->window, y2 - CHATBOX_HEIGHT - WINDOW_BAR_HEIGHT, x2, 0, 0);
}
scrollok(ctx->history, 0);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
self->x = 0; // trigger the statusbar to be re-sized
if (!self->scroll_pause) {
line_info_reset_start(self, ctx->hst);
}
}
static void group_onAction(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, uint32_t peer_id, const char *action,
size_t len)
{
const Client_Config *c_config = toxic->c_config;
ChatContext *ctx = self->chatwin;
char nick[TOX_MAX_NAME_LENGTH + 1];
get_group_nick_truncate(toxic->tox, nick, peer_id, groupnumber);
char self_nick[TOX_MAX_NAME_LENGTH + 1];
get_group_self_nick_truncate(toxic->tox, self_nick, groupnumber);
if (strcasestr(action, self_nick)) {
if (self->active_box != -1) {
box_notify2(self, toxic, generic_message, NT_WNDALERT_0 | NT_NOFOCUS |
c_config->bell_on_message, self->active_box, "* %s %s", nick, action);
} else {
box_notify(self, toxic, generic_message, NT_WNDALERT_0 | NT_NOFOCUS |
c_config->bell_on_message, &self->active_box, self->name, "* %s %s", nick, action);
}
} else {
sound_notify(self, toxic, silent, NT_WNDALERT_1, NULL);
}
line_info_add(self, c_config, true, nick, NULL, IN_ACTION, 0, 0, "%s", action);
write_to_log(ctx->log, c_config, action, nick, LOG_HINT_ACTION);
}
static void groupchat_onGroupMessage(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, uint32_t peer_id,
TOX_MESSAGE_TYPE type, const char *msg, size_t len)
{
if (toxic == NULL || self == NULL) {
return;
}
Tox *tox = toxic->tox;
const Client_Config *c_config = toxic->c_config;
if (self->num != groupnumber || !get_groupchat(groupnumber)) {
return;
}
groupchat_update_last_seen(groupnumber, peer_id);
if (type == TOX_MESSAGE_TYPE_ACTION) {
group_onAction(self, toxic, groupnumber, peer_id, msg, len);