diff --git a/pom.xml b/pom.xml index 020e6d2..6c8c579 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,12 @@ jar compile + + org.json + json + compile + 20090211 + junit junit diff --git a/src/main/java/us/camin/JoinListener.java b/src/main/java/us/camin/JoinListener.java index 85d3622..4431c83 100644 --- a/src/main/java/us/camin/JoinListener.java +++ b/src/main/java/us/camin/JoinListener.java @@ -17,18 +17,25 @@ package us.camin; along with Caminus. If not, see . */ + import java.io.BufferedReader; +import java.io.BufferedInputStream; import java.io.InputStreamReader; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.HttpURLConnection; import java.util.logging.Logger; +import java.util.Scanner; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerListener; import org.bukkit.event.player.PlayerLoginEvent; +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONException; + public class JoinListener extends PlayerListener { Logger log = Logger.getLogger("Caminus.Join"); private String m_url; @@ -51,17 +58,57 @@ public class JoinListener extends PlayerListener { public void onPlayerLogin(PlayerLoginEvent event) { Player p = event.getPlayer(); try { - if (!isUserAuthed(p.getName())) + if (!isUserAuthed(p.getName())) { event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "An active camin.us account is required."); + return; + } } catch (MalformedURLException e) { event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Auth URL is invalid!"); + return; } catch (IOException e) { event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Camin.us auth server seems down."); + return; + } + String[] motd = null; + try { + motd = fetchMOTD(p.getName()); + } catch (MalformedURLException e) { + p.chat("Could not fetch MOTD: Bad URL"); + } catch (IOException e) { + p.chat("Could not fetch MOTD: Communication error"); + } catch (JSONException e) { + p.chat("Could not fetch MOTD: Bad JSON"); + } + if (motd != null) { + for(String msg : motd) { + p.chat(msg); + } } } + public String[] fetchMOTD(String user) throws IOException, MalformedURLException, JSONException { + URL motdService = new URL(m_url+"motd/"+user); + HttpURLConnection conn = (HttpURLConnection)motdService.openConnection(); + BufferedInputStream in = new BufferedInputStream(conn.getInputStream()); + String jsonStr; + // Stupid scanner trick. \A means "beginning of input boundary". + try { + jsonStr = new java.util.Scanner(in).useDelimiter("\\A").next(); + } catch (java.util.NoSuchElementException e) { + jsonStr = ""; + } + in.close(); + JSONObject jsonObj = new JSONObject(jsonStr); + JSONArray motd = jsonObj.getJSONArray("motd"); + String[] ret = new String[motd.length()]; + for (int i = 0;i