From b4ac4a14fa8b1d7d16e8ccbb1913fe0d9d8939e4 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Wed, 18 Dec 2013 22:05:41 +0000 Subject: Prettified command error messages Using the magic of overlords (overloads) and standards compliance. --- src/Bindings/PluginManager.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'src/Bindings/PluginManager.cpp') diff --git a/src/Bindings/PluginManager.cpp b/src/Bindings/PluginManager.cpp index 732da24fa..832dc4249 100644 --- a/src/Bindings/PluginManager.cpp +++ b/src/Bindings/PluginManager.cpp @@ -9,6 +9,7 @@ #include "../Root.h" #include "../Server.h" #include "../CommandOutput.h" +#include "../ChatColor.h" #include "inifile/iniFile.h" #include "../Entities/Player.h" @@ -230,19 +231,25 @@ bool cPluginManager::CallHookBlockToPickups( bool cPluginManager::CallHookChat(cPlayer * a_Player, AString & a_Message) { - if (ExecuteCommand(a_Player, a_Message)) + bool WasCommandForbidden = false; + if (HandleCommand(a_Player, a_Message, true, WasCommandForbidden)) // We use HandleCommand as opposed to ExecuteCommand to accomodate the need to the WasCommandForbidden bool { - return true; + return true; // Chat message was handled as command + } + else if (WasCommandForbidden) // Couldn't be handled as command, was it because of insufficient permissions? + { + return true; // Yes - message was sent in HandleCommand, abort } // Check if it was a standard command (starts with a slash) + // If it was, we know that it was completely unrecognised (WasCommandForbidden == false) if (!a_Message.empty() && (a_Message[0] == '/')) { AStringVector Split(StringSplit(a_Message, " ")); ASSERT(!Split.empty()); // This should not happen - we know there's at least one char in the message so the split needs to be at least one item long - a_Player->SendMessage(Printf("Unknown Command: \"%s\"", Split[0].c_str())); - LOGINFO("Player \"%s\" issued an unknown command: \"%s\"", a_Player->GetName().c_str(), a_Message.c_str()); - return true; // Cancel sending + a_Player->SendMessage(Printf("%s[INFO] %sUnknown command: \"%s\"", cChatColor::Yellow.c_str(), cChatColor::White.c_str(), Split[0].c_str())); + LOGINFO("Player %s issued an unknown command: \"%s\"", a_Player->GetName().c_str(), a_Message.c_str()); + return true; // Cancel sending } HookMap::iterator Plugins = m_Hooks.find(HOOK_CHAT); @@ -1251,7 +1258,7 @@ bool cPluginManager::CallHookWorldTick(cWorld & a_World, float a_Dt, int a_LastT -bool cPluginManager::HandleCommand(cPlayer * a_Player, const AString & a_Command, bool a_ShouldCheckPermissions) +bool cPluginManager::HandleCommand(cPlayer * a_Player, const AString & a_Command, bool a_ShouldCheckPermissions, bool & a_WasCommandForbidden) { ASSERT(a_Player != NULL); @@ -1271,7 +1278,7 @@ bool cPluginManager::HandleCommand(cPlayer * a_Player, const AString & a_Command // Ask plugins first if a command is okay to execute the command: if (CallHookExecuteCommand(a_Player, Split)) { - LOGINFO("Player \"%s\" tried executing command \"%s\" that was stopped by the HOOK_EXECUTE_COMMAND hook", a_Player->GetName().c_str(), Split[0].c_str()); + LOGINFO("Player %s tried executing command \"%s\" that was stopped by the HOOK_EXECUTE_COMMAND hook", a_Player->GetName().c_str(), Split[0].c_str()); return false; } @@ -1281,7 +1288,9 @@ bool cPluginManager::HandleCommand(cPlayer * a_Player, const AString & a_Command !a_Player->HasPermission(cmd->second.m_Permission) ) { - LOGINFO("Player \"%s\" tried to execute forbidden command \"%s\".", a_Player->GetName().c_str(), Split[0].c_str()); + a_Player->SendMessage(Printf("%s[INFO] %sForbidden command; insufficient privileges: \"%s\"", cChatColor::Rose.c_str(), cChatColor::White.c_str(), Split[0].c_str())); + LOGINFO("Player %s tried to execute forbidden command: \"%s\"", a_Player->GetName().c_str(), Split[0].c_str()); + a_WasCommandForbidden = true; return false; } -- cgit v1.2.3