diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2014-02-04 21:43:47 +0100 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2014-02-09 12:55:40 +0100 |
commit | 66158f41f2fd6a0b20ee3fa3ba81c00da628c210 (patch) | |
tree | 16bb42f7fcabec911ad371862223c89b306f625b | |
parent | 639ac51593ef25998dac0c4828e6e0575120d559 (diff) | |
download | weechat-66158f41f2fd6a0b20ee3fa3ba81c00da628c210.zip |
trigger: add hook command_run
-rw-r--r-- | src/plugins/trigger/trigger-callback.c | 84 | ||||
-rw-r--r-- | src/plugins/trigger/trigger-callback.h | 3 | ||||
-rw-r--r-- | src/plugins/trigger/trigger-config.c | 3 | ||||
-rw-r--r-- | src/plugins/trigger/trigger.c | 31 | ||||
-rw-r--r-- | src/plugins/trigger/trigger.h | 1 |
5 files changed, 113 insertions, 9 deletions
diff --git a/src/plugins/trigger/trigger-callback.c b/src/plugins/trigger/trigger-callback.c index 04f629bf3..82ec50750 100644 --- a/src/plugins/trigger/trigger-callback.c +++ b/src/plugins/trigger/trigger-callback.c @@ -548,7 +548,7 @@ trigger_callback_print_cb (void *data, struct t_gui_buffer *buffer, if (!extra_vars) goto end; - /* add data in hashtable used for conditions/replace/command */ + /* add data in hashtables used for conditions/replace/command */ weechat_hashtable_set (pointers, "buffer", buffer); date_tmp = localtime (&date); if (date_tmp) @@ -636,6 +636,88 @@ end: } /* + * Callback for a command_run hooked. + */ + +int +trigger_callback_command_run_cb (void *data, struct t_gui_buffer *buffer, + const char *command) +{ + struct t_trigger *trigger; + struct t_hashtable *pointers, *extra_vars; + int rc; + + /* 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); + weechat_hashtable_set (extra_vars, "tg_command", command); + + /* display debug info on trigger buffer */ + if (!trigger_buffer && (weechat_trigger_plugin->debug >= 1)) + trigger_buffer_open (0); + if (trigger_buffer) + { + weechat_printf_tags (trigger_buffer, "no_trigger", + "command_run\t%s%s", + weechat_color ("chat_channel"), + trigger->name); + weechat_printf_tags (trigger_buffer, "no_trigger", + "\t buffer: %s", + weechat_buffer_get_string (buffer, "full_name")); + trigger_buffer_display_hashtable ("pointers", pointers); + trigger_buffer_display_hashtable ("extra_vars", extra_vars); + } + + /* check conditions */ + if (!trigger_callback_check_conditions (trigger, pointers, extra_vars)) + goto end; + + /* replace text with regex */ + trigger_callback_replace_regex (trigger, extra_vars); + + /* execute command */ + trigger_callback_run_command (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 timer hooked. */ diff --git a/src/plugins/trigger/trigger-callback.h b/src/plugins/trigger/trigger-callback.h index a12d5dc11..feb8f7953 100644 --- a/src/plugins/trigger/trigger-callback.h +++ b/src/plugins/trigger/trigger-callback.h @@ -32,6 +32,9 @@ 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_run_cb (void *data, + struct t_gui_buffer *buffer, + const char *command); extern int trigger_callback_timer_cb (void *data, int remaining_calls); extern void trigger_callback_init (); extern void trigger_callback_end (); diff --git a/src/plugins/trigger/trigger-config.c b/src/plugins/trigger/trigger-config.c index 3e5340bf5..9e521bf50 100644 --- a/src/plugins/trigger/trigger-config.c +++ b/src/plugins/trigger/trigger-config.c @@ -142,7 +142,8 @@ trigger_config_create_option (const char *trigger_name, int index_option, trigger_config_file, trigger_config_section_trigger, option_name, "integer", N_("type of hook used"), - "signal|hsignal|modifier|print|timer", 0, 0, value, NULL, 0, + "signal|hsignal|modifier|print|command_run|timer", + 0, 0, value, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); break; case TRIGGER_OPTION_ARGUMENTS: diff --git a/src/plugins/trigger/trigger.c b/src/plugins/trigger/trigger.c index 91df2ab54..cfbeafcfa 100644 --- a/src/plugins/trigger/trigger.c +++ b/src/plugins/trigger/trigger.c @@ -48,20 +48,21 @@ char *trigger_option_default[TRIGGER_NUM_OPTIONS] = { "on", "signal", "", "", "", "", "ok" }; char *trigger_hook_type_string[TRIGGER_NUM_HOOK_TYPES] = -{ "signal", "hsignal", "modifier", "print", "timer" }; +{ "signal", "hsignal", "modifier", "print", "command_run", "timer" }; char *trigger_hook_default_arguments[TRIGGER_NUM_HOOK_TYPES] = -{ "xxx", "xxx", "xxx", "", "60000;0;0" }; +{ "xxx", "xxx", "xxx", "", "/cmd", "60000;0;0" }; char *trigger_hook_default_condition[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", "/abc/def", "/abc/def" }; char *trigger_hook_default_command[TRIGGER_NUM_HOOK_TYPES] = -{ "/cmd", "/cmd", "/cmd", "/cmd", "/cmd" }; +{ "/cmd", "/cmd", "/cmd", "/cmd", "/cmd", "/cmd" }; char *trigger_hook_default_rc[TRIGGER_NUM_HOOK_TYPES] = -{ "ok,ok_eat,error", "ok,ok_eat,error", "", "ok,error", "ok" }; +{ "ok,ok_eat,error", "ok,ok_eat,error", "", "ok,error", "ok,ok_eat,error", + "ok" }; char *trigger_hook_regex_default_var[TRIGGER_NUM_HOOK_TYPES] = -{ "tg_signal_data", "", "tg_string", "tg_message", "" }; +{ "tg_signal_data", "", "tg_string", "tg_message", "tg_command", "" }; char *trigger_return_code_string[TRIGGER_NUM_RETURN_CODES] = { "ok", "ok_eat", "error" }; @@ -523,6 +524,22 @@ trigger_hook (struct t_trigger *trigger) trigger); } break; + case TRIGGER_HOOK_COMMAND_RUN: + if (argv && (argc >= 1)) + { + trigger->hooks = malloc (argc * sizeof (trigger->hooks[0])); + if (trigger->hooks) + { + 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); + } + } + } + break; case TRIGGER_HOOK_TIMER: if (argv && (argc >= 3)) { diff --git a/src/plugins/trigger/trigger.h b/src/plugins/trigger/trigger.h index 4fa43a6c9..b89f4f7d8 100644 --- a/src/plugins/trigger/trigger.h +++ b/src/plugins/trigger/trigger.h @@ -44,6 +44,7 @@ enum t_trigger_hook_type TRIGGER_HOOK_HSIGNAL, TRIGGER_HOOK_MODIFIER, TRIGGER_HOOK_PRINT, + TRIGGER_HOOK_COMMAND_RUN, TRIGGER_HOOK_TIMER, /* number of hook types */ TRIGGER_NUM_HOOK_TYPES, |