slightly optimize antispam

This commit is contained in:
Bella 2020-04-18 19:10:22 -04:00
parent c01a4db6a2
commit 4a38c7ab10
No known key found for this signature in database
GPG Key ID: DBD4A6030080C8B3
1 changed files with 24 additions and 12 deletions

View File

@ -102,18 +102,21 @@ public class AntiSpam extends Module {
});
@Override
public void onEnable() { messageHistory = new ConcurrentHashMap<>(); }
public void onEnable() {
messageHistory = new ConcurrentHashMap<>();
}
@Override
public void onDisable() { messageHistory = null; }
public void onDisable() {
messageHistory = null;
}
private boolean isSpam(String message) {
/* Quick bandaid fix for mc.player being null when the module is being registered, so don't register it with the map */
final String[] OWN_MESSAGE = {
"^<" + mc.player.getName() + "> ",
"^To .+: ",
};
if ((!filterOwn.getValue() && findPatterns(OWN_MESSAGE, message, false)) || isDirect(filterDMs.getValue(), message) || isDirectOther(filterDMs.getValue(), message)) {
if ((!filterOwn.getValue() && isOwn(OWN_MESSAGE, message)) || isDirect(filterDMs.getValue(), message) || isDirectOther(filterDMs.getValue(), message)) {
return false;
} else {
return detectSpam(removeUsername(message));
@ -121,11 +124,12 @@ public class AntiSpam extends Module {
}
private String removeUsername(String username) { return username.replaceAll("<[^>]*> ", ""); }
private String removeUsername(String username) {
return username.replaceAll("<[^>]*> ", "");
}
private boolean detectSpam(String message) {
for (Map.Entry<Setting<Boolean>, String[]> entry : settingMap.entrySet()) {
if (entry.getKey().getValue() && findPatterns(entry.getValue(), message, true)) {
sendResult(entry.getKey().getName(), message);
@ -137,18 +141,25 @@ public class AntiSpam extends Module {
if (messageHistory == null) messageHistory = new ConcurrentHashMap<>();
boolean isDuplicate = false;
if (messageHistory.containsKey(message) && (System.currentTimeMillis() - messageHistory.get(message)) / 1000 < duplicatesTimeout.getValue()) isDuplicate = true;
if (messageHistory.containsKey(message) && (System.currentTimeMillis() - messageHistory.get(message)) / 1000 < duplicatesTimeout.getValue())
isDuplicate = true;
messageHistory.put(message, System.currentTimeMillis());
if (isDuplicate) {
if (showBlocked.getValue().equals(ShowBlocked.CHAT)) sendChatMessage(getChatName() + "Duplicate: " + message);
else if (showBlocked.getValue().equals(ShowBlocked.LOG_FILE)) KamiMod.log.info(getChatName() + "Duplicate: " + message);
if (showBlocked.getValue().equals(ShowBlocked.CHAT))
sendChatMessage(getChatName() + "Duplicate: " + message);
else if (showBlocked.getValue().equals(ShowBlocked.LOG_FILE))
KamiMod.log.info(getChatName() + "Duplicate: " + message);
}
}
return false;
}
private boolean isOwn(String[] ownFilter, String message) {
return Pattern.compile(ownFilter[0], Pattern.CASE_INSENSITIVE).matcher(message).find();
}
private boolean findPatterns(String[] patterns, String string, boolean removeUsername) {
if (removeUsername) {
string = string.replaceAll("<[^>]*> ", ""); // remove username first
@ -161,7 +172,7 @@ public class AntiSpam extends Module {
return false;
}
private Map<Setting<Boolean>, String[]> settingMap = new HashMap<Setting<Boolean>, String[]>(){{
private Map<Setting<Boolean>, String[]> settingMap = new HashMap<Setting<Boolean>, String[]>() {{
put(wordsLongerThen, FilterPatterns.LONG_WORD);
put(greenText, FilterPatterns.GREEN_TEXT);
put(specialCharBegin, FilterPatterns.SPECIAL_BEGINNING);
@ -385,6 +396,7 @@ public class AntiSpam extends Module {
private void sendResult(String name, String message) {
if (showBlocked.getValue().equals(ShowBlocked.CHAT)) sendChatMessage(getChatName() + name + ": " + message);
else if (showBlocked.getValue().equals(ShowBlocked.LOG_FILE)) KamiMod.log.info(getChatName() + name + ": " + message);
else if (showBlocked.getValue().equals(ShowBlocked.LOG_FILE))
KamiMod.log.info(getChatName() + name + ": " + message);
}
}