From 1b1fdfd8338a339cc49cb3492e3edae03f3619b0 Mon Sep 17 00:00:00 2001 From: Aviu00 <93730715+Aviu00@users.noreply.github.com> Date: Thu, 17 Aug 2023 23:40:16 +0300 Subject: [PATCH] Antispam fixes (#312) --- Content.Server/Chat/Managers/ChatManager.cs | 33 +++++++++++++------ Content.Shared/White/WhiteCVars.cs | 6 ++++ .../ru-RU/chat/managers/chat-manager.ftl | 4 +++ 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/Content.Server/Chat/Managers/ChatManager.cs b/Content.Server/Chat/Managers/ChatManager.cs index 81d4bd0005..1807a2bed1 100644 --- a/Content.Server/Chat/Managers/ChatManager.cs +++ b/Content.Server/Chat/Managers/ChatManager.cs @@ -53,6 +53,7 @@ namespace Content.Server.Chat.Managers /// WD-EDIT [Dependency] private readonly SponsorsManager _sponsorsManager = default!; [Dependency] private readonly UtkaTCPWrapper _utkaSocketWrapper = default!; + [Dependency] private readonly IGameTiming _timing = default!; /// WD-EDIT /// @@ -64,7 +65,10 @@ namespace Content.Server.Chat.Managers private bool _adminOocEnabled = true; private readonly Dictionary _players = new(); - private Dictionary _lastMessages = new(); + private readonly Dictionary _lastMessages = new(); + private bool _antispam; + private int _antispamMinLength; + private double _antispamIntervalSeconds; public void Initialize() { @@ -75,6 +79,13 @@ namespace Content.Server.Chat.Managers _configurationManager.OnValueChanged(CCVars.AdminOocEnabled, OnAdminOocEnabledChanged, true); _playerManager.PlayerStatusChanged += PlayerStatusChanged; + + // WD START + _configurationManager.OnValueChanged(WhiteCVars.ChatAntispam, val => _antispam = val, true); + _configurationManager.OnValueChanged(WhiteCVars.AntispamMinLength, val => _antispamMinLength = val, true); + _configurationManager.OnValueChanged(WhiteCVars.AntispamIntervalSeconds, + val => _antispamIntervalSeconds = val, true); + // WD END } private void OnOocEnabledChanged(bool val) @@ -207,23 +218,25 @@ namespace Content.Server.Chat.Managers public bool TrySendNewMessage(ICommonSession session, string newMessage) { - if (!_configurationManager.GetCVar(WhiteCVars.ChatAntispam)) + if (!_antispam || newMessage.Length < _antispamMinLength) + { + _lastMessages.Remove(session.Data.UserId); return true; + } + var curTime = _timing.CurTime; if (_lastMessages.TryGetValue(session.Data.UserId, out var value)) { - if (value == newMessage) + var interval = (curTime - value.Item2).TotalSeconds; + var difference = _antispamIntervalSeconds - interval; + if (value.Item1 == newMessage && difference > 0d) { - DispatchServerMessage(session, "Не повторяйте сообщение."); + DispatchServerMessage(session, + Loc.GetString("chat-manager-antispam-warn-message", ("remainingTime", (int) difference))); return false; } - - _lastMessages[session.Data.UserId] = newMessage; - } - else - { - _lastMessages.Add(session.Data.UserId, newMessage); } + _lastMessages[session.Data.UserId] = (newMessage, curTime); return true; } diff --git a/Content.Shared/White/WhiteCVars.cs b/Content.Shared/White/WhiteCVars.cs index e60e48af07..512d083c25 100644 --- a/Content.Shared/White/WhiteCVars.cs +++ b/Content.Shared/White/WhiteCVars.cs @@ -34,6 +34,12 @@ public sealed class WhiteCVars public static readonly CVarDef ChatAntispam = CVarDef.Create("ic.antispam", true, CVar.SERVER | CVar.REPLICATED | CVar.ARCHIVE); + public static readonly CVarDef AntispamMinLength = + CVarDef.Create("ic.antispam_min_length", 7, CVar.SERVERONLY); + + public static readonly CVarDef AntispamIntervalSeconds = + CVarDef.Create("ic.antispam_interval_seconds", 60d, CVar.SERVERONLY); + /* * Sponsors */ diff --git a/Resources/Locale/ru-RU/chat/managers/chat-manager.ftl b/Resources/Locale/ru-RU/chat/managers/chat-manager.ftl index 57ba6c9a1c..2335c42c22 100644 --- a/Resources/Locale/ru-RU/chat/managers/chat-manager.ftl +++ b/Resources/Locale/ru-RU/chat/managers/chat-manager.ftl @@ -105,3 +105,7 @@ chat-speech-verb-ghost-1 = жалуется chat-speech-verb-ghost-2 = дышит chat-speech-verb-ghost-3 = мычит chat-speech-verb-ghost-4 = бормочет + +chat-manager-cooldown-warn-message_channel = Вы сможете писать { $inChat } через: { $remainingTime } сек. +chat-manager-cooldown-warn-message = Вы сможете писать через { $remainingTime } сек. +chat-manager-antispam-warn-message = Вы сможете повторить сообщение через { $remainingTime } сек.