Skip to content

Commit

Permalink
Add prohibited characters filter (SerbiaStrong-220#2538)
Browse files Browse the repository at this point in the history
* Add prohibited characters filter

* change message comparation
  • Loading branch information
Kirus59 authored Feb 9, 2025
1 parent 462ab5e commit 543b1e7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions Content.Server/Chat/Managers/ChatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ public void TrySendOOCMessage(ICommonSession player, string message, OOCChatType
switch (type)
{
case OOCChatType.OOC:
message = DeleteProhibitedCharacters(message, player); // SS220 delete prohibited characters
SendOOC(player, message);
break;
case OOCChatType.Admin:
Expand Down
6 changes: 6 additions & 0 deletions Content.Server/Chat/Managers/IChatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,11 @@ void ChatMessageToMany(ChatChannel channel, string message, string wrappedMessag
/// <param name="player">The player sending a chat message.</param>
/// <returns>False if the player has violated rate limits and should be blocked from sending further messages.</returns>
RateLimitStatus HandleRateLimit(ICommonSession player);

// SS220 delete prohibited characters begin
public string DeleteProhibitedCharacters(string message, EntityUid player);

public string DeleteProhibitedCharacters(string message, ICommonSession? player = null);
// SS220 delete prohibited characters end
}
}
3 changes: 3 additions & 0 deletions Content.Server/Chat/Systems/ChatSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ public void TrySendInGameICMessage(
bool shouldCapitalizeTheWordI = (!CultureInfo.CurrentCulture.IsNeutralCulture && CultureInfo.CurrentCulture.Parent.Name == "en")
|| (CultureInfo.CurrentCulture.IsNeutralCulture && CultureInfo.CurrentCulture.Name == "en");

message = _chatManager.DeleteProhibitedCharacters(message, source); // SS220 delete prohibited characters
message = SanitizeInGameICMessage(source, message, out var emoteStr, shouldCapitalize, shouldPunctuate, shouldCapitalizeTheWordI);

// Was there an emote in the message? If so, send it.
Expand Down Expand Up @@ -324,6 +325,7 @@ public void TrySendInGameOOCMessage(
if (player?.AttachedEntity is not { Valid: true } entity || source != entity)
return;

message = _chatManager.DeleteProhibitedCharacters(message, source); // SS220 delete prohibited characters
message = SanitizeInGameOOCMessage(message);

var sendType = type;
Expand Down Expand Up @@ -691,6 +693,7 @@ private void SendDeadChat(EntityUid source, ICommonSession player, string messag
{
var clients = GetDeadChatClients();
var playerName = Name(source);
message = _chatManager.DeleteProhibitedCharacters(message, player); // SS220 delete prohibited characters
string wrappedMessage;
if (_adminManager.IsAdmin(player))
{
Expand Down
3 changes: 3 additions & 0 deletions Content.Server/Communications/CommunicationsConsoleSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Content.Server.Administration.Logs;
using Content.Server.AlertLevel;
using Content.Server.Chat.Managers;
using Content.Server.Chat.Systems;
using Content.Server.DeviceNetwork.Components;
using Content.Server.DeviceNetwork.Systems;
Expand Down Expand Up @@ -38,6 +39,7 @@ public sealed class CommunicationsConsoleSystem : EntitySystem
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly IChatManager _chatManager = default!; // SS220 delete prohibited characters

private const float UIUpdateInterval = 5.0f;

Expand Down Expand Up @@ -268,6 +270,7 @@ private void OnAnnounceMessage(EntityUid uid, CommunicationsConsoleComponent com
Loc.TryGetString(comp.Title, out var title);
title ??= comp.Title;

msg = _chatManager.DeleteProhibitedCharacters(msg, message.Actor); // SS220 delete prohibited characters
msg += "\n" + Loc.GetString("comms-console-announcement-sent-by") + " " + author;
if (comp.Global)
{
Expand Down
32 changes: 32 additions & 0 deletions Content.Server/SS220/Chat/ChatManager.Censorship.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Content.Shared.Database;
using Content.Shared.Mind;
using Robust.Shared.Player;
using System.Text.RegularExpressions;

namespace Content.Server.Chat.Managers;

internal sealed partial class ChatManager
{
[GeneratedRegex(@"[\u0fd5-\u0fd8\u2500-\u25ff\u2800-\u28ff]+")]
private static partial Regex ProhibitedCharactersRegex();

public string DeleteProhibitedCharacters(string message, EntityUid player)
{
var mindSystem = _entityManager.System<SharedMindSystem>();
mindSystem.TryGetMind(player, out _, out var mind);

return DeleteProhibitedCharacters(message, mind?.Session);
}

public string DeleteProhibitedCharacters(string message, ICommonSession? player = null)
{
string censoredMessage = ProhibitedCharactersRegex().Replace(message, string.Empty);
if (message.Length != censoredMessage.Length && player != null)
{
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"{player.Name} tried to send a message with forbidden characters:\n{message}");
SendAdminAlert(Loc.GetString("chat-manager-founded-prohibited-characters", ("player", player.Name), ("message", message)));
}

return censoredMessage;
}
}
2 changes: 2 additions & 0 deletions Resources/Locale/ru-RU/ss220/chat/managers/chat-manager.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
chat-manager-founded-prohibited-characters = { $player } попытался отправить сообщение с запрещенными символами:
{ $message }

0 comments on commit 543b1e7

Please sign in to comment.