Added escaping & fixed reset stuff #77

Merged
NorbiPeti merged 1 commit from dev into master 2018-08-10 20:34:16 +00:00
7 changed files with 75 additions and 55 deletions

View file

@ -9,6 +9,7 @@ import sx.blah.discord.util.RequestBuffer.IVoidRequest;
import javax.annotation.Nullable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
public final class DPUtils {
@ -16,24 +17,31 @@ public final class DPUtils {
return builder.withAuthorIcon("https://minotar.net/avatar/" + playername + "/32.png");
}
/** Removes §[char] colour codes from strings */
public static String sanitizeString(String string) {
String sanitizedString = "";
boolean random = false;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == '§') {
i++;// Skips the data value, the 4 in "§4Alisolarflare"
if (string.charAt(i) == 'k')
random = true;
else
random = false;
} else {
if (!random) // Skip random/obfuscated characters
sanitizedString += string.charAt(i);
}
}
return sanitizedString;
}
/**
* Removes §[char] colour codes from strings & escapes them for Discord <br>
* Ensure that this method only gets called once (escaping)
*/
public static String sanitizeString(String string) {
return escape(sanitizeStringNoEscape(string));
}
/**
* Removes §[char] colour codes from strings
*/
public static String sanitizeStringNoEscape(String string) {
String sanitizedString = "";
boolean random = false;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == '§') {
i++;// Skips the data value, the 4 in "§4Alisolarflare"
random = string.charAt(i) == 'k';
} else {
if (!random) // Skip random/obfuscated characters
sanitizedString += string.charAt(i);
}
}
return sanitizedString;
}
/**
* Performs Discord actions, retrying when ratelimited. May return null if action fails too many times or in safe mode.
@ -78,10 +86,14 @@ public final class DPUtils {
RequestBuffer.request(action);
}
public static <T> void performNoWait(IRequest<T> action) {
if (DiscordPlugin.SafeMode)
return;
RequestBuffer.request(action);
}
public static <T> void performNoWait(IRequest<T> action) {
if (DiscordPlugin.SafeMode)
return;
RequestBuffer.request(action);
}
public static String escape(String message) {
return message.replaceAll("([*_~])", Matcher.quoteReplacement("\\")+"$1");
}
}

View file

@ -1,7 +1,10 @@
package buttondevteam.discordplugin;
import buttondevteam.discordplugin.commands.DiscordCommandBase;
import buttondevteam.discordplugin.listeners.*;
import buttondevteam.discordplugin.listeners.CommandListener;
import buttondevteam.discordplugin.listeners.ExceptionListener;
import buttondevteam.discordplugin.listeners.MCChatListener;
import buttondevteam.discordplugin.listeners.MCListener;
import buttondevteam.discordplugin.mccommands.DiscordMCCommandBase;
import buttondevteam.discordplugin.mccommands.ResetMCCommand;
import buttondevteam.lib.TBMCCoreAPI;
@ -188,9 +191,9 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
sent = true;
if (TBMCCoreAPI.IsTestServer() && !dc.getOurUser().getName().toLowerCase().contains("test")) {
TBMCCoreAPI.SendException(
"Won't load because we're in testing mode and not using the separate account.",
"Won't load because we're in testing mode and not using a separate account.",
new Exception(
"The plugin refuses to load until you change the token to the testing account."));
"The plugin refuses to load until you change the token to a testing account."));
Bukkit.getPluginManager().disablePlugin(this);
}
TBMCCoreAPI.SendUnsentExceptions();
@ -213,7 +216,6 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
MCChatListener mcchat = new MCChatListener();
dc.getDispatcher().registerListener(mcchat);
TBMCCoreAPI.RegisterEventsForExceptions(mcchat, this);
TBMCCoreAPI.RegisterEventsForExceptions(new AutoUpdaterListener(), this);
Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this);
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
@ -288,6 +290,16 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
try {
SafeMode = true; // Stop interacting with Discord
MCChatListener.stop(true);
try {
if (PlayerListWatcher.hookDown())
System.out.println("Finished unhooking the player list!");
else
System.out.println("Didn't have the player list hooked.");
hooked = false;
} catch (Throwable e) {
e.printStackTrace();
Bukkit.getLogger().warning("Couldn't unhook the player list!");
}
ChromaBot.delete();
dc.changePresence(StatusType.IDLE, ActivityType.PLAYING, "Chromacraft"); //No longer using the same account for testing
dc.logout();

View file

@ -82,6 +82,25 @@ public class PlayerListWatcher extends DedicatedPlayerList {
}
}
public static boolean hookDown() {
try {
Field conf = CraftServer.class.getDeclaredField("console");
conf.setAccessible(true);
val server = (MinecraftServer) conf.get(Bukkit.getServer());
val plist = (DedicatedPlayerList) server.getPlayerList();
if (!(plist instanceof PlayerListWatcher))
return false;
server.a(((PlayerListWatcher) plist).plist);
Field pllf = CraftServer.class.getDeclaredField("playerList");
pllf.setAccessible(true);
pllf.set(Bukkit.getServer(), ((PlayerListWatcher) plist).plist);
return true;
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while hacking the player list!", e);
return true;
}
}
public void a(EntityHuman entityhuman, IChatBaseComponent ichatbasecomponent) {
plist.a(entityhuman, ichatbasecomponent);
}

View file

@ -1,24 +0,0 @@
package buttondevteam.discordplugin.listeners;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.PluginUpdater;
import buttondevteam.lib.TBMCCoreAPI;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
public class AutoUpdaterListener implements Listener {
@EventHandler
public void handle(PluginUpdater.UpdatedEvent event) {
if (DiscordPlugin.SafeMode)
return;
try {
DPUtils.performNoWait(() -> DiscordPlugin.officechannel.getMessageHistory(10).stream()
.filter(m -> m.getWebhookLongID() == 239123781401051138L && m.getEmbeds().get(0).getTitle()
.contains(event.getData().get("repository").getAsJsonObject().get("name").getAsString()))
.findFirst().get().addReaction(DiscordPlugin.DELIVERED_REACTION));
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while reacting to plugin update!", e);
}
}
}

View file

@ -213,7 +213,7 @@ public class CommandListener {
private static boolean checkanddeletemention(StringBuilder cmdwithargs, String mention, IMessage message) {
if (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text
if (cmdwithargs.length() > mention.length() + 1)
cmdwithargs = cmdwithargs.delete(0,
cmdwithargs.delete(0,
cmdwithargs.charAt(mention.length()) == ' ' ? mention.length() + 1 : mention.length());
else
cmdwithargs.replace(0, cmdwithargs.length(), "help");

View file

@ -76,9 +76,9 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
e = se.getKey();
time = se.getValue();
final String authorPlayer = "[" + DPUtils.sanitizeString(e.getChannel().DisplayName) + "] " //
final String authorPlayer = "[" + DPUtils.sanitizeStringNoEscape(e.getChannel().DisplayName) + "] " //
+ (e.getSender() instanceof DiscordSenderBase ? "[D]" : "") //
+ (DPUtils.sanitizeString(e.getSender() instanceof Player //
+ (DPUtils.sanitizeStringNoEscape(e.getSender() instanceof Player //
? ((Player) e.getSender()).getDisplayName() //
: e.getSender().getName()));
final EmbedBuilder embed = new EmbedBuilder().withAuthorName(authorPlayer)

View file

@ -66,6 +66,7 @@ public class MCListener implements Listener {
if (!DiscordPlugin.hooked)
MCChatListener.sendSystemMessageToChat(message);
MCChatListener.forAllowedCustomMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message), e.getPlayer());
//System.out.println("Does this appear more than once?"); //No
MCChatListener.ListC = 0;
ChromaBot.getInstance().updatePlayerList();
});
@ -120,7 +121,7 @@ public class MCListener implements Listener {
public void onPlayerAFK(AfkStatusChangeEvent e) { //TODO: Add AFK to custom chats?
if (e.isCancelled() || !e.getAffected().getBase().isOnline())
return;
MCChatListener.sendSystemMessageToChat(DPUtils.sanitizeString(e.getAffected().getBase().getDisplayName())
MCChatListener.sendSystemMessageToChat(e.getAffected().getBase().getDisplayName()
+ " is " + (e.getValue() ? "now" : "no longer") + " AFK.");
}