WebChat enhancement and fixes

Added Spam Message config
Internal server now replies a spam message
Fixed getting webchat-interval to get int rather than double then cast
Fixed holding down "enter" on the empty chat line would spam chat attempts
Fixed so jsonip.appspot query is only done once
This commit is contained in:
Jason Booth 2011-03-15 19:17:58 -05:00
parent 07cb3ad1b3
commit 2cef9731ed
6 changed files with 48 additions and 30 deletions

View File

@ -80,6 +80,7 @@ web:
joinmessage: "%playername% joined"
quitmessage: "%playername% quit"
spammessage: "You may only chat once every %interval% seconds."
defaultworld: world
worlds:

View File

@ -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<SendMessageHandler.Message>() {
@Override
public void triggered(Message t) {

View File

@ -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<Message> onMessageReceived = new Event<SendMessageHandler.Message>();
public int maximumMessageInterval = 1000;
public String spamMessage = "\"You may only chat once every %interval% seconds.\"";
private HashMap<String, WebUser> disallowedUsers = new HashMap<String, WebUser>();
private LinkedList<WebUser> disallowedUserQueue = new LinkedList<WebUser>();
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;
}
}

View File

@ -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
}

View File

@ -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);

View File

@ -1,10 +1,11 @@
<?php
$msginterval = 5; //In seconds - add this to dynmap web config??
session_start();
$config = json_decode(file_get_contents('dynmap_config.json'), true);
$msginterval = $config['webchat-interval'];
if($_SERVER['REQUEST_METHOD'] == 'POST' && $_SESSION['lastchat'] < time())
{
$config = json_decode(file_get_contents('dynmap_config.json'), true);
$micro = explode(' ', microtime());
$timestamp = $micro[1].round($micro[0]*1000);
@ -25,8 +26,7 @@ if($_SERVER['REQUEST_METHOD'] == 'POST' && $_SESSION['lastchat'] < time())
}
elseif($_SERVER['REQUEST_METHOD'] == 'POST' && $_SESSION['lastchat'] > time())
{
echo json_encode('You may only chat once every '.$msginterval.' seconds.');
echo json_encode(str_replace('%interval%', $msginterval, $config['spammessage']));
}
?>