diff --git a/GNUmakefile b/GNUmakefile index 6d0ffb0..604e2fd 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -140,6 +140,7 @@ libuhub_SOURCES := \ src/core/route.c \ src/core/user.c \ src/core/usermanager.c \ + src/core/plugincallback.c \ src/core/plugininvoke.c \ src/core/pluginloader.c \ src/network/backend.c \ diff --git a/src/core/commands.c b/src/core/commands.c index 56a3e89..86e54e4 100644 --- a/src/core/commands.c +++ b/src/core/commands.c @@ -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 diff --git a/src/core/hub.c b/src/core/hub.c index 9a5ee28..77a7718 100644 --- a/src/core/hub.c +++ b/src/core/hub.c @@ -854,7 +854,7 @@ void hub_plugins_load(struct hub_info* hub) if (!hub->plugins) return; - if (plugin_initialize(hub->config, hub->plugins) < 0) + if (plugin_initialize(hub->config, hub) < 0) { hub_free(hub->plugins); hub->plugins = 0; diff --git a/src/core/plugincallback.c b/src/core/plugincallback.c new file mode 100644 index 0000000..be2974a --- /dev/null +++ b/src/core/plugincallback.c @@ -0,0 +1,84 @@ +/* + * uhub - A tiny ADC p2p connection hub + * 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 + * 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 . + * + */ + +#include "uhub.h" + +struct plugin_callback_data +{ + struct linked_list* commands; +}; + +/* +static struct plugin_callback_data* get_callback_data(struct plugin_handle* plugin) +{ + uhub_assert(plugin && plugin->handle && plugin->handle->callback_data); + struct plugin_callback_data* data = (struct plugin_callback_data*) plugin->handle->callback_data; + return data; +} +*/ + +static struct hub_user* convert_user_type(struct plugin_user* user) +{ + struct hub_user* huser = (struct hub_user*) user; + return huser; +} + +static int cbfunc_send_message(struct plugin_handle* plugin, struct plugin_user* user, const char* message) +{ +// struct plugin_callback_data* data = get_callback_data(plugin); + char* buffer = adc_msg_escape(message); + struct adc_message* command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6); + adc_msg_add_argument(command, buffer); + route_to_user(plugin_get_hub(plugin), convert_user_type(user), command); + adc_msg_free(command); + hub_free(buffer); + return 1; +} + +static int cbfunc_user_disconnect(struct plugin_handle* plugin, struct plugin_user* user) +{ + // struct plugin_callback_data* data = get_callback_data(plugin); + hub_disconnect_user(plugin_get_hub(plugin), convert_user_type(user), quit_kicked); + return 0; +} + +static int cbfunc_command_add(struct plugin_handle* plugin, struct plugin_command_handle* cmdh) +{ +// struct plugin_callback_data* data = get_callback_data(plugin); + return 0; +} + +static int cbfunc_command_del(struct plugin_handle* plugin, struct plugin_command_handle* cmdh) +{ +// struct plugin_callback_data* data = get_callback_data(plugin); + return 0; +} + + +void plugin_register_callback_functions(struct plugin_handle* handle) +{ + handle->hub.send_message = cbfunc_send_message; + handle->hub.user_disconnect = cbfunc_user_disconnect; + handle->hub.command_add = cbfunc_command_add; + handle->hub.command_del = cbfunc_command_del; +} + +void plugin_unregister_callback_functions(struct plugin_handle* handle) +{ +} diff --git a/src/core/plugincallback.h b/src/core/plugincallback.h new file mode 100644 index 0000000..0381baa --- /dev/null +++ b/src/core/plugincallback.h @@ -0,0 +1,28 @@ +/* + * uhub - A tiny ADC p2p connection hub + * 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 + * 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 . + * + */ + +#ifndef HAVE_UHUB_PLUGIN_CALLBACK_H +#define HAVE_UHUB_PLUGIN_CALLBACK_H + +struct plugin_handle; + +extern void plugin_register_callback_functions(struct plugin_handle* handle); +extern void plugin_unregister_callback_functions(struct plugin_handle* handle); + +#endif /* HAVE_UHUB_PLUGIN_CALLBACK_H */ \ No newline at end of file diff --git a/src/core/plugininvoke.c b/src/core/plugininvoke.c index 9915634..77b410a 100644 --- a/src/core/plugininvoke.c +++ b/src/core/plugininvoke.c @@ -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 @@ -78,14 +78,6 @@ static struct plugin_user* convert_user_type(struct hub_user* user) { struct plugin_user* puser = (struct plugin_user*) user; return puser; -#if 0 - puser->sid = user->id.sid; - puser->nick = user->id.nick; - puser->cid = user->id.cid; - puser->user_agent = user->user_agent; - puser->addr = user->id.addr; - puser->credentials = user->credentials; -#endif } plugin_st plugin_check_ip_early(struct hub_info* hub, struct ip_addr_encap* addr) diff --git a/src/core/plugininvoke.h b/src/core/plugininvoke.h index 4f3086b..d99b9b5 100644 --- a/src/core/plugininvoke.h +++ b/src/core/plugininvoke.h @@ -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 diff --git a/src/core/pluginloader.c b/src/core/pluginloader.c index b7413f6..33fcc7d 100644 --- a/src/core/pluginloader.c +++ b/src/core/pluginloader.c @@ -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,19 @@ #ifdef PLUGIN_SUPPORT #include "plugin_api/handle.h" +struct plugin_hub_internals +{ + struct hub_info* hub; + plugin_unregister_f unregister; +}; + +static struct plugin_hub_internals* get_internals(struct plugin_handle* handle) +{ + assert(handle && handle->handle && handle->handle->internals); + struct plugin_hub_internals* internals = (struct plugin_hub_internals*) handle->handle->internals; + return internals; +} + struct uhub_plugin* plugin_open(const char* filename) { LOG_TRACE("plugin_open: \"%s\"", filename); @@ -41,6 +54,8 @@ struct uhub_plugin* plugin_open(const char* filename) return 0; } + plugin->internals = hub_malloc_zero(sizeof(struct plugin_hub_internals)); + return plugin; #else return 0; @@ -49,6 +64,7 @@ struct uhub_plugin* plugin_open(const char* filename) void plugin_close(struct uhub_plugin* plugin) { + hub_free(plugin->internals); #ifdef HAVE_DLOPEN dlclose(plugin->handle); hub_free(plugin); @@ -65,21 +81,16 @@ void* plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol) #endif } -static void plugin_register_hub_functions(struct plugin_handle* handle) -{ - handle->hub.send_message = NULL; - handle->hub.user_disconnect = NULL; - handle->hub.command_add = NULL; - handle->hub.command_del = NULL; -} -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 uhub_plugin* plugin = plugin_open(filename); + struct plugin_hub_internals* internals; if (!plugin) return NULL; @@ -94,7 +105,9 @@ 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"); - plugin_register_hub_functions(handle); + plugin_register_callback_functions(handle); + + internals = (struct plugin_hub_internals*) plugin->internals; if (register_f && unregister_f) { @@ -105,7 +118,11 @@ 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; + + // Set hub internals + internals->unregister = unregister_f; + internals->hub = hub; + return handle; } else @@ -126,15 +143,18 @@ 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); } 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) @@ -159,7 +179,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); @@ -172,12 +192,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) @@ -185,7 +205,7 @@ 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) return -1; } @@ -205,4 +225,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 */ diff --git a/src/core/pluginloader.h b/src/core/pluginloader.h index 33b5cc4..a1e6628 100644 --- a/src/core/pluginloader.h +++ b/src/core/pluginloader.h @@ -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 @@ -23,13 +23,15 @@ #include "plugin_api/handle.h" struct hub_config; +struct hub_info; struct linked_list; struct plugin_handle; struct uhub_plugin { void* handle; - plugin_unregister_f unregister; + void* internals; // Hub internal stuff + void* callback_data; // Hub internal stuff }; struct uhub_plugins @@ -38,11 +40,11 @@ struct uhub_plugins }; // High level plugin loader ode -extern struct plugin_handle* plugin_load(const char* filename, const char* config); +extern struct plugin_handle* plugin_load(const char* filename, const char* config, struct hub_info* hub); extern void plugin_unload(struct plugin_handle* plugin); // extern void plugin_unload(struct plugin_handle*); -extern int plugin_initialize(struct hub_config* config, struct uhub_plugins* handle); +extern int plugin_initialize(struct hub_config* config, struct hub_info* hub); extern void plugin_shutdown(struct uhub_plugins* handle); // Low level plugin loader code (used internally) @@ -50,5 +52,8 @@ 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); +// Used internally only +extern struct hub_info* plugin_get_hub(struct plugin_handle*); + #endif /* HAVE_UHUB_PLUGIN_LOADER_H */ diff --git a/src/plugin_api/handle.h b/src/plugin_api/handle.h index 1e5b1b7..b7130eb 100644 --- a/src/plugin_api/handle.h +++ b/src/plugin_api/handle.h @@ -95,7 +95,7 @@ struct plugin_funcs struct plugin_command_handle; typedef int (*hfunc_send_message)(struct plugin_handle*, struct plugin_user* user, const char* message); -typedef int (*hfunc_user_disconnect)(struct plugin_user* user); +typedef int (*hfunc_user_disconnect)(struct plugin_handle*, struct plugin_user* user); typedef int (*hfunc_command_add)(struct plugin_handle*, struct plugin_command_handle*); typedef int (*hfunc_command_del)(struct plugin_handle*, struct plugin_command_handle*); diff --git a/src/uhub.h b/src/uhub.h index 44dcba4..224cab3 100644 --- a/src/uhub.h +++ b/src/uhub.h @@ -92,6 +92,7 @@ extern "C" { #include "core/commands.h" #include "core/inf.h" #include "core/hubevent.h" +#include "core/plugincallback.h" #include "core/plugininvoke.h" #include "core/pluginloader.h"