2009-02-19 16:14:09 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
|
|
|
* Copyright (C) 2007-2009, Jan Vidar Krey
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uhub.h"
|
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
typedef int (*command_handler)(struct hub_info* hub, struct user* user, const char* message);
|
2009-03-20 17:37:38 +00:00
|
|
|
|
|
|
|
struct commands_handler
|
|
|
|
{
|
|
|
|
const char* prefix;
|
|
|
|
size_t length;
|
2009-06-23 21:16:09 +00:00
|
|
|
size_t 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-05-15 16:45:26 +00:00
|
|
|
static void send_message(struct hub_info* hub, struct 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-05-15 16:45:26 +00:00
|
|
|
static int command_access_denied(struct hub_info* hub, struct user* user, const char* command)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-05-15 14:52:04 +00:00
|
|
|
char temp[128];
|
|
|
|
snprintf(temp, 128, "*** Access denied: \"%s\"", command);
|
2009-05-15 16:45:26 +00:00
|
|
|
send_message(hub, user, temp);
|
2009-05-15 14:52:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
static int command_not_found(struct hub_info* hub, struct user* user, const char* command)
|
2009-05-15 14:52:04 +00:00
|
|
|
{
|
|
|
|
char temp[128];
|
|
|
|
snprintf(temp, 128, "*** Command not found: \"%s\"", command);
|
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-05-15 16:45:26 +00:00
|
|
|
static int command_status(struct hub_info* hub, struct user* user, const char* command, const char* message)
|
2009-05-15 14:52:04 +00:00
|
|
|
{
|
|
|
|
char temp[1024];
|
|
|
|
snprintf(temp, 1024, "*** %s: %s", command, 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-05-15 16:45:26 +00:00
|
|
|
static int command_stats(struct hub_info* hub, struct user* user, const char* message)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-03-20 18:02:32 +00:00
|
|
|
char temp[128];
|
2009-05-15 14:52:04 +00:00
|
|
|
snprintf(temp, 128, "%zu users, peak: %zu. 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-03-20 18:02:32 +00:00
|
|
|
|
2009-06-17 23:46:17 +00:00
|
|
|
return command_status(hub, user, "stats", temp);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
static int command_help(struct hub_info* hub, struct user* user, const char* message)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-03-20 17:37:38 +00:00
|
|
|
#define MAX_HELP_MSG 1024
|
|
|
|
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-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-05-15 16:45:26 +00:00
|
|
|
return command_status(hub, user, "help", msg);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
static int command_uptime(struct hub_info* hub, struct user* user, const char* message)
|
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-05-15 16:45:26 +00:00
|
|
|
return command_status(hub, user, "uptime", tmp);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
static int command_kick(struct hub_info* hub, struct user* user, const char* message)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-04-08 22:48:00 +00:00
|
|
|
if (strlen(message) < 7)
|
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
return command_status(hub, user, "kick", "No nickname given");
|
2009-04-08 22:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* nick = &message[7];
|
2009-05-16 10:42:30 +00:00
|
|
|
struct user* target = uman_get_user_by_nick(hub, nick);
|
2009-04-08 22:48:00 +00:00
|
|
|
|
|
|
|
if (!target)
|
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
return command_status(hub, user, "kick", "No such user");
|
2009-04-08 22:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (target == user)
|
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
return command_status(hub, user, "kick", "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-05-15 16:45:26 +00:00
|
|
|
return command_status(hub, user, "kick", nick);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-06-23 21:16:09 +00:00
|
|
|
static int command_ban(struct hub_info* hub, struct user* user, const char* message)
|
|
|
|
{
|
|
|
|
if (strlen(message) < 6)
|
|
|
|
{
|
|
|
|
return command_status(hub, user, "ban", "No nickname given");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* nick = &message[6];
|
|
|
|
struct user* target = uman_get_user_by_nick(hub, nick);
|
|
|
|
|
|
|
|
if (!target)
|
|
|
|
{
|
|
|
|
return command_status(hub, user, "ban", "No such user");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target == user)
|
|
|
|
{
|
|
|
|
return command_status(hub, user, "ban", "Cannot kick/ban yourself");
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return command_status(hub, user, "ban", nick);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int command_unban(struct hub_info* hub, struct user* user, const char* message)
|
|
|
|
{
|
|
|
|
if (strlen(message) < 8)
|
|
|
|
{
|
|
|
|
return command_status(hub, user, "unban", "No nickname given");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* nick = &message[8];
|
|
|
|
struct user* target = uman_get_user_by_nick(hub, nick);
|
|
|
|
|
|
|
|
if (!target)
|
|
|
|
{
|
|
|
|
return command_status(hub, user, "unban", "No such user");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target == user)
|
|
|
|
{
|
|
|
|
return command_status(hub, user, "unban", "Cannot unban yourself");
|
|
|
|
}
|
|
|
|
|
|
|
|
return command_status(hub, user, "unban", "Not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
static int command_reload(struct hub_info* hub, struct user* user, const char* message)
|
2009-03-18 23:53:44 +00:00
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
hub->status = hub_status_restart;
|
|
|
|
return command_status(hub, user, "reload", "Reloading configuration...");
|
2009-03-18 23:53:44 +00:00
|
|
|
}
|
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
static int command_shutdown(struct hub_info* hub, struct user* user, const char* message)
|
2009-03-18 23:53:44 +00:00
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
hub->status = hub_status_shutdown;
|
|
|
|
return command_status(hub, user, "shutdown", "Hub shutting down...");
|
2009-03-18 23:53:44 +00:00
|
|
|
}
|
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
static int command_version(struct hub_info* hub, struct user* user, const char* message)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
return command_status(hub, user, "version", "Powered by " PRODUCT "/" VERSION);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
static int command_myip(struct hub_info* hub, struct user* user, const char* message)
|
2009-03-08 16:11:10 +00:00
|
|
|
{
|
|
|
|
char tmp[128];
|
2009-05-26 17:46:51 +00:00
|
|
|
snprintf(tmp, 128, "Your IP is \"%s\"", ip_convert_to_string(&user->net.ipaddr));
|
2009-05-15 16:45:26 +00:00
|
|
|
return command_status(hub, user, "myip", tmp);
|
2009-03-08 16:11:10 +00:00
|
|
|
}
|
|
|
|
|
2009-06-21 23:18:51 +00:00
|
|
|
#ifdef CRASH_DEBUG
|
|
|
|
static int command_crash(struct hub_info* hub, struct user* user, const char* message)
|
|
|
|
{
|
|
|
|
void (*crash)(void) = NULL;
|
|
|
|
crash();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
int command_dipatcher(struct hub_info* hub, struct 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;
|
|
|
|
for (n = 0; command_handlers[n].prefix; n++)
|
|
|
|
{
|
2009-05-19 09:22:57 +00:00
|
|
|
if (!strncmp(&message[1], command_handlers[n].prefix, command_handlers[n].length))
|
2009-03-20 17:37:38 +00:00
|
|
|
{
|
|
|
|
if (command_handlers[n].cred <= user->credentials)
|
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
return command_handlers[n].handler(hub, user, message);
|
2009-03-20 17:37:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
return command_access_denied(hub, user, command_handlers[n].prefix);
|
2009-03-20 17:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-15 14:52:04 +00:00
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
command_not_found(hub, user, message);
|
2009-02-19 16:14:09 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-03-20 17:37:38 +00:00
|
|
|
static struct commands_handler command_handlers[] = {
|
2009-06-23 21:16:09 +00:00
|
|
|
{ "help", 4, 0, cred_guest, command_help, "Show this help message." },
|
|
|
|
{ "stats", 5, 0, cred_super, command_stats, "Show hub statistics." },
|
|
|
|
{ "version", 7, 0, cred_guest, command_version, "Show hub version info." },
|
|
|
|
{ "uptime", 6, 0, cred_guest, command_uptime, "Display hub uptime info." },
|
|
|
|
{ "kick", 4, 1, cred_operator, command_kick, "Kick a user" },
|
|
|
|
{ "ban", 3, 1, cred_operator, command_ban, "Ban a user" },
|
|
|
|
{ "unban", 5, 1, cred_operator, command_unban, "Lift ban on a user" },
|
|
|
|
{ "reload", 6, 0, cred_admin, command_reload, "Reload configuration files." },
|
|
|
|
{ "shutdown", 8, 0, cred_admin, command_shutdown, "Shutdown hub." },
|
|
|
|
{ "myip", 4, 0, cred_guest, command_myip, "Show your own IP." },
|
2009-06-21 23:18:51 +00:00
|
|
|
#ifdef CRASH_DEBUG
|
2009-06-23 21:16:09 +00:00
|
|
|
{ "crash", 5, 0, cred_admin, command_crash, "Crash the hub (DEBUG)." },
|
2009-06-21 23:18:51 +00:00
|
|
|
#endif
|
2009-06-23 21:16:09 +00:00
|
|
|
{ 0, 0, 0, cred_none, command_help, "" }
|
2009-03-20 17:37:38 +00:00
|
|
|
};
|
|
|
|
|