Changed quote spam protection model [#72]

This commit is contained in:
reality 2012-12-19 21:03:03 +00:00
parent 6a28327d5c
commit 950ba882b8
4 changed files with 96 additions and 10 deletions

View File

@ -1,3 +1,4 @@
{
"dbKeys": [ "quoteArrs" ]
"dbKeys": [ "quoteArrs" ],
"rmLimit": 10
}

View File

@ -3,6 +3,9 @@ var quotes = function(dbot) {
var quotes = dbot.db.quoteArrs;
var addStack = [];
var rmAllowed = true;
dbot.sessionData.rmCache = [];
var rmCache = dbot.sessionData.rmCache;
var rmTimer;
// Retrieve a random quote from a given category, interpolating any quote
// references (~~QUOTE CATEGORY~~) within it
@ -32,12 +35,69 @@ var quotes = function(dbot) {
return quoteString;
};
var resetRemoveTimer = function(event, key, quote) {
rmAllowed = false;
dbot.timers.addOnceTimer(5000, function() {
rmAllowed = true;
});
rmCache.push({'key': key, 'quote': quote});
dbot.timers.clearTimeout(rmTimer);
if(rmCache.length < dbot.config.quotes.rmLimit) {
rmTimer = dbot.timers.addOnceTimer(600000, function() {
rmCache.length = 0; // lol what
});
} else {
for(var i=0;i<dbot.config.admins.length;i++) {
dbot.say(event.server, dbot.config.admins[i],
dbot.t('rm_cache_limit'));
}
}
};
var commands = {
// Alternative syntax to ~q
'~': function(event) {
commands['~q'](event);
},
'~rmstatus': function(event) {
var rmCacheCount = rmCache.length;
if(rmCacheCount < dbot.config.quotes.rmLimit) {
event.reply(dbot.t('quote_cache_auto_remove',
{ 'count': rmCacheCount }));
} else {
event.reply(dbot.t('quote_cache_manual_remove',
{ 'count': rmCacheCount }));
}
},
'~rmconfirm': function(event) {
if(dbot.config.admins.include(event.user)) {
var rmCacheCount = rmCache.length;
rmCache.length = 0;
event.reply(dbot.t('quote_cache_cleared',
{ 'count': rmCacheCount }));
}
},
'~rmdeny': function(event) {
if(dbot.config.admins.include(event.user)) {
var rmCacheCount = rmCache.length;
for(var i=0;i<rmCacheCount;i++) {
if(!quotes.hasOwnProperty(rmCache[i].key)) {
quotes[rmCache[i].key] = [];
}
quotes[rmCache[i].key].push(rmCache[i].quote);
}
rmCache.length = 0;
event.reply(dbot.t('quote_cache_reinstated',
{ 'count': rmCacheCount }));
}
},
// Retrieve quote from a category in the database.
'~q': function(event) {
var key = event.input[1].trim().toLowerCase();
@ -102,7 +162,8 @@ var quotes = function(dbot) {
if(quotes[key].length === 0) {
delete quotes[key];
}
rmAllowed = false;
resetRemoveTimer(event, key, quote);
event.reply(dbot.t('removed_from', {'quote': quote, 'category': key}));
} else {
event.reply(dbot.t('locked_category', {'category': q[1]}));
@ -124,11 +185,14 @@ var quotes = function(dbot) {
if(!dbot.db.locks.include(key)) {
var category = quotes[key];
var index = category.indexOf(quote);
var quote = category[index];
if(index !== -1) {
category.splice(index, 1);
if(category.length === 0) {
delete quotes[key];
}
resetRemoveTimer(event, key, quote);
event.reply(dbot.t('removed_from', {'category': key, 'quote': quote}));
} else {
event.reply(dbot.t('q_not_exist_under', {'category': key, 'quote': quote}));
@ -209,12 +273,6 @@ var quotes = function(dbot) {
'ignorable': true,
'commands': commands,
'onLoad': function() {
dbot.timers.addTimer(1000 * 60 * 3, function() {
rmAllowed = true;
});
},
'listener': function(event) {
// Reality Once listener
if((dbot.db.ignores.hasOwnProperty(event) &&

View File

@ -106,5 +106,20 @@
"spanish" : "{category} ({needle}): '{quote}' [{matches} resultados]",
"na'vi": "{category} ({needle}): '{quote}' [kum a{matches}]",
"welsh": "{category} ({needle}): '{quote}' [{matches} canlyniad]"
},
"quote_cache_auto_remove": {
"english": "There are {count} quotes in the removal cache, which will be automatically cleared."
},
"quote_cache_manual_remove": {
"english": "There are {count} quotes in the removal cache, which must be manually cleared."
},
"quote_cache_cleared": {
"english": "{count} quotes cleared from the removal cache."
},
"quote_cache_reinstated": {
"english": "{count} quotes reinstated from the removal cache."
},
"rm_cache_limit": {
"english": "Attention: Too many quotes removed, rmCache must be cleared or reinstated manually with ~rmconfirm or ~rmdeny."
}
}

View File

@ -4,11 +4,15 @@ var timers = function() {
return {
'addTimer': function(interval, callback) { // Because who puts the callback first. Really.
timers.push(setInterval(callback, interval));
var timer = setInterval(callback, interval);
timers.push(timer);
return timer;
},
'addOnceTimer': function(delay, callback) { // Because who seriously puts the callback first here too?
timeouts.push(setTimeout(callback, delay));
var timeout = setTimeout(callback, delay);
timeouts.push(timeout);
return timeout;
},
'clearTimers': function() {
@ -18,6 +22,14 @@ var timers = function() {
for(var i;i<timeouts.length;i++) {
clearTimeout(timeouts[i]);
}
},
'clearTimer': function(id) {
clearTimer(id);
},
'clearTimeout': function(id) {
clearTimeout(id);
}
};
};