Added support for dynamic commands.
Dynamic commands are user commands that can be added dynamically to the hub by a plugin. The example plugin (mod_example.c) adds a !example command that when invoked send a message to the user who invoked it.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2011, 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
|
||||
@@ -22,6 +22,23 @@
|
||||
#ifdef PLUGIN_SUPPORT
|
||||
#include "plugin_api/handle.h"
|
||||
|
||||
struct plugin_callback_data;
|
||||
|
||||
struct plugin_hub_internals
|
||||
{
|
||||
struct hub_info* hub;
|
||||
plugin_unregister_f unregister; /* The unregister function. */
|
||||
struct plugin_callback_data* callback_data; /* callback data that is unique for the plugin */
|
||||
};
|
||||
|
||||
static struct plugin_hub_internals* get_internals(struct plugin_handle* handle)
|
||||
{
|
||||
struct plugin_hub_internals* internals;
|
||||
assert(handle && handle->handle && handle->handle->internals);
|
||||
internals = (struct plugin_hub_internals*) handle->handle->internals;
|
||||
return internals;
|
||||
}
|
||||
|
||||
struct uhub_plugin* plugin_open(const char* filename)
|
||||
{
|
||||
struct uhub_plugin* plugin;
|
||||
@@ -51,12 +68,14 @@ struct uhub_plugin* plugin_open(const char* filename)
|
||||
}
|
||||
|
||||
plugin->filename = strdup(filename);
|
||||
plugin->internals = hub_malloc_zero(sizeof(struct plugin_hub_internals));
|
||||
return plugin;
|
||||
}
|
||||
|
||||
void plugin_close(struct uhub_plugin* plugin)
|
||||
{
|
||||
LOG_TRACE("plugin_close: \"%s\"", plugin->filename);
|
||||
hub_free(plugin->internals);
|
||||
#ifdef HAVE_DLOPEN
|
||||
dlclose(plugin->handle);
|
||||
#else
|
||||
@@ -77,13 +96,16 @@ void* plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol)
|
||||
#endif
|
||||
}
|
||||
|
||||
struct plugin_handle* plugin_load(const char* filename, const char* config)
|
||||
|
||||
|
||||
struct plugin_handle* plugin_load(const char* filename, const char* config, struct hub_info* hub)
|
||||
{
|
||||
plugin_register_f register_f;
|
||||
plugin_unregister_f unregister_f;
|
||||
int ret;
|
||||
struct plugin_handle* handle = hub_malloc_zero(sizeof(struct plugin_handle));
|
||||
struct plugin_handle* handle = (struct plugin_handle*) hub_malloc_zero(sizeof(struct plugin_handle));
|
||||
struct uhub_plugin* plugin = plugin_open(filename);
|
||||
struct plugin_hub_internals* internals = (struct plugin_hub_internals*) plugin->internals;
|
||||
|
||||
if (!plugin)
|
||||
return NULL;
|
||||
@@ -98,6 +120,14 @@ struct plugin_handle* plugin_load(const char* filename, const char* config)
|
||||
register_f = plugin_lookup_symbol(plugin, "plugin_register");
|
||||
unregister_f = plugin_lookup_symbol(plugin, "plugin_unregister");
|
||||
|
||||
// register hub internals
|
||||
internals->unregister = unregister_f;
|
||||
internals->hub = hub;
|
||||
internals->callback_data = plugin_callback_data_create();
|
||||
|
||||
// setup callback functions, where the plugin can contact the hub.
|
||||
plugin_register_callback_functions(handle);
|
||||
|
||||
if (register_f && unregister_f)
|
||||
{
|
||||
ret = register_f(handle, config);
|
||||
@@ -107,7 +137,6 @@ struct plugin_handle* plugin_load(const char* filename, const char* config)
|
||||
{
|
||||
LOG_INFO("Loaded plugin: %s: %s, version %s.", filename, handle->name, handle->version);
|
||||
LOG_TRACE("Plugin API version: %d (func table size: " PRINTF_SIZE_T ")", handle->plugin_api_version, handle->plugin_funcs_size);
|
||||
plugin->unregister = unregister_f;
|
||||
return handle;
|
||||
}
|
||||
else
|
||||
@@ -128,16 +157,19 @@ struct plugin_handle* plugin_load(const char* filename, const char* config)
|
||||
|
||||
void plugin_unload(struct plugin_handle* plugin)
|
||||
{
|
||||
plugin->handle->unregister(plugin);
|
||||
struct plugin_hub_internals* internals = get_internals(plugin);
|
||||
plugin_unregister_callback_functions(plugin);
|
||||
internals->unregister(plugin);
|
||||
plugin_close(plugin->handle);
|
||||
hub_free(plugin);
|
||||
}
|
||||
|
||||
static int plugin_parse_line(char* line, int line_count, void* ptr_data)
|
||||
{
|
||||
struct uhub_plugins* handle = (struct uhub_plugins*) ptr_data;
|
||||
struct plugin_handle* plugin;
|
||||
struct hub_info* hub = (struct hub_info*) ptr_data;
|
||||
struct uhub_plugins* handle = hub->plugins;
|
||||
struct cfg_tokens* tokens = cfg_tokenize(line);
|
||||
struct plugin_handle* plugin;
|
||||
char *directive, *soname, *params;
|
||||
|
||||
if (cfg_token_count(tokens) == 0)
|
||||
@@ -162,7 +194,7 @@ static int plugin_parse_line(char* line, int line_count, void* ptr_data)
|
||||
params = "";
|
||||
|
||||
LOG_TRACE("Load plugin: \"%s\", params=\"%s\"", soname, params);
|
||||
plugin = plugin_load(soname, params);
|
||||
plugin = plugin_load(soname, params, hub);
|
||||
if (plugin)
|
||||
{
|
||||
list_append(handle->loaded, plugin);
|
||||
@@ -175,12 +207,12 @@ static int plugin_parse_line(char* line, int line_count, void* ptr_data)
|
||||
return -1;
|
||||
}
|
||||
|
||||
int plugin_initialize(struct hub_config* config, struct uhub_plugins* handle)
|
||||
int plugin_initialize(struct hub_config* config, struct hub_info* hub)
|
||||
{
|
||||
int ret;
|
||||
|
||||
handle->loaded = list_create();
|
||||
if (!handle->loaded)
|
||||
hub->plugins->loaded = list_create();
|
||||
if (!hub->plugins->loaded)
|
||||
return -1;
|
||||
|
||||
if (config)
|
||||
@@ -188,12 +220,12 @@ int plugin_initialize(struct hub_config* config, struct uhub_plugins* handle)
|
||||
if (!*config->file_plugins)
|
||||
return 0;
|
||||
|
||||
ret = file_read_lines(config->file_plugins, handle, &plugin_parse_line);
|
||||
ret = file_read_lines(config->file_plugins, hub, &plugin_parse_line);
|
||||
if (ret == -1)
|
||||
{
|
||||
list_clear(handle->loaded, hub_free);
|
||||
list_destroy(handle->loaded);
|
||||
handle->loaded = 0;
|
||||
list_clear(hub->plugins->loaded, hub_free);
|
||||
list_destroy(hub->plugins->loaded);
|
||||
hub->plugins->loaded = 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -213,4 +245,11 @@ void plugin_shutdown(struct uhub_plugins* handle)
|
||||
list_destroy(handle->loaded);
|
||||
}
|
||||
|
||||
// Used internally only
|
||||
struct hub_info* plugin_get_hub(struct plugin_handle* plugin)
|
||||
{
|
||||
struct plugin_hub_internals* data = get_internals(plugin);
|
||||
return data->hub;
|
||||
}
|
||||
|
||||
#endif /* PLUGIN_SUPPORT */
|
||||
|
||||
Reference in New Issue
Block a user