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:
File diff suppressed because it is too large
Load Diff
@@ -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
|
||||
@@ -19,4 +19,83 @@
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
extern int command_dipatcher(struct hub_info* hub, struct hub_user* user, const char* message);
|
||||
struct command_base;
|
||||
struct command_handle;
|
||||
|
||||
struct hub_command
|
||||
{
|
||||
const char* message;
|
||||
char* prefix;
|
||||
size_t prefix_len;
|
||||
struct linked_list* args;
|
||||
};
|
||||
|
||||
typedef int (*command_handler)(struct command_base*, struct hub_user* user, struct command_handle*, struct hub_command*);
|
||||
|
||||
/**
|
||||
* Argument codes are used to automatically parse arguments
|
||||
* for a a hub command.
|
||||
*
|
||||
* n = nick name (must exist in hub session)
|
||||
* i = CID (must exist in hub)
|
||||
* a = (IP) address (must be a valid IPv4 or IPv6 address)
|
||||
* m = message (string)
|
||||
* p = password (string)
|
||||
* C = credentials (see auth_string_to_cred).
|
||||
* c = command (name of command)
|
||||
* N = number (integer)
|
||||
*
|
||||
* Prefix an argument with ? to make it optional.
|
||||
* NOTE; if an argument is optional then all following arguments must also be optional.
|
||||
*
|
||||
* Example:
|
||||
* "nia" means "nick cid ip"
|
||||
* "n?p" means "nick [password]" where password is optional.
|
||||
*
|
||||
*/
|
||||
struct command_handle
|
||||
{
|
||||
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 (see above)" */
|
||||
enum auth_credentials cred; /**<<< "Minimum access level for the command" */
|
||||
command_handler handler; /**<<< "Function pointer for the command" */
|
||||
const char* description; /**<<< "Description for the command" */
|
||||
const char* origin; /**<<< "Name of module where the command is implemented." */
|
||||
void* ptr;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns NULL on error, or handle
|
||||
*/
|
||||
extern struct command_base* command_initialize(struct hub_info* hub);
|
||||
extern void command_shutdown(struct command_base* cbase);
|
||||
|
||||
/**
|
||||
* Add a new command to the command base.
|
||||
* Returns 1 on success, or 0 on error.
|
||||
*/
|
||||
extern int command_add(struct command_base*, struct command_handle*, void* ptr);
|
||||
|
||||
/**
|
||||
* Remove a command from the command base.
|
||||
* Returns 1 on success, or 0 on error.
|
||||
*/
|
||||
extern int command_del(struct command_base*, struct command_handle*);
|
||||
|
||||
/**
|
||||
* Returns 1 if a command is available to a user (user has access to run it.)
|
||||
*/
|
||||
extern int command_is_available(struct command_handle*, struct hub_user* user);
|
||||
|
||||
/**
|
||||
* Dispatch a message and forward it as a command.
|
||||
* Returns 1 if the message should be forwarded as a chat message, or 0 if
|
||||
* it is supposed to be handled internally in the dispatcher.
|
||||
*
|
||||
* This will break the message down into a struct hub_command and invoke the command handler
|
||||
* for that command if the sufficient access credentials are met.
|
||||
*/
|
||||
extern int command_invoke(struct command_base*, struct hub_user* user, const char* message);
|
||||
|
||||
0
src/core/commands_builtin.c
Normal file
0
src/core/commands_builtin.c
Normal file
3
src/core/commands_builtin.h
Normal file
3
src/core/commands_builtin.h
Normal file
@@ -0,0 +1,3 @@
|
||||
|
||||
void commands_builtin_add(struct command_base*);
|
||||
void commands_builtin_remove(struct command_base*);
|
||||
@@ -289,7 +289,7 @@ int hub_handle_chat_message(struct hub_info* hub, struct hub_user* u, struct adc
|
||||
}
|
||||
else
|
||||
{
|
||||
relay = command_dipatcher(hub, u, message);
|
||||
relay = command_invoke(hub->commands, u, message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -813,6 +813,9 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
hub->status = hub_status_running;
|
||||
|
||||
g_hub = hub;
|
||||
|
||||
// Start the hub command sub-system
|
||||
hub->commands = command_initialize(hub);
|
||||
return hub;
|
||||
}
|
||||
|
||||
@@ -836,6 +839,7 @@ void hub_shutdown_service(struct hub_info* hub)
|
||||
list_destroy(hub->chat_history);
|
||||
list_clear(hub->logout_info, &hub_free);
|
||||
list_destroy(hub->logout_info);
|
||||
command_shutdown(hub->commands);
|
||||
hub_free(hub);
|
||||
hub = 0;
|
||||
g_hub = 0;
|
||||
@@ -851,7 +855,7 @@ int hub_plugins_load(struct hub_info* hub)
|
||||
if (!hub->plugins)
|
||||
return -1;
|
||||
|
||||
if (plugin_initialize(hub->config, hub->plugins) < 0)
|
||||
if (plugin_initialize(hub->config, hub) < 0)
|
||||
{
|
||||
hub_free(hub->plugins);
|
||||
hub->plugins = 0;
|
||||
|
||||
@@ -114,6 +114,8 @@ struct hub_info
|
||||
struct linked_list* chat_history; /* Chat history */
|
||||
struct linked_list* logout_info; /* Log of people logging out. */
|
||||
|
||||
struct command_base* commands; /* Hub command handler */
|
||||
|
||||
#ifdef PLUGIN_SUPPORT
|
||||
struct uhub_plugins* plugins;
|
||||
#endif
|
||||
|
||||
@@ -378,7 +378,7 @@ static int check_user_agent(struct hub_info* hub, struct hub_user* user, struct
|
||||
ua = adc_msg_unescape(ua_encoded);
|
||||
if (ua)
|
||||
{
|
||||
memcpy(user->user_agent, ua, MIN(strlen(ua), MAX_UA_LEN));
|
||||
memcpy(user->id.user_agent, ua, MIN(strlen(ua), MAX_UA_LEN));
|
||||
hub_free(ua);
|
||||
}
|
||||
}
|
||||
|
||||
147
src/core/plugincallback.c
Normal file
147
src/core/plugincallback.c
Normal file
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "uhub.h"
|
||||
#include "plugin_api/command_api.h"
|
||||
|
||||
struct plugin_callback_data
|
||||
{
|
||||
struct linked_list* commands;
|
||||
};
|
||||
|
||||
static struct plugin_callback_data* get_callback_data(struct plugin_handle* plugin)
|
||||
{
|
||||
struct plugin_callback_data* data;
|
||||
uhub_assert(plugin && plugin->handle && plugin->handle->internals);
|
||||
data = (struct plugin_callback_data*) plugin->handle->internals;
|
||||
return data;
|
||||
}
|
||||
|
||||
static int plugin_command_dispatch(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
||||
{
|
||||
struct plugin_handle* plugin = (struct plugin_handle*) handle->ptr;
|
||||
struct plugin_callback_data* data = get_callback_data(plugin);
|
||||
struct plugin_command_handle* cmdh;
|
||||
struct plugin_user* puser = (struct plugin_user*) user; // FIXME: Use a proper conversion function instead.
|
||||
struct plugin_command* pcommand = (struct plugin_command*) cmd; // FIXME: Use a proper conversion function instead.
|
||||
|
||||
LOG_INFO("plugin_command_dispatch: cmd=%s", cmd->prefix);
|
||||
|
||||
cmdh = (struct plugin_command_handle*) list_get_first(data->commands);
|
||||
while (cmdh)
|
||||
{
|
||||
if (cmdh->length != cmd->prefix_len)
|
||||
continue;
|
||||
|
||||
if (strcmp(cmdh->prefix, cmd->prefix) == 0)
|
||||
return cmdh->handler(plugin, puser, pcommand);
|
||||
|
||||
cmdh = (struct plugin_command_handle*) list_get_next(data->commands);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct plugin_callback_data* plugin_callback_data_create()
|
||||
{
|
||||
struct plugin_callback_data* data = (struct plugin_callback_data*) hub_malloc_zero(sizeof(struct plugin_callback_data));
|
||||
data->commands = list_create();
|
||||
return data;
|
||||
}
|
||||
|
||||
void plugin_callback_data_destroy(struct plugin_callback_data* data)
|
||||
{
|
||||
if (data->commands)
|
||||
{
|
||||
uhub_assert(list_size(data->commands) == 0);
|
||||
list_destroy(data->commands);
|
||||
}
|
||||
|
||||
hub_free(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);
|
||||
struct command_handle* command = (struct command_handle*) hub_malloc_zero(sizeof(struct command_handle));
|
||||
|
||||
command->prefix = cmdh->prefix;
|
||||
command->length = cmdh->length;
|
||||
command->args = cmdh->args;
|
||||
command->cred = cmdh->cred;
|
||||
command->description = cmdh->description;
|
||||
command->origin = cmdh->origin;
|
||||
command->handler = plugin_command_dispatch;
|
||||
|
||||
cmdh->internal_handle = data;
|
||||
list_append(data->commands, cmdh);
|
||||
command_add(plugin_get_hub(plugin)->commands, command, (void*) 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);
|
||||
struct command_handle* command = (struct command_handle*) cmdh->internal_handle;
|
||||
|
||||
list_remove(data->commands, cmdh);
|
||||
cmdh->internal_handle = 0;
|
||||
|
||||
command_del(plugin_get_hub(plugin)->commands, (void*) command);
|
||||
|
||||
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)
|
||||
{
|
||||
}
|
||||
32
src/core/plugincallback.h
Normal file
32
src/core/plugincallback.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef HAVE_UHUB_PLUGIN_CALLBACK_H
|
||||
#define HAVE_UHUB_PLUGIN_CALLBACK_H
|
||||
|
||||
struct plugin_handle;
|
||||
struct uhub_plugin;
|
||||
|
||||
extern struct plugin_callback_data* plugin_callback_data_create();
|
||||
extern void plugin_callback_data_destroy(struct plugin_callback_data* data);
|
||||
|
||||
extern void plugin_register_callback_functions(struct plugin_handle* plugin);
|
||||
extern void plugin_unregister_callback_functions(struct plugin_handle* plugin);
|
||||
|
||||
#endif /* HAVE_UHUB_PLUGIN_CALLBACK_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
|
||||
@@ -74,14 +74,10 @@
|
||||
#define PLUGIN_INVOKE_3(HUB, FUNCNAME, ARG1, ARG2, ARG3) INVOKE(HUB, FUNCNAME, { plugin->funcs.FUNCNAME(plugin, ARG1, ARG2, ARG3); })
|
||||
|
||||
|
||||
static void convert_user_type(struct plugin_user* puser, struct hub_user* user)
|
||||
static struct plugin_user* convert_user_type(struct hub_user* user)
|
||||
{
|
||||
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;
|
||||
struct plugin_user* puser = (struct plugin_user*) user;
|
||||
return puser;
|
||||
}
|
||||
|
||||
plugin_st plugin_check_ip_early(struct hub_info* hub, struct ip_addr_encap* addr)
|
||||
@@ -106,85 +102,71 @@ void plugin_log_connection_denied(struct hub_info* hub, struct ip_addr_encap* ip
|
||||
|
||||
void plugin_log_user_login_success(struct hub_info* hub, struct hub_user* who)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, who);
|
||||
PLUGIN_INVOKE_1(hub, on_user_login, &user);
|
||||
struct plugin_user* user = convert_user_type(who);
|
||||
PLUGIN_INVOKE_1(hub, on_user_login, user);
|
||||
}
|
||||
|
||||
void plugin_log_user_login_error(struct hub_info* hub, struct hub_user* who, const char* reason)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, who);
|
||||
PLUGIN_INVOKE_2(hub, on_user_login_error, &user, reason);
|
||||
struct plugin_user* user = convert_user_type(who);
|
||||
PLUGIN_INVOKE_2(hub, on_user_login_error, user, reason);
|
||||
}
|
||||
|
||||
void plugin_log_user_logout(struct hub_info* hub, struct hub_user* who, const char* reason)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, who);
|
||||
PLUGIN_INVOKE_2(hub, on_user_logout, &user, reason);
|
||||
struct plugin_user* user = convert_user_type(who);
|
||||
PLUGIN_INVOKE_2(hub, on_user_logout, user, reason);
|
||||
}
|
||||
|
||||
void plugin_log_user_nick_change(struct hub_info* hub, struct hub_user* who, const char* new_nick)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, who);
|
||||
PLUGIN_INVOKE_2(hub, on_user_nick_change, &user, new_nick);
|
||||
struct plugin_user* user = convert_user_type(who);
|
||||
PLUGIN_INVOKE_2(hub, on_user_nick_change, user, new_nick);
|
||||
}
|
||||
|
||||
void plugin_log_user_update_error(struct hub_info* hub, struct hub_user* who, const char* reason)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, who);
|
||||
PLUGIN_INVOKE_2(hub, on_user_update_error, &user, reason);
|
||||
struct plugin_user* user = convert_user_type(who);
|
||||
PLUGIN_INVOKE_2(hub, on_user_update_error, user, reason);
|
||||
}
|
||||
|
||||
void plugin_log_chat_message(struct hub_info* hub, struct hub_user* who, const char* message, int flags)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, who);
|
||||
PLUGIN_INVOKE_3(hub, on_user_chat_message, &user, message, flags);
|
||||
struct plugin_user* user = convert_user_type(who);
|
||||
PLUGIN_INVOKE_3(hub, on_user_chat_message, user, message, flags);
|
||||
}
|
||||
|
||||
plugin_st plugin_handle_chat_message(struct hub_info* hub, struct hub_user* from, const char* message, int flags)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, from);
|
||||
PLUGIN_INVOKE_STATUS_2(hub, on_chat_msg, &user, message);
|
||||
struct plugin_user* user = convert_user_type(from);
|
||||
PLUGIN_INVOKE_STATUS_2(hub, on_chat_msg, user, message);
|
||||
}
|
||||
|
||||
plugin_st plugin_handle_private_message(struct hub_info* hub, struct hub_user* from, struct hub_user* to, const char* message, int flags)
|
||||
{
|
||||
struct plugin_user user1;
|
||||
struct plugin_user user2;
|
||||
convert_user_type(&user1, from);
|
||||
convert_user_type(&user2, to);
|
||||
PLUGIN_INVOKE_STATUS_3(hub, on_private_msg, &user1, &user2, message);
|
||||
struct plugin_user* user1 = convert_user_type(from);
|
||||
struct plugin_user* user2 = convert_user_type(to);
|
||||
PLUGIN_INVOKE_STATUS_3(hub, on_private_msg, user1, user2, message);
|
||||
}
|
||||
|
||||
plugin_st plugin_handle_search(struct hub_info* hub, struct hub_user* from, const char* data)
|
||||
{
|
||||
struct plugin_user user;
|
||||
convert_user_type(&user, from);
|
||||
PLUGIN_INVOKE_STATUS_2(hub, on_search, &user, data);
|
||||
struct plugin_user* user = convert_user_type(from);
|
||||
PLUGIN_INVOKE_STATUS_2(hub, on_search, user, data);
|
||||
}
|
||||
|
||||
plugin_st plugin_handle_connect(struct hub_info* hub, struct hub_user* from, struct hub_user* to)
|
||||
{
|
||||
struct plugin_user user1;
|
||||
struct plugin_user user2;
|
||||
convert_user_type(&user1, from);
|
||||
convert_user_type(&user2, to);
|
||||
PLUGIN_INVOKE_STATUS_2(hub, on_p2p_connect, &user1, &user2);
|
||||
struct plugin_user* user1 = convert_user_type(from);
|
||||
struct plugin_user* user2 = convert_user_type(to);
|
||||
PLUGIN_INVOKE_STATUS_2(hub, on_p2p_connect, user1, user2);
|
||||
}
|
||||
|
||||
plugin_st plugin_handle_revconnect(struct hub_info* hub, struct hub_user* from, struct hub_user* to)
|
||||
{
|
||||
struct plugin_user user1;
|
||||
struct plugin_user user2;
|
||||
convert_user_type(&user1, from);
|
||||
convert_user_type(&user2, to);
|
||||
PLUGIN_INVOKE_STATUS_2(hub, on_p2p_revconnect, &user1, &user2);
|
||||
struct plugin_user* user1 = convert_user_type(from);
|
||||
struct plugin_user* user2 = convert_user_type(to);
|
||||
PLUGIN_INVOKE_STATUS_2(hub, on_p2p_revconnect, user1, user2);
|
||||
}
|
||||
|
||||
plugin_st plugin_auth_get_user(struct hub_info* hub, const char* nickname, struct auth_info* info)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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,6 +23,7 @@
|
||||
#include "plugin_api/handle.h"
|
||||
|
||||
struct hub_config;
|
||||
struct hub_info;
|
||||
struct linked_list;
|
||||
struct plugin_handle;
|
||||
|
||||
@@ -31,6 +32,7 @@ struct uhub_plugin
|
||||
void* handle;
|
||||
plugin_unregister_f unregister;
|
||||
char* filename;
|
||||
void* internals; // Hub internal stuff
|
||||
};
|
||||
|
||||
struct uhub_plugins
|
||||
@@ -39,11 +41,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)
|
||||
@@ -51,5 +53,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 */
|
||||
|
||||
|
||||
@@ -82,8 +82,9 @@ extern const char* user_get_quit_reason_string(enum user_quit_reason);
|
||||
struct hub_user_info
|
||||
{
|
||||
sid_t sid; /** session ID */
|
||||
char cid[MAX_CID_LEN+1]; /** global client ID */
|
||||
char nick[MAX_NICK_LEN+1]; /** User's nick name */
|
||||
char cid[MAX_CID_LEN+1]; /** global client ID */
|
||||
char user_agent[MAX_UA_LEN+1];/** User agent string */
|
||||
struct ip_addr_encap addr; /** User's IP address */
|
||||
};
|
||||
|
||||
@@ -105,11 +106,10 @@ struct hub_user_limits
|
||||
|
||||
struct hub_user
|
||||
{
|
||||
enum user_state state; /** see enum user_state */
|
||||
enum auth_credentials credentials; /** see enum user_credentials */
|
||||
struct hub_user_info id; /** Contains nick name and CID */
|
||||
enum auth_credentials credentials; /** see enum user_credentials */
|
||||
enum user_state state; /** see enum user_state */
|
||||
uint32_t flags; /** see enum user_features */
|
||||
char user_agent[MAX_UA_LEN+1];/** User agent string */
|
||||
struct linked_list* feature_cast; /** Features supported by feature cast */
|
||||
struct adc_message* info; /** ADC 'INF' message (broadcasted to everyone joining the hub) */
|
||||
struct hub_info* hub; /** The hub instance this user belong to */
|
||||
|
||||
Reference in New Issue
Block a user