From 9474eb2ce9a6842b769bd12ec1f47375c7a09e97 Mon Sep 17 00:00:00 2001 From: ImproperIssues Date: Sat, 27 May 2023 15:25:04 -0700 Subject: [PATCH] leet dictionary config --- .../fun/ogre/ogredupealias/data/Config.java | 15 ++++++--- .../events/ChatEventListener.java | 4 ++- .../ogredupealias/plugin/ChatConstraints.java | 31 ++++++++++++++++--- .../ogre/ogredupealias/utils/StringUtils.java | 16 ---------- src/main/resources/config.yml | 16 ++++++++++ 5 files changed, 57 insertions(+), 25 deletions(-) diff --git a/src/main/java/fun/ogre/ogredupealias/data/Config.java b/src/main/java/fun/ogre/ogredupealias/data/Config.java index bd2c3e4..5c47cd1 100644 --- a/src/main/java/fun/ogre/ogredupealias/data/Config.java +++ b/src/main/java/fun/ogre/ogredupealias/data/Config.java @@ -6,10 +6,7 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import java.io.File; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; import static fun.ogre.ogredupealias.OgreDupeAlias.instance; import static fun.ogre.ogredupealias.OgreDupeAlias.log; @@ -61,6 +58,16 @@ public abstract class Config { public static List blacklist() { return get().getStringList(path + "blacklist"); } + public static Map leetPatterns() { + Map dictionary = new HashMap<>(); + ConfigurationSection section = get().getConfigurationSection(path + "leet-patterns"); + + if (section == null) return dictionary; + for (String key : section.getKeys(false)) { + dictionary.put(key, section.getString(key)); + } + return dictionary; + } } public static class AntiSpam { diff --git a/src/main/java/fun/ogre/ogredupealias/events/ChatEventListener.java b/src/main/java/fun/ogre/ogredupealias/events/ChatEventListener.java index 32171ab..cd6092d 100644 --- a/src/main/java/fun/ogre/ogredupealias/events/ChatEventListener.java +++ b/src/main/java/fun/ogre/ogredupealias/events/ChatEventListener.java @@ -13,7 +13,9 @@ public class ChatEventListener implements Listener { try { this.handleChatConstraints(e); } - catch (Exception ignore) {} + catch (Exception ignore) { + ignore.printStackTrace(); + } } private void handleChatConstraints(AsyncPlayerChatEvent e) { diff --git a/src/main/java/fun/ogre/ogredupealias/plugin/ChatConstraints.java b/src/main/java/fun/ogre/ogredupealias/plugin/ChatConstraints.java index 7e30a94..c79d7e2 100644 --- a/src/main/java/fun/ogre/ogredupealias/plugin/ChatConstraints.java +++ b/src/main/java/fun/ogre/ogredupealias/plugin/ChatConstraints.java @@ -3,7 +3,6 @@ package fun.ogre.ogredupealias.plugin; import fun.ogre.ogredupealias.data.Config; import fun.ogre.ogredupealias.utils.ArrayUtils; import fun.ogre.ogredupealias.utils.ServerUtils; -import fun.ogre.ogredupealias.utils.StringUtils; import fun.ogre.ogredupealias.utils.Text; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.HoverEvent; @@ -11,6 +10,7 @@ import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.entity.Player; import java.util.*; +import java.util.regex.PatternSyntaxException; public class ChatConstraints { @@ -89,20 +89,21 @@ public class ChatConstraints { if (player.hasPermission("oda.chat.bypass.swear")) return true; // 1 - String msg = StringUtils.fromLeetString(message); + String msg = fromLeetString(message); msg = msg.replaceAll("[^A-Za-z0-9]", "").trim(); // 2 msg = msg.toLowerCase(); // 3 for (String whitelisted : Config.Chat.AntiSwear.whitelist()) { - msg = msg.replaceAll(whitelisted.toLowerCase(), "").trim(); + String key = whitelisted.toLowerCase().replaceAll(" ", ""); + msg = msg.replaceAll(key, "").trim(); } // 4 msg = msg.replaceAll("[. _-]", ""); // 5 List flags = new ArrayList<>(); for (String blacklisted : Config.Chat.AntiSwear.blacklist()) { - String key = blacklisted.toLowerCase(); + String key = blacklisted.toLowerCase().replaceAll(" ", ""); if (msg.contains(key)) { flags.add(blacklisted); msg = msg.replaceAll(key, Text.color("&e" + key + "&f")); @@ -159,4 +160,26 @@ public class ChatConstraints { public static boolean isChatMuted() { return isChatMuted; } + + public static String fromLeetString(String s) { + Map dictionary = Config.Chat.AntiSwear.leetPatterns(); + String msg = s; + + for (String key : dictionary.keySet()) { + if (!s.contains(key)) continue; + try { + if (key.equals("$")) { + msg = msg.replaceAll("\\$", "s"); + } + else { + msg = msg.replaceAll(key, dictionary.get(key)); + } + } + catch (PatternSyntaxException ex) { + String regex = "[" + key + "]"; + msg = msg.replaceAll(regex, dictionary.get(key)); + } + } + return msg; + } } diff --git a/src/main/java/fun/ogre/ogredupealias/utils/StringUtils.java b/src/main/java/fun/ogre/ogredupealias/utils/StringUtils.java index c198ef3..354dc12 100644 --- a/src/main/java/fun/ogre/ogredupealias/utils/StringUtils.java +++ b/src/main/java/fun/ogre/ogredupealias/utils/StringUtils.java @@ -15,20 +15,4 @@ public final class StringUtils { for (String str : sArray) sb.append(capitalize(str)).append(" "); return sb.toString().trim(); } - - public static String fromLeetString(String s) { - return s.replaceAll("0", "o") - .replaceAll("1", "i") - .replaceAll("3", "e") - .replaceAll("4", "a") - .replaceAll("5", "s") - .replaceAll("7", "l") - .replaceAll("\\$", "s") - .replaceAll("!", "i") - .replaceAll("\\+", "t") - .replaceAll("#", "h") - .replaceAll("@", "a") - .replaceAll("<", "c") - .replaceAll("v", "u"); - } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index dc109fb..6d23a4e 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -8,6 +8,22 @@ chat: - glass blacklist: - ass + leet-patterns: + 0: o + 1: i + 3: e + 4: a + 5: s + 6: g + 7: l + "$": s + "!": i + "+": t + "#": h + "@": a + "<": c + V: u + v: u anti-spam: enabled: true cooldown: 5