From 701d470494e4ed4d0e3b8d98a36763dea98b71ba Mon Sep 17 00:00:00 2001 From: calzoneman Date: Wed, 27 Jul 2016 23:31:23 -0700 Subject: [PATCH] Add initial blocking of new users in chat --- src/channel/chat.js | 15 +++++++++++++++ src/io/ioserver.js | 4 +++- src/user.js | 14 ++++++++++++++ www/js/callbacks.js | 16 ++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/channel/chat.js b/src/channel/chat.js index 16204089..da2117b7 100644 --- a/src/channel/chat.js +++ b/src/channel/chat.js @@ -131,6 +131,21 @@ ChatModule.prototype.handleChatMsg = function (user, data) { // Limit to 240 characters data.msg = data.msg.substring(0, 240); + + const firstSeen = user.getFirstSeenTime(); + // TODO: configurable times + if (firstSeen > Date.now() - 5 * 60 * 1000) { + user.socket.emit("spamFiltered", { + reason: "NEW_USER_CHAT" + }); + return; + } else if ((firstSeen > Date.now() - 24 * 60 * 60 * 1000) && data.msg.match(LINK)) { + user.socket.emit("spamFiltered", { + reason: "NEW_USER_CHAT_LINK" + }); + return; + } + // If channel doesn't permit them, strip ASCII control characters if (!this.channel.modules.options || !this.channel.modules.options.get("allow_ascii_control")) { diff --git a/src/io/ioserver.js b/src/io/ioserver.js index 603f5b18..5afa011c 100644 --- a/src/io/ioserver.js +++ b/src/io/ioserver.js @@ -52,7 +52,8 @@ function handleAuth(socket, accept) { if (!err) { socket.user = { name: user.name, - global_rank: user.global_rank + global_rank: user.global_rank, + registrationTime: new Date(user.time) }; } accept(null, true); @@ -235,6 +236,7 @@ function handleConnection(sock) { user.setFlag(Flags.U_REGISTERED); user.clearFlag(Flags.U_READY); user.account.name = sock.user.name; + user.registrationTime = sock.user.registrationTime; user.refreshAccount(function (err, account) { if (err) { user.clearFlag(Flags.U_REGISTERED); diff --git a/src/user.js b/src/user.js index a538c9b2..e38d5648 100644 --- a/src/user.js +++ b/src/user.js @@ -449,4 +449,18 @@ User.prototype.refreshAccount = function (cb) { }); }; +User.prototype.getFirstSeenTime = function getFirstSeenTime() { + if (this.registrationTime && this.socket.ipReputation && this.socket.ipReputation.first_seen) { + return Math.min(this.registrationTime.getTime(), this.socket.ipReputation.first_seen.getTime()); + } else if (this.registrationTime) { + return this.registrationTime.getTime(); + } else if (this.socket.ipReputation && this.socket.ipReputation.first_seen) { + return this.socket.ipReputation.first_seen.getTime(); + } else { + Logger.errlog.log(`User "${this.getName()}" (IP: ${this.realip}) has neither ` + + "an IP reputation nor a registered account."); + return Date.now(); + } +}; + module.exports = User; diff --git a/www/js/callbacks.js b/www/js/callbacks.js index e894faa4..6b89328e 100644 --- a/www/js/callbacks.js +++ b/www/js/callbacks.js @@ -88,6 +88,22 @@ Callbacks = { scrollChat(); }, + spamFiltered: function(data) { + var message = "Spam Filtered."; + switch (data.reason) { + case "NEW_USER_CHAT": + message = "Your account is too new to chat in this channel. " + + "Please wait a while and try again."; + break; + case "NEW_USER_CHAT_LINK": + message = "Your account is too new to post links in this channel. " + + "Please wait a while and try again."; + break; + } + + errDialog(message); + }, + needPassword: function (wrongpw) { var div = $("
"); $("").text("Channel Password")