1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Allow unloading all plugins

This commit is contained in:
James Booth 2017-02-05 18:54:33 +00:00
parent b768c8b5c5
commit a9fab9ed2d
5 changed files with 168 additions and 114 deletions

View File

@ -2047,13 +2047,18 @@ static struct cmd_t command_defs[] =
{ "/plugins",
parse_args, 0, 2, NULL,
CMD_NOSUBFUNCS
CMD_SUBFUNCS(
{ "install", cmd_plugins_install },
{ "load", cmd_plugins_load },
{ "unload", cmd_plugins_unload },
{ "reload", cmd_plugins_reload },
{ "python_version", cmd_plugins_python_version })
CMD_MAINFUNC(cmd_plugins)
CMD_NOTAGS
CMD_SYN(
"/plugins",
"/plugins install <path>",
"/plugins unload <plugin>",
"/plugins unload [<plugin>]",
"/plugins load <plugin>",
"/plugins reload [<plugin>]",
"/plugins python_version")
@ -2062,7 +2067,7 @@ static struct cmd_t command_defs[] =
CMD_ARGS(
{ "install <file>", "Install file to plugins directory, and load or reload the plugin." },
{ "load <plugin>", "Load a plugin that already exists in the plugin directory." },
{ "unload <plugin>", "Unload a loaded plugin." },
{ "unload [<plugin>]", "Unload a loaded plugin, passing no argument will unload all plugins." },
{ "reload [<plugin>]", "Reload a plugin, passing no argument will reload all plugins." },
{ "python_version", "Show the Python interpreter version." })
CMD_EXAMPLES(

View File

@ -6212,120 +6212,140 @@ cmd_xa(ProfWin *window, const char *const command, gchar **args)
}
gboolean
cmd_plugins(ProfWin *window, const char *const command, gchar **args)
cmd_plugins_install(ProfWin *window, const char *const command, gchar **args)
{
if (g_strcmp0(args[0], "install") == 0) {
char *filename = args[1];
if (filename == NULL) {
cons_bad_cmd_usage(command);
return TRUE;
}
// expand ~ to $HOME
if (filename[0] == '~' && filename[1] == '/') {
if (asprintf(&filename, "%s/%s", getenv("HOME"), filename+2) == -1) {
return TRUE;
}
} else {
filename = strdup(filename);
}
if (access(filename, R_OK) != 0) {
cons_show("File not found: %s", filename);
free(filename);
return TRUE;
}
if (!is_regular_file(filename)) {
cons_show("Not a file: %s", filename);
free(filename);
return TRUE;
}
if (!g_str_has_suffix(filename, ".py") && !g_str_has_suffix(filename, ".so")) {
cons_show("Plugins must have one of the following extensions: '.py' '.so'");
free(filename);
return TRUE;
}
gchar *plugin_name = g_path_get_basename(filename);
gboolean result = plugins_install(plugin_name, filename);
if (result) {
cons_show("Plugin installed: %s", plugin_name);
} else {
cons_show("Failed to install plugin: %s", plugin_name);
}
g_free(plugin_name);
free(filename);
return TRUE;
} else if (g_strcmp0(args[0], "load") == 0) {
if (args[1] == NULL) {
cons_bad_cmd_usage(command);
return TRUE;
}
gboolean res = plugins_load(args[1]);
if (res) {
cons_show("Loaded plugin: %s", args[1]);
} else {
cons_show("Failed to load plugin: %s", args[1]);
}
return TRUE;
} else if (g_strcmp0(args[0], "unload") == 0) {
if (args[1] == NULL) {
cons_bad_cmd_usage(command);
return TRUE;
}
gboolean res = plugins_unload(args[1]);
if (res) {
cons_show("Unloaded plugin: %s", args[1]);
} else {
cons_show("Failed to unload plugin: %s", args[1]);
}
return TRUE;
} else if (g_strcmp0(args[0], "reload") == 0) {
if (args[1] == NULL) {
plugins_reload_all();
cons_show("Reloaded all plugins");
} else {
gboolean res = plugins_reload(args[1]);
if (res) {
cons_show("Reloaded plugin: %s", args[1]);
} else {
cons_show("Failed to reload plugin: %s", args[1]);
}
}
return TRUE;
} else if (g_strcmp0(args[0], "python_version") == 0) {
#ifdef HAVE_PYTHON
const char *version = python_get_version();
cons_show("Python version:");
cons_show("%s", version);
#else
cons_show("This build does not support pytyon plugins.");
#endif
return TRUE;
} else {
GList *plugins = plugins_loaded_list();
if (plugins == NULL) {
cons_show("No plugins installed.");
return TRUE;
}
GList *curr = plugins;
cons_show("Installed plugins:");
while (curr) {
cons_show(" %s", curr->data);
curr = g_list_next(curr);
}
g_list_free(plugins);
char *filename = args[1];
if (filename == NULL) {
cons_bad_cmd_usage(command);
return TRUE;
}
// expand ~ to $HOME
if (filename[0] == '~' && filename[1] == '/') {
if (asprintf(&filename, "%s/%s", getenv("HOME"), filename+2) == -1) {
return TRUE;
}
} else {
filename = strdup(filename);
}
if (access(filename, R_OK) != 0) {
cons_show("File not found: %s", filename);
free(filename);
return TRUE;
}
if (!is_regular_file(filename)) {
cons_show("Not a file: %s", filename);
free(filename);
return TRUE;
}
if (!g_str_has_suffix(filename, ".py") && !g_str_has_suffix(filename, ".so")) {
cons_show("Plugins must have one of the following extensions: '.py' '.so'");
free(filename);
return TRUE;
}
gchar *plugin_name = g_path_get_basename(filename);
gboolean result = plugins_install(plugin_name, filename);
if (result) {
cons_show("Plugin installed: %s", plugin_name);
} else {
cons_show("Failed to install plugin: %s", plugin_name);
}
g_free(plugin_name);
free(filename);
return TRUE;
}
gboolean
cmd_plugins_load(ProfWin *window, const char *const command, gchar **args)
{
if (args[1] == NULL) {
cons_bad_cmd_usage(command);
return TRUE;
}
gboolean res = plugins_load(args[1]);
if (res) {
cons_show("Loaded plugin: %s", args[1]);
} else {
cons_show("Failed to load plugin: %s", args[1]);
}
return TRUE;
}
gboolean
cmd_plugins_unload(ProfWin *window, const char *const command, gchar **args)
{
if (args[1] == NULL) {
plugins_unload_all();
cons_show("Unloaded all plugins");
return TRUE;
}
gboolean res = plugins_unload(args[1]);
if (res) {
cons_show("Unloaded plugin: %s", args[1]);
} else {
cons_show("Failed to unload plugin: %s", args[1]);
}
return TRUE;
}
gboolean
cmd_plugins_reload(ProfWin *window, const char *const command, gchar **args)
{
if (args[1] == NULL) {
plugins_reload_all();
cons_show("Reloaded all plugins");
return TRUE;
}
gboolean res = plugins_reload(args[1]);
if (res) {
cons_show("Reloaded plugin: %s", args[1]);
} else {
cons_show("Failed to reload plugin: %s", args[1]);
}
return TRUE;
}
gboolean
cmd_plugins_python_version(ProfWin *window, const char *const command, gchar **args)
{
#ifdef HAVE_PYTHON
const char *version = python_get_version();
cons_show("Python version:");
cons_show("%s", version);
#else
cons_show("This build does not support pytyon plugins.");
#endif
return TRUE;
}
gboolean
cmd_plugins(ProfWin *window, const char *const command, gchar **args)
{
GList *plugins = plugins_loaded_list();
if (plugins == NULL) {
cons_show("No plugins installed.");
return TRUE;
}
GList *curr = plugins;
cons_show("Installed plugins:");
while (curr) {
cons_show(" %s", curr->data);
curr = g_list_next(curr);
}
g_list_free(plugins);
return TRUE;
}
gboolean

View File

@ -158,7 +158,14 @@ gboolean cmd_script(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_export(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_charset(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_console(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_plugins(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_plugins_install(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_plugins_load(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_plugins_unload(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_plugins_reload(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_plugins_python_version(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_blocked(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account(ProfWin *window, const char *const command, gchar **args);

View File

@ -190,6 +190,27 @@ plugins_load(const char *const name)
}
}
void
plugins_unload_all(void)
{
GList *plugin_names = g_hash_table_get_keys(plugins);
GList *plugin_names_dup = NULL;
GList *curr = plugin_names;
while (curr) {
plugin_names_dup = g_list_append(plugin_names_dup, strdup(curr->data));
curr = g_list_next(curr);
}
g_list_free(plugin_names);
curr = plugin_names_dup;
while (curr) {
plugins_unload(curr->data);
curr = g_list_next(curr);
}
g_list_free_full(plugin_names_dup, free);
}
gboolean
plugins_unload(const char *const name)
{

View File

@ -110,6 +110,7 @@ void plugins_shutdown(void);
gboolean plugins_install(const char *const plugin_name, const char *const filename);
gboolean plugins_load(const char *const name);
gboolean plugins_unload(const char *const name);
void plugins_unload_all(void);
gboolean plugins_reload(const char *const name);
void plugins_reload_all(void);