diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-05-21 19:32:26 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-05-21 22:49:45 +0800 |
| commit | 554a0a88b805dbee8361908538e072302c3ebfa8 (patch) | |
| tree | 127a0cfb2cb99daba5cd40451479f77b3a5a4937 /src/commandHandler.cpp | |
| parent | 2957f25ce96c4e55f99b8a14e08b9e4a6b5c275d (diff) | |
feat: non-admin commands
Diffstat (limited to 'src/commandHandler.cpp')
| -rw-r--r-- | src/commandHandler.cpp | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/src/commandHandler.cpp b/src/commandHandler.cpp index 7c04f68..7ca559d 100644 --- a/src/commandHandler.cpp +++ b/src/commandHandler.cpp @@ -1,5 +1,6 @@ #include "commandHandler.hpp" #include "log.hpp" +#include "session.hpp" #include "sessionManager.hpp" #include <algorithm> @@ -29,6 +30,7 @@ bool CCommandHandler::registerCommand(const SCommand &command) { m_vCommands.push_back(command); log(TRACE, "CommandHandler: registered command: {}", command.name); + return true; } @@ -54,14 +56,18 @@ CCommandHandler::SResult CCommandHandler::exeCommand(const SCommand &command, co return command.exe(parsed); } -CCommandHandler::SResult CCommandHandler::newCommand(const std::string &command, const std::string &args) const { +CCommandHandler::SResult CCommandHandler::newCommand(const std::string &command, const std::string &args, CSession* const user) const { if (!validCommand(command)) return {.result = "Invalid command", .good = false}; - return exeCommand(*getCommand(command), args); + const auto cmd = getCommand(command); + if (cmd->requireAdmin && !user->isAdmin()) + return {.result = "Permission denied", .good = false}; + + return exeCommand(*cmd, args); } -void CCommandHandler::handleCommand(std::string input, const std::string &ip) const { +void CCommandHandler::handleCommand(std::string input, CSession* const user) const { input = input.substr(1); std::string command; @@ -80,13 +86,12 @@ void CCommandHandler::handleCommand(std::string input, const std::string &ip) co } args.empty() ? log(LOG, "Spawning Command: {}", command) : log(LOG, "Spawning Command: {}, Args: {}", command, args); - const auto exe = newCommand(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 (!ip.empty()) - if (auto session = g_pSessionManager->getByIp(ip)) - outputter = [session](eFormatType, const std::string &msg) { session->write(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); |
