Properly escape MineDown in locales

This commit is contained in:
William committed 2022-08-17 21:25:32 +01:00
1 parent 423f18b2c3
commit a8c89da6a1
3 files changed
+45 -14

No files matched your search

@@ -2,11 +2,14 @@ package net.william278.huskhomes.command;
import de.themoep.minedown.MineDown;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Locales;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.PositionMeta;
import net.william278.huskhomes.util.Permission;
import net.william278.huskhomes.util.RegexUtil;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.commons.text.WordUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -140,17 +143,21 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons
final String oldHomeDescription = home.meta.description;
final String newDescription = editArgs;
plugin.getSavedPositionManager().updateHomeMeta(home, new PositionMeta(home.meta.name, newDescription))
.thenAccept(descriptionUpdateResult -> (switch (descriptionUpdateResult.resultType()) {
case SUCCESS -> {
if (home.owner.uuid.equals(editor.uuid)) {
yield plugin.getLocales().getLocale("edit_home_update_description",
home.meta.name,
oldHomeDescription, newDescription);
oldHomeDescription,
newDescription);
} else {
yield plugin.getLocales().getLocale("edit_home_update_description_other",
home.owner.username, home.meta.name,
oldHomeDescription, newDescription);
home.owner.username,
home.meta.name,
oldHomeDescription,
newDescription);
}
}
case FAILED_DESCRIPTION_LENGTH ->
@@ -270,13 +277,10 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons
.ifPresent(this::add);
if (home.meta.description.length() > 0) {
final String escapedDescription = MineDown.escape(home.meta.description
.replaceAll(Pattern.quote("("), "\\(")
.replaceAll(Pattern.quote(")"), "\\)"));
plugin.getLocales().getLocale("edit_home_menu_description",
escapedDescription.length() > 50
? escapedDescription.substring(0, 49).trim() + "…" : escapedDescription,
String.join("\n", escapedDescription.split("(?<=\\G.{40,}\\s)")))
home.meta.description.length() > 50
? home.meta.description.substring(0, 49).trim() + "…" : home.meta.description,
String.join("\n", home.meta.description.split("(?<=\\G.{40,}\\s)")))
.ifPresent(this::add);
}
@@ -1,14 +1,18 @@
package net.william278.huskhomes.config;
import de.themoep.minedown.MineDown;
import de.themoep.minedown.MineDownParser;
import de.themoep.minedown.Util;
import net.william278.annotaml.RootedMap;
import net.william278.annotaml.YamlFile;
import org.apache.commons.text.StringEscapeUtils;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
/**
* Loaded locales used by the plugin to display styled messages
@@ -37,7 +41,9 @@ public class Locales {
}
/**
* Returnsa raw, un-formatted locale loaded from the locales file, with replacements applied
* Returns a raw, un-formatted locale loaded from the locales file, with replacements applied
* <p>
* Note that replacements will not be MineDown-escaped; use {@link #escapeMineDown(String)} to escape replacements
*
* @param localeId String identifier of the locale, corresponding to a key in the file
* @param replacements Ordered array of replacement strings to fill in placeholders with
@@ -59,13 +65,16 @@ public class Locales {
/**
* Returns a MineDown-formatted locale from the locales file, with replacements applied
* <p>
* Note that replacements will be MineDown-escaped before application
*
* @param localeId String identifier of the locale, corresponding to a key in the file
* @param replacements Ordered array of replacement strings to fill in placeholders with
* @return An {@link Optional} containing the replacement-applied, formatted locale corresponding to the id, if it exists
*/
public Optional<MineDown> getLocale(@NotNull String localeId, @NotNull String... replacements) {
return getRawLocale(localeId, replacements).map(MineDown::new);
return getRawLocale(localeId, Arrays.stream(replacements).map(Locales::escapeMineDown)
.toArray(String[]::new)).map(MineDown::new);
}
/**
@@ -86,6 +95,24 @@ public class Locales {
return rawLocale;
}
@NotNull
private static String escapeMineDown(@NotNull String string) {
final StringBuilder value = new StringBuilder();
for (int i = 0; i < string.length(); ++i) {
char c = string.charAt(i);
boolean isEscape = c == '\\';
boolean isColorCode = i + 1 < string.length() && (c == 167 || c == '&');
boolean isEvent = c == '[' || c == ']' || c == '(' || c == ')';
boolean isFormatting = (c == '_' || c == '*' || c == '~' || c == '?' || c == '#') && Util.isDouble(string, i);
if (isEscape || isColorCode || isEvent || isFormatting) {
value.append('\\');
}
value.append(c);
}
return value.toString();
}
@SuppressWarnings("unused")
public Locales() {
}