[feat] SlangSatinization

# Conflicts:
#	Content.Server/Chat/Systems/ChatSystem.cs
This commit is contained in:
rhailrake
2023-04-25 02:17:40 +06:00
committed by Remuchi
parent dfb3a1e6c4
commit 0cbb69d0a1
8 changed files with 301 additions and 56 deletions

View File

@@ -0,0 +1,36 @@
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.Chat.Commands;
[AdminCommand(AdminFlags.Admin)]
public sealed class SlangSanitizationCommand : IConsoleCommand
{
public string Command => "enableSlangSanitization";
public string Description => "Toggles the slang sanitization.";
public string Help => "enableSlangSanitization <bool>";
[Dependency] private readonly IConfigurationManager _cfg = default!;
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 1 || !bool.TryParse(args[0], out bool value))
{
shell.WriteError($"{args[0]} is not a valid boolean.");
return;
}
_cfg.SetCVar(WhiteCVars.ChatSlangFilter, value);
var announce = Loc.GetString("chatsan-announce-slang-sanitization",
("admin", $"{shell.Player?.Name}"),
("value", $"{value}"));
IoCManager.Resolve<IChatManager>().DispatchServerAnnouncement(announce, Color.Red);
}
}

View File

@@ -1,13 +1,20 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.Json;
using System.Text.RegularExpressions;
using Content.Shared.CCVar;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.Utility;
namespace Content.Server.Chat.Managers;
public sealed class ChatSanitizationManager : IChatSanitizationManager
{
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
[Dependency] private readonly IResourceManager _resources = default!;
private Dictionary<string, string> _slangToNormal = new();
private static readonly Dictionary<string, string> SmileyToEmote = new()
{
@@ -49,6 +56,16 @@ public sealed class ChatSanitizationManager : IChatSanitizationManager
{ ":p", "chatsan-stick-out-tongue" },
{ ":b", "chatsan-stick-out-tongue" },
{ "0-0", "chatsan-wide-eyed" },
//WD-EDIT
{ "о-о", "chatsan-wide-eyed" }, // cyrillic о
{ "о.о", "chatsan-wide-eyed" }, // cyrillic о
{ "0_o", "chatsan-wide-eyed" },
{ "0_о", "chatsan-wide-eyed" }, // cyrillic о
{ "о/", "chatsan-waves" }, // cyrillic о
{ "лол", "chatsan-laughs" },
{ "о7", "chatsan-salutes" }, // cyrillic о
{ "хд", "chatsan-laughs" },
//WD-EDIT
{ "o-o", "chatsan-wide-eyed" },
{ "o.o", "chatsan-wide-eyed" },
{ "._.", "chatsan-surprised" },
@@ -79,6 +96,18 @@ public sealed class ChatSanitizationManager : IChatSanitizationManager
public void Initialize()
{
_configurationManager.OnValueChanged(CCVars.ChatSanitizerEnabled, x => _doSanitize = x, true);
//WD-EDIT
try
{
var filterData = _resources.ContentFileReadAllText(new ResPath("/White/ChatFilters/slang.json"));
_slangToNormal = JsonSerializer.Deserialize<Dictionary<string, string>>(filterData)!;
}
catch (Exception e)
{
Logger.ErrorS("chat", "Failed to load slang.json: {0}", e);
}
//WD-EDIT
}
public bool TrySanitizeOutSmilies(string input, EntityUid speaker, out string sanitized, [NotNullWhen(true)] out string? emote)
@@ -106,4 +135,16 @@ public sealed class ChatSanitizationManager : IChatSanitizationManager
emote = null;
return false;
}
//WD-EDIT
public string SanitizeOutSlang(string input)
{
var pattern = @"\b(?<word>\w+)\b";
var newMessage = Regex.Replace(input, pattern ,
match => _slangToNormal.ContainsKey(match.Groups[1].Value.ToLower()) ? _slangToNormal[match.Groups[1].Value.ToLower()] : match.Value, RegexOptions.IgnoreCase);
return newMessage;
}
//WD-EDIT
}

View File

@@ -7,4 +7,8 @@ public interface IChatSanitizationManager
public void Initialize();
public bool TrySanitizeOutSmilies(string input, EntityUid speaker, out string sanitized, [NotNullWhen(true)] out string? emote);
//WD-EDIT
public string SanitizeOutSlang(string input);
//WD-EDIT
}

View File

@@ -1,4 +1,3 @@
using System.Globalization;
using System.Linq;
using System.Text;
using Content.Server.Administration.Logs;
@@ -19,6 +18,7 @@ using Content.Shared.Interaction;
using Content.Shared.Mobs.Systems;
using Content.Shared.Players;
using Content.Shared.Radio;
using Content.Shared.White;
using Robust.Server.Player;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
@@ -64,7 +64,7 @@ public sealed partial class ChatSystem : SharedChatSystem
private bool _loocEnabled = true;
private bool _deadLoocEnabled;
private bool _critLoocEnabled;
private readonly bool _adminLoocEnabled = true;
private const bool AdminLoocEnabled = true;
public override void Initialize()
{
@@ -217,13 +217,11 @@ public sealed partial class ChatSystem : SharedChatSystem
message = message[1..];
}
bool shouldCapitalize = (desiredType != InGameICChatType.Emote);
bool shouldPunctuate = _configurationManager.GetCVar(CCVars.ChatPunctuation);
// Capitalizing the word I only happens in English, so we check language here
bool shouldCapitalizeTheWordI = (!CultureInfo.CurrentCulture.IsNeutralCulture && CultureInfo.CurrentCulture.Parent.Name == "en")
|| (CultureInfo.CurrentCulture.IsNeutralCulture && CultureInfo.CurrentCulture.Name == "en");
var shouldCapitalize = (desiredType != InGameICChatType.Emote);
var shouldPunctuate = _configurationManager.GetCVar(CCVars.ChatPunctuation);
var sanitizeSlang = _configurationManager.GetCVar(WhiteCVars.ChatSlangFilter);
message = SanitizeInGameICMessage(source, message, out var emoteStr, shouldCapitalize, shouldPunctuate, shouldCapitalizeTheWordI);
message = SanitizeInGameICMessage(source, message, out var emoteStr, shouldCapitalize, shouldPunctuate, sanitizeSlang);
// Was there an emote in the message? If so, send it.
if (player != null && emoteStr != message && emoteStr != null)
@@ -418,7 +416,7 @@ public sealed partial class ChatSystem : SharedChatSystem
// To avoid logging any messages sent by entities that are not players, like vendors, cloning, etc.
// Also doesn't log if hideLog is true.
if (!HasComp<ActorComponent>(source) || hideLog == true)
if (!HasComp<ActorComponent>(source) || hideLog)
return;
if (originalMessage == message)
@@ -431,11 +429,15 @@ public sealed partial class ChatSystem : SharedChatSystem
else
{
if (name != Name(source))
{
_adminLogger.Add(LogType.Chat, LogImpact.Low,
$"Say from {ToPrettyString(source):user} as {name}, original: {originalMessage}, transformed: {message}.");
}
else
{
_adminLogger.Add(LogType.Chat, LogImpact.Low,
$"Say from {ToPrettyString(source):user}, original: {originalMessage}, transformed: {message}.");
}
}
}
@@ -459,7 +461,7 @@ public sealed partial class ChatSystem : SharedChatSystem
var obfuscatedMessage = ObfuscateMessageReadability(message, 0.2f);
// get the entity's name by visual identity (if no override provided).
string nameIdentity = FormattedMessage.EscapeText(nameOverride ?? Identity.Name(source, EntityManager));
var nameIdentity = FormattedMessage.EscapeText(nameOverride ?? Identity.Name(source, EntityManager));
// get the entity's name by voice (if no override provided).
string name;
if (nameOverride != null)
@@ -486,47 +488,51 @@ public sealed partial class ChatSystem : SharedChatSystem
foreach (var (session, data) in GetRecipients(source, WhisperMuffledRange))
{
EntityUid listener;
if (session.AttachedEntity is not { Valid: true } playerEntity)
if (session.AttachedEntity is not { Valid: true })
continue;
listener = session.AttachedEntity.Value;
var listener = session.AttachedEntity.Value;
if (MessageRangeCheck(session, data, range) != MessageRangeCheckResult.Full)
continue; // Won't get logged to chat, and ghosts are too far away to see the pop-up, so we just won't send it to them.
if (data.Range <= WhisperClearRange)
_chatManager.ChatMessageToOne(ChatChannel.Whisper, message, wrappedMessage, source, false, session.ConnectedClient);
_chatManager.ChatMessageToOne(ChatChannel.Whisper, message, wrappedMessage, source, false, session.Channel);
//If listener is too far, they only hear fragments of the message
//Collisiongroup.Opaque is not ideal for this use. Preferably, there should be a check specifically with "Can Ent1 see Ent2" in mind
else if (_interactionSystem.InRangeUnobstructed(source, listener, WhisperMuffledRange, Shared.Physics.CollisionGroup.Opaque)) //Shared.Physics.CollisionGroup.Opaque
_chatManager.ChatMessageToOne(ChatChannel.Whisper, obfuscatedMessage, wrappedobfuscatedMessage, source, false, session.ConnectedClient);
_chatManager.ChatMessageToOne(ChatChannel.Whisper, obfuscatedMessage, wrappedobfuscatedMessage, source, false, session.Channel);
//If listener is too far and has no line of sight, they can't identify the whisperer's identity
else
_chatManager.ChatMessageToOne(ChatChannel.Whisper, obfuscatedMessage, wrappedUnknownMessage, source, false, session.ConnectedClient);
_chatManager.ChatMessageToOne(ChatChannel.Whisper, obfuscatedMessage, wrappedUnknownMessage, source, false, session.Channel);
}
_replay.RecordServerMessage(new ChatMessage(ChatChannel.Whisper, message, wrappedMessage, GetNetEntity(source), null, MessageRangeHideChatForReplay(range)));
var ev = new EntitySpokeEvent(source, message, channel, obfuscatedMessage);
RaiseLocalEvent(source, ev, true);
if (!hideLog)
if (originalMessage == message)
if (hideLog)
return;
if (originalMessage == message)
{
if (name != Name(source))
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Whisper from {ToPrettyString(source):user} as {name}: {originalMessage}.");
else
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Whisper from {ToPrettyString(source):user}: {originalMessage}.");
}
else
{
if (name != Name(source))
{
if (name != Name(source))
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Whisper from {ToPrettyString(source):user} as {name}: {originalMessage}.");
else
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Whisper from {ToPrettyString(source):user}: {originalMessage}.");
_adminLogger.Add(LogType.Chat, LogImpact.Low,
$"Whisper from {ToPrettyString(source):user} as {name}, original: {originalMessage}, transformed: {message}.");
}
else
{
if (name != Name(source))
_adminLogger.Add(LogType.Chat, LogImpact.Low,
$"Whisper from {ToPrettyString(source):user} as {name}, original: {originalMessage}, transformed: {message}.");
else
_adminLogger.Add(LogType.Chat, LogImpact.Low,
_adminLogger.Add(LogType.Chat, LogImpact.Low,
$"Whisper from {ToPrettyString(source):user}, original: {originalMessage}, transformed: {message}.");
}
}
}
private void SendEntityEmote(
@@ -545,7 +551,7 @@ public sealed partial class ChatSystem : SharedChatSystem
// get the entity's apparent name (if no override provided).
var ent = Identity.Entity(source, EntityManager);
string name = FormattedMessage.EscapeText(nameOverride ?? Name(ent));
var name = FormattedMessage.EscapeText(nameOverride ?? Name(ent));
// Emotes use Identity.Name, since it doesn't actually involve your voice at all.
var wrappedMessage = Loc.GetString("chat-manager-entity-me-wrap-message",
@@ -556,11 +562,13 @@ public sealed partial class ChatSystem : SharedChatSystem
if (checkEmote)
TryEmoteChatInput(source, action);
SendInVoiceRange(ChatChannel.Emotes, action, wrappedMessage, source, range, author);
if (!hideLog)
if (name != Name(source))
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Emote from {ToPrettyString(source):user} as {name}: {action}");
else
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Emote from {ToPrettyString(source):user}: {action}");
if (hideLog)
return;
if (name != Name(source))
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Emote from {ToPrettyString(source):user} as {name}: {action}");
else
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Emote from {ToPrettyString(source):user}: {action}");
}
// ReSharper disable once InconsistentNaming
@@ -570,9 +578,13 @@ public sealed partial class ChatSystem : SharedChatSystem
if (_adminManager.IsAdmin(player))
{
if (!_adminLoocEnabled) return;
if (!AdminLoocEnabled)
return;
}
else if (!_loocEnabled)
{
return;
}
else if (!_loocEnabled) return;
// If crit player LOOC is disabled, don't send the message at all.
if (!_critLoocEnabled && _mobStateSystem.IsCritical(source))
@@ -595,7 +607,7 @@ public sealed partial class ChatSystem : SharedChatSystem
{
wrappedMessage = Loc.GetString("chat-manager-send-admin-dead-chat-wrap-message",
("adminChannelName", Loc.GetString("chat-manager-admin-channel-name")),
("userName", player.ConnectedClient.UserName),
("userName", player.Channel.UserName),
("message", FormattedMessage.EscapeText(message)));
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Admin dead chat from {player:Player}: {message}");
}
@@ -704,15 +716,13 @@ public sealed partial class ChatSystem : SharedChatSystem
}
// ReSharper disable once InconsistentNaming
private string SanitizeInGameICMessage(EntityUid source, string message, out string? emoteStr, bool capitalize = true, bool punctuate = false, bool capitalizeTheWordI = true)
private string SanitizeInGameICMessage(EntityUid source, string message, out string? emoteStr, bool capitalize = true, bool punctuate = false, bool sanitizeSlang = true)
{
var newMessage = message.Trim();
newMessage = SanitizeMessageReplaceWords(newMessage);
if(sanitizeSlang)
newMessage = _sanitizer.SanitizeOutSlang(newMessage);
if (capitalize)
newMessage = SanitizeMessageCapital(newMessage);
if (capitalizeTheWordI)
newMessage = SanitizeMessageCapitalizeTheWordI(newMessage, "i");
if (punctuate)
newMessage = SanitizeMessagePeriod(newMessage);
@@ -768,15 +778,16 @@ public sealed partial class ChatSystem : SharedChatSystem
}
[ValidatePrototypeId<ReplacementAccentPrototype>]
public const string ChatSanitize_Accent = "chatsanitize";
public const string ChatSanitizeAccent = "chatsanitize";
public string SanitizeMessageReplaceWords(string message)
{
if (string.IsNullOrEmpty(message)) return message;
if (string.IsNullOrEmpty(message))
return message;
var msg = message;
msg = _wordreplacement.ApplyReplacements(msg, ChatSanitize_Accent);
msg = _wordreplacement.ApplyReplacements(msg, ChatSanitizeAccent);
return msg;
}
@@ -823,9 +834,7 @@ public sealed partial class ChatSystem : SharedChatSystem
return recipients;
}
public readonly record struct ICChatRecipientData(float Range, bool Observer, bool? HideChatOverride = null)
{
}
public readonly record struct ICChatRecipientData(float Range, bool Observer, bool? HideChatOverride = null);
private string ObfuscateMessageReadability(string message, float chance)
{
@@ -854,9 +863,7 @@ public sealed partial class ChatSystem : SharedChatSystem
/// This event is raised before chat messages are sent out to clients. This enables some systems to send the chat
/// messages to otherwise out-of view entities (e.g. for multiple viewports from cameras).
/// </summary>
public record ExpandICChatRecipientstEvent(EntityUid Source, float VoiceRange, Dictionary<ICommonSession, ChatSystem.ICChatRecipientData> Recipients)
{
}
public record ExpandICChatRecipientstEvent(EntityUid Source, float VoiceRange, Dictionary<ICommonSession, ChatSystem.ICChatRecipientData> Recipients);
public sealed class TransformSpeakerNameEvent : EntityEventArgs
{