/EssentialsGeoIP/src/com/earth2me/essentials/geoip/EssentialsGeoIPPlayerListener.java

https://github.com/YellowFellow/Essentials · Java · 178 lines · 170 code · 8 blank · 0 comment · 21 complexity · a0c502776c688fc78efbb04bd341f7cd MD5 · raw file

  1. package com.earth2me.essentials.geoip;
  2. import com.earth2me.essentials.EssentialsConf;
  3. import com.earth2me.essentials.IConf;
  4. import com.earth2me.essentials.IEssentials;
  5. import com.earth2me.essentials.User;
  6. import com.earth2me.essentials.Util;
  7. import com.maxmind.geoip.Location;
  8. import com.maxmind.geoip.LookupService;
  9. import com.maxmind.geoip.regionName;
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.OutputStream;
  15. import java.net.InetAddress;
  16. import java.net.MalformedURLException;
  17. import java.net.URL;
  18. import java.net.URLConnection;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import java.util.zip.GZIPInputStream;
  22. import org.bukkit.entity.Player;
  23. import org.bukkit.event.player.PlayerJoinEvent;
  24. import org.bukkit.event.player.PlayerListener;
  25. public class EssentialsGeoIPPlayerListener extends PlayerListener implements IConf
  26. {
  27. LookupService ls = null;
  28. private static final Logger logger = Logger.getLogger("Minecraft");
  29. File databaseFile;
  30. File dataFolder;
  31. EssentialsConf config;
  32. private final transient IEssentials ess;
  33. public EssentialsGeoIPPlayerListener(File dataFolder, IEssentials ess)
  34. {
  35. this.ess = ess;
  36. this.dataFolder = dataFolder;
  37. this.config = new EssentialsConf(new File(dataFolder, "config.yml"));
  38. config.setTemplateName("/config.yml", EssentialsGeoIP.class);
  39. reloadConfig();
  40. }
  41. @Override
  42. public void onPlayerJoin(PlayerJoinEvent event)
  43. {
  44. User u = ess.getUser(event.getPlayer());
  45. if (u.isAuthorized("essentials.geoip.hide"))
  46. {
  47. return;
  48. }
  49. InetAddress address = event.getPlayer().getAddress().getAddress();
  50. StringBuilder sb = new StringBuilder();
  51. if (config.getBoolean("database.show-cities", false))
  52. {
  53. Location loc = ls.getLocation(address);
  54. if (loc == null)
  55. {
  56. return;
  57. }
  58. if (loc.city != null)
  59. {
  60. sb.append(loc.city).append(", ");
  61. }
  62. String region = regionName.regionNameByCode(loc.countryCode, loc.region);
  63. if (region != null)
  64. {
  65. sb.append(region).append(", ");
  66. }
  67. sb.append(loc.countryName);
  68. }
  69. else
  70. {
  71. sb.append(ls.getCountry(address).getName());
  72. }
  73. if (config.getBoolean("show-on-whois", true))
  74. {
  75. u.setGeoLocation(sb.toString());
  76. }
  77. if (config.getBoolean("show-on-login", true))
  78. {
  79. for (Player player : event.getPlayer().getServer().getOnlinePlayers())
  80. {
  81. User user = ess.getUser(player);
  82. if (user.isAuthorized("essentials.geoip.show")) {
  83. user.sendMessage(Util.format("geoipJoinFormat", u.getDisplayName(), sb.toString()));
  84. }
  85. }
  86. }
  87. }
  88. @Override
  89. public final void reloadConfig()
  90. {
  91. config.load();
  92. if (config.getBoolean("database.show-cities", false))
  93. {
  94. databaseFile = new File(dataFolder, "GeoIPCity.dat");
  95. }
  96. else
  97. {
  98. databaseFile = new File(dataFolder, "GeoIP.dat");
  99. }
  100. if (!databaseFile.exists())
  101. {
  102. if (config.getBoolean("database.download-if-missing", true))
  103. {
  104. downloadDatabase();
  105. }
  106. else
  107. {
  108. logger.log(Level.SEVERE, Util.i18n("cantFindGeoIpDB"));
  109. return;
  110. }
  111. }
  112. try
  113. {
  114. ls = new LookupService(databaseFile);
  115. }
  116. catch (IOException ex)
  117. {
  118. logger.log(Level.SEVERE, Util.i18n("cantReadGeoIpDB"), ex);
  119. }
  120. }
  121. private void downloadDatabase()
  122. {
  123. try
  124. {
  125. String url;
  126. if (config.getBoolean("database.show-cities", false))
  127. {
  128. url = config.getString("database.download-url-city");
  129. }
  130. else
  131. {
  132. url = config.getString("database.download-url");
  133. }
  134. if (url == null || url.isEmpty())
  135. {
  136. logger.log(Level.SEVERE, Util.i18n("geoIpUrlEmpty"));
  137. return;
  138. }
  139. logger.log(Level.INFO, Util.i18n("downloadingGeoIp"));
  140. URL downloadUrl = new URL(url);
  141. URLConnection conn = downloadUrl.openConnection();
  142. conn.setConnectTimeout(10000);
  143. conn.connect();
  144. InputStream input = conn.getInputStream();
  145. if (url.endsWith(".gz"))
  146. {
  147. input = new GZIPInputStream(input);
  148. }
  149. OutputStream output = new FileOutputStream(databaseFile);
  150. byte[] buffer = new byte[2048];
  151. int length = input.read(buffer);
  152. while (length >= 0)
  153. {
  154. output.write(buffer, 0, length);
  155. length = input.read(buffer);
  156. }
  157. output.close();
  158. input.close();
  159. }
  160. catch (MalformedURLException ex)
  161. {
  162. logger.log(Level.SEVERE, Util.i18n("geoIpUrlInvalid"), ex);
  163. return;
  164. }
  165. catch (IOException ex)
  166. {
  167. logger.log(Level.SEVERE, Util.i18n("connectionFailed"), ex);
  168. }
  169. }
  170. }