Skip to content

Commit

Permalink
feat(Core/Scripting): Add hook during command security evaluation. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
heyitsbench authored Nov 14, 2024
1 parent e1766eb commit 53cd41b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/server/game/Chat/ChatCommands/ChatCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions src/server/game/Scripting/ScriptDefines/AllCommandScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include "AllCommandScript.h"
#include "ChatCommand.h"
#include "ScriptMgr.h"
#include "ScriptMgrMacros.h"

Expand All @@ -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<uint16> enabledHooks)
: ScriptObject(name, ALLCOMMANDHOOK_END)
{
Expand Down
4 changes: 4 additions & 0 deletions src/server/game/Scripting/ScriptDefines/AllCommandScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
#define SCRIPT_OBJECT_ALL_COMMAND_SCRIPT_H_

#include "ScriptObject.h"
#include "ChatCommand.h"
#include <vector>

enum AllCommandHook
{
ALLCOMMANDHOOK_ON_HANDLE_DEV_COMMAND,
ALLCOMMANDHOOK_ON_TRY_EXECUTE_COMMAND,
ALLCOMMANDHOOK_ON_BEFORE_IS_INVOKER_VISIBLE,
ALLCOMMANDHOOK_END
};

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/server/game/Scripting/ScriptMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 */

Expand Down

0 comments on commit 53cd41b

Please sign in to comment.