From 2460f78015ecf3178cc9270f0b92b849e79fd872 Mon Sep 17 00:00:00 2001 From: nytmyr <53322305+nytmyr@users.noreply.github.com> Date: Sun, 22 Dec 2024 00:05:45 -0600 Subject: [PATCH] more name checks and add proper soft deletes to bots --- .../base/base_bot_data_repository.h | 19 ++++++++--- zone/bot.cpp | 32 ++++++++++--------- zone/bot_database.cpp | 7 ++-- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/common/repositories/base/base_bot_data_repository.h b/common/repositories/base/base_bot_data_repository.h index eb0cdcb99..2cd4b1d04 100644 --- a/common/repositories/base/base_bot_data_repository.h +++ b/common/repositories/base/base_bot_data_repository.h @@ -349,14 +349,25 @@ public: int bot_data_id ) { - auto results = db.QueryDatabase( - fmt::format( + std::string query; + + if (RuleB(Bots, BotSoftDeletes)) { + query = fmt::format( + "UPDATE {} SET name = SUBSTRING(CONCAT(name, '-deleted-', UNIX_TIMESTAMP()), 1, 64) WHERE {} = {}", + TableName(), + PrimaryKey(), + bot_data_id + ); + } + else { + query = fmt::format( "DELETE FROM {} WHERE {} = {}", TableName(), PrimaryKey(), bot_data_id - ) - ); + ); + } + auto results = db.QueryDatabase(query); return (results.Success() ? results.RowsAffected() : 0); } diff --git a/zone/bot.cpp b/zone/bot.cpp index 7ec75f3de..abd437c26 100644 --- a/zone/bot.cpp +++ b/zone/bot.cpp @@ -1328,25 +1328,27 @@ bool Bot::IsValidName() bool Bot::IsValidName(std::string& name) { - if (name.empty() || name.length() < 4 || name.length() > 15) { + if (name.empty()) { // can't be empty return false; } - if (!isupper(name[0])) { + if (islower(name[0])) { // capitalize first letter if not + name[0] = toupper(name[0]); + } + + if (!EQ::ValueWithin(name.size(), 4, 15)) { // must be between 4 and 15 characters return false; } - for (char c : name.substr(1)) { - if (c == '_') { - return false; - } + if (std::any_of(name.begin(), name.end(), [](char c) { return c == ' ' || c == '_'; })) { // cannot contain spaces or _ + return false; + } - if (!isalpha(c)) { - return false; - } - - if (!RuleB(Bots, AllowCamelCaseNames) && !islower(c)) { - return false; + if (!RuleB(Bots, AllowCamelCaseNames)) { + for (int i = 1; i < name.size(); ++i) { + if (isupper(name[i])) { + return false; + } } } @@ -1450,10 +1452,10 @@ bool Bot::DeleteBot() if (!database.botdb.DeleteBotBlockedBuffs(GetBotID())) { return false; } + } - if (!database.botdb.DeleteBot(GetBotID())) { - return false; - } + if (!database.botdb.DeleteBot(GetBotID())) { + return false; } return true; diff --git a/zone/bot_database.cpp b/zone/bot_database.cpp index dc7bcf38d..84fa0dfa4 100644 --- a/zone/bot_database.cpp +++ b/zone/bot_database.cpp @@ -186,6 +186,7 @@ bool BotDatabase::QueryNameAvailablity(const std::string& bot_name, bool& availa if ( bot_name.empty() || bot_name.size() > 60 || + !database.CheckNameFilter(bot_name) || database.IsNameUsed(bot_name) ) { return false; @@ -246,6 +247,8 @@ bool BotDatabase::LoadBotsList(const uint32 owner_id, std::list