Implement regex-based nickname blacklist

Adds `nick-blacklist` list to config which takes phrases or regexes. Users with essentials.nick.blacklist.bypass can bypass the blacklist.
This commit is contained in:
md678685 2019-08-05 18:59:41 +01:00
parent 2502f5e6f2
commit 93d36779f0
5 changed files with 40 additions and 0 deletions

View File

@ -15,6 +15,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -332,4 +333,6 @@ public interface ISettings extends IConf {
boolean isSafeUsermap(); boolean isSafeUsermap();
boolean logCommandBlockCommands(); boolean logCommandBlockCommands();
Set<Predicate<String>> getNickBlacklist();
} }

View File

@ -35,6 +35,7 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -555,6 +556,7 @@ public class Settings implements net.ess3.api.ISettings {
isWaterSafe = _isWaterSafe(); isWaterSafe = _isWaterSafe();
isSafeUsermap = _isSafeUsermap(); isSafeUsermap = _isSafeUsermap();
logCommandBlockCommands = _logCommandBlockCommands(); logCommandBlockCommands = _logCommandBlockCommands();
nickBlacklist = _getNickBlacklist();
} }
void _lateLoadItemSpawnBlacklist() { void _lateLoadItemSpawnBlacklist() {
@ -1588,4 +1590,25 @@ public class Settings implements net.ess3.api.ISettings {
public boolean logCommandBlockCommands() { public boolean logCommandBlockCommands() {
return logCommandBlockCommands; return logCommandBlockCommands;
} }
private Set<Predicate<String>> nickBlacklist;
private Set<Predicate<String>> _getNickBlacklist() {
Set<Predicate<String>> blacklist = new HashSet<>();
config.getStringList("nick-blacklist").forEach(entry -> {
try {
blacklist.add(Pattern.compile(entry).asPredicate());
} catch (PatternSyntaxException e) {
logger.warning("Invalid nickname blacklist regex: " + entry);
}
});
return blacklist;
}
@Override
public Set<Predicate<String>> getNickBlacklist() {
return nickBlacklist;
}
} }

View File

@ -83,10 +83,17 @@ public class Commandnick extends EssentialsLoopCommand {
throw new Exception(tl("nickNamesAlpha")); throw new Exception(tl("nickNamesAlpha"));
} else if (user != null && (user.isAuthorized("essentials.nick.changecolors") && !user.isAuthorized("essentials.nick.changecolors.bypass")) && !FormatUtil.stripFormat(newNick).equals(user.getName())) { } else if (user != null && (user.isAuthorized("essentials.nick.changecolors") && !user.isAuthorized("essentials.nick.changecolors.bypass")) && !FormatUtil.stripFormat(newNick).equals(user.getName())) {
throw new Exception(tl("nickNamesOnlyColorChanges")); throw new Exception(tl("nickNamesOnlyColorChanges"));
} else if (user != null && !user.isAuthorized("essentials.nick.blacklist.bypass") && isNickBanned(newNick)) {
throw new Exception(tl("nickNameBlacklist", nick));
} }
return newNick; return newNick;
} }
private boolean isNickBanned(String newNick) {
return ess.getSettings().getNickBlacklist().stream()
.anyMatch(entry -> entry.test(newNick));
}
private int getNickLength(final String nick) { private int getNickLength(final String nick) {
return ess.getSettings().ignoreColorsInMaxLength() ? ChatColor.stripColor(nick).length() : nick.length(); return ess.getSettings().ignoreColorsInMaxLength() ? ChatColor.stripColor(nick).length() : nick.length();
} }

View File

@ -32,6 +32,12 @@ nickname-prefix: '~'
# The maximum length allowed in nicknames. The nickname prefix is included in this. # The maximum length allowed in nicknames. The nickname prefix is included in this.
max-nick-length: 15 max-nick-length: 15
# A list of phrases that cannot be used in nicknames. You can include regular expressions here.
# Users with essentials.nick.blacklist.bypass will be able to bypass this filter.
nick-blacklist:
#- Notch
#- '^Dinnerbone'
# When this option is enabled, nickname length checking will exclude color codes in player names. # When this option is enabled, nickname length checking will exclude color codes in player names.
# ie: "&6Notch" has 7 characters (2 are part of a color code), a length of 5 is used when this option is set to true # ie: "&6Notch" has 7 characters (2 are part of a color code), a length of 5 is used when this option is set to true
ignore-colors-in-max-nick-length: false ignore-colors-in-max-nick-length: false

View File

@ -323,6 +323,7 @@ negativeBalanceError=\u00a74User is not allowed to have a negative balance.
nickChanged=\u00a76Nickname changed. nickChanged=\u00a76Nickname changed.
nickDisplayName=\u00a74You have to enable change-displayname in Essentials config. nickDisplayName=\u00a74You have to enable change-displayname in Essentials config.
nickInUse=\u00a74That name is already in use. nickInUse=\u00a74That name is already in use.
nickNameBlacklist=\u00a74That nickname is not allowed.
nickNamesAlpha=\u00a74Nicknames must be alphanumeric. nickNamesAlpha=\u00a74Nicknames must be alphanumeric.
nickNamesOnlyColorChanges=\u00a74Nicknames can only have their colors changed. nickNamesOnlyColorChanges=\u00a74Nicknames can only have their colors changed.
nickNoMore=\u00a76You no longer have a nickname. nickNoMore=\u00a76You no longer have a nickname.