-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec18883
commit c7cd49f
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |