@@ -29,11 +29,15 @@ import com.lambda.module.tag.ModuleTag
2929import com.lambda.util.ChatUtils.addresses
3030import com.lambda.util.ChatUtils.colors
3131import com.lambda.util.ChatUtils.discord
32+ import com.lambda.util.ChatUtils.hex
3233import com.lambda.util.ChatUtils.sexual
3334import com.lambda.util.ChatUtils.slurs
3435import com.lambda.util.ChatUtils.swears
3536import com.lambda.util.ChatUtils.toAscii
3637import com.lambda.util.NamedEnum
38+ import com.lambda.util.text.MessageDirection
39+ import com.lambda.util.text.MessageParser
40+ import com.lambda.util.text.MessageType
3741import net.minecraft.text.Text
3842
3943object AntiSpam : Module(
@@ -44,6 +48,8 @@ object AntiSpam : Module(
4448 private val ignoreSelf by setting(" Ignore Self" , true )
4549 private val ignoreFriends by setting(" Ignore Friends" , true )
4650 private val fancyChats by setting(" Replace Fancy Chat" , false )
51+ private val ignoreSystem by setting(" Ignore System" , false )
52+ private val ignoreDms by setting(" Ignore DMs" , false )
4753
4854 private val detectSlurs = ReplaceSettings (" Slurs" , this , Group .Slurs )
4955 private val detectSwears = ReplaceSettings (" Swears" , this , Group .Swears )
@@ -52,6 +58,8 @@ object AntiSpam : Module(
5258 .apply { applyEdits { editTyped(::action) { defaultValue(ReplaceConfig .ActionStrategy .Hide ) } } }
5359 private val detectAddresses = ReplaceSettings (" Addresses" , this , Group .Addresses )
5460 .apply { applyEdits { editTyped(::action) { defaultValue(ReplaceConfig .ActionStrategy .Hide ) } } }
61+ private val detectHexBypass = ReplaceSettings (" Hex" , this , Group .Hex )
62+ .apply { applyEdits { editTyped(::action) { defaultValue(ReplaceConfig .ActionStrategy .Hide ) } } }
5563 private val detectColors = ReplaceSettings (" Colors" , this , Group .Colors )
5664 .apply { applyEdits { editTyped(::action) { defaultValue(ReplaceConfig .ActionStrategy .None ) } } }
5765
@@ -62,34 +70,29 @@ object AntiSpam : Module(
6270 Sexual (" Sexual" ),
6371 Discord (" Discord Invites" ),
6472 Addresses (" IPs and Addresses" ),
73+ Hex (" Hex Bypass" ),
6574 Colors (" Color Prefixes" )
6675 }
6776
6877 init {
6978 listen<ChatEvent .Message > { event ->
70- val author = event.message.string.substringAfter(' <' ).substringBefore(' >' )
71- var content = event.message.string.substringAfter(' ' )
72-
73- if (! ignoreFriends && FriendManager .isFriend(author) ||
74- ! ignoreSelf && player.gameProfile.name == author) return @listen
75-
76- val slurMatches = slurs.takeIf { detectSlurs.enabled }.orEmpty()
77- .flatMap { it.findAll(content).toList().reversed() }
78-
79- val swearMatches = swears.takeIf { detectSwears.enabled }.orEmpty()
80- .flatMap { it.findAll(content).toList().reversed() }
81-
82- val sexualMatches = sexual.takeIf { detectSexual.enabled }.orEmpty()
83- .flatMap { it.findAll(content).toList().reversed() }
84-
85- val discordMatches = discord.takeIf { detectDiscord.enabled }.orEmpty()
86- .flatMap { it.findAll(content).toList().reversed() }
87-
88- val addressMatches = addresses.takeIf { detectAddresses.enabled }.orEmpty()
89- .flatMap { it.findAll(content).toList().reversed() }
90-
91- val colorMatches = colors.takeIf { detectColors.enabled }.orEmpty()
92- .flatMap { it.findAll(content).toList().reversed() }
79+ var raw = event.message.string
80+ val author = MessageParser .playerName(raw)
81+
82+ if (
83+ ignoreSystem && ! MessageType .Both .matches(raw) && ! MessageDirection .Both .matches(raw) ||
84+ ignoreDms && MessageDirection .Receive .matches(raw) ||
85+ ignoreFriends && author?.let { FriendManager .isFriend(it) } == true ||
86+ ignoreSelf && MessageType .Self .matches(raw)
87+ ) return @listen
88+
89+ val slurMatches = slurs.takeIf { detectSlurs.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() }
90+ val swearMatches = swears.takeIf { detectSwears.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() }
91+ val sexualMatches = sexual.takeIf { detectSexual.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() }
92+ val discordMatches = discord.takeIf { detectDiscord.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() }
93+ val addressMatches = addresses.takeIf { detectAddresses.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() }
94+ val hexMatches = hex.takeIf { detectHexBypass.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() }
95+ val colorMatches = colors.takeIf { detectColors.enabled }.orEmpty().flatMap { it.findAll(raw).toList().reversed() }
9396
9497 var cancelled = false
9598 var hasMatches = false
@@ -100,9 +103,9 @@ object AntiSpam : Module(
100103 when (replace.action) {
101104 ReplaceConfig .ActionStrategy .Hide -> matches.firstOrNull()?.let { event.cancel(); cancelled = true } // If there's one detection, nuke the whole damn thang
102105 ReplaceConfig .ActionStrategy .Delete -> matches
103- .forEach { content = content .replaceRange(it.range, " " ); hasMatches = true }
106+ .forEach { raw = raw .replaceRange(it.range, " " ); hasMatches = true }
104107 ReplaceConfig .ActionStrategy .Replace -> matches
105- .forEach { content = content .replaceRange(it.range, replace.replace.block(it.value)); hasMatches = true }
108+ .forEach { raw = raw .replaceRange(it.range, replace.replace.block(it.value)); hasMatches = true }
106109 ReplaceConfig .ActionStrategy .None -> {}
107110 }
108111 }
@@ -112,12 +115,13 @@ object AntiSpam : Module(
112115 doMatch(detectSexual, sexualMatches)
113116 doMatch(detectDiscord, discordMatches)
114117 doMatch(detectAddresses, addressMatches)
118+ doMatch(detectHexBypass, hexMatches)
115119 doMatch(detectColors, colorMatches)
116120
121+ if (cancelled) return @listen event.cancel()
117122 if (! hasMatches) return @listen
118123
119- val postprocessed = if (fancyChats) content.toAscii else content
120- event.message = Text .of(" <$author > $postprocessed " )
124+ event.message = Text .of(if (fancyChats) raw.toAscii else raw)
121125 }
122126 }
123127
0 commit comments