Cleaned up command system somewhat. Added admin commands for reload and shutdown.

This commit is contained in:
Jan Vidar Krey 2009-03-19 00:55:11 +01:00
parent e10582fec2
commit 3bb563b432
1 changed files with 53 additions and 86 deletions

View File

@ -19,28 +19,29 @@
#include "uhub.h"
static int command_access_denied(struct user* user)
static void send_message(struct user* user, const char* message)
{
struct adc_message* command;
char* buffer = adc_msg_escape("Access denied.");
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
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(user, command);
adc_msg_free(command);
hub_free(buffer);
}
static int command_access_denied(struct user* user)
{
send_message(user, "*** Access denied.");
return 0;
}
static int command_stats(struct user* user, const char* message)
{
struct adc_message* command;
if (user->credentials < cred_super)
return command_access_denied(user);
char temp[128];
snprintf(temp, 128, "*** Stats: %zu users, peak: %zu. Network (up/down): %d/%d KB/s, peak: %d/%d KB/s",
user->hub->users->count,
user->hub->users->count_peak,
@ -49,39 +50,28 @@ static int command_stats(struct user* user, const char* message)
(int) user->hub->stats.net_tx_peak / 1024,
(int) user->hub->stats.net_rx_peak / 1024);
char* buffer = adc_msg_escape(temp);
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
adc_msg_add_argument(command, buffer);
route_to_user(user, command);
adc_msg_free(command);
hub_free(buffer);
send_message(user, temp);
return 0;
}
static int command_help(struct user* user, const char* message)
{
struct adc_message* command;
char* buffer = adc_msg_escape("\n"
send_message(user, "\n"
"*** Available commands:\n"
"!help - Show this help message\n"
"!stats - Show hub stats (super)\n"
"!version - Show this help message\n"
"!uptime - Display hub uptime\n"
"!kick <user> - Kick user (operator)\n"
"!reload - Reload configuration (admin)\n"
"!shutdown - Shutdown hub (admin)\n"
);
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
adc_msg_add_argument(command, buffer);
route_to_user(user, command);
adc_msg_free(command);
hub_free(buffer);
return 0;
}
static int command_uptime(struct user* user, const char* message)
{
struct adc_message* command;
char tmp[128];
size_t d;
size_t h;
@ -111,28 +101,16 @@ static int command_uptime(struct user* user, const char* message)
if (m < 10) strcat(tmp, "0");
strcat(tmp, uhub_itoa((int) m));
char* buffer = adc_msg_escape(tmp);
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
adc_msg_add_argument(command, buffer);
route_to_user(user, command);
adc_msg_free(command);
hub_free(buffer);
send_message(user, tmp);
return 0;
}
static int command_kick(struct user* user, const char* message)
{
struct adc_message* command;
if (user->credentials < cred_operator)
return command_access_denied(user);
char* buffer = adc_msg_escape("*** Kick not implemented!");
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
adc_msg_add_argument(command, buffer);
route_to_user(user, command);
adc_msg_free(command);
hub_free(buffer);
send_message(user, "*** Kick not implemented!");
return 0;
}
@ -141,6 +119,7 @@ static int command_reload(struct user* user, const char* message)
if (user->credentials < cred_admin)
return command_access_denied(user);
send_message(user, "*** Reloading configuration");
user->hub->status = hub_status_restart;
return 0;
}
@ -150,6 +129,7 @@ static int command_shutdown(struct user* user, const char* message)
if (user->credentials < cred_admin)
return command_access_denied(user);
send_message(user, "*** Hub shuting down...");
user->hub->status = hub_status_shutdown;
return 0;
}
@ -157,32 +137,19 @@ static int command_shutdown(struct user* user, const char* message)
static int command_version(struct user* user, const char* message)
{
struct adc_message* command;
char* buffer = adc_msg_escape("*** Powered by " PRODUCT "/" VERSION);
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
adc_msg_add_argument(command, buffer);
route_to_user(user, command);
adc_msg_free(command);
hub_free(buffer);
send_message(user, "*** Powered by " PRODUCT "/" VERSION);
return 0;
}
static int command_myip(struct user* user, const char* message)
{
struct adc_message* command;
char tmp[128];
char* buffer;
tmp[0] = 0;
strcat(tmp, "*** Your IP: ");
strcat(tmp, ip_convert_to_string(&user->ipaddr));
buffer = adc_msg_escape(tmp);
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
adc_msg_add_argument(command, buffer);
route_to_user(user, command);
adc_msg_free(command);
hub_free(buffer);
send_message(user, tmp);
return 0;
}