2013-07-22 02:48:34 +00:00
|
|
|
package me.StevenLawson.TotalFreedomMod;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
2013-08-18 19:52:32 +00:00
|
|
|
import java.util.List;
|
2013-07-22 02:48:34 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader;
|
2014-12-21 09:23:50 +00:00
|
|
|
import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry;
|
2013-07-22 02:48:34 +00:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandMap;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2013-08-18 20:45:35 +00:00
|
|
|
public class TFM_CommandBlocker
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2014-05-19 17:32:25 +00:00
|
|
|
public static final Pattern COMMAND_PATTERN;
|
|
|
|
private static final Map<String, CommandBlockerEntry> BLOCKED_COMMANDS;
|
|
|
|
|
|
|
|
static
|
|
|
|
{
|
|
|
|
COMMAND_PATTERN = Pattern.compile("^/?(\\S+)");
|
|
|
|
BLOCKED_COMMANDS = new HashMap<String, CommandBlockerEntry>();
|
|
|
|
}
|
2013-07-22 02:48:34 +00:00
|
|
|
|
2013-08-18 20:45:35 +00:00
|
|
|
private TFM_CommandBlocker()
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2014-05-19 17:32:25 +00:00
|
|
|
throw new AssertionError();
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 17:32:25 +00:00
|
|
|
public static final void load()
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2014-05-19 17:32:25 +00:00
|
|
|
BLOCKED_COMMANDS.clear();
|
2013-07-22 02:48:34 +00:00
|
|
|
|
2014-05-19 17:32:25 +00:00
|
|
|
final CommandMap commandMap = TFM_CommandLoader.getCommandMap();
|
2013-07-22 02:48:34 +00:00
|
|
|
if (commandMap == null)
|
|
|
|
{
|
|
|
|
TFM_Log.severe("Error loading commandMap.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-20 01:02:00 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2014-12-21 09:23:50 +00:00
|
|
|
List<String> blockedCommands = (List<String>) TFM_ConfigEntry.BLOCKED_COMMANDS.getList();
|
|
|
|
for (String rawEntry : blockedCommands)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2014-03-18 15:07:51 +00:00
|
|
|
final String[] parts = rawEntry.split(":");
|
2013-07-22 02:48:34 +00:00
|
|
|
if (parts.length < 3 || parts.length > 4)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-18 15:07:51 +00:00
|
|
|
final CommandBlockerRank rank = CommandBlockerRank.fromToken(parts[0]);
|
2013-07-22 02:48:34 +00:00
|
|
|
if (rank == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-18 15:07:51 +00:00
|
|
|
final CommandBlockerAction action = CommandBlockerAction.fromToken(parts[1]);
|
2013-07-22 02:48:34 +00:00
|
|
|
if (action == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String command = parts[2];
|
|
|
|
if (command == null || command.isEmpty())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-18 15:07:51 +00:00
|
|
|
final Matcher matcher = COMMAND_PATTERN.matcher(command);
|
2013-07-22 02:48:34 +00:00
|
|
|
if (matcher.find())
|
|
|
|
{
|
|
|
|
command = matcher.group(1);
|
|
|
|
if (command == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
command = command.toLowerCase().trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String message = null;
|
|
|
|
if (parts.length == 4)
|
|
|
|
{
|
|
|
|
message = parts[3];
|
|
|
|
}
|
|
|
|
|
2014-03-18 15:07:51 +00:00
|
|
|
final CommandBlockerEntry blockedCommandEntry = new CommandBlockerEntry(rank, action, command, message);
|
2013-07-22 02:48:34 +00:00
|
|
|
|
2014-03-18 15:07:51 +00:00
|
|
|
final Command bukkitCommand = commandMap.getCommand(command);
|
2013-07-22 02:48:34 +00:00
|
|
|
if (bukkitCommand == null)
|
|
|
|
{
|
|
|
|
//TFM_Log.info("Blocking unknown command: " + blockedCommandEntry.getCommand());
|
2014-05-19 17:32:25 +00:00
|
|
|
BLOCKED_COMMANDS.put(blockedCommandEntry.getCommand(), blockedCommandEntry);
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
blockedCommandEntry.setCommand(bukkitCommand.getName().toLowerCase());
|
|
|
|
|
|
|
|
//TFM_Log.info("Blocking command: " + blockedCommandEntry.getCommand());
|
2014-05-19 17:32:25 +00:00
|
|
|
BLOCKED_COMMANDS.put(blockedCommandEntry.getCommand(), blockedCommandEntry);
|
2013-07-22 02:48:34 +00:00
|
|
|
|
|
|
|
for (String alias : bukkitCommand.getAliases())
|
|
|
|
{
|
|
|
|
//TFM_Log.info("Blocking alias: " + alias.toLowerCase() + " of " + blockedCommandEntry.getCommand());
|
2014-05-19 17:32:25 +00:00
|
|
|
BLOCKED_COMMANDS.put(alias.toLowerCase(), blockedCommandEntry);
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 15:26:10 +00:00
|
|
|
|
2014-08-25 11:57:13 +00:00
|
|
|
TFM_Log.info("Loaded " + BLOCKED_COMMANDS.size() + " blocked commands");
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 17:32:25 +00:00
|
|
|
public static boolean isCommandBlocked(String command, CommandSender sender)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
|
|
|
return isCommandBlocked(command, sender, true);
|
|
|
|
}
|
|
|
|
|
2014-05-19 17:32:25 +00:00
|
|
|
public static boolean isCommandBlocked(String command, CommandSender sender, boolean doAction)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
|
|
|
if (command == null || command.isEmpty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-18 15:07:51 +00:00
|
|
|
final Matcher matcher = COMMAND_PATTERN.matcher(command);
|
2013-07-22 02:48:34 +00:00
|
|
|
if (matcher.find())
|
|
|
|
{
|
|
|
|
command = matcher.group(1);
|
|
|
|
if (command == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
command = command.toLowerCase().trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-18 15:07:51 +00:00
|
|
|
if (command.contains(":"))
|
|
|
|
{
|
|
|
|
TFM_Util.playerMsg(sender, "Plugin-specific commands are disabled.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-19 17:32:25 +00:00
|
|
|
final CommandBlockerEntry entry = BLOCKED_COMMANDS.get(command);
|
2013-07-22 02:48:34 +00:00
|
|
|
|
2014-03-18 15:07:51 +00:00
|
|
|
if (entry != null)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2014-03-18 15:07:51 +00:00
|
|
|
if (!entry.getRank().hasPermission(sender))
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
|
|
|
if (doAction)
|
|
|
|
{
|
2014-03-18 15:07:51 +00:00
|
|
|
entry.doActions(sender);
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static enum CommandBlockerRank
|
|
|
|
{
|
|
|
|
ANYONE("a", 0),
|
|
|
|
OP("o", 1),
|
|
|
|
SUPER("s", 2),
|
|
|
|
TELNET("t", 3),
|
|
|
|
SENIOR("c", 4),
|
|
|
|
NOBODY("n", 5);
|
|
|
|
private final String token;
|
|
|
|
private final int level;
|
|
|
|
|
|
|
|
private CommandBlockerRank(String token, int level)
|
|
|
|
{
|
|
|
|
this.token = token;
|
|
|
|
this.level = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getToken()
|
|
|
|
{
|
2013-07-26 23:48:18 +00:00
|
|
|
return this.token;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasPermission(CommandSender sender)
|
|
|
|
{
|
|
|
|
return getSenderRank(sender).level >= this.level;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static CommandBlockerRank getSenderRank(CommandSender sender)
|
|
|
|
{
|
2014-04-09 20:33:03 +00:00
|
|
|
if (!TFM_AdminList.isSuperAdmin(sender))
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
|
|
|
if (sender.isOp())
|
|
|
|
{
|
|
|
|
return OP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ANYONE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-09 20:33:03 +00:00
|
|
|
if (TFM_AdminList.isSeniorAdmin(sender))
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
|
|
|
return SENIOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(sender instanceof Player))
|
|
|
|
{
|
|
|
|
return TELNET;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SUPER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static CommandBlockerRank fromToken(String token)
|
|
|
|
{
|
|
|
|
for (CommandBlockerRank rank : CommandBlockerRank.values())
|
|
|
|
{
|
|
|
|
if (rank.getToken().equalsIgnoreCase(token))
|
|
|
|
{
|
|
|
|
return rank;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ANYONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 17:32:25 +00:00
|
|
|
private static enum CommandBlockerAction
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2013-12-17 15:24:56 +00:00
|
|
|
BLOCK("b"),
|
|
|
|
BLOCK_AND_EJECT("a"),
|
|
|
|
BLOCK_UNKNOWN("u");
|
2013-07-22 02:48:34 +00:00
|
|
|
private final String token;
|
|
|
|
|
|
|
|
private CommandBlockerAction(String token)
|
|
|
|
{
|
|
|
|
this.token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getToken()
|
|
|
|
{
|
2013-07-26 23:48:18 +00:00
|
|
|
return this.token;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static CommandBlockerAction fromToken(String token)
|
|
|
|
{
|
|
|
|
for (CommandBlockerAction action : CommandBlockerAction.values())
|
|
|
|
{
|
|
|
|
if (action.getToken().equalsIgnoreCase(token))
|
|
|
|
{
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 15:24:56 +00:00
|
|
|
private static class CommandBlockerEntry
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
|
|
|
private final CommandBlockerRank rank;
|
|
|
|
private final CommandBlockerAction action;
|
|
|
|
private String command;
|
|
|
|
private final String message;
|
|
|
|
|
2014-04-21 17:00:39 +00:00
|
|
|
private CommandBlockerEntry(CommandBlockerRank rank, CommandBlockerAction action, String command, String message)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
|
|
|
this.rank = rank;
|
|
|
|
this.action = action;
|
|
|
|
this.command = command;
|
|
|
|
this.message = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CommandBlockerAction getAction()
|
|
|
|
{
|
2013-07-26 23:48:18 +00:00
|
|
|
return this.action;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getCommand()
|
|
|
|
{
|
2013-07-26 23:48:18 +00:00
|
|
|
return this.command;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getMessage()
|
|
|
|
{
|
2013-07-26 23:48:18 +00:00
|
|
|
return this.message;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public CommandBlockerRank getRank()
|
|
|
|
{
|
2013-07-26 23:48:18 +00:00
|
|
|
return this.rank;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setCommand(String command)
|
|
|
|
{
|
|
|
|
this.command = command;
|
|
|
|
}
|
2013-07-26 23:48:18 +00:00
|
|
|
|
|
|
|
private void doActions(CommandSender sender)
|
|
|
|
{
|
|
|
|
if (this.action == CommandBlockerAction.BLOCK_AND_EJECT && sender instanceof Player)
|
|
|
|
{
|
|
|
|
TFM_Util.autoEject((Player) sender, "You used a prohibited command: " + this.command);
|
|
|
|
TFM_Util.bcastMsg(sender.getName() + " was automatically kicked for using harmful commands.", ChatColor.RED);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
String response;
|
|
|
|
|
|
|
|
if (this.action == CommandBlockerAction.BLOCK_UNKNOWN)
|
|
|
|
{
|
|
|
|
response = "Unknown command. Type \"help\" for help.";
|
|
|
|
}
|
|
|
|
else if (this.message == null || "_".equals(this.message))
|
|
|
|
{
|
|
|
|
response = ChatColor.GRAY + "That command is blocked.";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-21 17:58:16 +00:00
|
|
|
response = ChatColor.GRAY + TFM_Util.colorize(this.message);
|
2013-07-26 23:48:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sender.sendMessage(response);
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
}
|