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

View File

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