From 19b4c8aaa0d66be7b4c305c457aacf08611bb7a2 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Sun, 2 May 2021 21:42:35 +0200 Subject: [PATCH] Started working on a chat filter, doesn't load from config yet, has example data --- src/main/java/com/alttd/chat/util/Regex.java | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/main/java/com/alttd/chat/util/Regex.java diff --git a/src/main/java/com/alttd/chat/util/Regex.java b/src/main/java/com/alttd/chat/util/Regex.java new file mode 100644 index 0000000..641fe5f --- /dev/null +++ b/src/main/java/com/alttd/chat/util/Regex.java @@ -0,0 +1,38 @@ +package com.alttd.chat.util; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Regex { + + private static final HashMap> cancelRegex = new HashMap<>(); + private static final HashMap replaceRegex = new HashMap<>(); + + public static void initRegex() { + //TODO load data from config (a regex string, and it's exceptions if there are any) + cancelRegex.put(Pattern.compile("\\b([R]+[^\\w]?[4A]+[^\\w]?[P]+(([^\\w]?[E3]+[^\\w]?[DT]*)|([^\\w]?[I!1]+[^\\w]?[S5]+[^\\w]?[T7]+)|([^\\w]?[I!1]+[^\\w]?[N]+[^\\w]?[G69]+)))\\b"), new ArrayList<>()); + //TODO load data from config (a regex string and what to replace it with) + replaceRegex.put(":pirate:", "pirate"); + } + + public static boolean violatesFilter(String text) { + for (Map.Entry> entry : cancelRegex.entrySet()) { + Matcher matcher = entry.getKey().matcher(text); + while (matcher.find()) { + if (!entry.getValue().contains(matcher.group())) return true; + } + } + return false; + } + + public static String replaceText(String text) { + for (Map.Entry entry : replaceRegex.entrySet()) { + text = text.replaceAll(entry.getKey(), entry.getValue()); + } + return text; + } + +}