diff options
-rw-r--r-- | doc/en/weechat_plugin_api.en.adoc | 4 | ||||
-rw-r--r-- | doc/fr/weechat_plugin_api.fr.adoc | 4 | ||||
-rw-r--r-- | doc/it/weechat_plugin_api.it.adoc | 4 | ||||
-rw-r--r-- | doc/ja/weechat_plugin_api.ja.adoc | 4 | ||||
-rw-r--r-- | src/core/wee-command.c | 65 | ||||
-rw-r--r-- | src/core/wee-input.c | 105 | ||||
-rw-r--r-- | src/core/wee-input.h | 2 | ||||
-rw-r--r-- | src/plugins/plugin-api.c | 33 |
8 files changed, 146 insertions, 75 deletions
diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc index dcee3891f..0f67f4e99 100644 --- a/doc/en/weechat_plugin_api.en.adoc +++ b/doc/en/weechat_plugin_api.en.adoc @@ -14443,6 +14443,7 @@ Arguments: ** _commands_: a comma-separated list of commands allowed to be executed during this call; see function <<_string_match_list,string_match_list>> for the format +** _delay_: delay to execute command, in milliseconds Return value: @@ -14453,7 +14454,7 @@ C example: [source,C] ---- -/* allow any command except /exec */ +/* allow any command except /exec, run command in 2 seconds */ int rc; struct t_hashtable *options = weechat_hashtable_new (8, WEECHAT_HASHTABLE_STRING, @@ -14461,6 +14462,7 @@ struct t_hashtable *options = weechat_hashtable_new (8, NULL, NULL); weechat_hashtable_set (options, "commands", "*,!exec"); +weechat_hashtable_set (options, "delay", "2000"); rc = weechat_command_options (NULL, "/some_command arguments", options); ---- diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc index 6d73e0f06..16aca160f 100644 --- a/doc/fr/weechat_plugin_api.fr.adoc +++ b/doc/fr/weechat_plugin_api.fr.adoc @@ -14758,6 +14758,7 @@ Paramètres : ** _commands_ : une liste de commandes autorisées pendant l'appel, séparées par des virgules ; voir la fonction <<_string_match_list,string_match_list>> pour le format +** _delay_ : délai pour exécuter la commande, en millisecondes Valeur de retour : @@ -14768,7 +14769,7 @@ Exemple en C : [source,C] ---- -/* autoriser toute commande sauf /exec */ +/* autoriser toute commande sauf /exec, lancer la commande dans 2 secondes */ int rc; struct t_hashtable *options = weechat_hashtable_new (8, WEECHAT_HASHTABLE_STRING, @@ -14776,6 +14777,7 @@ struct t_hashtable *options = weechat_hashtable_new (8, NULL, NULL); weechat_hashtable_set (options, "commands", "*,!exec"); +weechat_hashtable_set (options, "delay", "2000"); rc = weechat_command_options (NULL, "/une_commande paramètres", options); ---- diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc index 201ccaa1a..b54445c31 100644 --- a/doc/it/weechat_plugin_api.it.adoc +++ b/doc/it/weechat_plugin_api.it.adoc @@ -15024,6 +15024,7 @@ Argomenti: ** _commands_: a comma-separated list of commands allowed to be executed during this call; see function <<_string_match_list,string_match_list>> for the format +** _delay_: delay to execute command, in milliseconds Valori restituiti: @@ -15035,7 +15036,7 @@ Esempio in C: // TRANSLATION MISSING [source,C] ---- -/* allow any command except /exec */ +/* allow any command except /exec, run command in 2 seconds */ int rc; struct t_hashtable *options = weechat_hashtable_new (8, WEECHAT_HASHTABLE_STRING, @@ -15043,6 +15044,7 @@ struct t_hashtable *options = weechat_hashtable_new (8, NULL, NULL); weechat_hashtable_set (options, "commands", "*,!exec"); +weechat_hashtable_set (options, "delay", "2000"); rc = weechat_command_options (NULL, "/some_command arguments", options); ---- diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc index 2871d8f5e..f2c11ed4d 100644 --- a/doc/ja/weechat_plugin_api.ja.adoc +++ b/doc/ja/weechat_plugin_api.ja.adoc @@ -14411,6 +14411,7 @@ int weechat_command_options (struct t_gui_buffer *buffer, const char *command, ** _commands_: a comma-separated list of commands allowed to be executed during this call; see function <<_string_match_list,string_match_list>> for the format +** _delay_: delay to execute command, in milliseconds 戻り値: @@ -14422,7 +14423,7 @@ C 言語での使用例: // TRANSLATION MISSING [source,C] ---- -/* allow any command except /exec */ +/* allow any command except /exec, run command in 2 seconds */ int rc; struct t_hashtable *options = weechat_hashtable_new (8, WEECHAT_HASHTABLE_STRING, @@ -14430,6 +14431,7 @@ struct t_hashtable *options = weechat_hashtable_new (8, NULL, NULL); weechat_hashtable_set (options, "commands", "*,!exec"); +weechat_hashtable_set (options, "delay", "2000"); rc = weechat_command_options (NULL, "/some_command arguments", options); ---- diff --git a/src/core/wee-command.c b/src/core/wee-command.c index a8ed5c72d..bdccee749 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -6554,48 +6554,6 @@ COMMAND_CALLBACK(version) } /* - * Callback for timer set by command_wait. - */ - -int -command_wait_timer_cb (const void *pointer, void *data, int remaining_calls) -{ - char **timer_args; - int i; - struct t_gui_buffer *ptr_buffer; - - /* make C compiler happy */ - (void) data; - (void) remaining_calls; - - timer_args = (char **)pointer; - - if (!timer_args) - return WEECHAT_RC_ERROR; - - if (timer_args[0] && timer_args[1]) - { - /* search buffer, fallback to core buffer if not found */ - ptr_buffer = gui_buffer_search_by_full_name (timer_args[0]); - if (!ptr_buffer) - ptr_buffer = gui_buffer_search_main (); - - /* execute command */ - if (ptr_buffer) - (void) input_data (ptr_buffer, timer_args[1], timer_args[2]); - } - - for (i = 0; i < 3; i++) - { - if (timer_args[i]) - free (timer_args[i]); - } - free (timer_args); - - return WEECHAT_RC_OK; -} - -/* * Callback for command "/wait": schedules a command execution in future. */ @@ -6603,7 +6561,6 @@ COMMAND_CALLBACK(wait) { char *pos, *str_number, *error; long number, factor, delay; - char **timer_args; /* make C compiler happy */ (void) pointer; @@ -6651,25 +6608,11 @@ COMMAND_CALLBACK(wait) delay = number * factor; - /* build arguments for timer callback */ - timer_args = malloc (3 * sizeof (*timer_args)); - if (!timer_args) - { - gui_chat_printf (NULL, - _("%sNot enough memory (%s)"), - gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], - "/wait"); - return WEECHAT_RC_OK; - } - timer_args[0] = strdup (buffer->full_name); - timer_args[1] = strdup (argv_eol[2]); - timer_args[2] = (input_commands_allowed) ? - string_build_with_split_string ( - (const char **)input_commands_allowed, ",") : NULL; + if (delay < 1) + COMMAND_ERROR; - /* schedule command, execute it after "delay" milliseconds */ - hook_timer (NULL, delay, 0, 1, - &command_wait_timer_cb, timer_args, NULL); + if (input_data_delayed (buffer, argv_eol[2], NULL, delay) != WEECHAT_RC_OK) + COMMAND_ERROR; return WEECHAT_RC_OK; } diff --git a/src/core/wee-input.c b/src/core/wee-input.c index 5ba90d005..7538b541d 100644 --- a/src/core/wee-input.c +++ b/src/core/wee-input.c @@ -57,10 +57,12 @@ input_exec_data (struct t_gui_buffer *buffer, const char *data) data); } else + { gui_chat_printf (buffer, _("%sYou can not write text in this " "buffer"), gui_chat_prefix[GUI_CHAT_PREFIX_ERROR]); + } } /* @@ -232,7 +234,7 @@ end: } /* - * Reads user input and sends data to buffer's callback. + * Sends data to a buffer's callback. * * Returns: * WEECHAT_RC_OK: data properly sent (or command executed successfully) @@ -349,3 +351,104 @@ end: return rc; } + +/* + * Callback for timer set by input_data_delayed. + */ + +int +input_data_timer_cb (const void *pointer, void *data, int remaining_calls) +{ + char **timer_args; + int i; + struct t_gui_buffer *ptr_buffer; + + /* make C compiler happy */ + (void) data; + (void) remaining_calls; + + timer_args = (char **)pointer; + + if (!timer_args) + return WEECHAT_RC_ERROR; + + if (timer_args[0] && timer_args[1]) + { + /* search buffer, fallback to core buffer if not found */ + ptr_buffer = gui_buffer_search_by_full_name (timer_args[0]); + if (!ptr_buffer) + ptr_buffer = gui_buffer_search_main (); + + /* execute command */ + if (ptr_buffer) + (void) input_data (ptr_buffer, timer_args[1], timer_args[2]); + } + + for (i = 0; i < 3; i++) + { + if (timer_args[i]) + free (timer_args[i]); + } + free (timer_args); + + return WEECHAT_RC_OK; +} + +/* + * Sends data to a buffer's callback with an optional delay (in milliseconds). + * + * If delay < 1, the command is executed immediately. + * If delay >= 1, the command is scheduled for execution in this number of ms. + * + * Returns: + * WEECHAT_RC_OK: data properly sent or scheduled for execution + * WEECHAT_RC_ERROR: error + */ + +int +input_data_delayed (struct t_gui_buffer *buffer, const char *data, + const char *commands_allowed, long delay) +{ + char **timer_args, *new_commands_allowed; + + if (delay < 1) + return input_data (buffer, data, commands_allowed); + + timer_args = malloc (3 * sizeof (*timer_args)); + if (!timer_args) + { + gui_chat_printf (NULL, + _("%sNot enough memory (%s)"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + "/wait"); + return WEECHAT_RC_ERROR; + } + + if (commands_allowed) + { + new_commands_allowed = strdup (commands_allowed); + } + else if (input_commands_allowed) + { + new_commands_allowed = string_build_with_split_string ( + (const char **)input_commands_allowed, ","); + } + else + { + new_commands_allowed = NULL; + } + + timer_args[0] = strdup (buffer->full_name); + timer_args[1] = strdup (data); + timer_args[2] = new_commands_allowed; + + /* schedule command, execute it after "delay" milliseconds */ + hook_timer (NULL, + (delay >= 1) ? delay : 1, + 0, + 1, + &input_data_timer_cb, + timer_args, NULL); + + return WEECHAT_RC_OK; +} diff --git a/src/core/wee-input.h b/src/core/wee-input.h index faaf1b490..4d42acd88 100644 --- a/src/core/wee-input.h +++ b/src/core/wee-input.h @@ -32,5 +32,7 @@ extern int input_exec_command (struct t_gui_buffer *buffer, const char *commands_allowed); extern int input_data (struct t_gui_buffer *buffer, const char *data, const char *commands_allowed); +extern int input_data_delayed (struct t_gui_buffer *buffer, const char *data, + const char *commands_allowed, long delay); #endif /* WEECHAT_INPUT_H */ diff --git a/src/plugins/plugin-api.c b/src/plugins/plugin-api.c index cd5c736ef..f7405d647 100644 --- a/src/plugins/plugin-api.c +++ b/src/plugins/plugin-api.c @@ -303,22 +303,37 @@ plugin_api_command_options (struct t_weechat_plugin *plugin, struct t_gui_buffer *buffer, const char *command, struct t_hashtable *options) { - char *command2; - const char *ptr_commands_allowed; + char *command2, *error; + const char *ptr_commands_allowed, *ptr_delay; + long delay; int rc; if (!plugin || !command) return WEECHAT_RC_ERROR; - ptr_commands_allowed = (options) ? - hashtable_get (options, "commands") : NULL; + ptr_commands_allowed = NULL; + delay = 0; + + if (options) + { + ptr_commands_allowed = hashtable_get (options, "commands"); + ptr_delay = hashtable_get (options, "delay"); + if (ptr_delay) + { + error = NULL; + delay = strtol (ptr_delay, &error, 10); + if (!error || error[0]) + delay = 0; + } + } command2 = string_iconv_to_internal (plugin->charset, command); - if (!buffer) - buffer = gui_current_window->buffer; - rc = input_data (buffer, - (command2) ? command2 : command, - ptr_commands_allowed); + + rc = input_data_delayed ((buffer) ? buffer : gui_current_window->buffer, + (command2) ? command2 : command, + ptr_commands_allowed, + delay); + if (command2) free (command2); |