This repository was archived by the owner on Jan 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtwc-commands.c
More file actions
1630 lines (1416 loc) · 55.8 KB
/
twc-commands.c
File metadata and controls
1630 lines (1416 loc) · 55.8 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
/*
* Copyright (c) 2018 Håvard Pettersson <mail@haavard.me>
*
* This file is part of Tox-WeeChat.
*
* Tox-WeeChat is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tox-WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wordexp.h>
#include <tox/tox.h>
#include <weechat/weechat-plugin.h>
#include "twc-bootstrap.h"
#include "twc-chat.h"
#include "twc-config.h"
#include "twc-friend-request.h"
#include "twc-group-invite.h"
#include "twc-list.h"
#include "twc-profile.h"
#include "twc-tfer.h"
#include "twc-utils.h"
#include "twc.h"
#include "twc-commands.h"
enum TWC_FRIEND_MATCH
{
TWC_FRIEND_MATCH_AMBIGUOUS = -1,
TWC_FRIEND_MATCH_NOMATCH = -2,
};
/**
* Make sure a command is executed on a Tox profile buffer. If not, warn user
* and abort.
*/
#define TWC_CHECK_PROFILE(profile) \
if (!profile) \
{ \
weechat_printf(NULL, \
"%s%s: command \"%s\" must be executed on a Tox " \
"buffer", \
weechat_prefix("error"), weechat_plugin->name, \
argv[0]); \
return WEECHAT_RC_OK; \
}
/**
* Make sure a command is executed in a chat buffer. If not, warn user and
* abort.
*/
#define TWC_CHECK_CHAT(chat) \
if (!chat) \
{ \
weechat_printf(NULL, \
"%s%s: command \"%s\" must be executed in a chat " \
"buffer", \
weechat_prefix("error"), weechat_plugin->name, \
argv[0]); \
return WEECHAT_RC_OK; \
}
/**
* Make sure a command is executed in a group chat buffer. If not, warn user
* and abort.
*/
#define TWC_CHECK_GROUP_CHAT(chat) \
if (!chat || chat->group_number < 0) \
{ \
weechat_printf(NULL, \
"%s%s: command \"%s\" must be executed in a group " \
"chat buffer ", \
weechat_prefix("error"), weechat_plugin->name, \
argv[0]); \
return WEECHAT_RC_OK; \
}
/**
* Make sure a profile with the given name exists. If not, warn user and abort.
*/
#define TWC_CHECK_PROFILE_EXISTS(profile) \
if (!profile) \
{ \
weechat_printf(NULL, "%s%s: profile \"%s\" does not exist.", \
weechat_prefix("error"), weechat_plugin->name, name); \
return WEECHAT_RC_OK; \
}
/**
* Make sure a profile is loaded.
*/
#define TWC_CHECK_PROFILE_LOADED(profile) \
if (!(profile->tox)) \
{ \
weechat_printf(profile->buffer, \
"%sprofile must be loaded for command \"%s\"", \
weechat_prefix("error"), argv[0]); \
return WEECHAT_RC_OK; \
}
/**
* Make sure friend exists.
*/
#define TWC_CHECK_FRIEND_NUMBER(profile, number, string) \
if (number == TWC_FRIEND_MATCH_NOMATCH) \
{ \
weechat_printf(profile->buffer, \
"%sno friend number, name or Tox ID found matching " \
"\"%s\"", \
weechat_prefix("error"), string); \
return WEECHAT_RC_OK; \
} \
if (number == TWC_FRIEND_MATCH_AMBIGUOUS) \
{ \
weechat_printf(profile->buffer, \
"%smultiple friends with name \"%s\" found; please " \
"use Tox ID instead", \
weechat_prefix("error"), string); \
return WEECHAT_RC_OK; \
}
/**
* Make sure a file exists.
*/
#define TWC_CHECK_FILE_EXISTS(filename) \
if (access(filename, F_OK) == -1) \
{ \
weechat_printf(NULL, "%sFile \"%s\" does not exist", \
weechat_prefix("error"), filename); \
return WEECHAT_RC_ERROR; \
}
#define TWC_RETURN_WITH_FILE_ERROR(filename, type) \
weechat_printf(NULL, \
"%s\"%s\" must be a regular file or pipe, " \
"not a %s", \
weechat_prefix("error"), filename, type); \
return WEECHAT_RC_ERROR;
/**
* Get number of friend matching string. Tries to match number, name and
* Tox ID.
*/
enum TWC_FRIEND_MATCH
twc_match_friend(struct t_twc_profile *profile, const char *search_string)
{
uint32_t friend_count = tox_self_get_friend_list_size(profile->tox);
uint32_t *friend_numbers = malloc(sizeof(uint32_t) * friend_count);
tox_self_get_friend_list(profile->tox, friend_numbers);
int32_t match = TWC_FRIEND_MATCH_NOMATCH;
char *endptr;
uint32_t friend_number = (uint32_t)strtoul(search_string, &endptr, 10);
if (endptr == search_string + strlen(search_string) &&
tox_friend_exists(profile->tox, friend_number))
return friend_number;
size_t search_size = strlen(search_string);
for (uint32_t i = 0; i < friend_count; ++i)
{
if (search_size == TOX_PUBLIC_KEY_SIZE * 2)
{
uint8_t tox_id[TOX_PUBLIC_KEY_SIZE];
char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1];
if (tox_friend_get_public_key(profile->tox, friend_numbers[i],
tox_id, NULL))
{
twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id);
if (weechat_strcasecmp(hex_id, search_string) == 0)
return friend_numbers[i];
}
}
char *name = twc_get_name_nt(profile->tox, friend_numbers[i]);
if (weechat_strcasecmp(name, search_string) == 0)
{
if (match == TWC_FRIEND_MATCH_NOMATCH)
match = friend_numbers[i];
else
return TWC_FRIEND_MATCH_AMBIGUOUS;
}
}
return match;
}
/**
* Command /bootstrap callback.
*/
int
twc_cmd_bootstrap(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
struct t_twc_profile *profile = twc_profile_search_buffer(buffer);
TWC_CHECK_PROFILE(profile);
TWC_CHECK_PROFILE_LOADED(profile);
/* /bootstrap connect <address> <port> <key> */
if (argc == 5 && weechat_strcasecmp(argv[1], "connect") == 0)
{
char *address = argv[2];
uint16_t port = atoi(argv[3]);
char *public_key = argv[4];
if (!twc_bootstrap_tox(profile->tox, address, port, public_key))
{
weechat_printf(profile->buffer,
"%sBootstrap could not open address \"%s\"",
weechat_prefix("error"), address);
}
return WEECHAT_RC_OK;
}
return WEECHAT_RC_ERROR;
}
/**
* Command /friend callback.
*/
int
twc_cmd_friend(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
struct t_twc_profile *profile = twc_profile_search_buffer(buffer);
TWC_CHECK_PROFILE(profile);
TWC_CHECK_PROFILE_LOADED(profile);
/* /friend or /friend list */
if (argc == 1 || (argc == 2 && weechat_strcasecmp(argv[1], "list") == 0))
{
size_t friend_count = tox_self_get_friend_list_size(profile->tox);
uint32_t friend_numbers[friend_count];
tox_self_get_friend_list(profile->tox, friend_numbers);
if (friend_count == 0)
{
weechat_printf(profile->buffer, "%sYou have no friends :(",
weechat_prefix("network"));
return WEECHAT_RC_OK;
}
weechat_printf(profile->buffer, "%s[#] Name [Tox ID (short)] Status",
weechat_prefix("network"));
for (size_t i = 0; i < friend_count; ++i)
{
uint32_t friend_number = friend_numbers[i];
char *name = twc_get_name_nt(profile->tox, friend_number);
char *hex_address =
twc_get_friend_id_short(profile->tox, friend_number);
char *status =
twc_get_status_message_nt(profile->tox, friend_number);
char *online_color =
(tox_friend_get_connection_status(profile->tox, friend_number,
NULL) != TOX_CONNECTION_NONE)
? "chat_nick"
: "chat_nick_offline";
weechat_printf(profile->buffer, "%s[%" PRIu32 "] %s%s [%s]%s %s",
weechat_prefix("network"), friend_number,
weechat_color(online_color), name, hex_address,
weechat_color("reset"), status);
free(name);
free(status);
free(hex_address);
}
return WEECHAT_RC_OK;
}
/* /friend add [-force] <Tox ID> [<message>] */
else if (argc >= 3 && weechat_strcasecmp(argv[1], "add") == 0)
{
bool force;
const char *hex_id;
const char *message;
force = weechat_strcasecmp(argv[2], "-force") == 0;
if (force)
{
hex_id = argv[3];
message = argc >= 5 ? argv_eol[4] : NULL;
}
else
{
hex_id = argv[2];
message = argc >= 4 ? argv_eol[3] : NULL;
}
if (!message)
message = weechat_config_string(twc_config_friend_request_message);
if (strlen(hex_id) != TOX_ADDRESS_SIZE * 2)
{
weechat_printf(profile->buffer,
"%sTox ID length invalid. Please try again.",
weechat_prefix("error"));
return WEECHAT_RC_OK;
}
uint8_t address[TOX_ADDRESS_SIZE];
twc_hex2bin(hex_id, TOX_ADDRESS_SIZE, address);
if (force)
{
bool fail = false;
char *hex_key = strndup(hex_id, TOX_PUBLIC_KEY_SIZE * 2);
int32_t friend_number = twc_match_friend(profile, hex_key);
free(hex_key);
if (friend_number == TWC_FRIEND_MATCH_AMBIGUOUS)
fail = true;
else if (friend_number != TWC_FRIEND_MATCH_NOMATCH)
fail = !tox_friend_delete(profile->tox, friend_number, NULL);
if (fail)
{
weechat_printf(profile->buffer,
"%scould not remove friend; please remove "
"manually before resending friend request",
weechat_prefix("error"));
return WEECHAT_RC_OK;
}
}
TOX_ERR_FRIEND_ADD err;
(void)tox_friend_add(profile->tox, (uint8_t *)address,
(uint8_t *)message, strlen(message), &err);
switch (err)
{
case TOX_ERR_FRIEND_ADD_OK:
weechat_printf(profile->buffer, "%sFriend request sent!",
weechat_prefix("network"));
break;
case TOX_ERR_FRIEND_ADD_TOO_LONG:
weechat_printf(profile->buffer,
"%sFriend request message too long! Try again.",
weechat_prefix("error"));
break;
case TOX_ERR_FRIEND_ADD_ALREADY_SENT:
case TOX_ERR_FRIEND_ADD_SET_NEW_NOSPAM:
weechat_printf(profile->buffer,
"%sYou have already sent a friend request to "
"that address (use -force to circumvent)",
weechat_prefix("error"));
break;
case TOX_ERR_FRIEND_ADD_OWN_KEY:
weechat_printf(profile->buffer,
"%sYou can't add yourself as a friend.",
weechat_prefix("error"));
break;
case TOX_ERR_FRIEND_ADD_BAD_CHECKSUM:
weechat_printf(profile->buffer,
"%sInvalid friend address - try again.",
weechat_prefix("error"));
break;
case TOX_ERR_FRIEND_ADD_MALLOC:
weechat_printf(profile->buffer,
"%sCould not add friend (out of memory).",
weechat_prefix("error"));
break;
case TOX_ERR_FRIEND_ADD_NULL:
case TOX_ERR_FRIEND_ADD_NO_MESSAGE: /* this should not happen as we
validate the message */
default:
weechat_printf(profile->buffer,
"%sCould not add friend (unknown error %d).",
weechat_prefix("error"), err);
break;
}
return WEECHAT_RC_OK;
}
/* /friend remove */
else if (argc >= 3 && (weechat_strcasecmp(argv[1], "remove") == 0))
{
int32_t friend_number = twc_match_friend(profile, argv[2]);
TWC_CHECK_FRIEND_NUMBER(profile, friend_number, argv[2]);
char *name = twc_get_name_nt(profile->tox, friend_number);
if (tox_friend_delete(profile->tox, friend_number, NULL))
{
weechat_printf(profile->buffer, "%sRemoved %s from friend list.",
weechat_prefix("network"), name);
}
else
{
weechat_printf(profile->buffer, "%sCould not remove friend!",
weechat_prefix("error"));
}
free(name);
return WEECHAT_RC_OK;
}
/* friend accept|decline <number>|all */
else if (argc == 3 && (weechat_strcasecmp(argv[1], "accept") == 0 ||
weechat_strcasecmp(argv[1], "decline") == 0))
{
int accept = weechat_strcasecmp(argv[1], "accept") == 0;
struct t_twc_friend_request *request;
if (weechat_strcasecmp(argv[2], "all") == 0)
{
size_t index;
size_t count = 0;
struct t_twc_list_item *item;
twc_list_foreach (profile->friend_requests, index, item)
{
if (accept)
{
if (twc_friend_request_accept(item->friend_request))
{
++count;
}
else
{
char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1];
twc_bin2hex(item->friend_request->tox_id,
TOX_PUBLIC_KEY_SIZE, hex_address);
weechat_printf(
profile->buffer,
"%sCould not accept friend request from %s",
weechat_prefix("error"), hex_address);
}
}
else
{
twc_friend_request_remove(item->friend_request);
++count;
}
}
weechat_printf(profile->buffer, "%s%s %zu friend requests.",
weechat_prefix("network"),
accept ? "Accepted" : "Declined", count);
return WEECHAT_RC_OK;
}
else
{
char *endptr;
unsigned long num = strtoul(argv[2], &endptr, 10);
if (endptr == argv[2] ||
(request = twc_friend_request_with_index(profile, num)) == NULL)
{
weechat_printf(profile->buffer, "%sInvalid friend request ID.",
weechat_prefix("error"));
return WEECHAT_RC_OK;
}
char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1];
twc_bin2hex(request->tox_id, TOX_PUBLIC_KEY_SIZE, hex_address);
if (accept)
{
if (!twc_friend_request_accept(request))
{
weechat_printf(profile->buffer,
"%sCould not accept friend request from %s",
weechat_prefix("error"), hex_address);
}
else
{
weechat_printf(profile->buffer,
"%sAccepted friend request from %s.",
weechat_prefix("network"), hex_address);
}
}
else
{
twc_friend_request_remove(request);
weechat_printf(profile->buffer,
"%sDeclined friend request from %s.",
weechat_prefix("network"), hex_address);
}
twc_friend_request_free(request);
return WEECHAT_RC_OK;
}
}
/* /friend requests */
else if (argc == 2 && weechat_strcasecmp(argv[1], "requests") == 0)
{
weechat_printf(profile->buffer,
"%sPending friend requests:", weechat_prefix("network"));
size_t index;
struct t_twc_list_item *item;
twc_list_foreach (profile->friend_requests, index, item)
{
char hex_address[TOX_PUBLIC_KEY_SIZE * 2 + 1];
twc_bin2hex(item->friend_request->tox_id, TOX_PUBLIC_KEY_SIZE,
hex_address);
weechat_printf(profile->buffer,
"%s[%zu] Address: %s\n"
"[%zu] Message: %s",
weechat_prefix("network"), index, hex_address, index,
item->friend_request->message);
}
return WEECHAT_RC_OK;
}
return WEECHAT_RC_ERROR;
}
/**
* Command /group callback.
*/
int
twc_cmd_group(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
struct t_twc_profile *profile = twc_profile_search_buffer(buffer);
TOX_ERR_CONFERENCE_NEW err = TOX_ERR_CONFERENCE_NEW_OK;
TWC_CHECK_PROFILE(profile);
TWC_CHECK_PROFILE_LOADED(profile);
/* /group create */
if (argc == 2 && weechat_strcasecmp(argv[1], "create") == 0)
{
int rc = tox_conference_new(profile->tox, &err);
if (err == TOX_ERR_CONFERENCE_NEW_OK)
twc_chat_search_group(profile, rc, true);
else
weechat_printf(profile->buffer,
"%sCould not create group chat with error %d",
weechat_prefix("error"), err);
return WEECHAT_RC_OK;
}
/* /group join|decline <number> */
else if (argc == 3 && (weechat_strcasecmp(argv[1], "join") == 0 ||
weechat_strcasecmp(argv[1], "decline") == 0))
{
bool join = weechat_strcasecmp(argv[1], "join") == 0;
struct t_twc_group_chat_invite *invite;
char *endptr;
unsigned long num = strtoul(argv[2], &endptr, 10);
if (endptr == argv[2] ||
(invite = twc_group_chat_invite_with_index(profile, num)) == NULL)
{
weechat_printf(profile->buffer, "%sInvalid group chat invite ID.",
weechat_prefix("error"));
return WEECHAT_RC_OK;
}
if (join)
{
int group_number = twc_group_chat_invite_join(invite);
/* create a buffer for the new group chat */
if (group_number >= 0)
twc_chat_search_group(profile, group_number, true);
else
weechat_printf(profile->buffer,
"%sCould not join group chat (unknown error)",
weechat_prefix("error"));
}
else
{
twc_group_chat_invite_remove(invite);
}
return WEECHAT_RC_OK;
}
/* /group invites */
else if (argc == 2 && weechat_strcasecmp(argv[1], "invites") == 0)
{
weechat_printf(profile->buffer, "%sPending group chat invites:",
weechat_prefix("network"));
size_t index;
struct t_twc_list_item *item;
twc_list_foreach (profile->group_chat_invites, index, item)
{
char *friend_name = twc_get_name_nt(
profile->tox, item->group_chat_invite->friend_number);
weechat_printf(profile->buffer, "%s[%zu] From: %s",
weechat_prefix("network"), index, friend_name);
free(friend_name);
}
return WEECHAT_RC_OK;
}
return WEECHAT_RC_ERROR;
}
/**
* Command /invite callback.
*/
int
twc_cmd_invite(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
if (argc == 1)
return WEECHAT_RC_ERROR;
TOX_ERR_CONFERENCE_INVITE err = TOX_ERR_CONFERENCE_INVITE_OK;
struct t_twc_chat *chat = twc_chat_search_buffer(buffer);
TWC_CHECK_PROFILE_LOADED(chat->profile);
TWC_CHECK_GROUP_CHAT(chat);
int32_t friend_number = twc_match_friend(chat->profile, argv_eol[1]);
TWC_CHECK_FRIEND_NUMBER(chat->profile, friend_number, argv_eol[1]);
tox_conference_invite(chat->profile->tox, friend_number, chat->group_number,
&err);
if (err == TOX_ERR_CONFERENCE_INVITE_OK)
{
char *friend_name = twc_get_name_nt(chat->profile->tox, friend_number);
weechat_printf(chat->buffer, "%sInvited %s to the group chat.",
weechat_prefix("network"), friend_name);
free(friend_name);
}
else
{
weechat_printf(chat->buffer,
"%sFailed to send group chat invite with error %d",
weechat_prefix("error"), err);
}
return WEECHAT_RC_OK;
}
/**
* Command /me callback.
*/
int
twc_cmd_me(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
if (argc == 1)
return WEECHAT_RC_ERROR;
struct t_twc_chat *chat = twc_chat_search_buffer(buffer);
TWC_CHECK_CHAT(chat);
TWC_CHECK_PROFILE_LOADED(chat->profile);
twc_chat_send_message(chat, argv_eol[1], TOX_MESSAGE_TYPE_ACTION);
return WEECHAT_RC_OK;
}
/**
* Command /msg callback.
*/
int
twc_cmd_msg(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
if (argc == 1)
return WEECHAT_RC_ERROR;
struct t_twc_profile *profile = twc_profile_search_buffer(buffer);
TWC_CHECK_PROFILE(profile);
TWC_CHECK_PROFILE_LOADED(profile);
/* do a shell split in case a friend has spaces in his name and we need
* quotes */
int shell_argc;
char **shell_argv = weechat_string_split_shell(argv_eol[0], &shell_argc);
char recipient[TOX_MAX_NAME_LENGTH + 1];
snprintf(recipient, TOX_MAX_NAME_LENGTH, "%s", shell_argv[1]);
weechat_string_free_split(shell_argv);
char *message = NULL;
if (shell_argc >= 3)
{
/* extract message, add two if quotes are used */
message = argv_eol[1] + strlen(recipient);
if (*argv[1] == '"' || *argv[1] == '\'')
message += 2;
}
int32_t friend_number = twc_match_friend(profile, recipient);
TWC_CHECK_FRIEND_NUMBER(profile, friend_number, recipient);
/* create chat buffer if it does not exist */
struct t_twc_chat *chat =
twc_chat_search_friend(profile, friend_number, true);
/* send a message if provided */
if (message)
twc_chat_send_message(chat, weechat_string_strip(message, 1, 1, " "),
TOX_MESSAGE_TYPE_NORMAL);
return WEECHAT_RC_OK;
}
/**
* Command /myid callback.
*/
int
twc_cmd_myid(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
struct t_twc_profile *profile = twc_profile_search_buffer(buffer);
TWC_CHECK_PROFILE(profile);
TWC_CHECK_PROFILE_LOADED(profile);
uint8_t address[TOX_ADDRESS_SIZE];
tox_self_get_address(profile->tox, address);
char address_str[TOX_ADDRESS_SIZE * 2 + 1];
twc_bin2hex(address, TOX_ADDRESS_SIZE, address_str);
weechat_printf(profile->buffer, "%sYour Tox address: %s",
weechat_prefix("network"), address_str);
return WEECHAT_RC_OK;
}
/**
* Command /name callback.
*/
int
twc_cmd_name(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
if (argc == 1)
return WEECHAT_RC_ERROR;
struct t_twc_profile *profile = twc_profile_search_buffer(buffer);
TWC_CHECK_PROFILE(profile);
TWC_CHECK_PROFILE_LOADED(profile);
const char *name = argv_eol[1];
TOX_ERR_SET_INFO err;
tox_self_set_name(profile->tox, (uint8_t *)name, strlen(name), &err);
if (err != TOX_ERR_SET_INFO_OK)
{
char *err_msg;
switch (err)
{
case TOX_ERR_SET_INFO_NULL:
err_msg = "no name given";
break;
case TOX_ERR_SET_INFO_TOO_LONG:
err_msg = "name too long";
break;
default:
err_msg = "unknown error";
break;
}
weechat_printf(profile->buffer, "%s%s%s", weechat_prefix("error"),
"Could not change name: ", err_msg);
return WEECHAT_RC_OK;
}
weechat_bar_item_update("input_prompt");
weechat_printf(profile->buffer, "%sYou are now known as %s",
weechat_prefix("network"), name);
size_t index;
struct t_twc_list_item *item;
twc_list_foreach (profile->chats, index, item)
{
weechat_printf(item->chat->buffer, "%sYou are now known as %s",
weechat_prefix("network"), name);
}
return WEECHAT_RC_OK;
}
/**
* Command /names callback.
*/
int
twc_cmd_names(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
struct t_twc_chat *chat = twc_chat_search_buffer(buffer);
TWC_CHECK_CHAT(chat);
TWC_CHECK_PROFILE_LOADED(chat->profile);
if (chat->group_number < 0)
{
weechat_printf(NULL,
"%s%s: command \"%s\" must be executed in a group chat "
"buffer",
weechat_prefix("error"), weechat_plugin->name, argv[0]);
return WEECHAT_RC_OK;
}
size_t const num_names = weechat_list_size(chat->nicks);
if (num_names == 0)
return WEECHAT_RC_ERROR;
char const *names[num_names];
char const *colors[num_names];
size_t total_names_length = 0;
/* iterate over all names, retrieving length and string representation */
struct t_weelist_item *name = weechat_list_get(chat->nicks, 0);
size_t index = 0;
while (name != NULL)
{
char const *const name_string = weechat_list_string(name);
char const *const color = weechat_info_get("nick_color", name_string);
names[index] = name_string;
colors[index] = color;
total_names_length += strlen(name_string) + strlen(color);
++index;
name = weechat_list_next(name);
}
/* allocate space for all names, plus spaces and colours, plus \0 */
char *const names_str = malloc(total_names_length + num_names);
if (!names_str)
return WEECHAT_RC_ERROR;
/* copy all names into name_str buffer, with nick coloring */
size_t loc = 0;
for (size_t index = 0; index < num_names; ++index)
{
if (loc > 0)
names_str[loc++] = ' ';
char const *const name = names[index];
char const *const color = colors[index];
strcpy(names_str + loc, color);
loc += strlen(color);
strcpy(names_str + loc, name);
loc += strlen(name);
}
weechat_printf(chat->buffer, "%s%zu names: %s", weechat_prefix("network"),
num_names, names_str);
free(names_str);
return WEECHAT_RC_OK;
}
/**
* Command /nospam callback.
*/
int
twc_cmd_nospam(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
if (argc > 2)
return WEECHAT_RC_ERROR;
struct t_twc_profile *profile = twc_profile_search_buffer(buffer);
TWC_CHECK_PROFILE(profile);
TWC_CHECK_PROFILE_LOADED(profile);
uint32_t new_nospam;
if (argc == 2)
{
char *endptr;
unsigned long value = strtoul(argv[1], &endptr, 16);
if (endptr == argv[1] || value > UINT32_MAX)
{
weechat_printf(profile->buffer,
"%snospam must be a hexadecimal value between "
"0x00000000 and 0xFFFFFFFF",
weechat_prefix("error"));
return WEECHAT_RC_OK;
}
/* reverse the value bytes so it's displayed as entered in the Tox ID */
new_nospam = twc_uint32_reverse_bytes(value);
}
else
{
new_nospam = random();
}
uint32_t old_nospam = tox_self_get_nospam(profile->tox);
tox_self_set_nospam(profile->tox, new_nospam);
weechat_printf(profile->buffer,
"%snew nospam has been set; this changes your Tox ID! To "
"revert, run \"/nospam %x\"",
weechat_prefix("network"),
twc_uint32_reverse_bytes(old_nospam));
return WEECHAT_RC_OK;
}
/**
* Command /part callback.
*/
int
twc_cmd_part(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
struct t_twc_chat *chat = twc_chat_search_buffer(buffer);
TOX_ERR_CONFERENCE_DELETE err = TOX_ERR_CONFERENCE_DELETE_OK;
TWC_CHECK_PROFILE_LOADED(chat->profile);
TWC_CHECK_GROUP_CHAT(chat);
tox_conference_delete(chat->profile->tox, chat->group_number, &err);
if (err == TOX_ERR_CONFERENCE_DELETE_OK)
{
weechat_printf(chat->buffer, "%sYou have left the group chat",
weechat_prefix("network"));
}
else
{
weechat_printf(chat->buffer,
"%sFailed to leave group chat with error %d",
weechat_prefix("error"), err);
}
weechat_buffer_set_pointer(chat->buffer, "input_callback", NULL);
weechat_buffer_set_pointer(chat->buffer, "close_callback", NULL);
twc_list_remove_with_data(chat->profile->chats, chat);
twc_chat_free(chat);
return WEECHAT_RC_OK;
}
/**
* Save Tox profile data when /save is executed.
*/
int
twc_cmd_save(const void *pointer, void *data, struct t_gui_buffer *buffer,
const char *command)
{
size_t index;
struct t_twc_list_item *item;
twc_list_foreach (twc_profiles, index, item)
{
if (!(item->profile->tox))
continue;
int rc = twc_profile_save_data_file(item->profile);
if (rc == -1)
{
weechat_printf(NULL, "%s%s: failed to save data for profile %s",
weechat_prefix("error"), weechat_plugin->name,
item->profile->name);
}
}
weechat_printf(NULL, "%s: profile data saved", weechat_plugin->name);
return WEECHAT_RC_OK;
}
/**
* Command /status callback.
*/
int
twc_cmd_status(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
if (argc != 2)
return WEECHAT_RC_ERROR;
struct t_twc_profile *profile = twc_profile_search_buffer(buffer);