Added function pointers for plugins to access hub internals.

This commit is contained in:
Jan Vidar Krey 2010-12-20 11:32:35 +01:00
parent 963564dc31
commit 5ca27a1a6d
5 changed files with 149 additions and 2 deletions

View File

@ -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_TARGET := mod_auth_sqlite.so
plugin_auth_sqlite_LIBS := -lsqlite3 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 # Source to objects
libuhub_OBJECTS := $(libuhub_SOURCES:.c=.o) libuhub_OBJECTS := $(libuhub_SOURCES:.c=.o)
@ -221,7 +223,7 @@ adcrush_OBJECTS := $(adcrush_SOURCES:.c=.o)
admin_OBJECTS := $(admin_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_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) uhub_BINARY=uhub$(BIN_EXT)
adcrush_BINARY=adcrush$(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 $(plugin_logging_TARGET): $(plugin_logging_SOURCES) $(libutils_OBJECTS) $(libadc_common_OBJECTS) src/network/network.o
$(MSG_CC) $(CC) -shared -fPIC -o $@ $^ $(CFLAGS) $(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) $(adcrush_BINARY): $(adcrush_OBJECTS) $(libuhub_OBJECTS) $(libutils_OBJECTS) $(libadc_common_OBJECTS) $(libadc_client_OBJECTS)
$(MSG_LD) $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS) $(MSG_LD) $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)

View File

@ -65,6 +65,14 @@ void* plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol)
#endif #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)
{ {
plugin_register_f register_f; 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"); register_f = plugin_lookup_symbol(plugin, "plugin_register");
unregister_f = plugin_lookup_symbol(plugin, "plugin_unregister"); unregister_f = plugin_lookup_symbol(plugin, "plugin_unregister");
plugin_register_hub_functions(handle);
if (register_f && unregister_f) if (register_f && unregister_f)
{ {
ret = register_f(handle, config); ret = register_f(handle, config);

View File

@ -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 <http://www.gnu.org/licenses/>.
*
*/
#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 */

View File

@ -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_update_user_t)(struct plugin_handle*, struct auth_info* user);
typedef plugin_st (*auth_delete_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 struct plugin_funcs
{ {
// Log events for connections // Log events for connections
@ -89,6 +92,26 @@ struct plugin_funcs
on_check_ip_late_t login_check_ip_late; 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 plugin_handle
{ {
struct uhub_plugin* handle; /* Must NOT be modified by the plugin */ 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. */ const char* error_msg; /* Error message for registration error. */
size_t plugin_api_version; /* Plugin API version */ size_t plugin_api_version; /* Plugin API version */
size_t plugin_funcs_size; /* Size of the plugin funcs */ 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 */
}; };

View File

@ -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 <http://www.gnu.org/licenses/>.
*
*/
#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 */