1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-27 21:36:37 +02:00

trigger: evaluate and replace regex groups at same time, new format for regex option in triggers (incompatible with version 1.0) (closes #224)

This commit is contained in:
Sébastien Helleu
2014-10-26 12:30:13 +01:00
parent a012eefb77
commit 3f5a810254
22 changed files with 238 additions and 161 deletions
+77 -47
View File
@@ -30,8 +30,11 @@
#include "trigger-buffer.h"
/* one hashtable by hook, used in callback to evaluate "conditions" */
struct t_hashtable *trigger_callback_hashtable_options = NULL;
/* hashtable used to evaluate "conditions" */
struct t_hashtable *trigger_callback_hashtable_options_conditions = NULL;
/* hashtable used to replace with regex */
struct t_hashtable *trigger_callback_hashtable_options_regex = NULL;
/*
@@ -148,10 +151,11 @@ trigger_callback_check_conditions (struct t_trigger *trigger,
if (!conditions || !conditions[0])
return 1;
value = weechat_string_eval_expression (conditions,
pointers,
extra_vars,
trigger_callback_hashtable_options);
value = weechat_string_eval_expression (
conditions,
pointers,
extra_vars,
trigger_callback_hashtable_options_conditions);
rc = (value && (strcmp (value, "1") == 0));
if (value)
free (value);
@@ -169,13 +173,27 @@ trigger_callback_replace_regex (struct t_trigger *trigger,
struct t_hashtable *extra_vars,
int display_monitor)
{
char *value, *replace_eval;
char *value;
const char *ptr_key, *ptr_value;
int i;
int i, pointers_allocated;
pointers_allocated = 0;
if (trigger->regex_count == 0)
return;
if (!pointers)
{
pointers = weechat_hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER,
NULL,
NULL);
if (!pointers)
return;
pointers_allocated = 1;
}
for (i = 0; i < trigger->regex_count; i++)
{
/* if regex is not set (invalid), skip it */
@@ -208,43 +226,45 @@ trigger_callback_replace_regex (struct t_trigger *trigger,
continue;
}
replace_eval = weechat_string_eval_expression (
trigger->regex[i].replace_escaped,
weechat_hashtable_set (pointers, "regex", trigger->regex[i].regex);
weechat_hashtable_set (trigger_callback_hashtable_options_regex,
"regex_replace",
trigger->regex[i].replace_escaped);
value = weechat_string_eval_expression (
ptr_value,
pointers,
extra_vars,
NULL);
if (replace_eval)
trigger_callback_hashtable_options_regex);
if (value)
{
value = weechat_string_replace_regex (ptr_value,
trigger->regex[i].regex,
replace_eval,
'$',
NULL, NULL);
if (value)
/* display debug info on trigger buffer */
if (trigger_buffer && display_monitor)
{
/* display debug info on trigger buffer */
if (trigger_buffer && display_monitor)
{
weechat_printf_tags (trigger_buffer, "no_trigger",
"\t regex %d %s(%s%s%s)%s: "
"%s\"%s%s%s\"",
i + 1,
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
ptr_key,
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
value,
weechat_color ("chat_delimiters"));
}
weechat_hashtable_set (extra_vars, ptr_key, value);
free (value);
weechat_printf_tags (trigger_buffer, "no_trigger",
"\t regex %d %s(%s%s%s)%s: "
"%s\"%s%s%s\"",
i + 1,
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
ptr_key,
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
value,
weechat_color ("chat_delimiters"));
}
free (replace_eval);
weechat_hashtable_set (extra_vars, ptr_key, value);
free (value);
}
}
if (pointers_allocated)
weechat_hashtable_free (pointers);
else
weechat_hashtable_remove (pointers, "regex");
}
/*
@@ -912,16 +932,24 @@ end:
void
trigger_callback_init ()
{
trigger_callback_hashtable_options = weechat_hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
if (trigger_callback_hashtable_options)
trigger_callback_hashtable_options_conditions = weechat_hashtable_new (
32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
if (trigger_callback_hashtable_options_conditions)
{
weechat_hashtable_set (trigger_callback_hashtable_options,
weechat_hashtable_set (trigger_callback_hashtable_options_conditions,
"type", "condition");
}
trigger_callback_hashtable_options_regex = weechat_hashtable_new (
32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
}
/*
@@ -931,6 +959,8 @@ trigger_callback_init ()
void
trigger_callback_end ()
{
if (trigger_callback_hashtable_options)
weechat_hashtable_free (trigger_callback_hashtable_options);
if (trigger_callback_hashtable_options_conditions)
weechat_hashtable_free (trigger_callback_hashtable_options_conditions);
if (trigger_callback_hashtable_options_regex)
weechat_hashtable_free (trigger_callback_hashtable_options_regex);
}
+8 -5
View File
@@ -70,7 +70,7 @@ char *trigger_config_default_list[][1 + TRIGGER_NUM_OPTIONS] =
"/set +[^ ]*password[^ ]* +|"
"/secure +(passphrase|decrypt|set +[^ ]+) +)"
"(.*)"
"==$1$.*+",
"==${re:1}${hide:*,${re:+}}",
"",
"" },
/* hide password in IRC auth message displayed */
@@ -78,7 +78,8 @@ char *trigger_config_default_list[][1 + TRIGGER_NUM_OPTIONS] =
"modifier",
"5000|irc_message_auth",
"",
"==^(.*(id|identify|register|ghost +[^ ]+|release +[^ ]+) +)(.*)==$1$.*+",
"==^(.*(id|identify|register|ghost +[^ ]+|release +[^ ]+) +)(.*)"
"==${re:1}${hide:*,${re:+}}",
"",
"" },
/* hide server password in commands /server and /connect */
@@ -86,7 +87,8 @@ char *trigger_config_default_list[][1 + TRIGGER_NUM_OPTIONS] =
"modifier",
"5000|input_text_display;5000|history_add",
"",
"==^(/(server|connect) .*-(sasl_)?password=)([^ ]+)(.*)==$1$.*4$5"
"==^(/(server|connect) .*-(sasl_)?password=)([^ ]+)(.*)"
"==${re:1}${hide:*,${re:4}}${re:5}"
"",
"" },
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
@@ -314,8 +316,9 @@ trigger_config_create_trigger_option (const char *trigger_name, int index_option
"chars are interpreted in the regex (for example \"\\n\"); "
"the separator \"/\" can be replaced by any char (one or "
"more identical chars); matching groups can be used in "
"replace: $0 to $99, $+ for last match and $.cN to replace "
"all chars of group N by char c"),
"replace: ${re:0} to ${re:99}, ${re:+} for last match and "
"${hide:c,${re:N}} to replace all chars of group N by "
"char 'c'"),
NULL, 0, 0, value, NULL, 0, NULL, NULL,
&trigger_config_change_trigger_regex, NULL, NULL, NULL);
break;