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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
#include "commandHandler.hpp"
#include "log.hpp"
#include "session.hpp"
#include <algorithm>
#include <memory>
CCommandHandler::CCommandHandler() {
log(LOG, "CommandHandler: initialized");
}
CCommandHandler::~CCommandHandler() {
log(SYS, "CommandHandler: bye");
m_vCommands.clear();
}
bool CCommandHandler::isCommand(const std::string &str) const {
return !str.empty() && str.front() == '/';
};
bool CCommandHandler::validCommand(const std::string &command) const {
return std::ranges::any_of(m_vCommands, [&command](const std::shared_ptr<SCommand> &cmd) {
return cmd->name == command;
});
}
bool CCommandHandler::registerCommand(const SCommand &command) {
if (command.name.empty() || !command.exe || !command.parser)
return false;
m_vCommands.push_back(std::make_shared<SCommand>(command));
log(TRACE, "CommandHandler: registered command: {}", command.name);
return true;
}
std::shared_ptr<CCommandHandler::SCommand> CCommandHandler::getCommand(const std::string &command) const {
auto it = std::ranges::find_if(m_vCommands, [&command](const std::shared_ptr<SCommand> &cmd) { return cmd->name == command; });
if (it != m_vCommands.end())
return *it;
return nullptr;
}
CCommandHandler::SResult CCommandHandler::exeCommand(const std::shared_ptr<SCommand> command, const std::string &args) const {
const auto [parsed, good] = command->parser(args);
if (!good)
try {
return {.result = std::any_cast<std::string>(parsed), .good = false};
} catch (const std::bad_any_cast &) {
return {.result = "", .good = false};
}
log(TRACE, "CommandHandler: executing command: {}", command->name);
return command->exe(parsed);
}
CCommandHandler::SResult CCommandHandler::newCommand(const std::string &command, const std::string &args, std::shared_ptr<CSession> session) const {
if (!validCommand(command))
return {.result = "Invalid command", .good = false};
auto cmd = getCommand(command);
if (!cmd)
return {.result = "Command not found", .good = false};
if (cmd->requireAdmin && !session->isAdmin())
return {.result = "Permission denied", .good = false};
return exeCommand(cmd, args);
}
void CCommandHandler::handleCommand(std::string input, std::shared_ptr<CSession> user) const {
input = input.substr(1);
std::string command;
std::string args;
size_t spacePos = input.find(' ');
if (spacePos == std::string::npos) {
command = input;
args = "";
} else {
command = input.substr(0, spacePos);
// skip extra spaces
size_t startPos = input.find_first_not_of(' ', spacePos);
if (startPos != std::string::npos)
args = input.substr(startPos);
}
args.empty() ? log(LOG, "Spawning Command: {}", command) : log(LOG, "Spawning Command: {}, Args: {}", command, args);
const auto exe = newCommand(command, args, user);
std::function<void(eFormatType, const std::string &)> outputter = [](eFormatType type, const std::string &msg) { log(type, "{}", msg); };
if (!user->getIp().empty())
outputter = [user](eFormatType, const std::string &msg) { user->write(msg); };
exe.good ? exe.result.empty() ? outputter(LOG, "Command succeeded") : outputter(LOG, exe.result) : exe.result.empty() ? outputter(ERR, "Command failed")
: outputter(ERR, exe.result);
};
|