From 53cd41b938ebb6621d45734b37a7a18c853368c7 Mon Sep 17 00:00:00 2001 From: Benjamin Jackson <38561765+heyitsbench@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:14:28 -0500 Subject: [PATCH] feat(Core/Scripting): Add hook during command security evaluation. (#20564) --- src/server/game/Chat/ChatCommands/ChatCommand.cpp | 3 +++ .../game/Scripting/ScriptDefines/AllCommandScript.cpp | 6 ++++++ src/server/game/Scripting/ScriptDefines/AllCommandScript.h | 4 ++++ src/server/game/Scripting/ScriptMgr.h | 2 ++ 4 files changed, 15 insertions(+) diff --git a/src/server/game/Chat/ChatCommands/ChatCommand.cpp b/src/server/game/Chat/ChatCommands/ChatCommand.cpp index 93bcd25f408242..c15e4d9a8e6ed2 100644 --- a/src/server/game/Chat/ChatCommands/ChatCommand.cpp +++ b/src/server/game/Chat/ChatCommands/ChatCommand.cpp @@ -506,6 +506,9 @@ bool Acore::Impl::ChatCommands::ChatCommandNode::IsInvokerVisible(ChatHandler co if (!_invoker) return false; + if (!sScriptMgr->OnBeforeIsInvokerVisible(_name, _permission, who)) + return true; + if (who.IsConsole() && (_permission.AllowConsole == Acore::ChatCommands::Console::No)) return false; diff --git a/src/server/game/Scripting/ScriptDefines/AllCommandScript.cpp b/src/server/game/Scripting/ScriptDefines/AllCommandScript.cpp index 59b1395f5af97a..cf01d409191317 100644 --- a/src/server/game/Scripting/ScriptDefines/AllCommandScript.cpp +++ b/src/server/game/Scripting/ScriptDefines/AllCommandScript.cpp @@ -16,6 +16,7 @@ */ #include "AllCommandScript.h" +#include "ChatCommand.h" #include "ScriptMgr.h" #include "ScriptMgrMacros.h" @@ -29,6 +30,11 @@ bool ScriptMgr::OnTryExecuteCommand(ChatHandler& handler, std::string_view cmdSt CALL_ENABLED_BOOLEAN_HOOKS(AllCommandScript, ALLCOMMANDHOOK_ON_TRY_EXECUTE_COMMAND, !script->OnTryExecuteCommand(handler, cmdStr)); } +bool ScriptMgr::OnBeforeIsInvokerVisible(std::string name, Acore::Impl::ChatCommands::CommandPermissions permissions, ChatHandler const& who) +{ + CALL_ENABLED_BOOLEAN_HOOKS(AllCommandScript, ALLCOMMANDHOOK_ON_BEFORE_IS_INVOKER_VISIBLE, !script->OnBeforeIsInvokerVisible(name, permissions, who)); +} + AllCommandScript::AllCommandScript(const char* name, std::vector enabledHooks) : ScriptObject(name, ALLCOMMANDHOOK_END) { diff --git a/src/server/game/Scripting/ScriptDefines/AllCommandScript.h b/src/server/game/Scripting/ScriptDefines/AllCommandScript.h index 75002811536f32..e56615b1521b6f 100644 --- a/src/server/game/Scripting/ScriptDefines/AllCommandScript.h +++ b/src/server/game/Scripting/ScriptDefines/AllCommandScript.h @@ -19,12 +19,14 @@ #define SCRIPT_OBJECT_ALL_COMMAND_SCRIPT_H_ #include "ScriptObject.h" +#include "ChatCommand.h" #include enum AllCommandHook { ALLCOMMANDHOOK_ON_HANDLE_DEV_COMMAND, ALLCOMMANDHOOK_ON_TRY_EXECUTE_COMMAND, + ALLCOMMANDHOOK_ON_BEFORE_IS_INVOKER_VISIBLE, ALLCOMMANDHOOK_END }; @@ -45,6 +47,8 @@ class AllCommandScript : public ScriptObject * @param cmdStr Contains information about the command name */ [[nodiscard]] virtual bool OnTryExecuteCommand(ChatHandler& /*handler*/, std::string_view /*cmdStr*/) { return true; } + + [[nodiscard]] virtual bool OnBeforeIsInvokerVisible(std::string /*name*/, Acore::Impl::ChatCommands::CommandPermissions /*permissions*/, ChatHandler const& /*who*/) { return true; } }; // Compatibility for old scripts diff --git a/src/server/game/Scripting/ScriptMgr.h b/src/server/game/Scripting/ScriptMgr.h index 7d074dbf7804cb..1825319bbcfbc7 100644 --- a/src/server/game/Scripting/ScriptMgr.h +++ b/src/server/game/Scripting/ScriptMgr.h @@ -22,6 +22,7 @@ #include "ArenaTeam.h" #include "AuctionHouseMgr.h" #include "Battleground.h" +#include "ChatCommand.h" #include "Common.h" #include "DBCStores.h" #include "DynamicObject.h" @@ -672,6 +673,7 @@ class ScriptMgr void OnHandleDevCommand(Player* player, bool& enable); bool OnTryExecuteCommand(ChatHandler& handler, std::string_view cmdStr); + bool OnBeforeIsInvokerVisible(std::string name, Acore::Impl::ChatCommands::CommandPermissions permissions, ChatHandler const& who); public: /* DatabaseScript */