From ccaa4860b49237f1d03a9aff50f77f03c8f74992 Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Sat, 12 Jun 2010 02:54:53 +0200 Subject: [PATCH] Started working on the plugin configuration and loader code. The example plugin works, but does not do anything. --- src/core/hub.c | 42 ++++++++++++++++++++++++ src/core/hub.h | 4 +++ src/core/pluginloader.c | 68 +++++++++++++++++++++++++++++++++++++++ src/core/pluginloader.h | 21 +++++++++--- src/plugin_api/handle.h | 10 +++++- src/plugins/mod_example.c | 8 +++-- src/uhub.h | 1 + 7 files changed, 146 insertions(+), 8 deletions(-) diff --git a/src/core/hub.c b/src/core/hub.c index 499621e..7c7fdf0 100644 --- a/src/core/hub.c +++ b/src/core/hub.c @@ -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); diff --git a/src/core/hub.h b/src/core/hub.h index 919707f..f7453c3 100644 --- a/src/core/hub.h +++ b/src/core/hub.h @@ -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; diff --git a/src/core/pluginloader.c b/src/core/pluginloader.c index 5b48bc4..ce89286 100644 --- a/src/core/pluginloader.c +++ b/src/core/pluginloader.c @@ -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 */ diff --git a/src/core/pluginloader.h b/src/core/pluginloader.h index 5924157..b9729bd 100644 --- a/src/core/pluginloader.h +++ b/src/core/pluginloader.h @@ -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 */ diff --git a/src/plugin_api/handle.h b/src/plugin_api/handle.h index afe60b9..2469123 100644 --- a/src/plugin_api/handle.h +++ b/src/plugin_api/handle.h @@ -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; diff --git a/src/plugins/mod_example.c b/src/plugins/mod_example.c index 904219f..97406ae 100644 --- a/src/plugins/mod_example.c +++ b/src/plugins/mod_example.c @@ -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; } diff --git a/src/uhub.h b/src/uhub.h index c0dfa29..17fdc79 100644 --- a/src/uhub.h +++ b/src/uhub.h @@ -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"