2011-12-09 09:29:50 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int plugin_command_dispatch(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
struct plugin_handle* plugin = (struct plugin_handle*) cmd->ptr;
|
2011-12-09 09:29:50 +00:00
|
|
|
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.
|
|
|
|
|
2011-12-09 15:36:14 +00:00
|
|
|
LOG_PLUGIN("plugin_command_dispatch: cmd=%s", cmd->prefix);
|
2011-12-09 09:29:50 +00:00
|
|
|
|
|
|
|
cmdh = (struct plugin_command_handle*) list_get_first(data->commands);
|
|
|
|
while (cmdh)
|
|
|
|
{
|
|
|
|
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));
|
2011-12-11 11:53:09 +00:00
|
|
|
LOG_PLUGIN("plugin_callback_data_create()");
|
2011-12-09 09:29:50 +00:00
|
|
|
data->commands = list_create();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void plugin_callback_data_destroy(struct plugin_callback_data* data)
|
|
|
|
{
|
2011-12-09 15:36:14 +00:00
|
|
|
LOG_PLUGIN("plugin_callback_data_destroy()");
|
2011-12-09 09:29:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-01-09 18:54:03 +00:00
|
|
|
static int cbfunc_send_status(struct plugin_handle* plugin, struct plugin_user* user, int code, const char* message)
|
|
|
|
{
|
|
|
|
// struct plugin_callback_data* data = get_callback_data(plugin);
|
|
|
|
char code_str[4];
|
|
|
|
char* buffer = adc_msg_escape(message);
|
|
|
|
struct adc_message* command = adc_msg_construct(ADC_CMD_ISTA, strlen(buffer) + 10);
|
|
|
|
snprintf(code_str, sizeof(code_str), "%03d", code);
|
|
|
|
adc_msg_add_argument(command, code_str);
|
|
|
|
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;
|
|
|
|
}
|
2011-12-09 09:29:50 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2012-01-19 01:58:20 +00:00
|
|
|
cmdh->internal_handle = command;
|
2011-12-09 09:29:50 +00:00
|
|
|
list_append(data->commands, cmdh);
|
|
|
|
command_add(plugin_get_hub(plugin)->commands, command, (void*) plugin);
|
2012-01-19 01:58:20 +00:00
|
|
|
printf("*** Add plugin command: %s (%p, %p)\n", command->prefix, command, cmdh);
|
2011-12-09 09:29:50 +00:00
|
|
|
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;
|
|
|
|
|
2012-01-19 01:58:20 +00:00
|
|
|
printf("*** Del plugin command: %s (%p, %p)\n", command->prefix, command, cmdh);
|
2011-12-09 09:29:50 +00:00
|
|
|
list_remove(data->commands, cmdh);
|
2012-01-19 01:58:20 +00:00
|
|
|
command_del(plugin_get_hub(plugin)->commands, command);
|
|
|
|
hub_free(command);
|
|
|
|
cmdh->internal_handle = NULL;
|
2011-12-09 09:29:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void plugin_register_callback_functions(struct plugin_handle* handle)
|
|
|
|
{
|
|
|
|
handle->hub.send_message = cbfunc_send_message;
|
2012-01-09 18:54:03 +00:00
|
|
|
handle->hub.send_status_message = cbfunc_send_status;
|
2011-12-09 09:29:50 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
}
|