mirror of
https://github.com/weechat/weechat.git
synced 2026-06-26 21:06:38 +02:00
Allow mask or regex for IRC command /ignore (mask is default)
This commit is contained in:
+32
-18
@@ -770,6 +770,30 @@ command_command (void *data, struct t_gui_buffer *buffer,
|
||||
return WEECHAT_RC_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* command_filter_display: display one filter
|
||||
*/
|
||||
|
||||
void
|
||||
command_filter_display (struct t_gui_filter *filter)
|
||||
{
|
||||
gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER,
|
||||
_(" %s[%s%s%s]%s buffer: %s%s%s "
|
||||
"/ tags: %s / regex: %s %s"),
|
||||
GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS),
|
||||
GUI_COLOR(GUI_COLOR_CHAT),
|
||||
filter->name,
|
||||
GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS),
|
||||
GUI_COLOR(GUI_COLOR_CHAT),
|
||||
GUI_COLOR(GUI_COLOR_CHAT_BUFFER),
|
||||
filter->buffer,
|
||||
GUI_COLOR(GUI_COLOR_CHAT),
|
||||
filter->tags,
|
||||
filter->regex,
|
||||
(filter->enabled) ?
|
||||
"" : _("(disabled)"));
|
||||
}
|
||||
|
||||
/*
|
||||
* command_filter: manage message filters
|
||||
*/
|
||||
@@ -787,7 +811,7 @@ command_filter (void *data, struct t_gui_buffer *buffer,
|
||||
if ((argc == 1)
|
||||
|| ((argc == 2) && (string_strcasecmp (argv[1], "list") == 0)))
|
||||
{
|
||||
/* display all key bindings */
|
||||
/* display all filters */
|
||||
gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER, "");
|
||||
gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER,
|
||||
"%s",
|
||||
@@ -802,21 +826,7 @@ command_filter (void *data, struct t_gui_buffer *buffer,
|
||||
for (ptr_filter = gui_filters; ptr_filter;
|
||||
ptr_filter = ptr_filter->next_filter)
|
||||
{
|
||||
gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER,
|
||||
_(" %s[%s%s%s]%s buffer: %s%s%s "
|
||||
"/ tags: %s / regex: %s %s"),
|
||||
GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS),
|
||||
GUI_COLOR(GUI_COLOR_CHAT),
|
||||
ptr_filter->name,
|
||||
GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS),
|
||||
GUI_COLOR(GUI_COLOR_CHAT),
|
||||
GUI_COLOR(GUI_COLOR_CHAT_BUFFER),
|
||||
ptr_filter->buffer,
|
||||
GUI_COLOR(GUI_COLOR_CHAT),
|
||||
ptr_filter->tags,
|
||||
ptr_filter->regex,
|
||||
(ptr_filter->enabled) ?
|
||||
"" : _("(disabled)"));
|
||||
command_filter_display (ptr_filter);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -968,11 +978,15 @@ command_filter (void *data, struct t_gui_buffer *buffer,
|
||||
return WEECHAT_RC_ERROR;
|
||||
}
|
||||
|
||||
if (gui_filter_new (1, argv[2], argv[3], argv[4], argv_eol[5]))
|
||||
ptr_filter = gui_filter_new (1, argv[2], argv[3], argv[4], argv_eol[5]);
|
||||
|
||||
if (ptr_filter)
|
||||
{
|
||||
gui_chat_printf (NULL, "");
|
||||
gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER,
|
||||
_("Filter \"%s\" added"),
|
||||
_("Filter \"%s\" added:"),
|
||||
argv[2]);
|
||||
command_filter_display (ptr_filter);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -715,6 +715,56 @@ string_has_highlight (const char *string, const char *highlight_words)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* string_mask_to_regex: convert a mask (string with only "*" as joker) to a
|
||||
* regex, paying attention to special chars in a regex
|
||||
*/
|
||||
|
||||
char *
|
||||
string_mask_to_regex (const char *mask)
|
||||
{
|
||||
char *result;
|
||||
const char *ptr_mask;
|
||||
int index_result;
|
||||
char *regex_special_char = ".[]{}()|?+";
|
||||
|
||||
if (!mask)
|
||||
return NULL;
|
||||
|
||||
result = malloc ((strlen (mask) * 2) + 1);
|
||||
if (!result)
|
||||
return NULL;
|
||||
|
||||
result[0] = '\0';
|
||||
index_result = 0;
|
||||
ptr_mask = mask;
|
||||
while (ptr_mask[0])
|
||||
{
|
||||
/* '*' in string ? then replace by '.*' */
|
||||
if (ptr_mask[0] == '*')
|
||||
{
|
||||
result[index_result++] = '.';
|
||||
result[index_result++] = '*';
|
||||
}
|
||||
/* special regex char in string ? escape it with '\' */
|
||||
else if (strchr (regex_special_char, ptr_mask[0]))
|
||||
{
|
||||
result[index_result++] = '\\';
|
||||
result[index_result++] = ptr_mask[0];
|
||||
}
|
||||
/* standard char, just copy it */
|
||||
else
|
||||
result[index_result++] = ptr_mask[0];
|
||||
|
||||
ptr_mask++;
|
||||
}
|
||||
|
||||
/* add final '\0' */
|
||||
result[index_result] = '\0';
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* string_explode: explode a string according to separators
|
||||
* examples:
|
||||
|
||||
@@ -41,6 +41,7 @@ extern char *string_strip (const char *string, int left, int right,
|
||||
extern char *string_convert_hex_chars (const char *string);
|
||||
extern int string_has_highlight (const char *string,
|
||||
const char *highlight_words);
|
||||
extern char *string_mask_to_regex (const char *mask);
|
||||
extern char **string_explode (const char *string, const char *separators,
|
||||
int keep_eol, int num_items_max, int *num_items);
|
||||
extern void string_free_exploded (char **exploded_string);
|
||||
|
||||
@@ -1302,6 +1302,27 @@ irc_command_halfop (void *data, struct t_gui_buffer *buffer, int argc,
|
||||
return WEECHAT_RC_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* irc_command_ignore_display: display a ignore
|
||||
*/
|
||||
|
||||
void
|
||||
irc_command_ignore_display (struct t_irc_ignore *ignore)
|
||||
{
|
||||
weechat_printf (NULL,
|
||||
_(" %s[%s%d%s]%s mask: %s / server: %s / channel: %s"),
|
||||
IRC_COLOR_CHAT_DELIMITERS,
|
||||
IRC_COLOR_CHAT,
|
||||
ignore->number,
|
||||
IRC_COLOR_CHAT_DELIMITERS,
|
||||
IRC_COLOR_CHAT,
|
||||
ignore->mask,
|
||||
(ignore->server) ?
|
||||
ignore->server : "*",
|
||||
(ignore->channel) ?
|
||||
ignore->channel : "*");
|
||||
}
|
||||
|
||||
/*
|
||||
* irc_command_ignore: add or remove ignore
|
||||
*/
|
||||
@@ -1310,9 +1331,8 @@ int
|
||||
irc_command_ignore (void *data, struct t_gui_buffer *buffer, int argc,
|
||||
char **argv, char **argv_eol)
|
||||
{
|
||||
int i;
|
||||
struct t_irc_ignore *ptr_ignore;
|
||||
char *mask, *server, *channel, *error;
|
||||
char *mask, *regex, *ptr_regex, *server, *channel, *error;
|
||||
long number;
|
||||
|
||||
/* make C compiler happy */
|
||||
@@ -1328,23 +1348,10 @@ irc_command_ignore (void *data, struct t_gui_buffer *buffer, int argc,
|
||||
{
|
||||
weechat_printf (NULL, "");
|
||||
weechat_printf (NULL, _("%s: ignore list:"), IRC_PLUGIN_NAME);
|
||||
i = 0;
|
||||
for (ptr_ignore = irc_ignore_list; ptr_ignore;
|
||||
ptr_ignore = ptr_ignore->next_ignore)
|
||||
{
|
||||
i++;
|
||||
weechat_printf (NULL,
|
||||
_(" %s[%s%d%s]%s mask: %s / server: %s / channel: %s"),
|
||||
IRC_COLOR_CHAT_DELIMITERS,
|
||||
IRC_COLOR_CHAT,
|
||||
i,
|
||||
IRC_COLOR_CHAT_DELIMITERS,
|
||||
IRC_COLOR_CHAT,
|
||||
ptr_ignore->mask,
|
||||
(ptr_ignore->server) ?
|
||||
ptr_ignore->server : "*",
|
||||
(ptr_ignore->channel) ?
|
||||
ptr_ignore->channel : "*");
|
||||
irc_command_ignore_display (ptr_ignore);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -1370,7 +1377,18 @@ irc_command_ignore (void *data, struct t_gui_buffer *buffer, int argc,
|
||||
server = (argc > 3) ? argv[3] : NULL;
|
||||
channel = (argc > 4) ? argv[4] : NULL;
|
||||
|
||||
if (irc_ignore_search (mask, server, channel))
|
||||
if (strncmp (mask, "re:", 3) == 0)
|
||||
{
|
||||
regex = NULL;
|
||||
ptr_regex = mask + 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
regex = weechat_string_mask_to_regex (mask);
|
||||
ptr_regex = (regex) ? regex : mask;
|
||||
}
|
||||
|
||||
if (irc_ignore_search (ptr_regex, server, channel))
|
||||
{
|
||||
weechat_printf (NULL,
|
||||
_("%s%s: ignore already exists"),
|
||||
@@ -1378,9 +1396,16 @@ irc_command_ignore (void *data, struct t_gui_buffer *buffer, int argc,
|
||||
return WEECHAT_RC_ERROR;
|
||||
}
|
||||
|
||||
if (irc_ignore_new (mask, server, channel))
|
||||
ptr_ignore = irc_ignore_new (ptr_regex, server, channel);
|
||||
|
||||
if (regex)
|
||||
free (regex);
|
||||
|
||||
if (ptr_ignore)
|
||||
{
|
||||
weechat_printf (NULL, _("%s: ignore added"), IRC_PLUGIN_NAME);
|
||||
weechat_printf (NULL, "");
|
||||
weechat_printf (NULL, _("%s: ignore added:"), IRC_PLUGIN_NAME);
|
||||
irc_command_ignore_display (ptr_ignore);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -3845,7 +3870,7 @@ irc_command_init ()
|
||||
NULL, &irc_command_halfop, NULL);
|
||||
weechat_hook_command ("ignore",
|
||||
N_("ignore nicks/hosts from servers or channels"),
|
||||
N_("[list] | [add nick/host [server [channel]]] | "
|
||||
N_("[list] | [add [re:]nick/host [server [channel]]] | "
|
||||
"[del number|-all]"),
|
||||
N_(" list: list all ignore\n"
|
||||
" add: add a ignore\n"
|
||||
@@ -3853,8 +3878,9 @@ irc_command_init ()
|
||||
" number: number of ignore to delete (look at "
|
||||
"list to find it)\n"
|
||||
" -all: delete all ignore\n"
|
||||
"nick/host: nick or host to ignore (regular "
|
||||
"expression allowed)\n"
|
||||
"nick/host: nick or host to ignore: syntax is "
|
||||
"\"re:regex\" or \"mask\" (a mask is a string with "
|
||||
"some \"*\" to replace one or more chars)\n"
|
||||
" server: internal server name where ignore "
|
||||
"is working\n"
|
||||
" channel: channel name where ignore is "
|
||||
|
||||
@@ -96,15 +96,12 @@ struct t_irc_ignore *
|
||||
irc_ignore_search_by_number (int number)
|
||||
{
|
||||
struct t_irc_ignore *ptr_ignore;
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
for (ptr_ignore = irc_ignore_list; ptr_ignore;
|
||||
ptr_ignore = ptr_ignore->next_ignore)
|
||||
{
|
||||
if (i == number)
|
||||
if (ptr_ignore->number == number)
|
||||
return ptr_ignore;
|
||||
i++;
|
||||
}
|
||||
|
||||
/* ignore not found */
|
||||
@@ -137,6 +134,7 @@ irc_ignore_new (const char *mask, const char *server, const char *channel)
|
||||
new_ignore = malloc (sizeof (*new_ignore));
|
||||
if (new_ignore)
|
||||
{
|
||||
new_ignore->number = (last_irc_ignore) ? last_irc_ignore->number + 1 : 1;
|
||||
new_ignore->mask = strdup (mask);
|
||||
new_ignore->regex_mask = regex;
|
||||
new_ignore->server = (server) ? strdup (server) : strdup ("*");
|
||||
@@ -212,9 +210,18 @@ irc_ignore_check (struct t_irc_server *server, struct t_irc_channel *channel,
|
||||
void
|
||||
irc_ignore_free (struct t_irc_ignore *ignore)
|
||||
{
|
||||
struct t_irc_ignore *ptr_ignore;
|
||||
|
||||
weechat_hook_signal_send ("irc_ignore_removing",
|
||||
WEECHAT_HOOK_SIGNAL_POINTER, ignore);
|
||||
|
||||
/* decrement number for all ignore after this one */
|
||||
for (ptr_ignore = ignore->next_ignore; ptr_ignore;
|
||||
ptr_ignore = ptr_ignore->next_ignore)
|
||||
{
|
||||
ptr_ignore->number--;
|
||||
}
|
||||
|
||||
/* free data */
|
||||
if (ignore->mask)
|
||||
free (ignore->mask);
|
||||
|
||||
@@ -27,6 +27,7 @@ struct t_irc_channel;
|
||||
|
||||
struct t_irc_ignore
|
||||
{
|
||||
int number; /* ignore number */
|
||||
char *mask; /* nick / host mask */
|
||||
regex_t *regex_mask; /* regex for mask */
|
||||
char *server; /* server name */
|
||||
|
||||
@@ -312,6 +312,7 @@ plugin_load (const char *filename)
|
||||
new_plugin->string_remove_quotes = &string_remove_quotes;
|
||||
new_plugin->string_strip = &string_strip;
|
||||
new_plugin->string_has_highlight = &string_has_highlight;
|
||||
new_plugin->string_mask_to_regex = &string_mask_to_regex;
|
||||
new_plugin->string_explode = &string_explode;
|
||||
new_plugin->string_free_exploded = &string_free_exploded;
|
||||
new_plugin->string_split_command = &string_split_command;
|
||||
|
||||
@@ -146,6 +146,7 @@ struct t_weechat_plugin
|
||||
const char *chars);
|
||||
int (*string_has_highlight) (const char *string,
|
||||
const char *highlight_words);
|
||||
char *(*string_mask_to_regex) (const char *mask);
|
||||
char **(*string_explode) (const char *string, const char *separators,
|
||||
int keep_eol, int num_items_max, int *num_items);
|
||||
void (*string_free_exploded) (char **exploded_string);
|
||||
@@ -616,6 +617,8 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
|
||||
weechat_plugin->string_strip(__string, __left, __right, __chars)
|
||||
#define weechat_string_has_highlight(__string, __highlight_words) \
|
||||
weechat_plugin->string_has_highlight(__string, __highlight_words)
|
||||
#define weechat_string_mask_to_regex(__mask) \
|
||||
weechat_plugin->string_mask_to_regex(__mask)
|
||||
#define weechat_string_explode(__string, __separator, __eol, __max, \
|
||||
__num_items) \
|
||||
weechat_plugin->string_explode(__string, __separator, __eol, \
|
||||
|
||||
Reference in New Issue
Block a user