diff --git a/src/main/java/me/totalfreedom/totalfreedommod/BackupManager.java b/src/main/java/me/totalfreedom/totalfreedommod/BackupManager.java index 43f84789..e70bbf36 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/BackupManager.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/BackupManager.java @@ -1,7 +1,7 @@ package me.totalfreedom.totalfreedommod; import java.io.File; -import me.totalfreedom.totalfreedommod.banning.PermbanList; +import me.totalfreedom.totalfreedommod.banning.IndefiniteBanList; import me.totalfreedom.totalfreedommod.config.YamlConfig; import me.totalfreedom.totalfreedommod.permissions.PermissionConfig; import me.totalfreedom.totalfreedommod.punishments.PunishmentList; @@ -29,7 +29,7 @@ public class BackupManager extends FreedomService public void createAllBackups() { createBackups(TotalFreedomMod.CONFIG_FILENAME, true); - createBackups(PermbanList.CONFIG_FILENAME); + createBackups(IndefiniteBanList.CONFIG_FILENAME); createBackups(PermissionConfig.PERMISSIONS_FILENAME, true); createBackups(PunishmentList.CONFIG_FILENAME); createBackups("database.db"); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/banning/IndefiniteBan.java b/src/main/java/me/totalfreedom/totalfreedommod/banning/IndefiniteBan.java new file mode 100644 index 00000000..1e3efcbe --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/banning/IndefiniteBan.java @@ -0,0 +1,63 @@ +package me.totalfreedom.totalfreedommod.banning; + +import com.google.common.collect.Lists; +import java.util.List; +import java.util.UUID; +import lombok.Getter; +import lombok.Setter; +import me.totalfreedom.totalfreedommod.config.IConfig; +import me.totalfreedom.totalfreedommod.util.FLog; +import org.bukkit.configuration.ConfigurationSection; + +public class IndefiniteBan implements IConfig +{ + + @Getter + @Setter + private String username = null; + @Getter + @Setter + private UUID uuid = null; + @Getter + private final List ips = Lists.newArrayList(); + @Getter + @Setter + private String reason = null; + + public IndefiniteBan() + { + } + + @Override + public void loadFrom(ConfigurationSection cs) + { + this.username = cs.getName(); + try + { + String strUUID = cs.getString("uuid", null); + if (strUUID != null) + { + UUID uuid = UUID.fromString(strUUID); + this.uuid = uuid; + } + } + catch (IllegalArgumentException e) + { + FLog.warning("Failed to load indefinite banned UUID for " + this.username + ". Make sure the UUID is in the correct format with dashes."); + } + this.ips.clear(); + this.ips.addAll(cs.getStringList("ips")); + this.reason = cs.getString("reason", null); + } + + @Override + public void saveTo(ConfigurationSection cs) + { + } + + @Override + public boolean isValid() + { + return username != null; + } +} diff --git a/src/main/java/me/totalfreedom/totalfreedommod/banning/IndefiniteBanList.java b/src/main/java/me/totalfreedom/totalfreedommod/banning/IndefiniteBanList.java new file mode 100644 index 00000000..042cdf75 --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/banning/IndefiniteBanList.java @@ -0,0 +1,140 @@ +package me.totalfreedom.totalfreedommod.banning; + +import com.google.common.base.Strings; +import com.google.common.collect.Sets; +import java.util.Set; +import java.util.UUID; +import lombok.Getter; +import me.totalfreedom.totalfreedommod.FreedomService; +import me.totalfreedom.totalfreedommod.config.ConfigEntry; +import me.totalfreedom.totalfreedommod.config.YamlConfig; +import me.totalfreedom.totalfreedommod.util.FLog; +import me.totalfreedom.totalfreedommod.util.FUtil; +import org.bukkit.ChatColor; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.player.PlayerLoginEvent; + +public class IndefiniteBanList extends FreedomService +{ + + public static final String CONFIG_FILENAME = "indefinitebans.yml"; + + @Getter + private final Set indefBans = Sets.newHashSet(); + + @Getter + private int nameBanCount = 0; + + @Getter + private int uuidBanCount = 0; + + @Getter + private int ipBanCount = 0; + + @Override + public void onStart() + { + indefBans.clear(); + + final YamlConfig config = new YamlConfig(plugin, CONFIG_FILENAME, true); + config.load(); + + for (String name : config.getKeys(false)) + { + if (!config.isConfigurationSection(name)) + { + FLog.warning("Could not load indefinite ban for " + name + ": Invalid format!"); + continue; + } + + IndefiniteBan indefBan = new IndefiniteBan(); + ConfigurationSection cs = config.getConfigurationSection(name); + indefBan.loadFrom(cs); + + if (!indefBan.isValid()) + { + FLog.warning("Not adding indefinite ban for " + name + ": Missing information."); + continue; + } + + indefBans.add(indefBan); + } + + updateCount(); + + FLog.info("Loaded " + nameBanCount + " indefinite name bans, " + uuidBanCount + " UUID bans, and " + ipBanCount + " ip bans"); + } + + @Override + public void onStop() + { + } + + @EventHandler(priority = EventPriority.LOWEST) + public void onPlayerLogin(PlayerLoginEvent event) + { + final String username = event.getPlayer().getName(); + final UUID uuid = event.getPlayer().getUniqueId(); + final String ip = FUtil.getIp(event); + + String bannedBy = ""; + IndefiniteBan ban = null; + + for (IndefiniteBan indefBan : indefBans) + { + if (username.toLowerCase().equals(indefBan.getUsername().toLowerCase())) + { + bannedBy = "username"; + ban = indefBan; + break; + } + else if (indefBan.getUuid() != null && indefBan.getUuid().equals(uuid)) + { + bannedBy = "UUID"; + ban = indefBan; + break; + } + else if (indefBan.getIps().contains(ip)) + { + bannedBy = "IP address"; + ban = indefBan; + break; + } + } + + if (ban != null) + { + String kickMessage = ChatColor.RED + "Your " + bannedBy + " is indefinitely banned from this server."; + String reason = ban.getReason(); + if (!Strings.isNullOrEmpty(reason)) + { + kickMessage += "\nReason: " + ChatColor.GOLD + reason; + } + String appealURL = ConfigEntry.SERVER_INDEFBAN_URL.getString(); + if (!Strings.isNullOrEmpty(appealURL)) + { + kickMessage += ChatColor.RED + "\n\nRelease procedures are available at\n" + ChatColor.GOLD + ConfigEntry.SERVER_INDEFBAN_URL.getString(); + } + event.disallow(PlayerLoginEvent.Result.KICK_OTHER, kickMessage); + } + } + + private void updateCount() + { + nameBanCount = 0; + uuidBanCount = 0; + ipBanCount = 0; + + for (IndefiniteBan indefBan : indefBans) + { + nameBanCount += 1; + if (indefBan.getUuid() != null) + { + uuidBanCount += 1; + } + ipBanCount += indefBan.getIps().size(); + } + } +} diff --git a/src/main/java/me/totalfreedom/totalfreedommod/banning/PermbanList.java b/src/main/java/me/totalfreedom/totalfreedommod/banning/PermbanList.java deleted file mode 100644 index 7265bab6..00000000 --- a/src/main/java/me/totalfreedom/totalfreedommod/banning/PermbanList.java +++ /dev/null @@ -1,92 +0,0 @@ -package me.totalfreedom.totalfreedommod.banning; - -import com.google.common.collect.Sets; -import java.util.Set; -import lombok.Getter; -import me.totalfreedom.totalfreedommod.FreedomService; -import me.totalfreedom.totalfreedommod.config.ConfigEntry; -import me.totalfreedom.totalfreedommod.config.YamlConfig; -import me.totalfreedom.totalfreedommod.util.FLog; -import me.totalfreedom.totalfreedommod.util.FUtil; -import org.bukkit.ChatColor; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.player.PlayerLoginEvent; - -public class PermbanList extends FreedomService -{ - - public static final String CONFIG_FILENAME = "permbans.yml"; - - @Getter - private final Set permbannedNames = Sets.newHashSet(); - @Getter - private final Set permbannedIps = Sets.newHashSet(); - - @Override - public void onStart() - { - permbannedNames.clear(); - permbannedIps.clear(); - - final YamlConfig config = new YamlConfig(plugin, CONFIG_FILENAME, true); - config.load(); - - for (String name : config.getKeys(false)) - { - permbannedNames.add(name.toLowerCase().trim()); - permbannedIps.addAll(config.getStringList(name)); - } - - FLog.info("Loaded " + permbannedIps.size() + " perm IP bans and " + permbannedNames.size() + " perm username bans."); - } - - @Override - public void onStop() - { - } - - @EventHandler(priority = EventPriority.LOWEST) - public void onPlayerLogin(PlayerLoginEvent event) - { - final String username = event.getPlayer().getName(); - final String ip = FUtil.getIp(event); - - // Permbanned IPs - for (String testIp : getPermbannedIps()) - { - if (FUtil.fuzzyIpMatch(testIp, ip, 4)) - { - event.disallow(PlayerLoginEvent.Result.KICK_OTHER, - ChatColor.RED + "Your IP address is permanently banned from this server.\n" - + "Release procedures are available at\n" - + ChatColor.GOLD + ConfigEntry.SERVER_PERMBAN_URL.getString()); - return; - } - } - - // Permbanned usernames - for (String testPlayer : getPermbannedNames()) - { - if (testPlayer.equalsIgnoreCase(username)) - { - event.disallow(PlayerLoginEvent.Result.KICK_OTHER, - ChatColor.RED + "Your username is permanently banned from this server.\n" - + "Release procedures are available at\n" - + ChatColor.GOLD + ConfigEntry.SERVER_PERMBAN_URL.getString()); - return; - } - } - - } - - public Set getPermbannedNames() - { - return this.permbannedNames; - } - - public Set getPermbannedIps() - { - return this.permbannedIps; - } -} diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_permban.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_indefban.java similarity index 57% rename from src/main/java/me/totalfreedom/totalfreedommod/command/Command_permban.java rename to src/main/java/me/totalfreedom/totalfreedommod/command/Command_indefban.java index ba8f8ef8..32f5f1bb 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_permban.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_indefban.java @@ -1,14 +1,13 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.rank.Rank; -import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @CommandPermissions(level = Rank.SENIOR_ADMIN, source = SourceType.ONLY_CONSOLE) -@CommandParameters(description = "Reload the permban list.", usage = "/ reload", aliases = "pb") -public class Command_permban extends FreedomCommand +@CommandParameters(description = "Reload the indefinite ban list.", usage = "/ reload", aliases = "ib") +public class Command_indefban extends FreedomCommand { @Override @@ -24,12 +23,10 @@ public class Command_permban extends FreedomCommand return false; } - msg("Reloading permban list...", ChatColor.RED); - plugin.pm.onStop(); - plugin.pm.onStart(); - msg("Reloaded permban list."); - msg(plugin.pm.getPermbannedIps().size() + " IPs and " - + plugin.pm.getPermbannedNames().size() + " usernames loaded."); + msg("Reloading the indefinite ban list..."); + plugin.im.onStop(); + plugin.im.onStart(); + msg("Reloaded the indefinite ban list."); return true; } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_permbans.java b/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_indefbans.java similarity index 71% rename from src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_permbans.java rename to src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_indefbans.java index a26c3d05..152390b3 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_permbans.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_indefbans.java @@ -3,14 +3,14 @@ package me.totalfreedom.totalfreedommod.httpd.module; import java.io.File; import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.admin.Admin; -import me.totalfreedom.totalfreedommod.banning.PermbanList; +import me.totalfreedom.totalfreedommod.banning.IndefiniteBanList; import me.totalfreedom.totalfreedommod.httpd.HTTPDaemon; import me.totalfreedom.totalfreedommod.httpd.NanoHTTPD; -public class Module_permbans extends HTTPDModule +public class Module_indefbans extends HTTPDModule { - public Module_permbans(TotalFreedomMod plugin, NanoHTTPD.HTTPSession session) + public Module_indefbans(TotalFreedomMod plugin, NanoHTTPD.HTTPSession session) { super(plugin, session); } @@ -18,17 +18,17 @@ public class Module_permbans extends HTTPDModule @Override public NanoHTTPD.Response getResponse() { - File permbanFile = new File(plugin.getDataFolder(), PermbanList.CONFIG_FILENAME); + File permbanFile = new File(plugin.getDataFolder(), IndefiniteBanList.CONFIG_FILENAME); final String remoteAddress = socket.getInetAddress().getHostAddress(); if (!isAuthorized(remoteAddress)) { return new NanoHTTPD.Response(NanoHTTPD.Response.Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, - "You may not view the permban list. Your IP, " + remoteAddress + ", is not registered to an admin on the server."); + "You may not view the indefinite ban list. Your IP, " + remoteAddress + ", is not registered to an admin on the server."); } if (permbanFile.exists()) { - return HTTPDaemon.serveFileBasic(new File(plugin.getDataFolder(), PermbanList.CONFIG_FILENAME)); + return HTTPDaemon.serveFileBasic(new File(plugin.getDataFolder(), IndefiniteBanList.CONFIG_FILENAME)); } else { diff --git a/src/main/resources/indefinitebans.yml b/src/main/resources/indefinitebans.yml new file mode 100644 index 00000000..4e4404ce --- /dev/null +++ b/src/main/resources/indefinitebans.yml @@ -0,0 +1,15 @@ +# +# TotalFreedomMod Indefinite Bans +# + +badplayer1: + uuid: '123e4567-e89b-12d3-a456-426614174000' + ips: + - 123.123.123.123 + - 321.321.321.321 + reason: 'bad player' +badplayer2: + ips: + - 111.111.111.111 +badplayer3: + ips: [] diff --git a/src/main/resources/permbans.yml b/src/main/resources/permbans.yml deleted file mode 100644 index 0df32bba..00000000 --- a/src/main/resources/permbans.yml +++ /dev/null @@ -1,10 +0,0 @@ -# -# TotalFreedomMod 5.5 Permanent Bans -# - -badplayer1: - - 123.123.123.123 - - 321.321.321.321 -badplayer2: - - 111.111.111.111 -badplayer3: []