Refactor InspIRCd::StripColor.

- Only strip characters we actually recognise.
- Strip the value for hex color codes.
This commit is contained in:
Sadie Powell 2024-08-30 16:53:41 +01:00
parent 5d801283bb
commit babb39cb17
2 changed files with 43 additions and 23 deletions

View File

@ -2588,8 +2588,8 @@
#<module name="starttls"> #<module name="starttls">
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# Strip color module: Adds channel mode +S that strips color codes and # Strip color module: Adds channel mode +S that strips IRC formatting
# all control codes except CTCP from all messages sent to the channel. # characters from all messages sent to the channel.
#<module name="stripcolor"> #<module name="stripcolor">
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#

View File

@ -91,31 +91,51 @@ bool InspIRCd::IsValidMask(const std::string& mask)
return true; return true;
} }
void InspIRCd::StripColor(std::string& sentence) void InspIRCd::StripColor(std::string& line)
{ {
/* refactor this completely due to SQUIT bug since the old code would strip last char and replace with \0 --peavey */ for (size_t idx = 0; idx < line.length(); )
int seq = 0;
for (std::string::iterator i = sentence.begin(); i != sentence.end();)
{ {
if (*i == 3) switch (line[idx])
seq = 1;
else if (seq && (( ((*i >= '0') && (*i <= '9')) || (*i == ',') ) ))
{ {
seq++; case '\x02': // Bold
if ( (seq <= 4) && (*i == ',') ) case '\x1D': // Italic
seq = 1; case '\x11': // Monospace
else if (seq > 3) case '\x16': // Reverse
seq = 0; case '\x1E': // Strikethrough
} case '\x1F': // Underline
else case '\x0F': // Reset
seq = 0; line.erase(idx, 1);
break;
// Strip all control codes too except \001 for CTCP case '\x03': // Color
if (seq || ((*i >= 0) && (*i < 32) && (*i != 1))) {
i = sentence.erase(i); auto start = idx;
else while (++idx < line.length() && idx - start < 6)
++i; {
const auto chr = line[idx];
if (chr != ',' && (chr < '0' || chr > '9'))
break;
}
line.erase(start, idx - start);
break;
}
case '\x04': // Hex Color
{
auto start = idx;
while (++idx < line.length() && idx - start < 14)
{
const auto chr = line[idx];
if (chr != ',' && (chr < '0' || chr > '9') && (chr < 'A' || chr > 'F') && (chr < 'a' || chr > 'f'))
break;
}
line.erase(start, idx - start);
break;
}
default: // Non-formatting character.
idx++;
break;
}
} }
} }