1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-10 11:43:13 +02:00

core: allow list for option weechat.plugin.extension (makes weechat.conf portable accross Un*x and Windows) (task #11479)

This commit is contained in:
Sebastien Helleu
2011-11-15 10:42:46 +01:00
parent 07ed065ab7
commit e5a74fca72
20 changed files with 305 additions and 181 deletions
+9 -6
View File
@@ -3572,7 +3572,7 @@ command_plugin_list (const char *name, int full)
COMMAND_CALLBACK(plugin)
{
int plugin_argc;
char **plugin_argv;
char **plugin_argv, *full_name;
/* make C compiler happy */
(void) data;
@@ -3614,16 +3614,19 @@ COMMAND_CALLBACK(plugin)
{
if (argc > 2)
{
plugin_argv = NULL;
plugin_argc = 0;
if (argc > 3)
{
plugin_argv = string_split (argv_eol[3], " ", 0, 0,
&plugin_argc);
plugin_load (argv[2], plugin_argc, plugin_argv);
if (plugin_argv)
string_free_split (plugin_argv);
}
else
plugin_load (argv[2], 0, NULL);
full_name = util_search_full_lib_name (argv[2], "plugins");
plugin_load (full_name, plugin_argc, plugin_argv);
if (full_name)
free (full_name);
if (plugin_argv)
string_free_split (plugin_argv);
}
else
{
+32 -9
View File
@@ -242,6 +242,8 @@ int config_day_change_old_day = -1;
regex_t *config_highlight_regex = NULL;
char **config_highlight_tags = NULL;
int config_num_highlight_tags = 0;
char **config_plugin_extensions = NULL;
int config_num_plugin_extensions = 0;
/*
@@ -534,6 +536,31 @@ config_change_network_gnutls_ca_file (void *data,
network_set_gnutls_ca_file ();
}
/*
* config_change_plugin_extension: called when plugin extension is changed
*/
void
config_change_plugin_extension (void *data, struct t_config_option *option)
{
/* make C compiler happy */
(void) data;
(void) option;
if (config_plugin_extensions)
{
string_free_split (config_plugin_extensions);
config_plugin_extensions = NULL;
}
config_num_plugin_extensions = 0;
if (CONFIG_STRING(config_plugin_extension)
&& CONFIG_STRING(config_plugin_extension)[0])
{
config_plugin_extensions = string_split (CONFIG_STRING(config_plugin_extension),
",", 0, 0, &config_num_plugin_extensions);
}
}
/*
* config_day_change_timer_cb: timer callback for displaying
@@ -2600,15 +2627,9 @@ config_weechat_init_options ()
config_plugin_extension = config_file_new_option (
weechat_config_file, ptr_section,
"extension", "string",
N_("standard plugins extension in filename (for example "
"\".so\" under Linux or \".dll\" under Microsoft Windows)"),
NULL, 0, 0,
#if defined(WIN32) || defined(__CYGWIN__)
".dll",
#else
".so",
#endif
NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL);
N_("comma separated list of file name extensions for plugins"),
NULL, 0, 0, ".so,.dll", NULL, 0, NULL, NULL,
&config_change_plugin_extension, NULL, NULL, NULL);
config_plugin_path = config_file_new_option (
weechat_config_file, ptr_section,
"path", "string",
@@ -2736,6 +2757,8 @@ config_weechat_init ()
config_change_highlight_regex (NULL, NULL);
if (!config_highlight_tags)
config_change_highlight_tags (NULL, NULL);
if (!config_plugin_extensions)
config_change_plugin_extension (NULL, NULL);
return rc;
}
+2
View File
@@ -252,6 +252,8 @@ extern struct t_config_option *config_plugin_save_config_on_unload;
extern regex_t *config_highlight_regex;
extern char **config_highlight_tags;
extern int config_num_highlight_tags;
extern char **config_plugin_extensions;
extern int config_num_plugin_extensions;
extern struct t_config_option *config_weechat_debug_get (const char *plugin_name);
+80 -25
View File
@@ -287,50 +287,49 @@ util_exec_on_files (const char *directory, int hidden_files, void *data,
}
/*
* util_search_full_lib_name: search the full name of a WeeChat library
* file with a part of name
* util_search_full_lib_name: search the full name of a WeeChat library
* file with name and extension
* - look in WeeChat user's dir, then WeeChat
* global lib dir
* - sys_directory is the system directory under
* WeeChat lib prefix, for example "plugins"
* - result has to be free() after use
* - result has to be free() after use (if not NULL)
* - NULL is returned if lib is not found
*/
char *
util_search_full_lib_name (const char *filename, const char *sys_directory)
util_search_full_lib_name_ext (const char *filename, const char *extension,
const char *plugins_dir)
{
char *name_with_ext, *final_name;
int length;
struct stat st;
/* filename is already a full path */
if (strchr (filename, '/') || strchr (filename, '\\'))
return strdup (filename);
length = strlen (filename) + 16;
if (CONFIG_STRING(config_plugin_extension)
&& CONFIG_STRING(config_plugin_extension)[0])
length += strlen (CONFIG_STRING(config_plugin_extension));
length = strlen (filename) + strlen (extension) + 1;
name_with_ext = malloc (length);
if (!name_with_ext)
return strdup (filename);
strcpy (name_with_ext, filename);
if (!strchr (filename, '.')
&& CONFIG_STRING(config_plugin_extension)
&& CONFIG_STRING(config_plugin_extension)[0])
strcat (name_with_ext, CONFIG_STRING(config_plugin_extension));
return NULL;
snprintf (name_with_ext, length,
"%s%s",
filename,
(strchr (filename, '.')) ? "" : extension);
/* try WeeChat user's dir */
length = strlen (weechat_home) + strlen (name_with_ext) +
strlen (sys_directory) + 16;
strlen (plugins_dir) + 16;
final_name = malloc (length);
if (!final_name)
{
free (name_with_ext);
return strdup (filename);
return NULL;
}
snprintf (final_name, length,
"%s/%s/%s", weechat_home, sys_directory, name_with_ext);
"%s%s%s%s%s",
weechat_home,
DIR_SEPARATOR,
plugins_dir,
DIR_SEPARATOR,
name_with_ext);
if ((stat (final_name, &st) == 0) && (st.st_size > 0))
{
free (name_with_ext);
@@ -340,15 +339,20 @@ util_search_full_lib_name (const char *filename, const char *sys_directory)
/* try WeeChat global lib dir */
length = strlen (WEECHAT_LIBDIR) + strlen (name_with_ext) +
strlen (sys_directory) + 16;
strlen (plugins_dir) + 16;
final_name = malloc (length);
if (!final_name)
{
free (name_with_ext);
return strdup (filename);
return NULL;
}
snprintf (final_name, length,
"%s/%s/%s", WEECHAT_LIBDIR, sys_directory, name_with_ext);
"%s%s%s%s%s",
WEECHAT_LIBDIR,
DIR_SEPARATOR,
plugins_dir,
DIR_SEPARATOR,
name_with_ext);
if ((stat (final_name, &st) == 0) && (st.st_size > 0))
{
free (name_with_ext);
@@ -356,7 +360,58 @@ util_search_full_lib_name (const char *filename, const char *sys_directory)
}
free (final_name);
return name_with_ext;
free (name_with_ext);
return NULL;
}
/*
* util_search_full_lib_name: search the full name of a WeeChat library
* file with a part of name
* - look in WeeChat user's dir, then WeeChat
* global lib dir
* - plugins_dir is the directory under WeeChat lib
* prefix (for system dir) or under WeeChat home,
* for example "plugins"
* - result has to be free() after use (if not NULL)
*/
char *
util_search_full_lib_name (const char *filename, const char *plugins_dir)
{
char *filename2, *full_name;
int i;
/* expand home in filename */
filename2 = string_expand_home (filename);
if (!filename2)
return NULL;
/* if full path, return it */
if (strchr (filename2, '/') || strchr (filename2, '\\'))
return filename2;
if (config_plugin_extensions)
{
for (i = 0; i < config_num_plugin_extensions; i++)
{
full_name = util_search_full_lib_name_ext (filename2,
config_plugin_extensions[i],
plugins_dir);
if (full_name)
return full_name;
}
}
else
{
full_name = util_search_full_lib_name_ext (filename2, "", plugins_dir);
if (full_name)
return full_name;
}
free (filename2);
return strdup (filename);
}
/*
+88 -73
View File
@@ -142,6 +142,39 @@ plugin_get_name (struct t_weechat_plugin *plugin)
return (plugin) ? plugin->name : plugin_core;
}
/*
* plugin_check_extension_allowed: check if extension of filename is allowed
* by option "weechat.plugin.extension"
*/
int
plugin_check_extension_allowed (const char *filename)
{
int i, length, length_ext;
/* extension allowed if no extension is defined */
if (!config_plugin_extensions)
return 1;
length = strlen (filename);
for (i = 0; i < config_num_plugin_extensions; i++)
{
length_ext = strlen (config_plugin_extensions[i]);
if (length >= length_ext)
{
if (string_strcasecmp (filename + length - length_ext,
config_plugin_extensions[i]) == 0)
{
/* extension allowed */
return 1;
}
}
}
/* extension not allowed */
return 0;
}
/*
* plugin_check_autoload: check if a plugin can be autoloaded or not
* return 1 if plugin can be autoloaded
@@ -151,36 +184,58 @@ plugin_get_name (struct t_weechat_plugin *plugin)
*/
int
plugin_check_autoload (char *plugin_full_name)
plugin_check_autoload (const char *filename)
{
int i, plugin_authorized, plugin_blacklisted;
char *ptr_base_name, *base_name, *plugin_name, *pos;
int i, plugin_authorized, plugin_blacklisted, length, length_ext;
char *full_name, *ptr_base_name, *base_name, *plugin_name;
/* by default we can auto load all plugins */
if (!plugin_autoload_array)
return 1;
full_name = strdup (filename);
if (!full_name)
return 0;
/* get short name of plugin (filename without extension) */
plugin_name = NULL;
ptr_base_name = basename (plugin_full_name);
ptr_base_name = basename (full_name);
if (!ptr_base_name)
{
free (full_name);
return 1;
}
base_name = strdup (ptr_base_name);
if (!base_name)
return 1;
if (CONFIG_STRING(config_plugin_extension)
&& CONFIG_STRING(config_plugin_extension)[0])
{
pos = strstr (base_name,
CONFIG_STRING(config_plugin_extension));
plugin_name = (pos) ?
string_strndup (base_name, pos - base_name) :
strdup (base_name);
free (full_name);
return 1;
}
free (full_name);
if (config_plugin_extensions)
{
length = strlen (base_name);
for (i = 0; i < config_num_plugin_extensions; i++)
{
length_ext = strlen (config_plugin_extensions[i]);
if (length >= length_ext)
{
if (string_strcasecmp (base_name + length - length_ext,
config_plugin_extensions[i]) == 0)
{
plugin_name = string_strndup (base_name, length - length_ext);
break;
}
}
}
}
else
{
plugin_name = strdup (base_name);
}
free (base_name);
@@ -242,7 +297,6 @@ plugin_find_pos (struct t_weechat_plugin *plugin)
struct t_weechat_plugin *
plugin_load (const char *filename, int argc, char **argv)
{
char *full_name, *full_name2;
void *handle;
char *name, *api_version, *author, *description, *version;
char *license, *charset;
@@ -255,39 +309,26 @@ plugin_load (const char *filename, int argc, char **argv)
if (!filename)
return NULL;
full_name = util_search_full_lib_name (filename, "plugins");
if (!full_name)
return NULL;
/*
* if plugin must not be autoloaded, then return immediately
* Note: the "plugin_autoload_array" variable is set only during auto-load,
* ie when WeeChat is starting or when doing /plugin autoload
*/
if (plugin_autoload_array && !plugin_check_autoload (full_name))
if (plugin_autoload_array && !plugin_check_autoload (filename))
return NULL;
full_name2 = string_expand_home (full_name);
if (full_name2)
{
free (full_name);
full_name = full_name2;
}
handle = dlopen (full_name, RTLD_GLOBAL | RTLD_NOW);
handle = dlopen (filename, RTLD_GLOBAL | RTLD_NOW);
if (!handle)
{
gui_chat_printf (NULL,
_("%sError: unable to load plugin \"%s\": %s"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
full_name, dlerror());
filename, dlerror());
gui_chat_printf (NULL,
_("%sIf you're trying to load a script and not a C "
"plugin, try command to load scripts (/perl, "
"/python, ...)"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR]);
free (full_name);
return NULL;
}
@@ -300,9 +341,8 @@ plugin_load (const char *filename, int argc, char **argv)
"plugin \"%s\", failed to load"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
"weechat_plugin_name",
full_name);
filename);
dlclose (handle);
free (full_name);
return NULL;
}
@@ -315,14 +355,13 @@ plugin_load (const char *filename, int argc, char **argv)
"plugin \"%s\", failed to load"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
"weechat_plugin_api_version",
full_name);
filename);
gui_chat_printf (NULL,
_("%sIf plugin \"%s\" is old/obsolete, you can "
"delete this file."),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
name);
dlclose (handle);
free (full_name);
return NULL;
}
if (strcmp (api_version, WEECHAT_PLUGIN_API_VERSION) != 0)
@@ -331,7 +370,7 @@ plugin_load (const char *filename, int argc, char **argv)
_("%sError: API mismatch for plugin \"%s\" (current "
"API: \"%s\", plugin API: \"%s\"), failed to load"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
full_name,
filename,
WEECHAT_PLUGIN_API_VERSION,
api_version);
gui_chat_printf (NULL,
@@ -340,7 +379,6 @@ plugin_load (const char *filename, int argc, char **argv)
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
name);
dlclose (handle);
free (full_name);
return NULL;
}
@@ -351,9 +389,8 @@ plugin_load (const char *filename, int argc, char **argv)
_("%sError: unable to load plugin \"%s\": a plugin "
"with same name already exists"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
full_name);
filename);
dlclose (handle);
free (full_name);
return NULL;
}
@@ -366,9 +403,8 @@ plugin_load (const char *filename, int argc, char **argv)
"in plugin \"%s\", failed to load"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
"weechat_plugin_description",
full_name);
filename);
dlclose (handle);
free (full_name);
return NULL;
}
@@ -381,9 +417,8 @@ plugin_load (const char *filename, int argc, char **argv)
"in plugin \"%s\", failed to load"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
"weechat_plugin_author",
full_name);
filename);
dlclose (handle);
free (full_name);
return NULL;
}
@@ -396,9 +431,8 @@ plugin_load (const char *filename, int argc, char **argv)
"plugin \"%s\", failed to load"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
"weechat_plugin_version",
full_name);
filename);
dlclose (handle);
free (full_name);
return NULL;
}
@@ -411,9 +445,8 @@ plugin_load (const char *filename, int argc, char **argv)
"plugin \"%s\", failed to load"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
"weechat_plugin_license",
full_name);
filename);
dlclose (handle);
free (full_name);
return NULL;
}
@@ -429,9 +462,8 @@ plugin_load (const char *filename, int argc, char **argv)
"found in plugin \"%s\", failed to load"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
"weechat_plugin_init",
full_name);
filename);
dlclose (handle);
free (full_name);
return NULL;
}
@@ -440,7 +472,7 @@ plugin_load (const char *filename, int argc, char **argv)
if (new_plugin)
{
/* variables */
new_plugin->filename = strdup (full_name);
new_plugin->filename = strdup (filename);
new_plugin->handle = handle;
new_plugin->name = strdup (name);
new_plugin->description = strdup (description);
@@ -779,9 +811,8 @@ plugin_load (const char *filename, int argc, char **argv)
_("%sError: unable to initialize plugin "
"\"%s\""),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
full_name);
filename);
plugin_remove (new_plugin);
free (full_name);
return NULL;
}
}
@@ -791,9 +822,8 @@ plugin_load (const char *filename, int argc, char **argv)
_("%sError: unable to load plugin \"%s\" "
"(not enough memory)"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
full_name);
filename);
dlclose (handle);
free (full_name);
return NULL;
}
@@ -804,8 +834,6 @@ plugin_load (const char *filename, int argc, char **argv)
name);
}
free (full_name);
return new_plugin;
}
@@ -818,24 +846,10 @@ void
plugin_auto_load_file (void *args, const char *filename)
{
struct t_plugin_args *plugin_args;
char *pos;
plugin_args = (struct t_plugin_args *)args;
if (CONFIG_STRING(config_plugin_extension)
&& CONFIG_STRING(config_plugin_extension)[0])
{
pos = strstr (filename, CONFIG_STRING(config_plugin_extension));
if (pos)
{
if (string_strcasecmp (pos,
CONFIG_STRING(config_plugin_extension)) == 0)
{
plugin_load (filename, plugin_args->argc, plugin_args->argv);
}
}
}
else
if (plugin_check_extension_allowed (filename))
plugin_load (filename, plugin_args->argc, plugin_args->argv);
}
@@ -849,6 +863,7 @@ plugin_auto_load (int argc, char **argv)
{
char *dir_name, *plugin_path, *plugin_path2;
struct t_plugin_args plugin_args;
int length;
plugin_args.argc = argc;
plugin_args.argv = argv;
@@ -885,11 +900,11 @@ plugin_auto_load (int argc, char **argv)
}
/* auto-load plugins in WeeChat global lib dir */
dir_name = malloc (strlen (WEECHAT_LIBDIR) + 16);
length = strlen (WEECHAT_LIBDIR) + 16 + 1;
dir_name = malloc (length);
if (dir_name)
{
snprintf (dir_name, strlen (WEECHAT_LIBDIR) + 16,
"%s/plugins", WEECHAT_LIBDIR);
snprintf (dir_name, length, "%s/plugins", WEECHAT_LIBDIR);
util_exec_on_files (dir_name, 0, &plugin_args, &plugin_auto_load_file);
free (dir_name);
}