Add MOTD support

This commit is contained in:
Trever Fischer 2012-02-27 10:20:36 -05:00
parent 52fa187d91
commit a8c43fb28a
3 changed files with 56 additions and 3 deletions

View File

@ -18,6 +18,12 @@
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<scope>compile</scope>
<version>20090211</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -17,18 +17,25 @@ package us.camin;
along with Caminus. If not, see <http://www.gnu.org/licenses/>.
*/
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<motd.length();i++) {
ret[i] = motd.optString(i);
}
return ret;
}
public boolean isUserAuthed(String user) throws IOException, MalformedURLException {
URL authServer = new URL(m_url+user);
URL authServer = new URL(m_url+"validate/"+user);
log.info("Authing "+user+" against "+authServer);
HttpURLConnection conn = (HttpURLConnection)authServer.openConnection();
int code = conn.getResponseCode();

View File

@ -40,7 +40,7 @@ public class Plugin extends JavaPlugin {
PluginManager pm = this.getServer().getPluginManager();
m_listener = new JoinListener();
Configuration conf = getConfig();
conf.addDefault("url", "http://camin.us/api/validate/");
conf.addDefault("url", "http://camin.us/api/");
String url = conf.getString("url");
m_listener.setURL(url);
pm.registerEvent(Event.Type.PLAYER_LOGIN, m_listener, Event.Priority.Normal, this);