2009-02-19 16:14:09 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
2011-08-16 13:09:02 +00:00
|
|
|
* Copyright (C) 2007-2011, 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
|
|
|
|
#define CRASH_DEBUG
|
|
|
|
#endif
|
|
|
|
|
2009-06-30 09:48:58 +00:00
|
|
|
#define MAX_HELP_MSG 1024
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_access_denied(struct command_base* cbase, struct hub_user* user, const char* prefix);
|
|
|
|
static int command_not_found(struct command_base* cbase, struct hub_user* user, const char* prefix);
|
|
|
|
static int command_syntax_error(struct command_base* cbase, struct hub_user* user);
|
|
|
|
|
2011-12-09 14:58:58 +00:00
|
|
|
static size_t str_append(char* dst, const char* buf, size_t size)
|
|
|
|
{
|
|
|
|
size_t dst_len = strlen(dst);
|
|
|
|
size_t buf_len = strlen(buf);
|
|
|
|
|
|
|
|
if (dst_len + buf_len >= size)
|
|
|
|
return dst_len + buf_len;
|
|
|
|
|
|
|
|
memcpy(dst + dst_len, buf, buf_len);
|
|
|
|
dst[dst_len + buf_len] = '\0';
|
|
|
|
return dst_len + buf_len;
|
|
|
|
}
|
|
|
|
|
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;
|
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);
|
|
|
|
uhub_assert(hub != NULL);
|
|
|
|
|
|
|
|
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-09 09:29:50 +00:00
|
|
|
/**
|
|
|
|
* Destroy / free a command created by command_create().
|
|
|
|
*/
|
2009-06-25 23:15:06 +00:00
|
|
|
static void command_destroy(struct hub_command* cmd)
|
|
|
|
{
|
|
|
|
if (!cmd) return;
|
|
|
|
hub_free(cmd->prefix);
|
|
|
|
|
|
|
|
if (cmd->args)
|
|
|
|
{
|
|
|
|
list_clear(cmd->args, &hub_free);
|
|
|
|
list_destroy(cmd->args);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!strncmp(prefix, handler->prefix, handler->length))
|
|
|
|
{
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct linked_list* command_extract_arguments(struct command_base* cbase, struct command_handle* command, struct linked_list* tokens)
|
|
|
|
{
|
|
|
|
int arg = 0;
|
|
|
|
int opt = 0;
|
|
|
|
char* token = NULL;
|
|
|
|
struct hub_user* target = NULL;
|
|
|
|
struct command_handle* target_command = NULL;
|
|
|
|
enum auth_credentials cred;
|
|
|
|
struct linked_list* args = list_create();
|
|
|
|
|
|
|
|
|
|
|
|
if (!args)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while ((token = list_get_first(tokens)))
|
|
|
|
{
|
|
|
|
switch (command->args[arg++])
|
|
|
|
{
|
|
|
|
case '?':
|
|
|
|
uhub_assert(opt == 0);
|
|
|
|
opt = 1;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
target = uman_get_user_by_nick(cbase->hub, token);
|
|
|
|
if (!target)
|
|
|
|
{
|
|
|
|
list_destroy(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
list_append(args, target);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
uman_get_user_by_cid(cbase->hub, token);
|
|
|
|
if (!target)
|
|
|
|
{
|
|
|
|
list_destroy(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
list_append(args, target);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
|
|
|
if (!(ip_is_valid_ipv4(token) || ip_is_valid_ipv6(token)))
|
|
|
|
{
|
|
|
|
list_destroy(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
list_append(args, token);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
case 'p':
|
|
|
|
list_append(args, token);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
target_command = command_handler_lookup(cbase, token);
|
|
|
|
if (!target_command)
|
|
|
|
{
|
|
|
|
list_destroy(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
list_append(args, target_command);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
if (!auth_string_to_cred(token, &cred))
|
|
|
|
{
|
|
|
|
list_destroy(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
list_append(args, (void*) cred);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'N':
|
|
|
|
list_append(args, (void*) (int*) (intptr_t) uhub_atoi(token));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\0':
|
|
|
|
if (!opt)
|
|
|
|
{
|
|
|
|
list_destroy(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
list_remove(tokens, token);
|
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a command and break it down into a struct hub_command.
|
|
|
|
*/
|
|
|
|
static int command_parse(struct command_base* cbase, struct hub_user* user, const char* message)
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
2010-01-28 00:06:41 +00:00
|
|
|
char* prefix;
|
|
|
|
int n;
|
2011-12-09 15:54:48 +00:00
|
|
|
int ret;
|
2009-06-25 23:15:06 +00:00
|
|
|
struct hub_command* cmd = hub_malloc_zero(sizeof(struct hub_command));
|
2011-12-09 09:29:50 +00:00
|
|
|
struct command_handle* handler = NULL;
|
|
|
|
struct linked_list* tokens = NULL;
|
2010-01-28 00:06:41 +00:00
|
|
|
|
2009-06-25 23:15:06 +00:00
|
|
|
if (!cmd) return 0;
|
|
|
|
|
|
|
|
cmd->message = message;
|
2011-12-09 09:29:50 +00:00
|
|
|
cmd->args = NULL;
|
|
|
|
tokens = list_create();
|
2009-06-25 23:15:06 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
n = split_string(message, "\\s", tokens, 0);
|
2009-06-25 23:15:06 +00:00
|
|
|
if (n <= 0)
|
|
|
|
{
|
2011-12-09 15:54:48 +00:00
|
|
|
ret = 0; // FIXME
|
|
|
|
goto command_parse_cleanup;
|
2009-06-25 23:15:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
// Find a matching command handler
|
|
|
|
prefix = list_get_first(tokens);
|
2009-11-18 16:41:28 +00:00
|
|
|
if (prefix && prefix[0] && prefix[1])
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
|
|
|
cmd->prefix = hub_strdup(&prefix[1]);
|
|
|
|
cmd->prefix_len = strlen(cmd->prefix);
|
2011-12-09 09:29:50 +00:00
|
|
|
handler = command_handler_lookup(cbase, cmd->prefix);
|
|
|
|
if (!handler)
|
|
|
|
{
|
2011-12-09 15:54:48 +00:00
|
|
|
ret = command_not_found(cbase, user, prefix);
|
|
|
|
goto command_parse_cleanup;
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
2009-06-25 23:15:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-09 15:54:48 +00:00
|
|
|
ret = command_syntax_error(cbase, user);
|
|
|
|
goto command_parse_cleanup;
|
2009-06-25 23:15:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
// Remove the first token.
|
|
|
|
list_remove(tokens, prefix);
|
|
|
|
hub_free(prefix);
|
2009-03-18 23:55:11 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
// Parse arguments
|
|
|
|
cmd->args = command_extract_arguments(cbase, handler, tokens);
|
2009-05-15 14:52:04 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
if (!cmd->args)
|
|
|
|
{
|
2011-12-09 15:54:48 +00:00
|
|
|
ret = 0;
|
|
|
|
goto command_parse_cleanup;
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
2009-06-25 23:15:06 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
if (command_is_available(handler, user))
|
|
|
|
{
|
|
|
|
handler->handler(cbase, user, handler, cmd);
|
2011-12-09 15:54:48 +00:00
|
|
|
ret = 0;
|
|
|
|
goto command_parse_cleanup;
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-09 15:54:48 +00:00
|
|
|
ret = command_access_denied(cbase, user, prefix);
|
|
|
|
goto command_parse_cleanup;
|
2011-12-09 09:29:50 +00:00
|
|
|
}
|
2011-12-09 15:54:48 +00:00
|
|
|
|
|
|
|
command_parse_cleanup:
|
|
|
|
command_destroy(cmd);
|
|
|
|
list_clear(tokens, &hub_free);
|
|
|
|
list_destroy(tokens);
|
|
|
|
return ret;
|
2009-06-25 23:15:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
const char* command_get_syntax(struct command_handle* handler)
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
|
|
|
static char args[128];
|
|
|
|
size_t n = 0;
|
2010-11-12 17:14:15 +00:00
|
|
|
int opt = 0;
|
2010-01-28 00:06:41 +00:00
|
|
|
args[0] = 0;
|
2009-06-25 23:15:06 +00:00
|
|
|
if (handler->args)
|
|
|
|
{
|
|
|
|
for (n = 0; n < strlen(handler->args); n++)
|
|
|
|
{
|
2011-12-09 14:58:58 +00:00
|
|
|
if (n > 0 && !opt) str_append(args, " ", sizeof(args));
|
2009-06-25 23:15:06 +00:00
|
|
|
switch (handler->args[n])
|
|
|
|
{
|
2011-12-09 14:58:58 +00:00
|
|
|
case '?': str_append(args, "[", sizeof(args)); opt = 1; continue;
|
|
|
|
case 'n': str_append(args, "<nick>", sizeof(args)); break;
|
|
|
|
case 'i': str_append(args, "<cid>", sizeof(args)); break;
|
|
|
|
case 'a': str_append(args, "<addr>", sizeof(args)); break;
|
|
|
|
case 'm': str_append(args, "<message>", sizeof(args)); break;
|
|
|
|
case 'p': str_append(args, "<password>", sizeof(args)); break;
|
|
|
|
case 'C': str_append(args, "<credentials>", sizeof(args)); break;
|
|
|
|
case 'c': str_append(args, "<command>", sizeof(args)); break;
|
|
|
|
case 'N': str_append(args, "<number>", sizeof(args)); break;
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
if (opt)
|
|
|
|
{
|
2011-12-09 14:58:58 +00:00
|
|
|
str_append(args, "]", sizeof(args));
|
2010-11-12 17:14:15 +00:00
|
|
|
opt = 0;
|
2009-06-25 23:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
|
|
|
|
void send_message(struct command_base* cbase, struct hub_user* user, const char* message)
|
|
|
|
{
|
|
|
|
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(cbase->hub, user, command);
|
|
|
|
adc_msg_free(command);
|
|
|
|
hub_free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int command_access_denied(struct command_base* cbase, struct hub_user* user, const char* prefix)
|
|
|
|
{
|
|
|
|
char temp[128];
|
|
|
|
snprintf(temp, 128, "*** %s: Access denied.", prefix);
|
|
|
|
send_message(cbase, user, temp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int command_not_found(struct command_base* cbase, struct hub_user* user, const char* prefix)
|
|
|
|
{
|
|
|
|
char temp[128];
|
|
|
|
snprintf(temp, 128, "*** %s: Command not found", prefix);
|
|
|
|
send_message(cbase, user, temp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int command_syntax_error(struct command_base* cbase, struct hub_user* user)
|
|
|
|
{
|
|
|
|
send_message(cbase, user, "*** Syntax error");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-09 09:29:50 +00:00
|
|
|
return command_parse(cbase, user, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
int command_is_available(struct command_handle* handle, struct hub_user* user)
|
|
|
|
{
|
|
|
|
uhub_assert(handle != NULL);
|
|
|
|
uhub_assert(user != NULL);
|
|
|
|
return handle->cred <= user->credentials;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
|
|
|
|
static int command_status(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd, const char* message)
|
2009-05-15 14:52:04 +00:00
|
|
|
{
|
|
|
|
char temp[1024];
|
2009-06-25 23:15:06 +00:00
|
|
|
snprintf(temp, 1024, "*** %s: %s", cmd->prefix, message);
|
2011-12-09 09:29:50 +00:00
|
|
|
send_message(cbase, user, temp);
|
2009-05-15 14:52:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_status_user_not_found(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd, const char* nick)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-03-20 18:02:32 +00:00
|
|
|
char temp[128];
|
2011-12-09 09:29:50 +00:00
|
|
|
snprintf(temp, 128, "*** %s: No user \"%s\"", cmd->prefix, nick);
|
|
|
|
send_message(cbase, user, temp);
|
|
|
|
return 0;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_help(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-03-20 17:37:38 +00:00
|
|
|
char msg[MAX_HELP_MSG];
|
2011-12-09 09:29:50 +00:00
|
|
|
struct command_handle* command = list_get_first(cmd->args);
|
|
|
|
size_t n = 0;
|
2011-01-02 01:39:25 +00:00
|
|
|
msg[0] = 0;
|
2009-06-30 09:48:58 +00:00
|
|
|
|
2010-11-12 17:14:15 +00:00
|
|
|
if (!command)
|
2009-03-20 17:37:38 +00:00
|
|
|
{
|
2011-12-09 14:58:58 +00:00
|
|
|
n += snprintf(msg, sizeof(msg), "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-09 14:58:58 +00:00
|
|
|
n += snprintf(msg + n, sizeof(msg) - n, "!%s%20s- %s\n", command->prefix, " ", command->description);
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
if (command_is_available(command, user))
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
2011-12-09 14:58:58 +00:00
|
|
|
snprintf(msg, sizeof(msg), "Usage: !%s %s\n%s\n", command->prefix, command_get_syntax(command), 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-09 14:58:58 +00:00
|
|
|
snprintf(msg, sizeof(msg), "This command is not available to you.\n");
|
2009-03-20 17:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, msg);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_uptime(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-03-18 23:55:11 +00:00
|
|
|
char tmp[128];
|
|
|
|
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);
|
2011-12-09 14:58:58 +00:00
|
|
|
size_t offset = 0;
|
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;
|
|
|
|
|
|
|
|
tmp[0] = 0;
|
|
|
|
if (d)
|
2011-12-09 14:58:58 +00:00
|
|
|
offset += snprintf(tmp, sizeof(tmp), "%d day%s, ", (int) d, d != 1 ? "s" : "");
|
|
|
|
snprintf(tmp + offset, sizeof(tmp) - offset, "%02d:%02d", (int) h, (int) m);
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, tmp);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_kick(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-06-25 23:15:06 +00:00
|
|
|
char* nick = list_get_first(cmd->args);
|
2010-01-28 00:06:41 +00:00
|
|
|
struct hub_user* target;
|
2009-11-18 16:41:28 +00:00
|
|
|
if (!nick)
|
|
|
|
return -1; // FIXME: bad syntax.
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
target = uman_get_user_by_nick(cbase->hub, nick);
|
2009-04-08 22:48:00 +00:00
|
|
|
|
|
|
|
if (!target)
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status_user_not_found(cbase, user, cmd, nick);
|
2009-04-08 22:48:00 +00:00
|
|
|
|
|
|
|
if (target == user)
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, "Cannot kick yourself");
|
2009-04-08 22:48:00 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
hub_disconnect_user(cbase->hub, target, quit_kicked);
|
|
|
|
return command_status(cbase, user, cmd, nick);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_ban(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-06-23 21:16:09 +00:00
|
|
|
{
|
2009-06-25 23:15:06 +00:00
|
|
|
char* nick = list_get_first(cmd->args);
|
2010-01-28 00:06:41 +00:00
|
|
|
struct hub_user* target;
|
2009-11-18 16:41:28 +00:00
|
|
|
if (!nick)
|
|
|
|
return -1; // FIXME: bad syntax.
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
target = uman_get_user_by_nick(cbase->hub, nick);
|
2009-06-23 21:16:09 +00:00
|
|
|
|
|
|
|
if (!target)
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status_user_not_found(cbase, user, cmd, nick);
|
2009-06-23 21:16:09 +00:00
|
|
|
|
|
|
|
if (target == user)
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, "Cannot kick/ban yourself");
|
2009-06-23 21:16:09 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
hub_disconnect_user(cbase->hub, target, quit_kicked);
|
|
|
|
acl_user_ban_nick(cbase->hub->acl, target->id.nick);
|
|
|
|
acl_user_ban_cid(cbase->hub->acl, target->id.cid);
|
2009-06-23 21:16:09 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, nick);
|
2009-06-23 21:16:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_unban(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-06-23 21:16:09 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, "Not implemented");
|
2009-06-23 21:16:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_mute(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2010-01-19 21:58:03 +00:00
|
|
|
{
|
|
|
|
char* nick = list_get_first(cmd->args);
|
2010-01-28 00:06:41 +00:00
|
|
|
struct hub_user* target;
|
2010-01-19 21:58:03 +00:00
|
|
|
if (!nick)
|
|
|
|
return -1; // FIXME: bad syntax.
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
target = uman_get_user_by_nick(cbase->hub, nick);
|
2010-01-19 21:58:03 +00:00
|
|
|
|
|
|
|
if (!target)
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status_user_not_found(cbase, user, cmd, nick);
|
2010-01-19 21:58:03 +00:00
|
|
|
|
|
|
|
if (strlen(cmd->prefix) == 4)
|
|
|
|
{
|
|
|
|
user_flag_set(target, flag_muted);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
user_flag_unset(target, flag_muted);
|
|
|
|
}
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, nick);
|
2010-01-19 21:58:03 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_reload(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, 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;
|
|
|
|
return command_status(cbase, user, cmd, "Reloading configuration...");
|
2009-03-18 23:53:44 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_shutdown_hub(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, 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;
|
|
|
|
return command_status(cbase, user, cmd, "Hub shutting down...");
|
2009-03-18 23:53:44 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_version(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2010-02-11 00:19:58 +00:00
|
|
|
const char* tmp;
|
2011-12-09 09:29:50 +00:00
|
|
|
if (cbase->hub->config->show_banner_sys_info)
|
2010-02-11 00:19:58 +00:00
|
|
|
tmp = "Powered by " PRODUCT_STRING " on " OPSYS "/" CPUINFO;
|
|
|
|
else
|
|
|
|
tmp = "Powered by " PRODUCT_STRING;
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, tmp);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_myip(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-03-08 16:11:10 +00:00
|
|
|
{
|
2009-06-25 23:15:06 +00:00
|
|
|
char tmp[128];
|
2009-09-28 21:15:15 +00:00
|
|
|
snprintf(tmp, 128, "Your address is \"%s\"", user_get_address(user));
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, tmp);
|
2009-06-25 23:15:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_getip(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
|
|
|
char tmp[128];
|
|
|
|
char* nick = list_get_first(cmd->args);
|
2010-01-28 00:06:41 +00:00
|
|
|
struct hub_user* target;
|
|
|
|
|
2010-04-05 12:21:40 +00:00
|
|
|
if (!nick)
|
2009-11-18 16:41:28 +00:00
|
|
|
return -1; // FIXME: bad syntax/OOM
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
target = uman_get_user_by_nick(cbase->hub, nick);
|
2009-06-25 23:15:06 +00:00
|
|
|
|
|
|
|
if (!target)
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status_user_not_found(cbase, user, cmd, nick);
|
2009-06-25 23:15:06 +00:00
|
|
|
|
2010-04-05 14:35:55 +00:00
|
|
|
snprintf(tmp, 128, "%s has address \"%s\"", nick, user_get_address(target));
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, tmp);
|
2009-03-08 16:11:10 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_whoip(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-07-26 02:02:14 +00:00
|
|
|
{
|
|
|
|
char* address = list_get_first(cmd->args);
|
|
|
|
struct ip_range range;
|
|
|
|
struct linked_list* users;
|
|
|
|
struct hub_user* u;
|
|
|
|
int ret = 0;
|
2010-01-28 00:06:41 +00:00
|
|
|
char tmp[128];
|
|
|
|
char* buffer;
|
2011-12-09 14:58:58 +00:00
|
|
|
size_t length;
|
2009-07-26 02:02:14 +00:00
|
|
|
|
2009-11-18 16:41:28 +00:00
|
|
|
if (!address)
|
|
|
|
return -1; // FIXME: bad syntax.
|
|
|
|
|
2009-07-26 02:02:14 +00:00
|
|
|
ret = ip_convert_address_to_range(address, &range);
|
|
|
|
if (!ret)
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, "Invalid IP address/range/mask");
|
2009-07-26 02:02:14 +00:00
|
|
|
|
|
|
|
users = (struct linked_list*) list_create();
|
2009-11-18 16:41:28 +00:00
|
|
|
if (!users)
|
|
|
|
return -1; // FIXME: OOM
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
ret = uman_get_user_by_addr(cbase->hub, users, &range);
|
2009-07-26 02:02:14 +00:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
list_destroy(users);
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, "No users found.");
|
2009-07-26 02:02:14 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 08:44:44 +00:00
|
|
|
snprintf(tmp, 128, "*** %s: Found %d match%s:", cmd->prefix, ret, ((ret != 1) ? "es" : ""));
|
2009-07-26 02:02:14 +00:00
|
|
|
|
2011-12-09 14:58:58 +00:00
|
|
|
length = ((MAX_NICK_LEN + INET6_ADDRSTRLEN + 5) * ret) + strlen(tmp) + 3;
|
|
|
|
buffer = hub_malloc(length);
|
2009-11-18 16:41:28 +00:00
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
list_destroy(users);
|
|
|
|
return -1; // FIXME: OOM
|
|
|
|
}
|
|
|
|
|
2009-07-26 02:02:14 +00:00
|
|
|
buffer[0] = 0;
|
2011-12-09 14:58:58 +00:00
|
|
|
str_append(buffer, tmp, length);
|
|
|
|
str_append(buffer, "\n", length);
|
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-09 14:58:58 +00:00
|
|
|
str_append(buffer, u->id.nick, length);
|
|
|
|
str_append(buffer, " (", length);
|
|
|
|
str_append(buffer, user_get_address(u), length);
|
|
|
|
str_append(buffer, ")\n", length);
|
2009-07-26 02:02:14 +00:00
|
|
|
u = (struct hub_user*) list_get_next(users);
|
|
|
|
}
|
2011-12-09 14:58:58 +00:00
|
|
|
str_append(buffer, "\n", length);
|
2009-07-26 02:02:14 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
send_message(cbase, user, buffer);
|
2009-07-26 02:02:14 +00:00
|
|
|
hub_free(buffer);
|
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-09 09:29:50 +00:00
|
|
|
static int command_broadcast(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-07-26 02:47:43 +00:00
|
|
|
{
|
2010-05-25 20:21:56 +00:00
|
|
|
size_t offset = 12;
|
|
|
|
#if USE_OLD_BROADCAST_STYLE
|
|
|
|
struct adc_message* command = adc_msg_construct(ADC_CMD_IMSG, strlen((cmd->message + offset)) + 6);
|
|
|
|
adc_msg_add_argument(command, (cmd->message + offset));
|
2009-07-26 02:47:43 +00:00
|
|
|
route_to_all(hub, command);
|
|
|
|
adc_msg_free(command);
|
|
|
|
return 0;
|
2010-05-25 20:21:56 +00:00
|
|
|
#else
|
|
|
|
size_t message_len = strlen(cmd->message + offset);
|
|
|
|
struct adc_message* command = 0;
|
|
|
|
char pm_flag[7] = "PM";
|
|
|
|
char from_sid[5];
|
2010-05-26 07:38:14 +00:00
|
|
|
char buffer[128];
|
|
|
|
size_t recipients = 0;
|
2011-01-02 01:39:25 +00:00
|
|
|
struct hub_user* target;
|
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));
|
|
|
|
adc_msg_add_argument(command, (cmd->message + offset));
|
|
|
|
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
|
|
|
|
|
|
|
snprintf(buffer, sizeof(buffer), "*** %s: Delivered to " PRINTF_SIZE_T " user%s", cmd->prefix, recipients, (recipients != 1 ? "s" : ""));
|
2011-12-09 09:29:50 +00:00
|
|
|
send_message(cbase, user, buffer);
|
2010-05-25 20:21:56 +00:00
|
|
|
return 0;
|
|
|
|
#endif
|
2009-07-26 02:47:43 +00:00
|
|
|
}
|
2009-07-26 02:02:14 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_history(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-07-26 03:23:56 +00:00
|
|
|
{
|
|
|
|
char* buffer;
|
2011-12-09 09:29:50 +00:00
|
|
|
struct linked_list* messages = cbase->hub->chat_history;
|
2009-07-26 03:23:56 +00:00
|
|
|
char* message = 0;
|
|
|
|
int ret = (int) list_size(messages);
|
|
|
|
size_t bufsize;
|
2010-01-28 00:06:41 +00:00
|
|
|
char tmp[128];
|
2010-11-12 17:14:15 +00:00
|
|
|
char* maxlines_str = list_get_first(cmd->args);
|
|
|
|
int maxlines = 0;
|
|
|
|
int lines = 0;
|
2009-07-26 03:23:56 +00:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, "No messages.");
|
2009-07-26 03:23:56 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 17:14:15 +00:00
|
|
|
if (maxlines_str)
|
|
|
|
maxlines = uhub_atoi(maxlines_str);
|
|
|
|
|
|
|
|
if (maxlines <= 0 || maxlines > ret)
|
|
|
|
maxlines = ret;
|
|
|
|
|
|
|
|
if (maxlines != ret)
|
|
|
|
{
|
|
|
|
lines = ret - maxlines;
|
|
|
|
snprintf(tmp, 128, "*** %s: Displaying %d of %d message%s:", cmd->prefix, maxlines, ret, ((ret != 1) ? "s" : ""));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(tmp, 128, "*** %s: Found %d message%s:", cmd->prefix, ret, ((ret != 1) ? "s" : ""));
|
|
|
|
}
|
|
|
|
|
2009-07-26 03:23:56 +00:00
|
|
|
bufsize = strlen(tmp);
|
|
|
|
message = (char*) list_get_first(messages);
|
|
|
|
while (message)
|
|
|
|
{
|
|
|
|
bufsize += strlen(message);
|
|
|
|
message = (char*) list_get_next(messages);
|
|
|
|
}
|
|
|
|
|
2011-12-09 14:58:58 +00:00
|
|
|
bufsize += 4;
|
|
|
|
buffer = hub_malloc(bufsize);
|
2009-07-26 04:03:43 +00:00
|
|
|
if (!buffer)
|
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, "Not enough memory.");
|
2009-07-26 04:03:43 +00:00
|
|
|
}
|
|
|
|
|
2009-07-26 03:23:56 +00:00
|
|
|
buffer[0] = 0;
|
2011-12-09 14:58:58 +00:00
|
|
|
str_append(buffer, tmp, bufsize);
|
|
|
|
str_append(buffer, "\n", bufsize);
|
2009-07-26 03:23:56 +00:00
|
|
|
|
|
|
|
message = (char*) list_get_first(messages);
|
|
|
|
while (message)
|
|
|
|
{
|
2010-11-12 17:14:15 +00:00
|
|
|
if (--lines < 0)
|
2011-12-09 14:58:58 +00:00
|
|
|
str_append(buffer, message, bufsize);
|
2009-07-26 03:23:56 +00:00
|
|
|
message = (char*) list_get_next(messages);
|
|
|
|
}
|
2011-12-09 14:58:58 +00:00
|
|
|
str_append(buffer, "\n", bufsize);
|
2009-07-26 03:23:56 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
send_message(cbase, user, buffer);
|
2009-07-26 03:23:56 +00:00
|
|
|
hub_free(buffer);
|
2009-08-02 19:58:12 +00:00
|
|
|
return 0;
|
2009-07-26 03:23:56 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_log(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-07-26 04:03:43 +00:00
|
|
|
{
|
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;
|
|
|
|
char tmp[1024];
|
2009-07-28 00:02:19 +00:00
|
|
|
char* search = 0;
|
|
|
|
size_t search_len = 0;
|
|
|
|
size_t search_hits = 0;
|
2009-07-26 04:03:43 +00:00
|
|
|
|
|
|
|
if (!list_size(messages))
|
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, "No entries logged.");
|
2009-07-26 04:03:43 +00:00
|
|
|
}
|
|
|
|
|
2009-07-28 00:02:19 +00:00
|
|
|
search = list_get_first(cmd->args);
|
|
|
|
if (search)
|
2009-07-26 04:03:43 +00:00
|
|
|
{
|
2009-07-28 00:02:19 +00:00
|
|
|
search_len = strlen(search);
|
2009-07-26 04:03:43 +00:00
|
|
|
}
|
|
|
|
|
2009-07-28 00:02:19 +00:00
|
|
|
if (search_len)
|
|
|
|
{
|
2011-11-30 11:19:37 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "Logged entries: " PRINTF_SIZE_T ", searching for \"%s\"", list_size(messages), search);
|
2009-07-28 00:02:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-30 11:19:37 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "Logged entries: " PRINTF_SIZE_T, list_size(messages));
|
2009-07-28 00:02:19 +00:00
|
|
|
}
|
2011-12-09 09:29:50 +00:00
|
|
|
command_status(cbase, user, cmd, tmp);
|
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-11-30 11:19:37 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "* %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));
|
2011-12-09 09:29:50 +00:00
|
|
|
send_message(cbase, user, tmp);
|
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-11-30 11:19:37 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), PRINTF_SIZE_T " entries shown.", search_hits);
|
2011-12-09 09:29:50 +00:00
|
|
|
command_status(cbase, user, cmd, tmp);
|
2009-07-28 00:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-07-26 04:03:43 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_register(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
|
|
|
struct auth_info data;
|
|
|
|
char tmp[1024];
|
|
|
|
char* password = list_get_first(cmd->args);
|
|
|
|
|
|
|
|
strncpy(data.nickname, user->id.nick, MAX_NICK_LEN);
|
|
|
|
strncpy(data.password, password, MAX_PASS_LEN);
|
|
|
|
data.nickname[MAX_NICK_LEN] = '\0';
|
|
|
|
data.password[MAX_PASS_LEN] = '\0';
|
|
|
|
data.credentials = auth_cred_user;
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
if (acl_register_user(cbase->hub, &data))
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "User \"%s\" registered.", user->id.nick);
|
|
|
|
return command_status(cbase, user, cmd, tmp);
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "Unable to register user \"%s\".", user->id.nick);
|
|
|
|
return command_status(cbase, user, cmd, tmp);
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_password(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
|
|
|
struct auth_info data;
|
|
|
|
char tmp[1024];
|
|
|
|
char* password = list_get_first(cmd->args);
|
|
|
|
|
|
|
|
strncpy(data.nickname, user->id.nick, MAX_NICK_LEN);
|
|
|
|
strncpy(data.password, password, MAX_PASS_LEN);
|
|
|
|
data.nickname[MAX_NICK_LEN] = '\0';
|
|
|
|
data.password[MAX_PASS_LEN] = '\0';
|
|
|
|
data.credentials = user->credentials;
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
if (acl_update_user(cbase->hub, &data))
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, "Password changed.");
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-30 11:19:37 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "Unable to change password for user \"%s\".", user->id.nick);
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, tmp);
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_useradd(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
|
|
|
struct auth_info data;
|
|
|
|
char tmp[1024];
|
|
|
|
char* nick = list_get_first(cmd->args);
|
|
|
|
char* pass = list_get_next(cmd->args);
|
|
|
|
char* cred = list_get_next(cmd->args);
|
|
|
|
enum auth_credentials credentials;
|
|
|
|
|
|
|
|
if (!(cred && auth_string_to_cred(cred, &credentials)))
|
|
|
|
{
|
|
|
|
credentials = auth_cred_user;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(data.nickname, nick, MAX_NICK_LEN);
|
|
|
|
strncpy(data.password, pass, MAX_PASS_LEN);
|
|
|
|
data.nickname[MAX_NICK_LEN] = '\0';
|
|
|
|
data.password[MAX_PASS_LEN] = '\0';
|
|
|
|
data.credentials = credentials;
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
if (acl_register_user(cbase->hub, &data))
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
2011-11-30 11:19:37 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "User \"%s\" registered.", nick);
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, tmp);
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-30 11:19:37 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "Unable to register user \"%s\".", nick);
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, tmp);
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_userdel(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
|
|
|
char tmp[1024];
|
|
|
|
char* nick = list_get_first(cmd->args);
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
if (acl_delete_user(cbase->hub, nick))
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
2011-11-30 11:19:37 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "User \"%s\" is deleted.", nick);
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, tmp);
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-30 11:19:37 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "Unable to delete user \"%s\".", nick);
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, tmp);
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_usermod(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, "Not implemented!");
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_userinfo(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, "Not implemented!");
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_userpass(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2010-11-12 17:14:15 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
return command_status(cbase, user, cmd, "Not implemented!");
|
2010-11-12 17:14:15 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_rules(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2010-01-19 22:20:19 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
if (!hub_send_rules(cbase->hub, user))
|
|
|
|
return command_status(cbase, user, cmd, "no rules defined.");
|
2010-01-19 22:20:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_motd(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2010-01-19 22:20:19 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
if (!hub_send_motd(cbase->hub, user))
|
|
|
|
return command_status(cbase, user, cmd, "no motd defined.");
|
2010-01-19 22:20:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-21 23:18:51 +00:00
|
|
|
#ifdef CRASH_DEBUG
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_crash(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-06-21 23:18:51 +00:00
|
|
|
{
|
|
|
|
void (*crash)(void) = NULL;
|
|
|
|
crash();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
static int command_stats(struct command_base* cbase, struct hub_user* user, struct command_handle* handle, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
char temp[128];
|
|
|
|
struct hub_info* hub = cbase->hub;
|
2009-06-29 21:22:13 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
snprintf(temp, 128, 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, temp);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
ADD_COMMAND("ban", 3, "n", auth_cred_operator, command_ban, "Ban a user" );
|
|
|
|
ADD_COMMAND("broadcast", 9, "m", auth_cred_operator, command_broadcast,"Send a message to all users" );
|
|
|
|
#ifdef CRASH_DEBUG
|
|
|
|
ADD_COMMAND("crash", 5, 0, auth_cred_admin, command_crash, "Crash the hub (DEBUG)." );
|
|
|
|
#endif
|
|
|
|
ADD_COMMAND("getip", 5, "n", auth_cred_operator, command_getip, "Show IP address for a user" );
|
|
|
|
ADD_COMMAND("help", 4, "?c",auth_cred_guest, command_help, "Show this help message." );
|
|
|
|
ADD_COMMAND("history", 7, "?N",auth_cred_guest, command_history, "Show the last chat messages." );
|
|
|
|
ADD_COMMAND("kick", 4, "n", auth_cred_operator, command_kick, "Kick a user" );
|
|
|
|
ADD_COMMAND("log", 3, 0, auth_cred_operator, command_log, "Display log" );
|
|
|
|
ADD_COMMAND("motd", 4, 0, auth_cred_guest, command_motd, "Show the message of the day" );
|
|
|
|
ADD_COMMAND("mute", 4, "n", auth_cred_operator, command_mute, "Mute user" );
|
|
|
|
ADD_COMMAND("myip", 4, 0, auth_cred_guest, command_myip, "Show your own IP." );
|
|
|
|
ADD_COMMAND("register", 8, "p", auth_cred_guest, command_register, "Register your username." );
|
|
|
|
ADD_COMMAND("reload", 6, 0, auth_cred_admin, command_reload, "Reload configuration files." );
|
|
|
|
ADD_COMMAND("rules", 5, 0, auth_cred_guest, command_rules, "Show the hub rules" );
|
|
|
|
ADD_COMMAND("password", 8, "p", auth_cred_user, command_password, "Change your own password." );
|
|
|
|
ADD_COMMAND("shutdown", 8, 0, auth_cred_admin, command_shutdown_hub, "Shutdown hub." );
|
|
|
|
ADD_COMMAND("stats", 5, 0, auth_cred_super, command_stats, "Show hub statistics." );
|
|
|
|
ADD_COMMAND("unban", 5, "n", auth_cred_operator, command_unban, "Lift ban on a user" );
|
|
|
|
ADD_COMMAND("unmute", 6, "n", auth_cred_operator, command_mute, "Unmute user" );
|
|
|
|
ADD_COMMAND("uptime", 6, 0, auth_cred_guest, command_uptime, "Display hub uptime info." );
|
|
|
|
ADD_COMMAND("useradd", 7, "np",auth_cred_operator, command_useradd, "Register a new user." );
|
|
|
|
ADD_COMMAND("userdel", 7, "n", auth_cred_operator, command_userdel, "Delete a registered user." );
|
|
|
|
ADD_COMMAND("userinfo", 8, "n", auth_cred_operator, command_userinfo, "Show registered user info." );
|
|
|
|
ADD_COMMAND("usermod", 7, "nC",auth_cred_admin, command_usermod, "Modify user credentials." );
|
|
|
|
ADD_COMMAND("userpass", 8, "np",auth_cred_operator, command_userpass, "Change password for a user." );
|
|
|
|
ADD_COMMAND("version", 7, 0, auth_cred_guest, command_version, "Show hub version info." );
|
|
|
|
ADD_COMMAND("whoip", 5, "a", auth_cred_operator, command_whoip, "Show users matching IP range" );
|
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
|
|
|
}
|
|
|
|
}
|