2019-08-06 14:36:37 -04:00
|
|
|
|
using System.Collections.Generic;
|
2023-02-11 21:49:21 -05:00
|
|
|
|
using System.Linq;
|
2018-09-04 13:40:29 -04:00
|
|
|
|
using System.Text.RegularExpressions;
|
2023-02-11 21:49:21 -05:00
|
|
|
|
using System.Threading;
|
2018-03-29 00:40:57 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Models;
|
2023-02-11 21:49:21 -05:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2018-04-10 20:36:28 -04:00
|
|
|
|
using SharedLibraryCore;
|
2023-02-11 21:49:21 -05:00
|
|
|
|
using SharedLibraryCore.Events.Game;
|
|
|
|
|
using SharedLibraryCore.Events.Management;
|
2018-04-10 20:36:28 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2023-02-11 21:49:21 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces.Events;
|
2018-03-29 00:40:57 -04:00
|
|
|
|
|
2023-02-11 21:49:21 -05:00
|
|
|
|
namespace IW4MAdmin.Plugins.ProfanityDeterment;
|
|
|
|
|
|
|
|
|
|
public class Plugin : IPluginV2
|
2018-03-29 00:40:57 -04:00
|
|
|
|
{
|
2023-02-11 21:49:21 -05:00
|
|
|
|
public string Name => "ProfanityDeterment";
|
|
|
|
|
public string Version => Utilities.GetVersionAsString();
|
|
|
|
|
public string Author => "RaidMax";
|
|
|
|
|
|
|
|
|
|
private const string ProfanityKey = "_profanityInfringements";
|
|
|
|
|
|
|
|
|
|
private readonly ProfanityDetermentConfiguration _configuration;
|
|
|
|
|
|
|
|
|
|
public static void RegisterDependencies(IServiceCollection serviceProvider)
|
2018-03-29 00:40:57 -04:00
|
|
|
|
{
|
2023-02-11 21:49:21 -05:00
|
|
|
|
serviceProvider.AddConfiguration<ProfanityDetermentConfiguration>("ProfanityDetermentSettings");
|
|
|
|
|
}
|
2018-03-29 00:40:57 -04:00
|
|
|
|
|
2023-02-11 21:49:21 -05:00
|
|
|
|
public Plugin(ProfanityDetermentConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
_configuration = configuration;
|
2018-03-29 00:40:57 -04:00
|
|
|
|
|
2023-04-07 22:21:18 -04:00
|
|
|
|
if (!(_configuration?.EnableProfanityDeterment ?? false))
|
2023-02-11 21:49:21 -05:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-03-29 00:40:57 -04:00
|
|
|
|
|
2023-02-11 21:49:21 -05:00
|
|
|
|
IManagementEventSubscriptions.ClientStateInitialized += OnClientStateInitialized;
|
|
|
|
|
IGameEventSubscriptions.ClientMessaged += GameEventSubscriptionsOnClientMessaged;
|
|
|
|
|
IManagementEventSubscriptions.ClientStateDisposed += (clientEvent, _) =>
|
|
|
|
|
{
|
|
|
|
|
clientEvent.Client.SetAdditionalProperty(ProfanityKey, null);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-02-11 17:44:06 -05:00
|
|
|
|
|
2023-02-11 21:49:21 -05:00
|
|
|
|
private Task GameEventSubscriptionsOnClientMessaged(ClientMessageEvent clientEvent, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (!_configuration?.EnableProfanityDeterment ?? false)
|
2020-02-11 17:44:06 -05:00
|
|
|
|
{
|
2023-02-11 21:49:21 -05:00
|
|
|
|
return Task.CompletedTask;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
}
|
2018-03-29 00:40:57 -04:00
|
|
|
|
|
2023-02-11 21:49:21 -05:00
|
|
|
|
var offensiveWords = _configuration!.OffensiveWords;
|
|
|
|
|
var containsOffensiveWord = false;
|
|
|
|
|
var matchedFilters = new List<string>();
|
|
|
|
|
|
|
|
|
|
foreach (var word in offensiveWords.Where(word =>
|
|
|
|
|
Regex.IsMatch(clientEvent.Message?.StripColors() ?? string.Empty, word,
|
|
|
|
|
RegexOptions.IgnoreCase)))
|
2018-03-29 00:40:57 -04:00
|
|
|
|
{
|
2023-02-11 21:49:21 -05:00
|
|
|
|
containsOffensiveWord = true;
|
|
|
|
|
matchedFilters.Add(word);
|
|
|
|
|
}
|
2018-03-30 00:13:40 -04:00
|
|
|
|
|
2023-02-11 21:49:21 -05:00
|
|
|
|
if (!containsOffensiveWord)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2018-03-29 00:40:57 -04:00
|
|
|
|
|
2023-02-11 21:49:21 -05:00
|
|
|
|
var profanityInfringements = clientEvent.Origin.GetAdditionalProperty<int>(ProfanityKey);
|
2018-03-29 00:40:57 -04:00
|
|
|
|
|
2023-02-11 21:49:21 -05:00
|
|
|
|
var sender = clientEvent.Server.AsConsoleClient();
|
|
|
|
|
sender.AdministeredPenalties = new List<EFPenalty>
|
|
|
|
|
{
|
|
|
|
|
new()
|
2018-03-29 00:40:57 -04:00
|
|
|
|
{
|
2023-02-11 21:49:21 -05:00
|
|
|
|
AutomatedOffense = $"{clientEvent.Message} - {string.Join(",", matchedFilters)}"
|
2018-03-29 00:40:57 -04:00
|
|
|
|
}
|
2023-02-11 21:49:21 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (profanityInfringements >= _configuration.KickAfterInfringementCount)
|
|
|
|
|
{
|
|
|
|
|
clientEvent.Client.Kick(_configuration.ProfanityKickMessage, sender);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (profanityInfringements < _configuration.KickAfterInfringementCount)
|
|
|
|
|
{
|
|
|
|
|
clientEvent.Client.SetAdditionalProperty(ProfanityKey, profanityInfringements + 1);
|
|
|
|
|
clientEvent.Client.Warn(_configuration.ProfanityWarningMessage, sender);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task OnClientStateInitialized(ClientStateInitializeEvent clientEvent, CancellationToken token)
|
|
|
|
|
{
|
2023-04-07 22:21:18 -04:00
|
|
|
|
if (!(_configuration?.EnableProfanityDeterment ?? false))
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_configuration.KickOnInfringingName)
|
2023-02-11 21:49:21 -05:00
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
return Task.CompletedTask;
|
2018-03-29 00:40:57 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 21:49:21 -05:00
|
|
|
|
clientEvent.Client.SetAdditionalProperty(ProfanityKey, 0);
|
|
|
|
|
|
|
|
|
|
var offensiveWords = _configuration!.OffensiveWords;
|
|
|
|
|
var matchedFilters = new List<string>();
|
|
|
|
|
var containsOffensiveWord = false;
|
|
|
|
|
|
|
|
|
|
foreach (var word in offensiveWords.Where(word =>
|
|
|
|
|
Regex.IsMatch(clientEvent.Client.CleanedName, word, RegexOptions.IgnoreCase)))
|
|
|
|
|
{
|
|
|
|
|
containsOffensiveWord = true;
|
|
|
|
|
matchedFilters.Add(word);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!containsOffensiveWord)
|
2018-03-29 00:40:57 -04:00
|
|
|
|
{
|
2023-02-11 21:49:21 -05:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sender = Utilities.IW4MAdminClient(clientEvent.Client.CurrentServer);
|
|
|
|
|
sender.AdministeredPenalties = new List<EFPenalty>
|
|
|
|
|
{
|
|
|
|
|
new()
|
2018-03-29 00:40:57 -04:00
|
|
|
|
{
|
2023-02-11 21:49:21 -05:00
|
|
|
|
AutomatedOffense = $"{clientEvent.Client.Name} - {string.Join(",", matchedFilters)}"
|
2018-03-29 00:40:57 -04:00
|
|
|
|
}
|
2023-02-11 21:49:21 -05:00
|
|
|
|
};
|
2018-03-29 00:40:57 -04:00
|
|
|
|
|
2023-02-11 21:49:21 -05:00
|
|
|
|
clientEvent.Client.Kick(_configuration.ProfanityKickMessage, sender);
|
2018-03-29 00:40:57 -04:00
|
|
|
|
|
2023-02-11 21:49:21 -05:00
|
|
|
|
return Task.CompletedTask;
|
2018-03-29 00:40:57 -04:00
|
|
|
|
}
|
|
|
|
|
}
|