diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtempbanip.java b/Essentials/src/com/earth2me/essentials/commands/Commandtempbanip.java new file mode 100644 index 000000000..4d74c707d --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtempbanip.java @@ -0,0 +1,87 @@ +package com.earth2me.essentials.commands; + +import com.earth2me.essentials.CommandSource; +import com.earth2me.essentials.Console; +import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.DateUtil; +import com.earth2me.essentials.utils.FormatUtil; +import org.bukkit.BanList; +import org.bukkit.Server; +import org.bukkit.entity.Player; + +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.logging.Level; + +import static com.earth2me.essentials.I18n.tl; + +public class Commandtempbanip extends EssentialsCommand { + public Commandtempbanip() { + super("tempbanip"); + } + + @Override + public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception { + if (args.length < 2) { + throw new NotEnoughArgumentsException(); + } + + final String senderName = sender.isPlayer() ? sender.getPlayer().getDisplayName() : Console.NAME; + + String ipAddress; + if (FormatUtil.validIP(args[0])) { + ipAddress = args[0]; + } else { + try { + User player = getPlayer(server, args, 0, true, true); + ipAddress = player.getLastLoginAddress(); + } catch (PlayerNotFoundException ex) { + ipAddress = args[0]; + } + } + + if (ipAddress.isEmpty()) { + throw new PlayerNotFoundException(); + } + + final String time = getFinalArg(args, 1); + final long banTimestamp = DateUtil.parseDateDiff(time, true); + String banReason = DateUtil.removeTimePattern(time); + + final long maxBanLength = ess.getSettings().getMaxTempban() * 1000; + if (maxBanLength > 0 && ((banTimestamp - GregorianCalendar.getInstance().getTimeInMillis()) > maxBanLength) && sender.isPlayer() && !(ess.getUser(sender.getPlayer()).isAuthorized("essentials.tempban.unlimited"))) { + sender.sendMessage(tl("oversizedTempban")); + throw new NoChargeException(); + } + + if (banReason.length() < 2) { + banReason = tl("defaultBanReason"); + } + + String banDisplay = tl("banFormat", banReason, senderName); + + ess.getServer().getBanList(BanList.Type.IP).addBan(ipAddress, banReason, new Date(banTimestamp), senderName); + final String message = tl("playerTempBanIpAddress", senderName, ipAddress, DateUtil.formatDateDiff(banTimestamp), banReason); + + for (Player player : ess.getServer().getOnlinePlayers()) { + if (player.getAddress().getAddress().getHostAddress().equalsIgnoreCase(ipAddress)) { + player.kickPlayer(banDisplay); + } + } + + server.getLogger().log(Level.INFO, message); + ess.broadcastMessage("essentials.banip.notify", message); + } + + @Override + protected List getTabCompleteOptions(Server server, CommandSource sender, String commandLabel, String[] args) { + if (args.length == 1) { + // TODO: Also list IP addresses? + return getPlayers(server, sender); + } else { + // Note: following args are both date diffs _and_ messages; ideally we'd mix with the vanilla handler + return COMMON_DATE_DIFFS; + } + } +} diff --git a/Essentials/src/messages.properties b/Essentials/src/messages.properties index 60ca02768..b6ad68d3b 100644 --- a/Essentials/src/messages.properties +++ b/Essentials/src/messages.properties @@ -392,6 +392,7 @@ payToggleOff=\u00a76You are no longer accepting payments. payToggleOn=\u00a76You are now accepting payments. pendingTeleportCancelled=\u00a74Pending teleportation request cancelled. playerBanIpAddress=\u00a76Player\u00a7c {0} \u00a76banned IP address\u00a7c {1} \u00a76for\: \u00a7c{2}\u00a76. +playerTempBanIpAddress=\u00a76Player\u00a7c {0} \u00a76temporarily banned IP address \u00a7c{1}\u00a76 for \u00a7c{2}\u00a76\: \u00a7c{3}\u00a76. playerBanned=\u00a76Player\u00a7c {0} \u00a76banned\u00a7c {1} \u00a76for\: \u00a7c{2}\u00a76. playerJailed=\u00a76Player\u00a7c {0} \u00a76jailed. playerJailedFor=\u00a76Player\u00a7c {0} \u00a76jailed for\u00a7c {1}\u00a76. diff --git a/Essentials/src/plugin.yml b/Essentials/src/plugin.yml index 5de7a6e45..622f69deb 100644 --- a/Essentials/src/plugin.yml +++ b/Essentials/src/plugin.yml @@ -428,6 +428,10 @@ commands: description: Temporary ban a user. usage: / aliases: [etempban] + tempbanip: + description: Temporarily ban an IP Address. + usage: / + aliases: [etempbanip] thunder: description: Enable/disable thunder. usage: / [duration]