-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandHandler.cpp
More file actions
64 lines (53 loc) · 1.51 KB
/
Copy pathCommandHandler.cpp
File metadata and controls
64 lines (53 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "CommandHandler.h"
#include "std_messages.h"
#include <cctype>
#include <optional>
#include "string_util.h"
CommandHandler::CommandHandler(ErrorHandler errorHandler) : errorHandler(errorHandler) {}
void CommandHandler::addCommand(std::string_view name, Command command, bool admin)
{
commands.emplace(name, std::pair{command, admin});
}
void CommandHandler::callCommand(std::string const& command, bool admin)
{
auto split_command = bracket_split(command);
auto it = commands.find(split_command[0]);
if (it != commands.end()) {
split_command.erase(split_command.begin()); //remove first arg
if (admin || !it->second.second) {
formatArgs(split_command);
it->second.first(std::move(split_command));
}
else {
errorHandler(INVALID_PERMS);
}
}
else {
errorHandler(INVALID_PERMS);
}
}
bool CommandHandler::isCommand(std::string const& command)
{
if (command.size() == 0) return false;
auto splited = bracket_split(command);
auto first_token = splited[0];
return commands.find(first_token) != commands.end();
}
void CommandHandler::formatArgs(std::vector<std::string>& args)
{
for (auto& arg : args) {
CommandHandler::formatArg(arg);
}
}
void CommandHandler::formatArg(std::string& arg)
{
size_t pos = arg.find('[');
if (pos != std::string::npos) {
if (pos == 0 && arg.back() == ']') {
arg = arg.substr(1, arg.size() - 2);
}
else if (isNumber(arg.substr(0, pos - 1))) {
arg = arg.substr(pos + 1, arg.size() - 2);
}
}
}