mirror of
https://github.com/weechat/weechat.git
synced 2026-06-28 13:56:37 +02:00
trigger: do not create the trigger with /trigger add if the regex is invalid
This commit is contained in:
@@ -285,10 +285,10 @@ trigger_command_list_default (int verbose)
|
||||
|
||||
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);
|
||||
if (trigger_regex_split (trigger_config_default_list[i][5],
|
||||
®ex_count,
|
||||
®ex) < 0)
|
||||
continue;
|
||||
trigger_split_command (trigger_config_default_list[i][6],
|
||||
&commands_count,
|
||||
&commands);
|
||||
@@ -309,7 +309,7 @@ trigger_command_list_default (int verbose)
|
||||
verbose);
|
||||
}
|
||||
|
||||
trigger_free_regex (®ex_count, ®ex);
|
||||
trigger_regex_free (®ex_count, ®ex);
|
||||
if (commands)
|
||||
weechat_string_free_split (commands);
|
||||
}
|
||||
@@ -430,8 +430,10 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc,
|
||||
char **argv, char **argv_eol)
|
||||
{
|
||||
struct t_trigger *ptr_trigger, *ptr_trigger2;
|
||||
struct t_trigger_regex *regex;
|
||||
char *value, **sargv, **items, input[1024], str_pos[16];
|
||||
int rc, i, type, count, index_option, enable, sargc, num_items, add_rc;
|
||||
int regex_count, regex_rc;
|
||||
|
||||
/* make C compiler happy */
|
||||
(void) data;
|
||||
@@ -484,6 +486,39 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc,
|
||||
weechat_prefix ("error"), sargv[1]);
|
||||
goto end;
|
||||
}
|
||||
if ((sargc > 4) && sargv[4][0])
|
||||
{
|
||||
regex_count = 0;
|
||||
regex = NULL;
|
||||
regex_rc = trigger_regex_split (sargv[4], ®ex_count, ®ex);
|
||||
trigger_regex_free (®ex_count, ®ex);
|
||||
switch (regex_rc)
|
||||
{
|
||||
case 0: /* OK */
|
||||
break;
|
||||
case -1: /* format error */
|
||||
weechat_printf (NULL,
|
||||
_("%sError: invalid format for regular "
|
||||
"expression"),
|
||||
weechat_prefix ("error"));
|
||||
goto end;
|
||||
break;
|
||||
case -2: /* regex compilation error */
|
||||
weechat_printf (NULL,
|
||||
_("%sError: invalid regular expression "
|
||||
"(compilation failed)"),
|
||||
weechat_prefix ("error"));
|
||||
goto end;
|
||||
break;
|
||||
case -3: /* memory error */
|
||||
weechat_printf (NULL,
|
||||
_("%s%s: not enough memory"),
|
||||
weechat_prefix ("error"),
|
||||
TRIGGER_PLUGIN_NAME);
|
||||
goto end;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ((sargc > 6) && sargv[6][0]
|
||||
&& (trigger_search_return_code (sargv[6]) < 0))
|
||||
{
|
||||
|
||||
@@ -186,8 +186,30 @@ trigger_config_change_trigger_regex (void *data, struct t_config_option *option)
|
||||
if (!ptr_trigger)
|
||||
return;
|
||||
|
||||
trigger_split_regex (ptr_trigger->name, weechat_config_string (option),
|
||||
&ptr_trigger->regex_count, &ptr_trigger->regex);
|
||||
switch (trigger_regex_split (weechat_config_string (option),
|
||||
&ptr_trigger->regex_count,
|
||||
&ptr_trigger->regex))
|
||||
{
|
||||
case 0: /* OK */
|
||||
break;
|
||||
case -1: /* format error */
|
||||
weechat_printf (NULL,
|
||||
_("%sError: invalid format for option \"regex\", "
|
||||
"see /help trigger.trigger.%s.regex"),
|
||||
weechat_prefix ("error"), ptr_trigger->name);
|
||||
break;
|
||||
case -2: /* regex compilation error */
|
||||
weechat_printf (NULL,
|
||||
_("%sError: invalid regular expression in option "
|
||||
"\"regex\", see /help trigger.trigger.%s.regex"),
|
||||
weechat_prefix ("error"), ptr_trigger->name);
|
||||
break;
|
||||
case -3: /* memory error */
|
||||
weechat_printf (NULL,
|
||||
_("%s%s: not enough memory"),
|
||||
weechat_prefix ("error"), TRIGGER_PLUGIN_NAME);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -449,7 +449,7 @@ trigger_hook (struct t_trigger *trigger)
|
||||
*/
|
||||
|
||||
void
|
||||
trigger_free_regex (int *regex_count, struct t_trigger_regex **regex)
|
||||
trigger_regex_free (int *regex_count, struct t_trigger_regex **regex)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -479,25 +479,32 @@ trigger_free_regex (int *regex_count, struct t_trigger_regex **regex)
|
||||
|
||||
/*
|
||||
* Splits the regex in structures, with regex and replacement text.
|
||||
*
|
||||
* Returns:
|
||||
* 0: OK
|
||||
* -1: format error
|
||||
* -2: regex compilation error
|
||||
* -3: not enough memory
|
||||
*/
|
||||
|
||||
void
|
||||
trigger_split_regex (const char *trigger_name, const char *str_regex,
|
||||
int
|
||||
trigger_regex_split (const char *str_regex,
|
||||
int *regex_count, struct t_trigger_regex **regex)
|
||||
{
|
||||
const char *ptr_regex, *pos, *pos_replace, *pos_replace_end;
|
||||
const char *pos_next_regex;
|
||||
char *delimiter;
|
||||
int index, length_delimiter;
|
||||
int rc, index, length_delimiter;
|
||||
struct t_trigger_regex *new_regex;
|
||||
|
||||
rc = 0;
|
||||
delimiter = NULL;
|
||||
|
||||
if (!regex_count || !regex)
|
||||
goto end;
|
||||
|
||||
/* remove any existing regex */
|
||||
trigger_free_regex (regex_count, regex);
|
||||
trigger_regex_free (regex_count, regex);
|
||||
|
||||
if (!str_regex || !str_regex[0])
|
||||
goto end;
|
||||
@@ -574,13 +581,15 @@ trigger_split_regex (const char *trigger_name, const char *str_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,
|
||||
(*regex)[index].str_regex);
|
||||
*/
|
||||
free ((*regex)[index].regex);
|
||||
(*regex)[index].regex = NULL;
|
||||
goto end;
|
||||
goto compile_error;
|
||||
}
|
||||
|
||||
/* set replace and replace_eval */
|
||||
@@ -627,24 +636,24 @@ trigger_split_regex (const char *trigger_name, const char *str_regex,
|
||||
goto end;
|
||||
|
||||
format_error:
|
||||
weechat_printf (NULL,
|
||||
_("%s%s: invalid value for option \"regex\", "
|
||||
"see /help trigger.trigger.%s.regex"),
|
||||
weechat_prefix ("error"), TRIGGER_PLUGIN_NAME,
|
||||
trigger_name);
|
||||
trigger_free_regex (regex_count, regex);
|
||||
rc = -1;
|
||||
goto end;
|
||||
|
||||
compile_error:
|
||||
rc = -2;
|
||||
goto end;
|
||||
|
||||
memory_error:
|
||||
weechat_printf (NULL,
|
||||
_("%s%s: not enough memory"),
|
||||
weechat_prefix ("error"), TRIGGER_PLUGIN_NAME);
|
||||
trigger_free_regex (regex_count, regex);
|
||||
rc = -3;
|
||||
goto end;
|
||||
|
||||
end:
|
||||
if (delimiter)
|
||||
free (delimiter);
|
||||
if (rc < 0)
|
||||
trigger_regex_free (regex_count, regex);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -825,10 +834,16 @@ trigger_new_with_options (const char *name, struct t_config_option **options)
|
||||
trigger_add (new_trigger, &triggers, &last_trigger);
|
||||
triggers_count++;
|
||||
|
||||
trigger_split_regex (new_trigger->name,
|
||||
weechat_config_string (new_trigger->options[TRIGGER_OPTION_REGEX]),
|
||||
&new_trigger->regex_count,
|
||||
&new_trigger->regex);
|
||||
if (trigger_regex_split (weechat_config_string (new_trigger->options[TRIGGER_OPTION_REGEX]),
|
||||
&new_trigger->regex_count,
|
||||
&new_trigger->regex) < 0)
|
||||
{
|
||||
weechat_printf (NULL,
|
||||
_("%sError: invalid regular expression in trigger "
|
||||
"\"%s\""),
|
||||
weechat_prefix ("error"),
|
||||
name);
|
||||
}
|
||||
trigger_split_command (weechat_config_string (new_trigger->options[TRIGGER_OPTION_COMMAND]),
|
||||
&new_trigger->commands_count,
|
||||
&new_trigger->commands);
|
||||
@@ -1020,7 +1035,7 @@ trigger_free (struct t_trigger *trigger)
|
||||
|
||||
/* free data */
|
||||
trigger_unhook (trigger);
|
||||
trigger_free_regex (&trigger->regex_count, &trigger->regex);
|
||||
trigger_regex_free (&trigger->regex_count, &trigger->regex);
|
||||
if (trigger->name)
|
||||
free (trigger->name);
|
||||
for (i = 0; i < TRIGGER_NUM_OPTIONS; i++)
|
||||
|
||||
@@ -126,12 +126,11 @@ 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_free_regex (int *regex_count,
|
||||
extern void trigger_regex_free (int *regex_count,
|
||||
struct t_trigger_regex **regex);
|
||||
extern int trigger_regex_split (const char *str_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);
|
||||
|
||||
Reference in New Issue
Block a user