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

fifo: add file fifo.conf and option fifo.file.path to customize FIFO pipe path/filename (closes #850)

This commit is contained in:
Sébastien Helleu
2016-11-22 19:24:40 +01:00
parent fe95a1c8a8
commit 0cec295a41
45 changed files with 844 additions and 499 deletions
+1
View File
@@ -20,6 +20,7 @@
add_library(fifo MODULE
fifo.c fifo.h
fifo-command.c fifo-command.h
fifo-config.c fifo-config.h
fifo-info.c fifo-info.h)
set_target_properties(fifo PROPERTIES PREFIX "")
+2
View File
@@ -27,6 +27,8 @@ fifo_la_SOURCES = fifo.c \
fifo.h \
fifo-command.c \
fifo-command.h \
fifo-config.c \
fifo-config.h \
fifo-info.c \
fifo-info.h
fifo_la_LDFLAGS = -module -no-undefined
+9 -7
View File
@@ -23,6 +23,7 @@
#include "../weechat-plugin.h"
#include "fifo.h"
#include "fifo-config.h"
/*
@@ -60,22 +61,24 @@ fifo_command_fifo (const void *pointer, void *data,
/* enable pipe */
if (weechat_strcasecmp (argv[1], "enable") == 0)
{
weechat_config_set_plugin (FIFO_OPTION_NAME, "on");
weechat_config_option_set (fifo_config_file_enabled, "on", 1);
return WEECHAT_RC_OK;
}
/* disable pipe */
if (weechat_strcasecmp (argv[1], "disable") == 0)
{
weechat_config_set_plugin (FIFO_OPTION_NAME, "off");
weechat_config_option_set (fifo_config_file_enabled, "off", 1);
return WEECHAT_RC_OK;
}
/* toggle pipe */
if (weechat_strcasecmp (argv[1], "toggle") == 0)
{
weechat_config_set_plugin (FIFO_OPTION_NAME,
(fifo_fd == -1) ? "on" : "off");
weechat_config_option_set (
fifo_config_file_enabled,
(weechat_config_boolean (fifo_config_file_enabled)) ? "off" : "on",
1);
return WEECHAT_RC_OK;
}
@@ -99,8 +102,7 @@ fifo_command_init ()
"\n"
"FIFO pipe is used as remote control of WeeChat: you can send "
"commands or text to the FIFO pipe from your shell.\n"
"By default the FIFO pipe is in ~/.weechat/weechat_fifo_xxx "
"(\"xxx\" is the WeeChat PID).\n"
"By default the FIFO pipe is in ~/.weechat/weechat_fifo\n"
"\n"
"The expected format is one of:\n"
" plugin.buffer *text or command here\n"
@@ -108,7 +110,7 @@ fifo_command_init ()
"\n"
"For example to change your freenode nick:\n"
" echo 'irc.server.freenode */nick newnick' "
">~/.weechat/weechat_fifo_12345\n"
">~/.weechat/weechat_fifo\n"
"\n"
"Please read the user's guide for more info and examples.\n"
"\n"
+160
View File
@@ -0,0 +1,160 @@
/*
* fifo-config.c - fifo configuration options (file fifo.conf)
*
* Copyright (C) 2003-2016 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include "../weechat-plugin.h"
#include "fifo.h"
#include "fifo-config.h"
struct t_config_file *fifo_config_file = NULL;
/* fifo config, file section */
struct t_config_option *fifo_config_file_enabled;
struct t_config_option *fifo_config_file_path;
/*
* Callback for changes on option "enabled".
*/
void
fifo_config_change_file_enabled (const void *pointer, void *data,
struct t_config_option *option)
{
/* make C compiler happy */
(void) pointer;
(void) data;
(void) option;
fifo_remove ();
if (weechat_config_boolean (fifo_config_file_enabled))
fifo_create();
}
/*
* Callback for changes on option "path".
*/
void
fifo_config_change_file_path (const void *pointer, void *data,
struct t_config_option *option)
{
/* make C compiler happy */
(void) pointer;
(void) data;
(void) option;
fifo_quiet = 1;
fifo_remove ();
fifo_create ();
fifo_quiet = 0;
}
/*
* Initializes fifo configuration file.
*
* Returns:
* 1: OK
* 0: error
*/
int
fifo_config_init ()
{
struct t_config_section *ptr_section;
fifo_config_file = weechat_config_new (FIFO_CONFIG_NAME,
NULL, NULL, NULL);
if (!fifo_config_file)
return 0;
/* file */
ptr_section = weechat_config_new_section (fifo_config_file, "file",
0, 0,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
if (!ptr_section)
{
weechat_config_free (fifo_config_file);
return 0;
}
fifo_config_file_enabled = weechat_config_new_option (
fifo_config_file, ptr_section,
"enabled", "boolean",
N_("enable FIFO pipe"),
NULL, 0, 0, "on", NULL, 0,
NULL, NULL, NULL,
&fifo_config_change_file_enabled, NULL, NULL,
NULL, NULL, NULL);
fifo_config_file_path = weechat_config_new_option (
fifo_config_file, ptr_section,
"path", "string",
N_("path for FIFO file; \"%h\" at beginning of string is "
"replaced by WeeChat home (\"~/.weechat\" by default); "
"WeeChat PID can be used in path with ${info:pid} "
"(note: content is evaluated, see /help eval)"),
NULL, 0, 0, "%h/weechat_fifo", NULL, 0,
NULL, NULL, NULL,
fifo_config_change_file_path, NULL, NULL,
NULL, NULL, NULL);
return 1;
}
/*
* Reads fifo configuration file.
*/
int
fifo_config_read ()
{
return weechat_config_read (fifo_config_file);
}
/*
* Writes fifo configuration file.
*/
int
fifo_config_write ()
{
return weechat_config_write (fifo_config_file);
}
/*
* Frees fifo configuration.
*/
void
fifo_config_free ()
{
weechat_config_free (fifo_config_file);
}
+34
View File
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2003-2016 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WEECHAT_FIFO_CONFIG_H
#define WEECHAT_FIFO_CONFIG_H 1
#define FIFO_CONFIG_NAME "fifo"
extern struct t_config_option *fifo_config_file_enabled;
extern struct t_config_option *fifo_config_file_path;
extern int fifo_config_init ();
extern int fifo_config_read ();
extern int fifo_config_write ();
extern void fifo_config_free ();
#endif /* WEECHAT_FIFO_CONFIG_H */
+76 -150
View File
@@ -33,6 +33,7 @@
#include "../weechat-plugin.h"
#include "fifo.h"
#include "fifo-command.h"
#include "fifo-config.h"
#include "fifo-info.h"
@@ -43,72 +44,19 @@ WEECHAT_PLUGIN_VERSION(WEECHAT_VERSION);
WEECHAT_PLUGIN_LICENSE(WEECHAT_LICENSE);
WEECHAT_PLUGIN_PRIORITY(7000);
#define FIFO_FILENAME_PREFIX "weechat_fifo_"
struct t_weechat_plugin *weechat_fifo_plugin = NULL;
#define weechat_plugin weechat_fifo_plugin
int fifo_quiet = 0;
int fifo_fd = -1;
struct t_hook *fifo_fd_hook = NULL;
char *fifo_filename;
char *fifo_filename = NULL;
char *fifo_unterminated = NULL;
int fifo_fd_cb ();
/*
* Removes old FIFO pipes in directory.
*/
void
fifo_remove_old_pipes ()
{
char *buf;
int buf_len, prefix_len;
const char *weechat_home, *dir_separator;
DIR *dp;
struct dirent *entry;
struct stat statbuf;
buf_len = PATH_MAX;
buf = malloc (buf_len);
if (!buf)
return;
weechat_home = weechat_info_get ("weechat_dir", "");
dir_separator = weechat_info_get ("dir_separator", "");
prefix_len = strlen (FIFO_FILENAME_PREFIX);
dp = opendir (weechat_home);
if (dp != NULL)
{
while ((entry = readdir (dp)) != NULL)
{
if (strcmp (entry->d_name, ".") == 0 || strcmp (entry->d_name, "..") == 0)
continue;
if (strncmp (entry->d_name, FIFO_FILENAME_PREFIX, prefix_len) == 0)
{
snprintf (buf, buf_len, "%s%s%s",
weechat_home, dir_separator, entry->d_name);
if (stat (buf, &statbuf) != -1)
{
weechat_printf (NULL,
_("%s: removing old fifo pipe \"%s\""),
FIFO_PLUGIN_NAME, buf);
unlink (buf);
}
}
}
closedir (dp);
}
free (buf);
}
/*
* Creates FIFO pipe for remote control.
*/
@@ -116,72 +64,71 @@ fifo_remove_old_pipes ()
void
fifo_create ()
{
int filename_length;
const char *fifo_option, *weechat_home;
struct stat st;
fifo_option = weechat_config_get_plugin ("fifo");
if (!fifo_option)
if (!weechat_config_boolean (fifo_config_file_enabled))
return;
if (!fifo_filename)
{
weechat_config_set_plugin ("fifo", "on");
fifo_option = weechat_config_get_plugin ("fifo");
/* replace %h and "~", evaluate path */
fifo_filename = weechat_string_eval_path_home (
weechat_config_string (fifo_config_file_path),
NULL, NULL, NULL);
}
weechat_home = weechat_info_get ("weechat_dir", "");
if (fifo_option && weechat_home)
if (!fifo_filename)
{
fifo_remove_old_pipes ();
weechat_printf (NULL,
_("%s%s: not enough memory (%s)"),
weechat_prefix ("error"), FIFO_PLUGIN_NAME,
"fifo_filename");
return;
}
if (weechat_strcasecmp (fifo_option, "on") == 0)
/* remove a pipe with same name (if exists) */
if (stat (fifo_filename, &st) == 0)
{
/* if the file is a FIFO pipe, delete it */
if (S_ISFIFO(st.st_mode))
unlink (fifo_filename);
}
fifo_fd = -1;
/* create FIFO pipe, writable for user only */
if (mkfifo (fifo_filename, 0600) == 0)
{
/* open FIFO pipe in read-only, non-blocking mode */
if ((fifo_fd = open (fifo_filename,
O_RDONLY | O_NONBLOCK)) != -1)
{
/*
* build FIFO filename:
* "<weechat_home>/weechat_fifo_" + process PID
*/
if (!fifo_filename)
{
filename_length = strlen (weechat_home) + 64;
fifo_filename = malloc (filename_length);
snprintf (fifo_filename, filename_length,
"%s/%s%d",
weechat_home, FIFO_FILENAME_PREFIX, (int) getpid());
}
fifo_fd = -1;
/* create FIFO pipe, writable for user only */
if (mkfifo (fifo_filename, 0600) == 0)
{
/* open FIFO pipe in read-only, non-blocking mode */
if ((fifo_fd = open (fifo_filename,
O_RDONLY | O_NONBLOCK)) != -1)
{
if ((weechat_fifo_plugin->debug >= 1) || !fifo_quiet)
{
weechat_printf (NULL,
_("%s: pipe opened (file: %s)"),
FIFO_PLUGIN_NAME,
fifo_filename);
}
fifo_fd_hook = weechat_hook_fd (fifo_fd, 1, 0, 0,
&fifo_fd_cb, NULL, NULL);
}
else
weechat_printf (NULL,
_("%s%s: unable to open pipe (%s) for "
"reading"),
weechat_prefix ("error"), FIFO_PLUGIN_NAME,
fifo_filename);
}
else
if ((weechat_fifo_plugin->debug >= 1) || !fifo_quiet)
{
weechat_printf (NULL,
_("%s%s: unable to create pipe for remote "
"control (%s): error %d %s"),
weechat_prefix ("error"), FIFO_PLUGIN_NAME,
fifo_filename, errno, strerror (errno));
_("%s: pipe opened (file: %s)"),
FIFO_PLUGIN_NAME,
fifo_filename);
}
fifo_fd_hook = weechat_hook_fd (fifo_fd, 1, 0, 0,
&fifo_fd_cb, NULL, NULL);
}
else
{
weechat_printf (NULL,
_("%s%s: unable to open pipe (%s) for "
"reading"),
weechat_prefix ("error"), FIFO_PLUGIN_NAME,
fifo_filename);
}
}
else
{
weechat_printf (NULL,
_("%s%s: unable to create pipe for remote "
"control (%s): error %d %s"),
weechat_prefix ("error"), FIFO_PLUGIN_NAME,
fifo_filename, errno, strerror (errno));
}
}
@@ -192,6 +139,10 @@ fifo_create ()
void
fifo_remove ()
{
int fifo_found;
fifo_found = (fifo_fd != -1);
/* remove fd hook */
if (fifo_fd_hook)
{
@@ -206,10 +157,6 @@ fifo_remove ()
fifo_fd = -1;
}
/* remove FIFO from disk */
if (fifo_filename)
unlink (fifo_filename);
/* remove any unterminated message */
if (fifo_unterminated)
{
@@ -217,16 +164,20 @@ fifo_remove ()
fifo_unterminated = NULL;
}
/* free filename */
/* remove FIFO from disk */
if (fifo_filename)
{
unlink (fifo_filename);
free (fifo_filename);
fifo_filename = NULL;
}
weechat_printf (NULL,
_("%s: pipe closed"),
FIFO_PLUGIN_NAME);
if (fifo_found && !fifo_quiet)
{
weechat_printf (NULL,
_("%s: pipe closed"),
FIFO_PLUGIN_NAME);
}
}
/*
@@ -397,33 +348,6 @@ fifo_fd_cb (const void *pointer, void *data, int fd)
return WEECHAT_RC_OK;
}
/*
* Callback for changes on option "plugins.var.fifo.fifo".
*/
int
fifo_config_cb (const void *pointer, void *data,
const char *option, const char *value)
{
/* make C compiler happy */
(void) pointer;
(void) data;
(void) option;
if (weechat_strcasecmp (value, "on") == 0)
{
if (fifo_fd < 0)
fifo_create ();
}
else
{
if (fifo_fd >= 0)
fifo_remove ();
}
return WEECHAT_RC_OK;
}
/*
* Initializes fifo plugin.
*/
@@ -431,22 +355,21 @@ fifo_config_cb (const void *pointer, void *data,
int
weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
{
char str_option[256];
/* make C compiler happy */
(void) argc;
(void) argv;
weechat_plugin = plugin;
if (!fifo_config_init ())
return WEECHAT_RC_ERROR;
fifo_config_read ();
fifo_quiet = 1;
fifo_create ();
snprintf (str_option, sizeof (str_option),
"plugins.var.fifo.%s", FIFO_OPTION_NAME);
weechat_hook_config (str_option, &fifo_config_cb, NULL, NULL);
fifo_command_init ();
fifo_info_init ();
@@ -467,5 +390,8 @@ weechat_plugin_end (struct t_weechat_plugin *plugin)
fifo_remove ();
fifo_config_write ();
fifo_config_free ();
return WEECHAT_RC_OK;
}
+4
View File
@@ -25,7 +25,11 @@
#define FIFO_OPTION_NAME "fifo"
extern struct t_weechat_plugin *weechat_fifo_plugin;
extern int fifo_quiet;
extern int fifo_fd;
extern char *fifo_filename;
extern void fifo_create ();
extern void fifo_remove ();
#endif /* WEECHAT_FIFO_H */