diff --git a/configuration.txt b/configuration.txt index 18434cf4..17a1b2b6 100644 --- a/configuration.txt +++ b/configuration.txt @@ -80,6 +80,7 @@ web: joinmessage: "%playername% joined" quitmessage: "%playername% quit" + spammessage: "You may only chat once every %interval% seconds." defaultworld: world worlds: diff --git a/src/main/java/org/dynmap/DynmapPlugin.java b/src/main/java/org/dynmap/DynmapPlugin.java index f6f790b9..f233f264 100644 --- a/src/main/java/org/dynmap/DynmapPlugin.java +++ b/src/main/java/org/dynmap/DynmapPlugin.java @@ -145,7 +145,8 @@ public class DynmapPlugin extends JavaPlugin { if (configuration.getNode("web").getBoolean("allowwebchat", false)) { SendMessageHandler messageHandler = new SendMessageHandler() {{ - maximumMessageInterval = (int)(configuration.getNode("web").getDouble("webchat-interval", 1.0) * 1000); + maximumMessageInterval = (configuration.getNode("web").getInt("webchat-interval", 1) * 1000); + spamMessage = "\""+configuration.getNode("web").getString("spammessage", "You may only chat once every %interval% seconds.")+"\""; onMessageReceived.addListener(new Listener() { @Override public void triggered(Message t) { diff --git a/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java b/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java index 2825bfaf..afca89a4 100644 --- a/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java +++ b/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java @@ -1,6 +1,8 @@ package org.dynmap.web.handlers; +import java.io.BufferedOutputStream; import java.io.InputStreamReader; +import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; @@ -23,6 +25,7 @@ public class SendMessageHandler implements HttpHandler { public Event onMessageReceived = new Event(); public int maximumMessageInterval = 1000; + public String spamMessage = "\"You may only chat once every %interval% seconds.\""; private HashMap disallowedUsers = new HashMap(); private LinkedList disallowedUserQueue = new LinkedList(); private Object disallowedUsersLock = new Object(); @@ -65,9 +68,21 @@ public class SendMessageHandler implements HttpHandler { disallowedUsers.put(user.name, user); disallowedUserQueue.add(user); } else { - response.fields.put(HttpField.ContentLength, "0"); - response.status = HttpStatus.Forbidden; - response.getBody(); + spamMessage = spamMessage.replaceAll("%interval%", Integer.toString(maximumMessageInterval/1000)); + byte[] stringBytes = spamMessage.getBytes(); + String dateStr = new Date().toString(); + + response.fields.put("Date", dateStr); + response.fields.put("Content-Type", "text/plain"); + response.fields.put("Expires", "Thu, 01 Dec 1994 16:00:00 GMT"); + response.fields.put("Last-modified", dateStr); + response.fields.put("Content-Length", Integer.toString(stringBytes.length)); + response.status = HttpStatus.OK; + + BufferedOutputStream out = null; + out = new BufferedOutputStream(response.getBody()); + out.write(stringBytes); + out.flush(); return; } } diff --git a/web/js/chat.js b/web/js/chat.js index 49bc792d..13ed1e00 100644 --- a/web/js/chat.js +++ b/web/js/chat.js @@ -1,23 +1,21 @@ +var ip; +$.ajax({ + type: "GET", + url: "http://jsonip.appspot.com/?callback=?", + dataType: "jsonp", + success: function(getip) { ip = getip.ip; } + }); function sendChat(me, message) { - var ip; + var data = '{"name":"'+ip+'","message":"'+message+'"}'; $.ajax({ - type: "GET", - url: "http://jsonip.appspot.com/?callback=?", - dataType: "jsonp", - success: function(getip) { - var data = '{"name":"'+getip.ip+'","message":"'+message+'"}'; - $.ajax({ - type: 'POST', - url: 'up/sendmessage', - data: data, - dataType: 'json', - success: function(response) { - //handle response - if(response) - me.onPlayerChat('', response); - } - }); + type: 'POST', + url: 'up/sendmessage', + data: data, + dataType: 'json', + success: function(response) { + //handle response + if(response) + me.onPlayerChat('', response); } }); -} -//curl -d '{"name":"Nickname","message":"Hello"}' http://localhost:8123/up/sendmessage +} \ No newline at end of file diff --git a/web/js/map.js b/web/js/map.js index 67dd0cc3..5125a179 100644 --- a/web/js/map.js +++ b/web/js/map.js @@ -213,8 +213,11 @@ DynMap.prototype = { .keydown(function(event) { if (event.keyCode == '13') { event.preventDefault(); - sendChat(me, chatinput.val()); - chatinput.val(''); + if(chatinput.val() != '') + { + sendChat(me, chatinput.val()); + chatinput.val(''); + } } }) .appendTo(chat); diff --git a/web/standalone/sendmessage.php b/web/standalone/sendmessage.php index f36b8961..16361100 100644 --- a/web/standalone/sendmessage.php +++ b/web/standalone/sendmessage.php @@ -1,10 +1,11 @@ time()) { - echo json_encode('You may only chat once every '.$msginterval.' seconds.'); + echo json_encode(str_replace('%interval%', $msginterval, $config['spammessage'])); } - ?> \ No newline at end of file