From 0448a3de73d74c0abd73dcd0af980a4d3834d79f Mon Sep 17 00:00:00 2001 From: elexis Date: Mon, 21 Mar 2016 14:29:41 +0000 Subject: [PATCH] Lobby cleanup. Make sense of the spammonitor by using an object instead of an array and introducing a global constant instead of using magic numbers. This was SVN commit r17930. --- binaries/data/mods/public/gui/lobby/lobby.js | 35 ++++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/binaries/data/mods/public/gui/lobby/lobby.js b/binaries/data/mods/public/gui/lobby/lobby.js index 8f55b0ef85..d92d9ebdb8 100644 --- a/binaries/data/mods/public/gui/lobby/lobby.js +++ b/binaries/data/mods/public/gui/lobby/lobby.js @@ -13,6 +13,11 @@ const g_MapTypes = prepareForDropdown(g_Settings ? g_Settings.MapTypes : undefin */ const g_ShowTimestamp = Engine.ConfigDB_GetValue("user", "lobby.chattimestamp") == "true"; +/** + * Mute clients who exceed the rate of 1 message per second for this time + */ +const g_SpamBlockTimeframe = 5; + /** * Mute spammers for this time. */ @@ -918,9 +923,13 @@ function updateSpamMonitor(msg) return; if (g_SpamMonitor[msg.from]) - ++g_SpamMonitor[msg.from][0]; + ++g_SpamMonitor[msg.from].count; else - g_SpamMonitor[msg.from] = [1, Math.floor(Date.now() / 1000), 0]; + g_SpamMonitor[msg.from] = { + "count": 1, + "lastSend": Math.floor(Date.now() / 1000), + "lastBlock": 0 + }; } /** @@ -938,26 +947,32 @@ function isSpam(text, from) // Initialize if not already in the database. if (!g_SpamMonitor[from]) - g_SpamMonitor[from] = [1, time, 0]; + g_SpamMonitor[from] = { + "count": 1, + "lastSend": time, + "lastBlock": 0 + }; // Block blank lines. if (!text.trim()) return true; // Block users who are still within their spam block period. - if (g_SpamMonitor[from][2] + g_SpamBlockDuration >= time) + if (g_SpamMonitor[from].lastBlock + g_SpamBlockDuration >= time) return true; // Block users who exceed the rate of 1 message per second for five seconds and are not already blocked. - if (g_SpamMonitor[from][0] == 6) + if (g_SpamMonitor[from].count == g_SpamBlockTimeframe + 1) { - g_SpamMonitor[from][2] = time; + g_SpamMonitor[from].lastBlock = time; + if (from == g_Username) addChatMessage({ "from": "system", "text": translate("Please do not spam. You have been blocked for thirty seconds."), "time": Date.now() / 1000 }); + return true; } @@ -973,11 +988,11 @@ function checkSpamMonitor() var time = Math.floor(Date.now() / 1000); for (let i in g_SpamMonitor) { - let stats = g_SpamMonitor[i]; - if (stats[1] + 5 <= time) + // Reset the spam-status after being silent long enough + if (g_SpamMonitor[i].lastSend + g_SpamBlockTimeframe <= time) { - stats[1] = time; - stats[0] = 0; + g_SpamMonitor[i].count = 0; + g_SpamMonitor[i].lastSend = time; } } }