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".
This commit is contained in:
Sadie Powell 2023-06-20 19:17:11 +01:00
parent 5d706e3d01
commit f9416899c5
2 changed files with 79 additions and 30 deletions

View File

@ -669,20 +669,21 @@
# To use the cloak_user module you must define a <cloak> tag. This #
# tag can have the following fields. #
# #
# class - If non-empty then a comma-delimited list of connect #
# class names that a user has to be in to get the cloak #
# from this tag. #
# class - If non-empty then a comma-delimited list of connect #
# class names that a user has to be in to get the #
# cloak from this tag. #
# #
# prefix - A freeform value to prefix cloaks with. This must not #
# contain spaces. #
# invalidchar - The action to take when an invalid host character is #
# encountered in the cloak. Can be set to "reject" to #
# not apply the cloak, "strip" to remove the invalid #
# host character, or "truncate" to truncate the cloak #
# at the invalid host character. Defaults to "strip". #
# #
# suffix - A freeform value to suffix IPv4/IPv6 cloaks with. This #
# must not contain spaces. #
# prefix - A freeform value to prefix cloaks with. This must #
# not contain spaces. #
# #
# sanitize - If enabled then any characters in the account name, #
# account id or nickname that are not valid in a hostname #
# will be removed rather than skipping the cloak method. #
# Defaults to yes. #
# suffix - A freeform value to suffix IPv4/IPv6 cloaks with. #
# This must not contain spaces. #
# #
# IMPORTANT: Changing these details will break all of your existing #
# bans. If you do not want this to happen you can define multiple #
@ -692,27 +693,27 @@
#
#<cloak method="account"
# class=""
# invalidchar="strip"
# prefix=""
# suffix=".users.example.com"
# sanitize="yes">
# suffix=".users.example.com">
#
#<cloak method="account-id"
# class=""
# invalidchar="strip"
# prefix=""
# suffix=".users.example.com"
# sanitize="yes">
# suffix=".users.example.com">
#
#<cloak method="ident"
# class=""
# invalidchar="strip"
# prefix="Users/"
# suffix=""
# sanitize="yes">
# suffix="">
#
#<cloak method="nick"
# class=""
# invalidchar="strip"
# prefix="Users/"
# suffix=""
# sanitize="yes">
# suffix="">
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# Codepage module: Allows using a custom 8-bit codepage for nicknames

View File

@ -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.
continue;
safemiddle.push_back(chr);
continue; // Character is valid.
}
safemiddle.push_back(chr);
// 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;
}
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;
}
};