From d111fce37b97bf6b1d24d1a444efe0371dbafe2e Mon Sep 17 00:00:00 2001 From: Vankka Date: Thu, 7 Sep 2023 17:41:50 +0300 Subject: [PATCH] Add filtering for which control characters can be sent to Minecraft, add removal of line break spam to default config --- .../config/main/channels/DiscordToMinecraftChatConfig.java | 4 +++- .../messageforwarding/discord/DiscordChatMessageModule.java | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/com/discordsrv/common/config/main/channels/DiscordToMinecraftChatConfig.java b/common/src/main/java/com/discordsrv/common/config/main/channels/DiscordToMinecraftChatConfig.java index 916bd2e9..d49cf39d 100644 --- a/common/src/main/java/com/discordsrv/common/config/main/channels/DiscordToMinecraftChatConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/main/channels/DiscordToMinecraftChatConfig.java @@ -51,7 +51,9 @@ public class DiscordToMinecraftChatConfig { // TODO: more info on regex pairs (String#replaceAll) @Comment("Regex filters for Discord message contents (this is the %message% part of the \"format\" option)") @Untranslated(Untranslated.Type.VALUE) - public Map contentRegexFilters = new LinkedHashMap<>(); + public Map contentRegexFilters = new LinkedHashMap() {{ + put(Pattern.compile("\\n{2,}"), "\n"); + }}; @Comment("Users, bots, roles and webhooks to ignore") public DiscordIgnoresConfig ignores = new DiscordIgnoresConfig(); diff --git a/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordChatMessageModule.java b/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordChatMessageModule.java index eeaaa80c..46cfc7b5 100644 --- a/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordChatMessageModule.java +++ b/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordChatMessageModule.java @@ -52,9 +52,14 @@ import java.util.Collection; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; +import java.util.regex.Pattern; public class DiscordChatMessageModule extends AbstractModule { + // Filter for ASCII control characters which have no use being displayed, but might be misinterpreted somewhere + // Notably this excludes, 0x09 HT (\t), 0x0A LF (\n), 0x0B VT (\v) and 0x0D CR (\r) (which may be used for text formatting) + private static final Pattern ASCII_CONTROL_FILTER = Pattern.compile("[\\u0000-\\u0008\\u000C\\u000E-\\u001F\\u007F]"); + private final Map sends = new ConcurrentHashMap<>(); public DiscordChatMessageModule(DiscordSRV discordSRV) { @@ -182,6 +187,7 @@ public class DiscordChatMessageModule extends AbstractModule { } Placeholders message = new Placeholders(event.getContent()); + message.replaceAll(ASCII_CONTROL_FILTER, ""); chatConfig.contentRegexFilters.forEach(message::replaceAll); Component messageComponent = DiscordSRVMinecraftRenderer.getWithContext(guild, chatConfig, () ->