From bca7c7438a276dde99b9ca55b91ea49a96fea07e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sun, 13 Oct 2024 10:51:33 +0200 Subject: [PATCH] api: add special value `-` (hyphen-minus) in options of function command_options to prevent execution of commands (issue #2199) --- CHANGELOG.md | 1 + doc/en/weechat_plugin_api.en.adoc | 11 ++++++----- doc/fr/weechat_plugin_api.fr.adoc | 12 +++++++----- doc/it/weechat_plugin_api.it.adoc | 13 ++++++++----- doc/ja/weechat_plugin_api.ja.adoc | 11 +++++++---- doc/sr/weechat_plugin_api.sr.adoc | 11 ++++++++--- src/core/core-input.c | 17 ++++++++++++++--- tests/unit/core/test-core-input.cpp | 9 +++++++++ 8 files changed, 60 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e84a96e3..363f324b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Changed - api: return the buffer input callback return code in functions command and command_options +- api: add special value `-` (hyphen-minus) in options of function command_options to prevent execution of commands - relay/api: allow array with multiple requests in websocket frame received from client - core, plugins: simplify help on parameters that can be repeated in commands diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc index 6cbde1d1e..bbe74b8f1 100644 --- a/doc/en/weechat_plugin_api.en.adoc +++ b/doc/en/weechat_plugin_api.en.adoc @@ -16849,7 +16849,7 @@ rc = weechat.command(weechat.buffer_search("irc", "libera.#weechat"), "/whois Fl ==== command_options -_WeeChat ≥ 2.5, updated in 4.0.0._ +_WeeChat ≥ 2.5, updated in 4.0.0, 4.5.0._ Execute a command or send text to buffer with options. @@ -16865,13 +16865,14 @@ Arguments: * _buffer_: buffer pointer (command is executed on this buffer, use NULL for current buffer) -* _command_: command to execute (if beginning with a "/"), or text to send to - buffer +* _command_: command to execute (if beginning with a `/` or a command char), + or text to send to buffer * _options_: a hashtable with some options (keys and values must be string) (can be NULL): ** _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 + this call (see function <<_string_match_list,string_match_list>> for the + format); the special value `-` (hyphen-minus) disables the execution of commands + and the string in _command_ is sent as-is to the buffer (_WeeChat ≥ 4.5.0_) ** _delay_: delay to execute command, in milliseconds ** _split_newline_: `1` to split commands on newline char (`\n`) (_WeeChat ≥ 4.0.0_) diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc index 1fc999a2a..3b61ab7c2 100644 --- a/doc/fr/weechat_plugin_api.fr.adoc +++ b/doc/fr/weechat_plugin_api.fr.adoc @@ -17210,7 +17210,7 @@ rc = weechat.command(weechat.buffer_search("irc", "libera.#weechat"), "/whois Fl ==== command_options -_WeeChat ≥ 2.5, mis à jour dans la 4.0.0._ +_WeeChat ≥ 2.5, mis à jour dans la 4.0.0, 4.5.0._ Exécuter une commande ou envoyer du texte au tampon avec des options. @@ -17226,13 +17226,15 @@ Paramètres : * _buffer_ : pointeur vers le tampon (la commande est exécutée sur ce tampon, NULL pour le tampon courant) -* _command_ : commande à exécuter (si elle commence par "/"), ou texte à - envoyer au tampon +* _command_ : commande à exécuter (si elle commence par `/` ou un caractère de + commande), ou texte à envoyer au tampon * _options_ : table de hachage avec des options (les clés et valeurs doivent être des chaînes) (peut être NULL) : ** _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 + des virgules (voir la fonction <<_string_match_list,string_match_list>> + pour le format) ; la valeur spéciale `-` (moins) désactive l'exécution des + commandes et la chaîne dans _command_ est envoyée telle quelle au tampon + (_WeeChat ≥ 4.5.0_) ** _delay_ : délai pour exécuter la commande, en millisecondes ** _split_newline_ : `1` pour découper les commandes sur le caractère de retour à la ligne (`\n`) (_WeeChat ≥ 4.0.0_) diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc index 8630b3bc6..e1f8a45c0 100644 --- a/doc/it/weechat_plugin_api.it.adoc +++ b/doc/it/weechat_plugin_api.it.adoc @@ -17670,7 +17670,7 @@ rc = weechat.command(weechat.buffer_search("irc", "libera.#weechat"), "/whois Fl ==== command_options // TRANSLATION MISSING -_WeeChat ≥ 2.5, updated in 4.0.0._ +_WeeChat ≥ 2.5, updated in 4.0.0, 4.5.0._ // TRANSLATION MISSING Execute a command or send text to buffer with options. @@ -17687,14 +17687,17 @@ Argomenti: * _buffer_: puntatore al buffer (il comando viene eseguito su questo buffer, utilizzare NULL per il buffer corrente) -* _command_: comando da eseguire (se preceduto da "/"), oppure il testo - viene inviato sul buffer +// TRANSLATION MISSING +* _command_: comando da eseguire (if beginning with a `/` or a command char), + oppure il testo viene inviato sul buffer // TRANSLATION MISSING * _options_: a hashtable with some options (keys and values must be string) (can be NULL): +// TRANSLATION MISSING ** _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 + this call (see function <<_string_match_list,string_match_list>> for the + format); the special value `-` (hyphen-minus) disables the execution of commands + and the string in _command_ is sent as-is to the buffer (_WeeChat ≥ 4.5.0_) ** _delay_: delay to execute command, in milliseconds // TRANSLATION MISSING ** _split_newline_: `1` to split commands on newline char (`\n`) (_WeeChat ≥ 4.0.0_) diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc index 59dd51f93..cc2eb7455 100644 --- a/doc/ja/weechat_plugin_api.ja.adoc +++ b/doc/ja/weechat_plugin_api.ja.adoc @@ -17133,7 +17133,7 @@ rc = weechat.command(weechat.buffer_search("irc", "libera.#weechat"), "/whois Fl ==== command_options // TRANSLATION MISSING -_WeeChat ≥ 2.5, updated in 4.0.0._ +_WeeChat ≥ 2.5, updated in 4.0.0, 4.5.0._ バッファにてオプション付きでコマンドを実行またはテキストを送信 @@ -17149,14 +17149,17 @@ int weechat_command_options (struct t_gui_buffer *buffer, const char *command, * _buffer_: バッファへのポインタ (コマンドは指定したバッファで実行されます、現在のバッファで実行するには NULL を指定してください) -* _command_: 実行するコマンド ("/" - で始まっている場合)、またはバッファに送信するテキスト +// TRANSLATION MISSING +* _command_: 実行するコマンド (if beginning with a `/` or a command char)、またはバッファに送信するテキスト * _options_: オプションを含むハッシュテーブル (キーと値は文字列です) (NULL は使えません): * _options_: a hashtable with some options (keys and values must be string) (can be NULL): +// TRANSLATION MISSING ** _commands_: 指定したコマンドの中で実行を許可するコマンドのカンマ区切りリスト、リストの書式は関数 - <<_string_match_list,string_match_list>> を参考にしてください + <<_string_match_list,string_match_list>> を参考にしてください; + the special value `-` (hyphen-minus) disables the execution of commands + and the string in _command_ is sent as-is to the buffer (_WeeChat ≥ 4.5.0_) ** _delay_: コマンド実行までの遅延時間、ミリ秒単位 // TRANSLATION MISSING ** _split_newline_: `1` to split commands on newline char (`\n`) (_WeeChat ≥ 4.0.0_) diff --git a/doc/sr/weechat_plugin_api.sr.adoc b/doc/sr/weechat_plugin_api.sr.adoc index 1fed5d316..1eec013f6 100644 --- a/doc/sr/weechat_plugin_api.sr.adoc +++ b/doc/sr/weechat_plugin_api.sr.adoc @@ -16399,7 +16399,7 @@ rc = weechat.command(weechat.buffer_search("irc", "libera.#weechat"), "/whois Fl ==== command_options -_WeeChat ≥ 2.5, ажурирано у верзији 4.0.0._ +_WeeChat ≥ 2.5, ажурирано у верзији 4.0.0, 4.5.0._ Извршава команду или шаље текст у бафер са опцијама. @@ -16414,9 +16414,14 @@ int weechat_command_options (struct t_gui_buffer *buffer, const char *command, Аргументи: * _buffer_: показивач на бафер (команда се извршава у овом баферу, употребите NULL за текући бафер) -* _command_: команда која се извршава (ако почиње са „/”), или текст који се шаље у бафер +// TRANSLATION MISSING +* _command_: команда која се извршава (if beginning with a `/` or a command char), или текст који се шаље у бафер * _options_: хеш табела са неким опцијама (кључеви и вредности морају бити стринг) (може да буде NULL): -** _commands_: листа команди које смеју да се изврше током овог позива раздвојене запетама; за формат, погледајте функцију <<_string_match_list,string_match_list>> +// TRANSLATION MISSING +** _commands_: листа команди које смеју да се изврше током овог позива раздвојене запетама + (за формат, погледајте функцију <<_string_match_list,string_match_list>>); + the special value `-` (hyphen-minus) disables the execution of commands + and the string in _command_ is sent as-is to the buffer (_WeeChat ≥ 4.5.0_) ** _delay_: пауза за извршавање команде, у милисекундама ** _split_newline_: `1` да се команде поделе на карактеру прелома линије (`\n`) (_WeeChat ≥ 4.0.0_) diff --git a/src/core/core-input.c b/src/core/core-input.c index cd0ca6317..ac9588efd 100644 --- a/src/core/core-input.c +++ b/src/core/core-input.c @@ -327,9 +327,20 @@ input_data (struct t_gui_buffer *buffer, const char *data, } else { - /* execute command on buffer */ - rc = input_exec_command (buffer, 1, buffer->plugin, ptr_data, - commands_allowed); + /* + * if commands_allowed has special value "-", send data as-is + * to the buffer input callback, otherwise execute the command + * on the buffer + */ + if (commands_allowed && (strcmp (commands_allowed, "-") == 0)) + { + rc = input_exec_data (buffer, ptr_data); + } + else + { + rc = input_exec_command (buffer, 1, buffer->plugin, + ptr_data, commands_allowed); + } } } diff --git a/tests/unit/core/test-core-input.cpp b/tests/unit/core/test-core-input.cpp index a138656b5..e28489146 100644 --- a/tests/unit/core/test-core-input.cpp +++ b/tests/unit/core/test-core-input.cpp @@ -182,6 +182,15 @@ TEST(CoreInput, Data) RECORD_CHECK_MSG("core.test", "", "line2", NULL); LONGS_EQUAL(2, record_count_messages ()); + /* on core buffer: valid command but with commands disabled */ + TEST_INPUT_DATA(WEECHAT_RC_ERROR, gui_buffers, "/print core\n/print line2", "-", 0, 0); + RECORD_CHECK_MSG("core.weechat", "=!=", "You cannot write text in this buffer", NULL); + LONGS_EQUAL(1, record_count_messages ()); + + /* on test buffer: valid command but with commands disabled */ + TEST_INPUT_DATA(WEECHAT_RC_OK, buffer, "/print core\n/print line2", "-", 0, 0); + RECORD_CHECK_NO_MSG(); + gui_buffer_close (buffer); }