From 3aacf3c84131306cfcd0cdbd3020b81a7a8be04a Mon Sep 17 00:00:00 2001 From: Aviu00 <93730715+Aviu00@users.noreply.github.com> Date: Mon, 31 Jul 2023 09:33:04 +0300 Subject: [PATCH] Antispam (#232) * Antispam * Add cvar --- Content.Server/Chat/Managers/ChatManager.cs | 29 +++++++++++++++ Content.Server/Chat/Managers/IChatManager.cs | 7 +++- Content.Server/Chat/Systems/ChatSystem.cs | 11 ++++++ .../White/Commands/AntispamCommand.cs | 35 +++++++++++++++++++ Content.Shared/White/WhiteCVars.cs | 7 ++++ 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 Content.Server/White/Commands/AntispamCommand.cs diff --git a/Content.Server/Chat/Managers/ChatManager.cs b/Content.Server/Chat/Managers/ChatManager.cs index 8c8fd20f97..3ff5d56d3b 100644 --- a/Content.Server/Chat/Managers/ChatManager.cs +++ b/Content.Server/Chat/Managers/ChatManager.cs @@ -5,6 +5,7 @@ using Content.Server.Administration.Logs; using Content.Server.Administration.Managers; using Content.Server.Administration.Systems; using Content.Server.MoMMI; +using Content.Server.Players; using Content.Server.Preferences.Managers; using Content.Server.UtkaIntegration; using Content.Server.White.Sponsors; @@ -13,6 +14,7 @@ using Content.Shared.CCVar; using Content.Shared.Chat; using Content.Shared.Database; using Content.Shared.Mind; +using Content.Shared.White; using Robust.Server.Player; using Robust.Shared.Configuration; using Robust.Shared.Network; @@ -62,6 +64,7 @@ namespace Content.Server.Chat.Managers private bool _adminOocEnabled = true; private readonly Dictionary _players = new(); + private Dictionary _lastMessages = new(); public void Initialize() { @@ -201,6 +204,29 @@ namespace Content.Server.Chat.Managers _utkaSocketWrapper.SendMessageToAll(asayEventMessage); } + + public bool TrySendNewMessage(ICommonSession session, string newMessage) + { + if (!_configurationManager.GetCVar(WhiteCVars.ChatAntispam)) + return true; + + if (_lastMessages.TryGetValue(session.Data.UserId, out var value)) + { + if (value == newMessage) + { + DispatchServerMessage(session, "Не повторяйте сообщение."); + return false; + } + + _lastMessages[session.Data.UserId] = newMessage; + } + else + { + _lastMessages.Add(session.Data.UserId, newMessage); + } + + return true; + } //WD-EDIT #endregion @@ -254,6 +280,9 @@ namespace Content.Server.Chat.Managers return; } + if (!TrySendNewMessage(player, message)) // WD + return; + Color? colorOverride = null; var wrappedMessage = Loc.GetString("chat-manager-send-ooc-wrap-message", ("playerName",player.Name), ("message", FormattedMessage.EscapeText(message))); if (_adminManager.HasAdminFlag(player, AdminFlags.Admin)) diff --git a/Content.Server/Chat/Managers/IChatManager.cs b/Content.Server/Chat/Managers/IChatManager.cs index 9c0a0b1c93..8069aba82c 100644 --- a/Content.Server/Chat/Managers/IChatManager.cs +++ b/Content.Server/Chat/Managers/IChatManager.cs @@ -21,7 +21,12 @@ namespace Content.Server.Chat.Managers void TrySendOOCMessage(ICommonSession player, string message, OOCChatType type); void SendHookOOC(string sender, string message); - void SendHookAdminChat(string sender, string message); // WD-EDIT + + // WD-EDIT + void SendHookAdminChat(string sender, string message); + bool TrySendNewMessage(ICommonSession session, string newMessage); + // WD-EDIT + void SendAdminAnnouncement(string message); void SendAdminAlert(string message); void SendAdminAlert(EntityUid player, string message); diff --git a/Content.Server/Chat/Systems/ChatSystem.cs b/Content.Server/Chat/Systems/ChatSystem.cs index f57c0c0845..5781688ee8 100644 --- a/Content.Server/Chat/Systems/ChatSystem.cs +++ b/Content.Server/Chat/Systems/ChatSystem.cs @@ -6,6 +6,10 @@ using Content.Server.Chat.Managers; using Content.Server.GameTicking; using Content.Server.Speech.Components; using Content.Server.Speech.EntitySystems; +using Content.Server.Ghost.Components; +using Content.Server.Players; +using Content.Server.Popups; +using Content.Server.Speech.Components; using Content.Server.Station.Components; using Content.Server.Station.Systems; using Content.Server.UtkaIntegration; @@ -245,6 +249,10 @@ public sealed partial class ChatSystem : SharedChatSystem if (string.IsNullOrEmpty(message)) return; + if (desiredType != InGameICChatType.Emote && player is not null && + !_chatManager.TrySendNewMessage(player, message)) // WD + return; + // This message may have a radio prefix, and should then be whispered to the resolved radio channel if (checkRadioPrefix) { @@ -302,6 +310,9 @@ public sealed partial class ChatSystem : SharedChatSystem if (!_critLoocEnabled && _mobStateSystem.IsCritical(source)) return; + if (!_chatManager.TrySendNewMessage(player, message)) // WD + return; + switch (sendType) { case InGameOOCChatType.Dead: diff --git a/Content.Server/White/Commands/AntispamCommand.cs b/Content.Server/White/Commands/AntispamCommand.cs new file mode 100644 index 0000000000..149a277a6b --- /dev/null +++ b/Content.Server/White/Commands/AntispamCommand.cs @@ -0,0 +1,35 @@ +using Content.Server.Administration; +using Content.Server.Chat.Managers; +using Content.Shared.Administration; +using Content.Shared.CCVar; +using Content.Shared.White; +using Robust.Shared.Configuration; +using Robust.Shared.Console; + +namespace Content.Server.White.Commands; + +[AdminCommand(AdminFlags.Admin)] +public sealed class AntispamCommand : IConsoleCommand +{ + public string Command => "setantispam"; + public string Description => "Переключает антиспам систему."; + public string Help => "setantispam "; + + [Dependency] private readonly IConfigurationManager _cfg = default!; + + public void Execute(IConsoleShell shell, string argStr, string[] args) + { + if (args.Length != 1 || !bool.TryParse(args[0], out var value)) + { + shell.WriteError($"{args[0]} is not a valid boolean."); + return; + } + + _cfg.SetCVar(WhiteCVars.ChatAntispam, value); + + var toggle = value ? "включил" : "выключил"; + var announce = $"{shell.Player?.Name} {toggle} антиспам систему"; + + IoCManager.Resolve().DispatchServerAnnouncement(announce, Color.Red); + } +} diff --git a/Content.Shared/White/WhiteCVars.cs b/Content.Shared/White/WhiteCVars.cs index 16728c1b28..e871dd2533 100644 --- a/Content.Shared/White/WhiteCVars.cs +++ b/Content.Shared/White/WhiteCVars.cs @@ -19,6 +19,13 @@ public sealed class WhiteCVars public static readonly CVarDef ChatSlangFilter = CVarDef.Create("ic.slang_filter", true, CVar.SERVER | CVar.REPLICATED | CVar.ARCHIVE); + /* + * Antispam + */ + + public static readonly CVarDef ChatAntispam = + CVarDef.Create("ic.antispam", true, CVar.SERVER | CVar.REPLICATED | CVar.ARCHIVE); + /* * Sponsors */