Start switching chat flood system

This commit is contained in:
calzoneman 2013-11-19 15:14:40 -06:00
parent 21bb2b9a4e
commit ee9b19b0ff
8 changed files with 108 additions and 4 deletions

View file

@ -84,14 +84,19 @@ var Channel = function(name) {
self.opts = {
allow_voteskip: true,
voteskip_ratio: 0.5,
afk_timeout: 180,
afk_timeout: 600,
pagetitle: self.name,
maxlength: 0,
externalcss: "",
externaljs: "",
chat_antiflood: false,
chat_antiflood_params: {
burst: 4,
sustained: 1,
cooldown: 4
},
show_public: false,
enable_link_regex: true
enable_link_regex: true,
};
self.filters = [
new Filter("monospace", "`([^`]+)`", "g", "<code>$1</code>"),
@ -2099,12 +2104,27 @@ Channel.prototype.tryUpdateOptions = function(user, data) {
if(key in adminonly && user.rank < 3) {
continue;
}
if (key === "chat_antiflood_params") {
var b = parseInt(data[key].burst);
if (isNaN(b) || b < 0)
b = 1;
var s = parseFloat(data[key].sustained);
if (isNaN(s) || s <= 0)
s = 1;
var c = parseFloat(data[key].cooldown);
if (isNaN(c) || c < 0)
c = 0;
this.opts.chat_antiflood_params.burst = b;
this.opts.chat_antiflood_params.sustained = s;
this.opts.chat_antiflood_params.cooldown = c;
continue;
}
this.opts[key] = data[key];
if(key === "afk_timeout" && this.opts[key] != data[key]) {
this.users.forEach(function (u) {
u.autoAFK();
});
}
this.opts[key] = data[key];
}
}
@ -2222,7 +2242,16 @@ Channel.prototype.tryChat = function(user, data) {
if(msg.length > 240) {
msg = msg.substring(0, 240);
}
if(this.opts.chat_antiflood && user.noflood("chat", 2.0)) {
if (this.opts.chat_antiflood &&
user.chatLimiter.throttle(this.opts.chat_antiflood_params)) {
user.socket.emit("chatCooldown", 1000/this.opts.chat_antiflood_params.sustained);
/*
user.socket.emit("noflood", {
action: "chat",
msg: "sending messages too quickly!"
});
*/
return;
}

View file

@ -36,6 +36,7 @@ var User = function (socket) {
this.throttle = {};
this.flooded = {};
this.queueLimiter = $util.newRateLimiter();
this.chatLimiter = $util.newRateLimiter();
this.profile = {
image: "",
text: ""

View file

@ -127,6 +127,43 @@ Callbacks = {
scrollChat();
},
chatCooldown: function (time) {
time = time + 200;
/*
var msg = $("#chat-cooldown-msg");
if (msg.length > 0) {
var timer = msg.data("timer");
if (timer) {
clearTimeout(timer);
}
} else {
msg = $("<div/>")
.addClass("server-msg-disconnect")
.attr("id", "chat-cooldown-msg")
.text("Chat rate limit exceeded, please wait before sending another message")
.appendTo($("#messagebuffer"));
}
if (SCROLLCHAT) {
scrollChat();
}
*/
$("#chatline").css("color", "#ff0000");
if (CHATTHROTTLE && $("#chatline").data("throttle_timer")) {
clearTimeout($("#chatline").data("throttle_timer"));
}
CHATTHROTTLE = true;
$("#chatline").data("throttle_timer", setTimeout(function () {
CHATTHROTTLE = false;
$("#chatline").css("color", "");
/*
msg.remove();
if (SCROLLCHAT) {
scrollChat();
}
*/
}, time));
},
channelNotRegistered: function() {
var div = $("<div/>").addClass("alert alert-info")
.attr("id", "chregnotice")

View file

@ -67,6 +67,10 @@
else {
len = parseInt(hms[0]);
}
var sus = parseFloat($("#opt_chat_antiflood_sustained").val()) || 0;
if (sus <= 0) {
sus = 1;
}
socket.emit("setOptions", {
allow_voteskip: $("#opt_allow_voteskip").prop("checked"),
voteskip_ratio: parseFloat($("#opt_voteskip_ratio").val()),
@ -75,6 +79,11 @@
externalcss: $("#opt_externalcss").val(),
externaljs: $("#opt_externaljs").val(),
chat_antiflood: $("#opt_chat_antiflood").prop("checked"),
chat_antiflood_params: {
burst: $("#opt_chat_antiflood_burst").val(),
sustained: $("#opt_chat_antiflood_sustained").val(),
cooldown: $("#opt_chat_antiflood_cooldown").val()
},
show_public: $("#opt_show_public").prop("checked"),
enable_link_regex: $("#opt_enable_link_regex").prop("checked"),
afk_timeout: parseInt($("#opt_afktimeout").val())

View file

@ -54,6 +54,7 @@ var socket = {
var IGNORED = [];
var CHATHIST = [];
var CHATHISTIDX = 0;
var CHATTHROTTLE = false;
var SCROLLCHAT = true;
var LASTCHATNAME = "";
var LASTCHATTIME = 0;

View file

@ -148,6 +148,9 @@ $("#messagebuffer").mouseleave(function() { SCROLLCHAT = true; });
$("#chatline").keydown(function(ev) {
if(ev.keyCode == 13) {
if (CHATTHROTTLE) {
return;
}
var msg = $("#chatline").val();
if(msg.trim()) {
var meta = {};

View file

@ -1010,6 +1010,9 @@ function handleModPermissions() {
$("#opt_externaljs").val(CHANNEL.opts.externaljs);
$("#opt_externaljs").attr("disabled", CLIENT.rank < 3);
$("#opt_chat_antiflood").prop("checked", CHANNEL.opts.chat_antiflood);
$("#opt_chat_antiflood_burst").val(CHANNEL.opts.chat_antiflood_params.burst);
$("#opt_chat_antiflood_sustained").val(CHANNEL.opts.chat_antiflood_params.sustained);
$("#opt_chat_antiflood_cooldown").val(CHANNEL.opts.chat_antiflood_params.cooldown);
$("#opt_show_public").prop("checked", CHANNEL.opts.show_public);
$("#opt_show_public").attr("disabled", CLIENT.rank < 3);
$("#opt_enable_link_regex").prop("checked", CHANNEL.opts.enable_link_regex);

View file

@ -29,6 +29,27 @@
<input type="checkbox" id="opt_chat_antiflood">
</div>
</div>
<!-- chat flood burst -->
<div class="control-group">
<label class="control-label" for="opt_chat_antiflood_burst"># of messages allowed before throttling</label>
<div class="controls">
<input type="text" id="opt_chat_antiflood_burst">
</div>
</div>
<!-- chat flood sustained -->
<div class="control-group">
<label class="control-label" for="opt_chat_antiflood_sustained"># of messages allowed per second (sustained)</label>
<div class="controls">
<input type="text" id="opt_chat_antiflood_sustained">
</div>
</div>
<!-- chat flood cooldown -->
<div class="control-group">
<label class="control-label" for="opt_chat_antiflood_cooldown">Cooldown time after being throttled (seconds)</label>
<div class="controls">
<input type="text" id="opt_chat_antiflood_cooldown">
</div>
</div>
<!-- convert URLs to links -->
<div class="control-group">
<label class="control-label" for="opt_enable_link_regex">Convert URLs in chat to links</label>