Skip to content

Commit

Permalink
fix: Fixed FloodCheck not working on some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Sep 15, 2023
1 parent 1fa6509 commit 32a28b5
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final class FloodCheck implements Check {
.maximumSize(3)
.initialCapacity(1)
.build(length -> Pattern.compile(
"(\\w)\\1{"+length+",}|(\\w{28,})|([^\\wñ]{20,})|(^.{220,}$)",
"(\\w)\\1{"+length+",}", //(\w{28,})|([^\wñ]{20,})|(^.{220,}$)
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)
);
private final Pattern pattern;
Expand All @@ -42,7 +42,7 @@ private FloodCheck(final Pattern pattern, final ControlType controlType) {
if (controlType == ControlType.BLOCK) {
return CheckResult.denied(type());
} else {
return CheckResult.modified(type(), matcher.replaceAll(match -> Character.toString(match.group().charAt(0))));
return CheckResult.modified(type(), matcher.replaceAll(match -> String.valueOf(match.group().charAt(0))));
}
}
return CheckResult.allowed();
Expand All @@ -63,7 +63,7 @@ public static FloodCheck.Builder builder(){
return new FloodCheck.Builder();
}

/**Flood Check Builder */
/** Flood Check Builder */
public static class Builder implements AbstractBuilder<FloodCheck> {
private Pattern pattern;
private ControlType controlType;
Expand All @@ -75,7 +75,7 @@ public Builder limit(@NonNegative int limit){
return this;
}

public Builder controlType(ControlType controlType) {
public Builder controlType(final @NotNull ControlType controlType) {
this.controlType = controlType;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
)
}
)
public class ChatRegulator implements ChatRegulatorAPI {
public final class ChatRegulator implements ChatRegulatorAPI {
@Inject
private EventManager eventManager;
@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.github._4drian3d.chatregulator.api.result.CheckResult;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -25,7 +24,7 @@ public static LazyDetection checks(final CheckProvider<? extends Check>... check

public @NotNull CompletableFuture<CheckResult> detect(final @NotNull InfractionPlayer player, final @NotNull String string) {
return CompletableFuture.supplyAsync(() -> {
final AtomicReference<Map.Entry<InfractionType, String>> modifiedString = new AtomicReference<>();
final AtomicReference<InfractionDetection> modifiedString = new AtomicReference<>();
for (final CheckProvider<? extends Check> provider : checks) {
final Check providedCheck = provider.provide(player);
if (providedCheck == null) {
Expand All @@ -41,17 +40,20 @@ public static LazyDetection checks(final CheckProvider<? extends Check>... check
}

if (result instanceof final CheckResult.ReplaceCheckResult replaceCheckResult) {
modifiedString.set(Map.entry(replaceCheckResult.infractionType(), replaceCheckResult.replaced()));
modifiedString.set(new InfractionDetection(replaceCheckResult.infractionType(), replaceCheckResult.replaced()));
break;
}
}
final Map.Entry<InfractionType, String> finalResult = modifiedString.get();
final InfractionDetection finalResult = modifiedString.get();
if (finalResult == null) {
return CheckResult.allowed();
}
if (!Objects.equals(finalResult.getValue(), string)) {
return CheckResult.modified(finalResult.getKey(), finalResult.getValue());
if (!Objects.equals(finalResult.modified, string)) {
return CheckResult.modified(finalResult.infractionType, finalResult.modified);
}
return CheckResult.allowed();
});
}

private record InfractionDetection(@NotNull InfractionType infractionType, @NotNull String modified) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io.github._4drian3d.chatregulator.plugin.ChatRegulator;

public interface RegulatorExecutor<E> extends AwaitingEventExecutor<E> {
default void register(ChatRegulator plugin, EventManager eventManager) {
default void register(final ChatRegulator plugin, final EventManager eventManager) {
eventManager.register(plugin, eventClass(), postOrder(), this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class ChatListener implements RegulatorExecutor<PlayerChatEvent> {
private EventManager eventManager;

@Override
public @Nullable EventTask executeAsync(PlayerChatEvent event) {
public @Nullable EventTask executeAsync(final PlayerChatEvent event) {
if (!event.getResult().isAllowed()) {
return null;
}
Expand All @@ -68,18 +68,18 @@ public final class ChatListener implements RegulatorExecutor<PlayerChatEvent> {
return CheckResult.allowed();
}).thenAccept(checkResult -> {
if (checkResult instanceof final CheckResult.DeniedCheckResult deniedResult) {
eventManager.fireAndForget(new ChatInfractionEvent(player, deniedResult.infractionType(), checkResult, event.getMessage()));
this.eventManager.fireAndForget(new ChatInfractionEvent(player, deniedResult.infractionType(), checkResult, event.getMessage()));
player.onDetection(deniedResult, event.getMessage());
event.setResult(ChatResult.denied());
} else if (checkResult instanceof final CheckResult.ReplaceCheckResult replaceResult) {
String finalMessage = replaceResult.replaced();

final Configuration configuration = configurationContainer.get();
if (configuration.getFormatterConfig().enabled()) {
final Configuration.Formatter configuration = configurationContainer.get().getFormatterConfig();
if (configuration.enabled()) {
finalMessage = applyFormat(finalMessage, configuration);
}
player.getChain(SourceType.CHAT).executed(event.getMessage());
eventManager.fireAndForget(new ChatInfractionEvent(player, replaceResult.infractionType(), checkResult, event.getMessage()));
this.eventManager.fireAndForget(new ChatInfractionEvent(player, replaceResult.infractionType(), checkResult, event.getMessage()));
player.onDetection(replaceResult, event.getMessage());
event.setResult(ChatResult.message(finalMessage));
} else {
Expand All @@ -102,15 +102,15 @@ public PostOrder postOrder() {
return PostOrder.EARLY;
}

public static @NotNull String applyFormat(final @NotNull String string, Configuration config) {
public static @NotNull String applyFormat(final @NotNull String string, Configuration.Formatter config) {
return firstLetterUppercase(addFinalDot(string, config), config);
}
public static @NotNull String firstLetterUppercase(@NotNull final String string, Configuration config) {
if (!config.getFormatterConfig().setFirstLetterUppercase()) return string;
public static @NotNull String firstLetterUppercase(@NotNull final String string, Configuration.Formatter config) {
if (!config.setFirstLetterUppercase()) return string;
return Replacer.firstLetterUppercase(string);
}
public static String addFinalDot(final String string, Configuration config) {
return config.getFormatterConfig().setFinalDot()
public static String addFinalDot(final String string, Configuration.Formatter config) {
return config.setFinalDot()
? Replacer.addFinalDot(string)
: string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ private boolean checkIfCanCheck(final String command) {
logger.error("An error occurred while calculating command result", ex);
} else {
if (result instanceof final CheckResult.DeniedCheckResult deniedResult) {
eventManager.fireAndForget(new CommandInfractionEvent(infractionPlayer, deniedResult.infractionType(), result, event.getCommand()));
this.eventManager.fireAndForget(new CommandInfractionEvent(infractionPlayer, deniedResult.infractionType(), result, event.getCommand()));
infractionPlayer.onDetection(deniedResult, event.getCommand());
event.setResult(CommandExecuteEvent.CommandResult.denied());
return null;
}
if (result instanceof final CheckResult.ReplaceCheckResult replaceResult) {
this.eventManager.fireAndForget(new CommandInfractionEvent(infractionPlayer, replaceResult.infractionType(), result, event.getCommand()));
final String replacedCommand = replaceResult.replaced();
infractionPlayer.getChain(SourceType.COMMAND).executed(replacedCommand);
infractionPlayer.onDetection(replaceResult, event.getCommand());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.github._4drian3d.chatregulator.plugin.config.Messages;
import io.github._4drian3d.chatregulator.plugin.listener.RegulatorExecutor;
import io.github._4drian3d.chatregulator.plugin.placeholders.formatter.Formatter;
import io.github._4drian3d.velocityhexlogger.HexLogger;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
Expand All @@ -26,6 +27,8 @@ public final class SpyListener implements RegulatorExecutor<CommandExecuteEvent>
private Formatter formatter;
@Inject
private ProxyServer proxyServer;
@Inject
private HexLogger logger;

@Override
public EventTask executeAsync(final CommandExecuteEvent event) {
Expand All @@ -50,12 +53,12 @@ public EventTask executeAsync(final CommandExecuteEvent event) {
Placeholder.unparsed("player", player.getUsername())
)
);
proxyServer.getAllPlayers().forEach(pl -> {
for (final Player pl : this.proxyServer.getAllPlayers()) {
if (Permission.COMMAND_SPY_ALERT.test(pl)) {
pl.sendMessage(message);
}
});
proxyServer.getConsoleCommandSource().sendMessage(message);
}
logger.info(message);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public final class JoinListener implements RegulatorExecutor<PostLoginEvent> {

@Override
public EventTask executeAsync(PostLoginEvent event) {
return EventTask.async(() -> playerManager.getPlayer(event.getPlayer()).isOnline(true));
return EventTask.async(() -> this.playerManager.getPlayer(event.getPlayer()).isOnline(true));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class LeaveListener implements RegulatorExecutor<DisconnectEvent> {
private ConfigurationContainer<Configuration> configurationContainer;

@Override
public @Nullable EventTask executeAsync(DisconnectEvent event) {
public @Nullable EventTask executeAsync(final DisconnectEvent event) {
if (event.getLoginStatus() == DisconnectEvent.LoginStatus.CONFLICTING_LOGIN) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class ProviderModule extends AbstractModule {
@Singleton
@Provides
private CheckProvider<RegexCheck> regex(
ConfigurationContainer<Checks> configurationContainer,
ConfigurationContainer<Blacklist> blacklistContainer
final ConfigurationContainer<Checks> configurationContainer,
final ConfigurationContainer<Blacklist> blacklistContainer
) {
return player -> {
InfractionPlayerImpl infractionPlayer = (InfractionPlayerImpl) player;
Expand All @@ -38,12 +38,12 @@ private CheckProvider<RegexCheck> regex(
private CheckProvider<CapsCheck> caps(final ConfigurationContainer<Checks> configurationContainer) {
return player -> {
final InfractionPlayerImpl infractionPlayer = (InfractionPlayerImpl) player;
final Checks configuration = configurationContainer.get();
if (infractionPlayer.isAllowed(InfractionType.CAPS) && configuration.isEnabled(InfractionType.CAPS)) {
final Checks.Caps configuration = configurationContainer.get().getCapsConfig();
if (infractionPlayer.isAllowed(InfractionType.CAPS) && configuration.enabled()) {
return CapsCheck.builder()
.limit(configuration.getCapsConfig().limit())
.controlType(configuration.getCapsConfig().getControlType())
.algorithm(configuration.getCapsConfig().getAlgorithm())
.limit(configuration.limit())
.controlType(configuration.getControlType())
.algorithm(configuration.getAlgorithm())
.build();
}
return null;
Expand All @@ -53,8 +53,8 @@ private CheckProvider<CapsCheck> caps(final ConfigurationContainer<Checks> confi
@Singleton
@Provides
private CheckProvider<CommandCheck> command(
ConfigurationContainer<Checks> configurationContainer,
ConfigurationContainer<Blacklist> blacklistContainer
final ConfigurationContainer<Checks> configurationContainer,
final ConfigurationContainer<Blacklist> blacklistContainer
) {
return player -> {
final InfractionPlayerImpl infractionPlayer = (InfractionPlayerImpl) player;
Expand All @@ -70,7 +70,7 @@ private CheckProvider<CommandCheck> command(

@Singleton
@Provides
private CheckProvider<FloodCheck> flood(ConfigurationContainer<Checks> configurationContainer) {
private CheckProvider<FloodCheck> flood(final ConfigurationContainer<Checks> configurationContainer) {
return player -> {
final InfractionPlayerImpl infractionPlayer = (InfractionPlayerImpl) player;
final Checks configuration = configurationContainer.get();
Expand All @@ -87,7 +87,7 @@ private CheckProvider<FloodCheck> flood(ConfigurationContainer<Checks> configura
@Singleton
@Provides
@Named("command")
private CheckProvider<SpamCheck> commandSpam(ConfigurationContainer<Checks> configurationContainer) {
private CheckProvider<SpamCheck> commandSpam(final ConfigurationContainer<Checks> configurationContainer) {
return player -> {
final InfractionPlayerImpl infractionPlayer = (InfractionPlayerImpl) player;
final Checks configuration = configurationContainer.get();
Expand All @@ -104,7 +104,7 @@ private CheckProvider<SpamCheck> commandSpam(ConfigurationContainer<Checks> conf
@Singleton
@Provides
@Named("chat")
private CheckProvider<SpamCheck> chatSpam(ConfigurationContainer<Checks> configurationContainer) {
private CheckProvider<SpamCheck> chatSpam(final ConfigurationContainer<Checks> configurationContainer) {
return player -> {
final InfractionPlayerImpl infractionPlayer = (InfractionPlayerImpl) player;
final Checks configuration = configurationContainer.get();
Expand All @@ -120,7 +120,7 @@ private CheckProvider<SpamCheck> chatSpam(ConfigurationContainer<Checks> configu

@Singleton
@Provides
private CheckProvider<SyntaxCheck> syntax(ConfigurationContainer<Checks> configurationContainer) {
private CheckProvider<SyntaxCheck> syntax(final ConfigurationContainer<Checks> configurationContainer) {
return player -> {
final InfractionPlayerImpl infractionPlayer = (InfractionPlayerImpl) player;
final Checks configuration = configurationContainer.get();
Expand All @@ -136,7 +136,7 @@ private CheckProvider<SyntaxCheck> syntax(ConfigurationContainer<Checks> configu
@Singleton
@Provides
@Named("command")
private CheckProvider<CooldownCheck> commandCooldown(ConfigurationContainer<Checks> configurationContainer) {
private CheckProvider<CooldownCheck> commandCooldown(final ConfigurationContainer<Checks> configurationContainer) {
return player -> {
final InfractionPlayerImpl infractionPlayer = (InfractionPlayerImpl) player;
final Checks.Cooldown config = configurationContainer.get().getCooldownConfig();
Expand All @@ -154,7 +154,7 @@ private CheckProvider<CooldownCheck> commandCooldown(ConfigurationContainer<Chec
@Singleton
@Provides
@Named("chat")
private CheckProvider<CooldownCheck> chatCooldown(ConfigurationContainer<Checks> configurationContainer) {
private CheckProvider<CooldownCheck> chatCooldown(final ConfigurationContainer<Checks> configurationContainer) {
return player -> {
final InfractionPlayerImpl infractionPlayer = (InfractionPlayerImpl) player;
final Checks.Cooldown config = configurationContainer.get().getCooldownConfig();
Expand All @@ -171,7 +171,7 @@ private CheckProvider<CooldownCheck> chatCooldown(ConfigurationContainer<Checks>

@Singleton
@Provides
private CheckProvider<UnicodeCheck> unicode(ConfigurationContainer<Checks> configurationContainer) {
private CheckProvider<UnicodeCheck> unicode(final ConfigurationContainer<Checks> configurationContainer) {
return player -> {
final InfractionPlayerImpl infractionPlayer = (InfractionPlayerImpl) player;
final Checks.Unicode config = configurationContainer.get().getUnicodeConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

class FloodTest {
@ParameterizedTest
@CsvSource({
"aa floOoOOOooOod aa, aa flod aa",
"helloooooooooooooo, hello"
"aa floOoOOOooOod aa",
"helloooooooooooooo"
})
@DisplayName("Flood Check")
void floodCheck(String original, String expected) {
void floodCheck(String original) {
CheckResult result = FloodCheck.builder()
.limit(5)
.controlType(ControlType.BLOCK)
Expand All @@ -29,13 +29,33 @@ void floodCheck(String original, String expected) {

@Test
@DisplayName("MultiFlood Replacement")
void multiFlood(){
void multiFlood() {
String original = "helloooooo everyoneeeeeee";
FloodCheck check = FloodCheck.builder()
.controlType(ControlType.REPLACE)
.limit(5)
.build();

assertTrue(check.check(TestsUtils.dummyPlayer(), original).shouldModify());
final var result = check.check(TestsUtils.dummyPlayer(), original);
assertTrue(result.shouldModify());
final var replaceableResult = assertInstanceOf(CheckResult.ReplaceCheckResult.class, result);
assertEquals("hello everyone", replaceableResult.replaced());
}

@ParameterizedTest
@CsvSource({
"gaaaaaaaa causa, ga causa",
"helllllllllllllllllllllllllllllllllllllllllllllo, helo",
"holabcdefghijkaaaaaaabc, holabcdefghijkabc"
})
void testTest(String original, String expected) {
final FloodCheck check = FloodCheck.builder()
.controlType(ControlType.REPLACE)
.limit(5)
.build();

CheckResult.ReplaceCheckResult result = assertInstanceOf(CheckResult.ReplaceCheckResult.class, check.check(TestsUtils.dummyPlayer(), original));

assertEquals(expected, result.replaced());
}
}

0 comments on commit 32a28b5

Please sign in to comment.