Antispam fixes (#312)

This commit is contained in:
Aviu00
2023-08-17 23:40:16 +03:00
committed by Aviu00
parent 0c865f6833
commit 1b1fdfd833
3 changed files with 33 additions and 10 deletions

View File

@@ -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
/// <summary>
@@ -64,7 +65,10 @@ namespace Content.Server.Chat.Managers
private bool _adminOocEnabled = true;
private readonly Dictionary<NetUserId, ChatUser> _players = new();
private Dictionary<NetUserId, string> _lastMessages = new();
private readonly Dictionary<NetUserId, (string, TimeSpan)> _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;
}

View File

@@ -34,6 +34,12 @@ public sealed class WhiteCVars
public static readonly CVarDef<bool> ChatAntispam =
CVarDef.Create("ic.antispam", true, CVar.SERVER | CVar.REPLICATED | CVar.ARCHIVE);
public static readonly CVarDef<int> AntispamMinLength =
CVarDef.Create("ic.antispam_min_length", 7, CVar.SERVERONLY);
public static readonly CVarDef<double> AntispamIntervalSeconds =
CVarDef.Create("ic.antispam_interval_seconds", 60d, CVar.SERVERONLY);
/*
* Sponsors
*/

View File

@@ -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 } сек.