escape player-provided text in various places

This commit is contained in:
Martin Michelsen
2023-12-26 07:37:00 -08:00
parent 5382e12b8d
commit ccd1b56cae
6 changed files with 74 additions and 25 deletions
+37 -1
View File
@@ -281,7 +281,6 @@ void add_color(StringWriter& w, const char* src, size_t max_input_chars) {
}
src++;
}
w.put<char>(0);
}
string add_color(const string& s) {
@@ -290,6 +289,35 @@ string add_color(const string& s) {
return std::move(w.str());
}
void remove_color(StringWriter& w, const char* src, size_t max_input_chars) {
for (size_t x = 0; (x < max_input_chars) && *src; x++) {
if (*src == '$') {
w.put<char>('%');
w.put<char>('s');
} else if (*src == '%') {
w.put<char>('%');
w.put<char>('%');
} else if (*src == '#') {
w.put<char>('%');
w.put<char>('n');
} else if (*src == '\t') {
w.put<char>('$');
} else if (*src == '\n') {
w.put<char>('#');
} else {
w.put<char>(*src);
}
src++;
}
w.put<char>(0);
}
string remove_color(const string& s) {
StringWriter w;
remove_color(w, s.data(), s.size());
return std::move(w.str());
}
string strip_color(const string& s) {
string ret;
for (size_t r = 0; r < s.size(); r++) {
@@ -302,3 +330,11 @@ string strip_color(const string& s) {
}
return ret;
}
string escape_player_name(const string& name) {
if (name.size() > 2 && name[0] == '\t' && name[1] != 'C') {
return remove_color(name.substr(2));
} else {
return remove_color(name);
}
}