Skip to content

Instantly share code, notes, and snippets.

@DreamTexX
Last active March 29, 2021 22:31
Show Gist options
  • Save DreamTexX/2e19aa9e6c7c29e36df9d042bbd92fd7 to your computer and use it in GitHub Desktop.
Save DreamTexX/2e19aa9e6c7c29e36df9d042bbd92fd7 to your computer and use it in GitHub Desktop.
NOT FOR COPY PASTE. Its specific code, written for a specific project, you may need to change the code in some parts.
// Global Vars
private HashMap<ProxiedPlayer, List<List<String>>> previosMessageList = new HashMap<>();
private HashMap<ProxiedPlayer, Integer> anitSpamWarnings = new HashMap<>();
// START BAD WORD FILTER
int filteredWords = 0;
String editedMessage = e.getMessage().replaceAll("[ !.,\\-_?/&%$§\"'()\\[\\]{}=*#+~:;<>|²³`´]", "").replace("0", "o")
.replace("1", "i").replace("2", "z").replace("3", "e").replace("4", "a")
.replace("5", "s").replace("8", "b").toLowerCase();
for (String character : editedMessage.split(""))
editedMessage = editedMessage.replaceAll(character + "{2,}", character);
for (String word : BungeeSystem.blacklistManager.getBlacklistedWords()) {
if (editedMessage.contains(word.toLowerCase()))
filteredWords++;
}
if (filteredWords > 0) {
e.setCancelled(true);
p.sendMessage(TextComponent.fromLegacyText(BungeeSystem.INSTANCE.getConfig().getString("filter.warning_badword_message")));
ProxyServer.getInstance().getScheduler().runAsync(BungeeSystem.INSTANCE, () -> BungeeSystem.INSTANCE.getDiscord().sendFilterWarning(p, e.getMessage(), "Wortfilter"));
MySQLUtils.getActiveModerators().forEach(proxiedPlayer -> {
String notifyMessage = BungeeSystem.INSTANCE.getConfig().getString("filter.notify_message")
.replace("{PLAYER}", p.getName())
.replace("{MESSAGE}", e.getMessage());
proxiedPlayer.sendMessage(TextComponent.fromLegacyText(notifyMessage));
});
return;
}
// END BAD WORD FILTER
// START IP, DOMAIN, LINK, EMAIL-FILTER
String[] preparedMessage = ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', e.getMessage().toLowerCase()))
.replace(" {2,}", " ")
.replace(" . ", ".")
.replace(" .", ".")
.replace("@", "")
.split("[ /]");
for (String item : preparedMessage) {
if (
/*!item.matches("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])")
&& */!item.matches("(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z][a-z-]{0,61}[a-z]")
&& !item.matches("(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"))
continue;
e.setCancelled(true);
p.sendMessage(TextComponent.fromLegacyText(BungeeSystem.INSTANCE.getConfig().getString("filter.warning_message")));
ProxyServer.getInstance().getScheduler().runAsync(BungeeSystem.INSTANCE, () -> BungeeSystem.INSTANCE.getDiscord().sendFilterWarning(p, e.getMessage(), "Domain, Ip oder Mail-Adressen Filter"));
MySQLUtils.getActiveModerators().forEach(proxiedPlayer -> {
String notifyMessage = BungeeSystem.INSTANCE.getConfig().getString("filter.notify_message")
.replace("{PLAYER}", p.getName())
.replace("{MESSAGE}", e.getMessage());
proxiedPlayer.sendMessage(TextComponent.fromLegacyText(notifyMessage));
});
}
// END IP, DOMAIN, LINK, E-MAIL-FILTER
// START ANTI-SPAM
if (!BungeeSystem.INSTANCE.getPm().hasPermission(p, "antispam.bypass") &&
!(e.getMessage().startsWith("/") || e.getMessage().startsWith("7"))) {
List<String> preparedMessage = Arrays.asList(e.getMessage().toLowerCase().split(" "));
List<List<String>> previousMessages = previosMessageList.computeIfAbsent(p, ignored -> new ArrayList<>());
if (!previousMessages.isEmpty()) {
for (List<String> message : previousMessages) {
double i = 0;
for (String msg : preparedMessage) {
if (message.contains(msg))
i++;
}
double equality = i / message.size();
if (equality > 0.85 && equality < 1.15) {
int warnings = anitSpamWarnings.getOrDefault(p, 0) + 1;
if (warnings > 10) {
ProxyServer.getInstance().getPluginManager().dispatchCommand(
ProxyServer.getInstance().getConsole(),
"mute " + p.getName() + " Spam 2h");
p.sendMessage(TextComponent.fromLegacyText(BungeeSystem.INSTANCE.getConfig().getString("antispam.mute_message")));
MySQLUtils.getActiveModerators().forEach(proxiedPlayer -> {
String notifyMessage = BungeeSystem.INSTANCE.getConfig().getString("antispam.mute_notify_message")
.replace("{PLAYER}", p.getName());
proxiedPlayer.sendMessage(TextComponent.fromLegacyText(notifyMessage));
});
warnings = 0;
e.setCancelled(true);
} else {
ProxyServer.getInstance().getScheduler().schedule(BungeeSystem.INSTANCE, () -> {
int warings = anitSpamWarnings.getOrDefault(p, 0);
if (warings > 0) warings--;
anitSpamWarnings.put(p, warings);
}, 5, TimeUnit.MINUTES);
}
anitSpamWarnings.put(p, warnings);
if (!e.isCancelled()) {
ProxyServer.getInstance().getScheduler().runAsync(BungeeSystem.INSTANCE, () -> BungeeSystem.INSTANCE.getDiscord().sendFilterWarning(p, e.getMessage(), "Spamfilter"));
p.sendMessage(TextComponent.fromLegacyText(BungeeSystem.INSTANCE.getConfig().getString("antispam.warning_message")));
e.setCancelled(true);
}
break;
}
}
}
if (previousMessages.size() > 2)
previousMessages.remove(0);
previousMessages.add(preparedMessage);
ProxyServer.getInstance().getScheduler().schedule(BungeeSystem.INSTANCE, () -> previousMessages.remove(preparedMessage), 30, TimeUnit.SECONDS);
}
// END ANTI-SPAM
// BLACKLIST MANAGER
public class BlacklistManager {
private final ArrayList<String> blacklistedWords;
private final Configuration config;
private final File file;
public BlacklistManager() throws IOException {
file = new File(BungeeSystem.INSTANCE.getDataFolder().getPath(), "blacklist.yml");
if (!file.exists()) {
file.createNewFile();
}
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
blacklistedWords = (ArrayList<String>) config.getList("words", new ArrayList<>());
}
public void addWords(List<String> words) {
blacklistedWords.addAll(words);
save();
}
private void save() {
config.set("words", blacklistedWords);
try {
ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, file);
} catch (IOException e) {
e.printStackTrace();
}
}
public void removeWords(List<String> words) {
blacklistedWords.removeAll(words);
save();
}
public ArrayList<String> getBlacklistedWords() {
return blacklistedWords;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment