summaryrefslogtreecommitdiffstats
path: root/EssentialsGeoIP/src/net/ess3/geoip/EssentialsGeoIPPlayerListener.java
blob: 857b170a0de1e4c04be2734488c4ed007adfd5a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package net.ess3.geoip;

import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName;
import java.io.*;
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 static net.ess3.I18n._;
import net.ess3.api.IEssentials;
import net.ess3.api.IReload;
import net.ess3.api.IUser;
import net.ess3.permissions.Permissions;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;


public class EssentialsGeoIPPlayerListener implements Listener, IReload
{
	private LookupService ls = null;
	private static final Logger LOGGER = Logger.getLogger("Minecraft");
	private File databaseFile;
	private final ConfigHolder config;
	private final IEssentials ess;
	private final Plugin geoip;

	public EssentialsGeoIPPlayerListener(final Plugin geoip, final IEssentials ess)
	{
		super();
		this.ess = ess;
		this.geoip = geoip;
		this.config = new ConfigHolder(ess, geoip);
		onReload();
	}

	@EventHandler(priority = EventPriority.MONITOR)
	public void onPlayerJoin(final PlayerJoinEvent event)
	{
		if (Permissions.GEOIP_HIDE.isAuthorized(event.getPlayer()))
		{
			return;
		}
		if (event.getPlayer().getAddress() == null || event.getPlayer().getAddress().getAddress() == null)
		{
			return;
		}
		final InetAddress address = event.getPlayer().getAddress().getAddress();

		final StringBuilder builder = new StringBuilder();
		if (config.getData().getDatabase().isShowCities())
		{
			final Location loc = ls.getLocation(address);
			if (loc == null)
			{
				return;
			}
			if (loc.city != null)
			{
				builder.append(loc.city).append(", ");
			}
			final String region = regionName.regionNameByCode(loc.countryCode, loc.region);
			if (region != null)
			{
				builder.append(region).append(", ");
			}
			builder.append(loc.countryName);
		}
		else
		{
			builder.append(ls.getCountry(address).getName());
		}
		if (config.getData().isShowOnWhois())
		{
			final IUser u = ess.getUserMap().getUser(event.getPlayer());
			u.getData().setGeolocation(builder.toString());
			u.queueSave();
		}
		if (config.getData().isShowOnLogin())
		{
			for (Player player : event.getPlayer().getServer().getOnlinePlayers())
			{
				if (!player.canSee(event.getPlayer()))
				{
					continue;
				}
				if (Permissions.GEOIP_SHOW.isAuthorized(player))
				{
					player.sendMessage(_("Player {0} comes from {1}.", player.getDisplayName(), builder.toString()));
				}
			}
		}
	}

	@Override
	public final void onReload()
	{
		config.onReload();
		if (config.getData().getDatabase().isShowCities())
		{
			databaseFile = new File(geoip.getDataFolder(), "GeoIPCity.dat");
		}
		else
		{
			databaseFile = new File(geoip.getDataFolder(), "GeoIP.dat");
		}
		if (!databaseFile.exists())
		{
			if (config.getData().getDatabase().isDownloadIfMissing())
			{
				if (config.getData().getDatabase().isShowCities())
				{
					downloadDatabase(config.getData().getDatabase().getDownloadUrlCity());
				}
				else
				{
					downloadDatabase(config.getData().getDatabase().getDownloadUrl());
				}
			}
			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(final String url)
	{
		if (url == null || url.isEmpty())
		{
			LOGGER.log(Level.SEVERE, _("GeoIP download url is empty."));
			return;
		}
		if (!databaseFile.getAbsoluteFile().getParentFile().exists())
		{
			databaseFile.getAbsoluteFile().getParentFile().mkdirs();
		}
		InputStream input = null;
		OutputStream output = null;
		try
		{
			LOGGER.log(Level.INFO, _("Downloading GeoIP database... this might take a while (country: 0.6 MB, city: 20MB)"));
			final URL downloadUrl = new URL(url);
			final URLConnection conn = downloadUrl.openConnection();
			conn.setConnectTimeout(10000);
			conn.connect();
			input = conn.getInputStream();
			if (url.endsWith(".gz"))
			{
				input = new GZIPInputStream(input);
			}
			output = new FileOutputStream(databaseFile);
			final byte[] buffer = new byte[2048];
			int length = input.read(buffer);
			while (length >= 0)
			{
				output.write(buffer, 0, length);
				length = input.read(buffer);
			}
			input.close();
			output.close();
		}
		catch (MalformedURLException ex)
		{
			LOGGER.log(Level.SEVERE, _("GeoIP download url is invalid."), ex);
		}
		catch (IOException ex)
		{
			LOGGER.log(Level.SEVERE, _("Failed to open connection."), ex);
		}
		finally
		{
			if (output != null)
			{
				try
				{
					output.close();
				}
				catch (IOException ex)
				{
					LOGGER.log(Level.SEVERE, _("Failed to open connection."), ex);
				}
			}
			if (input != null)
			{
				try
				{
					input.close();
				}
				catch (IOException ex)
				{
					LOGGER.log(Level.SEVERE, _("Failed to open connection."), ex);
				}
			}
		}
	}
}