summaryrefslogtreecommitdiff
path: root/src/plugins
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2014-10-26 12:30:13 +0100
committerSébastien Helleu <flashcode@flashtux.org>2014-10-26 12:30:13 +0100
commit3f5a810254db47c0943007ca7ff749f9b78145ee (patch)
treeab0d898adb0e33b9cd8fbc110174250b7b3b06c3 /src/plugins
parenta012eefb77c246d294b99afcee2ef065258e7e4e (diff)
downloadweechat-3f5a810254db47c0943007ca7ff749f9b78145ee.zip
trigger: evaluate and replace regex groups at same time, new format for regex option in triggers (incompatible with version 1.0) (closes #224)
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/trigger/trigger-callback.c124
-rw-r--r--src/plugins/trigger/trigger-config.c13
2 files changed, 85 insertions, 52 deletions
diff --git a/src/plugins/trigger/trigger-callback.c b/src/plugins/trigger/trigger-callback.c
index 79782369d..16a5ceef5 100644
--- a/src/plugins/trigger/trigger-callback.c
+++ b/src/plugins/trigger/trigger-callback.c
@@ -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);
}
diff --git a/src/plugins/trigger/trigger-config.c b/src/plugins/trigger/trigger-config.c
index 5ab24a268..91782c962 100644
--- a/src/plugins/trigger/trigger-config.c
+++ b/src/plugins/trigger/trigger-config.c
@@ -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;