From 0a8175ef037732aa25a908687a2c3a019ba5eb55 Mon Sep 17 00:00:00 2001 From: Kitzunu <24550914+Kitzunu@users.noreply.github.com> Date: Wed, 3 Jul 2024 00:23:32 +0200 Subject: [PATCH] refactor(Core/ObjectMgr): Handle Profanity & Reserved Names in load (#19259) * refactor(Core/ObjectMgr): Handle Profanity & Reserved Names in load * closes https://github.com/azerothcore/azerothcore-wotlk/issues/18556 * Update ObjectMgr.cpp * Update ObjectMgr.cpp * I swear I am not drunk * We already check all of these * fix build * Forgot we dont send the responsecode in senderrormessage * last commit I swear --- src/server/game/Battlegrounds/ArenaTeam.cpp | 2 +- .../game/Entities/Player/PlayerStorage.cpp | 3 +- src/server/game/Globals/ObjectMgr.cpp | 197 +++++++----------- src/server/game/Globals/ObjectMgr.h | 10 +- src/server/game/Guilds/Guild.cpp | 2 +- src/server/game/Handlers/PetHandler.cpp | 12 -- src/server/game/Handlers/PetitionsHandler.cpp | 8 +- src/server/game/World/World.cpp | 6 +- src/server/scripts/Commands/cs_character.cpp | 27 +-- src/server/scripts/Commands/cs_guild.cpp | 2 +- src/server/scripts/Commands/cs_reload.cpp | 14 +- 11 files changed, 117 insertions(+), 166 deletions(-) diff --git a/src/server/game/Battlegrounds/ArenaTeam.cpp b/src/server/game/Battlegrounds/ArenaTeam.cpp index 25aa3f4ce15933..c0a6025a0ab035 100644 --- a/src/server/game/Battlegrounds/ArenaTeam.cpp +++ b/src/server/game/Battlegrounds/ArenaTeam.cpp @@ -281,7 +281,7 @@ bool ArenaTeam::LoadMembersFromDB(QueryResult result) bool ArenaTeam::SetName(std::string const& name) { - if (TeamName == name || name.empty() || name.length() > 24 || sObjectMgr->IsReservedName(name) || sObjectMgr->IsProfanityName(name) || !ObjectMgr::IsValidCharterName(name)) + if (TeamName == name || name.empty() || name.length() > 24 || !ObjectMgr::IsValidCharterName(name)) return false; TeamName = name; diff --git a/src/server/game/Entities/Player/PlayerStorage.cpp b/src/server/game/Entities/Player/PlayerStorage.cpp index 3b0482861ca794..7a3e35fcf7c754 100644 --- a/src/server/game/Entities/Player/PlayerStorage.cpp +++ b/src/server/game/Entities/Player/PlayerStorage.cpp @@ -4980,8 +4980,7 @@ bool Player::LoadFromDB(ObjectGuid playerGuid, CharacterDatabaseQueryHolder cons m_name = fields[2].Get(); // check name limitations - if (ObjectMgr::CheckPlayerName(m_name) != CHAR_NAME_SUCCESS || - (AccountMgr::IsPlayerAccount(GetSession()->GetSecurity()) && (sObjectMgr->IsReservedName(m_name) || sObjectMgr->IsProfanityName(m_name)))) + if (ObjectMgr::CheckPlayerName(m_name) != CHAR_NAME_SUCCESS || AccountMgr::IsPlayerAccount(GetSession()->GetSecurity())) { CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_ADD_AT_LOGIN_FLAG); stmt->SetData(0, uint16(AT_LOGIN_RENAME)); diff --git a/src/server/game/Globals/ObjectMgr.cpp b/src/server/game/Globals/ObjectMgr.cpp index 1cd00cfde3b1bb..cd00e46b3e33de 100644 --- a/src/server/game/Globals/ObjectMgr.cpp +++ b/src/server/game/Globals/ObjectMgr.cpp @@ -203,62 +203,6 @@ std::string ScriptInfo::GetDebugInfo() const return std::string(sz); } -/** - * @name ReservedNames - * @brief Checks NamesReserved.dbc for reserved names - * - * @param name Name to check for match in NamesReserved.dbc - * @return true/false - */ -bool ReservedNames(std::wstring& name) -{ - for (NamesReservedEntry const* reservedStore : sNamesReservedStore) - { - std::wstring PatternString; - - Utf8toWStr(reservedStore->Pattern, PatternString); - - boost::algorithm::replace_all(PatternString, "\\<", ""); - boost::algorithm::replace_all(PatternString, "\\>", ""); - - int stringCompare = name.compare(PatternString); - if (stringCompare == 0) - { - return true; - } - } - - return false; -}; - -/** - * @name ProfanityNames - * @brief Checks NamesProfanity.dbc for reserved names - * - * @param name Name to check for match in NamesProfanity.dbc - * @return true/false - */ -bool ProfanityNames(std::wstring& name) -{ - for (NamesProfanityEntry const* profanityStore : sNamesProfanityStore) - { - std::wstring PatternString; - - Utf8toWStr(profanityStore->Pattern, PatternString); - - boost::algorithm::replace_all(PatternString, "\\<", ""); - boost::algorithm::replace_all(PatternString, "\\>", ""); - - int stringCompare = name.compare(PatternString); - if (stringCompare == 0) - { - return true; - } - } - - return false; -} - bool normalizePlayerName(std::string& name) { if (name.empty()) @@ -8141,7 +8085,7 @@ void ObjectMgr::LoadCreatureQuestEnders() } } -void ObjectMgr::LoadReservedPlayersNames() +void ObjectMgr::LoadReservedPlayerNamesDB() { uint32 oldMSTime = getMSTime(); @@ -8151,8 +8095,7 @@ void ObjectMgr::LoadReservedPlayersNames() if (!result) { - LOG_WARN("server.loading", ">> Loaded 0 reserved player names. DB table `reserved_name` is empty!"); - LOG_INFO("server.loading", " "); + LOG_WARN("server.loading", ">> Loaded 0 reserved names. DB table `reserved_name` is empty!"); return; } @@ -8177,7 +8120,36 @@ void ObjectMgr::LoadReservedPlayersNames() ++count; } while (result->NextRow()); - LOG_INFO("server.loading", ">> Loaded {} reserved player names in {} ms", count, GetMSTimeDiffToNow(oldMSTime)); + LOG_INFO("server.loading", ">> Loaded {} reserved names from DB in {} ms", count, GetMSTimeDiffToNow(oldMSTime)); +} + +void ObjectMgr::LoadReservedPlayerNamesDBC() +{ + if (!sWorld->getBoolConfig(CONFIG_STRICT_NAMES_RESERVED)) + { + LOG_WARN("server.loading", ">> Loaded 0 reserved names from DBC. Config option disabled."); + return; + } + + uint32 oldMSTime = getMSTime(); + + uint32 count = 0; + + for (NamesReservedEntry const* reservedStore : sNamesReservedStore) + { + std::wstring wstr; + + Utf8toWStr(reservedStore->Pattern, wstr); + + // DBC does not have clean entries, remove the junk. + boost::algorithm::replace_all(wstr, "\\<", ""); + boost::algorithm::replace_all(wstr, "\\>", ""); + + _reservedNamesStore.insert(wstr); + count++; + } + + LOG_INFO("server.loading", ">> Loaded {} reserved names from DBC in {} ms", count, GetMSTimeDiffToNow(oldMSTime)); LOG_INFO("server.loading", " "); } @@ -8216,7 +8188,7 @@ void ObjectMgr::AddReservedPlayerName(std::string const& name) } } -void ObjectMgr::LoadProfanityPlayersNames() +void ObjectMgr::LoadProfanityNamesFromDB() { uint32 oldMSTime = getMSTime(); @@ -8226,8 +8198,7 @@ void ObjectMgr::LoadProfanityPlayersNames() if (!result) { - LOG_WARN("server.loading", ">> Loaded 0 profanity player names. DB table `profanity_name` is empty!"); - LOG_INFO("server.loading", " "); + LOG_WARN("server.loading", ">> Loaded 0 profanity names. DB table `profanity_name` is empty!"); return; } @@ -8252,7 +8223,36 @@ void ObjectMgr::LoadProfanityPlayersNames() ++count; } while (result->NextRow()); - LOG_INFO("server.loading", ">> Loaded {} profanity player names in {} ms", count, GetMSTimeDiffToNow(oldMSTime)); + LOG_INFO("server.loading", ">> Loaded {} profanity names from DB in {} ms", count, GetMSTimeDiffToNow(oldMSTime)); +} + +void ObjectMgr::LoadProfanityNamesFromDBC() +{ + if (!sWorld->getBoolConfig(CONFIG_STRICT_NAMES_PROFANITY)) + { + LOG_WARN("server.loading", ">> Loaded 0 profanity names from DBC. Config option disabled."); + return; + } + + uint32 oldMSTime = getMSTime(); + + uint32 count = 0; + + for (NamesProfanityEntry const* profanityStore : sNamesProfanityStore) + { + std::wstring wstr; + + Utf8toWStr(profanityStore->Pattern, wstr); + + // DBC does not have clean entries, remove the junk. + boost::algorithm::replace_all(wstr, "\\<", ""); + boost::algorithm::replace_all(wstr, "\\>", ""); + + _profanityNamesStore.insert(wstr); + count++; + } + + LOG_INFO("server.loading", ">> Loaded {} profanity names from DBC in {} ms", count, GetMSTimeDiffToNow(oldMSTime)); LOG_INFO("server.loading", " "); } @@ -8392,34 +8392,13 @@ uint8 ObjectMgr::CheckPlayerName(std::string_view name, bool create) if (wname[i] == wname[i - 1] && wname[i] == wname[i - 2]) return CHAR_NAME_THREE_CONSECUTIVE; - // Check Reserved Name from Database + // Check Reserved Name if (sObjectMgr->IsReservedName(name)) - { return CHAR_NAME_RESERVED; - } + // Check Profanity Name if (sObjectMgr->IsProfanityName(name)) - { return CHAR_NAME_PROFANE; - } - - // Check for Reserved Name from DBC - if (sWorld->getBoolConfig(CONFIG_STRICT_NAMES_RESERVED)) - { - if (ReservedNames(wname)) - { - return CHAR_NAME_RESERVED; - } - } - - // Check for Profanity - if (sWorld->getBoolConfig(CONFIG_STRICT_NAMES_PROFANITY)) - { - if (ProfanityNames(wname)) - { - return CHAR_NAME_PROFANE; - } - } return CHAR_NAME_SUCCESS; } @@ -8437,23 +8416,13 @@ bool ObjectMgr::IsValidCharterName(std::string_view name) if (wname.size() < minName) return false; - // Check for Reserved Name from DBC - if (sWorld->getBoolConfig(CONFIG_STRICT_NAMES_RESERVED)) - { - if (ReservedNames(wname)) - { - return false; - } - } + // Check Reserved Name + if (sObjectMgr->IsReservedName(name)) + return false; - // Check for Profanity - if (sWorld->getBoolConfig(CONFIG_STRICT_NAMES_PROFANITY)) - { - if (ProfanityNames(wname)) - { - return false; - } - } + // Check Profanity Name + if (sObjectMgr->IsProfanityName(name)) + return false; uint32 strictMask = sWorld->getIntConfig(CONFIG_STRICT_CHARTER_NAMES); @@ -8491,23 +8460,13 @@ PetNameInvalidReason ObjectMgr::CheckPetName(std::string_view name) if (!isValidString(wname, strictMask, false)) return PET_NAME_MIXED_LANGUAGES; - // Check for Reserved Name from DBC - if (sWorld->getBoolConfig(CONFIG_STRICT_NAMES_RESERVED)) - { - if (ReservedNames(wname)) - { - return PET_NAME_RESERVED; - } - } + // Check Reserved Name + if (sObjectMgr->IsReservedName(name)) + return PET_NAME_RESERVED; - // Check for Profanity - if (sWorld->getBoolConfig(CONFIG_STRICT_NAMES_PROFANITY)) - { - if (ProfanityNames(wname)) - { - return PET_NAME_PROFANE; - } - } + // Check Profanity Name + if (sObjectMgr->IsProfanityName(name)) + return PET_NAME_PROFANE; return PET_NAME_SUCCESS; } diff --git a/src/server/game/Globals/ObjectMgr.h b/src/server/game/Globals/ObjectMgr.h index 48a35915609230..17d6d480f9ec04 100644 --- a/src/server/game/Globals/ObjectMgr.h +++ b/src/server/game/Globals/ObjectMgr.h @@ -691,8 +691,6 @@ SkillRangeType GetSkillRangeType(SkillRaceClassInfoEntry const* rcEntry); #define MAX_CHARTER_NAME 24 // max allowed by client name length #define MAX_CHANNEL_NAME 50 // pussywizard -bool ReservedNames(std::wstring& name); -bool ProfanityNames(std::wstring& name); bool normalizePlayerName(std::string& name); struct LanguageDesc @@ -1334,12 +1332,14 @@ class ObjectMgr uint32 AddCreData(uint32 entry, uint32 map, float x, float y, float z, float o, uint32 spawntimedelay = 0); // reserved names - void LoadReservedPlayersNames(); + void LoadReservedPlayerNamesDB(); + void LoadReservedPlayerNamesDBC(); [[nodiscard]] bool IsReservedName(std::string_view name) const; void AddReservedPlayerName(std::string const& name); // profanity names - void LoadProfanityPlayersNames(); + void LoadProfanityNamesFromDB(); + void LoadProfanityNamesFromDBC(); [[nodiscard]] bool IsProfanityName(std::string_view name) const; void AddProfanityPlayerName(std::string const& name); @@ -1508,7 +1508,7 @@ class ObjectMgr //character profanity names typedef std::set ProfanityNamesContainer; - ReservedNamesContainer _profanityNamesStore; + ProfanityNamesContainer _profanityNamesStore; GameTeleContainer _gameTeleStore; diff --git a/src/server/game/Guilds/Guild.cpp b/src/server/game/Guilds/Guild.cpp index f86d0e08dc3930..eee35454164c78 100644 --- a/src/server/game/Guilds/Guild.cpp +++ b/src/server/game/Guilds/Guild.cpp @@ -1187,7 +1187,7 @@ void Guild::OnPlayerStatusChange(Player* player, uint32 flag, bool state) bool Guild::SetName(std::string_view const& name) { - if (m_name == name || name.empty() || name.length() > 24 || sObjectMgr->IsReservedName(name) || sObjectMgr->IsProfanityName(name) || !ObjectMgr::IsValidCharterName(name)) + if (m_name == name || name.empty() || name.length() > 24 || !ObjectMgr::IsValidCharterName(name)) { return false; } diff --git a/src/server/game/Handlers/PetHandler.cpp b/src/server/game/Handlers/PetHandler.cpp index bf0ab70fa1798f..9c0b4d4a6a4622 100644 --- a/src/server/game/Handlers/PetHandler.cpp +++ b/src/server/game/Handlers/PetHandler.cpp @@ -856,18 +856,6 @@ void WorldSession::HandlePetRename(WorldPacket& recvData) return; } - if (sObjectMgr->IsReservedName(name)) - { - SendPetNameInvalid(PET_NAME_RESERVED, name, nullptr); - return; - } - - if (sObjectMgr->IsProfanityName(name)) - { - SendPetNameInvalid(PET_NAME_PROFANE, name, nullptr); - return; - } - pet->SetName(name); Unit* owner = pet->GetOwner(); diff --git a/src/server/game/Handlers/PetitionsHandler.cpp b/src/server/game/Handlers/PetitionsHandler.cpp index 6ce8b6039c3a5c..3023822345f550 100644 --- a/src/server/game/Handlers/PetitionsHandler.cpp +++ b/src/server/game/Handlers/PetitionsHandler.cpp @@ -136,7 +136,7 @@ void WorldSession::HandlePetitionBuyOpcode(WorldPacket& recvData) return; } - if (sObjectMgr->IsReservedName(name) || sObjectMgr->IsProfanityName(name) || !ObjectMgr::IsValidCharterName(name)) + if (!ObjectMgr::IsValidCharterName(name)) { Guild::SendCommandResult(this, GUILD_COMMAND_CREATE, ERR_GUILD_NAME_INVALID, name); return; @@ -149,7 +149,7 @@ void WorldSession::HandlePetitionBuyOpcode(WorldPacket& recvData) SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, name, "", ERR_ARENA_TEAM_NAME_EXISTS_S); return; } - if (sObjectMgr->IsReservedName(name) || sObjectMgr->IsProfanityName(name) || !ObjectMgr::IsValidCharterName(name)) + if (!ObjectMgr::IsValidCharterName(name)) { SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, name, "", ERR_ARENA_TEAM_NAME_INVALID); return; @@ -350,7 +350,7 @@ void WorldSession::HandlePetitionRenameOpcode(WorldPacket& recvData) Guild::SendCommandResult(this, GUILD_COMMAND_CREATE, ERR_GUILD_NAME_EXISTS_S, newName); return; } - if (sObjectMgr->IsReservedName(newName) || sObjectMgr->IsProfanityName(newName) || !ObjectMgr::IsValidCharterName(newName)) + if (!ObjectMgr::IsValidCharterName(newName)) { Guild::SendCommandResult(this, GUILD_COMMAND_CREATE, ERR_GUILD_NAME_INVALID, newName); return; @@ -363,7 +363,7 @@ void WorldSession::HandlePetitionRenameOpcode(WorldPacket& recvData) SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, newName, "", ERR_ARENA_TEAM_NAME_EXISTS_S); return; } - if (sObjectMgr->IsReservedName(newName) || sObjectMgr->IsProfanityName(newName) || !ObjectMgr::IsValidCharterName(newName)) + if (!ObjectMgr::IsValidCharterName(newName)) { SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, newName, "", ERR_ARENA_TEAM_NAME_INVALID); return; diff --git a/src/server/game/World/World.cpp b/src/server/game/World/World.cpp index d2923c47b7cb8a..63bc50a473dbdc 100644 --- a/src/server/game/World/World.cpp +++ b/src/server/game/World/World.cpp @@ -1916,10 +1916,12 @@ void World::SetInitialWorldSettings() sGroupMgr->LoadGroups(); LOG_INFO("server.loading", "Loading Reserved Names..."); - sObjectMgr->LoadReservedPlayersNames(); + sObjectMgr->LoadReservedPlayerNamesDB(); + sObjectMgr->LoadReservedPlayerNamesDBC(); // Needs to be after LoadReservedPlayerNamesDB() LOG_INFO("server.loading", "Loading Profanity Names..."); - sObjectMgr->LoadProfanityPlayersNames(); + sObjectMgr->LoadProfanityNamesFromDB(); + sObjectMgr->LoadProfanityNamesFromDBC(); // Needs to be after LoadProfanityNamesFromDB() LOG_INFO("server.loading", "Loading GameObjects for Quests..."); sObjectMgr->LoadGameObjectForQuests(); diff --git a/src/server/scripts/Commands/cs_character.cpp b/src/server/scripts/Commands/cs_character.cpp index 9122c06f451cbf..e04c969040e963 100644 --- a/src/server/scripts/Commands/cs_character.cpp +++ b/src/server/scripts/Commands/cs_character.cpp @@ -347,21 +347,22 @@ class character_commandscript : public CommandScript return false; } - if (ObjectMgr::CheckPlayerName(newName, true) != CHAR_NAME_SUCCESS) + ResponseCodes res = ResponseCodes(ObjectMgr::CheckPlayerName(newName, true)); + if (res != CHAR_NAME_SUCCESS) { - handler->SendErrorMessage(LANG_BAD_VALUE); - return false; - } - - if (sObjectMgr->IsReservedName(newName)) - { - handler->SendErrorMessage(LANG_RESERVED_NAME); - return false; - } + switch (res) + { + case CHAR_NAME_RESERVED: + handler->SendErrorMessage(LANG_RESERVED_NAME); + break; + case CHAR_NAME_PROFANE: + handler->SendErrorMessage(LANG_PROFANITY_NAME); + break; + default: + handler->SendErrorMessage(LANG_BAD_VALUE); + break; + } - if (sObjectMgr->IsProfanityName(newName)) - { - handler->SendErrorMessage(LANG_PROFANITY_NAME); return false; } diff --git a/src/server/scripts/Commands/cs_guild.cpp b/src/server/scripts/Commands/cs_guild.cpp index 254ff7df499257..8b1e6182cc88ba 100644 --- a/src/server/scripts/Commands/cs_guild.cpp +++ b/src/server/scripts/Commands/cs_guild.cpp @@ -85,7 +85,7 @@ class guild_commandscript : public CommandScript return false; } - if (sObjectMgr->IsReservedName(guildName) || sObjectMgr->IsProfanityName(guildName) || !sObjectMgr->IsValidCharterName(guildName)) + if (!sObjectMgr->IsValidCharterName(guildName)) { handler->SendErrorMessage(LANG_BAD_VALUE); return false; diff --git a/src/server/scripts/Commands/cs_reload.cpp b/src/server/scripts/Commands/cs_reload.cpp index d73ef896bf67f6..1cd1e0a23bfdb1 100644 --- a/src/server/scripts/Commands/cs_reload.cpp +++ b/src/server/scripts/Commands/cs_reload.cpp @@ -780,17 +780,19 @@ class reload_commandscript : public CommandScript static bool HandleReloadReservedNameCommand(ChatHandler* handler) { - LOG_INFO("server.loading", "Re-Loading `reserved_player` Table!"); - sObjectMgr->LoadReservedPlayersNames(); - handler->SendGlobalGMSysMessage("DB table `reserved_name` reloaded."); + LOG_INFO("server.loading", "Re-Loading Reserved Names!"); + sObjectMgr->LoadReservedPlayerNamesDB(); + sObjectMgr->LoadReservedPlayerNamesDBC(); // Needed because we clear the store in LoadReservedPlayerNamesDB() + handler->SendGlobalGMSysMessage("Reserved Names reloaded."); return true; } static bool HandleReloadProfanityNameCommand(ChatHandler* handler) { - LOG_INFO("server.loading", "Re-Loading `profanity_player` Table!"); - sObjectMgr->LoadProfanityPlayersNames(); - handler->SendGlobalGMSysMessage("DB table `profanity_player` reloaded."); + LOG_INFO("server.loading", "Re-Loading Profanity Names!"); + sObjectMgr->LoadProfanityNamesFromDB(); + sObjectMgr->LoadProfanityNamesFromDBC(); // Needed because we clear the store in LoadProfanityNamesFromDB() + handler->SendGlobalGMSysMessage("Profanity Names reloaded."); return true; }