mirror of
https://github.com/weechat/weechat.git
synced 2026-06-30 14:56:39 +02:00
trigger: add default triggers
This commit is contained in:
@@ -29,17 +29,123 @@
|
||||
#include "trigger-config.h"
|
||||
|
||||
|
||||
/*
|
||||
* Displays one trigger.
|
||||
*/
|
||||
|
||||
void
|
||||
trigger_command_display_trigger (const char *name, int enabled, const char *hook,
|
||||
const char *arguments, const char *conditions,
|
||||
int regex_count, struct t_trigger_regex *regex,
|
||||
int commands_count, char **commands,
|
||||
int return_code, int full)
|
||||
{
|
||||
char spaces[256];
|
||||
int i, length;
|
||||
|
||||
if (full)
|
||||
{
|
||||
weechat_printf_tags (
|
||||
NULL, "no_trigger",
|
||||
" %s%s%s: %s%s(%s%s%s)",
|
||||
(enabled) ?
|
||||
weechat_color (weechat_config_string (trigger_config_color_trigger)) :
|
||||
weechat_color (weechat_config_string (trigger_config_color_trigger_disabled)),
|
||||
name,
|
||||
weechat_color ("reset"),
|
||||
hook,
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color ("reset"),
|
||||
arguments,
|
||||
weechat_color ("chat_delimiters"));
|
||||
length = weechat_strlen_screen (name) + 3;
|
||||
if (length >= (int)sizeof (spaces))
|
||||
length = sizeof (spaces) - 1;
|
||||
memset (spaces, ' ', length);
|
||||
spaces[length] = '\0';
|
||||
if (conditions && conditions[0])
|
||||
{
|
||||
weechat_printf_tags (NULL, "no_trigger",
|
||||
"%s =? %s\"%s%s%s\"",
|
||||
spaces,
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color ("reset"),
|
||||
conditions,
|
||||
weechat_color ("chat_delimiters"));
|
||||
}
|
||||
for (i = 0; i < regex_count; i++)
|
||||
{
|
||||
weechat_printf_tags (NULL, "no_trigger",
|
||||
"%s %d~ %s\"%s%s%s\" --> "
|
||||
"\"%s%s%s\"%s%s%s%s",
|
||||
spaces,
|
||||
i + 1,
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color (weechat_config_string (trigger_config_color_regex)),
|
||||
regex[i].str_regex,
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color (weechat_config_string (trigger_config_color_replace)),
|
||||
regex[i].replace,
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color ("reset"),
|
||||
(regex[i].variable) ? " (" : "",
|
||||
(regex[i].variable) ? regex[i].variable : "",
|
||||
(regex[i].variable) ? ")" : "");
|
||||
}
|
||||
if (commands)
|
||||
{
|
||||
for (i = 0; commands[i]; i++)
|
||||
{
|
||||
weechat_printf_tags (NULL, "no_trigger",
|
||||
"%s %d> %s\"%s%s%s\"",
|
||||
spaces,
|
||||
i + 1,
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color ("reset"),
|
||||
commands[i],
|
||||
weechat_color ("chat_delimiters"));
|
||||
}
|
||||
}
|
||||
if ((return_code >= 0) && (return_code != TRIGGER_RC_OK))
|
||||
{
|
||||
weechat_printf_tags (NULL, "no_trigger",
|
||||
"%s $: %s",
|
||||
spaces,
|
||||
trigger_return_code_string[return_code]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
weechat_printf_tags (
|
||||
NULL, "no_trigger",
|
||||
" %s%s%s: %s%s(%s%s%s)%s (%s%s%d regex, %d %s)",
|
||||
(enabled) ?
|
||||
weechat_color (weechat_config_string (trigger_config_color_trigger)) :
|
||||
weechat_color (weechat_config_string (trigger_config_color_trigger_disabled)),
|
||||
name,
|
||||
weechat_color ("reset"),
|
||||
hook,
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color ("reset"),
|
||||
arguments,
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color ("reset"),
|
||||
(conditions && conditions[0]) ? _("conditions") : "",
|
||||
(conditions && conditions[0]) ? ", " : "",
|
||||
regex_count,
|
||||
commands_count,
|
||||
NG_("command", "commands", commands_count));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Displays a list of triggers.
|
||||
*/
|
||||
|
||||
void
|
||||
trigger_command_list (int full)
|
||||
trigger_command_list (const char *message, int full)
|
||||
{
|
||||
struct t_trigger *ptr_trigger;
|
||||
const char *option;
|
||||
char spaces[256];
|
||||
int i, length, rc;
|
||||
|
||||
if (!triggers)
|
||||
{
|
||||
@@ -48,111 +154,73 @@ trigger_command_list (int full)
|
||||
}
|
||||
|
||||
weechat_printf_tags (NULL, "no_trigger", "");
|
||||
weechat_printf_tags (NULL, "no_trigger", _("List of triggers:"));
|
||||
weechat_printf_tags (NULL, "no_trigger", message);
|
||||
|
||||
for (ptr_trigger = triggers; ptr_trigger;
|
||||
ptr_trigger = ptr_trigger->next_trigger)
|
||||
{
|
||||
if (full)
|
||||
{
|
||||
weechat_printf_tags (
|
||||
NULL, "no_trigger",
|
||||
" %s%s%s: %s%s(%s%s%s)%s",
|
||||
weechat_config_boolean (ptr_trigger->options[TRIGGER_OPTION_ENABLED]) ?
|
||||
weechat_color (weechat_config_string (trigger_config_color_trigger)) :
|
||||
weechat_color (weechat_config_string (trigger_config_color_trigger_disabled)),
|
||||
ptr_trigger->name,
|
||||
weechat_color ("reset"),
|
||||
weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_HOOK]),
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color ("reset"),
|
||||
weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_ARGUMENTS]),
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color ("reset"));
|
||||
length = weechat_strlen_screen (ptr_trigger->name) + 3;
|
||||
if (length >= (int)sizeof (spaces))
|
||||
length = sizeof (spaces) - 1;
|
||||
memset (spaces, ' ', length);
|
||||
spaces[length] = '\0';
|
||||
option = weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_CONDITIONS]);
|
||||
if (option && option[0])
|
||||
{
|
||||
weechat_printf_tags (NULL, "no_trigger",
|
||||
"%s =? %s\"%s%s%s\"",
|
||||
spaces,
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color ("reset"),
|
||||
option,
|
||||
weechat_color ("chat_delimiters"));
|
||||
}
|
||||
for (i = 0; i < ptr_trigger->regex_count; i++)
|
||||
{
|
||||
weechat_printf_tags (NULL, "no_trigger",
|
||||
"%s %d~ %s\"%s%s%s\" --> "
|
||||
"\"%s%s%s\"%s%s%s%s",
|
||||
spaces,
|
||||
i + 1,
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color (weechat_config_string (trigger_config_color_regex)),
|
||||
ptr_trigger->regex[i].str_regex,
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color (weechat_config_string (trigger_config_color_replace)),
|
||||
ptr_trigger->regex[i].replace,
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color ("reset"),
|
||||
(ptr_trigger->regex[i].variable) ? " (" : "",
|
||||
(ptr_trigger->regex[i].variable) ? ptr_trigger->regex[i].variable : "",
|
||||
(ptr_trigger->regex[i].variable) ? ")" : "");
|
||||
}
|
||||
if (ptr_trigger->commands)
|
||||
{
|
||||
for (i = 0; ptr_trigger->commands[i]; i++)
|
||||
{
|
||||
weechat_printf_tags (NULL, "no_trigger",
|
||||
"%s %d> %s\"%s%s%s\"",
|
||||
spaces,
|
||||
i + 1,
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color ("reset"),
|
||||
ptr_trigger->commands[i],
|
||||
weechat_color ("chat_delimiters"));
|
||||
}
|
||||
}
|
||||
rc = weechat_config_integer (ptr_trigger->options[TRIGGER_OPTION_RETURN_CODE]);
|
||||
if (rc != TRIGGER_RC_OK)
|
||||
{
|
||||
weechat_printf_tags (NULL, "no_trigger",
|
||||
"%s $: %s",
|
||||
spaces,
|
||||
trigger_return_code_string[rc]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
option = weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_CONDITIONS]);
|
||||
weechat_printf_tags (
|
||||
NULL, "no_trigger",
|
||||
" %s%s%s: %s%s(%s%s%s)%s (%s%s%d regex, %d %s)",
|
||||
weechat_config_boolean (ptr_trigger->options[TRIGGER_OPTION_ENABLED]) ?
|
||||
weechat_color (weechat_config_string (trigger_config_color_trigger)) :
|
||||
weechat_color (weechat_config_string (trigger_config_color_trigger_disabled)),
|
||||
ptr_trigger->name,
|
||||
weechat_color ("reset"),
|
||||
weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_HOOK]),
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color ("reset"),
|
||||
weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_ARGUMENTS]),
|
||||
weechat_color ("chat_delimiters"),
|
||||
weechat_color ("reset"),
|
||||
(option && option[0]) ? _("conditions") : "",
|
||||
(option && option[0]) ? ", " : "",
|
||||
ptr_trigger->regex_count,
|
||||
ptr_trigger->commands_count,
|
||||
NG_("command", "commands", ptr_trigger->commands_count));
|
||||
}
|
||||
trigger_command_display_trigger (
|
||||
ptr_trigger->name,
|
||||
weechat_config_boolean (ptr_trigger->options[TRIGGER_OPTION_ENABLED]),
|
||||
weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_HOOK]),
|
||||
weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_ARGUMENTS]),
|
||||
weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_CONDITIONS]),
|
||||
ptr_trigger->regex_count,
|
||||
ptr_trigger->regex,
|
||||
ptr_trigger->commands_count,
|
||||
ptr_trigger->commands,
|
||||
weechat_config_integer (ptr_trigger->options[TRIGGER_OPTION_RETURN_CODE]),
|
||||
full);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Displays a list of default triggers.
|
||||
*/
|
||||
|
||||
void
|
||||
trigger_command_list_default (int full)
|
||||
{
|
||||
int i, regex_count, commands_count;
|
||||
struct t_trigger_regex *regex;
|
||||
char **commands;
|
||||
|
||||
regex_count = 0;
|
||||
regex = NULL;
|
||||
commands_count = 0;
|
||||
commands = NULL;
|
||||
|
||||
weechat_printf_tags (NULL, "no_trigger", "");
|
||||
weechat_printf_tags (NULL, "no_trigger", _("List of default triggers:"));
|
||||
|
||||
for (i = 0; trigger_config_default_list[i][0]; i++)
|
||||
{
|
||||
trigger_split_regex (trigger_config_default_list[i][0],
|
||||
trigger_config_default_list[i][5],
|
||||
®ex_count,
|
||||
®ex);
|
||||
trigger_split_command (trigger_config_default_list[i][6],
|
||||
&commands_count,
|
||||
&commands);
|
||||
trigger_command_display_trigger (
|
||||
trigger_config_default_list[i][0],
|
||||
weechat_config_string_to_boolean (trigger_config_default_list[i][1]),
|
||||
trigger_config_default_list[i][2],
|
||||
trigger_config_default_list[i][3],
|
||||
trigger_config_default_list[i][4],
|
||||
regex_count,
|
||||
regex,
|
||||
commands_count,
|
||||
commands,
|
||||
trigger_search_return_code (trigger_config_default_list[i][7]),
|
||||
full);
|
||||
}
|
||||
|
||||
trigger_free_regex (®ex_count, ®ex);
|
||||
if (commands)
|
||||
weechat_string_free_split (commands);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set "enabled" value in a trigger.
|
||||
*
|
||||
@@ -270,14 +338,21 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc,
|
||||
if ((argc == 1)
|
||||
|| ((argc == 2) && (weechat_strcasecmp (argv[1], "list") == 0)))
|
||||
{
|
||||
trigger_command_list (0);
|
||||
trigger_command_list (_("List of triggers:"), 0);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* full list of all triggers */
|
||||
if ((argc == 2) && (weechat_strcasecmp (argv[1], "listfull") == 0))
|
||||
{
|
||||
trigger_command_list (1);
|
||||
trigger_command_list (_("List of triggers:"), 1);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* list of default triggers */
|
||||
if ((argc == 2) && (weechat_strcasecmp (argv[1], "listdefault") == 0))
|
||||
{
|
||||
trigger_command_list_default (1);
|
||||
goto end;
|
||||
}
|
||||
|
||||
@@ -537,6 +612,25 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc,
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* restore default triggers */
|
||||
if (weechat_strcasecmp (argv[1], "default") == 0)
|
||||
{
|
||||
if ((argc >= 3) && (weechat_strcasecmp (argv[2], "-yes") == 0))
|
||||
{
|
||||
trigger_free_all ();
|
||||
trigger_create_default ();
|
||||
trigger_command_list (_("Default triggers restored:"), 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
weechat_printf (NULL,
|
||||
_("%sError: \"-yes\" argument is required for "
|
||||
"restoring default triggers (security reason)"),
|
||||
weechat_prefix ("error"));
|
||||
}
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* open the trigger monitor buffer */
|
||||
if (weechat_strcasecmp (argv[1], "monitor") == 0)
|
||||
{
|
||||
@@ -561,7 +655,7 @@ trigger_command_init ()
|
||||
weechat_hook_command (
|
||||
"trigger",
|
||||
N_("manage triggers"),
|
||||
N_("list|listfull"
|
||||
N_("list|listfull|listdefault"
|
||||
" || add <name> <hook> [\"<arguments>\" [\"<conditions>\" "
|
||||
"[\"<regex>\" [\"<command>\" [\"<return_code>\"]]]]]"
|
||||
" || addinput [<hook>]"
|
||||
@@ -569,8 +663,12 @@ trigger_command_init ()
|
||||
" || rename <name> <new_name>"
|
||||
" || enable|disable|toggle|restart <name>|-all [<name>...]"
|
||||
" || del <name>|-all [<name>...]"
|
||||
" || default -yes"
|
||||
" || monitor"),
|
||||
N_(" add: add a trigger\n"
|
||||
N_(" list: list triggers (without argument, this list is displayed)\n"
|
||||
" listfull: list triggers with detailed info for each trigger\n"
|
||||
"listdefault: list default triggers\n"
|
||||
" add: add a trigger\n"
|
||||
" name: name of trigger\n"
|
||||
" hook: signal, hsignal, modifier, print, timer\n"
|
||||
" arguments: arguments for the hook, depending on hook (separated "
|
||||
@@ -600,6 +698,7 @@ trigger_command_init ()
|
||||
" restart: restart trigger(s) (for timer)\n"
|
||||
" del: delete a trigger\n"
|
||||
" -all: do action on all triggers\n"
|
||||
" default: restore default triggers\n"
|
||||
" monitor: open the trigger monitor buffer\n"
|
||||
"\n"
|
||||
"When a trigger callback is called, following actions are performed, "
|
||||
@@ -626,7 +725,7 @@ trigger_command_init ()
|
||||
"==/(\\S+)/==/${color:italic}$1${color:-italic}/\"\n"
|
||||
" silently save config each hour:\n"
|
||||
" /trigger add cfgsave timer 3600000;0;0 \"\" \"\" \"/mute /save\""),
|
||||
"list|listfull"
|
||||
"list|listfull|listdefault"
|
||||
" || add %(trigger_names) %(trigger_hooks) %(trigger_hook_arguments) "
|
||||
"%(trigger_hook_condition) %(trigger_hook_regex) "
|
||||
"%(trigger_hook_command) %(trigger_hook_rc)"
|
||||
@@ -634,6 +733,7 @@ trigger_command_init ()
|
||||
" || set %(trigger_names) %(trigger_options)|name %(trigger_option_value)"
|
||||
" || rename %(trigger_names) %(trigger_names)"
|
||||
" || enable|disable|toggle|restart|del %(trigger_names)|-all %(trigger_names)|%*"
|
||||
" || default"
|
||||
" || monitor",
|
||||
&trigger_command_trigger, NULL);
|
||||
}
|
||||
|
||||
@@ -42,6 +42,51 @@ struct t_config_option *trigger_config_color_replace;
|
||||
struct t_config_option *trigger_config_color_trigger;
|
||||
struct t_config_option *trigger_config_color_trigger_disabled;
|
||||
|
||||
char *trigger_config_default_list[][1 + TRIGGER_NUM_OPTIONS] =
|
||||
{
|
||||
/* beep on highlight/private message */
|
||||
{ "beep", "on",
|
||||
"print",
|
||||
"",
|
||||
"${tg_highlight} || ${tg_msg_pv}",
|
||||
"",
|
||||
"/print -stderr \\a",
|
||||
"ok" },
|
||||
/* hide passwords in commands */
|
||||
{ "cmd_pass", "on",
|
||||
"modifier",
|
||||
"5000|input_text_display;5000|history_add;5000|irc_command_auth",
|
||||
"",
|
||||
"==^("
|
||||
"(/(msg|quote) +nickserv "
|
||||
"+(id|identify|register|ghost \\S+|release \\S+|regain \\S+) +)|"
|
||||
"/oper +\\S+ +|"
|
||||
"/quote pass +|"
|
||||
"/set +\\S*password\\S* +|"
|
||||
"/secure +(passphrase|decrypt|set \\S+) +)"
|
||||
"(.*)"
|
||||
"==$1$.*+",
|
||||
"",
|
||||
"" },
|
||||
/* hide password in IRC auth message displayed */
|
||||
{ "msg_auth", "on",
|
||||
"modifier",
|
||||
"5000|irc_message_auth",
|
||||
"",
|
||||
"==^(.*(id|identify|register|ghost \\S+|release \\S+) +)(.*)==$1$.*+",
|
||||
"",
|
||||
"" },
|
||||
/* hide server password in commands /server and /connect */
|
||||
{ "server_pass", "on",
|
||||
"modifier",
|
||||
"5000|input_text_display;5000|history_add",
|
||||
"",
|
||||
"==^(/(server|connect) .*-(sasl_)?password=)(\\S+)(.*)==$1$.*4$5"
|
||||
"",
|
||||
"" },
|
||||
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Callback called when trigger option "enabled" is changed.
|
||||
@@ -121,7 +166,8 @@ trigger_config_change_regex (void *data, struct t_config_option *option)
|
||||
if (!ptr_trigger)
|
||||
return;
|
||||
|
||||
trigger_set_regex (ptr_trigger);
|
||||
trigger_split_regex (ptr_trigger->name, weechat_config_string (option),
|
||||
&ptr_trigger->regex_count, &ptr_trigger->regex);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -140,7 +186,9 @@ trigger_config_change_command (void *data, struct t_config_option *option)
|
||||
if (!ptr_trigger)
|
||||
return;
|
||||
|
||||
trigger_set_commands (ptr_trigger);
|
||||
trigger_split_command (weechat_config_string (option),
|
||||
&ptr_trigger->commands_count,
|
||||
&ptr_trigger->commands);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -401,6 +449,47 @@ trigger_config_trigger_read_cb (void *data, struct t_config_file *config_file,
|
||||
return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Writes default triggers in trigger configuration file.
|
||||
*/
|
||||
|
||||
int
|
||||
trigger_config_trigger_write_default_cb (void *data,
|
||||
struct t_config_file *config_file,
|
||||
const char *section_name)
|
||||
{
|
||||
int i, j, quotes;
|
||||
char option_name[512];
|
||||
|
||||
/* make C compiler happy */
|
||||
(void) data;
|
||||
|
||||
if (!weechat_config_write_line (config_file, section_name, NULL))
|
||||
return WEECHAT_CONFIG_WRITE_ERROR;
|
||||
|
||||
for (i = 0; trigger_config_default_list[i][0]; i++)
|
||||
{
|
||||
for (j = 0; j < TRIGGER_NUM_OPTIONS; j++)
|
||||
{
|
||||
snprintf (option_name, sizeof (option_name),
|
||||
"%s.%s",
|
||||
trigger_config_default_list[i][0],
|
||||
trigger_option_string[j]);
|
||||
quotes = (j & (TRIGGER_OPTION_ARGUMENTS | TRIGGER_OPTION_CONDITIONS |
|
||||
TRIGGER_OPTION_REGEX | TRIGGER_OPTION_COMMAND));
|
||||
if (!weechat_config_write_line (config_file, option_name, "%s%s%s",
|
||||
(quotes) ? "\"" : "",
|
||||
trigger_config_default_list[i][j + 1],
|
||||
(quotes) ? "\"" : ""))
|
||||
{
|
||||
return WEECHAT_CONFIG_WRITE_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return WEECHAT_CONFIG_WRITE_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Reloads trigger configuration file.
|
||||
*/
|
||||
@@ -502,7 +591,8 @@ trigger_config_init ()
|
||||
0, 0,
|
||||
&trigger_config_trigger_read_cb, NULL,
|
||||
NULL, NULL,
|
||||
NULL, NULL, NULL, NULL,
|
||||
&trigger_config_trigger_write_default_cb, NULL,
|
||||
NULL, NULL,
|
||||
NULL, NULL);
|
||||
if (!ptr_section)
|
||||
{
|
||||
|
||||
@@ -31,6 +31,8 @@ extern struct t_config_option *trigger_config_color_replace;
|
||||
extern struct t_config_option *trigger_config_color_trigger;
|
||||
extern struct t_config_option *trigger_config_color_trigger_disabled;
|
||||
|
||||
extern char *trigger_config_default_list[][1 + TRIGGER_NUM_OPTIONS];
|
||||
|
||||
extern struct t_config_option *trigger_config_create_option (const char *trigger_name,
|
||||
int index_option,
|
||||
const char *value);
|
||||
|
||||
+123
-92
@@ -388,42 +388,43 @@ trigger_hook (struct t_trigger *trigger)
|
||||
*/
|
||||
|
||||
void
|
||||
trigger_free_regex (struct t_trigger *trigger)
|
||||
trigger_free_regex (int *regex_count, struct t_trigger_regex **regex)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (trigger->regex_count > 0)
|
||||
if (*regex_count > 0)
|
||||
{
|
||||
for (i = 0; i < trigger->regex_count; i++)
|
||||
for (i = 0; i < *regex_count; i++)
|
||||
{
|
||||
if (trigger->regex[i].variable)
|
||||
free (trigger->regex[i].variable);
|
||||
if (trigger->regex[i].str_regex)
|
||||
free (trigger->regex[i].str_regex);
|
||||
if (trigger->regex[i].regex)
|
||||
if ((*regex)[i].variable)
|
||||
free ((*regex)[i].variable);
|
||||
if ((*regex)[i].str_regex)
|
||||
free ((*regex)[i].str_regex);
|
||||
if ((*regex)[i].regex)
|
||||
{
|
||||
regfree (trigger->regex[i].regex);
|
||||
free (trigger->regex[i].regex);
|
||||
regfree ((*regex)[i].regex);
|
||||
free ((*regex)[i].regex);
|
||||
}
|
||||
if (trigger->regex[i].replace)
|
||||
free (trigger->regex[i].replace);
|
||||
if (trigger->regex[i].replace_eval)
|
||||
free (trigger->regex[i].replace_eval);
|
||||
if ((*regex)[i].replace)
|
||||
free ((*regex)[i].replace);
|
||||
if ((*regex)[i].replace_eval)
|
||||
free ((*regex)[i].replace_eval);
|
||||
}
|
||||
free (trigger->regex);
|
||||
trigger->regex = NULL;
|
||||
trigger->regex_count = 0;
|
||||
free (*regex);
|
||||
*regex = NULL;
|
||||
*regex_count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Sets the regex and replacement text in a trigger.
|
||||
* Splits the regex in structures, with regex and replacement text.
|
||||
*/
|
||||
|
||||
void
|
||||
trigger_set_regex (struct t_trigger *trigger)
|
||||
trigger_split_regex (const char *trigger_name, const char *str_regex,
|
||||
int *regex_count, struct t_trigger_regex **regex)
|
||||
{
|
||||
const char *option_regex, *ptr_option, *pos, *pos_replace, *pos_replace_end;
|
||||
const char *ptr_regex, *pos, *pos_replace, *pos_replace_end;
|
||||
const char *pos_next_regex;
|
||||
char *delimiter;
|
||||
int index, length_delimiter;
|
||||
@@ -431,21 +432,22 @@ trigger_set_regex (struct t_trigger *trigger)
|
||||
|
||||
delimiter = NULL;
|
||||
|
||||
/* remove all regex in the trigger */
|
||||
trigger_free_regex (trigger);
|
||||
if (!regex_count || !regex)
|
||||
goto end;
|
||||
|
||||
/* get regex option in trigger */
|
||||
option_regex = weechat_config_string (trigger->options[TRIGGER_OPTION_REGEX]);
|
||||
if (!option_regex || !option_regex[0])
|
||||
/* remove any existing regex */
|
||||
trigger_free_regex (regex_count, regex);
|
||||
|
||||
if (!str_regex || !str_regex[0])
|
||||
goto end;
|
||||
|
||||
/* min 3 chars, for example: "/a/" */
|
||||
if (strlen (option_regex) < 3)
|
||||
if (strlen (str_regex) < 3)
|
||||
goto format_error;
|
||||
|
||||
/* parse regular expressions in the option */
|
||||
ptr_option = option_regex;
|
||||
while (ptr_option && ptr_option[0])
|
||||
ptr_regex = str_regex;
|
||||
while (ptr_regex && ptr_regex[0])
|
||||
{
|
||||
if (delimiter)
|
||||
{
|
||||
@@ -454,14 +456,14 @@ trigger_set_regex (struct t_trigger *trigger)
|
||||
}
|
||||
|
||||
/* search the delimiter (which can be more than one char) */
|
||||
pos = weechat_utf8_next_char (ptr_option);
|
||||
while (pos[0] && (weechat_utf8_charcmp (ptr_option, pos) == 0))
|
||||
pos = weechat_utf8_next_char (ptr_regex);
|
||||
while (pos[0] && (weechat_utf8_charcmp (ptr_regex, pos) == 0))
|
||||
{
|
||||
pos = weechat_utf8_next_char (pos);
|
||||
}
|
||||
if (!pos[0])
|
||||
goto format_error;
|
||||
delimiter = weechat_strndup (ptr_option, pos - ptr_option);
|
||||
delimiter = weechat_strndup (ptr_regex, pos - ptr_regex);
|
||||
if (!delimiter)
|
||||
goto memory_error;
|
||||
if ((strcmp (delimiter, "\\") == 0) || (strcmp (delimiter, "(") == 0))
|
||||
@@ -469,96 +471,96 @@ trigger_set_regex (struct t_trigger *trigger)
|
||||
|
||||
length_delimiter = strlen (delimiter);
|
||||
|
||||
ptr_option = pos;
|
||||
if (!ptr_option[0])
|
||||
ptr_regex = pos;
|
||||
if (!ptr_regex[0])
|
||||
goto format_error;
|
||||
|
||||
/* search the start of replacement string */
|
||||
pos_replace = strstr (ptr_option, delimiter);
|
||||
pos_replace = strstr (ptr_regex, delimiter);
|
||||
if (!pos_replace)
|
||||
goto format_error;
|
||||
|
||||
/* search the end of replacement string */
|
||||
pos_replace_end = strstr (pos_replace + length_delimiter, delimiter);
|
||||
|
||||
new_regex = realloc (trigger->regex,
|
||||
(trigger->regex_count + 1) * sizeof (trigger->regex[0]));
|
||||
new_regex = realloc (*regex,
|
||||
(*regex_count + 1) * sizeof ((*regex)[0]));
|
||||
if (!new_regex)
|
||||
goto memory_error;
|
||||
|
||||
trigger->regex = new_regex;
|
||||
trigger->regex_count++;
|
||||
index = trigger->regex_count - 1;
|
||||
*regex = new_regex;
|
||||
(*regex_count)++;
|
||||
index = *regex_count - 1;
|
||||
|
||||
/* initialize new regex */
|
||||
trigger->regex[index].variable = NULL;
|
||||
trigger->regex[index].str_regex = NULL;
|
||||
trigger->regex[index].regex = NULL;
|
||||
trigger->regex[index].replace = NULL;
|
||||
trigger->regex[index].replace_eval = NULL;
|
||||
(*regex)[index].variable = NULL;
|
||||
(*regex)[index].str_regex = NULL;
|
||||
(*regex)[index].regex = NULL;
|
||||
(*regex)[index].replace = NULL;
|
||||
(*regex)[index].replace_eval = NULL;
|
||||
|
||||
/* set string with regex */
|
||||
trigger->regex[index].str_regex = weechat_strndup (ptr_option,
|
||||
pos_replace - ptr_option);
|
||||
if (!trigger->regex[index].str_regex)
|
||||
(*regex)[index].str_regex = weechat_strndup (ptr_regex,
|
||||
pos_replace - ptr_regex);
|
||||
if (!(*regex)[index].str_regex)
|
||||
goto memory_error;
|
||||
|
||||
/* set regex */
|
||||
trigger->regex[index].regex = malloc (sizeof (*trigger->regex[index].regex));
|
||||
if (!trigger->regex[index].regex)
|
||||
(*regex)[index].regex = malloc (sizeof (*(*regex)[index].regex));
|
||||
if (!(*regex)[index].regex)
|
||||
goto memory_error;
|
||||
if (weechat_string_regcomp (trigger->regex[index].regex,
|
||||
trigger->regex[index].str_regex,
|
||||
if (weechat_string_regcomp ((*regex)[index].regex,
|
||||
(*regex)[index].str_regex,
|
||||
REG_EXTENDED | REG_ICASE) != 0)
|
||||
{
|
||||
weechat_printf (NULL,
|
||||
_("%s%s: error compiling regular expression \"%s\""),
|
||||
weechat_prefix ("error"), TRIGGER_PLUGIN_NAME,
|
||||
trigger->regex[index].str_regex);
|
||||
free (trigger->regex[index].regex);
|
||||
trigger->regex[index].regex = NULL;
|
||||
(*regex)[index].str_regex);
|
||||
free ((*regex)[index].regex);
|
||||
(*regex)[index].regex = NULL;
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* set replace and replace_eval */
|
||||
trigger->regex[index].replace = (pos_replace_end) ?
|
||||
(*regex)[index].replace = (pos_replace_end) ?
|
||||
weechat_strndup (pos_replace + length_delimiter,
|
||||
pos_replace_end - pos_replace - length_delimiter) :
|
||||
strdup (pos_replace + length_delimiter);
|
||||
if (!trigger->regex[index].replace)
|
||||
if (!(*regex)[index].replace)
|
||||
goto memory_error;
|
||||
trigger->regex[index].replace_eval =
|
||||
weechat_string_eval_expression (trigger->regex[index].replace,
|
||||
(*regex)[index].replace_eval =
|
||||
weechat_string_eval_expression ((*regex)[index].replace,
|
||||
NULL, NULL, NULL);
|
||||
if (!trigger->regex[index].replace_eval)
|
||||
if (!(*regex)[index].replace_eval)
|
||||
goto memory_error;
|
||||
|
||||
if (!pos_replace_end)
|
||||
break;
|
||||
|
||||
/* set variable (optional) */
|
||||
ptr_option = pos_replace_end + length_delimiter;
|
||||
if (!ptr_option[0])
|
||||
ptr_regex = pos_replace_end + length_delimiter;
|
||||
if (!ptr_regex[0])
|
||||
break;
|
||||
if (ptr_option[0] == ' ')
|
||||
pos_next_regex = ptr_option;
|
||||
if (ptr_regex[0] == ' ')
|
||||
pos_next_regex = ptr_regex;
|
||||
else
|
||||
{
|
||||
pos_next_regex = strchr (ptr_option, ' ');
|
||||
trigger->regex[index].variable = (pos_next_regex) ?
|
||||
weechat_strndup (ptr_option, pos_next_regex - ptr_option) :
|
||||
strdup (ptr_option);
|
||||
if (!trigger->regex[index].variable)
|
||||
pos_next_regex = strchr (ptr_regex, ' ');
|
||||
(*regex)[index].variable = (pos_next_regex) ?
|
||||
weechat_strndup (ptr_regex, pos_next_regex - ptr_regex) :
|
||||
strdup (ptr_regex);
|
||||
if (!(*regex)[index].variable)
|
||||
goto memory_error;
|
||||
}
|
||||
if (!pos_next_regex)
|
||||
break;
|
||||
|
||||
/* skip spaces before next regex */
|
||||
ptr_option = pos_next_regex + 1;
|
||||
while (ptr_option[0] == ' ')
|
||||
ptr_regex = pos_next_regex + 1;
|
||||
while (ptr_regex[0] == ' ')
|
||||
{
|
||||
ptr_option++;
|
||||
ptr_regex++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -569,15 +571,15 @@ format_error:
|
||||
_("%s%s: invalid value for option \"regex\", "
|
||||
"see /help trigger.trigger.%s.regex"),
|
||||
weechat_prefix ("error"), TRIGGER_PLUGIN_NAME,
|
||||
trigger->name);
|
||||
trigger_free_regex (trigger);
|
||||
trigger_name);
|
||||
trigger_free_regex (regex_count, regex);
|
||||
goto end;
|
||||
|
||||
memory_error:
|
||||
weechat_printf (NULL,
|
||||
_("%s%s: not enough memory"),
|
||||
weechat_prefix ("error"), TRIGGER_PLUGIN_NAME);
|
||||
trigger_free_regex (trigger);
|
||||
trigger_free_regex (regex_count, regex);
|
||||
goto end;
|
||||
|
||||
end:
|
||||
@@ -586,32 +588,34 @@ end:
|
||||
}
|
||||
|
||||
/*
|
||||
* Sets the commands in a trigger.
|
||||
* Splits command of a trigger.
|
||||
*/
|
||||
|
||||
void
|
||||
trigger_set_commands (struct t_trigger *trigger)
|
||||
trigger_split_command (const char *command,
|
||||
int *commands_count, char ***commands)
|
||||
{
|
||||
const char *option_command;
|
||||
int i;
|
||||
|
||||
if (trigger->commands)
|
||||
{
|
||||
weechat_string_free_split (trigger->commands);
|
||||
trigger->commands = NULL;
|
||||
}
|
||||
trigger->commands_count = 0;
|
||||
if (!commands_count || !commands)
|
||||
return;
|
||||
|
||||
option_command = weechat_config_string (trigger->options[TRIGGER_OPTION_COMMAND]);
|
||||
if (option_command && option_command[0])
|
||||
if (*commands)
|
||||
{
|
||||
trigger->commands = weechat_string_split_command (option_command, ';');
|
||||
if (trigger->commands)
|
||||
weechat_string_free_split (*commands);
|
||||
*commands = NULL;
|
||||
}
|
||||
*commands_count = 0;
|
||||
|
||||
if (command && command[0])
|
||||
{
|
||||
*commands = weechat_string_split_command (command, ';');
|
||||
if (*commands)
|
||||
{
|
||||
for (i = 0; trigger->commands[i]; i++)
|
||||
for (i = 0; (*commands)[i]; i++)
|
||||
{
|
||||
}
|
||||
trigger->commands_count = i;
|
||||
*commands_count = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -761,8 +765,13 @@ trigger_new_with_options (const char *name, struct t_config_option **options)
|
||||
trigger_add (new_trigger, &triggers, &last_trigger);
|
||||
triggers_count++;
|
||||
|
||||
trigger_set_regex (new_trigger);
|
||||
trigger_set_commands (new_trigger);
|
||||
trigger_split_regex (new_trigger->name,
|
||||
weechat_config_string (new_trigger->options[TRIGGER_OPTION_REGEX]),
|
||||
&new_trigger->regex_count,
|
||||
&new_trigger->regex);
|
||||
trigger_split_command (weechat_config_string (new_trigger->options[TRIGGER_OPTION_COMMAND]),
|
||||
&new_trigger->commands_count,
|
||||
&new_trigger->commands);
|
||||
|
||||
if (weechat_config_boolean (new_trigger->options[TRIGGER_OPTION_ENABLED]))
|
||||
trigger_hook (new_trigger);
|
||||
@@ -822,6 +831,28 @@ trigger_new (const char *name, const char *enabled, const char *hook,
|
||||
return new_trigger;
|
||||
}
|
||||
|
||||
/*
|
||||
* Creates default triggers.
|
||||
*/
|
||||
|
||||
void
|
||||
trigger_create_default ()
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; trigger_config_default_list[i][0]; i++)
|
||||
{
|
||||
trigger_new (trigger_config_default_list[i][0], /* name */
|
||||
trigger_config_default_list[i][1], /* enabled */
|
||||
trigger_config_default_list[i][2], /* hook */
|
||||
trigger_config_default_list[i][3], /* arguments */
|
||||
trigger_config_default_list[i][4], /* conditions */
|
||||
trigger_config_default_list[i][5], /* regex */
|
||||
trigger_config_default_list[i][6], /* command */
|
||||
trigger_config_default_list[i][7]); /* return code */
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Renames a trigger.
|
||||
*
|
||||
@@ -903,7 +934,7 @@ trigger_free (struct t_trigger *trigger)
|
||||
|
||||
/* free data */
|
||||
trigger_unhook (trigger);
|
||||
trigger_free_regex (trigger);
|
||||
trigger_free_regex (&trigger->regex_count, &trigger->regex);
|
||||
if (trigger->name)
|
||||
free (trigger->name);
|
||||
for (i = 0; i < TRIGGER_NUM_OPTIONS; i++)
|
||||
|
||||
@@ -120,8 +120,14 @@ extern int trigger_search_hook_type (const char *type);
|
||||
extern int trigger_search_return_code (const char *return_code);
|
||||
extern struct t_trigger *trigger_search (const char *name);
|
||||
extern struct t_trigger *trigger_search_with_option (struct t_config_option *option);
|
||||
extern void trigger_set_regex (struct t_trigger *trigger);
|
||||
extern void trigger_set_commands (struct t_trigger *trigger);
|
||||
extern void trigger_free_regex (int *regex_count,
|
||||
struct t_trigger_regex **regex);
|
||||
extern void trigger_split_regex (const char *trigger_name,
|
||||
const char *str_regex,
|
||||
int *regex_count,
|
||||
struct t_trigger_regex **regex);
|
||||
extern void trigger_split_command (const char *command,
|
||||
int *commands_count, char ***commands);
|
||||
extern void trigger_unhook (struct t_trigger *trigger);
|
||||
extern void trigger_hook (struct t_trigger *trigger);
|
||||
extern int trigger_name_valid (const char *name);
|
||||
@@ -139,6 +145,7 @@ extern struct t_trigger *trigger_new (const char *name,
|
||||
const char *replace,
|
||||
const char *command,
|
||||
const char *return_code);
|
||||
extern void trigger_create_default ();
|
||||
extern int trigger_rename (struct t_trigger *trigger, const char *name);
|
||||
extern void trigger_free (struct t_trigger *trigger);
|
||||
extern void trigger_free_all ();
|
||||
|
||||
Reference in New Issue
Block a user