diff options
author | snowleo <snowleo@e251c2fe-e539-e718-e476-b85c1f46cddb> | 2011-05-01 11:04:34 +0000 |
---|---|---|
committer | snowleo <snowleo@e251c2fe-e539-e718-e476-b85c1f46cddb> | 2011-05-01 11:04:34 +0000 |
commit | 41d73bf90e2e94c91df8a865d54d5cb23b9101c9 (patch) | |
tree | 3c80345d9a699a8d60d2a03e37f69e0f98fac676 /EssentialsGeoIP/src/com/earth2me/essentials/geoip | |
parent | ec18c0096d91f237a27e9aa88a96857403b2ed40 (diff) | |
download | Essentials-41d73bf90e2e94c91df8a865d54d5cb23b9101c9.tar Essentials-41d73bf90e2e94c91df8a865d54d5cb23b9101c9.tar.gz Essentials-41d73bf90e2e94c91df8a865d54d5cb23b9101c9.tar.lz Essentials-41d73bf90e2e94c91df8a865d54d5cb23b9101c9.tar.xz Essentials-41d73bf90e2e94c91df8a865d54d5cb23b9101c9.zip |
EssentialsGeoIP is now officially part of Essentials
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1292 e251c2fe-e539-e718-e476-b85c1f46cddb
Diffstat (limited to 'EssentialsGeoIP/src/com/earth2me/essentials/geoip')
-rw-r--r-- | EssentialsGeoIP/src/com/earth2me/essentials/geoip/EssentialsGeoIP.java | 45 | ||||
-rw-r--r-- | EssentialsGeoIP/src/com/earth2me/essentials/geoip/EssentialsGeoIPPlayerListener.java | 162 |
2 files changed, 207 insertions, 0 deletions
diff --git a/EssentialsGeoIP/src/com/earth2me/essentials/geoip/EssentialsGeoIP.java b/EssentialsGeoIP/src/com/earth2me/essentials/geoip/EssentialsGeoIP.java new file mode 100644 index 000000000..1a01c7132 --- /dev/null +++ b/EssentialsGeoIP/src/com/earth2me/essentials/geoip/EssentialsGeoIP.java @@ -0,0 +1,45 @@ +package com.earth2me.essentials.geoip; + +import com.earth2me.essentials.Essentials; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.bukkit.event.Event.Priority; +import org.bukkit.event.Event.Type; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.java.JavaPlugin; + + +public class EssentialsGeoIP extends JavaPlugin +{ + private static final Logger logger = Logger.getLogger("Minecraft"); + + public EssentialsGeoIP() + { + } + + @Override + public void onDisable() + { + } + + @Override + public void onEnable() + { + PluginManager pm = getServer().getPluginManager(); + Plugin ess = pm.getPlugin("Essentials"); + if (ess != null) + { + if (!pm.isPluginEnabled(ess)) + { + pm.enablePlugin(ess); + } + } + EssentialsGeoIPPlayerListener playerListener = new EssentialsGeoIPPlayerListener(getDataFolder()); + pm.registerEvent(Type.PLAYER_JOIN, playerListener, Priority.Monitor, this); + + logger.info("Loaded " + this.getDescription().getName() + " build " + this.getDescription().getVersion() + " by " + Essentials.AUTHORS); + + logger.log(Level.INFO, "This product includes GeoLite data created by MaxMind, available from http://www.maxmind.com/."); + } +} diff --git a/EssentialsGeoIP/src/com/earth2me/essentials/geoip/EssentialsGeoIPPlayerListener.java b/EssentialsGeoIP/src/com/earth2me/essentials/geoip/EssentialsGeoIPPlayerListener.java new file mode 100644 index 000000000..2d9bd7749 --- /dev/null +++ b/EssentialsGeoIP/src/com/earth2me/essentials/geoip/EssentialsGeoIPPlayerListener.java @@ -0,0 +1,162 @@ +package com.earth2me.essentials.geoip; + +import com.earth2me.essentials.EssentialsConf; +import com.earth2me.essentials.IConf; +import com.earth2me.essentials.User; +import com.maxmind.geoip.Location; +import com.maxmind.geoip.LookupService; +import com.maxmind.geoip.regionName; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.zip.GZIPInputStream; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerListener; + + +public class EssentialsGeoIPPlayerListener extends PlayerListener implements IConf +{ + LookupService ls = null; + private static final Logger logger = Logger.getLogger("Minecraft"); + File databaseFile; + File dataFolder; + EssentialsConf config; + + public EssentialsGeoIPPlayerListener(File dataFolder) + { + this.dataFolder = dataFolder; + this.config = new EssentialsConf(new File(dataFolder, "config.yml")); + config.setTemplateName("/config.yml", EssentialsGeoIP.class); + reloadConfig(); + } + + @Override + public void onPlayerJoin(PlayerJoinEvent event) + { + InetAddress address = event.getPlayer().getAddress().getAddress(); + StringBuilder sb = new StringBuilder(); + if (config.getBoolean("database.show-cities", false)) + { + Location loc = ls.getLocation(address); + if (loc == null) + { + return; + } + if (loc.city != null) + { + sb.append(loc.city).append(", "); + } + String region = regionName.regionNameByCode(loc.countryCode, loc.region); + if (region != null) + { + sb.append(region).append(", "); + } + sb.append(loc.countryName); + } + else + { + sb.append(ls.getCountry(address).getName()); + } + if (config.getBoolean("show-on-whois", true)) + { + User.get(event.getPlayer()).setMetadata("location", sb.toString()); + } + if (config.getBoolean("show-on-login", true)) + { + event.getPlayer().getServer().broadcastMessage("Player " + event.getPlayer().getDisplayName() + " comes from " + sb.toString()); + } + } + + @Override + public final void reloadConfig() + { + config.load(); + + if (config.getBoolean("database.show-cities", false)) + { + databaseFile = new File(dataFolder, "GeoIPCity.dat"); + } + else + { + databaseFile = new File(dataFolder, "GeoIP.dat"); + } + if (!databaseFile.exists()) + { + if (config.getBoolean("database.download-if-missing", true)) + { + downloadDatabase(); + } + else + { + logger.log(Level.SEVERE, "Can't find GeoIP database!"); + return; + } + } + try + { + ls = new LookupService(databaseFile); + } + catch (IOException ex) + { + logger.log(Level.SEVERE, "Failed to read GeoIP database!", ex); + } + } + + private void downloadDatabase() + { + try + { + String url; + if (config.getBoolean("database.show-cities", false)) + { + url = config.getString("database.download-url-city"); + } + else + { + url = config.getString("database.download-url"); + } + if (url == null || url.isEmpty()) + { + logger.log(Level.SEVERE, "GeoIP download url is empty."); + return; + } + logger.log(Level.INFO, "Downloading GeoIP database ... this might take a while (country: 0.6 MB, city: 20MB)"); + URL downloadUrl = new URL(url); + URLConnection conn = downloadUrl.openConnection(); + conn.setConnectTimeout(10000); + conn.connect(); + InputStream input = conn.getInputStream(); + if (url.endsWith(".gz")) + { + input = new GZIPInputStream(input); + } + OutputStream output = new FileOutputStream(databaseFile); + byte[] buffer = new byte[2048]; + int length = input.read(buffer); + while (length >= 0) + { + output.write(buffer, 0, length); + length = input.read(buffer); + } + output.close(); + input.close(); + } + catch (MalformedURLException ex) + { + logger.log(Level.SEVERE, "GeoIP download url is invalid.", ex); + return; + } + catch (IOException ex) + { + logger.log(Level.SEVERE, "Failed to open connection.", ex); + } + } +} |