aboutsummaryrefslogtreecommitdiff
path: root/src/modules/m_cloak_user.cpp
diff options
context:
space:
mode:
authorGravatar Sadie Powell2023-06-20 19:17:11 +0100
committerGravatar Sadie Powell2023-06-20 19:17:11 +0100
commitf9416899c56e91f0acfca18ec8f96b144028422d (patch)
tree0be286c7f909328f134b7b5e8d2d9dea0c205f05 /src/modules/m_cloak_user.cpp
parentMisc cleanup of cloak_user. (diff)
Replace <cloak:sanitize> with <cloak:invalidchar>.
This adds support for truncating values e.g. with the nick foo|afk a user will receive a cloak of "foo".
Diffstat (limited to 'src/modules/m_cloak_user.cpp')
-rw-r--r--src/modules/m_cloak_user.cpp68
1 files changed, 58 insertions, 10 deletions
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<unsigned char>(chr)))
+ if (hostmap.test(static_cast<unsigned char>(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;
}
};