Started working on the plugin configuration and loader code.
The example plugin works, but does not do anything.
This commit is contained in:
parent
7218011449
commit
ccaa4860b4
@ -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;
|
||||
|
@ -111,5 +111,73 @@ struct uhub_plugin_handle* plugin_load(const char* filename, const char* config)
|
||||
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 */
|
||||
|
@ -22,6 +22,8 @@
|
||||
|
||||
#ifdef PLUGIN_SUPPORT
|
||||
|
||||
struct hub_config;
|
||||
struct linked_list;
|
||||
struct uhub_plugin_handle;
|
||||
|
||||
struct uhub_plugin
|
||||
@ -31,16 +33,25 @@ struct uhub_plugin
|
||||
#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);
|
||||
|
||||
|
||||
// Load and register a plugin
|
||||
extern struct uhub_plugin_handle* plugin_load(const char* filename, const char* config);
|
||||
extern void plugin_unload(struct uhub_plugin_handle*);
|
||||
|
||||
#endif /* PLUGIN_SUPPORT */
|
||||
|
||||
|
@ -22,11 +22,19 @@
|
||||
|
||||
#define PLUGIN_API_VERSION 0
|
||||
|
||||
#ifndef MAX_NICK_LEN
|
||||
#define MAX_NICK_LEN 64
|
||||
#endif
|
||||
|
||||
#ifndef MAX_PASS_LEN
|
||||
#define MAX_PASS_LEN 64
|
||||
#endif
|
||||
|
||||
struct ip_addr_encap;
|
||||
|
||||
struct plugin_user
|
||||
{
|
||||
sid_t sid;
|
||||
unsigned int sid;
|
||||
const char* nick;
|
||||
const char* cid;
|
||||
struct ip_addr_encap addr;
|
||||
|
@ -2,7 +2,7 @@
|
||||
* This is a minimal example plugin for uhub.
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
// #include "uhub.h"
|
||||
#include "plugin_api/handle.h"
|
||||
|
||||
int plugin_register(struct uhub_plugin_handle* plugin, const char* config)
|
||||
@ -14,11 +14,15 @@ int plugin_register(struct uhub_plugin_handle* plugin, const char* config)
|
||||
plugin->plugin_api_version = PLUGIN_API_VERSION;
|
||||
plugin->plugin_funcs_size = sizeof(struct plugin_funcs);
|
||||
memset(&plugin->funcs, 0, sizeof(struct plugin_funcs));
|
||||
|
||||
puts("plugin register");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void plugin_unregister(struct uhub_plugin_handle* plugin)
|
||||
int plugin_unregister(struct uhub_plugin_handle* plugin)
|
||||
{
|
||||
/* No need to do anything! */
|
||||
puts("plugin unregister");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -81,6 +81,7 @@ extern "C" {
|
||||
#include "core/user.h"
|
||||
#include "core/usermanager.h"
|
||||
#include "core/route.h"
|
||||
#include "core/pluginloader.h"
|
||||
#include "core/hub.h"
|
||||
#include "core/commands.h"
|
||||
#include "core/inf.h"
|
||||
|
Loading…
Reference in New Issue
Block a user