2009-02-19 16:14:09 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
2012-01-10 01:21:27 +00:00
|
|
|
* Copyright (C) 2007-2012, Jan Vidar Krey
|
2009-02-19 16:14:09 +00:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2009-06-29 21:22:13 +00:00
|
|
|
#ifdef DEBUG
|
2012-01-19 01:58:20 +00:00
|
|
|
// #define DEBUG_UNLOAD_PLUGINS
|
2009-06-29 21:22:13 +00:00
|
|
|
#endif
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
#define MAX_HELP_MSG 16384
|
|
|
|
#define MAX_HELP_LINE 512
|
2009-06-30 09:48:58 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int send_command_access_denied(struct command_base* cbase, struct hub_user* user, const char* prefix);
|
|
|
|
static int send_command_not_found(struct command_base* cbase, struct hub_user* user, const char* prefix);
|
|
|
|
static int send_command_syntax_error(struct command_base* cbase, struct hub_user* user);
|
|
|
|
static int send_command_missing_arguments(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
|
|
|
static void null_free(void* ptr) { }
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
struct command_base
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
struct hub_info* hub;
|
|
|
|
struct linked_list* handlers;
|
|
|
|
size_t prefix_length_max;
|
2011-12-18 23:34:20 +00:00
|
|
|
struct ip_range range; // cached for parsing. Only one can be used per parameter.
|
2009-06-25 23:15:06 +00:00
|
|
|
};
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
struct command_base* command_initialize(struct hub_info* hub)
|
|
|
|
{
|
|
|
|
struct command_base* cbase = (struct command_base*) hub_malloc(sizeof(struct command_base));
|
|
|
|
uhub_assert(cbase != NULL);
|
2011-12-18 23:34:20 +00:00
|
|
|
// uhub_assert(hub != NULL);
|
2011-12-09 09:29:50 +00:00
|
|
|
|
|
|
|
cbase->hub = hub;
|
|
|
|
cbase->handlers = (struct linked_list*) list_create();
|
|
|
|
cbase->prefix_length_max = 0;
|
2009-03-20 17:37:38 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
uhub_assert(cbase->handlers != NULL);
|
|
|
|
|
|
|
|
commands_builtin_add(cbase);
|
|
|
|
|
|
|
|
return cbase;
|
|
|
|
}
|
|
|
|
|
|
|
|
void command_shutdown(struct command_base* cbase)
|
2009-03-20 17:37:38 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
commands_builtin_remove(cbase);
|
|
|
|
assert(list_size(cbase->handlers) == 0);
|
2011-12-09 15:35:42 +00:00
|
|
|
list_destroy(cbase->handlers);
|
2011-12-09 09:29:50 +00:00
|
|
|
hub_free(cbase);
|
|
|
|
}
|
2009-03-20 17:37:38 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
int command_add(struct command_base* cbase, struct command_handle* cmd, void* ptr)
|
|
|
|
{
|
|
|
|
uhub_assert(cbase != NULL);
|
|
|
|
uhub_assert(cmd != NULL);
|
|
|
|
uhub_assert(cmd->length == strlen(cmd->prefix));
|
|
|
|
uhub_assert(cmd->handler != NULL);
|
|
|
|
uhub_assert(cmd->description && *cmd->description);
|
|
|
|
list_append(cbase->handlers, cmd);
|
|
|
|
cbase->prefix_length_max = MAX(cmd->length, cbase->prefix_length_max);
|
|
|
|
cmd->ptr = ptr;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int command_del(struct command_base* cbase, struct command_handle* cmd)
|
|
|
|
{
|
|
|
|
uhub_assert(cbase != NULL);
|
|
|
|
uhub_assert(cmd != NULL);
|
|
|
|
list_remove(cbase->handlers, cmd);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-03-20 17:37:38 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int command_is_available(struct command_handle* handle, const struct hub_user* user)
|
|
|
|
{
|
|
|
|
uhub_assert(handle != NULL);
|
|
|
|
uhub_assert(user != NULL);
|
|
|
|
return handle->cred <= user->credentials;
|
|
|
|
}
|
|
|
|
|
2012-04-18 21:03:05 +00:00
|
|
|
void hub_command_args_free(struct hub_command* cmd)
|
|
|
|
{
|
|
|
|
struct hub_command_arg_data* data = NULL;
|
|
|
|
|
|
|
|
if (!cmd->args)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (data = (struct hub_command_arg_data*) list_get_first(cmd->args); data; data = (struct hub_command_arg_data*) list_get_next(cmd->args))
|
|
|
|
{
|
|
|
|
switch (data->type)
|
|
|
|
{
|
|
|
|
case type_string:
|
|
|
|
hub_free(data->data.string);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list_clear(cmd->args, hub_free);
|
|
|
|
list_destroy(cmd->args);
|
|
|
|
cmd->args = NULL;
|
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
void command_free(struct hub_command* cmd)
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
|
|
|
if (!cmd) return;
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
hub_free(cmd->prefix);
|
2012-04-18 21:03:05 +00:00
|
|
|
hub_command_args_free(cmd);
|
2009-06-25 23:15:06 +00:00
|
|
|
hub_free(cmd);
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static struct command_handle* command_handler_lookup(struct command_base* cbase, const char* prefix)
|
|
|
|
{
|
|
|
|
struct command_handle* handler = NULL;
|
|
|
|
size_t prefix_len = strlen(prefix);
|
|
|
|
|
|
|
|
for (handler = (struct command_handle*) list_get_first(cbase->handlers); handler; handler = (struct command_handle*) list_get_next(cbase->handlers))
|
|
|
|
{
|
|
|
|
if (prefix_len != handler->length)
|
|
|
|
continue;
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
if (!memcmp(prefix, handler->prefix, handler->length))
|
2011-12-09 09:29:50 +00:00
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-18 21:03:05 +00:00
|
|
|
|
|
|
|
static enum command_parse_status command_extract_arguments(struct command_base* cbase, const struct hub_user* user, struct command_handle* command, struct linked_list* tokens, struct linked_list* args)
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
|
|
|
int arg = 0;
|
|
|
|
int opt = 0;
|
2012-04-18 22:21:49 +00:00
|
|
|
int greedy = 0;
|
2011-12-18 23:34:20 +00:00
|
|
|
char arg_code;
|
2011-12-09 09:29:50 +00:00
|
|
|
char* token = NULL;
|
2012-04-18 22:21:49 +00:00
|
|
|
char* tmp = NULL;
|
|
|
|
size_t size = 0;
|
2012-04-18 21:03:05 +00:00
|
|
|
struct hub_command_arg_data* data = NULL;
|
2011-12-18 23:34:20 +00:00
|
|
|
enum command_parse_status status = cmd_status_ok;
|
2011-12-09 09:29:50 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
// Ignore the first token since it is the prefix.
|
|
|
|
token = list_get_first(tokens);
|
|
|
|
list_remove(tokens, token);
|
|
|
|
hub_free(token);
|
2011-12-09 09:29:50 +00:00
|
|
|
|
2012-04-18 21:03:05 +00:00
|
|
|
while (status == cmd_status_ok && (arg_code = command->args[arg++]))
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
2012-04-18 22:21:49 +00:00
|
|
|
if (greedy)
|
|
|
|
{
|
|
|
|
size = 1;
|
|
|
|
for (tmp = (char*) list_get_first(tokens); tmp; tmp = (char*) list_get_next(tokens))
|
|
|
|
size += (strlen(tmp) + 1);
|
|
|
|
token = hub_malloc_zero(size);
|
|
|
|
|
|
|
|
while ((tmp = list_get_first(tokens)))
|
|
|
|
{
|
|
|
|
if (*token)
|
|
|
|
strcat(token, " ");
|
|
|
|
strcat(token, tmp);
|
|
|
|
list_remove(tokens, tmp);
|
|
|
|
hub_free(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
token = list_get_first(tokens);
|
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
if (!token || !*token)
|
|
|
|
{
|
2012-04-19 01:06:52 +00:00
|
|
|
if (arg_code == '?' || opt == 1)
|
|
|
|
status = cmd_status_ok;
|
|
|
|
else
|
|
|
|
status = cmd_status_missing_args;
|
2011-12-18 23:34:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (arg_code)
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
|
|
|
case '?':
|
|
|
|
opt = 1;
|
|
|
|
continue;
|
|
|
|
|
2012-04-18 22:21:49 +00:00
|
|
|
case '+':
|
|
|
|
greedy = 1;
|
|
|
|
continue;
|
|
|
|
|
2012-04-18 21:03:05 +00:00
|
|
|
case 'u':
|
|
|
|
data = hub_malloc(sizeof(*data));
|
|
|
|
data->type = type_user;
|
|
|
|
data->data.user = uman_get_user_by_nick(cbase->hub, token);
|
|
|
|
if (!data->data.user)
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
2012-04-18 21:03:05 +00:00
|
|
|
hub_free(data);
|
|
|
|
data = NULL;
|
2011-12-18 23:34:20 +00:00
|
|
|
status = cmd_status_arg_nick;
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
2012-04-18 21:03:05 +00:00
|
|
|
data = hub_malloc(sizeof(*data));
|
|
|
|
data->type = type_user;
|
|
|
|
data->data.user = uman_get_user_by_cid(cbase->hub, token);
|
|
|
|
if (!data->data.user)
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
2012-04-18 21:03:05 +00:00
|
|
|
hub_free(data);
|
|
|
|
data = NULL;
|
2011-12-18 23:34:20 +00:00
|
|
|
status = cmd_status_arg_cid;
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
2012-04-18 21:03:05 +00:00
|
|
|
data = hub_malloc(sizeof(*data));
|
|
|
|
data->type = type_address;
|
|
|
|
if (ip_convert_to_binary(token, data->data.address) == -1)
|
|
|
|
{
|
|
|
|
hub_free(data);
|
|
|
|
data = NULL;
|
|
|
|
status = cmd_status_arg_address;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
data = hub_malloc(sizeof(*data));
|
|
|
|
data->type = type_range;
|
|
|
|
if (!ip_convert_address_to_range(token, data->data.range))
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
2012-04-18 21:03:05 +00:00
|
|
|
hub_free(data);
|
|
|
|
data = NULL;
|
2011-12-18 23:34:20 +00:00
|
|
|
status = cmd_status_arg_address;
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-04-18 21:03:05 +00:00
|
|
|
case 'n':
|
2011-12-09 09:29:50 +00:00
|
|
|
case 'm':
|
|
|
|
case 'p':
|
2012-04-18 21:03:05 +00:00
|
|
|
data = hub_malloc(sizeof(*data));
|
|
|
|
data->type = type_string;
|
|
|
|
data->data.string = strdup(token);
|
2011-12-09 09:29:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
2012-04-18 21:03:05 +00:00
|
|
|
data = hub_malloc(sizeof(*data));
|
|
|
|
data->type = type_command;
|
|
|
|
data->data.command = command_handler_lookup(cbase, token);
|
|
|
|
if (!data->data.command)
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
2012-04-18 21:03:05 +00:00
|
|
|
hub_free(data);
|
|
|
|
data = NULL;
|
2011-12-18 23:34:20 +00:00
|
|
|
status = cmd_status_arg_command;
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
2012-04-18 21:03:05 +00:00
|
|
|
data = hub_malloc(sizeof(*data));
|
|
|
|
data->type = type_credentials;
|
|
|
|
if (!auth_string_to_cred(token, &data->data.credentials))
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
2012-04-18 21:03:05 +00:00
|
|
|
hub_free(data);
|
|
|
|
data = NULL;
|
2011-12-18 23:34:20 +00:00
|
|
|
status = cmd_status_arg_cred;
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'N':
|
2012-04-18 21:03:05 +00:00
|
|
|
data = hub_malloc(sizeof(*data));
|
|
|
|
data->type = type_integer;
|
|
|
|
if (!is_number(token, &data->data.integer))
|
2011-12-18 23:34:20 +00:00
|
|
|
{
|
2012-04-18 21:03:05 +00:00
|
|
|
hub_free(data);
|
|
|
|
data = NULL;
|
2012-04-19 01:06:52 +00:00
|
|
|
status = cmd_status_arg_number;
|
2011-12-18 23:34:20 +00:00
|
|
|
}
|
2011-12-09 09:29:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\0':
|
|
|
|
if (!opt)
|
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
status = cmd_status_missing_args;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = cmd_status_ok;
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-18 21:03:05 +00:00
|
|
|
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
list_append(args, data);
|
|
|
|
data = NULL;
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
list_remove(tokens, token);
|
2011-12-18 23:34:20 +00:00
|
|
|
hub_free(token);
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
|
|
|
|
2012-04-18 21:03:05 +00:00
|
|
|
hub_free(data);
|
2011-12-18 23:34:20 +00:00
|
|
|
return status;
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static struct command_handle* command_get_handler(struct command_base* cbase, const char* prefix, const struct hub_user* user, struct hub_command* cmd)
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
struct command_handle* handler = NULL;
|
2011-12-21 00:42:21 +00:00
|
|
|
uhub_assert(cmd != NULL);
|
2010-01-28 00:06:41 +00:00
|
|
|
|
2009-11-18 16:41:28 +00:00
|
|
|
if (prefix && prefix[0] && prefix[1])
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
handler = command_handler_lookup(cbase, prefix + 1);
|
|
|
|
if (handler)
|
|
|
|
{
|
|
|
|
cmd->ptr = handler->ptr;
|
|
|
|
cmd->handler = handler->handler;
|
|
|
|
cmd->status = command_is_available(handler, user) ? cmd_status_ok : cmd_status_access_error;
|
|
|
|
}
|
|
|
|
else
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
cmd->status = cmd_status_not_found;
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
2009-06-25 23:15:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
cmd->status = cmd_status_syntax_error;
|
2009-06-25 23:15:06 +00:00
|
|
|
}
|
2011-12-18 23:34:20 +00:00
|
|
|
return handler;
|
|
|
|
}
|
2009-06-25 23:15:06 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
/**
|
|
|
|
* Parse a command and break it down into a struct hub_command.
|
|
|
|
*/
|
|
|
|
struct hub_command* command_parse(struct command_base* cbase, const struct hub_user* user, const char* message)
|
|
|
|
{
|
|
|
|
struct linked_list* tokens = list_create();
|
|
|
|
struct hub_command* cmd = NULL;
|
|
|
|
struct command_handle* handle = NULL;
|
2009-03-18 23:55:11 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
cmd = hub_malloc_zero(sizeof(struct hub_command));
|
|
|
|
cmd->status = cmd_status_ok;
|
|
|
|
cmd->message = message;
|
|
|
|
cmd->prefix = NULL;
|
2012-04-18 21:03:05 +00:00
|
|
|
cmd->args = list_create();
|
2011-12-18 23:34:20 +00:00
|
|
|
cmd->user = user;
|
2009-05-15 14:52:04 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
if (split_string(message, " ", tokens, 0) <= 0)
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
cmd->status = cmd_status_syntax_error;
|
2011-12-09 15:54:48 +00:00
|
|
|
goto command_parse_cleanup;
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
2009-06-25 23:15:06 +00:00
|
|
|
|
2011-12-22 00:49:21 +00:00
|
|
|
// Setup hub command.
|
|
|
|
cmd->prefix = strdup(((char*) list_get_first(tokens)) + 1);
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
// Find a matching command handler
|
|
|
|
handle = command_get_handler(cbase, list_get_first(tokens), user, cmd);
|
|
|
|
if (cmd->status != cmd_status_ok)
|
2011-12-09 15:54:48 +00:00
|
|
|
goto command_parse_cleanup;
|
2011-12-18 23:34:20 +00:00
|
|
|
|
|
|
|
// Parse arguments
|
2012-04-18 21:03:05 +00:00
|
|
|
cmd->status = command_extract_arguments(cbase, user, handle, tokens, cmd->args);
|
2011-12-18 23:34:20 +00:00
|
|
|
goto command_parse_cleanup;
|
2011-12-09 15:54:48 +00:00
|
|
|
|
|
|
|
command_parse_cleanup:
|
|
|
|
list_clear(tokens, &hub_free);
|
|
|
|
list_destroy(tokens);
|
2011-12-18 23:34:20 +00:00
|
|
|
return cmd;
|
2009-06-25 23:15:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-21 12:53:12 +00:00
|
|
|
void command_get_syntax(struct command_handle* handler, struct cbuffer* buf)
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
2012-04-21 04:08:36 +00:00
|
|
|
size_t n, arg_count;
|
2010-11-12 17:14:15 +00:00
|
|
|
int opt = 0;
|
2012-04-21 04:08:36 +00:00
|
|
|
char arg_code, last_arg = -1;
|
|
|
|
|
|
|
|
cbuf_append_format(buf, "!%s", handler->prefix);
|
2009-06-25 23:15:06 +00:00
|
|
|
if (handler->args)
|
|
|
|
{
|
2012-04-21 04:08:36 +00:00
|
|
|
arg_count = strlen(handler->args);
|
|
|
|
for (n = 0; n < arg_count; n++)
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
2012-04-21 04:08:36 +00:00
|
|
|
if (!strchr("?+", last_arg))
|
2012-04-19 01:06:52 +00:00
|
|
|
cbuf_append(buf, " ");
|
2012-04-21 04:08:36 +00:00
|
|
|
arg_code = handler->args[n];
|
|
|
|
switch (arg_code)
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
2012-04-21 04:08:36 +00:00
|
|
|
case '?': cbuf_append(buf, "["); opt++; break;
|
|
|
|
case '+': /* ignore */ break;
|
|
|
|
case 'n': cbuf_append(buf, "<nick>"); break;
|
|
|
|
case 'u': cbuf_append(buf, "<user>"); break;
|
|
|
|
case 'i': cbuf_append(buf, "<cid>"); break;
|
|
|
|
case 'a': cbuf_append(buf, "<addr>"); break;
|
|
|
|
case 'r': cbuf_append(buf, "<addr range>"); break;
|
|
|
|
case 'm': cbuf_append(buf, "<message>"); break;
|
|
|
|
case 'p': cbuf_append(buf, "<password>"); break;
|
2011-12-18 23:34:20 +00:00
|
|
|
case 'C': cbuf_append(buf, "<credentials>"); break;
|
2012-04-21 04:08:36 +00:00
|
|
|
case 'c': cbuf_append(buf, "<command>"); break;
|
|
|
|
case 'N': cbuf_append(buf, "<number>"); break;
|
|
|
|
default: LOG_ERROR("unknown argument code '%c'", arg_code);
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
2012-04-21 04:08:36 +00:00
|
|
|
last_arg = arg_code;
|
2009-06-25 23:15:06 +00:00
|
|
|
}
|
2012-04-19 01:06:52 +00:00
|
|
|
while (opt--)
|
|
|
|
cbuf_append(buf, "]");
|
2009-06-25 23:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
|
2011-12-21 12:53:12 +00:00
|
|
|
void send_message(struct command_base* cbase, struct hub_user* user, struct cbuffer* buf)
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
char* buffer = adc_msg_escape(cbuf_get(buf));
|
2011-12-09 09:29:50 +00:00
|
|
|
struct adc_message* command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
|
|
|
|
adc_msg_add_argument(command, buffer);
|
|
|
|
route_to_user(cbase->hub, user, command);
|
|
|
|
adc_msg_free(command);
|
|
|
|
hub_free(buffer);
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_destroy(buf);
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int send_command_access_denied(struct command_base* cbase, struct hub_user* user, const char* prefix)
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf = cbuf_create(128);
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append_format(buf, "*** %s: Access denied.", prefix);
|
|
|
|
send_message(cbase, user, buf);
|
2011-12-09 09:29:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int send_command_not_found(struct command_base* cbase, struct hub_user* user, const char* prefix)
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf = cbuf_create(128);
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append_format(buf, "*** %s: Command not found.", prefix);
|
|
|
|
send_message(cbase, user, buf);
|
2011-12-09 09:29:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int send_command_syntax_error(struct command_base* cbase, struct hub_user* user)
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf = cbuf_create(128);
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append(buf, "*** Syntax error.");
|
|
|
|
send_message(cbase, user, buf);
|
2011-12-09 09:29:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int send_command_missing_arguments(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
|
|
|
{
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf = cbuf_create(512);
|
2011-12-22 00:49:21 +00:00
|
|
|
cbuf_append_format(buf, "*** Missing argument: See !help %s\n", cmd->prefix);
|
2011-12-18 23:34:20 +00:00
|
|
|
send_message(cbase, user, buf);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-12-09 09:29:50 +00:00
|
|
|
|
|
|
|
static size_t command_count_required_args(struct command_handle* handler)
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
for (n = 0; n < strlen(handler->args); n++)
|
|
|
|
{
|
|
|
|
if (handler->args[n] == '?')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
int command_check_args(struct hub_command* cmd, struct command_handle* handler)
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
|
|
|
if (!handler->args)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (list_size(cmd->args) >= command_count_required_args(handler))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
int command_invoke(struct command_base* cbase, struct hub_user* user, const char* message)
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
int ret = 0;
|
|
|
|
struct hub_command* cmd = command_parse(cbase, user, message);
|
2011-12-09 09:29:50 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
switch (cmd->status)
|
|
|
|
{
|
|
|
|
case cmd_status_ok:
|
|
|
|
ret = cmd->handler(cbase, user, cmd);
|
|
|
|
break;
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
case cmd_status_not_found:
|
|
|
|
ret = send_command_not_found(cbase, user, cmd->prefix);
|
|
|
|
break;
|
2011-12-09 09:29:50 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
case cmd_status_access_error:
|
|
|
|
ret = send_command_access_denied(cbase, user, cmd->prefix);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case cmd_status_missing_args:
|
|
|
|
ret = send_command_missing_arguments(cbase, user, cmd);
|
|
|
|
break;
|
|
|
|
|
2012-04-21 04:08:36 +00:00
|
|
|
case cmd_status_syntax_error:
|
2011-12-18 23:34:20 +00:00
|
|
|
case cmd_status_arg_nick:
|
|
|
|
case cmd_status_arg_cid:
|
|
|
|
case cmd_status_arg_address:
|
|
|
|
case cmd_status_arg_number:
|
|
|
|
case cmd_status_arg_cred:
|
|
|
|
case cmd_status_arg_command:
|
|
|
|
ret = send_command_syntax_error(cbase, user);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_free(cmd);
|
|
|
|
|
|
|
|
return ret;
|
2009-05-15 14:52:04 +00:00
|
|
|
}
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2012-04-18 21:03:05 +00:00
|
|
|
size_t hub_command_arg_reset(struct hub_command* cmd)
|
|
|
|
{
|
|
|
|
cmd->args->iterator = NULL;
|
|
|
|
return list_size(cmd->args);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct hub_command_arg_data* hub_command_arg_next(struct hub_command* cmd, enum hub_command_arg_type type)
|
|
|
|
{
|
|
|
|
struct hub_command_arg_data* ptr = (struct hub_command_arg_data*) list_get_next(cmd->args);
|
|
|
|
if (!ptr)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
uhub_assert(ptr->type == type);
|
|
|
|
if (ptr->type != type)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2011-12-21 12:53:12 +00:00
|
|
|
static int command_status(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd, struct cbuffer* msg)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf = cbuf_create(cbuf_size(msg) + strlen(cmd->prefix) + 8);
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append_format(buf, "*** %s: %s", cmd->prefix, cbuf_get(msg));
|
|
|
|
send_message(cbase, user, buf);
|
|
|
|
cbuf_destroy(msg);
|
2011-12-09 09:29:50 +00:00
|
|
|
return 0;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int command_help(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2011-12-28 09:45:02 +00:00
|
|
|
size_t n;
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf = cbuf_create(MAX_HELP_LINE);
|
2012-04-18 21:03:05 +00:00
|
|
|
struct hub_command_arg_data* data = hub_command_arg_next(cmd, type_command);
|
|
|
|
struct command_handle* command;
|
2009-06-30 09:48:58 +00:00
|
|
|
|
2012-04-18 21:03:05 +00:00
|
|
|
if (!data)
|
2009-03-20 17:37:38 +00:00
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append(buf, "Available commands:\n");
|
2010-11-12 17:14:15 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
for (command = (struct command_handle*) list_get_first(cbase->handlers); command; command = (struct command_handle*) list_get_next(cbase->handlers))
|
2009-03-20 17:37:38 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
if (command_is_available(command, user))
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
2011-12-28 09:45:02 +00:00
|
|
|
cbuf_append_format(buf, "!%s", command->prefix);
|
|
|
|
for (n = strlen(command->prefix); n < cbase->prefix_length_max; n++)
|
|
|
|
cbuf_append(buf, " ");
|
|
|
|
cbuf_append_format(buf, " - %s\n", command->description);
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-18 21:03:05 +00:00
|
|
|
command = data->data.command;
|
2011-12-09 09:29:50 +00:00
|
|
|
if (command_is_available(command, user))
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
2012-04-21 04:08:36 +00:00
|
|
|
cbuf_append_format(buf, "Usage: ");
|
2011-12-18 23:34:20 +00:00
|
|
|
command_get_syntax(command, buf);
|
|
|
|
cbuf_append_format(buf, "\n%s\n", command->description);
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
2011-12-09 09:29:50 +00:00
|
|
|
else
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append(buf, "This command is not available to you.\n");
|
2009-03-20 17:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-18 23:34:20 +00:00
|
|
|
return command_status(cbase, user, cmd, buf);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int command_uptime(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf = cbuf_create(128);
|
2009-03-18 23:55:11 +00:00
|
|
|
size_t d;
|
|
|
|
size_t h;
|
|
|
|
size_t m;
|
2011-12-09 09:29:50 +00:00
|
|
|
size_t D = (size_t) difftime(time(0), cbase->hub->tm_started);
|
2009-03-18 23:55:11 +00:00
|
|
|
|
|
|
|
d = D / (24 * 3600);
|
|
|
|
D = D % (24 * 3600);
|
|
|
|
h = D / 3600;
|
|
|
|
D = D % 3600;
|
|
|
|
m = D / 60;
|
|
|
|
|
|
|
|
if (d)
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append_format(buf, "%d day%s, ", (int) d, d != 1 ? "s" : "");
|
|
|
|
cbuf_append_format(buf, "%02d:%02d", (int) h, (int) m);
|
|
|
|
return command_status(cbase, user, cmd, buf);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int command_kick(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf;
|
2012-04-18 21:03:05 +00:00
|
|
|
struct hub_command_arg_data* arg = hub_command_arg_next(cmd, type_user);
|
|
|
|
struct hub_user* target = arg->data.user;
|
2011-12-18 23:34:20 +00:00
|
|
|
|
|
|
|
buf = cbuf_create(128);
|
2009-04-08 22:48:00 +00:00
|
|
|
if (target == user)
|
2011-12-18 23:34:20 +00:00
|
|
|
{
|
|
|
|
cbuf_append(buf, "Cannot kick yourself.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cbuf_append_format(buf, "Kicking user \"%s\".", target->id.nick);
|
|
|
|
hub_disconnect_user(cbase->hub, target, quit_kicked);
|
|
|
|
}
|
|
|
|
return command_status(cbase, user, cmd, buf);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int command_reload(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
2009-03-18 23:53:44 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
cbase->hub->status = hub_status_restart;
|
2011-12-18 23:34:20 +00:00
|
|
|
return command_status(cbase, user, cmd, cbuf_create_const("Reloading configuration..."));
|
2009-03-18 23:53:44 +00:00
|
|
|
}
|
|
|
|
|
2012-01-19 01:58:20 +00:00
|
|
|
#ifdef DEBUG_UNLOAD_PLUGINS
|
|
|
|
int hub_plugins_load(struct hub_info* hub);
|
|
|
|
int hub_plugins_unload(struct hub_info* hub);
|
|
|
|
|
|
|
|
static int command_load(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
|
|
|
{
|
|
|
|
hub_plugins_load(cbase->hub);
|
|
|
|
return command_status(cbase, user, cmd, cbuf_create_const("Loading plugins..."));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int command_unload(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
|
|
|
{
|
|
|
|
hub_plugins_unload(cbase->hub);
|
|
|
|
return command_status(cbase, user, cmd, cbuf_create_const("Unloading plugins..."));
|
|
|
|
}
|
|
|
|
#endif /* DEBUG_UNLOAD_PLUGINS */
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int command_shutdown_hub(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
2009-03-18 23:53:44 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
cbase->hub->status = hub_status_shutdown;
|
2011-12-18 23:34:20 +00:00
|
|
|
return command_status(cbase, user, cmd, cbuf_create_const("Hub shutting down..."));
|
2009-03-18 23:53:44 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int command_version(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf;
|
2011-12-09 09:29:50 +00:00
|
|
|
if (cbase->hub->config->show_banner_sys_info)
|
2011-12-18 23:34:20 +00:00
|
|
|
buf = cbuf_create_const("Powered by " PRODUCT_STRING " on " OPSYS "/" CPUINFO);
|
2010-02-11 00:19:58 +00:00
|
|
|
else
|
2011-12-18 23:34:20 +00:00
|
|
|
buf = cbuf_create_const("Powered by " PRODUCT_STRING);
|
|
|
|
return command_status(cbase, user, cmd, buf);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int command_myip(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
2009-03-08 16:11:10 +00:00
|
|
|
{
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf = cbuf_create(128);
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append_format(buf, "Your address is \"%s\"", user_get_address(user));
|
|
|
|
return command_status(cbase, user, cmd, buf);
|
2009-06-25 23:15:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int command_getip(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf = cbuf_create(128);
|
2012-04-18 21:03:05 +00:00
|
|
|
struct hub_command_arg_data* arg = hub_command_arg_next(cmd, type_user);
|
|
|
|
cbuf_append_format(buf, "\"%s\" has address \"%s\"", arg->data.user->id.nick, user_get_address(arg->data.user));
|
2011-12-18 23:34:20 +00:00
|
|
|
return command_status(cbase, user, cmd, buf);
|
2009-03-08 16:11:10 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int command_whoip(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
2009-07-26 02:02:14 +00:00
|
|
|
{
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf;
|
2012-04-18 21:03:05 +00:00
|
|
|
struct hub_command_arg_data* arg = hub_command_arg_next(cmd, type_range);
|
2011-12-18 23:34:20 +00:00
|
|
|
struct linked_list* users = (struct linked_list*) list_create();
|
2009-07-26 02:02:14 +00:00
|
|
|
struct hub_user* u;
|
|
|
|
int ret = 0;
|
|
|
|
|
2012-04-18 21:03:05 +00:00
|
|
|
ret = uman_get_user_by_addr(cbase->hub, users, arg->data.range);
|
2009-07-26 02:02:14 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
list_clear(users, &null_free);
|
2009-07-26 02:02:14 +00:00
|
|
|
list_destroy(users);
|
2011-12-18 23:34:20 +00:00
|
|
|
return command_status(cbase, user, cmd, cbuf_create_const("No users found."));
|
2009-07-26 02:02:14 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
buf = cbuf_create(128 + ((MAX_NICK_LEN + INET6_ADDRSTRLEN + 5) * ret));
|
|
|
|
cbuf_append_format(buf, "*** %s: Found %d match%s:\n", cmd->prefix, ret, ((ret != 1) ? "es" : ""));
|
2009-08-02 19:58:12 +00:00
|
|
|
|
2009-07-26 02:02:14 +00:00
|
|
|
u = (struct hub_user*) list_get_first(users);
|
|
|
|
while (u)
|
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append_format(buf, "%s (%s)\n", u->id.nick, user_get_address(u));
|
2009-07-26 02:02:14 +00:00
|
|
|
u = (struct hub_user*) list_get_next(users);
|
|
|
|
}
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append(buf, "\n");
|
2009-07-26 02:02:14 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
send_message(cbase, user, buf);
|
|
|
|
list_clear(users, &null_free);
|
2009-11-18 16:41:28 +00:00
|
|
|
list_destroy(users);
|
2009-08-02 19:58:12 +00:00
|
|
|
return 0;
|
2009-07-26 02:02:14 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
|
|
|
|
static int command_broadcast(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
2009-07-26 02:47:43 +00:00
|
|
|
{
|
2012-04-20 23:56:26 +00:00
|
|
|
struct hub_command_arg_data* arg = hub_command_arg_next(cmd, type_string);
|
|
|
|
char* message = arg->data.string;
|
|
|
|
size_t message_len = strlen(message);
|
2010-05-25 20:21:56 +00:00
|
|
|
char pm_flag[7] = "PM";
|
|
|
|
char from_sid[5];
|
2010-05-26 07:38:14 +00:00
|
|
|
size_t recipients = 0;
|
2011-01-02 01:39:25 +00:00
|
|
|
struct hub_user* target;
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf = cbuf_create(128);
|
2012-01-10 01:21:27 +00:00
|
|
|
struct adc_message* command = NULL;
|
2010-05-25 20:21:56 +00:00
|
|
|
|
|
|
|
memcpy(from_sid, sid_to_string(user->id.sid), sizeof(from_sid));
|
|
|
|
memcpy(pm_flag + 2, from_sid, sizeof(from_sid));
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
target = (struct hub_user*) list_get_first(cbase->hub->users->list);
|
2010-05-25 20:21:56 +00:00
|
|
|
while (target)
|
|
|
|
{
|
2010-05-26 07:38:14 +00:00
|
|
|
if (target != user)
|
|
|
|
{
|
|
|
|
recipients++;
|
|
|
|
command = adc_msg_construct(ADC_CMD_DMSG, message_len + 23);
|
|
|
|
if (!command)
|
|
|
|
break;
|
|
|
|
|
|
|
|
adc_msg_add_argument(command, from_sid);
|
|
|
|
adc_msg_add_argument(command, sid_to_string(target->id.sid));
|
2012-01-10 01:21:27 +00:00
|
|
|
adc_msg_add_argument(command, message);
|
2010-05-26 07:38:14 +00:00
|
|
|
adc_msg_add_argument(command, pm_flag);
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
route_to_user(cbase->hub, target, command);
|
2010-05-26 07:38:14 +00:00
|
|
|
adc_msg_free(command);
|
|
|
|
}
|
2011-12-09 09:29:50 +00:00
|
|
|
target = (struct hub_user*) list_get_next(cbase->hub->users->list);
|
2010-05-25 20:21:56 +00:00
|
|
|
}
|
2010-05-26 07:38:14 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append_format(buf, "*** %s: Delivered to " PRINTF_SIZE_T " user%s", cmd->prefix, recipients, (recipients != 1 ? "s" : ""));
|
|
|
|
send_message(cbase, user, buf);
|
2012-01-10 01:21:27 +00:00
|
|
|
hub_free(message);
|
2010-05-25 20:21:56 +00:00
|
|
|
return 0;
|
2009-07-26 02:47:43 +00:00
|
|
|
}
|
2009-07-26 02:02:14 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int command_log(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
2009-07-26 04:03:43 +00:00
|
|
|
{
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf;
|
2012-04-18 21:03:05 +00:00
|
|
|
struct hub_command_arg_data* arg = hub_command_arg_next(cmd, type_string);
|
2011-12-09 09:29:50 +00:00
|
|
|
struct linked_list* messages = cbase->hub->logout_info;
|
2009-07-26 04:03:43 +00:00
|
|
|
struct hub_logout_info* log;
|
2012-04-18 21:03:05 +00:00
|
|
|
char* search = arg ? arg->data.string : "";
|
|
|
|
size_t search_len = strlen(search);
|
2009-07-28 00:02:19 +00:00
|
|
|
size_t search_hits = 0;
|
2009-07-26 04:03:43 +00:00
|
|
|
|
|
|
|
if (!list_size(messages))
|
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
return command_status(cbase, user, cmd, cbuf_create_const("No entries logged."));
|
2009-07-26 04:03:43 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
buf = cbuf_create(128);
|
2012-04-18 21:03:05 +00:00
|
|
|
cbuf_append_format(buf, "Logged entries: " PRINTF_SIZE_T, list_size(messages));
|
2009-07-26 04:03:43 +00:00
|
|
|
|
2009-07-28 00:02:19 +00:00
|
|
|
if (search_len)
|
|
|
|
{
|
2012-04-18 21:03:05 +00:00
|
|
|
cbuf_append_format(buf, ", searching for \"%s\"", search);
|
2009-07-28 00:02:19 +00:00
|
|
|
}
|
2011-12-18 23:34:20 +00:00
|
|
|
command_status(cbase, user, cmd, buf);
|
2009-07-26 04:03:43 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
buf = cbuf_create(MAX_HELP_LINE);
|
2009-07-26 04:03:43 +00:00
|
|
|
log = (struct hub_logout_info*) list_get_first(messages);
|
|
|
|
while (log)
|
|
|
|
{
|
2009-07-28 00:02:19 +00:00
|
|
|
const char* address = ip_convert_to_string(&log->addr);
|
|
|
|
int show = 0;
|
|
|
|
|
|
|
|
if (search_len)
|
|
|
|
{
|
|
|
|
if (memmem(log->cid, MAX_CID_LEN, search, search_len) || memmem(log->nick, MAX_NICK_LEN, search, search_len) || memmem(address, strlen(address), search, search_len))
|
|
|
|
{
|
|
|
|
search_hits++;
|
|
|
|
show = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
show = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (show)
|
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append_format(buf, "* %s %s, %s [%s] - %s", get_timestamp(log->time), log->cid, log->nick, ip_convert_to_string(&log->addr), user_get_quit_reason_string(log->reason));
|
|
|
|
send_message(cbase, user, buf);
|
|
|
|
buf = cbuf_create(MAX_HELP_LINE);
|
2009-07-28 00:02:19 +00:00
|
|
|
}
|
2009-07-26 04:03:43 +00:00
|
|
|
log = (struct hub_logout_info*) list_get_next(messages);
|
|
|
|
}
|
|
|
|
|
2009-07-28 00:02:19 +00:00
|
|
|
if (search_len)
|
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append_format(buf, PRINTF_SIZE_T " entries shown.", search_hits);
|
|
|
|
command_status(cbase, user, cmd, buf);
|
|
|
|
buf = NULL;
|
2009-07-28 00:02:19 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
if (buf)
|
|
|
|
cbuf_destroy(buf);
|
2009-07-28 00:02:19 +00:00
|
|
|
return 0;
|
2009-07-26 04:03:43 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
static int command_stats(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2011-12-21 12:53:12 +00:00
|
|
|
struct cbuffer* buf = cbuf_create(128);
|
2011-12-09 09:29:50 +00:00
|
|
|
struct hub_info* hub = cbase->hub;
|
2009-06-29 21:22:13 +00:00
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
cbuf_append_format(buf, PRINTF_SIZE_T " users, peak: " PRINTF_SIZE_T ". Network (up/down): %d/%d KB/s, peak: %d/%d KB/s",
|
|
|
|
hub->users->count,
|
|
|
|
hub->users->count_peak,
|
|
|
|
(int) hub->stats.net_tx / 1024,
|
|
|
|
(int) hub->stats.net_rx / 1024,
|
|
|
|
(int) hub->stats.net_tx_peak / 1024,
|
|
|
|
(int) hub->stats.net_rx_peak / 1024);
|
|
|
|
return command_status(cbase, user, cmd, buf);
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
2009-06-25 23:15:06 +00:00
|
|
|
|
2011-12-09 14:58:58 +00:00
|
|
|
static struct command_handle* add_builtin(struct command_base* cbase, const char* prefix, const char* args, enum auth_credentials cred, command_handler handler, const char* description)
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
|
|
|
struct command_handle* handle = (struct command_handle*) hub_malloc_zero(sizeof(struct command_handle));
|
2011-12-09 15:35:42 +00:00
|
|
|
handle->prefix = prefix;
|
2011-12-09 09:29:50 +00:00
|
|
|
handle->length = strlen(prefix);
|
2011-12-09 15:35:42 +00:00
|
|
|
handle->args = args;
|
2011-12-09 09:29:50 +00:00
|
|
|
handle->cred = cred;
|
|
|
|
handle->handler = handler;
|
2011-12-09 15:35:42 +00:00
|
|
|
handle->description = description;
|
2011-12-09 09:29:50 +00:00
|
|
|
handle->origin = "built-in";
|
2011-12-09 14:58:58 +00:00
|
|
|
handle->ptr = cbase;
|
2011-12-09 09:29:50 +00:00
|
|
|
return handle;
|
|
|
|
}
|
2009-06-25 23:15:06 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
#define ADD_COMMAND(PREFIX, LENGTH, ARGS, CREDENTIALS, FUNCTION, DESCRIPTION) \
|
2011-12-09 14:58:58 +00:00
|
|
|
command_add(cbase, add_builtin(cbase, PREFIX, ARGS, CREDENTIALS, FUNCTION, DESCRIPTION), NULL)
|
2009-05-15 14:52:04 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
void commands_builtin_add(struct command_base* cbase)
|
|
|
|
{
|
2012-04-20 23:56:26 +00:00
|
|
|
ADD_COMMAND("broadcast", 9, "+m",auth_cred_operator, command_broadcast,"Send a message to all users" );
|
2012-04-18 21:03:05 +00:00
|
|
|
ADD_COMMAND("getip", 5, "u", auth_cred_operator, command_getip, "Show IP address for a user" );
|
2011-12-09 09:29:50 +00:00
|
|
|
ADD_COMMAND("help", 4, "?c",auth_cred_guest, command_help, "Show this help message." );
|
2012-04-18 21:03:05 +00:00
|
|
|
ADD_COMMAND("kick", 4, "u", auth_cred_operator, command_kick, "Kick a user" );
|
2012-04-17 08:57:10 +00:00
|
|
|
ADD_COMMAND("log", 3, "?m",auth_cred_operator, command_log, "Display log" ); // fail
|
2011-12-18 23:34:20 +00:00
|
|
|
ADD_COMMAND("myip", 4, "", auth_cred_guest, command_myip, "Show your own IP." );
|
|
|
|
ADD_COMMAND("reload", 6, "", auth_cred_admin, command_reload, "Reload configuration files." );
|
2012-01-19 01:58:20 +00:00
|
|
|
ADD_COMMAND("shutdown", 8, "", auth_cred_admin, command_shutdown_hub, "Shutdown hub." );
|
2011-12-18 23:34:20 +00:00
|
|
|
ADD_COMMAND("stats", 5, "", auth_cred_super, command_stats, "Show hub statistics." );
|
|
|
|
ADD_COMMAND("uptime", 6, "", auth_cred_guest, command_uptime, "Display hub uptime info." );
|
|
|
|
ADD_COMMAND("version", 7, "", auth_cred_guest, command_version, "Show hub version info." );
|
2012-04-18 21:03:05 +00:00
|
|
|
ADD_COMMAND("whoip", 5, "r", auth_cred_operator, command_whoip, "Show users matching IP range" );
|
2012-01-19 01:58:20 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_UNLOAD_PLUGINS
|
|
|
|
ADD_COMMAND("load", 4, "", auth_cred_admin, command_load, "Load plugins." );
|
|
|
|
ADD_COMMAND("unload", 6, "", auth_cred_admin, command_unload, "Unload plugins." );
|
|
|
|
#endif /* DEBUG_UNLOAD_PLUGINS */
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
void commands_builtin_remove(struct command_base* cbase)
|
|
|
|
{
|
|
|
|
struct command_handle* command;
|
|
|
|
while ((command = list_get_first(cbase->handlers)))
|
|
|
|
{
|
|
|
|
command_del(cbase, command);
|
2011-12-09 15:35:42 +00:00
|
|
|
hub_free(command);
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
|
|
|
}
|