From 5ca27a1a6d31f61e0f4edaa6152f9eccfad4a99d Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Mon, 20 Dec 2010 11:32:35 +0100 Subject: [PATCH] Added function pointers for plugins to access hub internals. --- GNUmakefile | 7 +++- src/core/pluginloader.c | 10 +++++ src/plugin_api/command_api.h | 79 ++++++++++++++++++++++++++++++++++++ src/plugin_api/handle.h | 26 +++++++++++- src/plugin_api/message_api.h | 29 +++++++++++++ 5 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 src/plugin_api/command_api.h create mode 100644 src/plugin_api/message_api.h diff --git a/GNUmakefile b/GNUmakefile index f46d315..6d0ffb0 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -209,6 +209,8 @@ plugin_auth_sqlite_SOURCES := src/plugins/mod_auth_sqlite.c plugin_auth_sqlite_TARGET := mod_auth_sqlite.so plugin_auth_sqlite_LIBS := -lsqlite3 +plugin_chat_history_SOURCE := src/plugins/mod_chat_history.c +plugin_chat_history_TARGET := mod_chat_history.so # Source to objects libuhub_OBJECTS := $(libuhub_SOURCES:.c=.o) @@ -221,7 +223,7 @@ adcrush_OBJECTS := $(adcrush_SOURCES:.c=.o) admin_OBJECTS := $(admin_SOURCES:.c=.o) all_OBJECTS := $(libuhub_OBJECTS) $(uhub_OBJECTS) $(libutils_OBJECTS) $(adcrush_OBJECTS) $(autotest_OBJECTS) $(admin_OBJECTS) $(libadc_common_OBJECTS) $(libadc_client_OBJECTS) -all_plugins := $(plugin_example_TARGET) $(plugin_logging_TARGET) $(plugin_auth_TARGET) $(plugin_auth_sqlite_TARGET) +all_plugins := $(plugin_example_TARGET) $(plugin_logging_TARGET) $(plugin_auth_TARGET) $(plugin_auth_sqlite_TARGET) $(plugin_chat_history_TARGET) uhub_BINARY=uhub$(BIN_EXT) adcrush_BINARY=adcrush$(BIN_EXT) @@ -253,6 +255,9 @@ $(plugin_example_TARGET): $(plugin_example_SOURCES) $(plugin_logging_TARGET): $(plugin_logging_SOURCES) $(libutils_OBJECTS) $(libadc_common_OBJECTS) src/network/network.o $(MSG_CC) $(CC) -shared -fPIC -o $@ $^ $(CFLAGS) +$(plugin_chat_history_TARGET): $(plugin_chat_history_SOURCE) $(libutils_OBJECTS) + $(MSG_CC) $(CC) -shared -fPIC -o $@ $^ $(CFLAGS) + $(adcrush_BINARY): $(adcrush_OBJECTS) $(libuhub_OBJECTS) $(libutils_OBJECTS) $(libadc_common_OBJECTS) $(libadc_client_OBJECTS) $(MSG_LD) $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS) diff --git a/src/core/pluginloader.c b/src/core/pluginloader.c index fec9256..b7413f6 100644 --- a/src/core/pluginloader.c +++ b/src/core/pluginloader.c @@ -65,6 +65,14 @@ 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) { plugin_register_f register_f; @@ -86,6 +94,8 @@ 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); + if (register_f && unregister_f) { ret = register_f(handle, config); diff --git a/src/plugin_api/command_api.h b/src/plugin_api/command_api.h new file mode 100644 index 0000000..45fa6ee --- /dev/null +++ b/src/plugin_api/command_api.h @@ -0,0 +1,79 @@ +/* + * 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 . + * + */ + +#ifndef HAVE_UHUB_PLUGIN_API_H +#define HAVE_UHUB_PLUGIN_API_H + +/** + * This file describes the interface a plugin implementation may use from + * uhub. + */ + +#include "system.h" +#include "plugin_api/types.h" + +struct plugin_command +{ + const char* message; + char* prefix; + size_t prefix_len; + struct linked_list* args; +}; + +typedef int (*plugin_command_handler)(struct plugin_handle*, struct plugin_user* to, struct plugin_command*); + +struct plugin_command_handle +{ + void* internal_handle; /**<<< "Internal used by the hub only" */ + struct plugin_handle* handle; /**<<< "The plugin handle this is associated with" */ + const char* prefix; /**<<< "Command prefix, for instance 'help' would be the prefix for the !help command." */ + size_t length; /**<<< "Length of the prefix" */ + const char* args; /**<<< "Argument codes" */ + enum auth_credentials cred; /**<<< "Minimum access level for the command" */ + plugin_command_handler handler; /**<<< "Function pointer for the command" */ + const char* description; /**<<< "Description for the command" */ +}; + +#define PLUGIN_COMMAND_INITIALIZE(PTR, HANDLE, PREFIX, ARGS, CRED, CALLBACK, DESC) \ + do { \ + PTR->internal_handle = 0; \ + PTR->handle = HANDLE; \ + PTR->prefix = PREFIX; \ + PTR->length = strlen(PREFIX); \ + PTR->args = ARGS; \ + PTR->cred = CRED; \ + PTR->handler = CALLBACK; \ + PTR->description = DESC; \ + } while (0) + +extern int plugin_command_add(struct plugin_handle*, struct plugin_command_handle*); +extern int plugin_command_del(struct plugin_handle*, struct plugin_command_handle*); + +/** + * Send a message to a user. + * From the user's perspective the message will originate from the hub. + */ +extern int plugin_command_send_message(struct plugin_handle*, struct plugin_user* to, const char* message); + +/** + * Send a reply to a command. + */ +extern int plugin_command_send_reply(struct plugin_handle*, struct plugin_user* user, struct plugin_command* command, const char* message); + +#endif /* HAVE_UHUB_PLUGIN_API_H */ diff --git a/src/plugin_api/handle.h b/src/plugin_api/handle.h index 39d1e83..1e5b1b7 100644 --- a/src/plugin_api/handle.h +++ b/src/plugin_api/handle.h @@ -57,6 +57,9 @@ typedef plugin_st (*auth_register_user_t)(struct plugin_handle*, struct auth_inf typedef plugin_st (*auth_update_user_t)(struct plugin_handle*, struct auth_info* user); typedef plugin_st (*auth_delete_user_t)(struct plugin_handle*, struct auth_info* user); +/** + * These are callbacks used for the hub to invoke functions in plugins. + */ struct plugin_funcs { // Log events for connections @@ -89,6 +92,26 @@ struct plugin_funcs on_check_ip_late_t login_check_ip_late; }; +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_command_add)(struct plugin_handle*, struct plugin_command_handle*); +typedef int (*hfunc_command_del)(struct plugin_handle*, struct plugin_command_handle*); + +/** + * These are functions created and initialized by the hub and which can be used + * by plugins to access functionality internal to the hub. + */ +struct plugin_hub_funcs +{ + hfunc_send_message send_message; + hfunc_user_disconnect user_disconnect; + hfunc_command_add command_add; + hfunc_command_del command_del; +}; + + struct plugin_handle { struct uhub_plugin* handle; /* Must NOT be modified by the plugin */ @@ -99,7 +122,8 @@ struct plugin_handle const char* error_msg; /* Error message for registration error. */ size_t plugin_api_version; /* Plugin API version */ size_t plugin_funcs_size; /* Size of the plugin funcs */ - struct plugin_funcs funcs; + struct plugin_funcs funcs; /* Table of functions that can be implemented by a plugin */ + struct plugin_hub_funcs hub; /* Table of core hub functions that can be used by a plugin */ }; diff --git a/src/plugin_api/message_api.h b/src/plugin_api/message_api.h new file mode 100644 index 0000000..628805c --- /dev/null +++ b/src/plugin_api/message_api.h @@ -0,0 +1,29 @@ +/* + * 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 . + * + */ + +#ifndef HAVE_UHUB_PLUGIN_MESSAGE_API_H +#define HAVE_UHUB_PLUGIN_MESSAGE_API_H + +/** + * Send an informal message to a user. + * The user will see the message as if the hub sent it. + */ +extern int plugin_send_message(struct plugin_handle*, struct plugin_user* to, const char* message); + +#endif /* HAVE_UHUB_PLUGIN_API_H */