2016-03-02 19:28:01 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.command;
|
2013-07-27 21:49:25 +00:00
|
|
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2016-03-06 15:56:15 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.admin.Admin;
|
2016-03-06 15:56:15 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
|
|
|
import me.totalfreedom.totalfreedommod.rank.Rank;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
2014-11-29 19:16:00 +00:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2013-07-27 21:49:25 +00:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
@CommandPermissions(level = Rank.SUPER_ADMIN, source = SourceType.ONLY_IN_GAME)
|
2013-07-29 17:20:08 +00:00
|
|
|
@CommandParameters(description = "Register your connection with the TFM logviewer.", usage = "/<command> [off]")
|
2015-10-19 17:43:46 +00:00
|
|
|
public class Command_logs extends FreedomCommand
|
2013-07-27 21:49:25 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2013-07-27 21:49:25 +00:00
|
|
|
@Override
|
2015-11-22 18:26:47 +00:00
|
|
|
public boolean run(final CommandSender sender, final Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
|
2013-07-27 21:49:25 +00:00
|
|
|
{
|
2013-07-29 17:20:08 +00:00
|
|
|
LogsRegistrationMode mode = LogsRegistrationMode.UPDATE;
|
|
|
|
|
|
|
|
if (args.length == 1)
|
2013-07-27 21:49:25 +00:00
|
|
|
{
|
2014-05-05 13:31:12 +00:00
|
|
|
mode = ("off".equals(args[0]) ? LogsRegistrationMode.DELETE : LogsRegistrationMode.UPDATE);
|
2013-07-27 21:49:25 +00:00
|
|
|
}
|
|
|
|
|
2015-11-22 18:26:47 +00:00
|
|
|
updateLogsRegistration(sender, playerSender, mode);
|
2013-07-27 21:49:25 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateLogsRegistration(final CommandSender sender, final Player target, final LogsRegistrationMode mode)
|
|
|
|
{
|
|
|
|
updateLogsRegistration(sender, target.getName(), target.getAddress().getAddress().getHostAddress().trim(), mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateLogsRegistration(final CommandSender sender, final String targetName, final String targetIP, final LogsRegistrationMode mode)
|
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
final String logsRegisterUrl = ConfigEntry.LOGS_URL.getString();
|
2015-10-19 17:43:46 +00:00
|
|
|
final String logsRegisterPassword = ConfigEntry.LOGS_SECRET.getString();
|
2013-07-27 21:49:25 +00:00
|
|
|
|
2015-11-22 18:26:47 +00:00
|
|
|
if (logsRegisterUrl == null || logsRegisterPassword == null || logsRegisterUrl.isEmpty() || logsRegisterPassword.isEmpty())
|
2013-07-27 21:49:25 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new BukkitRunnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (sender != null)
|
|
|
|
{
|
|
|
|
sender.sendMessage(ChatColor.YELLOW + "Connecting...");
|
|
|
|
}
|
|
|
|
|
2015-11-22 18:26:47 +00:00
|
|
|
URL url = new URLBuilder(logsRegisterUrl)
|
2013-07-27 21:49:25 +00:00
|
|
|
.addQueryParameter("mode", mode.toString())
|
2013-08-18 19:52:32 +00:00
|
|
|
.addQueryParameter("password", logsRegisterPassword)
|
2013-07-27 21:49:25 +00:00
|
|
|
.addQueryParameter("name", targetName)
|
|
|
|
.addQueryParameter("ip", targetIP)
|
|
|
|
.getURL();
|
|
|
|
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
connection.setConnectTimeout(1000 * 5);
|
|
|
|
connection.setReadTimeout(1000 * 5);
|
|
|
|
connection.setUseCaches(false);
|
|
|
|
connection.setRequestMethod("HEAD");
|
|
|
|
|
|
|
|
final int responseCode = connection.getResponseCode();
|
|
|
|
|
|
|
|
if (sender != null)
|
|
|
|
{
|
|
|
|
new BukkitRunnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
if (responseCode == 200)
|
|
|
|
{
|
|
|
|
sender.sendMessage(ChatColor.GREEN + "Registration " + mode.toString() + "d.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sender.sendMessage(ChatColor.RED + "Error contacting logs registration server.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.runTask(TotalFreedomMod.plugin);
|
|
|
|
}
|
|
|
|
}
|
2013-08-14 13:28:19 +00:00
|
|
|
catch (Exception ex)
|
2013-07-27 21:49:25 +00:00
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-07-27 21:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}.runTaskAsynchronously(TotalFreedomMod.plugin);
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static void deactivateSuperadmin(Admin superadmin)
|
2013-07-27 21:49:25 +00:00
|
|
|
{
|
|
|
|
for (String ip : superadmin.getIps())
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
updateLogsRegistration(null, superadmin.getName(), ip, Command_logs.LogsRegistrationMode.DELETE);
|
2013-07-27 21:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static enum LogsRegistrationMode
|
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2013-07-27 21:49:25 +00:00
|
|
|
UPDATE("update"), DELETE("delete");
|
|
|
|
private final String mode;
|
|
|
|
|
|
|
|
private LogsRegistrationMode(String mode)
|
|
|
|
{
|
|
|
|
this.mode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class URLBuilder
|
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2013-07-27 21:49:25 +00:00
|
|
|
private final String requestPath;
|
2016-03-02 19:28:01 +00:00
|
|
|
private final Map<String, String> queryStringMap = new HashMap<>();
|
2013-07-27 21:49:25 +00:00
|
|
|
|
|
|
|
public URLBuilder(String requestPath)
|
|
|
|
{
|
|
|
|
this.requestPath = requestPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public URLBuilder addQueryParameter(String key, String value)
|
|
|
|
{
|
|
|
|
queryStringMap.put(key, value);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public URL getURL() throws MalformedURLException
|
|
|
|
{
|
2016-03-02 19:28:01 +00:00
|
|
|
List<String> pairs = new ArrayList<>();
|
2013-07-27 21:49:25 +00:00
|
|
|
Iterator<Entry<String, String>> it = queryStringMap.entrySet().iterator();
|
|
|
|
while (it.hasNext())
|
|
|
|
{
|
|
|
|
Entry<String, String> pair = it.next();
|
|
|
|
pairs.add(pair.getKey() + "=" + pair.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
return new URL(requestPath + "?" + StringUtils.join(pairs, "&"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|