From e7eafa811423a9cf76f03b9afec384616925d409 Mon Sep 17 00:00:00 2001 From: snowleo Date: Wed, 30 Mar 2011 12:56:34 +0000 Subject: Revert to 1013 git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1021 e251c2fe-e539-e718-e476-b85c1f46cddb --- EssentialsServerlist/build.xml | 76 ++ EssentialsServerlist/nbproject/build-impl.xml | 1072 ++++++++++++++++++++ EssentialsServerlist/nbproject/genfiles.properties | 8 + EssentialsServerlist/nbproject/project.properties | 76 ++ EssentialsServerlist/nbproject/project.xml | 28 + .../src/net/mcserverlist/bukkit/Mcsl.java | 149 +++ .../mcserverlist/bukkit/McslPlayerListener.java | 209 ++++ .../src/net/mcserverlist/bukkit/Whitelist.java | 107 ++ EssentialsServerlist/src/plugin.yml | 5 + 9 files changed, 1730 insertions(+) create mode 100644 EssentialsServerlist/build.xml create mode 100644 EssentialsServerlist/nbproject/build-impl.xml create mode 100644 EssentialsServerlist/nbproject/genfiles.properties create mode 100644 EssentialsServerlist/nbproject/project.properties create mode 100644 EssentialsServerlist/nbproject/project.xml create mode 100644 EssentialsServerlist/src/net/mcserverlist/bukkit/Mcsl.java create mode 100644 EssentialsServerlist/src/net/mcserverlist/bukkit/McslPlayerListener.java create mode 100644 EssentialsServerlist/src/net/mcserverlist/bukkit/Whitelist.java create mode 100644 EssentialsServerlist/src/plugin.yml (limited to 'EssentialsServerlist') diff --git a/EssentialsServerlist/build.xml b/EssentialsServerlist/build.xml new file mode 100644 index 000000000..7650691f1 --- /dev/null +++ b/EssentialsServerlist/build.xml @@ -0,0 +1,76 @@ + + ]> + + + + + + + + + + Builds, tests, and runs the project EssentialsServerlist. + + &buildinc; + + diff --git a/EssentialsServerlist/nbproject/build-impl.xml b/EssentialsServerlist/nbproject/build-impl.xml new file mode 100644 index 000000000..ee4ae2895 --- /dev/null +++ b/EssentialsServerlist/nbproject/build-impl.xml @@ -0,0 +1,1072 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + + + + + + java -cp "${run.classpath.with.dist.jar}" ${main.class} + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + + + + + + + + Must select one file in the IDE or set profile.class + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/EssentialsServerlist/nbproject/genfiles.properties b/EssentialsServerlist/nbproject/genfiles.properties new file mode 100644 index 000000000..3255d99dc --- /dev/null +++ b/EssentialsServerlist/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=b3f32d5a +build.xml.script.CRC32=d07c143a +build.xml.stylesheet.CRC32=28e38971@1.38.2.45 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=28f52c06 +nbproject/build-impl.xml.script.CRC32=bdef3982 +nbproject/build-impl.xml.stylesheet.CRC32=19debb58@1.42.1.45 diff --git a/EssentialsServerlist/nbproject/project.properties b/EssentialsServerlist/nbproject/project.properties new file mode 100644 index 000000000..535a1673b --- /dev/null +++ b/EssentialsServerlist/nbproject/project.properties @@ -0,0 +1,76 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=EssentialsServerlist +application.vendor=Paul +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/EssentialsServerlist.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +file.reference.craftbukkit-0.0.1-SNAPSHOT.jar-1=..\\lib\\craftbukkit-0.0.1-SNAPSHOT.jar +includes=** +jar.compress=false +javac.classpath=\ + ${reference.Essentials.jar}:\ + ${file.reference.craftbukkit-0.0.1-SNAPSHOT.jar-1} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.5 +javac.target=1.5 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${libs.junit.classpath}:\ + ${libs.junit_4.classpath} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +meta.inf.dir=${src.dir}/META-INF +platform.active=default_platform +project.Essentials=../Essentials +reference.Essentials.jar=${project.Essentials}/dist/Essentials.jar +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project +# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value +# or test-sys-prop.name=value to set system properties for unit tests): +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/EssentialsServerlist/nbproject/project.xml b/EssentialsServerlist/nbproject/project.xml new file mode 100644 index 000000000..a65ec9af8 --- /dev/null +++ b/EssentialsServerlist/nbproject/project.xml @@ -0,0 +1,28 @@ + + + org.netbeans.modules.java.j2seproject + + + EssentialsServerlist + + + + + + + + + ../lib\nblibraries.properties + + + + Essentials + jar + + jar + clean + jar + + + + diff --git a/EssentialsServerlist/src/net/mcserverlist/bukkit/Mcsl.java b/EssentialsServerlist/src/net/mcserverlist/bukkit/Mcsl.java new file mode 100644 index 000000000..c9b234fd7 --- /dev/null +++ b/EssentialsServerlist/src/net/mcserverlist/bukkit/Mcsl.java @@ -0,0 +1,149 @@ +package net.mcserverlist.bukkit; + +import com.earth2me.essentials.Essentials; +import java.io.*; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.event.Event; +import org.bukkit.event.Event.Priority; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginDescriptionFile; +import org.bukkit.plugin.java.JavaPlugin; + + +public class Mcsl extends JavaPlugin +{ + private static final Logger logger = Logger.getLogger("Minecraft"); + private McslPlayerListener playerListener; + public final String author; + + public Mcsl() throws IOException + { + + PluginDescriptionFile desc = this.getDescription(); + + // Compile author list + List authors = new ArrayList(); + authors.add("Vimae Development"); + int alen = authors.size(); + if (alen == 1) + { + author = " by " + authors.get(0); + } + else if (alen > 1) + { + int i = 0; + StringBuilder bldr = new StringBuilder(); + for (String a : desc.getAuthors()) + { + if (i + 1 == alen) + { + if (alen > 2) bldr.append(","); + bldr.append(" and "); + } + else if (i++ > 0) + { + bldr.append(", "); + } + bldr.append(a); + } + bldr.insert(0, " by "); + author = bldr.toString(); + } + else + { + author = ""; + } + } + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) + { + McslCommands mcslCmd; + try + { + switch (McslCommands.valueOf(cmd.getName().toUpperCase())) + { + case WHITELIST: + whitelist(sender, WhitelistCommands.valueOf(args[0].toUpperCase()), args); + return true; + + default: + return false; + } + } + catch (IllegalArgumentException ex) + { + return false; + } + catch (Exception ex) + { + logger.log(Level.WARNING, "MCSL encountered an unknown error.", ex); + sender.sendMessage("MCSL encountered an unknown error."); + return true; + } + } + + @SuppressWarnings("LoggerStringConcat") + public void onEnable() + { + Plugin p = this.getServer().getPluginManager().getPlugin("Essentials"); + if (p != null) { + if (!this.getServer().getPluginManager().isPluginEnabled(p)) { + this.getServer().getPluginManager().enablePlugin(p); + } + } + playerListener = new McslPlayerListener(this); + getServer().getPluginManager().registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Monitor, this); + getServer().getPluginManager().registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Monitor, this); + getServer().getPluginManager().registerEvent(Event.Type.PLAYER_LOGIN, playerListener, Priority.Lowest, this); + + if (!this.getDescription().getVersion().equals(Essentials.getStatic().getDescription().getVersion())) { + logger.log(Level.WARNING, "Version mismatch! Please update all Essentials jars to the same version."); + } + logger.info(getDescription().getName() + " version " + getDescription().getVersion() + author + " enabled."); + } + + @SuppressWarnings("LoggerStringConcat") + public void onDisable() + { + logger.info(getDescription().getName() + " version " + getDescription().getVersion() + " disabled."); + } + + private void whitelist(CommandSender sender, WhitelistCommands cmd, String[] args) + { + if (!playerListener.isWhitelistEnabled()) + { + sender.sendMessage("§cThe whitelist is disabled."); + return; + } + + if (!sender.isOp()) + { + sender.sendMessage("§cYou must be an operator to manage the whitelist."); + return; + } + + switch (cmd) + { + case RELOAD: + playerListener.whitelistReload(); + sender.sendMessage("A whitelist updated has been queued."); + break; + } + } + + private enum McslCommands + { + WHITELIST + } + + private enum WhitelistCommands + { + RELOAD + } +} diff --git a/EssentialsServerlist/src/net/mcserverlist/bukkit/McslPlayerListener.java b/EssentialsServerlist/src/net/mcserverlist/bukkit/McslPlayerListener.java new file mode 100644 index 000000000..63e4343d2 --- /dev/null +++ b/EssentialsServerlist/src/net/mcserverlist/bukkit/McslPlayerListener.java @@ -0,0 +1,209 @@ +package net.mcserverlist.bukkit; + +import com.earth2me.essentials.Essentials; +import java.io.*; +import java.net.*; +import java.util.logging.*; +import org.bukkit.*; +import org.bukkit.entity.*; +import org.bukkit.event.player.*; + + +public class McslPlayerListener extends PlayerListener +{ + private final static Logger logger = Logger.getLogger("Minecraft"); + private boolean running = true; + private final Server server; + private Thread thread; + private Whitelist whitelist = null; + private volatile boolean updateNeeded = true; + + @SuppressWarnings("CallToThreadStartDuringObjectConstruction") + public McslPlayerListener(Mcsl parent) + { + this.server = parent.getServer(); + + // Get the data from the server.properties file as the server sees it, rather than reading it manually + try + { + this.whitelist = new Whitelist(server); + } + catch (Throwable ex) + { + // Disable the plugin + logger.log(Level.WARNING, "Error encountered while initializing MCServerlist plugin.", ex); + parent.getPluginLoader().disablePlugin(parent); + return; + } + + // Run Update on a set interval of 1 minute with an initial delay of 10 seconds + thread = new Thread(new UpdateRunnable()); + thread.setDaemon(true); + thread.start(); + } + + @Override + @SuppressWarnings("FinalizeDeclaration") + protected void finalize() throws Throwable + { + // Stop the timer + if (thread != null && thread.isAlive()) + { + running = false; + thread.join(); + } + + super.finalize(); + } + + @Override + public void onPlayerLogin(PlayerLoginEvent event) + { + if (event.getResult() != PlayerLoginEvent.Result.ALLOWED) + return; + + Player player = event.getPlayer(); + + // Check the whitelist + if (!Essentials.getSettings().getWhitelistEnabled() || player.isOp() || whitelist == null || whitelist.isAllowed(player.getName())) + { + // Player is an op, there is no whitelist, or the player is whitelisted. + return; + } + + // Player is not whitelisted. + event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "This server employs a whitelist."); + } + + @Override + public void onPlayerJoin(PlayerEvent event) + { + update(); + } + + @Override + public void onPlayerQuit(PlayerEvent event) + { + update(); + } + + public boolean isWhitelistEnabled() + { + return whitelist != null; + } + + public void whitelistReload() + { + whitelist.update(); + } + + public void update() + { + updateNeeded = true; + } + + + private class UpdateRunnable implements Runnable + { + @SuppressWarnings("SleepWhileInLoop") + public void run() + { + do + { + if (updateNeeded) + { + updateNeeded = false; + update(); + } + + try + { + Thread.sleep(60000); + } + catch (InterruptedException ex) + { + logger.info("Forcing MCServerlist update."); + } + } + while (running); + } + + @SuppressWarnings("CallToThreadDumpStack") + public void update() + { + // Check that we aren't receiving an event inappropriately + if (Essentials.getSettings().getMcslKey() == null || Essentials.getSettings().getMcslKey().equals("")) return; + // Compile a comma-space-delimted list of players + Player[] players = server.getOnlinePlayers(); + StringBuilder list = new StringBuilder(); + if (players.length > 0) + { + for (int i = 0; i < players.length; i++) + { + if (i > 0) list.append(", "); + list.append(players[i].getName()); + } + } + + try + { + // Compile POST data + StringBuilder data = new StringBuilder(); + data.append("key="); + data.append(URLEncoder.encode(Essentials.getSettings().getMcslKey(), "UTF-8")); + data.append("&player_count="); + data.append(Integer.toString(players.length)); + data.append("&max_players="); + data.append(Integer.toString(server.getMaxPlayers())); + data.append("&player_list="); + data.append(URLEncoder.encode(list.toString(), "UTF-8")); + + OutputStreamWriter tx = null; + BufferedReader rx = null; + try + { + // Send POST request + URL url = new URL("http://mcserverlist.net/api/update"); + // Swap line for testing purposes + //URL url = new URL("http://localhost/mcsl/update.php"); + HttpURLConnection http = (HttpURLConnection)url.openConnection(); + http.setRequestMethod("POST"); + http.setUseCaches(false); + http.setConnectTimeout(1000); + http.setAllowUserInteraction(false); + http.setInstanceFollowRedirects(true); + http.setRequestProperty("User-Agent", "Java;Mcsl"); + http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); + http.setRequestProperty("X-Mcsl-Key", Essentials.getSettings().getMcslKey()); + http.setRequestProperty("X-Minecraft-Name", URLEncoder.encode(server.getName(), "UTF-8")); + http.setRequestProperty("X-Minecraft-Version", server.getVersion()); + http.setDoInput(true); + http.setDoOutput(true); + tx = new OutputStreamWriter(http.getOutputStream()); + tx.write(data.toString()); + tx.flush(); + + // Get the HTTP response + rx = new BufferedReader(new InputStreamReader(http.getInputStream())); + for (String l = ""; rx.ready(); l = rx.readLine()) + { + if ("".equals(l)) continue; + else if (l.startsWith("i:")) logger.info(l.substring(2)); + else if (l.startsWith("w:")) logger.warning(l.substring(2)); + else System.out.println(l); + } + } + finally + { + if (tx != null) tx.close(); + if (rx != null) rx.close(); + } + } + catch (Exception ex) + { + logger.log(Level.WARNING, "Error communication with MCServerlist.", ex); + ex.printStackTrace(); + } + } + } +} diff --git a/EssentialsServerlist/src/net/mcserverlist/bukkit/Whitelist.java b/EssentialsServerlist/src/net/mcserverlist/bukkit/Whitelist.java new file mode 100644 index 000000000..96a96ecbc --- /dev/null +++ b/EssentialsServerlist/src/net/mcserverlist/bukkit/Whitelist.java @@ -0,0 +1,107 @@ +package net.mcserverlist.bukkit; + +import com.earth2me.essentials.Essentials; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.bukkit.Server; + + +public class Whitelist +{ + private static final Logger logger = Logger.getLogger("Minecraft"); + private List allowed = new ArrayList(); + private final Object allowedLock = new Object(); + private Server server; + + public Whitelist(Server server) + { + this.server = server; + } + + public void update() + { + Thread thread = new Thread(new UpdateRunnable()); + thread.setDaemon(true); + thread.start(); + } + + public boolean isAllowed(String player) + { + String p = player.toLowerCase(); + synchronized (allowedLock) + { + return allowed.contains(p); + } + } + + private class UpdateRunnable implements Runnable + { + @SuppressWarnings("CallToThreadDumpStack") + public void run() + { + // Check that we aren't receiving an event inappropriately + if (Essentials.getSettings().getMcslKey() == null || Essentials.getSettings().getMcslKey().equals("")) return; + + try + { + OutputStreamWriter tx = null; + BufferedReader rx = null; + try + { + // Send GET request + URL url = new URL("http://mcserverlist.net/api/whitelist"); + // Swap line for testing purposes + //URL url = new URL("http://localhost/mcsl/whitelist.php"); + HttpURLConnection http = (HttpURLConnection)url.openConnection(); + http.setRequestMethod("POST"); + http.setUseCaches(false); + http.setConnectTimeout(1000); + http.setAllowUserInteraction(false); + http.setInstanceFollowRedirects(true); + http.setRequestProperty("User-Agent", "Java;Mcsl"); + http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); + http.setRequestProperty("X-Mcsl-Key", Essentials.getSettings().getMcslKey()); + http.setRequestProperty("X-Minecraft-Name", URLEncoder.encode(server.getName(), "UTF-8")); + http.setRequestProperty("X-Minecraft-Version", server.getVersion()); + http.setDoInput(true); + http.setDoOutput(false); + + // Get the HTTP response + rx = new BufferedReader(new InputStreamReader(http.getInputStream())); + List allowed = new ArrayList(); + for (String l = ""; rx.ready(); l = rx.readLine()) + { + if ("".equals(l)) continue; + else if (l.startsWith("i:")) logger.info(l.substring(2)); + else if (l.startsWith("w:")) logger.warning(l.substring(2)); + else allowed.add(l.toLowerCase()); // Add to whitelist + } + + synchronized (Whitelist.this.allowedLock) + { + Whitelist.this.allowed = allowed; + allowed = null; // Remove our reference so that we don't accidentally use it + } + } + finally + { + if (tx != null) tx.close(); + if (rx != null) rx.close(); + } + } + catch (Exception ex) + { + logger.log(Level.WARNING, "Error communication with MCServerlist.", ex); + ex.printStackTrace(); + } + } + } +} diff --git a/EssentialsServerlist/src/plugin.yml b/EssentialsServerlist/src/plugin.yml new file mode 100644 index 000000000..cb6d7bddc --- /dev/null +++ b/EssentialsServerlist/src/plugin.yml @@ -0,0 +1,5 @@ +name: EssentialsServerlist +main: net.mcserverlist.bukkit.Mcsl +version: TeamCity +website: http://mcserverlist.net/ +description: Official MCServerlist API implementation \ No newline at end of file -- cgit v1.2.3