diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-03-21 18:09:05 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-03-22 15:02:46 +0800 |
| commit | 3e19bf603af0ac01f6690afe7cf59702a4a5cc02 (patch) | |
| tree | b8ee33d721aa200364617239aab557639b5003c5 /src/commandHandler.cpp | |
| parent | 97d82139d2011a790c0d3d254add5d800de2742f (diff) | |
feat(commands): client execute commands (#8)
refactor(session): writeChat, unify written chat
internal: removed `CChatManager::broadcastMessage`, just use `g_pSessionManager->broadcastChat(msg)`
Diffstat (limited to 'src/commandHandler.cpp')
| -rw-r--r-- | src/commandHandler.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/commandHandler.cpp b/src/commandHandler.cpp index 12b51dc..712bbe2 100644 --- a/src/commandHandler.cpp +++ b/src/commandHandler.cpp @@ -1,5 +1,6 @@ #include "commandHandler.hpp" #include "log.hpp" +#include "sessionManager.hpp" #include <algorithm> CCommandHandler::CCommandHandler() { @@ -56,3 +57,34 @@ CCommandHandler::SResult CCommandHandler::newCommand(const std::string &command, return exeCommand(*getCommand(command), args); } + +void CCommandHandler::handleCommand(std::string input, const char ip[INET_ADDRSTRLEN]) 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); + + std::function<void(eFormatType, const std::string &)> outputter = [](eFormatType type, const std::string &msg) { log(type, "{}", msg); }; + + if (ip != nullptr) + if (auto session = g_pSessionManager->getByIp(ip)) + outputter = [session](eFormatType, const std::string &msg) { session->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); +}; |
