From d4b8cdc3348e28538d360a672e61f0658a859140 Mon Sep 17 00:00:00 2001 From: phinner <62483793+phinner@users.noreply.github.com> Date: Thu, 14 Dec 2023 18:35:39 +0100 Subject: [PATCH] feat: Expand name protection rules to chat messages --- .../imperium/common/misc/StringExtensions.kt | 4 ++++ .../imperium/mindustry/chat/ChatMessageListener.kt | 13 +++++++++++++ .../mindustry/security/GatekeeperListener.kt | 4 ++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/misc/StringExtensions.kt b/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/misc/StringExtensions.kt index 4cd93747..777094a8 100644 --- a/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/misc/StringExtensions.kt +++ b/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/misc/StringExtensions.kt @@ -39,3 +39,7 @@ fun String.toInetAddressOrNull(): InetAddress? = fun ByteArray.encodeBase64(): String = Base64.getEncoder().encodeToString(this) fun String.decodeBase64(): ByteArray = Base64.getDecoder().decode(this) + +private val LINK_REGEX = Regex("(https?://|discord.gg)") + +fun String.containsLink(): Boolean = LINK_REGEX.containsMatchIn(lowercase()) diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/chat/ChatMessageListener.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/chat/ChatMessageListener.kt index bd9dfebf..652e2242 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/chat/ChatMessageListener.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/chat/ChatMessageListener.kt @@ -30,6 +30,7 @@ import com.xpdustry.imperium.common.inject.InstanceManager import com.xpdustry.imperium.common.inject.get import com.xpdustry.imperium.common.message.Messenger import com.xpdustry.imperium.common.misc.MINDUSTRY_ORANGE_COLOR +import com.xpdustry.imperium.common.misc.containsLink import com.xpdustry.imperium.common.misc.logger import com.xpdustry.imperium.common.misc.stripMindustryColors import com.xpdustry.imperium.common.misc.toHexString @@ -112,6 +113,18 @@ class ChatMessageListener(instances: InstanceManager) : ImperiumApplication.List ctx.message } + chatMessagePipeline.register("anti-links", Priority.NORMAL) { ctx -> + if (ctx.sender == null || !ctx.message.containsLink()) { + return@register ctx.message + } else { + if (ctx.target == ctx.sender) { + ctx.sender.sendMessage( + "[scarlet]You can't send discord invitations or links in the chat.") + } + return@register "" + } + } + val chaoticHourFormat = DecimalFormat("000") placeholderPipeline.registerCaching("subject_playtime", 10.seconds, ::getContextKey) { (subject, query) -> diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/security/GatekeeperListener.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/security/GatekeeperListener.kt index acc7de99..97cdbae8 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/security/GatekeeperListener.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/security/GatekeeperListener.kt @@ -27,6 +27,7 @@ import com.xpdustry.imperium.common.config.ServerConfig import com.xpdustry.imperium.common.inject.InstanceManager import com.xpdustry.imperium.common.inject.get import com.xpdustry.imperium.common.misc.LoggerDelegate +import com.xpdustry.imperium.common.misc.containsLink import com.xpdustry.imperium.common.misc.logger import com.xpdustry.imperium.common.misc.stripMindustryColors import com.xpdustry.imperium.common.network.VpnDetection @@ -75,8 +76,7 @@ class GatekeeperListener(instances: InstanceManager) : ImperiumApplication.Liste pipeline.register("cracked-client", Priority.NORMAL, CrackedClientGatekeeper()) pipeline.register("links", Priority.NORMAL) { context -> - val name = context.name.lowercase() - if (name.contains("discord.gg") || context.name.matches(Regex("https?://"))) { + if (context.name.containsLink()) { GatekeeperResult.Failure("Your name cannot contain a link.") } else { GatekeeperResult.Success