1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-05 01:03:14 +02:00

core: add command /pipe

This commit is contained in:
Sébastien Helleu
2024-12-16 13:39:14 +01:00
parent 0fc0071297
commit 732f24b6ba
20 changed files with 5629 additions and 3637 deletions
+282 -1
View File
@@ -4959,6 +4959,235 @@ COMMAND_CALLBACK(mute)
return WEECHAT_RC_OK;
}
/*
* Opens a file in write mode to redirect messages.
*
* Returns a pointer to the file, NULL if error.
*/
FILE *
command_pipe_open_file (const char *filename)
{
char *filename2;
FILE *file;
filename2 = string_expand_home (filename);
if (!filename2)
return NULL;
file = fopen (filename2, "w");
if (!file)
return NULL;
fchmod (fileno (file), 0600);
free (filename2);
return file;
}
/*
* Callback for command "/pipe": redirect command output to a buffer or a file
*/
COMMAND_CALLBACK(pipe)
{
const char *ptr_command, *ptr_filename, *ptr_hsignal;
const char *ptr_concat_separator, *ptr_strip_chars;
char *command, space[2] = " ", newline[2] = "\n";
int i, index_command, skip_empty_lines, send_to_buffer, no_locale, pipe_set;
struct t_gui_buffer *ptr_buffer;
/* make C compiler happy */
(void) pointer;
(void) data;
if (argc < 2)
{
/* silently ignore missing arguments ("/pipe" does nothing) */
return WEECHAT_RC_OK;
}
index_command = 1;
send_to_buffer = 0;
no_locale = 0;
ptr_concat_separator = NULL;
ptr_strip_chars = NULL;
skip_empty_lines = 0;
ptr_buffer = NULL;
ptr_filename = NULL;
ptr_hsignal = NULL;
ptr_command = NULL;
for (i = 1; i < argc; i++)
{
if (string_strcmp (argv[i], "-concat") == 0)
{
COMMAND_MIN_ARGS(i + 2, argv[i]);
i++;
ptr_concat_separator = argv[i];
index_command = i + 1;
}
else if (string_strcmp (argv[i], "-strip") == 0)
{
COMMAND_MIN_ARGS(i + 2, argv[i]);
i++;
ptr_strip_chars = argv[i];
index_command = i + 1;
}
else if (string_strcmp (argv[i], "-skipempty") == 0)
{
skip_empty_lines = 1;
index_command = i + 1;
}
else if (string_strcmp (argv[i], "-c") == 0)
{
ptr_concat_separator = space;
ptr_strip_chars = space;
skip_empty_lines = 1;
index_command = i + 1;
}
else if (string_strcmp (argv[i], "-nl") == 0)
{
no_locale = 1;
index_command = i + 1;
}
else if (string_strcmp (argv[i], "-o") == 0)
{
send_to_buffer = 1;
index_command = i + 1;
}
else if (string_strcmp (argv[i], "-buffer") == 0)
{
COMMAND_MIN_ARGS(i + 2, argv[i]);
i++;
ptr_buffer = gui_buffer_search_by_full_name (argv[i]);
if (!ptr_buffer)
{
gui_chat_printf (NULL,
_("%sBuffer \"%s\" not found"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
argv[i]);
return WEECHAT_RC_ERROR;
}
index_command = i + 1;
}
else if (string_strcmp (argv[i], "-file") == 0)
{
COMMAND_MIN_ARGS(i + 2, argv[i]);
i++;
ptr_filename = argv[i];
index_command = i + 1;
}
else if (string_strcmp (argv[i], "-hsignal") == 0)
{
COMMAND_MIN_ARGS(i + 2, argv[i]);
i++;
ptr_hsignal = argv[i];
index_command = i + 1;
}
}
if (index_command < argc)
ptr_command = argv_eol[index_command];
if (!ptr_command || !ptr_command[0])
{
/* silently ignore missing command */
return WEECHAT_RC_OK;
}
/* for hsignal, set default concat separator to newline if not set */
if (ptr_hsignal && !ptr_concat_separator)
ptr_concat_separator = newline;
/*
* when chaining /pipe command, only the first one (surrounding) wins;
* if buffer/file is already set, ignore it and just execute the command
* with the existing redirection
*/
pipe_set = 0;
if (!gui_chat_pipe)
{
gui_chat_pipe_command = strdup (ptr_command);
if (ptr_filename)
{
gui_chat_pipe_file = command_pipe_open_file (ptr_filename);
if (!gui_chat_pipe_file)
{
gui_chat_printf (NULL,
_("%sUnable to open file \"%s\""),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
ptr_filename);
return WEECHAT_RC_ERROR;
}
}
else if (ptr_hsignal)
{
gui_chat_pipe_hsignal = strdup (ptr_hsignal);
if (!gui_chat_pipe_hsignal)
COMMAND_ERROR;
}
else
{
gui_chat_pipe_buffer = (ptr_buffer) ? ptr_buffer : buffer;
if (!gui_chat_pipe_buffer)
COMMAND_ERROR;
if (gui_chat_pipe_buffer->type != GUI_BUFFER_TYPE_FORMATTED)
{
gui_chat_printf (NULL,
_("%sCommand /pipe can only use buffers "
"with formatted content"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR]);
gui_chat_pipe_buffer = NULL;
return WEECHAT_RC_ERROR;
}
}
gui_chat_pipe_send_to_buffer = send_to_buffer;
if (ptr_concat_separator)
{
gui_chat_pipe_concat_lines = string_dyn_alloc (1024);
gui_chat_pipe_concat_sep = string_convert_escaped_chars (
ptr_concat_separator);
}
if (ptr_strip_chars)
{
gui_chat_pipe_strip_chars = string_convert_escaped_chars (
ptr_strip_chars);
}
gui_chat_pipe_skip_empty_lines = skip_empty_lines;
if (no_locale)
setlocale (LC_ALL, "C");
pipe_set = 1;
gui_chat_pipe = 1;
}
if (string_asprintf (
&command,
"%s%s",
(string_is_command_char (ptr_command)) ? "" : "/",
ptr_command) < 0)
COMMAND_ERROR;
(void) input_exec_command (
buffer,
1, /* any_plugin */
NULL, /* plugin */
command,
NULL); /* commands_allowed */
free (command);
if (pipe_set)
{
gui_chat_pipe_end ();
if (no_locale)
setlocale (LC_ALL, "");
}
return WEECHAT_RC_OK;
}
/*
* Displays a list of loaded plugins.
*/
@@ -9074,7 +9303,7 @@ command_init ()
NULL, "mute",
N_("execute a command silently"),
/* TRANSLATORS: only text between angle brackets (eg: "<name>") may be translated */
N_("[-core | -current | -buffer <name>] <command>"),
N_("[-core|-current|-buffer <name>] <command>"),
CMD_ARGS_DESC(
N_("raw[-core]: no output on WeeChat core buffer"),
N_("raw[-current]: no output on current buffer"),
@@ -9095,6 +9324,58 @@ command_init ()
" || -buffer %(buffers_plugins_names) %(commands:/)|%*"
" || %(commands:/)|%*",
&command_mute, NULL, NULL);
hook_command (
NULL, "pipe",
N_("redirect command output to a buffer, a file or a hsignal"),
/* TRANSLATORS: only text between angle brackets (eg: "<name>") may be translated */
N_("[-buffer <name>|-file <filename>|-hsignal <name>] "
"[-concat <separator>] [-strip <chars>] [-skipempty] [-c] [-o] "
"[-g] [-nl] <command>"),
CMD_ARGS_DESC(
N_("raw[-buffer]: display command output on this buffer"),
N_("name: full buffer name (examples: \"core.weechat\", "
"\"irc.server.libera\", \"irc.libera.#weechat\")"),
N_("raw[-file]: write command output in this file"),
N_("raw[-hsignal]: send command output as hsignal "
"(keys: \"command\" and \"output\")"),
N_("raw[-o]: send command output to the buffer as input; "
"colors are stripped and commands are NOT executed "
"(used only with -buffer)"),
N_("raw[-concat]: concatenate all lines displayed using a separator; "
"chars can be escaped (example: \\x20 for space)"),
N_("raw[-strip]: strip chars from lines (beginning/end); "
"chars can be escaped (example: \\x20 for space)"),
N_("raw[-skipempty]: skip empty lines when lines are concatenated"),
N_("raw[-c]: alias for \"-concat \\x20 -strip \\x20 -skipempty\""),
N_("raw[-nl]: display messages in English during the command execution "
"(do not use the current locale)"),
N_("command: command to execute (a \"/\" is automatically added "
"if not found at beginning of command)"),
"",
N_("If no target is specified (\"-buffer\", \"-file\" or \"-hsignal\"), "
"then the command output is sent on the current buffer."),
"",
N_("Note: for commands that display messages in an asynchronous way "
"(like /exec and many IRC commands), the output will not be "
"caught by this command."),
N_("For example \"/pipe /whois nick\" will NOT redirect the answer "
"from IRC server to the current buffer."),
"",
N_("Examples:"),
N_(" write info about external libraries in a file:"),
AI(" /pipe -file /tmp/libs.txt /debug libs"),
N_(" send output of \"/debug libs\" as a single line on current channel:"),
AI(" /pipe -o -c /debug libs"),
N_(" display info about all buffers on current buffer:"),
AI(" /pipe /allbuf /eval /print ${buffer.full_name} -> "
"${buffer.number}. ${buffer.short_name} (${buffer})"),
N_(" send list of filters on current channel, in English:"),
AI(" /pipe -o -nl /filter")),
"-buffer %(buffers_plugins_names) %(commands:/)|%*"
" || -file %(filename) %(commands:/)|%*"
" || -hsignal %- %(commands:/)|%*"
" || -o %(commands:/)|%*",
&command_pipe, NULL, NULL);
hook_command (
NULL, "plugin",
N_("list/load/unload plugins"),
+220
View File
@@ -37,6 +37,7 @@
#include "../core/core-eval.h"
#include "../core/core-hashtable.h"
#include "../core/core-hook.h"
#include "../core/core-input.h"
#include "../core/core-string.h"
#include "../core/core-utf8.h"
#include "../core/core-util.h"
@@ -60,6 +61,18 @@ int gui_chat_display_tags = 0; /* display tags? */
char **gui_chat_lines_waiting_buffer = NULL; /* lines waiting for core */
/* buffer */
/* command /pipe */
int gui_chat_pipe = 0; /* pipe enabled */
char *gui_chat_pipe_command = NULL; /* piped command */
struct t_gui_buffer *gui_chat_pipe_buffer = NULL; /* pipe msgs to a buffer */
int gui_chat_pipe_send_to_buffer = 0; /* send as input to buffer */
FILE *gui_chat_pipe_file = NULL; /* pipe msgs to a file */
char *gui_chat_pipe_hsignal = NULL; /* pipe msgs to a hsignal */
char *gui_chat_pipe_concat_sep = NULL; /* separator to concat lines*/
char **gui_chat_pipe_concat_lines = NULL; /* concatenated lines */
char *gui_chat_pipe_strip_chars = NULL; /* chars to strip on lines */
int gui_chat_pipe_skip_empty_lines = 0; /* skip empty lines */
/*
* Initializes some variables for chat area (called before reading WeeChat
@@ -590,6 +603,205 @@ gui_chat_buffer_valid (struct t_gui_buffer *buffer,
return 1;
}
/*
* Builds a message with a line: "<prefix> <message>" or "<message>" if there
* is no prefix. The colors are stripped from prefix and message.
*
* Note: result must be freed after use.
*/
char *
gui_chat_pipe_build_message (struct t_gui_line *line)
{
char *prefix, *message, *data;
if (!line)
return NULL;
prefix = (line->data->prefix) ?
gui_color_decode (line->data->prefix, NULL) : strdup ("");
message = (line->data->message) ?
gui_color_decode (line->data->message, NULL) : strdup ("");
string_asprintf (&data,
"%s%s%s",
(prefix) ? prefix : "",
(prefix && prefix[0] && message && message[0]) ? " " : "",
(message) ? message : "");
free (prefix);
free (message);
return data;
}
/*
* Sends data to a buffer (command `/pipe -o`).
*/
void
gui_chat_pipe_send_buffer_input (struct t_gui_buffer *buffer, const char *data)
{
struct t_gui_buffer *buffer_saved;
int send_to_buffer_saved;
if (!buffer || !data)
return;
buffer_saved = gui_chat_pipe_buffer;
send_to_buffer_saved = gui_chat_pipe_send_to_buffer;
/* temporarily disable the pipe redirection, to prevent infinite loop */
gui_chat_pipe_buffer = NULL;
gui_chat_pipe_send_to_buffer = 0;
input_data (
buffer,
(data[0]) ? data : " ",
"-", /* commands_allowed */
0, /* split_newline */
0); /* user_data */
/* restore pipe redirection */
gui_chat_pipe_buffer = buffer_saved;
gui_chat_pipe_send_to_buffer = send_to_buffer_saved;
}
/*
* Handles a redirection with /pipe command.
*
* Returns:
* 1: line has been handled and must NOT be displayed
* 0: line has NOT been handled and must be displayed
*/
int
gui_chat_pipe_handle_line (struct t_gui_line *line)
{
char *data, *data2;
int rc;
if (!line || !gui_chat_pipe)
return 0;
rc = 0;
data = gui_chat_pipe_build_message (line);
if (!data)
return 1;
if (gui_chat_pipe_concat_lines)
{
/*
* concatenate line with previous ones, it will be displayed, sent to
* buffer input or written to a file later
*/
data2 = (gui_chat_pipe_strip_chars) ?
string_strip (data, 1, 1, gui_chat_pipe_strip_chars) : strdup (data);
if (data2 && (data2[0] || !gui_chat_pipe_skip_empty_lines))
{
if ((*gui_chat_pipe_concat_lines)[0])
{
string_dyn_concat (gui_chat_pipe_concat_lines,
gui_chat_pipe_concat_sep,
-1);
}
string_dyn_concat (gui_chat_pipe_concat_lines, data2, -1);
}
free (data2);
rc = 1;
}
else if (gui_chat_pipe_file)
{
/* pipe to a file */
fprintf (gui_chat_pipe_file, "%s\n", data);
rc = 1;
}
else if (gui_chat_pipe_buffer && gui_chat_pipe_send_to_buffer)
{
/* pipe to a buffer as input */
gui_chat_pipe_send_buffer_input (gui_chat_pipe_buffer, data);
rc = 1;
}
free (data);
return rc;
}
/*
* Ends pipe and flushes concatenated lines to a buffer or a file
* (if command `/pipe -g` was used).
*/
void
gui_chat_pipe_end ()
{
struct t_gui_buffer *pipe_buffer;
struct t_hashtable *hashtable;
int pipe_send_to_buffer;
FILE *pipe_file;
pipe_buffer = gui_chat_pipe_buffer;
pipe_send_to_buffer = gui_chat_pipe_send_to_buffer;
pipe_file = gui_chat_pipe_file;
gui_chat_pipe = 0;
gui_chat_pipe_buffer = NULL;
gui_chat_pipe_send_to_buffer = 0;
gui_chat_pipe_file = NULL;
free (gui_chat_pipe_concat_sep);
gui_chat_pipe_concat_sep = NULL;
free (gui_chat_pipe_strip_chars);
gui_chat_pipe_strip_chars = NULL;
gui_chat_pipe_skip_empty_lines = 0;
if (gui_chat_pipe_concat_lines)
{
if (pipe_file)
{
fprintf (pipe_file, "%s\n", *gui_chat_pipe_concat_lines);
}
else if (pipe_buffer)
{
if (pipe_send_to_buffer)
{
gui_chat_pipe_send_buffer_input (
pipe_buffer,
*gui_chat_pipe_concat_lines);
}
else
{
gui_chat_printf (pipe_buffer,
"%s", *gui_chat_pipe_concat_lines);
}
}
else if (gui_chat_pipe_hsignal)
{
hashtable = hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL, NULL);
if (hashtable)
{
hashtable_set (hashtable, "command", gui_chat_pipe_command);
hashtable_set (hashtable, "output", *gui_chat_pipe_concat_lines);
hook_hsignal_send (gui_chat_pipe_hsignal, hashtable);
hashtable_free (hashtable);
}
}
string_dyn_free (gui_chat_pipe_concat_lines, 1);
gui_chat_pipe_concat_lines = NULL;
}
if (pipe_file)
fclose (pipe_file);
free (gui_chat_pipe_command);
gui_chat_pipe_command = NULL;
free (gui_chat_pipe_hsignal);
gui_chat_pipe_hsignal = NULL;
}
/*
* Displays a message in a buffer with optional date and tags.
* This function is called internally by the function
@@ -771,6 +983,12 @@ gui_chat_printf_datetime_tags_internal (struct t_gui_buffer *buffer,
}
}
if (gui_chat_pipe_handle_line (new_line))
{
/* line was handled with /pipe command, do NOT display it */
goto no_print;
}
/* add line in the buffer */
gui_line_add (new_line);
@@ -883,6 +1101,8 @@ gui_chat_printf_datetime_tags (struct t_gui_buffer *buffer,
if (gui_init_ok)
{
if (gui_chat_pipe_buffer)
buffer = gui_chat_pipe_buffer;
if (!buffer)
buffer = gui_buffer_search_main ();
if (!gui_chat_buffer_valid (buffer, GUI_BUFFER_TYPE_FORMATTED))
+12
View File
@@ -71,6 +71,17 @@ extern char gui_chat_prefix_empty[];
extern int gui_chat_time_length;
extern int gui_chat_mute;
extern struct t_gui_buffer *gui_chat_mute_buffer;
extern int gui_chat_pipe;
extern char *gui_chat_pipe_command;
extern struct t_gui_buffer *gui_chat_pipe_buffer;
extern int gui_chat_pipe_send_to_buffer;
extern FILE *gui_chat_pipe_file;
extern char *gui_chat_pipe_hsignal;
extern char *gui_chat_pipe_concat_sep;
extern char **gui_chat_pipe_concat_lines;
extern char *gui_chat_pipe_strip_chars;
extern int gui_chat_pipe_skip_empty_lines;
extern int gui_chat_display_tags;
/* chat functions */
@@ -96,6 +107,7 @@ extern int gui_chat_get_time_length ();
extern void gui_chat_change_time_format ();
extern int gui_chat_buffer_valid (struct t_gui_buffer *buffer,
int buffer_type);
extern void gui_chat_pipe_end ();
extern void gui_chat_printf_datetime_tags (struct t_gui_buffer *buffer,
time_t date, int date_usec,
const char *tags,