From 44f72815e1ddc30f2073d05a5cae17c1749c01d2 Mon Sep 17 00:00:00 2001 From: Jerom van der Sar Date: Thu, 4 Jul 2013 22:46:51 +0200 Subject: [PATCH] Added configurable blocked commands --- appinfo.properties | 8 +- buildnumber.properties | 4 +- src/config.yml | 61 +++++- .../Listener/TFM_PlayerListener.java | 207 ++++++------------ .../TotalFreedomMod/TFM_CommandBlocker.java | 144 ++++++++++++ .../TotalFreedomMod/TotalFreedomMod.java | 8 +- src/plugin.yml | 2 +- 7 files changed, 279 insertions(+), 155 deletions(-) create mode 100644 src/me/StevenLawson/TotalFreedomMod/TFM_CommandBlocker.java diff --git a/appinfo.properties b/appinfo.properties index 9a644a7e..4d535afb 100644 --- a/appinfo.properties +++ b/appinfo.properties @@ -1,5 +1,5 @@ -#Tue, 02 Jul 2013 19:01:31 -0400 +#Thu, 04 Jul 2013 22:45:10 +0200 -program.VERSION=2.20 -program.BUILDNUM=242 -program.BUILDDATE=07/02/2013 07\:01 PM +program.VERSION=2.21 +program.BUILDNUM=255 +program.BUILDDATE=07/04/2013 10\:45 PM diff --git a/buildnumber.properties b/buildnumber.properties index e8fd8781..467b32d1 100644 --- a/buildnumber.properties +++ b/buildnumber.properties @@ -1,3 +1,3 @@ #Build Number for ANT. Do not edit! -#Tue Jul 02 19:01:31 EDT 2013 -build.number=243 +#Thu Jul 04 22:45:10 CEST 2013 +build.number=256 diff --git a/src/config.yml b/src/config.yml index 6a7579fc..2325753f 100644 --- a/src/config.yml +++ b/src/config.yml @@ -1,4 +1,4 @@ -# TotalFreedomMod v2.20 Configuration +# TotalFreedomMod v2.21 Configuration # by Madgeek1450 and DarthSalamon # Block placement prevention: @@ -14,7 +14,64 @@ allow_tnt_minecarts: false allow_explosions: false explosiveRadius: 4.0 -#Automatically wipe dropped objects: +# Blocked commands: +# +# How blocked commands work: +# All sections described below are delimited by colon-characters. +# Make sure that you block a command by it's main command name, not an alias +# -as all aliases are blocked by default. Commands are case-insensitive ofcourse. +# You can block specific subcommands aswell. eg: /mail sendall +# +# * The first section is a letter which indicates which rank may use this command +# Valid ranks: +# n - Nobody (Completely disabled) +# c - Senior Admins (Console) +# t - Telnet Admins (Console) +# s - SuperAdmins +# o - Ops (Non-Ops won't be able to use it) +# +# * The second section is a letter which indicates what to do when a player executes that command. +# Valid actions: +# b - Block the command +# a - Block the command and auto-eject that player (for ops and below) +# u - Block the command and Return an "Unknown command" message (Used to hide commands) +# +# * The third section is the command to be blocked, prefixed with a slash +# +# * The fourth section is the message to send to the player when executing that command. +# This should be ommited if unwanted. ChatColors are supported with the &-key. By default +# -the starting ChatColor is set to gray. You can use the default 'That command is blocked.' message +# by using a single underscore. +# +# Examples: +# - 'n:b:/mail sendall:&4You can't send mails to everyone!' +# - 's:a:/stop' +# - 'n:b:/ban:_' +# +blocked_commands: + # Disabled commands + - 'n:b:/time:Server-side time changing is disabled. Please use /ptime to set your own personal time.' + - 'n:b:/md:This server now uses DisguiseCraft instead of MobDisguise. Type /d to disguise and /u to undisguise.' + - 'n:b:/gamemode:Use /creative and /survival to set your gamemode.' + - 'n:b:/ban:_' + - 'n:b:/pardon:_' + - 'n:b:/toggledownfall:_' + + # Superadmin commands + - 's:b:/kick:_' + - 's:b:/socialspy:_' + - 's:b:/kill:_' + - 's:a:/stop' + - 's:a:/reload' + - 's:a:/nuke' + - 's:a:/save-all' + - 's:a:/save-on' + - 's:a:/save-off' + - 's:a:/clearhistory' + - 's:a:/butcher' + - 's:a://butcher' + +# Automatically wipe dropped objects: auto_wipe: true # Nuking prevention: diff --git a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java index 30978182..baef4db4 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java +++ b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java @@ -6,7 +6,15 @@ import java.util.Iterator; import java.util.List; import java.util.Map.Entry; import java.util.regex.Pattern; -import me.StevenLawson.TotalFreedomMod.*; +import me.StevenLawson.TotalFreedomMod.TFM_CommandBlocker; +import me.StevenLawson.TotalFreedomMod.TFM_LandmineData; +import me.StevenLawson.TotalFreedomMod.TFM_Log; +import me.StevenLawson.TotalFreedomMod.TFM_PlayerData; +import me.StevenLawson.TotalFreedomMod.TFM_ServerInterface; +import me.StevenLawson.TotalFreedomMod.TFM_SuperadminList; +import me.StevenLawson.TotalFreedomMod.TFM_UserList; +import me.StevenLawson.TotalFreedomMod.TFM_Util; +import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -22,7 +30,17 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.block.LeavesDecayEvent; -import org.bukkit.event.player.*; +import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerKickEvent; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.server.RemoteServerCommandEvent; +import org.bukkit.event.server.ServerCommandEvent; import org.bukkit.event.server.ServerListPingEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; @@ -367,7 +385,7 @@ public class TFM_PlayerListener implements Listener TFM_PlayerData playerdata = TFM_PlayerData.getPlayerData(p); playerdata.incrementMsgCount(); - // check for spam + // Check for spam if (playerdata.getMsgCount() > 10) { TFM_Util.bcastMsg(p.getName() + " was automatically kicked for spamming chat.", ChatColor.RED); @@ -379,7 +397,7 @@ public class TFM_PlayerListener implements Listener return; } - // check for message repeat + // Check for message repeat if (playerdata.getLastMessage().equalsIgnoreCase(message)) { TFM_Util.playerMsg(p, "Please do not repeat messages."); @@ -388,7 +406,7 @@ public class TFM_PlayerListener implements Listener } playerdata.setLastMessage(message); - // check for muted + // Check for muted if (playerdata.isMuted()) { if (!TFM_SuperadminList.isUserSuperadmin(p)) @@ -403,17 +421,17 @@ public class TFM_PlayerListener implements Listener } } - // strip color from messages + // Strip color from messages message = ChatColor.stripColor(message); - // truncate messages that are too long - 100 characters is vanilla client max + // Truncate messages that are too long - 100 characters is vanilla client max if (message.length() > 100) { message = message.substring(0, 100); TFM_Util.playerMsg(p, "Message was shortened because it was too long to send."); } - // check for caps - Quit messing with this :-/ + // Check for caps if (message.length() >= 6) { int caps = 0; @@ -430,7 +448,7 @@ public class TFM_PlayerListener implements Listener } } - // check for adminchat + // Check for adminchat if (playerdata.inAdminChat()) { TFM_Util.adminChatMessage(p, message, false); @@ -438,7 +456,7 @@ public class TFM_PlayerListener implements Listener return; } - // finally, set message + // Finally, set message event.setMessage(message); } catch (Exception ex) @@ -455,6 +473,7 @@ public class TFM_PlayerListener implements Listener TFM_PlayerData playerdata = TFM_PlayerData.getPlayerData(p); playerdata.incrementMsgCount(); + playerdata.setLastCommand(command); if (playerdata.getMsgCount() > 10) { @@ -476,144 +495,14 @@ public class TFM_PlayerListener implements Listener return; } - if (TotalFreedomMod.preprocessLogEnabled) - { - TFM_Log.info(String.format("[PREPROCESS_COMMAND] %s(%s): %s", p.getName(), ChatColor.stripColor(p.getDisplayName()), command), true); - } - - playerdata.setLastCommand(command); - - command = command.toLowerCase().trim(); - - boolean block_command = false; - - //Commands that will auto-kick the user: - if (Pattern.compile("^/stop").matcher(command).find()) - { - if (!TFM_SuperadminList.isUserSuperadmin(p)) - { - block_command = true; - } - } - else if (Pattern.compile("^/reload").matcher(command).find()) - { - if (!TFM_SuperadminList.isUserSuperadmin(p)) - { - block_command = true; - } - } - else if (Pattern.compile("^/nuke").matcher(command).find()) - { - if (!TFM_SuperadminList.isUserSuperadmin(p)) - { - block_command = true; - } - } - else if (Pattern.compile("^/rl").matcher(command).find()) - { - if (!TFM_SuperadminList.isUserSuperadmin(p)) - { - block_command = true; - } - } - else if (Pattern.compile("^/save-").matcher(command).find()) - { - if (!TFM_SuperadminList.isUserSuperadmin(p)) - { - block_command = true; - } - } - else if (Pattern.compile("^/clearhistory").matcher(command).find()) - { - if (!TFM_SuperadminList.isUserSuperadmin(p)) - { - block_command = true; - } - } - else if (Pattern.compile("^//?butcher").matcher(command).find()) - { - if (!TFM_SuperadminList.isUserSuperadmin(p)) - { - block_command = true; - } - } - - if (block_command) - { - TFM_Util.autoEject(p, "You used a prohibited command: " + command); - TFM_Util.bcastMsg(p.getName() + " was automatically kicked for using harmful commands.", ChatColor.RED); - } - else - { - // commands that will not auto-kick the user, but still deny: - if (Pattern.compile("^/time").matcher(command).find()) - { - p.sendMessage(ChatColor.GRAY + "Server-side time changing is disabled. Please use /ptime to set your own personal time."); - block_command = true; - } - else if (Pattern.compile("^/md").matcher(command).find()) - { - p.sendMessage(ChatColor.GRAY + "This server now uses DisguiseCraft instead of MobDisguise. Type /d to disguise and /u to undisguise."); - block_command = true; - } - else if (Pattern.compile("^/gamemode").matcher(command).find()) - { - p.sendMessage(ChatColor.GRAY + "Use /creative and /survival to set your gamemode."); - block_command = true; - } - else if (Pattern.compile("^/ban").matcher(command).find()) - { - if (!Pattern.compile("^/banlist").matcher(command).find()) - { - block_command = true; - } - } - else if (Pattern.compile("^/kick").matcher(command).find()) - { - if (!TFM_SuperadminList.isUserSuperadmin(p)) - { - block_command = true; - } - } - else if (Pattern.compile("^/kill").matcher(command).find()) - { - if (!TFM_SuperadminList.isUserSuperadmin(p)) - { - block_command = true; - } - } - else if (Pattern.compile("^/socialspy").matcher(command).find()) - { - if (!TFM_SuperadminList.isUserSuperadmin(p)) - { - block_command = true; - } - } - else if (Pattern.compile("^/pardon").matcher(command).find()) - { - block_command = true; - } - else if (Pattern.compile("^/toggledownfall").matcher(command).find()) - { - block_command = true; - } - } - - if (block_command) - { - p.sendMessage(ChatColor.GRAY + "That command is blocked."); - event.setCancelled(true); - return; - } - - // block commands while player is muted + // Block commands if player is muted if (playerdata.isMuted()) { if (!TFM_SuperadminList.isUserSuperadmin(p)) { for (String test_command : BLOCKED_MUTED_CMDS) { - if (Pattern.compile("^/" + test_command.toLowerCase() + " ").matcher(command.toLowerCase()).find()) + if (Pattern.compile("^/" + test_command.toLowerCase() + " ").matcher(command).find()) { p.sendMessage(ChatColor.RED + "That command is blocked while you are muted."); event.setCancelled(true); @@ -625,7 +514,20 @@ public class TFM_PlayerListener implements Listener { playerdata.setMuted(false); } - return; + } + + if (TotalFreedomMod.preprocessLogEnabled) + { + TFM_Log.info(String.format("[PREPROCESS_COMMAND] %s(%s): %s", p.getName(), ChatColor.stripColor(p.getDisplayName()), command), true); + } + + command = command.toLowerCase().trim(); + + // Blocked commands + if (TFM_CommandBlocker.isCommandBlocked(command, event.getPlayer())) + { + // CommandBlocker handles messages and broadcasts + event.setCancelled(true); } if (!TFM_SuperadminList.isUserSuperadmin(p)) @@ -640,6 +542,24 @@ public class TFM_PlayerListener implements Listener } } + @EventHandler(priority = EventPriority.NORMAL) + public void onRemoteServerCommand(RemoteServerCommandEvent event) + { + if (TFM_CommandBlocker.isCommandBlocked("/" + event.getCommand(), event.getSender())) + { + event.setCommand(""); + } + } + + @EventHandler(priority = EventPriority.NORMAL) + public void onServerCommand(ServerCommandEvent event) + { + if (TFM_CommandBlocker.isCommandBlocked("/" + event.getCommand(), event.getSender())) + { + event.setCommand(""); + } + } + @EventHandler(priority = EventPriority.NORMAL) public void onPlayerDropItem(PlayerDropItemEvent event) { @@ -766,6 +686,7 @@ public class TFM_PlayerListener implements Listener { event.setMotd(ChatColor.RED + "You are banned!"); } + if (TotalFreedomMod.adminOnlyMode) { event.setMotd(ChatColor.RED + "Server in AdminMode!"); diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_CommandBlocker.java b/src/me/StevenLawson/TotalFreedomMod/TFM_CommandBlocker.java new file mode 100644 index 00000000..aa7b397a --- /dev/null +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_CommandBlocker.java @@ -0,0 +1,144 @@ +package me.StevenLawson.TotalFreedomMod; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.server.RemoteServerCommandEvent; + + +public class TFM_CommandBlocker +{ + + public static boolean isCommandBlocked(String command, CommandSender sender) + { + String name = sender.getName(); + command = command.toLowerCase().trim(); + + for (String blocked_command : TotalFreedomMod.blockedCommands) + { + String[] parts = blocked_command.split(":"); + if (parts.length < 3 || parts.length > 4) + { + continue; + } + + if (!(command + " ").startsWith(parts[2] + " ")) + { + continue; + } + + if (SenderRank.hasPermissions(sender, parts[0])) + { + continue; + } + + // Past this line indicates that the command is blocked. + + // Optional: Send a message + if (parts.length == 4) + { + if ("_".equals(parts[3])) + { + sender.sendMessage(ChatColor.GRAY + "That command is blocked."); + } + else + { + sender.sendMessage(ChatColor.GRAY + ChatColor.translateAlternateColorCodes('&', parts[3])); + } + } + + TFM_Log.info("Player Rank: " + SenderRank.getSenderRank(sender).rank); + + // Action + if ("b".equals(parts[1])) + { + return true; + } + else if ("a".equals(parts[1])) + { + if (SenderRank.getSenderRank(sender).rank < 2) // Only auto-eject Ops and non-ops + { + TFM_Util.autoEject((Player) sender, "You used a prohibited command: " + command); + TFM_Util.bcastMsg(name + " was automatically kicked for using harmful commands.", ChatColor.RED); + } + return true; + } + else if ("u".equals(parts[1])) + { + sender.sendMessage("Unknown command. Type \"help\" for help."); + return true; + } + return false; + } + + return false; + } + + public enum SenderRank + { + ANYONE("a", 0), + OP("o", 1), + SUPER("s", 2), + TELNET("t", 3), + SENIOR("c", 4), + NOBODY("n", 5); + + private String letter = "n"; + private int rank = 5; + + SenderRank(String letter, int rank) + { + this.letter = letter; + this.rank = rank; + } + + public static boolean hasPermissions(CommandSender sender, String letter) + { + return (getSenderRank(sender).rank >= getSenderRankByLetter(letter).rank); + } + + public static SenderRank getSenderRank(CommandSender sender) + { + if (!(sender instanceof Player)) + { + if (TFM_SuperadminList.isSeniorAdmin(sender)) + { + return SenderRank.SENIOR; + } + else + { + return SenderRank.TELNET; + } + } + + if (TFM_SuperadminList.isUserSuperadmin(sender)) + { + return SenderRank.SUPER; + } + + if (sender.isOp()) + { + return SenderRank.OP; + } + + return SenderRank.ANYONE; + } + + public static SenderRank getSenderRankByLetter(String letter) + { + for (SenderRank rank : SenderRank.values()) + { + if (letter.equals(rank.letter)) + { + return rank; + } + } + return SenderRank.NOBODY; + } + + } + + + + +} diff --git a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java index 74f7484c..469a6e40 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java +++ b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java @@ -211,6 +211,7 @@ public class TotalFreedomMod extends JavaPlugin public static boolean allowFliudSpread = false; public static boolean allowTntMinecarts = false; public static double explosiveRadius = 4.0D; + public static List blockedCommands = new ArrayList(); public static boolean autoEntityWipe = true; public static boolean nukeMonitor = true; public static int nukeMonitorCountBreak = 100; @@ -255,6 +256,7 @@ public class TotalFreedomMod extends JavaPlugin allowExplosions = config.getBoolean("allow_explosions", allowExplosions); allowTntMinecarts = config.getBoolean("allow_tnt_minecarts", allowTntMinecarts); explosiveRadius = config.getDouble("explosiveRadius", explosiveRadius); + blockedCommands = config.getStringList("blocked_commands"); autoEntityWipe = config.getBoolean("auto_wipe", autoEntityWipe); nukeMonitor = config.getBoolean("nuke_monitor", nukeMonitor); nukeMonitorCountBreak = config.getInt("nuke_monitor_count_break", nukeMonitorCountBreak); @@ -281,9 +283,9 @@ public class TotalFreedomMod extends JavaPlugin autoProtectSpawnpoints = config.getBoolean("auto_protect_spawnpoints", autoProtectSpawnpoints); autoProtectRadius = config.getDouble("auto_protect_radius", autoProtectRadius); host_sender_names = config.getStringList("host_sender_names"); - twitterbotEnabled = config.getBoolean("twitterbot_enabled"); - twitterbotUrl = config.getString("twitterbot_url"); - twitterbotSecret = config.getString("twitterbot_secret"); + twitterbotEnabled = config.getBoolean("twitterbot_enabled", twitterbotEnabled); + twitterbotUrl = config.getString("twitterbot_url", twitterbotUrl); + twitterbotSecret = config.getString("twitterbot_secret", twitterbotSecret); } catch (Exception ex) { diff --git a/src/plugin.yml b/src/plugin.yml index c9b98f72..6e377cc2 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: TotalFreedomMod main: me.StevenLawson.TotalFreedomMod.TotalFreedomMod -version: 2.20 +version: 2.21 description: Plugin for the Total Freedom server. authors: [StevenLawson / Madgeek1450, JeromSar / DarthSalamon]