Cleanup command list, automatically generate help list depending on user credentials.
This commit is contained in:
parent
cf2994b570
commit
f8d5b72a89
|
@ -19,6 +19,19 @@
|
||||||
|
|
||||||
#include "uhub.h"
|
#include "uhub.h"
|
||||||
|
|
||||||
|
typedef int (*command_handler)(struct user* user, const char* message);
|
||||||
|
|
||||||
|
struct commands_handler
|
||||||
|
{
|
||||||
|
const char* prefix;
|
||||||
|
size_t length;
|
||||||
|
enum user_credentials cred;
|
||||||
|
command_handler handler;
|
||||||
|
const char* description;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct commands_handler command_handlers[];
|
||||||
|
|
||||||
static void send_message(struct user* user, const char* message)
|
static void send_message(struct user* user, const char* message)
|
||||||
{
|
{
|
||||||
char* buffer = adc_msg_escape(message);
|
char* buffer = adc_msg_escape(message);
|
||||||
|
@ -57,16 +70,23 @@ static int command_stats(struct user* user, const char* message)
|
||||||
|
|
||||||
static int command_help(struct user* user, const char* message)
|
static int command_help(struct user* user, const char* message)
|
||||||
{
|
{
|
||||||
send_message(user, "\n"
|
#define MAX_HELP_MSG 1024
|
||||||
"*** Available commands:\n"
|
size_t n;
|
||||||
"!help - Show this help message\n"
|
char msg[MAX_HELP_MSG];
|
||||||
"!stats - Show hub stats (super)\n"
|
msg[0] = 0;
|
||||||
"!version - Show hub version info\n"
|
strcat(msg, "\n*** Available commands:\n");
|
||||||
"!uptime - Display hub uptime\n"
|
|
||||||
"!kick <user> - Kick user (operator)\n"
|
for (n = 0; command_handlers[n].prefix; n++)
|
||||||
"!reload - Reload configuration (admin)\n"
|
{
|
||||||
"!shutdown - Shutdown hub (admin)\n"
|
if (command_handlers[n].cred <= user->credentials)
|
||||||
);
|
{
|
||||||
|
strcat(msg, command_handlers[n].prefix);
|
||||||
|
strcat(msg, " - ");
|
||||||
|
strcat(msg, command_handlers[n].description);
|
||||||
|
strcat(msg, "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
send_message(user, msg);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,19 +173,35 @@ static int command_myip(struct user* user, const char* message)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int command_dipatcher(struct user* user, const char* message)
|
int command_dipatcher(struct user* user, const char* message)
|
||||||
{
|
{
|
||||||
if (!strncmp(message, "!stats", 6)) command_stats(user, message);
|
size_t n = 0;
|
||||||
else if (!strncmp(message, "!help", 5)) command_help(user, message);
|
for (n = 0; command_handlers[n].prefix; n++)
|
||||||
else if (!strncmp(message, "!kick", 5)) command_kick(user, message);
|
{
|
||||||
else if (!strncmp(message, "!version", 8)) command_version(user, message);
|
if (!strncmp(message, command_handlers[n].prefix, command_handlers[n].length))
|
||||||
else if (!strncmp(message, "!uptime", 7)) command_uptime(user, message);
|
{
|
||||||
else if (!strncmp(message, "+myip", 5)) command_myip(user, message);
|
if (command_handlers[n].cred <= user->credentials)
|
||||||
else if (!strncmp(message, "!reload", 7)) command_reload(user, message);
|
{
|
||||||
else if (!strncmp(message, "!shutdown",9)) command_shutdown(user, message);
|
return command_handlers[n].handler(user, message);
|
||||||
else
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return command_access_denied(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return 1;
|
return 1;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct commands_handler command_handlers[] = {
|
||||||
|
{ "!help", 5, cred_guest, command_help, "Show this help message." },
|
||||||
|
{ "!stats", 6, cred_super, command_stats, "Show hub statistics." },
|
||||||
|
{ "!version", 8, cred_guest, command_version, "Show hub version info." },
|
||||||
|
{ "!uptime", 7, cred_guest, command_uptime, "Display hub uptime info." },
|
||||||
|
{ "!kick", 5, cred_operator, command_kick, "Kick a user" },
|
||||||
|
{ "!reload", 7, cred_admin, command_reload, "Reload configuration files." },
|
||||||
|
{ "!shutdown", 9, cred_admin, command_shutdown, "Shutdown hub." },
|
||||||
|
{ "+myip", 5, cred_guest, command_myip, "Show your own IP." },
|
||||||
|
{ 0, 0, cred_none, command_help, "{ Last dummy option }" }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue