Merge branch 'plugin_work' of git://github.com/janvidar/uhub
This commit is contained in:
@@ -490,6 +490,19 @@
|
||||
]]></example>
|
||||
</option>
|
||||
|
||||
<option name="file_plugins" type="file" default="">
|
||||
<short>Plugin configuration file</short>
|
||||
<description><![CDATA[
|
||||
Plugin configuration file.
|
||||
]]></description>
|
||||
<since>0.3.3</since>
|
||||
<example><![CDATA[
|
||||
<p>
|
||||
file_plugins = "/etc/uhub/plugins.conf"
|
||||
</p>
|
||||
]]></example>
|
||||
</option>
|
||||
|
||||
<option name="msg_hub_full" type="message" default="Hub is full" >
|
||||
<description><![CDATA[This will be sent if the hub is full]]></description>
|
||||
<since>0.2.0</since>
|
||||
|
||||
@@ -47,6 +47,7 @@ void config_defaults(struct hub_config* config)
|
||||
config->file_motd = hub_strdup("");
|
||||
config->file_acl = hub_strdup("");
|
||||
config->file_rules = hub_strdup("");
|
||||
config->file_plugins = hub_strdup("");
|
||||
config->msg_hub_full = hub_strdup("Hub is full");
|
||||
config->msg_hub_disabled = hub_strdup("Hub is disabled");
|
||||
config->msg_hub_registered_users_only = hub_strdup("Hub is for registered users only");
|
||||
@@ -545,6 +546,16 @@ static int apply_config(struct hub_config* config, char* key, char* data, int li
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!strcmp(key, "file_plugins"))
|
||||
{
|
||||
if (!apply_string(key, data, &config->file_plugins, (char*) ""))
|
||||
{
|
||||
LOG_ERROR("Configuration parse error on line %d", line_count);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!strcmp(key, "msg_hub_full"))
|
||||
{
|
||||
if (!apply_string(key, data, &config->msg_hub_full, (char*) ""))
|
||||
@@ -932,6 +943,8 @@ void free_config(struct hub_config* config)
|
||||
|
||||
hub_free(config->file_rules);
|
||||
|
||||
hub_free(config->file_plugins);
|
||||
|
||||
hub_free(config->msg_hub_full);
|
||||
|
||||
hub_free(config->msg_hub_disabled);
|
||||
@@ -1143,6 +1156,9 @@ void dump_config(struct hub_config* config, int ignore_defaults)
|
||||
if (!ignore_defaults || strcmp(config->file_rules, "") != 0)
|
||||
fprintf(stdout, "file_rules = \"%s\"\n", config->file_rules);
|
||||
|
||||
if (!ignore_defaults || strcmp(config->file_plugins, "") != 0)
|
||||
fprintf(stdout, "file_plugins = \"%s\"\n", config->file_plugins);
|
||||
|
||||
if (!ignore_defaults || strcmp(config->msg_hub_full, "Hub is full") != 0)
|
||||
fprintf(stdout, "msg_hub_full = \"%s\"\n", config->msg_hub_full);
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ struct hub_config
|
||||
char* file_motd; /*<<< File containing the 'message of the day (default: ) */
|
||||
char* file_acl; /*<<< File containing access control lists (default: ) */
|
||||
char* file_rules; /*<<< File containing hub rules (default: ) */
|
||||
char* file_plugins; /*<<< Plugin configuration file (default: ) */
|
||||
char* msg_hub_full; /*<<< "Hub is full" */
|
||||
char* msg_hub_disabled; /*<<< "Hub is disabled" */
|
||||
char* msg_hub_registered_users_only; /*<<< "Hub is for registered users only" */
|
||||
|
||||
@@ -806,6 +806,40 @@ void hub_shutdown_service(struct hub_info* hub)
|
||||
g_hub = 0;
|
||||
}
|
||||
|
||||
#ifdef PLUGIN_SUPPORT
|
||||
void hub_plugins_load(struct hub_info* hub)
|
||||
{
|
||||
if (!hub->config->file_plugins || !*hub->config->file_plugins)
|
||||
return;
|
||||
|
||||
hub->plugins = hub_malloc_zero(sizeof(struct uhub_plugins));
|
||||
if (!hub->plugins)
|
||||
return;
|
||||
|
||||
if (plugin_initialize(hub->config, hub->plugins) < 0)
|
||||
{
|
||||
hub_free(hub->plugins);
|
||||
hub->plugins = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void hub_plugins_unload(struct hub_info* hub)
|
||||
{
|
||||
struct uhub_plugin_handle* plugin = (struct uhub_plugin_handle*) list_get_first(hub->plugins->loaded);
|
||||
while (plugin)
|
||||
{
|
||||
plugin_unload(plugin);
|
||||
plugin = (struct uhub_plugin_handle*) list_get_next(hub->plugins->loaded);
|
||||
}
|
||||
|
||||
list_destroy(hub->plugins->loaded);
|
||||
hub_free(hub->plugins->plugin_dir);
|
||||
hub_free(hub->plugins);
|
||||
hub->plugins = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
void hub_set_variables(struct hub_info* hub, struct acl_handle* acl)
|
||||
{
|
||||
int fd, ret;
|
||||
@@ -880,6 +914,10 @@ void hub_set_variables(struct hub_info* hub, struct acl_handle* acl)
|
||||
hub_free(tmp);
|
||||
}
|
||||
|
||||
#ifdef PLUGIN_SUPPORT
|
||||
hub_plugins_load(hub);
|
||||
#endif
|
||||
|
||||
hub->status = (hub->config->hub_enabled ? hub_status_running : hub_status_disabled);
|
||||
hub_free(server);
|
||||
}
|
||||
@@ -887,6 +925,10 @@ void hub_set_variables(struct hub_info* hub, struct acl_handle* acl)
|
||||
|
||||
void hub_free_variables(struct hub_info* hub)
|
||||
{
|
||||
#ifdef PLUGIN_SUPPORT
|
||||
hub_plugins_unload(hub);
|
||||
#endif
|
||||
|
||||
adc_msg_free(hub->command_info);
|
||||
adc_msg_free(hub->command_banner);
|
||||
|
||||
|
||||
@@ -114,6 +114,10 @@ struct hub_info
|
||||
struct linked_list* chat_history; /* Chat history */
|
||||
struct linked_list* logout_info; /* Log of people logging out. */
|
||||
|
||||
#ifdef PLUGIN_SUPPORT
|
||||
struct uhub_plugins* plugins;
|
||||
#endif
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
SSL_METHOD* ssl_method;
|
||||
SSL_CTX* ssl_ctx;
|
||||
|
||||
183
src/core/pluginloader.c
Normal file
183
src/core/pluginloader.c
Normal file
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
#ifdef PLUGIN_SUPPORT
|
||||
#include "plugin_api/handle.h"
|
||||
|
||||
struct uhub_plugin* plugin_open(const char* filename)
|
||||
{
|
||||
#ifdef HAVE_DLOPEN
|
||||
struct uhub_plugin* plugin = (struct uhub_plugin*) hub_malloc_zero(sizeof(struct uhub_plugin));
|
||||
if (!plugin)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
plugin->handle = dlopen(filename, RTLD_LAZY);
|
||||
|
||||
if (!plugin->handle)
|
||||
{
|
||||
LOG_ERROR("Unable to open plugin %s: %s", filename, dlerror());
|
||||
hub_free(plugin);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return plugin;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void plugin_close(struct uhub_plugin* plugin)
|
||||
{
|
||||
#ifdef HAVE_DLOPEN
|
||||
dlclose(plugin->handle);
|
||||
hub_free(plugin);
|
||||
#endif
|
||||
}
|
||||
|
||||
void* plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol)
|
||||
{
|
||||
#ifdef HAVE_DLOPEN
|
||||
void* addr = dlsym(plugin->handle, symbol);
|
||||
return addr;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
struct uhub_plugin_handle* plugin_load(const char* filename, const char* config)
|
||||
{
|
||||
plugin_register_f register_f;
|
||||
plugin_unregister_f unregister_f;
|
||||
int ret;
|
||||
struct uhub_plugin_handle* handle = hub_malloc_zero(sizeof(struct uhub_plugin_handle));
|
||||
struct uhub_plugin* plugin = plugin_open(filename);
|
||||
|
||||
if (!plugin)
|
||||
return NULL;
|
||||
|
||||
if (!handle)
|
||||
{
|
||||
plugin_close(plugin);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
handle->handle = plugin;
|
||||
register_f = plugin_lookup_symbol(plugin, "plugin_register");
|
||||
unregister_f = plugin_lookup_symbol(plugin, "plugin_unregister");
|
||||
|
||||
if (register_f && unregister_f)
|
||||
{
|
||||
ret = register_f(handle, config);
|
||||
if (ret == 0)
|
||||
{
|
||||
if (handle->plugin_api_version == PLUGIN_API_VERSION && handle->plugin_funcs_size == sizeof(struct plugin_funcs))
|
||||
{
|
||||
LOG_INFO("Loaded plugin: %s: \"%s\", version %s.", filename, handle->name, handle->version);
|
||||
return handle;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("Unable to load plugin: %s - API version mistmatch", filename);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("Unable to load plugin: %s - Failed to initialize", filename);
|
||||
}
|
||||
}
|
||||
|
||||
plugin_close(plugin);
|
||||
hub_free(handle);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void plugin_unload(struct uhub_plugin_handle* plugin)
|
||||
{
|
||||
plugin_close(plugin->handle);
|
||||
}
|
||||
|
||||
static int plugin_parse_line(char* line, int line_count, void* ptr_data)
|
||||
{
|
||||
struct uhub_plugins* handle = (struct uhub_plugins*) ptr_data;
|
||||
char* pos;
|
||||
|
||||
strip_off_ini_line_comments(line, line_count);
|
||||
|
||||
line = strip_white_space(line);
|
||||
if (!*line)
|
||||
return 0;
|
||||
|
||||
LOG_TRACE("plugin: parse line %d: \"%s\"", line_count, line);
|
||||
|
||||
// Set plugin directory.
|
||||
pos = strstr(line, "plugin_directory");
|
||||
if (pos && is_white_space(line[(pos - line) + strlen("plugin_directory")]))
|
||||
{
|
||||
if (handle->plugin_dir)
|
||||
hub_free(handle->plugin_dir);
|
||||
handle->plugin_dir = strdup(strip_white_space(pos + strlen("plugin_directory") + 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Load plugin
|
||||
pos = strstr(line, "plugin");
|
||||
if (pos && is_white_space(line[(pos - line) + strlen("plugin")]))
|
||||
{
|
||||
char* data = strip_white_space(pos + strlen("plugin") + 1);
|
||||
if (*data)
|
||||
{
|
||||
LOG_TRACE("Load plugin: \"%s\"", data);
|
||||
struct uhub_plugin_handle* plugin = plugin_load(data, "");
|
||||
if (plugin)
|
||||
{
|
||||
list_append(handle->loaded, plugin);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int plugin_initialize(struct hub_config* config, struct uhub_plugins* handle)
|
||||
{
|
||||
int ret;
|
||||
|
||||
handle->loaded = list_create();
|
||||
if (!handle->loaded)
|
||||
return -1;
|
||||
|
||||
if (config)
|
||||
{
|
||||
if (!*config->file_plugins)
|
||||
return 0;
|
||||
|
||||
ret = file_read_lines(config->file_plugins, handle, &plugin_parse_line);
|
||||
if (ret == -1)
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#endif /* PLUGIN_SUPPORT */
|
||||
59
src/core/pluginloader.h
Normal file
59
src/core/pluginloader.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef HAVE_UHUB_PLUGIN_LOADER_H
|
||||
#define HAVE_UHUB_PLUGIN_LOADER_H
|
||||
|
||||
#ifdef PLUGIN_SUPPORT
|
||||
|
||||
struct hub_config;
|
||||
struct linked_list;
|
||||
struct uhub_plugin_handle;
|
||||
|
||||
struct uhub_plugin
|
||||
{
|
||||
#ifdef HAVE_DLOPEN
|
||||
void* handle;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct uhub_plugins
|
||||
{
|
||||
struct linked_list* loaded;
|
||||
char* plugin_dir;
|
||||
};
|
||||
|
||||
// High level plugin loader ode
|
||||
extern struct uhub_plugin_handle* plugin_load(const char* filename, const char* config);
|
||||
extern void plugin_unload(struct uhub_plugin_handle* plugin);
|
||||
|
||||
// extern void plugin_unload(struct uhub_plugin_handle*);
|
||||
extern int plugin_initialize(struct hub_config* config, struct uhub_plugins* handle);
|
||||
|
||||
// Low level plugin loader code (used internally)
|
||||
extern struct uhub_plugin* plugin_open(const char* filename);
|
||||
extern void plugin_close(struct uhub_plugin*);
|
||||
extern void* plugin_lookup_symbol(struct uhub_plugin*, const char* symbol);
|
||||
|
||||
|
||||
|
||||
#endif /* PLUGIN_SUPPORT */
|
||||
|
||||
#endif /* HAVE_UHUB_PLUGIN_LOADER_H */
|
||||
|
||||
Reference in New Issue
Block a user