Add MOTD support
This commit is contained in:
parent
52fa187d91
commit
a8c43fb28a
6
pom.xml
6
pom.xml
@ -18,6 +18,12 @@
|
|||||||
<type>jar</type>
|
<type>jar</type>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
<scope>compile</scope>
|
||||||
|
<version>20090211</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
|
@ -17,18 +17,25 @@ package us.camin;
|
|||||||
along with Caminus. If not, see <http://www.gnu.org/licenses/>.
|
along with Caminus. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.player.PlayerListener;
|
import org.bukkit.event.player.PlayerListener;
|
||||||
import org.bukkit.event.player.PlayerLoginEvent;
|
import org.bukkit.event.player.PlayerLoginEvent;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.json.JSONException;
|
||||||
|
|
||||||
public class JoinListener extends PlayerListener {
|
public class JoinListener extends PlayerListener {
|
||||||
Logger log = Logger.getLogger("Caminus.Join");
|
Logger log = Logger.getLogger("Caminus.Join");
|
||||||
private String m_url;
|
private String m_url;
|
||||||
@ -51,17 +58,57 @@ public class JoinListener extends PlayerListener {
|
|||||||
public void onPlayerLogin(PlayerLoginEvent event) {
|
public void onPlayerLogin(PlayerLoginEvent event) {
|
||||||
Player p = event.getPlayer();
|
Player p = event.getPlayer();
|
||||||
try {
|
try {
|
||||||
if (!isUserAuthed(p.getName()))
|
if (!isUserAuthed(p.getName())) {
|
||||||
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "An active camin.us account is required.");
|
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "An active camin.us account is required.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Auth URL is invalid!");
|
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Auth URL is invalid!");
|
||||||
|
return;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Camin.us auth server seems down.");
|
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 {
|
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);
|
log.info("Authing "+user+" against "+authServer);
|
||||||
HttpURLConnection conn = (HttpURLConnection)authServer.openConnection();
|
HttpURLConnection conn = (HttpURLConnection)authServer.openConnection();
|
||||||
int code = conn.getResponseCode();
|
int code = conn.getResponseCode();
|
||||||
|
@ -40,7 +40,7 @@ public class Plugin extends JavaPlugin {
|
|||||||
PluginManager pm = this.getServer().getPluginManager();
|
PluginManager pm = this.getServer().getPluginManager();
|
||||||
m_listener = new JoinListener();
|
m_listener = new JoinListener();
|
||||||
Configuration conf = getConfig();
|
Configuration conf = getConfig();
|
||||||
conf.addDefault("url", "http://camin.us/api/validate/");
|
conf.addDefault("url", "http://camin.us/api/");
|
||||||
String url = conf.getString("url");
|
String url = conf.getString("url");
|
||||||
m_listener.setURL(url);
|
m_listener.setURL(url);
|
||||||
pm.registerEvent(Event.Type.PLAYER_LOGIN, m_listener, Event.Priority.Normal, this);
|
pm.registerEvent(Event.Type.PLAYER_LOGIN, m_listener, Event.Priority.Normal, this);
|
||||||
|
Loading…
Reference in New Issue
Block a user