leet dictionary config

This commit is contained in:
ImproperIssues
2023-05-27 15:25:04 -07:00
parent 7e30dd7b93
commit 9474eb2ce9
5 changed files with 57 additions and 25 deletions

View File

@@ -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<String> blacklist() {
return get().getStringList(path + "blacklist");
}
public static Map<String, String> leetPatterns() {
Map<String, String> 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 {

View File

@@ -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) {

View File

@@ -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<String> 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<String, String> 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;
}
}

View File

@@ -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");
}
}

View File

@@ -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