Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mirror 25908: Chat Censorship Systems #444

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Content.Shared/Chat/V2/Moderation/ChatCensor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Linq;

namespace Content.Shared.Chat.V2.Moderation;

public interface IChatCensor
{
public bool Censor(string input, out string output, char replaceWith = '*');
}

public sealed class CompoundChatCensor(IEnumerable<IChatCensor> censors) : IChatCensor
{
public bool Censor(string input, out string output, char replaceWith = '*')
{
var censored = false;

foreach (var censor in censors)
{
if (censor.Censor(input, out output, replaceWith))
{
censored = true;
}
}

output = input;

return censored;
}
}

public sealed class ChatCensorFactory
{
private List<IChatCensor> _censors = new();

public void With(IChatCensor censor)
{
_censors.Add(censor);
}

/// <summary>
/// Builds a ChatCensor that combines all the censors that have been added to this.
/// </summary>
public IChatCensor Build()
{
return new CompoundChatCensor(_censors.ToArray());
}

/// <summary>
/// Resets the build state to zero, allowing for different rules to be provided to the next censor(s) built.
/// </summary>
/// <returns>True if the builder had any setup prior to the reset.</returns>
public bool Reset()
{
var notEmpty = _censors.Count > 0;

_censors = new List<IChatCensor>();

return notEmpty;
}
}
15 changes: 15 additions & 0 deletions Content.Shared/Chat/V2/Moderation/RegexCensor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.RegularExpressions;

namespace Content.Shared.Chat.V2.Moderation;

public sealed class RegexCensor(Regex censorInstruction) : IChatCensor
{
private readonly Regex _censorInstruction = censorInstruction;

public bool Censor(string input, out string output, char replaceWith = '*')
{
output = _censorInstruction.Replace(input, replaceWith.ToString());

return !string.Equals(input, output);
}
}
Loading
Loading