From f9416899c56e91f0acfca18ec8f96b144028422d Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Tue, 20 Jun 2023 19:17:11 +0100 Subject: Replace with . This adds support for truncating values e.g. with the nick foo|afk a user will receive a cloak of "foo". --- src/modules/m_cloak_user.cpp | 68 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 10 deletions(-) (limited to 'src/modules/m_cloak_user.cpp') diff --git a/src/modules/m_cloak_user.cpp b/src/modules/m_cloak_user.cpp index 369950df2..a958de651 100644 --- a/src/modules/m_cloak_user.cpp +++ b/src/modules/m_cloak_user.cpp @@ -24,15 +24,29 @@ class UserMethod : public Cloak::Method { private: + // The action to take when an invalid character is encountered. + enum InvalidChar + : uint8_t + { + // Reject the value as a valid cloak, + REJECT, + + // Strip the invalid character from the value. + STRIP, + + // Truncate the value at the missing character. + TRUNCATE, + }; + // The characters which are valid in a hostname. const CharState& hostmap; + // The action to take when an invalid character is encountered. + InvalidChar invalidchar; + // The prefix for cloaks (e.g. users.). const std::string prefix; - // Whether to strip non-host characters from the cloak. - const bool sanitize; - // The suffix for IP cloaks (e.g. .example.org). const std::string suffix; @@ -44,9 +58,13 @@ protected: : Cloak::Method(engine, tag) , hostmap(hm) , prefix(tag->getString("prefix")) - , sanitize(tag->getBool("sanitize", true)) , suffix(tag->getString("suffix")) { + invalidchar = tag->getEnum("invalidchar", InvalidChar::STRIP, { + { "reject", InvalidChar::REJECT }, + { "strip", InvalidChar::STRIP }, + { "truncate", InvalidChar::TRUNCATE }, + }); } public: @@ -63,17 +81,36 @@ public: safemiddle.reserve(middle.length()); for (const auto chr : middle) { - if (!hostmap.test(static_cast(chr))) + if (hostmap.test(static_cast(chr))) { - if (!sanitize) - return {}; // Contains invalid characters. + safemiddle.push_back(chr); + continue; // Character is valid. + } - continue; + // Character is invalid. What should we do? + bool done = false; + switch (invalidchar) + { + case InvalidChar::REJECT: + safemiddle.clear(); + done = true; + break; + + case InvalidChar::STRIP: + continue; + + case InvalidChar::TRUNCATE: + done = true; + break; } - safemiddle.push_back(chr); + if (done) + break; } + ServerInstance->Logs.Debug(MODNAME, "Cleaned {} for cloak: {} => {}", + GetName(), middle, safemiddle); + if (safemiddle.empty()) return {}; // No cloak. @@ -88,8 +125,19 @@ public: void GetLinkData(Module::LinkData& data, std::string& compatdata) override { + switch (invalidchar) + { + case InvalidChar::REJECT: + data["invalidchar"] = "reject"; + break; + case InvalidChar::STRIP: + data["invalidchar"] = "strip"; + break; + case InvalidChar::TRUNCATE: + data["invalidchar"] = "truncate"; + break; + } data["prefix"] = prefix; - data["sanitize"] = sanitize ? "yes" : "no"; data["suffix"] = suffix; } }; -- cgit v1.3.1-10-gc9f91