1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-29 06:16:40 +02:00

api: add option "delay" in hashtable options of function command_options (issue #1327)

This commit is contained in:
Sébastien Helleu
2019-03-28 22:05:42 +01:00
parent 2f37de9148
commit 4e9e5f2652
8 changed files with 146 additions and 75 deletions
+3 -1
View File
@@ -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);
----
+3 -1
View File
@@ -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);
----
+3 -1
View File
@@ -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);
----
+3 -1
View File
@@ -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);
----
+4 -61
View File
@@ -6553,48 +6553,6 @@ COMMAND_CALLBACK(version)
return WEECHAT_RC_OK;
}
/*
* 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;
}
+104 -1
View File
@@ -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;
}
+2
View File
@@ -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 */
+24 -9
View File
@@ -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);