From fb6dabc7ca0603054bd0c02021fd06a34d1f947d Mon Sep 17 00:00:00 2001 From: Ray Clark Date: Sun, 5 Apr 2020 11:30:12 +1000 Subject: [PATCH] Escaped newlines in strings to prevent faliure of &embedraw --- .../command/registrar/RegistrarUtil.java | 27 +++++++------------ src/main/java/com/ibdiscord/utils/UInput.java | 18 +++++++++++++ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/ibdiscord/command/registrar/RegistrarUtil.java b/src/main/java/com/ibdiscord/command/registrar/RegistrarUtil.java index 6557ca93..f9230b6b 100644 --- a/src/main/java/com/ibdiscord/command/registrar/RegistrarUtil.java +++ b/src/main/java/com/ibdiscord/command/registrar/RegistrarUtil.java @@ -40,6 +40,7 @@ import com.ibdiscord.reminder.Reminder; import com.ibdiscord.reminder.ReminderHandler; import com.ibdiscord.utils.UDatabase; +import com.ibdiscord.utils.UInput; import com.ibdiscord.utils.UString; import de.arraying.gravity.Gravity; import de.arraying.kotys.JSON; @@ -86,15 +87,11 @@ public void register(CommandRegistry registry) { return; } context.assertArguments(2, "error.missing_data"); - StringBuilder jsonBuilder = new StringBuilder(); - for (int i = 1; i < context.getArguments().length; i++) { - jsonBuilder.append(context.getArguments()[i]).append(" "); - } - + String json = UInput.escapeLinebreakInQuotations(UString.concat(context.getArguments(), " ", 1)); JSON embed; try { - embed = new JSON(jsonBuilder.toString()); + embed = new JSON(json.toString()); } catch (IllegalStateException e) { context.replyI18n("error.missing_data"); return; @@ -174,18 +171,14 @@ public void register(CommandRegistry registry) { .noneMatch(it -> it.getName().equalsIgnoreCase("update"))) { target.sendMessage(builder.build()).queue(); } else { - try { - for (Option option : context.getOptions()) { - if (option.getName().equalsIgnoreCase("update")) { - long id = Long.valueOf(option.getValue()); - target.retrieveMessageById(id) - .queue(success -> target.editMessageById(id, builder.build()).queue(), - failure -> context.replyI18n("error.pin_channel")); - break; - } + for (Option option : context.getOptions()) { + if (option.getName().equalsIgnoreCase("update")) { + long id = UString.toLong(option.getValue()); + target.retrieveMessageById(id) + .queue(success -> target.editMessageById(id, builder.build()).queue(), + failure -> context.replyI18n("error.pin_channel")); + break; } - } catch(NumberFormatException exception) { - exception.printStackTrace(); } } }); diff --git a/src/main/java/com/ibdiscord/utils/UInput.java b/src/main/java/com/ibdiscord/utils/UInput.java index 60524d2b..9cf9a945 100644 --- a/src/main/java/com/ibdiscord/utils/UInput.java +++ b/src/main/java/com/ibdiscord/utils/UInput.java @@ -65,6 +65,11 @@ public final class UInput { */ private static final Pattern QUOTATION_MARK = Pattern.compile("[“”„«»]"); + /** + * The regular expression for a (universal) quotation. + */ + private static final Pattern QUOTATION = Pattern.compile("[\"“”„«»][^“”„«»\"]*[“”„«»\"]"); + /** * Gets the member corresponding to the given user input. * @param guild The guild. @@ -204,4 +209,17 @@ public static boolean isValidRegex(String regex) { } } + /** + * Escapes newline characters found within quotations only. + * @param input The string to escape. + * @return Escaped string. + */ + public static String escapeLinebreakInQuotations(String input) { + StringBuilder string = new StringBuilder(input); + QUOTATION.matcher(string.toString()).results().forEach(matchResult -> + string.replace(matchResult.start(), matchResult.end(), + matchResult.group().replaceAll("\n", "\\\\n"))); + return string.toString(); + } + }