diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2014-01-28 11:49:25 +0100 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2014-02-09 12:55:40 +0100 |
commit | a703fc8c1771e5e8ef4ad08f8c82b6e18594ee10 (patch) | |
tree | 6f363a4ceca62ebba219a78aa3dd6fc3382a3cf2 /src/plugins | |
parent | 8368fc8b1ef7f588a36a8a2dbf72c96989dbff47 (diff) | |
download | weechat-a703fc8c1771e5e8ef4ad08f8c82b6e18594ee10.zip |
trigger: add timer
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/trigger/trigger-callback.c | 79 | ||||
-rw-r--r-- | src/plugins/trigger/trigger-callback.h | 1 | ||||
-rw-r--r-- | src/plugins/trigger/trigger.c | 29 |
3 files changed, 106 insertions, 3 deletions
diff --git a/src/plugins/trigger/trigger-callback.c b/src/plugins/trigger/trigger-callback.c index eab3d72a1..116bb318e 100644 --- a/src/plugins/trigger/trigger-callback.c +++ b/src/plugins/trigger/trigger-callback.c @@ -527,7 +527,7 @@ trigger_callback_print_cb (void *data, struct t_gui_buffer *buffer, rc = trigger_return_code[weechat_config_integer (trigger->options[TRIGGER_OPTION_RETURN_CODE])]; - /* return if the buffer does not match buffers defined in the trigger */ + /* do nothing if the buffer does not match buffers defined in the trigger */ if (trigger->hook_print_buffers && !weechat_buffer_match_list (buffer, trigger->hook_print_buffers)) goto end; @@ -636,6 +636,83 @@ end: } /* + * Callback for a timer hooked. + */ + +int +trigger_callback_timer_cb (void *data, int remaining_calls) +{ + struct t_trigger *trigger; + struct t_hashtable *extra_vars; + char str_temp[128]; + int rc; + time_t date; + struct tm *date_tmp; + + /* 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; + + extra_vars = NULL; + + rc = trigger_return_code[weechat_config_integer (trigger->options[TRIGGER_OPTION_RETURN_CODE])]; + + /* create hashtable */ + extra_vars = weechat_hashtable_new (32, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING, + NULL, + NULL); + if (!extra_vars) + goto end; + + /* add data in hashtable used for conditions/replace/command */ + snprintf (str_temp, sizeof (str_temp), "%d", remaining_calls); + weechat_hashtable_set (extra_vars, "tg_remaining_calls", str_temp); + date = time (NULL); + date_tmp = localtime (&date); + if (date_tmp) + { + strftime (str_temp, sizeof (str_temp), "%Y-%m-%d %H:%M:%S", date_tmp); + weechat_hashtable_set (extra_vars, "tg_date", str_temp); + } + + /* 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", + "timer\t%s%s", + weechat_color ("chat_channel"), + trigger->name); + trigger_buffer_display_hashtable ("extra_vars", extra_vars); + } + + /* check conditions */ + if (!trigger_callback_check_conditions (trigger, NULL, extra_vars)) + goto end; + + /* replace text with regex */ + trigger_callback_replace_regex (trigger, extra_vars); + + /* execute command */ + trigger_callback_run_command (trigger, NULL, NULL, extra_vars); + +end: + if (extra_vars) + weechat_hashtable_free (extra_vars); + + trigger->hook_running = 0; + + return rc; +} + +/* * Initializes trigger callback. */ diff --git a/src/plugins/trigger/trigger-callback.h b/src/plugins/trigger/trigger-callback.h index b9327709b..a12d5dc11 100644 --- a/src/plugins/trigger/trigger-callback.h +++ b/src/plugins/trigger/trigger-callback.h @@ -32,6 +32,7 @@ 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_timer_cb (void *data, int remaining_calls); extern void trigger_callback_init (); extern void trigger_callback_end (); diff --git a/src/plugins/trigger/trigger.c b/src/plugins/trigger/trigger.c index 315c58968..c4118cc07 100644 --- a/src/plugins/trigger/trigger.c +++ b/src/plugins/trigger/trigger.c @@ -426,8 +426,9 @@ trigger_unhook (struct t_trigger *trigger) void trigger_hook (struct t_trigger *trigger) { - char **argv, **argv_eol, *tags, *message; + char **argv, **argv_eol, *tags, *message, *error1, *error2, *error3; int i, argc, strip_colors; + long interval, align_second, max_calls; trigger_unhook (trigger); @@ -512,8 +513,32 @@ trigger_hook (struct t_trigger *trigger) } break; case TRIGGER_HOOK_TIMER: - if (argv && (argc >= 1)) + if (argv && (argc >= 3)) { + error1 = NULL; + error2 = NULL; + error3 = NULL; + interval = strtol (argv[0], &error1, 10); + align_second = strtol (argv[1], &error2, 10); + max_calls = strtol (argv[2], &error3, 10); + if (error1 && !error1[0] + && error2 && !error2[0] + && error3 && !error3[0] + && (interval > 0) + && (align_second >= 0) + && (max_calls >= 0)) + { + trigger->hooks = malloc (1 * 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); + } + } } break; } |