summaryrefslogtreecommitdiff
path: root/src/plugins/trigger
diff options
context:
space:
mode:
authorSebastien Helleu <flashcode@flashtux.org>2014-02-13 11:33:16 +0100
committerSebastien Helleu <flashcode@flashtux.org>2014-02-13 11:33:16 +0100
commit00402b87c27fe60460ffcc68da287a9932b13bd6 (patch)
tree3bae4b25110cf086cdbfb75c45968c2672c0bf21 /src/plugins/trigger
parentbc3eaa8d6209d448b0eef6e5a39774da8043b272 (diff)
downloadweechat-00402b87c27fe60460ffcc68da287a9932b13bd6.zip
trigger: add hook command
Diffstat (limited to 'src/plugins/trigger')
-rw-r--r--src/plugins/trigger/trigger-callback.c69
-rw-r--r--src/plugins/trigger/trigger-callback.h2
-rw-r--r--src/plugins/trigger/trigger-command.c30
-rw-r--r--src/plugins/trigger/trigger-completion.c35
-rw-r--r--src/plugins/trigger/trigger.c102
-rw-r--r--src/plugins/trigger/trigger.h8
6 files changed, 173 insertions, 73 deletions
diff --git a/src/plugins/trigger/trigger-callback.c b/src/plugins/trigger/trigger-callback.c
index 3277ef366..a0bd98f54 100644
--- a/src/plugins/trigger/trigger-callback.c
+++ b/src/plugins/trigger/trigger-callback.c
@@ -67,7 +67,8 @@ trigger_callback_set_tags (struct t_gui_buffer *buffer,
if (strcmp (tags[i] + 7, "private") == 0)
{
snprintf (str_temp, sizeof (str_temp), "%d",
- (localvar_type && (strcmp (localvar_type, "private") == 0)) ? 1 : 0);
+ (localvar_type
+ && (strcmp (localvar_type, "private") == 0)) ? 1 : 0);
weechat_hashtable_set (extra_vars, "tg_msg_pv", str_temp);
}
}
@@ -651,6 +652,72 @@ end:
}
/*
+ * Callback for a command hooked.
+ */
+
+int
+trigger_callback_command_cb (void *data, struct t_gui_buffer *buffer,
+ int argc, char **argv, char **argv_eol)
+{
+ struct t_trigger *trigger;
+ struct t_hashtable *pointers, *extra_vars;
+ char str_name[32];
+ int rc, i;
+
+ /* get trigger pointer, return immediately if not found or trigger running */
+ trigger = (struct t_trigger *)data;
+ if (!trigger || trigger->hook_running)
+ return WEECHAT_RC_OK;
+
+ trigger->hook_count_cb++;
+ trigger->hook_running = 1;
+
+ pointers = NULL;
+ extra_vars = NULL;
+
+ rc = trigger_return_code[weechat_config_integer (trigger->options[TRIGGER_OPTION_RETURN_CODE])];
+
+ /* create hashtables */
+ pointers = weechat_hashtable_new (32,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_POINTER,
+ NULL,
+ NULL);
+ if (!pointers)
+ goto end;
+ extra_vars = weechat_hashtable_new (32,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_STRING,
+ NULL,
+ NULL);
+ if (!extra_vars)
+ goto end;
+
+ /* add data in hashtables used for conditions/replace/command */
+ weechat_hashtable_set (pointers, "buffer", buffer);
+ for (i = 0; i < argc; i++)
+ {
+ snprintf (str_name, sizeof (str_name), "tg_argv%d", i);
+ weechat_hashtable_set (extra_vars, str_name, argv[i]);
+ snprintf (str_name, sizeof (str_name), "tg_argv_eol%d", i);
+ weechat_hashtable_set (extra_vars, str_name, argv_eol[i]);
+ }
+
+ /* execute the trigger (conditions, regex, command) */
+ trigger_callback_execute (trigger, buffer, pointers, extra_vars);
+
+end:
+ if (pointers)
+ weechat_hashtable_free (pointers);
+ if (extra_vars)
+ weechat_hashtable_free (extra_vars);
+
+ trigger->hook_running = 0;
+
+ return rc;
+}
+
+/*
* Callback for a command_run hooked.
*/
diff --git a/src/plugins/trigger/trigger-callback.h b/src/plugins/trigger/trigger-callback.h
index 3d157b4d4..267ce814e 100644
--- a/src/plugins/trigger/trigger-callback.h
+++ b/src/plugins/trigger/trigger-callback.h
@@ -32,6 +32,8 @@ extern int trigger_callback_print_cb (void *data, struct t_gui_buffer *buffer,
const char **tags, int displayed,
int highlight, const char *prefix,
const char *message);
+extern int trigger_callback_command_cb (void *data, struct t_gui_buffer *buffer,
+ int argc, char **argv, char **argv_eol);
extern int trigger_callback_command_run_cb (void *data,
struct t_gui_buffer *buffer,
const char *command);
diff --git a/src/plugins/trigger/trigger-command.c b/src/plugins/trigger/trigger-command.c
index f0e7d2968..69b66e828 100644
--- a/src/plugins/trigger/trigger-command.c
+++ b/src/plugins/trigger/trigger-command.c
@@ -521,9 +521,9 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc,
"/trigger add name %s \"%s\" \"%s\" \"%s\" \"%s\"%s%s%s",
trigger_hook_type_string[type],
trigger_hook_default_arguments[type],
- trigger_hook_default_conditions[type],
- trigger_hook_default_regex[type],
- trigger_hook_default_command[type],
+ TRIGGER_HOOK_DEFAULT_CONDITIONS,
+ TRIGGER_HOOK_DEFAULT_REGEX,
+ TRIGGER_HOOK_DEFAULT_COMMAND,
(items && (num_items > 0)) ? " \"" : "",
(items && (num_items > 0)) ? items[0] : "",
(items && (num_items > 0)) ? "\"" : "");
@@ -775,17 +775,20 @@ trigger_command_init ()
"listdefault: list default triggers\n"
" add: add a trigger\n"
" name: name of trigger\n"
- " hook: signal, hsignal, modifier, print, command_run, timer, "
- "config\n"
+ " hook: signal, hsignal, modifier, print, command, command_run, "
+ "timer, config\n"
" arguments: arguments for the hook, depending on hook (separated "
"by semicolons):\n"
- " signal: name(s) of signal\n"
- " hsignal: name(s) of hsignal\n"
- " modifier: name(s) of modifier\n"
+ " signal: name(s) of signal (required)\n"
+ " hsignal: name(s) of hsignal (required)\n"
+ " modifier: name(s) of modifier (required)\n"
" print: buffer, tags, message, strip_colors\n"
- " command_run: command\n"
- " timer: interval, align_second, max_calls\n"
- " config: name of option\n"
+ " command: command (required), description, arguents, "
+ "description of arguments, completion\n"
+ " command_run: command (required)\n"
+ " timer: interval (required), align_second (required), "
+ "max_calls (required)\n"
+ " config: name of option (required)\n"
" conditions: evaluated conditions for the trigger\n"
" regex: one or more regular expressions to replace strings "
"in variables\n"
@@ -841,12 +844,13 @@ trigger_command_init ()
" /trigger add cfgsave timer 3600000;0;0 \"\" \"\" \"/mute /save\""),
"list|listfull|listdefault"
" || add %(trigger_names) %(trigger_hooks) %(trigger_hook_arguments) "
- "%(trigger_hook_condition) %(trigger_hook_regex) "
+ "%(trigger_hook_conditions) %(trigger_hook_regex) "
"%(trigger_hook_command) %(trigger_hook_rc)"
" || addinput %(trigger_hooks)"
" || set %(trigger_names) %(trigger_options)|name %(trigger_option_value)"
" || rename %(trigger_names) %(trigger_names)"
- " || enable|disable|toggle|restart|del %(trigger_names)|-all %(trigger_names)|%*"
+ " || enable|disable|toggle|restart|del %(trigger_names)|-all "
+ "%(trigger_names)|%*"
" || show %(trigger_names)"
" || default"
" || monitor",
diff --git a/src/plugins/trigger/trigger-completion.c b/src/plugins/trigger/trigger-completion.c
index c8ed9671f..aaa3ab8d5 100644
--- a/src/plugins/trigger/trigger-completion.c
+++ b/src/plugins/trigger/trigger-completion.c
@@ -263,22 +263,23 @@ trigger_completion_hook_arguments_cb (void *data, const char *completion_item,
}
/*
- * Adds default condition for hook to completion list.
+ * Adds default conditions for hook to completion list.
*/
int
-trigger_completion_hook_condition_cb (void *data, const char *completion_item,
- struct t_gui_buffer *buffer,
- struct t_gui_completion *completion)
+trigger_completion_hook_conditions_cb (void *data, const char *completion_item,
+ struct t_gui_buffer *buffer,
+ struct t_gui_completion *completion)
{
/* make C compiler happy */
(void) data;
(void) completion_item;
(void) buffer;
- trigger_completion_add_default_for_hook (completion,
- trigger_hook_default_conditions,
- NULL);
+ weechat_hook_completion_list_add (completion,
+ "\"" TRIGGER_HOOK_DEFAULT_CONDITIONS "\"",
+ 0,
+ WEECHAT_LIST_POS_END);
weechat_hook_completion_list_add (completion, "\"\"", 0,
WEECHAT_LIST_POS_END);
@@ -299,9 +300,10 @@ trigger_completion_hook_regex_cb (void *data, const char *completion_item,
(void) completion_item;
(void) buffer;
- trigger_completion_add_default_for_hook (completion,
- trigger_hook_default_regex,
- NULL);
+ weechat_hook_completion_list_add (completion,
+ "\"" TRIGGER_HOOK_DEFAULT_REGEX "\"",
+ 0,
+ WEECHAT_LIST_POS_END);
weechat_hook_completion_list_add (completion, "\"\"", 0,
WEECHAT_LIST_POS_END);
@@ -322,9 +324,10 @@ trigger_completion_hook_command_cb (void *data, const char *completion_item,
(void) completion_item;
(void) buffer;
- trigger_completion_add_default_for_hook (completion,
- trigger_hook_default_command,
- NULL);
+ weechat_hook_completion_list_add (completion,
+ "\"" TRIGGER_HOOK_DEFAULT_COMMAND "\"",
+ 0,
+ WEECHAT_LIST_POS_END);
weechat_hook_completion_list_add (completion, "\"\"", 0,
WEECHAT_LIST_POS_END);
@@ -374,9 +377,9 @@ trigger_completion_init ()
weechat_hook_completion ("trigger_hook_arguments",
N_("default arguments for a hook"),
&trigger_completion_hook_arguments_cb, NULL);
- weechat_hook_completion ("trigger_hook_condition",
- N_("default condition for a hook"),
- &trigger_completion_hook_condition_cb, NULL);
+ weechat_hook_completion ("trigger_hook_conditions",
+ N_("default conditions for a hook"),
+ &trigger_completion_hook_conditions_cb, NULL);
weechat_hook_completion ("trigger_hook_regex",
N_("default regular expression for a hook"),
&trigger_completion_hook_regex_cb, NULL);
diff --git a/src/plugins/trigger/trigger.c b/src/plugins/trigger/trigger.c
index e33e0e9b5..aec867ae6 100644
--- a/src/plugins/trigger/trigger.c
+++ b/src/plugins/trigger/trigger.c
@@ -48,25 +48,20 @@ char *trigger_option_default[TRIGGER_NUM_OPTIONS] =
{ "on", "signal", "", "", "", "", "ok" };
char *trigger_hook_type_string[TRIGGER_NUM_HOOK_TYPES] =
-{ "signal", "hsignal", "modifier", "print", "command_run", "timer", "config" };
+{ "signal", "hsignal", "modifier", "print", "command", "command_run", "timer",
+ "config" };
char *trigger_hook_option_values =
- "signal|hsignal|modifier|print|command_run|timer|config";
+ "signal|hsignal|modifier|print|command|command_run|timer|config";
char *trigger_hook_default_arguments[TRIGGER_NUM_HOOK_TYPES] =
-{ "xxx", "xxx", "xxx", "", "/cmd", "60000;0;0", "xxx" };
-char *trigger_hook_default_conditions[TRIGGER_NUM_HOOK_TYPES] =
-{ "${...}", "${...}", "${...}", "${...}", "${...}", "${...}", "${...}" };
-char *trigger_hook_default_regex[TRIGGER_NUM_HOOK_TYPES] =
-{ "/abc/def", "/abc/def", "/abc/def", "/abc/def", "/abc/def", "/abc/def",
- "/abc/def" };
-char *trigger_hook_default_command[TRIGGER_NUM_HOOK_TYPES] =
-{ "/cmd", "/cmd", "/cmd", "/cmd", "/cmd", "/cmd", "/cmd" };
+{ "xxx", "xxx", "xxx", "", "cmd;desc;args;args_desc;%(buffers_names)", "/cmd",
+ "60000;0;0", "xxx" };
char *trigger_hook_default_rc[TRIGGER_NUM_HOOK_TYPES] =
-{ "ok,ok_eat,error", "ok,ok_eat,error", "", "ok,error", "ok,ok_eat,error",
- "ok", "ok" };
+{ "ok,ok_eat,error", "ok,ok_eat,error", "", "ok,error", "ok,error",
+ "ok,ok_eat,error", "ok", "ok" };
char *trigger_hook_regex_default_var[TRIGGER_NUM_HOOK_TYPES] =
-{ "tg_signal_data", "", "tg_string", "tg_message", "tg_command", "",
- "tg_value" };
+{ "tg_signal_data", "", "tg_string", "tg_message", "tg_argv_eol1", "tg_command",
+ "", "tg_value" };
char *trigger_return_code_string[TRIGGER_NUM_RETURN_CODES] =
{ "ok", "ok_eat", "error" };
@@ -262,9 +257,10 @@ trigger_hook (struct t_trigger *trigger)
trigger->hooks_count = argc;
for (i = 0; i < argc; i++)
{
- trigger->hooks[i] = weechat_hook_signal (argv[i],
- &trigger_callback_signal_cb,
- trigger);
+ trigger->hooks[i] = weechat_hook_signal (
+ argv[i],
+ &trigger_callback_signal_cb,
+ trigger);
}
}
}
@@ -278,9 +274,10 @@ trigger_hook (struct t_trigger *trigger)
trigger->hooks_count = argc;
for (i = 0; i < argc; i++)
{
- trigger->hooks[i] = weechat_hook_hsignal (argv[i],
- &trigger_callback_hsignal_cb,
- trigger);
+ trigger->hooks[i] = weechat_hook_hsignal (
+ argv[i],
+ &trigger_callback_hsignal_cb,
+ trigger);
}
}
}
@@ -294,9 +291,10 @@ trigger_hook (struct t_trigger *trigger)
trigger->hooks_count = argc;
for (i = 0; i < argc; i++)
{
- trigger->hooks[i] = weechat_hook_modifier (argv[i],
- &trigger_callback_modifier_cb,
- trigger);
+ trigger->hooks[i] = weechat_hook_modifier (
+ argv[i],
+ &trigger_callback_modifier_cb,
+ trigger);
}
}
}
@@ -316,14 +314,35 @@ trigger_hook (struct t_trigger *trigger)
if (argc >= 4)
strip_colors = (strcmp (argv[3], "0") != 0) ? 1 : 0;
}
- trigger->hooks = malloc (1 * sizeof (trigger->hooks[0]));
+ trigger->hooks = malloc (sizeof (trigger->hooks[0]));
if (trigger->hooks)
{
trigger->hooks_count = 1;
- trigger->hooks[0] = weechat_hook_print (NULL, tags, message,
- strip_colors,
- &trigger_callback_print_cb,
- trigger);
+ trigger->hooks[0] = weechat_hook_print (
+ NULL,
+ tags,
+ message,
+ strip_colors,
+ &trigger_callback_print_cb,
+ trigger);
+ }
+ break;
+ case TRIGGER_HOOK_COMMAND:
+ if (argv && (argc >= 1))
+ {
+ trigger->hooks = malloc (sizeof (trigger->hooks[0]));
+ if (trigger->hooks)
+ {
+ trigger->hooks_count = 1;
+ trigger->hooks[0] = weechat_hook_command (
+ argv[0], /* command */
+ (argc > 1) ? argv[1] : "", /* description */
+ (argc > 2) ? argv[2] : "", /* arguments */
+ (argc > 3) ? argv[3] : "", /* description of args */
+ (argc > 4) ? argv[4] : "", /* completion */
+ &trigger_callback_command_cb,
+ trigger);
+ }
}
break;
case TRIGGER_HOOK_COMMAND_RUN:
@@ -335,9 +354,10 @@ trigger_hook (struct t_trigger *trigger)
trigger->hooks_count = argc;
for (i = 0; i < argc; i++)
{
- trigger->hooks[i] = weechat_hook_command_run (argv[i],
- &trigger_callback_command_run_cb,
- trigger);
+ trigger->hooks[i] = weechat_hook_command_run (
+ argv[i],
+ &trigger_callback_command_run_cb,
+ trigger);
}
}
}
@@ -358,15 +378,16 @@ trigger_hook (struct t_trigger *trigger)
&& (align_second >= 0)
&& (max_calls >= 0))
{
- trigger->hooks = malloc (1 * sizeof (trigger->hooks[0]));
+ trigger->hooks = malloc (sizeof (trigger->hooks[0]));
if (trigger->hooks)
{
trigger->hooks_count = 1;
- trigger->hooks[0] = weechat_hook_timer (interval,
- (int)align_second,
- (int)max_calls,
- &trigger_callback_timer_cb,
- trigger);
+ trigger->hooks[0] = weechat_hook_timer (
+ interval,
+ (int)align_second,
+ (int)max_calls,
+ &trigger_callback_timer_cb,
+ trigger);
}
}
}
@@ -380,9 +401,10 @@ trigger_hook (struct t_trigger *trigger)
trigger->hooks_count = argc;
for (i = 0; i < argc; i++)
{
- trigger->hooks[i] = weechat_hook_config (argv[i],
- &trigger_callback_config_cb,
- trigger);
+ trigger->hooks[i] = weechat_hook_config (
+ argv[i],
+ &trigger_callback_config_cb,
+ trigger);
}
}
}
diff --git a/src/plugins/trigger/trigger.h b/src/plugins/trigger/trigger.h
index 19149b650..1c9334e06 100644
--- a/src/plugins/trigger/trigger.h
+++ b/src/plugins/trigger/trigger.h
@@ -25,6 +25,10 @@
#define weechat_plugin weechat_trigger_plugin
#define TRIGGER_PLUGIN_NAME "trigger"
+#define TRIGGER_HOOK_DEFAULT_CONDITIONS "${...}"
+#define TRIGGER_HOOK_DEFAULT_REGEX "/abc/def"
+#define TRIGGER_HOOK_DEFAULT_COMMAND "/cmd"
+
enum t_trigger_option
{
TRIGGER_OPTION_ENABLED = 0, /* true if trigger is enabled */
@@ -44,6 +48,7 @@ enum t_trigger_hook_type
TRIGGER_HOOK_HSIGNAL,
TRIGGER_HOOK_MODIFIER,
TRIGGER_HOOK_PRINT,
+ TRIGGER_HOOK_COMMAND,
TRIGGER_HOOK_COMMAND_RUN,
TRIGGER_HOOK_TIMER,
TRIGGER_HOOK_CONFIG,
@@ -104,9 +109,6 @@ extern char *trigger_option_default[];
extern char *trigger_hook_type_string[];
extern char *trigger_hook_option_values;
extern char *trigger_hook_default_arguments[];
-extern char *trigger_hook_default_conditions[];
-extern char *trigger_hook_default_regex[];
-extern char *trigger_hook_default_command[];
extern char *trigger_hook_default_rc[];
extern char *trigger_hook_regex_default_var[];
extern char *trigger_return_code_string[];