Partly stitch up command protection.

- Never skip undoing changes. This is necessary to also process commands
that are not registered or that are fall-back aliases.
- Check adding all plugin commands always.
- Check for aliases as well for decision if to match a command.
This commit is contained in:
asofold 2014-04-15 22:31:53 +02:00
parent 547dfc890b
commit b8fd9da08f
2 changed files with 106 additions and 66 deletions

View File

@ -25,7 +25,6 @@ public class CommandUtil {
* @return Returns null if not CraftBukkit or CommandMap not available. * @return Returns null if not CraftBukkit or CommandMap not available.
*/ */
public static CommandMap getCommandMap() { public static CommandMap getCommandMap() {
// TODO: compat / null
try { try {
return NCPAPIProvider.getNoCheatPlusAPI().getMCAccess().getCommandMap(); return NCPAPIProvider.getNoCheatPlusAPI().getMCAccess().getCommandMap();
} }
@ -36,16 +35,20 @@ public class CommandUtil {
} }
/** /**
* Fails with an exception if SimpleCommandMap is not found, currently. * Get all Command instances, that NCP can get hold of. Attempt to get a SimpleCommandMap instance from the server to get the actually registered commands, but also get commands from JavaPlugin instances.
* @return * @return
*/ */
public static Collection<Command> getCommands() { public static Collection<Command> getCommands() {
CommandMap commandMap = getCommandMap(); final Collection<Command> commands = new LinkedHashSet<Command>(500);
// All (?) commands from the SimpleCommandMap of the server, if available.
final CommandMap commandMap = getCommandMap();
if (commandMap != null && commandMap instanceof SimpleCommandMap) { if (commandMap != null && commandMap instanceof SimpleCommandMap) {
return ((SimpleCommandMap) commandMap).getCommands(); commands.addAll(((SimpleCommandMap) commandMap).getCommands());
} }
else{ // TODO: Fall-back for Vanilla / CB commands? [Fall-back should be altering permission defaults, though negating permissions is the right way.]
final Collection<Command> commands = new LinkedHashSet<Command>(100);
// Fall-back: plugin commands.
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) { for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (plugin instanceof JavaPlugin) { if (plugin instanceof JavaPlugin) {
final JavaPlugin javaPlugin = (JavaPlugin) plugin; final JavaPlugin javaPlugin = (JavaPlugin) plugin;
@ -53,28 +56,31 @@ public class CommandUtil {
if (map != null) { if (map != null) {
for (String label : map.keySet()) { for (String label : map.keySet()) {
Command command = javaPlugin.getCommand(label); Command command = javaPlugin.getCommand(label);
if (command != null) commands.add(command); if (command != null) {
commands.add(command);
} }
} }
} }
} }
// TODO: Vanilla / CB commands !? }
return commands; return commands;
} }
}
/** /**
* Get the command label (trim + lower case), include server commands [subject to change]. * Get the command label (trim + lower case), include server commands [subject to change].
* @param alias * @param alias
* @param strict If to return null if no command is found. * @param strict If to return null if no command is found.
* @return * @return The command label, if possible to find, or the alias itself (+ trim + lower-case).
*/ */
public static String getCommandLabel(final String alias, final boolean strict) { public static String getCommandLabel(final String alias, final boolean strict) {
final Command command = getCommand(alias); final Command command = getCommand(alias);
if (command == null) { if (command == null) {
return strict ? null : alias.trim().toLowerCase(); return strict ? null : alias.trim().toLowerCase();
} }
else return command.getLabel().trim().toLowerCase(); else {
return command.getLabel().trim().toLowerCase();
}
} }
/** /**
@ -103,13 +109,17 @@ public class CommandUtil {
final List<String> res = new ArrayList<String>(); final List<String> res = new ArrayList<String>();
for (final CheckType checkType : CheckType.values()) { for (final CheckType checkType : CheckType.values()) {
final String name = checkType.name(); final String name = checkType.name();
if (name.startsWith(ref)) res.add(name); if (name.startsWith(ref)) {
res.add(name);
}
} }
if (ref.indexOf('_') == -1) { if (ref.indexOf('_') == -1) {
for (final CheckType checkType : CheckType.values()) { for (final CheckType checkType : CheckType.values()) {
final String name = checkType.name(); final String name = checkType.name();
final String[] split = name.split("_", 2); final String[] split = name.split("_", 2);
if (split.length > 1 && split[1].startsWith(ref)) res.add(name); if (split.length > 1 && split[1].startsWith(ref)) {
res.add(name);
}
} }
} }
if (!res.isEmpty()) { if (!res.isEmpty()) {

View File

@ -47,13 +47,18 @@ public class PermissionUtil {
} }
public void restore() { public void restore() {
Command registered = CommandUtil.getCommand(label); // (Don't skip resetting, as there could be fall-back aliases.)
if (registered == null || registered != command) return; // Command registered = CommandUtil.getCommand(label);
if (!label.equalsIgnoreCase(command.getLabel().trim().toLowerCase())) command.setLabel(label); // if (registered == null || registered != command) return;
if (!label.equalsIgnoreCase(command.getLabel().trim().toLowerCase())) {
command.setLabel(label);
}
command.setPermission(permission); command.setPermission(permission);
if (permission != null && permissionDefault != null) { if (permission != null && permissionDefault != null) {
Permission perm = Bukkit.getPluginManager().getPermission(permission); Permission perm = Bukkit.getPluginManager().getPermission(permission);
if (perm != null) perm.setDefault(permissionDefault); if (perm != null && perm.getDefault() != permissionDefault) {
perm.setDefault(permissionDefault);
}
} }
command.setPermissionMessage(permissionMessage); command.setPermissionMessage(permissionMessage);
} }
@ -61,7 +66,7 @@ public class PermissionUtil {
/** /**
* *
* @param commands * @param commands Command white-list.
* @param permissionBase * @param permissionBase
* @param ops * @param ops
* @return * @return
@ -91,25 +96,28 @@ public class PermissionUtil {
* @param permissionMessage * @param permissionMessage
* @return * @return
*/ */
public static List<CommandProtectionEntry> protectCommands(String permissionBase, Collection<String> ignoredCommands, boolean invertIgnored, boolean ops, String permissionMessage){ public static List<CommandProtectionEntry> protectCommands(final String permissionBase, final Collection<String> ignoredCommands, final boolean invertIgnored, final boolean ops, final String permissionMessage) {
Set<String> checked = new HashSet<String>(); final Set<String> checked = new HashSet<String>();
for (String label : ignoredCommands) { for (String label : ignoredCommands) {
checked.add(CommandUtil.getCommandLabel(label, false)); checked.add(CommandUtil.getCommandLabel(label, false));
} }
PluginManager pm = Bukkit.getPluginManager(); final PluginManager pm = Bukkit.getPluginManager();
Permission rootPerm = pm.getPermission(permissionBase); Permission rootPerm = pm.getPermission(permissionBase);
if (rootPerm == null) { if (rootPerm == null) {
rootPerm = new Permission(permissionBase); rootPerm = new Permission(permissionBase);
pm.addPermission(rootPerm); pm.addPermission(rootPerm);
} }
List<CommandProtectionEntry> changed = new LinkedList<CommandProtectionEntry>(); final List<CommandProtectionEntry> changed = new LinkedList<CommandProtectionEntry>();
for (Command command : CommandUtil.getCommands()){ // Apply protection based on white-list or black-list.
String lcLabel = command.getLabel().trim().toLowerCase(); for (final Command command : CommandUtil.getCommands()) {
if (checked != null){ final String lcLabel = command.getLabel().trim().toLowerCase();
if (checked.contains(lcLabel)){ if (checked.contains(lcLabel) || containsAnyAliases(checked, command)) {
if (!invertIgnored) continue; if (!invertIgnored) {
continue;
} }
else if (invertIgnored) continue; }
else if (invertIgnored) {
continue;
} }
// Set the permission for the command. // Set the permission for the command.
String cmdPermName = command.getPermission(); String cmdPermName = command.getPermission();
@ -133,8 +141,12 @@ public class PermissionUtil {
} }
} }
// Create change history entry. // Create change history entry.
if (cmdHadPerm) changed.add(new CommandProtectionEntry(command, lcLabel, cmdPermName, cmdPerm.getDefault(), command.getPermissionMessage())); if (cmdHadPerm) {
else changed.add(new CommandProtectionEntry(command, lcLabel, null, null, command.getPermissionMessage())); changed.add(new CommandProtectionEntry(command, lcLabel, cmdPermName, cmdPerm.getDefault(), command.getPermissionMessage()));
}
else {
changed.add(new CommandProtectionEntry(command, lcLabel, null, null, command.getPermissionMessage()));
}
// Change // Change
cmdPerm.setDefault(ops ? PermissionDefault.OP : PermissionDefault.FALSE); cmdPerm.setDefault(ops ? PermissionDefault.OP : PermissionDefault.FALSE);
command.setPermissionMessage(permissionMessage); command.setPermissionMessage(permissionMessage);
@ -142,6 +154,24 @@ public class PermissionUtil {
return changed; return changed;
} }
/**
* Check if the checked set contains any trim+lower-case alias of the command.
* @param checked
* @param command
* @return
*/
private static final boolean containsAnyAliases(final Set<String> checked, final Command command) {
final Collection<String> aliases = command.getAliases();
if (aliases != null) {
for (final String alias : aliases) {
if (checked.contains(alias.trim().toLowerCase())) {
return true;
}
}
}
return false;
}
/** /**
* Set a permission as child for all the other permissions given in a Collection. * Set a permission as child for all the other permissions given in a Collection.
* @param permissions Not expected to exist. * @param permissions Not expected to exist.