2009-02-19 16:14:09 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
2010-01-19 21:58:03 +00:00
|
|
|
* Copyright (C) 2007-2010, 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
|
|
|
|
|
2009-06-25 23:15:06 +00:00
|
|
|
struct hub_command
|
|
|
|
{
|
|
|
|
const char* message;
|
|
|
|
char* prefix;
|
|
|
|
size_t prefix_len;
|
|
|
|
struct linked_list* args;
|
|
|
|
};
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
typedef int (*command_handler)(struct hub_info* hub, struct hub_user* user, struct hub_command*);
|
2009-03-20 17:37:38 +00:00
|
|
|
|
|
|
|
struct commands_handler
|
|
|
|
{
|
|
|
|
const char* prefix;
|
|
|
|
size_t length;
|
2009-06-25 23:15:06 +00:00
|
|
|
const char* args;
|
2009-03-20 17:37:38 +00:00
|
|
|
enum user_credentials cred;
|
|
|
|
command_handler handler;
|
|
|
|
const char* description;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct commands_handler command_handlers[];
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct hub_command* command_create(const char* message)
|
|
|
|
{
|
2010-01-28 00:06:41 +00:00
|
|
|
char* prefix;
|
|
|
|
int n;
|
2009-06-25 23:15:06 +00:00
|
|
|
struct hub_command* cmd = hub_malloc_zero(sizeof(struct hub_command));
|
2010-01-28 00:06:41 +00:00
|
|
|
|
2009-06-25 23:15:06 +00:00
|
|
|
if (!cmd) return 0;
|
|
|
|
|
|
|
|
cmd->message = message;
|
|
|
|
cmd->args = list_create();
|
|
|
|
|
2010-01-28 00:06:41 +00:00
|
|
|
n = split_string(message, "\\s", cmd->args, 0);
|
2009-06-25 23:15:06 +00:00
|
|
|
if (n <= 0)
|
|
|
|
{
|
|
|
|
command_destroy(cmd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-28 00:06:41 +00:00
|
|
|
prefix = list_get_first(cmd->args);
|
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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
command_destroy(cmd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
list_remove(cmd->args, prefix);
|
|
|
|
hub_free(prefix);
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static void send_message(struct hub_info* hub, struct hub_user* user, const char* message)
|
2009-03-18 23:55:11 +00:00
|
|
|
{
|
|
|
|
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);
|
2009-05-18 14:30:17 +00:00
|
|
|
route_to_user(hub, user, command);
|
2009-03-18 23:55:11 +00:00
|
|
|
adc_msg_free(command);
|
|
|
|
hub_free(buffer);
|
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_access_denied(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-05-15 14:52:04 +00:00
|
|
|
char temp[128];
|
2009-06-29 21:22:13 +00:00
|
|
|
snprintf(temp, 128, "*** %s: Access denied.", cmd->prefix);
|
2009-05-15 16:45:26 +00:00
|
|
|
send_message(hub, user, temp);
|
2009-05-15 14:52:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_not_found(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
2009-05-15 14:52:04 +00:00
|
|
|
{
|
|
|
|
char temp[128];
|
2009-06-25 23:15:06 +00:00
|
|
|
snprintf(temp, 128, "*** %s: Command not found", cmd->prefix);
|
|
|
|
send_message(hub, user, temp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_status_user_not_found(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd, const char* nick)
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
|
|
|
char temp[128];
|
|
|
|
snprintf(temp, 128, "*** %s: No user \"%s\"", cmd->prefix, nick);
|
|
|
|
send_message(hub, user, temp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* command_get_syntax(struct commands_handler* handler)
|
|
|
|
{
|
|
|
|
static char args[128];
|
|
|
|
size_t n = 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++)
|
|
|
|
{
|
2009-06-30 09:48:58 +00:00
|
|
|
if (n > 0) strcat(args, " ");
|
2009-06-25 23:15:06 +00:00
|
|
|
switch (handler->args[n])
|
|
|
|
{
|
|
|
|
case 'n': strcat(args, "<nick>"); break;
|
2009-07-26 02:02:14 +00:00
|
|
|
case 'c': strcat(args, "<cid>"); break;
|
2009-06-25 23:15:06 +00:00
|
|
|
case 'a': strcat(args, "<addr>"); break;
|
2009-07-26 04:03:43 +00:00
|
|
|
case 'm': strcat(args, "<message>"); break;
|
2009-06-25 23:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_arg_mismatch(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd, struct commands_handler* handler)
|
2009-06-25 23:15:06 +00:00
|
|
|
{
|
|
|
|
char temp[256];
|
|
|
|
const char* args = command_get_syntax(handler);
|
|
|
|
if (args) snprintf(temp, 256, "*** %s: Use: !%s %s", cmd->prefix, cmd->prefix, args);
|
|
|
|
else snprintf(temp, 256, "*** %s: Use: !%s", cmd->prefix, cmd->prefix);
|
2009-05-15 16:45:26 +00:00
|
|
|
send_message(hub, user, temp);
|
2009-03-20 18:02:32 +00:00
|
|
|
return 0;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_status(struct hub_info* hub, 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);
|
2009-05-15 16:45:26 +00:00
|
|
|
send_message(hub, user, temp);
|
2009-05-15 14:52:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_stats(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-03-20 18:02:32 +00:00
|
|
|
char temp[128];
|
2009-08-20 08:44:44 +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",
|
2009-05-15 16:45:26 +00:00
|
|
|
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);
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status(hub, user, cmd, temp);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_help(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-03-20 17:37:38 +00:00
|
|
|
size_t n;
|
|
|
|
char msg[MAX_HELP_MSG];
|
|
|
|
msg[0] = 0;
|
2009-05-15 14:52:04 +00:00
|
|
|
strcat(msg, "Available commands:\n");
|
2009-06-30 09:48:58 +00:00
|
|
|
|
2009-03-20 17:37:38 +00:00
|
|
|
for (n = 0; command_handlers[n].prefix; n++)
|
|
|
|
{
|
|
|
|
if (command_handlers[n].cred <= user->credentials)
|
|
|
|
{
|
2009-05-15 14:52:04 +00:00
|
|
|
strcat(msg, "!");
|
2009-03-20 17:37:38 +00:00
|
|
|
strcat(msg, command_handlers[n].prefix);
|
|
|
|
strcat(msg, " - ");
|
|
|
|
strcat(msg, command_handlers[n].description);
|
|
|
|
strcat(msg, "\n");
|
|
|
|
}
|
|
|
|
}
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status(hub, user, cmd, msg);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_uptime(struct hub_info* hub, struct hub_user* user, 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;
|
2009-05-15 16:45:26 +00:00
|
|
|
size_t D = (size_t) difftime(time(0), 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;
|
|
|
|
|
|
|
|
tmp[0] = 0;
|
|
|
|
if (d)
|
|
|
|
{
|
2009-03-19 00:01:36 +00:00
|
|
|
strcat(tmp, uhub_itoa((int) d));
|
|
|
|
strcat(tmp, " day");
|
|
|
|
if (d != 1) strcat(tmp, "s");
|
|
|
|
strcat(tmp, ", ");
|
2009-03-18 23:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (h < 10) strcat(tmp, "0");
|
2009-05-15 14:52:04 +00:00
|
|
|
strcat(tmp, uhub_itoa((int) h));
|
2009-03-18 23:55:11 +00:00
|
|
|
strcat(tmp, ":");
|
|
|
|
if (m < 10) strcat(tmp, "0");
|
2009-05-15 14:52:04 +00:00
|
|
|
strcat(tmp, uhub_itoa((int) m));
|
2009-03-18 23:55:11 +00:00
|
|
|
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status(hub, user, cmd, tmp);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_kick(struct hub_info* hub, struct hub_user* user, 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.
|
|
|
|
|
2010-01-28 00:06:41 +00:00
|
|
|
target = uman_get_user_by_nick(hub, nick);
|
2009-04-08 22:48:00 +00:00
|
|
|
|
|
|
|
if (!target)
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status_user_not_found(hub, user, cmd, nick);
|
2009-04-08 22:48:00 +00:00
|
|
|
|
|
|
|
if (target == user)
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status(hub, user, cmd, "Cannot kick yourself");
|
2009-04-08 22:48:00 +00:00
|
|
|
|
2009-05-18 14:30:17 +00:00
|
|
|
hub_disconnect_user(hub, target, quit_kicked);
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status(hub, user, cmd, nick);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_ban(struct hub_info* hub, struct hub_user* user, 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.
|
|
|
|
|
2010-01-28 00:06:41 +00:00
|
|
|
target = uman_get_user_by_nick(hub, nick);
|
2009-06-23 21:16:09 +00:00
|
|
|
|
|
|
|
if (!target)
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status_user_not_found(hub, user, cmd, nick);
|
2009-06-23 21:16:09 +00:00
|
|
|
|
|
|
|
if (target == user)
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status(hub, user, cmd, "Cannot kick/ban yourself");
|
2009-06-23 21:16:09 +00:00
|
|
|
|
|
|
|
hub_disconnect_user(hub, target, quit_kicked);
|
|
|
|
acl_user_ban_nick(hub->acl, target->id.nick);
|
|
|
|
acl_user_ban_cid(hub->acl, target->id.cid);
|
|
|
|
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status(hub, user, cmd, nick);
|
2009-06-23 21:16:09 +00:00
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_unban(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
2009-06-23 21:16:09 +00:00
|
|
|
{
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status(hub, user, cmd, "Not implemented");
|
2009-06-23 21:16:09 +00:00
|
|
|
}
|
|
|
|
|
2010-01-19 21:58:03 +00:00
|
|
|
static int command_mute(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
|
2010-01-28 00:06:41 +00:00
|
|
|
target = uman_get_user_by_nick(hub, nick);
|
2010-01-19 21:58:03 +00:00
|
|
|
|
|
|
|
if (!target)
|
|
|
|
return command_status_user_not_found(hub, user, cmd, nick);
|
|
|
|
|
|
|
|
if (strlen(cmd->prefix) == 4)
|
|
|
|
{
|
|
|
|
user_flag_set(target, flag_muted);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
user_flag_unset(target, flag_muted);
|
|
|
|
}
|
|
|
|
return command_status(hub, user, cmd, nick);
|
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_reload(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
2009-03-18 23:53:44 +00:00
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
hub->status = hub_status_restart;
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status(hub, user, cmd, "Reloading configuration...");
|
2009-03-18 23:53:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_shutdown(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
2009-03-18 23:53:44 +00:00
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
hub->status = hub_status_shutdown;
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status(hub, user, cmd, "Hub shutting down...");
|
2009-03-18 23:53:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_version(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2010-02-11 00:19:58 +00:00
|
|
|
const char* tmp;
|
|
|
|
if (hub->config->show_banner_sys_info)
|
|
|
|
tmp = "Powered by " PRODUCT_STRING " on " OPSYS "/" CPUINFO;
|
|
|
|
else
|
|
|
|
tmp = "Powered by " PRODUCT_STRING;
|
|
|
|
return command_status(hub, user, cmd, tmp);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_myip(struct hub_info* hub, struct hub_user* user, 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));
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status(hub, user, cmd, tmp);
|
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_getip(struct hub_info* hub, struct hub_user* user, 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
|
|
|
|
|
2010-01-28 00:06:41 +00:00
|
|
|
target = uman_get_user_by_nick(hub, nick);
|
2009-06-25 23:15:06 +00:00
|
|
|
|
|
|
|
if (!target)
|
|
|
|
return command_status_user_not_found(hub, user, cmd, nick);
|
|
|
|
|
2010-04-05 14:35:55 +00:00
|
|
|
snprintf(tmp, 128, "%s has address \"%s\"", nick, user_get_address(target));
|
2009-06-25 23:15:06 +00:00
|
|
|
return command_status(hub, user, cmd, tmp);
|
2009-03-08 16:11:10 +00:00
|
|
|
}
|
|
|
|
|
2009-07-26 02:02:14 +00:00
|
|
|
static int command_whoip(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
|
|
|
{
|
|
|
|
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;
|
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)
|
|
|
|
return command_status(hub, user, cmd, "Invalid IP address/range/mask");
|
|
|
|
|
|
|
|
users = (struct linked_list*) list_create();
|
2009-11-18 16:41:28 +00:00
|
|
|
if (!users)
|
|
|
|
return -1; // FIXME: OOM
|
|
|
|
|
2009-07-26 02:02:14 +00:00
|
|
|
ret = uman_get_user_by_addr(hub, users, &range);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
list_destroy(users);
|
|
|
|
return command_status(hub, user, cmd, "No users found.");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-01-28 00:06:41 +00:00
|
|
|
buffer = hub_malloc(((MAX_NICK_LEN + INET6_ADDRSTRLEN + 5) * ret) + strlen(tmp) + 3);
|
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;
|
|
|
|
strcat(buffer, tmp);
|
|
|
|
strcat(buffer, "\n");
|
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)
|
|
|
|
{
|
|
|
|
strcat(buffer, u->id.nick);
|
2009-07-26 04:32:15 +00:00
|
|
|
strcat(buffer, " (");
|
2009-09-28 21:15:15 +00:00
|
|
|
strcat(buffer, user_get_address(u));
|
2009-07-26 04:30:48 +00:00
|
|
|
strcat(buffer, ")\n");
|
2009-07-26 02:02:14 +00:00
|
|
|
u = (struct hub_user*) list_get_next(users);
|
|
|
|
}
|
2009-07-26 02:47:43 +00:00
|
|
|
strcat(buffer, "\n");
|
2009-07-26 02:02:14 +00:00
|
|
|
|
2009-08-02 19:58:12 +00:00
|
|
|
send_message(hub, 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
|
|
|
}
|
|
|
|
|
2009-07-26 02:47:43 +00:00
|
|
|
static int command_broadcast(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
|
|
|
{
|
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];
|
|
|
|
|
|
|
|
memcpy(from_sid, sid_to_string(user->id.sid), sizeof(from_sid));
|
|
|
|
memcpy(pm_flag + 2, from_sid, sizeof(from_sid));
|
|
|
|
|
|
|
|
struct hub_user* target = (struct hub_user*) list_get_first(hub->users->list);
|
|
|
|
while (target)
|
|
|
|
{
|
|
|
|
command = adc_msg_construct(ADC_CMD_DMSG, message_len + 23);
|
|
|
|
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);
|
|
|
|
|
|
|
|
route_to_user(hub, target, command);
|
|
|
|
|
|
|
|
target = (struct hub_user*) list_get_next(hub->users->list);
|
|
|
|
adc_msg_free(command);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#endif
|
2009-07-26 02:47:43 +00:00
|
|
|
}
|
2009-07-26 02:02:14 +00:00
|
|
|
|
2009-07-26 03:23:56 +00:00
|
|
|
static int command_history(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
|
|
|
{
|
|
|
|
char* buffer;
|
|
|
|
struct linked_list* messages = hub->chat_history;
|
|
|
|
char* message = 0;
|
|
|
|
int ret = (int) list_size(messages);
|
|
|
|
size_t bufsize;
|
2010-01-28 00:06:41 +00:00
|
|
|
char tmp[128];
|
2009-07-26 03:23:56 +00:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
return command_status(hub, user, cmd, "No messages.");
|
|
|
|
}
|
|
|
|
|
2009-08-20 08:44:44 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = hub_malloc(bufsize+4);
|
2009-07-26 04:03:43 +00:00
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
return command_status(hub, user, cmd, "Not enough memory.");
|
|
|
|
}
|
|
|
|
|
2009-07-26 03:23:56 +00:00
|
|
|
buffer[0] = 0;
|
|
|
|
strcat(buffer, tmp);
|
|
|
|
strcat(buffer, "\n");
|
|
|
|
|
|
|
|
message = (char*) list_get_first(messages);
|
|
|
|
while (message)
|
|
|
|
{
|
|
|
|
strcat(buffer, message);
|
|
|
|
message = (char*) list_get_next(messages);
|
|
|
|
}
|
|
|
|
strcat(buffer, "\n");
|
|
|
|
|
2009-08-02 19:58:12 +00:00
|
|
|
send_message(hub, 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
|
|
|
}
|
|
|
|
|
2009-07-26 04:03:43 +00:00
|
|
|
static int command_log(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
|
|
|
{
|
|
|
|
struct linked_list* messages = hub->logout_info;
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
return command_status(hub, user, cmd, "No entries logged.");
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2009-08-20 08:44:44 +00:00
|
|
|
sprintf(tmp, "Logged entries: " PRINTF_SIZE_T ", searching for \"%s\"", list_size(messages), search);
|
2009-07-28 00:02:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-20 08:44:44 +00:00
|
|
|
sprintf(tmp, "Logged entries: " PRINTF_SIZE_T, list_size(messages));
|
2009-07-28 00:02:19 +00:00
|
|
|
}
|
2009-07-28 00:17:57 +00:00
|
|
|
command_status(hub, 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)
|
|
|
|
{
|
2009-08-02 20:53:25 +00:00
|
|
|
sprintf(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));
|
2009-07-28 00:02:19 +00:00
|
|
|
send_message(hub, user, tmp);
|
|
|
|
}
|
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)
|
|
|
|
{
|
2009-08-20 08:44:44 +00:00
|
|
|
sprintf(tmp, PRINTF_SIZE_T " entries shown.", search_hits);
|
2009-07-28 00:17:57 +00:00
|
|
|
command_status(hub, user, cmd, tmp);
|
2009-07-28 00:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-07-26 04:03:43 +00:00
|
|
|
}
|
|
|
|
|
2010-01-19 22:20:19 +00:00
|
|
|
static int command_rules(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
|
|
|
{
|
|
|
|
if (!hub_send_rules(hub, user))
|
|
|
|
return command_status(hub, user, cmd, "no rules defined.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int command_motd(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
|
|
|
{
|
|
|
|
if (!hub_send_motd(hub, user))
|
|
|
|
return command_status(hub, user, cmd, "no motd defined.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-21 23:18:51 +00:00
|
|
|
#ifdef CRASH_DEBUG
|
2009-07-25 23:47:17 +00:00
|
|
|
static int command_crash(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
2009-06-21 23:18:51 +00:00
|
|
|
{
|
|
|
|
void (*crash)(void) = NULL;
|
|
|
|
crash();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
int command_dipatcher(struct hub_info* hub, struct hub_user* user, const char* message)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-03-20 17:37:38 +00:00
|
|
|
size_t n = 0;
|
2009-06-25 23:15:06 +00:00
|
|
|
int rc;
|
2009-06-29 21:22:13 +00:00
|
|
|
|
|
|
|
/* Parse and validate the command */
|
2009-06-25 23:15:06 +00:00
|
|
|
struct hub_command* cmd = command_create(message);
|
2009-10-06 16:02:37 +00:00
|
|
|
if (!cmd) return 0;
|
2009-06-25 23:15:06 +00:00
|
|
|
|
2009-03-20 17:37:38 +00:00
|
|
|
for (n = 0; command_handlers[n].prefix; n++)
|
|
|
|
{
|
2009-06-25 23:15:06 +00:00
|
|
|
struct commands_handler* handler = &command_handlers[n];
|
|
|
|
if (cmd->prefix_len != handler->length)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!strncmp(cmd->prefix, handler->prefix, handler->length))
|
2009-03-20 17:37:38 +00:00
|
|
|
{
|
2009-06-25 23:15:06 +00:00
|
|
|
if (handler->cred <= user->credentials)
|
2009-03-20 17:37:38 +00:00
|
|
|
{
|
2009-06-25 23:15:06 +00:00
|
|
|
if (!handler->args || (handler->args && list_size(cmd->args) >= strlen(handler->args)))
|
|
|
|
{
|
|
|
|
rc = handler->handler(hub, user, cmd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rc = command_arg_mismatch(hub, user, cmd, handler);
|
|
|
|
}
|
|
|
|
command_destroy(cmd);
|
|
|
|
return rc;
|
2009-03-20 17:37:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-25 23:15:06 +00:00
|
|
|
rc = command_access_denied(hub, user, cmd);
|
|
|
|
command_destroy(cmd);
|
|
|
|
return rc;
|
2009-03-20 17:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-15 14:52:04 +00:00
|
|
|
|
2009-06-25 23:15:06 +00:00
|
|
|
command_not_found(hub, user, cmd);
|
|
|
|
command_destroy(cmd);
|
2009-10-06 16:02:37 +00:00
|
|
|
return 0;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-03-20 17:37:38 +00:00
|
|
|
static struct commands_handler command_handlers[] = {
|
2010-01-19 23:24:57 +00:00
|
|
|
{ "ban", 3, "n", cred_operator, command_ban, "Ban a user" },
|
|
|
|
{ "broadcast", 9, "m", cred_operator, command_broadcast,"Send a message to all users" },
|
|
|
|
#ifdef CRASH_DEBUG
|
|
|
|
{ "crash", 5, 0, cred_admin, command_crash, "Crash the hub (DEBUG)." },
|
|
|
|
#endif
|
|
|
|
{ "getip", 5, "n", cred_operator, command_getip, "Show IP address for a user" },
|
2009-06-25 23:15:06 +00:00
|
|
|
{ "help", 4, 0, cred_guest, command_help, "Show this help message." },
|
2009-08-02 17:26:21 +00:00
|
|
|
{ "history", 7, 0, cred_guest, command_history, "Show the last chat messages." },
|
2009-06-25 23:15:06 +00:00
|
|
|
{ "kick", 4, "n", cred_operator, command_kick, "Kick a user" },
|
2009-07-26 04:03:43 +00:00
|
|
|
{ "log", 3, 0, cred_operator, command_log, "Display log" },
|
2010-01-19 23:24:57 +00:00
|
|
|
{ "motd", 4, 0, cred_guest, command_motd, "Show the message of the day" },
|
2010-01-19 21:58:03 +00:00
|
|
|
{ "mute", 4, "n", cred_operator, command_mute, "Mute user" },
|
2010-01-19 23:24:57 +00:00
|
|
|
{ "myip", 4, 0, cred_guest, command_myip, "Show your own IP." },
|
|
|
|
{ "reload", 6, 0, cred_admin, command_reload, "Reload configuration files." },
|
2010-01-19 22:20:19 +00:00
|
|
|
{ "rules", 5, 0, cred_guest, command_rules, "Show the hub rules" },
|
2010-01-19 23:24:57 +00:00
|
|
|
{ "shutdown", 8, 0, cred_admin, command_shutdown, "Shutdown hub." },
|
|
|
|
{ "stats", 5, 0, cred_super, command_stats, "Show hub statistics." },
|
|
|
|
{ "unban", 5, "n", cred_operator, command_unban, "Lift ban on a user" },
|
|
|
|
{ "unmute", 6, "n", cred_operator, command_mute, "Unmute user" },
|
|
|
|
{ "uptime", 6, 0, cred_guest, command_uptime, "Display hub uptime info." },
|
|
|
|
{ "version", 7, 0, cred_guest, command_version, "Show hub version info." },
|
|
|
|
{ "whoip", 5, "a", cred_operator, command_whoip, "Show users matching IP range" },
|
2009-06-25 23:15:06 +00:00
|
|
|
{ 0, 0, 0, cred_none, command_help, "" }
|
2009-03-20 17:37:38 +00:00
|
|
|
};
|
|
|
|
|