Add verbose pastebin upload functionality - resolves #55

This commit is contained in:
Luck 2016-12-03 19:30:25 +00:00
parent 8b60fd0e55
commit 8f9a1aa931
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
7 changed files with 183 additions and 18 deletions

View File

@ -124,7 +124,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
Executor bukkitAsyncExecutor = r -> getServer().getScheduler().runTaskAsynchronously(this, r);
log = LogFactory.wrap(getLogger());
debugHandler = new DebugHandler(bukkitAsyncExecutor);
debugHandler = new DebugHandler(bukkitAsyncExecutor, getVersion());
senderFactory = new BukkitSenderFactory(this);
permissionCache = new PermissionCache(bukkitAsyncExecutor);

View File

@ -100,7 +100,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
public void onEnable() {
executor = r -> getProxy().getScheduler().runAsync(this, r);
log = LogFactory.wrap(getLogger());
debugHandler = new DebugHandler(executor);
debugHandler = new DebugHandler(executor, getVersion());
senderFactory = new BungeeSenderFactory(this);
permissionCache = new PermissionCache(executor);

View File

@ -22,6 +22,8 @@
package me.lucko.luckperms.common.commands.misc;
import com.google.common.collect.ImmutableList;
import me.lucko.luckperms.common.LuckPermsPlugin;
import me.lucko.luckperms.common.commands.Arg;
import me.lucko.luckperms.common.commands.CommandResult;
@ -39,7 +41,7 @@ public class VerboseCommand extends SingleCommand {
public VerboseCommand() {
super("Verbose", "Enable verbose permission check output", "/%s verbose <true|false> [filters...]", Permission.VERBOSE, Predicates.is(0),
Arg.list(
Arg.create("true|false", true, "whether to enable the feature"),
Arg.create("true|false|record|paste", true, "whether to enable/disable logging, or start/stop recording"),
Arg.create("filters...", false, "the name of the user / start of the node to filter by")
)
);
@ -52,30 +54,63 @@ public class VerboseCommand extends SingleCommand {
return CommandResult.INVALID_ARGS;
}
if (!args.get(0).equalsIgnoreCase("true") && !args.get(0).equalsIgnoreCase("false")) {
sendUsage(sender, label);
return CommandResult.INVALID_ARGS;
String mode = args.get(0).toLowerCase();
if (mode.equals("on") || mode.equals("true")) {
List<String> filters = new ArrayList<>();
if (args.size() != 1) {
filters.addAll(args.subList(1, args.size()));
}
plugin.getDebugHandler().register(sender, filters);
if (!filters.isEmpty()) {
Message.VERBOSE_ON_QUERY.send(sender, filters.stream().collect(Collectors.joining("&7, &f")));
} else {
Message.VERBOSE_ON.send(sender);
}
return CommandResult.SUCCESS;
}
if (args.get(0).equalsIgnoreCase("false")) {
if (mode.equals("off") || mode.equals("false")) {
plugin.getDebugHandler().unregister(sender.getUuid());
Message.VERBOSE_OFF.send(sender);
return CommandResult.SUCCESS;
}
List<String> filters = new ArrayList<>();
if (args.size() != 1) {
filters.addAll(args.subList(1, args.size()));
if (mode.equals("record")) {
List<String> filters = new ArrayList<>();
if (args.size() != 1) {
filters.addAll(args.subList(1, args.size()));
}
plugin.getDebugHandler().setPastedFilters(ImmutableList.copyOf(filters));
plugin.getDebugHandler().setPasted(true);
if (!filters.isEmpty()) {
Message.VERBOSE_RECORDING_ON_QUERY.send(sender, filters.stream().collect(Collectors.joining("&7, &f")));
} else {
Message.VERBOSE_RECORDING_ON.send(sender);
}
return CommandResult.SUCCESS;
}
if (mode.equals("paste")) {
plugin.getDebugHandler().setPasted(false);
Message.VERBOSE_RECORDING_UPLOAD_START.send(sender);
plugin.getDebugHandler().register(sender, filters);
if (!filters.isEmpty()) {
Message.VERBOSE_ON_QUERY.send(sender, filters.stream().collect(Collectors.joining("&7, &f")));
} else {
Message.VERBOSE_ON.send(sender);
String url = plugin.getDebugHandler().uploadPastedData();
if (url == null) {
url = "null";
}
Message.VERBOSE_RECORDING_URL.send(sender, url);
return CommandResult.SUCCESS;
}
return CommandResult.SUCCESS;
sendUsage(sender, label);
return CommandResult.INVALID_ARGS;
}
}

View File

@ -86,6 +86,11 @@ public enum Message {
VERBOSE_ON_QUERY("&bVerbose checking output set to &aTRUE &bfor permissions matching the following filters: &f{0}", true),
VERBOSE_OFF("&bVerbose checking output set to &cFALSE&b.", true),
VERBOSE_RECORDING_ON("&bVerbose recording set to &aTRUE &bfor all permissions.", true),
VERBOSE_RECORDING_ON_QUERY("&bVerbose recording set to &aTRUE &bfor permissions matching the following filters: &f{0}", true),
VERBOSE_RECORDING_UPLOAD_START("&bVerbose recording was disabled. Uploading results...", true),
VERBOSE_RECORDING_URL("&aVerbose results URL: {0}", true),
CREATE_SUCCESS("&b{0}&a was successfully created.", true),
DELETE_SUCCESS("&b{0}&a was successfully deleted.", true),
RENAME_SUCCESS("&b{0}&a was successfully renamed to &b{1}&a.", true),

View File

@ -25,6 +25,7 @@ package me.lucko.luckperms.common.utils;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import com.google.common.collect.ImmutableList;
@ -32,6 +33,8 @@ import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.constants.Message;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Queue;
@ -39,12 +42,24 @@ import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;
public class DebugHandler {
private final String pluginVersion;
@Setter
private boolean pasted = false;
@Setter
private List<String> pastedFilters = ImmutableList.of();
private final List<String> pastedList = Collections.synchronizedList(new ArrayList<>());
private final Map<Receiver, List<String>> listeners;
private final Queue<Data> queue;
public DebugHandler(Executor executor) {
public DebugHandler(Executor executor, String pluginVersion) {
this.pluginVersion = "v" + pluginVersion;
listeners = new ConcurrentHashMap<>();
queue = new ConcurrentLinkedQueue<>();
@ -63,6 +78,10 @@ public class DebugHandler {
}
private void handleOutput(String checked, String node, Tristate value) {
if (pasted) {
pastedList.add("`" + checked + "` - " + node + " - **" + value.toString() + "** ");
}
all:
for (Map.Entry<Receiver, List<String>> e : listeners.entrySet()) {
for (String filter : e.getValue()) {
@ -93,6 +112,21 @@ public class DebugHandler {
listeners.remove(new Receiver(uuid, null));
}
public String uploadPastedData() {
List<String> output = ImmutableList.<String>builder()
.add("#### This file was automatically generated by [LuckPerms](https://github.com/lucko/LuckPerms) " + pluginVersion)
.add("")
.add("Format: `<checked>` `<permission>` `<value>`")
.add("")
.add("___")
.add("")
.addAll(pastedList)
.build();
pastedList.clear();
return PasteUtils.paste(output.stream().collect(Collectors.joining("\n")));
}
@Getter
@EqualsAndHashCode(of = "uuid")
@AllArgsConstructor

View File

@ -0,0 +1,91 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.utils;
import com.google.common.base.Charsets;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class PasteUtils {
public static void main(String[] args) {
String s = "hey\nlol\nxd";
System.out.println(paste(s));
}
public static String paste(String contents) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL("https://api.github.com/gists").openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
try (OutputStream os = connection.getOutputStream()) {
StringWriter sw = new StringWriter();
new JsonWriter(sw).beginObject()
.name("description").value("LuckPerms Verbose Output")
.name("public").value(false)
.name("files")
.beginObject().name("lp-verbose.md")
.beginObject().name("content").value(contents)
.endObject()
.endObject()
.endObject();
os.write(sw.toString().getBytes());
}
if (connection.getResponseCode() >= 400) {
return null;
}
JsonObject response = new Gson().fromJson(new InputStreamReader(connection.getInputStream(), Charsets.UTF_8), JsonObject.class);
String pasteUrl = response.get("html_url").getAsString();
connection.disconnect();
try {
connection = (HttpURLConnection) new URL("https://git.io").openConnection();
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
try (OutputStream os = connection.getOutputStream()) {
os.write(("url=" + pasteUrl).getBytes());
}
pasteUrl = connection.getHeaderField("Location");
connection.disconnect();
} catch (Exception e) {
// ignored
}
return pasteUrl;
} catch (Exception e) {
return null;
}
}
}

View File

@ -151,7 +151,7 @@ public class LPSpongePlugin implements LuckPermsPlugin {
@Listener(order = Order.FIRST)
public void onEnable(GamePreInitializationEvent event) {
log = LogFactory.wrap(logger);
debugHandler = new DebugHandler(asyncExecutor);
debugHandler = new DebugHandler(asyncExecutor, getVersion());
senderFactory = new SpongeSenderFactory(this);
permissionCache = new PermissionCache(asyncExecutor);
timings = new LPTimings(this);