This commit is contained in:
len
2021-06-13 13:53:49 +02:00
parent bfadd18020
commit a5c23d842d
24 changed files with 139 additions and 166 deletions
@@ -3,8 +3,6 @@ package com.alttd.chat;
import com.alttd.chat.database.DatabaseConnection;
import net.luckperms.api.LuckPerms;
import java.util.UUID;
public interface ChatAPI {
static ChatAPI get() {
@@ -20,8 +20,8 @@ public class ChatImplementation implements ChatAPI{
Config.init();
luckPerms = getLuckPerms();
databaseConnection = getDataBase();
Queries.createTables();
//databaseConnection = getDataBase(); // todo fix sql
//Queries.createTables(); // todo fix sql
ChatUserManager.initialize(); // loads all the users from the db and adds them.
RegexManager.initialize(); // load the filters and regexes from config
@@ -17,6 +17,7 @@ import java.lang.reflect.Modifier;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
public final class Config {
@@ -162,12 +163,14 @@ public final class Config {
public static List<String> STAFFGROUPS = new ArrayList<>();
public static String MINIMIUMSTAFFRANK = "trainee";
public static String CONSOLENAME = "Console";
public static UUID CONSOLEUUID = UUID.randomUUID();
private static void settings() {
PREFIXGROUPS = getList("settings.prefix-groups",
Lists.newArrayList("discord", "socialmedia", "eventteam", "eventleader", "youtube", "twitch", "developer"));
STAFFGROUPS = getList("settings.staff-groups",
Lists.newArrayList("trainee", "moderator", "headmod", "admin", "manager", "owner"));
CONSOLENAME = getString("settings.console-name", CONSOLENAME);
CONSOLEUUID = UUID.fromString(getString("settings.console-uuid", CONSOLEUUID.toString()));
MINIMIUMSTAFFRANK = getString("settings.minimum-staff-rank", MINIMIUMSTAFFRANK);
}
@@ -2,11 +2,11 @@ package com.alttd.chat.config;
import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.objects.ChatFilter;
import com.alttd.chat.util.ALogger;
import com.google.common.base.Throwables;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ConfigurationOptions;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
import org.yaml.snakeyaml.DumperOptions;
@@ -33,9 +33,6 @@ public final class RegexConfig {
public static ConfigurationNode config;
public static YAMLConfigurationLoader configLoader;
static int version;
static boolean verbose;
public static void init() {
CONFIG_FILE = new File(Config.CONFIGPATH, "filters.yml");;
configLoader = YAMLConfigurationLoader.builder()
@@ -64,10 +61,7 @@ public final class RegexConfig {
e.printStackTrace();
}
verbose = getBoolean("verbose", true);
version = getInt("config-version", 1);
readConfig(Config.class, null);
readConfig(RegexConfig.class, null);
try {
configLoader.save(config);
} catch (IOException e) {
@@ -146,7 +140,9 @@ public final class RegexConfig {
return new ArrayList<>();
}
private void loadChatFilters() {
public static List<String> ChatFilters = new ArrayList<>();
private static void loadChatFilters() {
ALogger.info("loading filters");
// for (Map.Entry<Object, ? extends ConfigurationNode> entry : config.getChildrenMap().entrySet()) {
// String name = entry.getKey().toString(); // the name in the config this filter has
// String type = entry.getValue().getNode("type").getString(); // the type of filter, block or replace
@@ -170,19 +166,18 @@ public final class RegexConfig {
// }
Map<String, Object> properties = new HashMap<>();
config.getChildrenMap().forEach((key, value) -> {
if (value.hasMapChildren()) {
String rkey = key.toString();
properties.put("name", rkey);
for (Map.Entry<Object, ? extends ConfigurationNode> vl : value.getChildrenMap().entrySet()) {
properties.put(rkey + "." + vl.getKey(), vl.getValue().getValue());
}
} else {
properties.put(key.toString(), value.getValue());
config.getChildrenMap().entrySet().forEach(entry -> {
try {
String name = entry.getKey().toString();
String type = entry.getValue().getNode("type").getString();
String regex = entry.getValue().getNode("regex").getString();
String replacement = entry.getValue().getNode("replacement").getString();
List<String> exclusions = entry.getValue().getNode("exclusions").getList(TypeToken.of(String.class), new ArrayList<>());
ChatFilter chatFilter = new ChatFilter(name, type, regex, replacement, exclusions);
RegexManager.addFilter(chatFilter);
} catch(ObjectMappingException ex) {
}
});
ChatFilter chatFilter = new ChatFilter(properties);
RegexManager.addFilter(chatFilter);
}
}
@@ -39,12 +39,12 @@ public class DatabaseConnection {
return;
}
try {
Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
connection = DriverManager.getConnection(
"jdbc:" + Config.DRIVER + "://" + Config.IP + ":" + Config.PORT + "/" + Config.DATABASE + "?autoReconnect=true",
"jdbc:" + Config.DRIVER + "://" + Config.IP + ":" + Config.PORT + "/" + Config.DATABASE + "?autoReconnect=true&enabledTLSProtocols=TLSv1.1",
Config.USERNAME, Config.PASSWORD);
}
}
@@ -3,7 +3,6 @@ package com.alttd.chat.database;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Party;
import com.alttd.chat.util.ALogger;
import java.sql.Connection;
import java.sql.PreparedStatement;
@@ -272,7 +271,6 @@ public class Queries {
if (party == null) {
//TODO log this properly
ALogger.error("INCORRECT LOGGING: party was empty, the party id stored in the database with user " + uuid + " was invalid.");
System.out.println("INCORRECT LOGGING: party was empty, the party id stored in the database with user " + uuid + " was invalid.");
continue;
}
@@ -4,6 +4,7 @@ import com.alttd.chat.database.Queries;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Mail;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -11,25 +12,27 @@ import java.util.stream.Collectors;
public final class ChatUserManager {
private static CopyOnWriteArrayList<ChatUser> chatUsers;// not sure on this, could cause errors later on
private static ArrayList<ChatUser> chatUsers;// not sure on this, could cause errors later on
public static void initialize() {
chatUsers = new CopyOnWriteArrayList<>();
Queries.loadChatUsers();
chatUsers = new ArrayList<>();
//Queries.loadChatUsers(); // todo fix sql
}
public static void addUser(ChatUser user) {
if(getChatUser(user.getUuid()) != null)
if(getChatUser(user.getUuid()) == null)
chatUsers.add(user);
}
public static ChatUser getChatUser(UUID uuid) {
for(ChatUser user : chatUsers) {
if(user.getUuid() == uuid) {
if(uuid.compareTo(user.getUuid()) == 0) {
return user;
}
}
return null;
ChatUser user = new ChatUser(uuid, -1, false, false);
chatUsers.add(user);
return user; // create a new user?
}
public List<Mail> getUnReadMail(ChatUser user) {
@@ -2,7 +2,7 @@ package com.alttd.chat.managers;
import com.alttd.chat.config.RegexConfig;
import com.alttd.chat.objects.ChatFilter;
import com.alttd.chat.objects.FilterType;
import com.alttd.chat.util.ALogger;
import java.util.ArrayList;
import java.util.List;
@@ -18,22 +18,15 @@ public class RegexManager {
}
public static void addFilter(ChatFilter filter) {
ALogger.info("Adding " + filter.getName());
chatFilters.add(filter);
}
// public static boolean violatesFilter(String text) {
// for (Map.Entry<Pattern, ArrayList<String>> 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) { // TODO loop all objects in the list and check if they violate based on the MATCHER
for(ChatFilter chatFilter : chatFilters) {
switch (chatFilter.getType()) {
case CHAT:
break;
case REPLACE:
text = chatFilter.replaceText(text);
break;
@@ -44,9 +37,6 @@ public class RegexManager {
break;
}
}
// for (Map.Entry<String, String> entry : replaceRegex.entrySet()) {
// text = text.replaceAll(entry.getKey(), entry.getValue());
// }
return text;
}
@@ -1,48 +1,45 @@
package com.alttd.chat.objects;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ChatFilter {
private final Properties properties = new Properties();
private final Pattern pattern;
private final String name;
private final FilterType filterType;
private final String regex;
private final Pattern pattern;
private final String replacement;
private final List<String> exclusions;
public ChatFilter(Map<String, Object> props) {
addDefaults();
properties.keySet().stream().filter(props::containsKey).forEach((nkey) -> properties.put(nkey, props.get(nkey)));
pattern = Pattern.compile(getRegex());
filterType = FilterType.getType((String) properties.get("type"));
public ChatFilter(String name, String type, String regex, String replacement, List<String> exclusions) {
this.name = name;
this.filterType = FilterType.getType(type);
this.regex = regex;
this.pattern = Pattern.compile(getRegex());
this.replacement = replacement;
this.exclusions = exclusions;
}
private void addDefaults() {
properties.put("name", "");
properties.put("type", null);
properties.put("regex", "");
properties.put("replacement", "");
properties.put("exclusions", new ArrayList<String>());
public String getName() {
return this.name;
}
public String getRegex() {
return (String) properties.get("regex");
return this.regex;
}
public FilterType getType() {
return filterType;
return this.filterType;
}
public String getReplacement() {
return (String) properties.get("replacement");
return this.replacement;
}
@SuppressWarnings("unchecked")
public List<String> getExclusions() {
return (List<String>) properties.get("exclusions");
return this.exclusions;
}
public boolean matches(String input) {
@@ -53,8 +50,8 @@ public class ChatFilter {
public String replaceText(String input) {
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String group = matcher.group();
if(!getExclusions().contains(group)) { // doesn't work well with capitals, use a stream filter?
String group = matcher.group(); // todo debug
if(getExclusions().stream().noneMatch(s -> s.equalsIgnoreCase(group))) { // idk how heavy this is:/
input = input.replace(group, getReplacement());
}
}
@@ -27,10 +27,10 @@ public class ChatUser {
this.partyId = partyId;
this.toggledPartyChat = toggledChat;
displayName = Queries.getNickname(uuid);
if (displayName == null) {
//displayName = Queries.getNickname(uuid); // todo fix sql
//if (displayName == null) {
displayName = Utility.getDisplayName(uuid);
}
//}
prefix = Utility.getPrefix(uuid, true);
staffPrefix = Utility.getStaffPrefix(uuid);
@@ -2,6 +2,8 @@ package com.alttd.chat.objects;
public enum FilterType {
REPLACE("replace"),
CHAT("chat"),
REPLACEMATCHER("replacematcher"),
BLOCK("block");
private final String name;
@@ -4,7 +4,7 @@ public class ALogger {
private static org.slf4j.Logger logger;
public ALogger(org.slf4j.Logger log) {
public static void init(org.slf4j.Logger log) {
logger = log;
}