Skip to content

Commit

Permalink
Markdown, implement API, use-commands option
Browse files Browse the repository at this point in the history
  • Loading branch information
mastercoms committed Feb 17, 2015
2 parents d47afdb + 8b4af9d commit 8f6a596
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 211 deletions.
Binary file modified Slack.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>us.circuitsoft.slack</groupId>
<artifactId>Slack</artifactId>
<version>1.4.4</version>
<version>1.5.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
47 changes: 33 additions & 14 deletions src/main/java/org/circuitsoft/slack/api/BukkitPoster.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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) {
}
}
Expand Down
66 changes: 51 additions & 15 deletions src/main/java/org/circuitsoft/slack/api/BungeePoster.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,75 @@
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;

/**
* 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");
Expand All @@ -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) {
}
}
Expand Down
49 changes: 24 additions & 25 deletions src/main/java/org/circuitsoft/slack/bukkit/SlackBukkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 <username> <image URL or null for username's skin> <message> - send a custom message to slack \n/slack reload - reload Slack's config");
sender.sendMessage(ChatColor.GOLD + "/slack send <username> <image URL or null for username's skin> <message> - send a custom message to slack\n/slack reload - reload Slack's config");
}
return true;
}
Expand Down
66 changes: 0 additions & 66 deletions src/main/java/org/circuitsoft/slack/bukkit/SlackBukkitPoster.java

This file was deleted.

Loading

0 comments on commit 8f6a596

Please sign in to comment.