Cleaned up command system somewhat. Added admin commands for reload and shutdown.
This commit is contained in:
parent
e10582fec2
commit
3bb563b432
@ -19,28 +19,29 @@
|
|||||||
|
|
||||||
#include "uhub.h"
|
#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(message);
|
||||||
char* buffer = adc_msg_escape("Access denied.");
|
struct adc_message* command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
|
||||||
command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
|
|
||||||
adc_msg_add_argument(command, buffer);
|
adc_msg_add_argument(command, buffer);
|
||||||
route_to_user(user, command);
|
route_to_user(user, command);
|
||||||
adc_msg_free(command);
|
adc_msg_free(command);
|
||||||
hub_free(buffer);
|
hub_free(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int command_access_denied(struct user* user)
|
||||||
|
{
|
||||||
|
send_message(user, "*** Access denied.");
|
||||||
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,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_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;
|
||||||
@ -111,28 +101,16 @@ static int command_uptime(struct user* user, const char* message)
|
|||||||
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);
|
|
||||||
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_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)
|
if (user->credentials < cred_operator)
|
||||||
return command_access_denied(user);
|
return command_access_denied(user);
|
||||||
|
|
||||||
char* buffer = adc_msg_escape("*** Kick not implemented!");
|
send_message(user, "*** 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user