Skip to content

Commit

Permalink
Add chat filtering code
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinLMayes committed Jan 15, 2020
1 parent ec18883 commit c7cd49f
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Java/bukkit/chat/filter/ChatFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package network.walrus.ubiquitous.bukkit.chat.filter;

import network.walrus.common.CommandSender;
import network.walrus.ubiquitous.bukkit.UbiquitousPermissions;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;

/**
* Something which takes a {@link String} and manipulates it to fit certain specifications.
*
* @author Austin Mayes
*/
public abstract class ChatFilter {

private final String permission;

/**
* @param permission needed for players to bypass this filter
*/
public ChatFilter(String permission) {
this.permission = permission;
}

/**
* Filter the specified string
*
* @param original to filter
* @return the filtered string
*/
public abstract String filter(String original);

/**
* Determine if the given sender has the ability to bypass being affected by this filter.
*
* @param sender to check
* @return if the sender can bypass the filter
*/
public boolean canBypass(CommandSender sender) {
return sender instanceof ConsoleCommandSender
|| (sender instanceof Player
&& (((Player) sender).hasPermission(UbiquitousPermissions.FILTER_BYPASS_ALL)
|| ((Player) sender).hasPermission(this.permission)));
}
}
43 changes: 43 additions & 0 deletions Java/bukkit/chat/filter/IPFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package network.walrus.ubiquitous.bukkit.chat.filter;

import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import network.walrus.ubiquitous.bukkit.UbiquitousPermissions;

/**
* Filter which removes domains and IPs from chat messages.
*
* @author Austin Mayes
*/
public class IPFilter extends ChatFilter {

private final Pattern URL_PATTERN =
Pattern.compile(
"(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]");
private final Set<String> allowedDomains;

/**
* Constructor
* @param allowedDomains domains which are not filtered out
*/
public IPFilter(Set<String> allowedDomains) {
super(UbiquitousPermissions.IP_FILTER_BYPASS);
this.allowedDomains = allowedDomains;
}

@Override
public String filter(String original) {
String res = original;
Matcher matcher = URL_PATTERN.matcher(original);
while (matcher.find()) {
String url = matcher.group();
for (String domain : allowedDomains) {
if (!url.toLowerCase().contains(domain)) {
res = original.replaceAll("(?i)" + Pattern.quote(url), "[REDACTED URL]");
}
}
}
return res;
}
}
56 changes: 56 additions & 0 deletions Java/bukkit/chat/filter/WordFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package network.walrus.ubiquitous.bukkit.chat.filter;

import java.util.Random;
import java.util.Set;
import java.util.regex.Pattern;
import network.walrus.ubiquitous.bukkit.UbiquitousPermissions;

/**
* Filter which replaces a set of words with a random replacement.
*
* @author Austin Mayes
*/
public class WordFilter extends ChatFilter {

private static final Random RANDOM = new Random();
private static final String[] REPLACEMENTS =
new String[]{
"Gardyloo",
"Taradiddle",
"Snickersnee",
"Collywobbles",
"Lollygag",
"Malarkey",
"Wabbit",
"Quire",
"Zoanthropy",
"Bumfuzzle",
"Gobbledygook",
"Dongle",
"Pronk",
"Abear",
"Oxter",
"Fartlek",
"Popple"
};
private final Set<String> words;

/**
* @param words to replace
*/
public WordFilter(Set<String> words) {
super(UbiquitousPermissions.WORDS_FILTER_BYPASS);
this.words = words;
}

@Override
public String filter(String original) {
String res = original;
for (String word : words) {
res =
original.replaceAll(
"(?i)" + Pattern.quote(word), REPLACEMENTS[RANDOM.nextInt(REPLACEMENTS.length - 1)]);
}
return res;
}
}

0 comments on commit c7cd49f

Please sign in to comment.