summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/modules/check/Check.java
blob: 8ebdcc0ab140b60d38fab593dc638d15f06f4726 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package com.redstoner.modules.check;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

import com.redstoner.exceptions.NonSaveableConfigException;
import com.redstoner.misc.mysql.Config;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import com.nemez.cmdmgr.Command;
import com.nemez.cmdmgr.Command.AsyncType;
import com.nemez.cmdmgr.CommandManager;
import com.redstoner.annotations.Commands;
import com.redstoner.annotations.Version;
import com.redstoner.coremods.moduleLoader.ModuleLoader;
import com.redstoner.misc.CommandHolderType;
import com.redstoner.misc.Main;
import com.redstoner.misc.mysql.MysqlHandler;
import com.redstoner.misc.mysql.elements.ConstraintOperator;
import com.redstoner.misc.mysql.elements.MysqlConstraint;
import com.redstoner.misc.mysql.elements.MysqlDatabase;
import com.redstoner.misc.mysql.elements.MysqlTable;
import com.redstoner.modules.Module;

import net.nemez.chatapi.click.Message;

@Commands(CommandHolderType.File)
@Version(major = 5, minor = 0, revision = 0, compatible = 4)
public class Check implements Module, Listener {
	MysqlTable table;
	String noTableReason;

	@Override
	public boolean onEnable() {
		Config config;

		try {
			config = Config.getConfig("check.json");
		} catch (IOException | org.json.simple.parser.ParseException e1) {
			e1.printStackTrace();
			return false;
		}

		if (config == null || !config.containsKey("database") || !config.containsKey("table")) {
			getLogger().warn("Could not load the Check config file, ip info for offline users and website data will be unavaliable!");
			noTableReason = "Could not load the config file";
			
			config.put("database", "redstoner");
			config.put("table", "users");

			try {
				config.save();
				getLogger().info("A default config file has been created for you.");
			} catch (IOException | NonSaveableConfigException e) {
				getLogger().error("A default config file was unable to be created.");
			}

			return true;
		}

		try {
			getLogger().info("Attempting to load the database and the table, this might take a few seconds.");
			MysqlDatabase database = MysqlHandler.INSTANCE.getDatabase(config.get("database") + "?autoReconnect=true");
			table = database.getTable(config.get("table"));
		} catch (Exception e) {
			getLogger().warn("Could not use the Check config file, ip info for offline users and website data will be unavaliable!");
			noTableReason = "Could not use the config file";
		}

		return true;
	}

	@Override
	public void postEnable() {
		CommandManager.registerCommand(getCommandString(), this, Main.plugin);
	}

	@SuppressWarnings("deprecation")
	@Command(hook = "checkCommand", async = AsyncType.ALWAYS)
	public void checkCommand(final CommandSender sender, final String player) {
		OfflinePlayer oPlayer = Bukkit.getPlayer(player);
		if (oPlayer == null) oPlayer = Bukkit.getServer().getOfflinePlayer(player);

		sendData(sender, oPlayer);

		if (ModuleLoader.exists("Tag")) {
			try {
				Bukkit.getScheduler().callSyncMethod(ModuleLoader.getPlugin(), () -> Bukkit.dispatchCommand(sender, "tag check " + player)).get();
			} catch (ExecutionException | InterruptedException e) {
				Message msg = new Message(sender, null);
				msg.appendText("&4Running /tag check failed! Please inform a dev about this incident!");

				e.printStackTrace();
			}
		}
	}
	
	@Command(hook = "ipinfo", async = AsyncType.ALWAYS)
	public void ipinfo(final CommandSender sender, final String ip) {
		String[] info = getIpInfo(ip);
		
		Message msg = new Message(sender, null);
		
		if (info == null) {
			msg.appendText("\n&6> &cData Unavailable");
		} else {
			String region = info[1];
			String asn = info[2];
			String org = info[3];

			msg.appendText("\n&6> IP: ").appendSuggestHover("&e" + ip, ip, "Click to copy!");
			msg.appendText("\n&6> Region: ").appendSuggestHover("&e" + region, region, "Click to copy!");
			msg.appendText("\n&6> ASN: ").appendSuggestHover("&e" + asn, asn, "Click to copy!");
			msg.appendText("\n&6> Org: ").appendSuggestHover("&e" + org, org, "Click to copy!");
		} 
		
		msg.send();
	}

	public String read(URL url) {
		String data = "";

		try {
			Scanner in = new Scanner(new InputStreamReader(url.openStream()));

			while (in.hasNextLine()) {
				data += in.nextLine();
			}

			in.close();
			return data;
		} catch (IOException e) {}

		return null;
	}

	public String[] getIpInfo(OfflinePlayer player) {
		String ip = "";
		
		if (player.isOnline()) {
			ip = player.getPlayer().getAddress().getHostString();
		} else if (table != null) {
			try {
				ip = (String) table.get("last_ip", new MysqlConstraint("uuid", ConstraintOperator.EQUAL, player.getUniqueId().toString().replace("-", "")))[0];
			} catch (Exception e) {
				return null;
			}
		} else {
			return null;
		}
		return getIpInfo(ip);
	}
	
	public String[] getIpInfo(String ip) {
		String[] info = new String[4];

		try {
			URL ipinfo = new URL("https://ipapi.co/" + ip + "/json");
			JSONObject json = (JSONObject) new JSONParser().parse(read(ipinfo));

			info[0] = ip;

			Object o_country = json.get("country_name");
			Object o_region = json.get("region");
			Object o_asn = json.get("asn");
			Object o_org = json.get("org");

			String country = o_country == null ? "Unknown" : (String) o_country;
			String region = o_region == null ? "" : ", " + (String) o_region;
			String asn = o_asn == null ? "Unknown" : (String) o_asn;
			String org = o_org == null ? "Unknown" : (String) o_org;

			info[1] = country.equals("") ? "Unknown" : country + (region.equals(", ") ? "" : region);
			info[2] = asn.equals("") ? "Unknown" : asn;
			info[3] = org.equals("") ? "Unknown" : org;

			return info;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public String getFirstJoin(OfflinePlayer player) {
		Long firstJoin = player.getFirstPlayed();
		Date date = new Date(firstJoin);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return format.format(date);
	}

	public String getLastSeen(OfflinePlayer player) {
		Long lastSeen = player.getLastPlayed();
		Date date = new Date(lastSeen);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return format.format(date);
	}

	public Object[] getWebsiteData(OfflinePlayer player) {
		if (table == null)
			return null;

		MysqlConstraint constraint = new MysqlConstraint("uuid", ConstraintOperator.EQUAL, player.getUniqueId().toString().replace("-", ""));

		try {
			int id = (int) table.get("id", constraint)[0];
			String email = (String) table.get("email", constraint)[0];
			boolean confirmed = (boolean) table.get("confirmed", constraint)[0];

			return new Object[] { "https://redstoner.com/users/" + id, email, confirmed };
		} catch (Exception e) {
			try {
				int id = (int) table.get("id", constraint)[0];
				String email = (String) table.get("email", constraint)[0];
				boolean confirmed = (boolean) table.get("confirmed", constraint)[0];

				return new Object[] { "https://redstoner.com/users/" + id, email, confirmed };
			} catch (Exception e2) {}

			return new Object[] { null };
		}
	}

	public String getAllNames(OfflinePlayer player) {
		String uuid = player.getUniqueId().toString().replace("-", "");
		String nameString = "";

		try {
			String rawJson = read(new URL("https://api.mojang.com/user/profiles/" + uuid + "/names"));
			JSONArray names = (JSONArray) new JSONParser().parse(rawJson);

			for (Object obj : names) {
				nameString += "&e" + ((JSONObject) obj).get("name") + "&7, ";
			}

			nameString = nameString.substring(0, nameString.length() - 2);
			return nameString;
		} catch (Exception e) {}

		return "None";
	}

	public void sendData(CommandSender sender, OfflinePlayer player) {
		try {
			// data
			String uuid = player.getUniqueId().toString();

			String firstJoin = getFirstJoin(player);
			String lastSeen = getLastSeen(player);
			firstJoin = (firstJoin.equals("1970-01-01 01:00")) ? "&eNever" : "&7(yyyy-MM-dd hh:mm) &e" + firstJoin;
			lastSeen = (lastSeen.equals("1970-1-1 01:00")) ? "&eNever" : "&7(yyyy-MM-dd hh:mm) &e" + lastSeen;

			Object[] websiteData = getWebsiteData(player);


			String[] ipInfo = getIpInfo(player);

			String namesUsed = getAllNames(player);

			// messages
			Message msg = new Message(sender, null);

			msg.appendText("\n" + getLogger().getHeader());
			msg.appendText("\n&7Data provided by redstoner:");
			msg.appendText("\n&6> UUID: ").appendSuggestHover("&e" + uuid, uuid, "Click to copy!");
			msg.appendText("\n&6> First joined: &e" + firstJoin);
			msg.appendText("\n&6> Last Seen: &e" + lastSeen);

			if (websiteData != null) {
				String websiteUrl = (websiteData[0] == null) ? "None" : (String) websiteData[0];
				String email = (websiteData[0] == null) ? "Unknown" : (String) websiteData[1];
				boolean emailNotConfirmed = (websiteData[0] == null) ? false : !((boolean) websiteData[2]);

				msg.appendText("\n&6> Website account: &e").appendLink(websiteUrl, websiteUrl);
				msg.appendText("\n&6> Email: &e" + (emailNotConfirmed ? "\n&6> &cEmail NOT Confirmed!" : "")).appendSuggestHover("&e" + email, email, "Click to copy!");
			}
			else {
				msg.appendTextHover("\n&6> Website account: &cData Unavailable", "&c" + noTableReason);
				msg.appendTextHover("\n&6> Email: &cData Unavailable", "&c" + noTableReason);
			}
			msg.appendText("\n\n&7Data provided by ipapi.co:");

			if (ipInfo == null && table == null) {
				msg.appendTextHover("\n&6> &cData Unavailable", "&c" + noTableReason);
			} else if (ipInfo == null) {
				msg.appendText("\n&6> &cData Unavailable");
			} else {
				String ip = ipInfo[0];
				String region = ipInfo[1];
				String asn = ipInfo[2];
				String org = ipInfo[3];

				msg.appendText("\n&6> IP: ").appendSuggestHover("&e" + ip, ip, "Click to copy!");
				msg.appendText("\n&6> Region: ").appendSuggestHover("&e" + region, region, "Click to copy!");
				msg.appendText("\n&6> ASN: ").appendSuggestHover("&e" + asn, asn, "Click to copy!");
				msg.appendText("\n&6> Org: ").appendSuggestHover("&e" + org, org, "Click to copy!");
			}

			msg.appendText("\n\n&7Data provided by mojang:");
			msg.appendText("\n&6> All ingame names used so far: &e" + namesUsed);
			msg.send();
		} catch (Exception e) {
			getLogger().message(sender, true, "Sorry, something went wrong while fetching data");
			e.printStackTrace();
		}
	}
}