diff --git a/Slack.jar b/Slack.jar index 0f3b013..c3f94ed 100644 Binary files a/Slack.jar and b/Slack.jar differ diff --git a/pom.xml b/pom.xml index f767622..c7e8ff8 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 us.circuitsoft.slack Slack - 1.4.4 + 1.5.0 jar UTF-8 diff --git a/src/main/java/org/circuitsoft/slack/api/BukkitPoster.java b/src/main/java/org/circuitsoft/slack/api/BukkitPoster.java index 64f08f8..e96db6b 100644 --- a/src/main/java/org/circuitsoft/slack/api/BukkitPoster.java +++ b/src/main/java/org/circuitsoft/slack/api/BukkitPoster.java @@ -4,6 +4,7 @@ import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; +import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import org.json.simple.JSONObject; @@ -15,34 +16,49 @@ */ public class BukkitPoster extends BukkitRunnable { - private final String name; private final String message; - private final String webhookUrl = getWebhookUrl(); + private final String name; private final String iconUrl; + private final boolean useMarkdown; + private final String webhookUrl = getWebhookUrl(); /** - * Prepares the message to send to Slack. + * Posts a message to Slack. * - * @param message The message to send to Slack. - * @param name The username of the message to send to Slack. - * @param iconUrl The image URL of the user that sends the message to Slack. Make this null if the username is a Minecraft player name. + * @param message The message sent to Slack. + * @param name The name attributed to the message sent to Slack. + * @param iconUrl The image URL of the user that sends the message to Slack. + * @param useMarkdown Use markdown formatting in the message. */ - public BukkitPoster(String message, String name, String iconUrl) { - this.name = name; + public BukkitPoster(String message, String name, String iconUrl, boolean useMarkdown) { this.message = message; + this.name = name; + this.useMarkdown = useMarkdown; this.iconUrl = iconUrl; } + + /** + * Posts a player sent message to Slack. + * + * @param message The message sent to Slack. + * @param player The player that sent the message. + * @param useMarkdown Use markdown formatting in the message. + */ + public BukkitPoster(String message, Player player, boolean useMarkdown) { + this.message = message; + name = player.getName(); + iconUrl = "https://cravatar.eu/helmhead/" + player.getUniqueId().toString() + "/128.png"; + this.useMarkdown = useMarkdown; + } + @Override public void run() { JSONObject json = new JSONObject(); - json.put("text", name + ": " + message); + json.put("text", message); json.put("username", name); - if (iconUrl == null) { - json.put("icon_url", "https://cravatar.eu/helmhead/" + name + "/100.png"); - } else { - json.put("icon_url", iconUrl); - } + json.put("icon_url", iconUrl); + json.put("mrkdwn", useMarkdown); String jsonStr = "payload=" + json.toJSONString(); try { HttpURLConnection webhookConnection = (HttpURLConnection) new URL(webhookUrl).openConnection(); @@ -51,8 +67,11 @@ public void run() { try (BufferedOutputStream bufOut = new BufferedOutputStream(webhookConnection.getOutputStream())) { bufOut.write(jsonStr.getBytes(StandardCharsets.UTF_8)); bufOut.flush(); + bufOut.close(); } + int serverResponseCode = webhookConnection.getResponseCode(); webhookConnection.disconnect(); + webhookConnection = null; } catch (Exception ignored) { } } diff --git a/src/main/java/org/circuitsoft/slack/api/BungeePoster.java b/src/main/java/org/circuitsoft/slack/api/BungeePoster.java index c861fe6..172438b 100644 --- a/src/main/java/org/circuitsoft/slack/api/BungeePoster.java +++ b/src/main/java/org/circuitsoft/slack/api/BungeePoster.java @@ -5,6 +5,7 @@ import java.net.URL; import com.google.gson.JsonObject; +import net.md_5.bungee.api.connection.ProxiedPlayer; import static org.circuitsoft.slack.bungee.SlackBungee.getWebhookUrl; @@ -12,35 +13,67 @@ * Posts a message to Slack when using Bungee. */ public class BungeePoster implements Runnable { - - private final String name; + private final String message; + private final String name; + private final String iconUrl; + private final boolean useMarkdown; private final String webhookUrl = getWebhookUrl(); - private final String icon; /** - * Prepares the message to send to Slack. + * Posts a message to Slack involving the proxy or network. * - * @param message The message to send to Slack. - * @param name The username of the message to send to Slack. - * @param icon The image URL of the user that sends the message to Slack. Make this null if the username is a Minecraft player name. + * @param message The message sent to Slack. + * @param name The name attributed to the message sent to Slack. + * @param iconUrl The image URL of the user that sends the message to Slack. + * @param useMarkdown Use markdown formatting in the message. */ - public BungeePoster(String message, String name, String icon) { + public BungeePoster(String message, String name, String iconUrl, boolean useMarkdown) { + this.message = message; this.name = name; + this.useMarkdown = useMarkdown; + this.iconUrl = iconUrl; + } + + /** + * Posts a message to Slack involving a server on the proxy. + * + * @param message The message sent to Slack. + * @param name The name attributed to the message sent to Slack. + * @param iconUrl The image URL of the user that sends the message to Slack. + * @param serverName The server the event took place on. + * @param useMarkdown Use markdown formatting in the message. + */ + public BungeePoster(String message, String name, String iconUrl, String serverName, boolean useMarkdown) { + this.message = message; + this.name = name + " (" + serverName + ")"; + this.useMarkdown = useMarkdown; + this.iconUrl = iconUrl; + } + + /** + * Posts a player sent message to Slack. + * + * @param message The message sent to Slack. + * @param player The player that sent the message. + * @param serverName The server the player is on. + * @param useMarkdown Use markdown formatting in the message. + */ + public BungeePoster(String message, ProxiedPlayer player, String serverName, boolean useMarkdown) { this.message = message; - this.icon = icon; + + name = player.getName() + " (" + serverName + ")"; + iconUrl = "https://cravatar.eu/helmhead/" + player.getUniqueId().toString() + "/128.png"; + this.useMarkdown = useMarkdown; } @Override public void run() { JsonObject json = new JsonObject(); - json.addProperty("text", name + ": " + message); + json.addProperty("text", message); json.addProperty("username", name); - if (icon == null) { - json.addProperty("icon_url", "https://cravatar.eu/helmhead/" + name + "/100.png"); - } else { - json.addProperty("icon_url", icon); - } + json.addProperty("icon_url", iconUrl);; + json.addProperty("mrkdwn", useMarkdown); try { HttpURLConnection webhookConnection = (HttpURLConnection) new URL(webhookUrl).openConnection(); webhookConnection.setRequestMethod("POST"); @@ -49,8 +82,11 @@ public void run() { String jsonStr = "payload=" + json.toString(); bufOut.write(jsonStr.getBytes("utf8")); bufOut.flush(); + bufOut.close(); } + int serverResponseCode = webhookConnection.getResponseCode(); webhookConnection.disconnect(); + webhookConnection = null; } catch (Exception ignored) { } } diff --git a/src/main/java/org/circuitsoft/slack/bukkit/SlackBukkit.java b/src/main/java/org/circuitsoft/slack/bukkit/SlackBukkit.java index b9316ef..fd034ca 100644 --- a/src/main/java/org/circuitsoft/slack/bukkit/SlackBukkit.java +++ b/src/main/java/org/circuitsoft/slack/bukkit/SlackBukkit.java @@ -3,6 +3,7 @@ import java.text.MessageFormat; import java.util.List; import java.util.UUID; +import java.util.logging.Level; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; @@ -17,6 +18,8 @@ import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.plugin.java.JavaPlugin; +import org.circuitsoft.slack.api.BukkitPoster; +import org.json.simple.JSONObject; public class SlackBukkit extends JavaPlugin implements Listener { @@ -42,38 +45,38 @@ public void onDisable() { @EventHandler(priority = EventPriority.MONITOR) public void onChat(AsyncPlayerChatEvent event) { - if (isVisible("slack.hide.chat", event.getPlayer())) { - send('"' + event.getMessage() + '"', event.getPlayer().getName()); + if (isVisible("slack.hide.chat", event.getPlayer().getUniqueId())) { + send(event.getMessage(), event.getPlayer(), false); } } @EventHandler(priority = EventPriority.MONITOR) public void onLogin(PlayerJoinEvent event) { if (isVisible("slack.hide.login", event.getPlayer().getUniqueId())) { - send("logged in", event.getPlayer().getName()); + send("_joined_", event.getPlayer(), true); } } @EventHandler(priority = EventPriority.MONITOR) public void onQuit(PlayerQuitEvent event) { - if (isVisible("slack.hide.logout", event.getPlayer())) { - send("logged out", event.getPlayer().getName()); + if (isVisible("slack.hide.logout", event.getPlayer().getUniqueId())) { + send("_quit_", event.getPlayer(), true); } } @EventHandler(priority = EventPriority.MONITOR) public void onCommand(PlayerCommandPreprocessEvent event) { - if (isAllowed(event.getMessage()) && isVisible("slack.hide.command", event.getPlayer()) && !event.getMessage().contains("/slack send")) { - send(event.getMessage(), event.getPlayer().getName()); + if (!getConfig().getBoolean("send-commands")) { + return; + } + String command = event.getMessage().split(" ")[0]; + if (isAllowed(command) && isVisible("slack.hide.command", event.getPlayer().getUniqueId()) && !event.getMessage().contains("/slack send")) { + send(event.getMessage(), event.getPlayer(), false); } } - - public void send(String message, String name) { - new SlackBukkitPoster(this, message, name, null).runTaskAsynchronously(this); - } - - public void send(String message, String name, String iconUrl) { - new SlackBukkitPoster(this, message, name, iconUrl).runTaskAsynchronously(this); + + private void send(String message, Player player, boolean useMarkdown) { + new BukkitPoster(message, player, useMarkdown).runTaskAsynchronously(this); } private boolean isAllowed(String command) { @@ -90,18 +93,10 @@ private void updateConfig(String version) { getConfig().set("version", version); saveConfig(); } - - private boolean isVisible(String permission, Player player) { - if (getConfig().getBoolean("use-perms")) { - return !player.hasPermission(permission); - } else { - return true; - } - } private boolean isVisible(String permission, UUID uuid) { if (getConfig().getBoolean("use-perms")) { - return !Bukkit.getServer().getPlayer(uuid).hasPermission(permission); + return !getServer().getPlayer(uuid).hasPermission(permission); } else { return true; } @@ -150,13 +145,17 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String senderName = sender.getName(); } sb.append(MessageFormat.format(" (sent by {0})", senderName)); - send(sb.toString(), args[1], args[2].equalsIgnoreCase("null") ? null : args[2]); + if (args[2].equalsIgnoreCase("null")) { + new BukkitPoster(sb.toString(), args[1], "https://cravatar.eu/helmhead/" + getServer().getPlayer(args[1]).getUniqueId() + "/128.png", false).runTaskAsynchronously(this); + } else { + new BukkitPoster(sb.toString(), args[1], args[2], false).runTaskAsynchronously(this); + } } } else { sender.sendMessage(ChatColor.DARK_RED + "You are not allowed to execute this command!"); } } else { - sender.sendMessage(ChatColor.GOLD + "/slack send - send a custom message to slack \n/slack reload - reload Slack's config"); + sender.sendMessage(ChatColor.GOLD + "/slack send - send a custom message to slack\n/slack reload - reload Slack's config"); } return true; } diff --git a/src/main/java/org/circuitsoft/slack/bukkit/SlackBukkitPoster.java b/src/main/java/org/circuitsoft/slack/bukkit/SlackBukkitPoster.java deleted file mode 100644 index 2e9da7d..0000000 --- a/src/main/java/org/circuitsoft/slack/bukkit/SlackBukkitPoster.java +++ /dev/null @@ -1,66 +0,0 @@ -package org.circuitsoft.slack.bukkit; - -import java.io.BufferedOutputStream; -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.logging.Level; -import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.scheduler.BukkitRunnable; -import org.json.simple.JSONObject; -import static org.circuitsoft.slack.bukkit.SlackBukkit.getWebhookUrl; - -/** - * Poster for Slack plugin's internal use. Do not use this. - */ -public class SlackBukkitPoster extends BukkitRunnable { - - private final JavaPlugin plugin; - private final String name; - private final String message; - private final String webhookUrl = getWebhookUrl(); - private final String iconUrl; - - public SlackBukkitPoster(JavaPlugin plugin, String message, String name, String iconUrl) { - this.plugin = plugin; - this.name = name; - this.message = message; - this.iconUrl = iconUrl; - } - - @Override - public void run() { - JSONObject json = new JSONObject(); - json.put("text", name + ": " + message); - json.put("username", name); - if (iconUrl == null) { - json.put("icon_url", "https://cravatar.eu/helmhead/" + name + "/100.png"); - } else { - json.put("icon_url", iconUrl); - } - String b = "payload=" + json.toJSONString(); - try { - HttpURLConnection webhookConnection = (HttpURLConnection) new URL(webhookUrl).openConnection(); - webhookConnection.setRequestMethod("POST"); - webhookConnection.setDoOutput(true); - try (BufferedOutputStream bufOut = new BufferedOutputStream(webhookConnection.getOutputStream())) { - bufOut.write(b.getBytes("utf8")); - bufOut.flush(); - } - webhookConnection.disconnect(); - int responseCode = webhookConnection.getResponseCode(); - String responseMessage = webhookConnection.getResponseMessage(); - if (plugin.getConfig().getBoolean("debug")) { - plugin.getLogger().log(Level.INFO, "{0} {1}", new Object[]{ - responseCode, - responseMessage - }); - } - } catch (MalformedURLException e) { - plugin.getLogger().log(Level.SEVERE, "URL is not valid: ", e); - } catch (IOException e) { - plugin.getLogger().log(Level.SEVERE, "IO exception: ", e); - } - } -} diff --git a/src/main/java/org/circuitsoft/slack/bungee/SlackBungee.java b/src/main/java/org/circuitsoft/slack/bungee/SlackBungee.java index e843160..64984ad 100644 --- a/src/main/java/org/circuitsoft/slack/bungee/SlackBungee.java +++ b/src/main/java/org/circuitsoft/slack/bungee/SlackBungee.java @@ -16,6 +16,7 @@ import net.md_5.bungee.config.YamlConfiguration; import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventPriority; +import org.circuitsoft.slack.api.BungeePoster; public class SlackBungee extends Plugin implements Listener { @@ -55,39 +56,38 @@ public void reloadConfig() { public void onChat(ChatEvent event) { ProxiedPlayer p = (ProxiedPlayer) event.getSender(); if (event.isCommand()) { - if (hasPermission(p, "slack.hide.command") && isOnBlacklist(event.getMessage())) { - send('"' + event.getMessage() + '"', p.getName() + " (" + p.getServer().getInfo().getName() + ")"); + if (!config.getBoolean("send-commands")) { + return; + } + if (hasPermission(p, "slack.hide.command") && isAllowed(event.getMessage())) { + send(event.getMessage(), p, p.getServer().getInfo().getName(), false); } } else if (hasPermission(p, "slack.hide.chat")) { - send('"' + event.getMessage() + '"', p.getName() + " (" + p.getServer().getInfo().getName() + ")"); + send(event.getMessage(), p, p.getServer().getInfo().getName(), false); } } @EventHandler(priority = EventPriority.HIGHEST) public void onJoin(ServerConnectedEvent event) { if (hasPermission(event.getPlayer(), "slack.hide.login")) { - send("logged in", event.getPlayer().getName() + " (" + event.getServer().getInfo().getName() + ")"); + send("_joined_", event.getPlayer(), event.getServer().getInfo().getName(), true); } } @EventHandler(priority = EventPriority.HIGHEST) public void onQuit(ServerDisconnectEvent event) { if (hasPermission(event.getPlayer(), "slack.hide.logout")) { - send("logged out", event.getPlayer().getName() + " (" + event.getTarget().getName() + ")"); + send("_quit_", event.getPlayer(), event.getTarget().getName(), true); } } - public void send(String message, String name) { - send(message, name, null); - } - - public void send(String message, String name, String iconUrl) { - getProxy().getScheduler().runAsync(this, new SlackBungeePoster(this, config, message, name, iconUrl)); + private void send(String message, ProxiedPlayer player, String serverName, boolean useMarkdown) { + getProxy().getScheduler().runAsync(this, new BungeePoster(message, player, serverName, useMarkdown)); } - private boolean isOnBlacklist(String name) { + private boolean isAllowed(String command) { if (config.getBoolean("use-blacklist")) { - return !blacklist.contains(name); + return !blacklist.contains(command); } else { return true; } diff --git a/src/main/java/org/circuitsoft/slack/bungee/SlackBungeeCommand.java b/src/main/java/org/circuitsoft/slack/bungee/SlackBungeeCommand.java index b9778dd..3814a8f 100644 --- a/src/main/java/org/circuitsoft/slack/bungee/SlackBungeeCommand.java +++ b/src/main/java/org/circuitsoft/slack/bungee/SlackBungeeCommand.java @@ -12,7 +12,6 @@ public class SlackBungeeCommand extends Command { public static final BaseComponent[] helpMsg = new ComponentBuilder("/slack send - send a custom message to slack\n/slack reload - reload Slack's config").color(ChatColor.GOLD).create(); private static final BaseComponent[] sendHelpMsg = new ComponentBuilder("/slack send ").color(ChatColor.GOLD).create(); - private static final BaseComponent[] reloadHelpMsg = new ComponentBuilder("/slack reload - reload Slack's config").color(ChatColor.GOLD).create(); private static final BaseComponent[] reloadMsg = new ComponentBuilder("Slack has been reloaded.").color(ChatColor.GREEN).create(); private static final BaseComponent[] noPermMsg = new ComponentBuilder("You are not allowed to execute this command!").color(ChatColor.DARK_RED).create(); @@ -54,7 +53,11 @@ public void execute(CommandSender sender, String[] args) { } sb.append(args[i]); } - plugin.getProxy().getScheduler().runAsync(plugin, new BungeePoster(sb.toString(), args[1], args[2].equalsIgnoreCase("null") ? null : args[2])); + if (args[2].equalsIgnoreCase("null")) { + plugin.getProxy().getScheduler().runAsync(plugin, new BungeePoster(sb.toString(), args[1], "https://cravatar.eu/helmhead/" + plugin.getProxy().getPlayer(args[1]).getUniqueId() + "/128.png", false)); + } else { + plugin.getProxy().getScheduler().runAsync(plugin, new BungeePoster(sb.toString(), args[1], args[2], false)); + } } } else { sender.sendMessage(noPermMsg); diff --git a/src/main/java/org/circuitsoft/slack/bungee/SlackBungeePoster.java b/src/main/java/org/circuitsoft/slack/bungee/SlackBungeePoster.java deleted file mode 100644 index 1dae7e6..0000000 --- a/src/main/java/org/circuitsoft/slack/bungee/SlackBungeePoster.java +++ /dev/null @@ -1,71 +0,0 @@ -package org.circuitsoft.slack.bungee; - -import java.io.BufferedOutputStream; -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.logging.Level; - -import com.google.gson.JsonObject; -import net.md_5.bungee.api.plugin.Plugin; -import net.md_5.bungee.config.Configuration; - -import static org.circuitsoft.slack.bungee.SlackBungee.getWebhookUrl; - -/** - * Poster for Slack plugin's internal use. Do not use this. - */ -public class SlackBungeePoster implements Runnable { - - private final Plugin plugin; - private final Configuration config; - private final String name; - private final String message; - private final String webhookUrl = getWebhookUrl(); - private final String iconUrl; - - public SlackBungeePoster(Plugin plugin, Configuration config, String message, String name, String iconUrl) { - this.plugin = plugin; - this.config = config; - this.name = name; - this.message = message; - this.iconUrl = iconUrl; - } - - @Override - public void run() { - JsonObject json = new JsonObject(); - json.addProperty("text", name + ": " + message); - json.addProperty("username", name); - if (iconUrl == null) { - json.addProperty("icon_url", "https://cravatar.eu/helmhead/" + name + "/100.png"); - } else { - json.addProperty("icon_url", iconUrl); - } - try { - HttpURLConnection webhookConnection = (HttpURLConnection) new URL(webhookUrl).openConnection(); - webhookConnection.setRequestMethod("POST"); - webhookConnection.setDoOutput(true); - try (BufferedOutputStream bufOut = new BufferedOutputStream(webhookConnection.getOutputStream())) { - String jsonStr = "payload=" + json.toString(); - bufOut.write(jsonStr.getBytes(StandardCharsets.UTF_8)); - bufOut.flush(); - } - webhookConnection.disconnect(); - int responseCode = webhookConnection.getResponseCode(); - String responseMessage = webhookConnection.getResponseMessage(); - if (config.getBoolean("debug")) { - plugin.getLogger().log(Level.INFO, "{0} {1}", new Object[]{ - responseCode, - responseMessage - }); - } - } catch (MalformedURLException e) { - plugin.getLogger().log(Level.SEVERE, "URL is not valid: ", e); - } catch (IOException e) { - plugin.getLogger().log(Level.SEVERE, "IO exception: ", e); - } - } -} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index a24ffcf..3080240 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,10 +1,12 @@ #The version. Do not touch this. -version: 1.4.4 +version: 1.5.0 #The incoming webhook URL you got from Slack. webhook: https://hooks.slack.com/services/ #Show HTTP response codes in console debug: false -#Causes a bit of lag but you can hvae certain players not post to Slack +#Send player commands to Slack +send-commands: true +#Causes a bit of lag but you can have certain players not post to Slack use-perms: false #Causes lag but you can block commands from being shown to Slack use-blacklist: false diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0608424..e135f2e 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,8 +1,8 @@ name: Slack -version: 1.4.4 +version: 1.5.0 description: Link your server to Slack! author: CircuitSoft -authors: [Janmm14, LaxWasHere, it3d] +authors: [Janmm14, LaxWasHere, it3d, Davy] website: https://circuitsoft.us main: org.circuitsoft.slack.bukkit.SlackBukkit