Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit ecb2eb8

Browse files
committed
Add /ignore feature
1 parent 07acf2b commit ecb2eb8

11 files changed

Lines changed: 333 additions & 12 deletions

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ add_library(tox MODULE
3030
src/twc-friend-request.c
3131
src/twc-gui.c
3232
src/twc-group-invite.c
33+
src/twc-ignore.c
3334
src/twc-list.c
3435
src/twc-message-queue.c
3536
src/twc-profile.c
@@ -64,4 +65,3 @@ endif()
6465
set(PLUGIN_PATH "lib/weechat/plugins" CACHE PATH
6566
"Path to install the plugin binary to.")
6667
install(TARGETS tox DESTINATION "${PLUGIN_PATH}")
67-

src/twc-chat.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,32 @@ twc_chat_search_buffer(struct t_gui_buffer *buffer)
273273
return NULL;
274274
}
275275

276+
/**
277+
* Set a prefix to a nickname in the nicklist of a chat.
278+
*/
279+
void
280+
twc_chat_update_prefix(struct t_twc_chat *chat, const char *name, const char *prefix, const char *prefix_color)
281+
{
282+
struct t_gui_nick_group *ptr_group = NULL;
283+
struct t_gui_nick *ptr_nick = NULL;
284+
285+
weechat_nicklist_get_next_item(chat->buffer, &ptr_group, &ptr_nick);
286+
while(ptr_group || ptr_nick)
287+
{
288+
if (ptr_nick)
289+
{
290+
const char *name_field = weechat_nicklist_nick_get_string(chat->buffer, ptr_nick, "name");
291+
if (!strncmp(name, name_field , TOX_MAX_NAME_LENGTH))
292+
{
293+
weechat_nicklist_nick_set(chat->buffer, ptr_nick, "prefix", prefix);
294+
weechat_nicklist_nick_set(chat->buffer, ptr_nick, "prefix_color", prefix_color);
295+
weechat_nicklist_nick_set(chat->buffer, ptr_nick, "color", "default");
296+
}
297+
}
298+
weechat_nicklist_get_next_item(chat->buffer, &ptr_group, &ptr_nick);
299+
}
300+
}
301+
276302
/**
277303
* Print a chat message to a chat's buffer.
278304
*/

src/twc-chat.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ twc_chat_search_group(struct t_twc_profile *profile, int32_t group_number,
5252
struct t_twc_chat *
5353
twc_chat_search_buffer(struct t_gui_buffer *target_buffer);
5454

55+
void
56+
twc_chat_update_prefix(struct t_twc_chat *chat, const char *name, const char *prefix, const char *prefix_color);
57+
5558
enum t_twc_rc
5659
twc_chat_set_logging(struct t_twc_chat const *const chat, bool logging);
5760

src/twc-commands.c

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "twc-bootstrap.h"
3030
#include "twc-tfer.h"
31+
#include "twc-ignore.h"
3132
#include "twc-chat.h"
3233
#include "twc-config.h"
3334
#include "twc-friend-request.h"
@@ -973,6 +974,10 @@ twc_cmd_save(const void *pointer, void *data, struct t_gui_buffer *buffer,
973974
weechat_prefix("error"), weechat_plugin->name,
974975
item->profile->name);
975976
}
977+
twc_ignore_dump(item->profile);
978+
weechat_printf(NULL, "%s: ignore list for profile '%s' saved",
979+
weechat_plugin->name,
980+
item->profile->name);
976981
}
977982

978983
weechat_printf(NULL, "%s: profile data saved", weechat_plugin->name);
@@ -1238,7 +1243,7 @@ twc_cmd_send(const void *pointer, void *data, struct t_gui_buffer *buffer,
12381243
/* /send <number>|<name>|<Tox ID> <file> */
12391244
if (argc >= 3)
12401245
{
1241-
/* do a shell split in case a friend has spaces in his name
1246+
/* do a shell split in case a friend has spaces in his name
12421247
* and join the name */
12431248
int shell_argc;
12441249
char **shell_argv = weechat_string_split_shell(argv_eol[1], &shell_argc);
@@ -1314,6 +1319,81 @@ twc_cmd_send(const void *pointer, void *data, struct t_gui_buffer *buffer,
13141319
return WEECHAT_RC_OK;
13151320
}
13161321

1322+
void
1323+
twc_name_prefix_in_groupchats(struct t_twc_profile *profile,
1324+
const char *name, const char *prefix, const char *prefix_color)
1325+
{
1326+
struct t_twc_list_item *item;
1327+
struct t_twc_chat *chat;
1328+
size_t index;
1329+
twc_list_foreach(profile->chats , index, item)
1330+
{
1331+
chat = (struct t_twc_chat *)(item->chat);
1332+
if ((chat->group_number) >= 0)
1333+
{
1334+
twc_chat_update_prefix(chat, name, prefix, prefix_color);
1335+
}
1336+
}
1337+
}
1338+
/**
1339+
* Command /ignore callback.
1340+
*/
1341+
int
1342+
twc_cmd_ignore(const void *pointer, void *data, struct t_gui_buffer *buffer,
1343+
int argc, char **argv, char **argv_eol)
1344+
{
1345+
struct t_twc_profile *profile = twc_profile_search_buffer(buffer);
1346+
TWC_CHECK_PROFILE(profile);
1347+
TWC_CHECK_PROFILE_LOADED(profile);
1348+
1349+
/* list ignores */
1350+
if ((argc == 1)
1351+
|| ((argc == 2) && (weechat_strcasecmp (argv[1], "list") == 0)))
1352+
{
1353+
twc_ignore_print(profile);
1354+
return WEECHAT_RC_OK;
1355+
}
1356+
1357+
const char *nick = argv_eol[2];
1358+
struct t_weelist_item *item;
1359+
item = weechat_list_search(profile->ignore_list, nick);
1360+
1361+
/* add ignore */
1362+
if (weechat_strcasecmp (argv[1], "add") == 0)
1363+
{
1364+
WEECHAT_COMMAND_MIN_ARGS(3, "add");
1365+
if (!item)
1366+
{
1367+
weechat_list_add(profile->ignore_list, nick, WEECHAT_LIST_POS_SORT, NULL);
1368+
weechat_printf(profile->buffer, "%snickname '%s' has been added to the ignore list",
1369+
weechat_prefix("action"), nick);
1370+
twc_name_prefix_in_groupchats(profile, nick, "-", "yellow");
1371+
}
1372+
else
1373+
weechat_printf(profile->buffer, "%snickname '%s' is already in the ignore list",
1374+
weechat_prefix("error"), nick);
1375+
return WEECHAT_RC_OK;
1376+
}
1377+
1378+
/* delete ignore */
1379+
if (weechat_strcasecmp (argv[1], "del") == 0)
1380+
{
1381+
WEECHAT_COMMAND_MIN_ARGS(3, "del");
1382+
if (item)
1383+
{
1384+
weechat_list_remove(profile->ignore_list, item);
1385+
weechat_printf(profile->buffer, "%snickname '%s' has been deleted from the ignore list",
1386+
weechat_prefix("action"), nick);
1387+
twc_name_prefix_in_groupchats(profile, nick, " ", "default");
1388+
}
1389+
else
1390+
weechat_printf(profile->buffer, "%sthere's no nickname '%s' in the ignore list",
1391+
weechat_prefix("error"), nick);
1392+
return WEECHAT_RC_OK;
1393+
}
1394+
WEECHAT_COMMAND_ERROR
1395+
}
1396+
13171397
/**
13181398
* Register Tox-WeeChat commands.
13191399
*/
@@ -1445,4 +1525,15 @@ twc_commands_init()
14451525
"%(filename)"
14461526
" || %(tox_friend_name)|%(tox_friend_tox_id) %(filename)",
14471527
twc_cmd_send, NULL, NULL);
1528+
weechat_hook_command("ignore", "ommit messages from people with certain nicks",
1529+
"list"
1530+
" || add <name>"
1531+
" || del <name>",
1532+
"list: show the list of ignores\n"
1533+
"add: add a nick to the list\n"
1534+
"del: delete a nick from the list\n",
1535+
"list"
1536+
" || add %*"
1537+
" || del %*",
1538+
twc_cmd_ignore, NULL, NULL);
14481539
}

src/twc-config.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "twc-list.h"
2828
#include "twc-profile.h"
2929
#include "twc-tfer.h"
30+
#include "twc-ignore.h"
3031
#include "twc.h"
3132

3233
#include "twc-config.h"
@@ -42,7 +43,7 @@ struct t_config_option *twc_config_short_id_size;
4243
char *twc_profile_option_names[TWC_PROFILE_NUM_OPTIONS] = {
4344
"save_file", "autoload", "autojoin", "autojoin_delay",
4445
"max_friend_requests", "proxy_address", "proxy_port", "proxy_type",
45-
"udp", "ipv6", "passphrase", "logging", "downloading_path",
46+
"udp", "ipv6", "passphrase", "logging", "downloading_path", "ignore_file"
4647
};
4748

4849
/**
@@ -191,6 +192,8 @@ twc_config_profile_change_callback(const void *pointer, void *data,
191192
case TWC_PROFILE_OPTION_DOWNLOADING_PATH:
192193
twc_tfer_update_downloading_path(profile);
193194
break;
195+
case TWC_PROFILE_OPTION_IGNORE_FILE:
196+
twc_ignore_reload(profile);
194197
default:
195198
break;
196199
}
@@ -295,6 +298,11 @@ twc_config_init_option(struct t_twc_profile *profile,
295298
"WeeChat home folder and \"%p\" by profile name";
296299
default_value = "%h/tfer/%p/";
297300
break;
301+
case TWC_PROFILE_OPTION_IGNORE_FILE:
302+
type = "string";
303+
description = "path to ignore file (new line separated list of nicknames)";
304+
default_value = "%h/tox/%p.ignore.list";
305+
break;
298306
default:
299307
return NULL;
300308
}

src/twc-ignore.c

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* This file is part of Tox-WeeChat.
3+
*
4+
* Tox-WeeChat is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Tox-WeeChat is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
#include <string.h>
18+
#include <stdbool.h>
19+
#include <stdio.h>
20+
21+
#include <weechat/weechat-plugin.h>
22+
23+
#include "twc-ignore.h"
24+
#include "twc-list.h"
25+
#include "twc-chat.h"
26+
#include "twc.h"
27+
28+
/**
29+
* Get a profile's expanded ignore list path, replacing:
30+
* - %h with WeeChat home
31+
* - %p with profile name
32+
*
33+
* Returned string must be freed.
34+
*/
35+
char *
36+
twc_ignore_expanded_path(struct t_twc_profile *profile)
37+
{
38+
const char *weechat_dir = weechat_info_get("weechat_dir", NULL);
39+
const char *base_path =
40+
TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_IGNORE_FILE);
41+
char *home_expanded = weechat_string_replace(base_path, "%h", weechat_dir);
42+
char *full_path =
43+
weechat_string_replace(home_expanded, "%p", profile->name);
44+
free(home_expanded);
45+
46+
return full_path;
47+
}
48+
49+
50+
/**
51+
* Load ignore list from the file.
52+
*/
53+
void
54+
twc_ignore_load(struct t_twc_profile *profile)
55+
{
56+
char *full_path = twc_ignore_expanded_path(profile);
57+
FILE *fp;
58+
fp = fopen(full_path, "r");
59+
if (fp)
60+
{
61+
size_t len, max_len = TOX_MAX_NAME_LENGTH + 2; /* for \0 and \n */
62+
char line[max_len];
63+
while(fgets(line, max_len, fp))
64+
{
65+
len = strlen(line);
66+
if (len > 0 && line[len-1] == '\n')
67+
line[--len] = '\0';
68+
weechat_list_add(profile->ignore_list, line, WEECHAT_LIST_POS_SORT, NULL);
69+
}
70+
}
71+
free(full_path);
72+
}
73+
74+
/**
75+
* Reload ignore list from the file. Called when "ignore_file" option is changing.
76+
*/
77+
void
78+
twc_ignore_reload(struct t_twc_profile *profile)
79+
{
80+
weechat_list_free(profile->ignore_list);
81+
profile->ignore_list = weechat_list_new();
82+
twc_ignore_load(profile);
83+
}
84+
85+
/**
86+
* Write ignore list to the file.
87+
*/
88+
void
89+
twc_ignore_dump(struct t_twc_profile *profile)
90+
{
91+
char *full_path = twc_ignore_expanded_path(profile);
92+
FILE *fp;
93+
fp = fopen(full_path, "w");
94+
if (fp)
95+
{
96+
size_t index, size = weechat_list_size(profile->ignore_list);
97+
for (index = 0; index < size; index++)
98+
{
99+
fprintf(fp, "%s\n", weechat_list_string(weechat_list_get(profile->ignore_list, index)));
100+
}
101+
fclose(fp);
102+
}
103+
free(full_path);
104+
}
105+
106+
/**
107+
* Print ignore list to its profile buffer.
108+
*/
109+
void
110+
twc_ignore_print(struct t_twc_profile *profile)
111+
{
112+
size_t index, count = (size_t)weechat_list_size(profile->ignore_list);
113+
if (!count)
114+
{
115+
weechat_printf(profile->buffer, "ignore list is empty!");
116+
return;
117+
}
118+
119+
/* 2 characters for quote symbols before and after a nick and 2 for ", " separator */
120+
size_t length = count*(TOX_MAX_NAME_LENGTH + 4);
121+
122+
char *formatted_list = malloc(length * sizeof(char) + 1);
123+
memset(formatted_list, 0, length * sizeof(char));
124+
125+
for (index = 0; index < count; index++)
126+
{
127+
strcat(formatted_list, "'");
128+
strcat(formatted_list, weechat_list_string(weechat_list_get(profile->ignore_list, index)));
129+
strcat(formatted_list, "'");
130+
if (index + 1 < count)
131+
strcat(formatted_list, ", ");
132+
133+
}
134+
weechat_printf(profile->buffer, "%slist of ignores: %s",
135+
weechat_prefix("action"), formatted_list);
136+
free(formatted_list);
137+
}

src/twc-ignore.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of Tox-WeeChat.
3+
*
4+
* Tox-WeeChat is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Tox-WeeChat is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
#ifndef TOX_WEECHAT_IGNORE_H
18+
#define TOX_WEECHAT_IGNORE_H
19+
20+
#include <stdbool.h>
21+
22+
#include "twc-profile.h"
23+
24+
void
25+
twc_ignore_load(struct t_twc_profile *profile);
26+
27+
void
28+
twc_ignore_reload(struct t_twc_profile *profile);
29+
30+
void
31+
twc_ignore_dump(struct t_twc_profile *profile);
32+
33+
void
34+
twc_ignore_print(struct t_twc_profile *profile);
35+
36+
#endif /* TOX_WEECHAT_IGNORE_H */

0 commit comments

Comments
 (0)