-
Notifications
You must be signed in to change notification settings - Fork 17
Feature: AntiSpam module #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
emyfops
wants to merge
9
commits into
1.21.11
Choose a base branch
from
feature/antispam
base: 1.21.11
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+506
−7
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
43ee927
draft: antispam
emyfops 53061c2
additional and better detections
emyfops d9267e9
Update ChatUtils.kt
emyfops 0ae3bcd
applied clanker suggestions
emyfops 0c7d909
Merge branch '1.21.11' into feature/antispam
emyfops 5a5f59c
better detection
emyfops 1f8d595
run the antispam before the other modules
emyfops b67bcaa
added more antispam stuff
emyfops 2f2159a
Update AntiSpam.kt
emyfops File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright 2025 Lambda | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.lambda.config.groups | ||
|
|
||
| import com.lambda.util.Describable | ||
|
|
||
| interface ReplaceConfig { | ||
| val action: ActionStrategy | ||
| val replace: ReplaceStrategy | ||
|
|
||
| val enabled: Boolean get() = action != ActionStrategy.None | ||
|
|
||
| enum class ActionStrategy(override val description: String) : Describable { | ||
| Hide("Hides the message. Will override other strategies."), | ||
| Delete("Deletes the matching part off the message."), | ||
| Replace("Replace the matching string in the message with one of the following replace strategy."), | ||
| None("Don't do anything."), | ||
| } | ||
|
|
||
| enum class ReplaceStrategy(val block: (String) -> String) { | ||
| CensorAll({ it.replaceRange(0..<it.length, "*".repeat(it.length))}), | ||
| CensorHalf({ it.foldIndexed("") { i, acc, char -> if (i % 2 == 0) acc + char else "$acc*" } }), | ||
emyfops marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| KeepFirst({ if (it.length <= 1) it else it.replaceRange(1, it.length, "*".repeat(it.length - 1)) }), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Copyright 2025 Lambda | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.lambda.event.events | ||
|
|
||
| import com.lambda.event.Event | ||
| import com.lambda.event.callback.Cancellable | ||
| import net.minecraft.client.gui.hud.MessageIndicator | ||
| import net.minecraft.network.message.MessageSignatureData | ||
| import net.minecraft.text.Text | ||
|
|
||
| sealed class ChatEvent { | ||
| class Message( | ||
| var message: Text, | ||
| var signature: MessageSignatureData?, | ||
| var indicator: MessageIndicator?, | ||
| ) : Event, Cancellable() | ||
| } |
144 changes: 144 additions & 0 deletions
144
src/main/kotlin/com/lambda/module/modules/chat/AntiSpam.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /* | ||
| * Copyright 2025 Lambda | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.lambda.module.modules.chat | ||
|
|
||
| import com.lambda.config.Configurable | ||
| import com.lambda.config.SettingGroup | ||
| import com.lambda.config.applyEdits | ||
| import com.lambda.config.groups.ReplaceConfig | ||
| import com.lambda.event.events.ChatEvent | ||
| import com.lambda.event.listener.SafeListener.Companion.listen | ||
| import com.lambda.friend.FriendManager | ||
| import com.lambda.module.Module | ||
| import com.lambda.module.tag.ModuleTag | ||
| import com.lambda.util.ChatUtils.addresses | ||
| import com.lambda.util.ChatUtils.colors | ||
| import com.lambda.util.ChatUtils.discord | ||
| import com.lambda.util.ChatUtils.hex | ||
| import com.lambda.util.ChatUtils.sexual | ||
| import com.lambda.util.ChatUtils.slurs | ||
| import com.lambda.util.ChatUtils.swears | ||
| import com.lambda.util.ChatUtils.toAscii | ||
| import com.lambda.util.NamedEnum | ||
| import com.lambda.util.text.MessageDirection | ||
| import com.lambda.util.text.MessageParser | ||
| import com.lambda.util.text.MessageType | ||
| import net.minecraft.text.Text | ||
|
|
||
| object AntiSpam : Module( | ||
| name = "AntiSpam", | ||
| description = "Keeps your chat clean", | ||
| tag = ModuleTag.CHAT, | ||
| ) { | ||
| private val fancyChats by setting("Replace Fancy Chat", false) | ||
|
|
||
| private val filterSelf by setting("Ignore Self", true) | ||
| private val filterFriends by setting("Ignore Friends", true) | ||
| private val filterSystem by setting("Filter System Messages", false) | ||
| private val filterDms by setting("Filter DMs", true) | ||
|
|
||
| private val ignoreSystem by setting("Ignore System", false) | ||
| private val ignoreDms by setting("Ignore DMs", false) | ||
|
|
||
| private val detectSlurs = ReplaceSettings("Slurs", this, Group.Slurs) | ||
| private val detectSwears = ReplaceSettings("Swears", this, Group.Swears) | ||
| private val detectSexual = ReplaceSettings("Sexual", this, Group.Sexual) | ||
| private val detectDiscord = ReplaceSettings("Discord", this, Group.Discord) | ||
| .apply { applyEdits { editTyped(::action) { defaultValue(ReplaceConfig.ActionStrategy.Hide) } } } | ||
| private val detectAddresses = ReplaceSettings("Addresses", this, Group.Addresses) | ||
| .apply { applyEdits { editTyped(::action) { defaultValue(ReplaceConfig.ActionStrategy.Hide) } } } | ||
| private val detectHexBypass = ReplaceSettings("Hex", this, Group.Hex) | ||
| .apply { applyEdits { editTyped(::action) { defaultValue(ReplaceConfig.ActionStrategy.Hide) } } } | ||
| private val detectColors = ReplaceSettings("Colors", this, Group.Colors) | ||
| .apply { applyEdits { editTyped(::action) { defaultValue(ReplaceConfig.ActionStrategy.None) } } } | ||
|
|
||
| enum class Group(override val displayName: String) : NamedEnum { | ||
| General("General"), | ||
| Slurs("Slurs"), | ||
| Swears("Swears"), | ||
| Sexual("Sexual"), | ||
| Discord("Discord Invites"), | ||
| Addresses("IPs and Addresses"), | ||
| Hex("Hex Bypass"), | ||
| Colors("Color Prefixes") | ||
| } | ||
|
|
||
| init { | ||
| listen<ChatEvent.Message> { event -> | ||
| var raw = event.message.string | ||
| val author = MessageParser.playerName(raw) | ||
|
|
||
| if ( | ||
| ignoreSystem && !MessageType.Both.matches(raw) && !MessageDirection.Both.matches(raw) || | ||
| ignoreDms && MessageDirection.Receive.matches(raw) | ||
| ) return@listen | ||
|
|
||
| val slurMatches = slurs.takeIf { detectSlurs.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() } | ||
| val swearMatches = swears.takeIf { detectSwears.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() } | ||
| val sexualMatches = sexual.takeIf { detectSexual.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() } | ||
| val discordMatches = discord.takeIf { detectDiscord.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() } | ||
| val addressMatches = addresses.takeIf { detectAddresses.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() } | ||
| val hexMatches = hex.takeIf { detectHexBypass.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() } | ||
| val colorMatches = colors.takeIf { detectColors.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() } | ||
|
|
||
| var cancelled = false | ||
| var hasMatches = false | ||
|
|
||
| fun doMatch(replace: ReplaceConfig, matches: Sequence<MatchResult>) { | ||
| if ( | ||
| cancelled || | ||
| filterSystem && !MessageType.Both.matches(raw) && !MessageDirection.Both.matches(raw) || | ||
| filterDms && MessageDirection.Receive.matches(raw) || | ||
| filterFriends && author?.let { FriendManager.isFriend(it) } == true || | ||
| filterSelf && MessageType.Self.matches(raw) | ||
| ) return | ||
|
|
||
| when (replace.action) { | ||
| ReplaceConfig.ActionStrategy.Hide -> matches.firstOrNull()?.let { event.cancel(); cancelled = true } // If there's one detection, nuke the whole damn thang | ||
| ReplaceConfig.ActionStrategy.Delete -> matches | ||
| .forEach { raw = raw.replaceRange(it.range, ""); hasMatches = true } | ||
| ReplaceConfig.ActionStrategy.Replace -> matches | ||
| .forEach { raw = raw.replaceRange(it.range, replace.replace.block(it.value)); hasMatches = true } | ||
| ReplaceConfig.ActionStrategy.None -> {} | ||
| } | ||
| } | ||
|
|
||
| doMatch(detectSlurs, slurMatches) | ||
| doMatch(detectSwears, swearMatches) | ||
| doMatch(detectSexual, sexualMatches) | ||
| doMatch(detectDiscord, discordMatches) | ||
| doMatch(detectAddresses, addressMatches) | ||
| doMatch(detectHexBypass, hexMatches) | ||
| doMatch(detectColors, colorMatches) | ||
|
|
||
| if (cancelled) return@listen event.cancel() | ||
| if (!hasMatches) return@listen | ||
|
|
||
| event.message = Text.of(if (fancyChats) raw.toAscii else raw) | ||
| } | ||
| } | ||
|
|
||
| class ReplaceSettings( | ||
| name: String, | ||
| c: Configurable, | ||
| baseGroup: NamedEnum, | ||
| ) : ReplaceConfig, SettingGroup(c) { | ||
| override val action by setting("$name Action Strategy", ReplaceConfig.ActionStrategy.Replace).group(baseGroup) | ||
| override val replace by setting("$name Replace Strategy", ReplaceConfig.ReplaceStrategy.CensorAll) { action == ReplaceConfig.ActionStrategy.Replace }.group(baseGroup) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.