From fb219052c8a09bb8fa9635521756819482da2cdd Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sun, 23 Mar 2025 07:42:47 +0000 Subject: Move DefaultIsNick/DefaultIsUser/IsNick/IsUser to UserManager. --- include/inspircd.h | 20 ---------------- include/usermanager.h | 20 ++++++++++++++++ include/users.h | 1 - modules/chgident.cpp | 2 +- modules/codepage.cpp | 8 +++---- modules/core/core_user/cmd_nick.cpp | 2 +- modules/core/core_user/cmd_user.cpp | 2 +- modules/gateway.cpp | 2 +- modules/ident.cpp | 2 +- modules/monitor.cpp | 2 +- modules/nicklock.cpp | 2 +- modules/sanick.cpp | 2 +- modules/setident.cpp | 2 +- src/helperfuncs.cpp | 47 ------------------------------------- src/usermanager.cpp | 38 ++++++++++++++++++++++++++++++ 15 files changed, 71 insertions(+), 81 deletions(-) diff --git a/include/inspircd.h b/include/inspircd.h index 9da420f0e..b7d20b3da 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -242,12 +242,6 @@ public: /** Fills a buffer with random bytes. */ std::function GenRandom = &DefaultGenRandom; - /** Determines whether a nickname is valid. */ - std::function IsNick = &DefaultIsNick; - - /** Determines whether a username is valid. */ - std::function IsUser = &DefaultIsUser; - /** List of the open listeners. */ std::vector Ports; @@ -308,20 +302,6 @@ public: */ static void DefaultGenRandom(char* output, size_t max); - /** Determines whether a nickname is valid according to the RFC 1459 rules. - * This is the default function for InspIRCd::IsNick. - * @param nick The nickname to validate. - * @return True if the nickname is valid according to RFC 1459 rules; otherwise, false. - */ - static bool DefaultIsNick(const std::string_view& nick); - - /** Determines whether a username is valid according to the RFC 1459 rules. - * This is the default function for InspIRCd::IsUser. - * @param user The username to validate. - * @return True if the username is valid according to RFC 1459 rules; otherwise, false. - */ - static bool DefaultIsUser(const std::string_view& user); - /** Causes the server to exit after unloading modules and closing all open file descriptors. * @param status The exit code to give to the operating system. */ diff --git a/include/usermanager.h b/include/usermanager.h index ee9a9f603..ec333d51b 100644 --- a/include/usermanager.h +++ b/include/usermanager.h @@ -80,6 +80,12 @@ public: */ ~UserManager(); + /** Determines whether a nickname is valid. */ + std::function IsNick = &DefaultIsNick; + + /** Determines whether a username is valid. */ + std::function IsUser = &DefaultIsUser; + /** Nickname string -> User* map. Contains all users, including partially connected ones. */ UserMap clientlist; @@ -98,6 +104,20 @@ public: /** Number of local unknown (not fully connected) users. */ size_t unknown_count = 0; + /** Determines whether a nickname is valid according to the RFC 1459 rules. + * This is the default function for UserManager::IsNick. + * @param nick The nickname to validate. + * @return True if the nickname is valid according to RFC 1459 rules; otherwise, false. + */ + static bool DefaultIsNick(const std::string_view& nick); + + /** Determines whether a username is valid according to the RFC 1459 rules. + * This is the default function for UserManager::IsUser. + * @param user The username to validate. + * @return True if the username is valid according to RFC 1459 rules; otherwise, false. + */ + static bool DefaultIsUser(const std::string_view& user); + /** Perform background user events for all local users such as PING checks, connection timeouts, * penalty management and recvq processing for users who have data in their recvq due to throttling. */ diff --git a/include/users.h b/include/users.h index 06d97342c..9f72c5283 100644 --- a/include/users.h +++ b/include/users.h @@ -438,7 +438,6 @@ public: irc::sockets::sockaddrs client_sa; /** The users nickname. - * Use InspIRCd::IsNick() to validate nicknames. */ std::string nick; diff --git a/modules/chgident.cpp b/modules/chgident.cpp index 06bdda768..593ceaae1 100644 --- a/modules/chgident.cpp +++ b/modules/chgident.cpp @@ -53,7 +53,7 @@ public: return CmdResult::FAILURE; } - if (!ServerInstance->IsUser(parameters[1])) + if (!ServerInstance->Users.IsUser(parameters[1])) { user->WriteNotice("*** CHGIDENT: Invalid characters in username"); return CmdResult::FAILURE; diff --git a/modules/codepage.cpp b/modules/codepage.cpp index f39aba5be..7a8c27e47 100644 --- a/modules/codepage.cpp +++ b/modules/codepage.cpp @@ -311,7 +311,7 @@ private: { for (auto* user : ServerInstance->Users.GetLocalUsers()) { - if (user->nick != user->uuid && !ServerInstance->IsNick(user->nick)) + if (user->nick != user->uuid && !ServerInstance->Users.IsNick(user->nick)) ChangeNick(user, "Your nickname is no longer valid."); } } @@ -340,13 +340,13 @@ public: , ISupport::EventListener(this) , origcasemap(national_case_insensitive_map) , origcasemapname(ServerInstance->Config->CaseMapping) - , origisnick(ServerInstance->IsNick) + , origisnick(ServerInstance->Users.IsNick) { } ~ModuleCodepage() override { - ServerInstance->IsNick = origisnick; + ServerInstance->Users.IsNick = origisnick; CheckInvalidNick(); ServerInstance->Config->CaseMapping = origcasemapname; @@ -416,7 +416,7 @@ public: charset = codepagetag->getString("charset"); std::swap(codepage, newcodepage); - ServerInstance->IsNick = [this](const std::string_view& nick) { return codepage->IsValidNick(nick); }; + ServerInstance->Users.IsNick = [this](const std::string_view& nick) { return codepage->IsValidNick(nick); }; CheckInvalidNick(); ServerInstance->Config->CaseMapping = name; diff --git a/modules/core/core_user/cmd_nick.cpp b/modules/core/core_user/cmd_nick.cpp index 03d280bb0..028a02f86 100644 --- a/modules/core/core_user/cmd_nick.cpp +++ b/modules/core/core_user/cmd_nick.cpp @@ -58,7 +58,7 @@ CmdResult CommandNick::HandleLocal(LocalUser* user, const Params& parameters) { newnick = user->uuid; } - else if (!ServerInstance->IsNick(newnick)) + else if (!ServerInstance->Users.IsNick(newnick)) { user->WriteNumeric(ERR_ERRONEUSNICKNAME, newnick, "Erroneous Nickname"); return CmdResult::FAILURE; diff --git a/modules/core/core_user/cmd_user.cpp b/modules/core/core_user/cmd_user.cpp index 37761e9a0..2cd87b9b5 100644 --- a/modules/core/core_user/cmd_user.cpp +++ b/modules/core/core_user/cmd_user.cpp @@ -45,7 +45,7 @@ CmdResult CommandUser::HandleLocal(LocalUser* user, const Params& parameters) /* A user may only send the USER command once */ if (!(user->connected & User::CONN_USER)) { - if (!ServerInstance->IsUser(parameters[0])) + if (!ServerInstance->Users.IsUser(parameters[0])) { user->WriteNumeric(ERR_INVALIDUSERNAME, name, "Your username is not valid"); return CmdResult::FAILURE; diff --git a/modules/gateway.cpp b/modules/gateway.cpp index 53d8d7e47..4daee3816 100644 --- a/modules/gateway.cpp +++ b/modules/gateway.cpp @@ -386,7 +386,7 @@ public: if (insp::equalsci(type, "username") || insp::equalsci(type, "ident")) { // The IP address should be looked up from the hex IP address. - const std::string newuser = tag->getString("newusername", "gateway", ServerInstance->IsUser); + const std::string newuser = tag->getString("newusername", "gateway", ServerInstance->Users.IsUser); userhosts.emplace_back(masks, newuser); } else if (insp::equalsci(type, "webirc")) diff --git a/modules/ident.cpp b/modules/ident.cpp index 50de7865c..97e504e83 100644 --- a/modules/ident.cpp +++ b/modules/ident.cpp @@ -251,7 +251,7 @@ public: * we're done. */ result += chr; - if (!ServerInstance->IsUser(result)) + if (!ServerInstance->Users.IsUser(result)) { result.pop_back(); break; diff --git a/modules/monitor.cpp b/modules/monitor.cpp index 832c55aa4..5c41018ab 100644 --- a/modules/monitor.cpp +++ b/modules/monitor.cpp @@ -122,7 +122,7 @@ public: WatchResult Watch(LocalUser* user, const std::string& nick, unsigned long maxwatch) { - if (!ServerInstance->IsNick(nick)) + if (!ServerInstance->Users.IsNick(nick)) return WR_INVALIDNICK; WatchedList* watched = GetWatchedPriv(user, true); diff --git a/modules/nicklock.cpp b/modules/nicklock.cpp index 8b36e449d..ba4c6d9c7 100644 --- a/modules/nicklock.cpp +++ b/modules/nicklock.cpp @@ -61,7 +61,7 @@ public: /* Do local sanity checks and bails */ if (IS_LOCAL(user)) { - if (!ServerInstance->IsNick(parameters[1])) + if (!ServerInstance->Users.IsNick(parameters[1])) { user->WriteNotice("*** Invalid nickname '" + parameters[1] + "'"); return CmdResult::FAILURE; diff --git a/modules/sanick.cpp b/modules/sanick.cpp index 7b1d8a11b..580c405ce 100644 --- a/modules/sanick.cpp +++ b/modules/sanick.cpp @@ -59,7 +59,7 @@ public: return CmdResult::FAILURE; } - if (!ServerInstance->IsNick(parameters[1])) + if (!ServerInstance->Users.IsNick(parameters[1])) { user->WriteNotice("*** Invalid nickname: '" + parameters[1] + "'"); return CmdResult::FAILURE; diff --git a/modules/setident.cpp b/modules/setident.cpp index ef8607d0a..744250ba4 100644 --- a/modules/setident.cpp +++ b/modules/setident.cpp @@ -43,7 +43,7 @@ public: return CmdResult::FAILURE; } - if (!ServerInstance->IsUser(parameters[0])) + if (!ServerInstance->Users.IsUser(parameters[0])) { user->WriteNotice("*** SETIDENT: Invalid characters in username"); return CmdResult::FAILURE; diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index c259a5ada..cf92be745 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -241,53 +241,6 @@ void InspIRCd::ProcessColors(std::string& line) } } -/* true for valid nickname, false else */ -bool InspIRCd::DefaultIsNick(const std::string_view& n) -{ - if (n.empty() || n.length() > ServerInstance->Config->Limits.MaxNick) - return false; - - for (std::string_view::const_iterator i = n.begin(); i != n.end(); ++i) - { - if ((*i >= 'A') && (*i <= '}')) - { - /* "A"-"}" can occur anywhere in a nickname */ - continue; - } - - if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i != n.begin())) - { - /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */ - continue; - } - - /* invalid character! abort */ - return false; - } - - return true; -} - -/* return true for good username, false else */ -bool InspIRCd::DefaultIsUser(const std::string_view& n) -{ - if (n.empty()) - return false; - - for (const auto chr : n) - { - if (chr >= 'A' && chr <= '}') - continue; - - if ((chr >= '0' && chr <= '9') || chr == '-' || chr == '.') - continue; - - return false; - } - - return true; -} - bool InspIRCd::IsHost(const std::string_view& host, bool allowsimple) { // Hostnames must be non-empty and shorter than the maximum hostname length. diff --git a/src/usermanager.cpp b/src/usermanager.cpp index b8c355524..491cfdb71 100644 --- a/src/usermanager.cpp +++ b/src/usermanager.cpp @@ -465,3 +465,41 @@ User* UserManager::FindUUID(const std::string& uuid, bool fullyconnected) return user; } + +/* true for valid nickname, false else */ +bool UserManager::DefaultIsNick(const std::string_view& nick) +{ + if (nick.empty() || nick.length() > ServerInstance->Config->Limits.MaxNick) + return false; + + for (auto it = nick.cbegin(); it != nick.cend(); ++it) + { + const auto chr = *it; + if (chr >= 'A' && chr <= '}') + continue; // "A"-"}" can occur anywhere in a nickname. + + if (((chr >= '0' && chr <= '9') || (chr == '-')) && it != nick.begin()) + continue; // "0"-"9", "-" can occur anywhere BUT the first char of a nickname. + + return false; + } + return true; +} + +bool UserManager::DefaultIsUser(const std::string_view& user) +{ + if (user.empty()) + return false; + + for (const auto chr : user) + { + if (chr >= 'A' && chr <= '}') + continue; + + if ((chr >= '0' && chr <= '9') || chr == '-' || chr == '.') + continue; + + return false; + } + return true; +} -- cgit v1.3.1-10-gc9f91