summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandseen.java
blob: f82342c30daa4add3e0d89f42fe754c67e6b5d67 (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
215
216
package com.earth2me.essentials.commands;

import com.earth2me.essentials.CommandSource;
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.User;
import com.earth2me.essentials.UserMap;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.FormatUtil;
import com.earth2me.essentials.utils.StringUtil;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.Server;


public class Commandseen extends EssentialsCommand
{
	public Commandseen()
	{
		super("seen");
	}

	@Override
	protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		seen(server, sender, args, true, true, true);
	}

	@Override
	protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
	{
		seen(server, user.getSource(), args, user.isAuthorized("essentials.seen.banreason"), user.isAuthorized("essentials.seen.extra"), user.isAuthorized("essentials.seen.ipsearch"));
	}

	protected void seen(final Server server, final CommandSource sender, final String[] args, final boolean showBan, final boolean extra, final boolean ipLookup) throws Exception
	{
		if (args.length < 1)
		{
			throw new NotEnoughArgumentsException();
		}
		try
		{
			User user = getPlayer(server, sender, args, 0);
			seenOnline(server, sender, user, showBan, extra);
		}
		catch (NoSuchFieldException e)
		{
			User player = ess.getOfflineUser(args[0]);
			if (player == null)
			{
				if (ipLookup && FormatUtil.validIP(args[0]))
				{
					seenIP(server, sender, args[0]);
					return;
				}
				else if (Bukkit.getBanList(BanList.Type.IP).isBanned(args[0]))
				{
					sender.sendMessage(tl("isIpBanned", args[0]));
					return;
				}
				
				else if (Bukkit.getBanList(BanList.Type.NAME).isBanned(args[0]))
				{
					sender.sendMessage(tl("whoisBanned", showBan ? Bukkit.getBanList(BanList.Type.NAME).getBanEntry(args[0]).getReason() : tl("true")));
					return;
				}
				else
				{
					throw new PlayerNotFoundException();
				}
			}
			seenOffline(server, sender, player, showBan, extra);
		}
	}

	private void seenOnline(final Server server, final CommandSource sender, final User user, final boolean showBan, final boolean extra) throws Exception
	{

		user.setDisplayNick();
		sender.sendMessage(tl("seenOnline", user.getDisplayName(), DateUtil.formatDateDiff(user.getLastLogin())));

		if (ess.getSettings().isDebug())
		{
			ess.getLogger().info("UUID: " + user.getBase().getUniqueId().toString());
		}

		List<String> history = ess.getUserMap().getUserHistory(user.getBase().getUniqueId());
		if (history != null && history.size() > 1)
		{
			sender.sendMessage(tl("seenAccounts", StringUtil.joinListSkip(", ", user.getName(), history)));
		}

		if (user.isAfk())
		{
			sender.sendMessage(tl("whoisAFK", tl("true")));
		}
		if (user.isJailed())
		{
			sender.sendMessage(tl("whoisJail", (user.getJailTimeout() > 0
												? DateUtil.formatDateDiff(user.getJailTimeout())
												: tl("true"))));
		}
		if (user.isMuted())
		{
			sender.sendMessage(tl("whoisMuted", (user.getMuteTimeout() > 0
												 ? DateUtil.formatDateDiff(user.getMuteTimeout())
												 : tl("true"))));
		}
		final String location = user.getGeoLocation();
		if (location != null && (!(sender.isPlayer()) || ess.getUser(sender.getPlayer()).isAuthorized("essentials.geoip.show")))
		{
			sender.sendMessage(tl("whoisGeoLocation", location));
		}
		if (extra)
		{
			sender.sendMessage(tl("whoisIPAddress", user.getBase().getAddress().getAddress().toString()));
		}
	}

	private void seenOffline(final Server server, final CommandSource sender, User user, final boolean showBan, final boolean extra) throws Exception
	{
		user.setDisplayNick();
		if (user.getLastLogout() > 0)
		{
			sender.sendMessage(tl("seenOffline", user.getName(), DateUtil.formatDateDiff(user.getLastLogout())));
		}
		else
		{
			sender.sendMessage(tl("userUnknown", user.getName()));
		}

		if (ess.getSettings().isDebug())
		{
			ess.getLogger().info("UUID: " + user.getBase().getUniqueId().toString());
		}

		List<String> history = ess.getUserMap().getUserHistory(user.getBase().getUniqueId());
		if (history != null && history.size() > 1)
		{
			sender.sendMessage(tl("seenAccounts", StringUtil.joinListSkip(", ", user.getName(), history)));
		}

		if (user.getBase().isBanned())
		{
			sender.sendMessage(tl("whoisBanned", showBan ? Bukkit.getBanList(BanList.Type.NAME).getBanEntry(user.getName()).getReason() : tl("true")));
		}
		final String location = user.getGeoLocation();
		if (location != null && (!(sender.isPlayer()) || ess.getUser(sender.getPlayer()).isAuthorized("essentials.geoip.show")))
		{
			sender.sendMessage(tl("whoisGeoLocation", location));
		}
		if (extra)
		{
			if (!user.getLastLoginAddress().isEmpty())
			{
				sender.sendMessage(tl("whoisIPAddress", user.getLastLoginAddress()));
			}
			final Location loc = user.getLogoutLocation();
			if (loc != null)
			{
				sender.sendMessage(tl("whoisLocation", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
			}
		}
	}

	private void seenIP(final Server server, final CommandSource sender, final String ipAddress) throws Exception
	{
		final UserMap userMap = ess.getUserMap();

		if (Bukkit.getBanList(BanList.Type.IP).isBanned(ipAddress))
		{
			sender.sendMessage(tl("isIpBanned", ipAddress));
		}

		sender.sendMessage(tl("runningPlayerMatch", ipAddress));

		ess.runTaskAsynchronously(new Runnable()
		{
			@Override
			public void run()
			{
				final List<String> matches = new ArrayList<String>();
				for (final UUID u : userMap.getAllUniqueUsers())
				{
					final User user = ess.getUserMap().getUser(u);
					if (user == null)
					{
						continue;
					}

					final String uIPAddress = user.getLastLoginAddress();

					if (!uIPAddress.isEmpty() && uIPAddress.equalsIgnoreCase(ipAddress))
					{
						matches.add(user.getName());
					}
				}

				if (matches.size() > 0)
				{
					sender.sendMessage(tl("matchingIPAddress"));
					sender.sendMessage(StringUtil.joinList(matches));
				}
				else
				{
					sender.sendMessage(tl("noMatchingPlayers"));
				}

			}
		});

	}
}