Allow using color codes by name in MOTD files.

This commit is contained in:
Sadie Powell 2024-08-29 22:25:20 +01:00
parent 7a59ed2a90
commit baad39b85c
2 changed files with 69 additions and 1 deletions

View File

@ -353,6 +353,7 @@
# used in your MOTD: # used in your MOTD:
# Bold: \b # Bold: \b
# Color: \c<fg>[,<bg>] # Color: \c<fg>[,<bg>]
# Color (alt): \c{<fg>[,<bg>]}
# Hex Color: \h<fg>[,<bg>] # Hex Color: \h<fg>[,<bg>]
# Italic: \i # Italic: \i
# Monospace: \m (not widely supported) # Monospace: \m (not widely supported)
@ -360,6 +361,12 @@
# Reverse: \r # Reverse: \r
# Strikethrough: \s (not widely supported) # Strikethrough: \s (not widely supported)
# Underline: \u # Underline: \u
#
# When using the alternate color syntax the following colors can be used:
# black, blue, brown, cyan, default, green, grey, light blue,
# light cyan, light green, light grey, magenta orange, pink,
# red, white, yellow.
#
# See https://defs.ircdocs.horse/info/formatting.html for more information # See https://defs.ircdocs.horse/info/formatting.html for more information
# on client support for formatting characters. # on client support for formatting characters.
motd="secretmotd" motd="secretmotd"

View File

@ -139,6 +139,27 @@ void InspIRCd::ProcessColors(std::string& line)
{ 'u', "\x1F" }, // Underline { 'u', "\x1F" }, // Underline
{ 'x', "\x0F" }, // Reset { 'x', "\x0F" }, // Reset
}; };
static const insp::flat_map<std::string, uint8_t, irc::insensitive_swo> colors = {
{ "white", 0 },
{ "black", 1 },
{ "blue", 2 },
{ "green", 3 },
{ "red", 4 },
{ "brown", 5 },
{ "magenta", 6 },
{ "orange", 7 },
{ "yellow", 8 },
{ "light green", 9 },
{ "cyan", 10 },
{ "light cyan", 11 },
{ "light blue", 12 },
{ "pink", 13 },
{ "gray", 14 },
{ "grey", 14 },
{ "light gray", 15 },
{ "light grey", 15 },
{ "default", 99 },
};
for (size_t idx = 0; idx < line.length(); ) for (size_t idx = 0; idx < line.length(); )
{ {
@ -157,7 +178,8 @@ void InspIRCd::ProcessColors(std::string& line)
continue; continue;
} }
const auto it = formats.find(line[idx]); const auto chr = line[idx];
const auto it = formats.find(chr);
if (it == formats.end()) if (it == formats.end())
{ {
// Unknown escape, strip. // Unknown escape, strip.
@ -167,6 +189,45 @@ void InspIRCd::ProcessColors(std::string& line)
line.replace(start, 2, it->second); line.replace(start, 2, it->second);
idx = start + it->second.length(); idx = start + it->second.length();
if (chr != 'c')
continue; // Only colors can have values.
start = idx;
if (idx >= line.length() || line[idx] != '{')
continue; // No color value.
const auto fgend = line.find_first_of(",}", idx + 1);
if (fgend == std::string::npos)
{
// Malformed color value, strip.
line.erase(start);
break;
}
size_t bgend = std::string::npos;
if (line[fgend] == ',')
{
bgend = line.find_first_of("}", fgend + 1);
if (bgend == std::string::npos)
{
// Malformed color value, strip.
line.erase(start);
break;
}
}
const auto fg = colors.find(line.substr(start + 1, fgend - start - 1));
auto tmp = ConvToStr(fg == colors.end() ? 99 : fg->second);
if (bgend != std::string::npos)
{
const auto bg = colors.find(line.substr(fgend + 1, bgend - fgend - 1));
tmp.push_back(',');
tmp.append(ConvToStr(bg == colors.end() ? 99 : bg->second));
}
const auto end = bgend == std::string::npos ? fgend : bgend;
line.replace(start, end - start + 1, tmp);
} }
} }