NoSoliciting/NoSoliciting/Filter.cs

215 lines
7.6 KiB
C#
Raw Permalink Normal View History

2021-08-22 22:07:28 +00:00
using System;
using Dalamud.Game.Gui.PartyFinder.Types;
2021-04-05 18:45:04 +00:00
using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling;
2021-05-03 20:24:57 +00:00
using NoSoliciting.Interface;
2020-12-21 02:49:10 +00:00
using NoSoliciting.Ml;
2020-08-04 22:22:57 +00:00
namespace NoSoliciting {
2020-08-25 12:00:18 +00:00
public partial class Filter : IDisposable {
2020-12-21 02:49:10 +00:00
private const uint MinWords = 4;
public static readonly ChatType[] FilteredChatTypes = {
ChatType.Say,
ChatType.Yell,
ChatType.Shout,
ChatType.TellIncoming,
ChatType.Party,
ChatType.CrossParty,
ChatType.Alliance,
ChatType.FreeCompany,
ChatType.PvpTeam,
ChatType.CrossLinkshell1,
ChatType.CrossLinkshell2,
ChatType.CrossLinkshell3,
ChatType.CrossLinkshell4,
ChatType.CrossLinkshell5,
ChatType.CrossLinkshell6,
ChatType.CrossLinkshell7,
ChatType.CrossLinkshell8,
ChatType.Linkshell1,
ChatType.Linkshell2,
ChatType.Linkshell3,
ChatType.Linkshell4,
ChatType.Linkshell5,
ChatType.Linkshell6,
ChatType.Linkshell7,
ChatType.Linkshell8,
ChatType.NoviceNetwork,
};
private Plugin Plugin { get; }
private int LastBatch { get; set; } = -1;
2020-12-21 02:49:10 +00:00
private bool _disposedValue;
2020-08-04 22:22:57 +00:00
2020-12-21 02:49:10 +00:00
public Filter(Plugin plugin) {
this.Plugin = plugin ?? throw new ArgumentNullException(nameof(plugin), "Plugin cannot be null");
2021-08-22 22:07:28 +00:00
this.Plugin.ChatGui.CheckMessageHandled += this.OnChat;
this.Plugin.PartyFinderGui.ReceiveListing += this.OnListing;
2020-12-21 02:49:10 +00:00
}
2020-08-04 22:22:57 +00:00
2021-03-31 13:46:47 +00:00
private void Dispose(bool disposing) {
if (this._disposedValue) {
return;
}
2020-12-21 02:49:10 +00:00
2021-03-31 13:46:47 +00:00
if (disposing) {
2021-08-22 22:07:28 +00:00
this.Plugin.ChatGui.CheckMessageHandled -= this.OnChat;
this.Plugin.PartyFinderGui.ReceiveListing -= this.OnListing;
2020-12-21 02:49:10 +00:00
}
2021-03-31 13:46:47 +00:00
this._disposedValue = true;
}
public void Dispose() {
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void OnChat(XivChatType type, uint senderId, ref SeString sender, ref SeString message, ref bool isHandled) {
isHandled = isHandled || this.FilterMessage(type, senderId, sender, message);
2021-03-31 13:46:47 +00:00
}
private void OnListing(PartyFinderListing listing, PartyFinderListingEventArgs args) {
try {
if (this.LastBatch != args.BatchNumber) {
2021-03-31 13:46:47 +00:00
this.Plugin.ClearPartyFinderHistory();
}
this.LastBatch = args.BatchNumber;
var version = this.Plugin.MlFilter?.Version;
2021-05-15 22:19:27 +00:00
var (category, reason) = this.MlListingFilterReason(listing);
2021-03-31 13:46:47 +00:00
this.Plugin.AddPartyFinderHistory(new Message(
2021-05-15 22:19:27 +00:00
version,
2021-03-31 13:46:47 +00:00
ChatType.None,
listing.ContentIdLower,
2021-03-31 13:46:47 +00:00
listing.Name,
listing.Description,
2021-05-15 22:19:27 +00:00
category,
reason == "custom",
reason == "ilvl",
this.Plugin.Config.CreateFiltersClone()
2021-03-31 13:46:47 +00:00
));
2021-05-15 22:19:27 +00:00
if (category == null && reason == null) {
2021-03-31 13:46:47 +00:00
return;
}
args.Visible = false;
if (this.Plugin.Config.LogFilteredPfs) {
2023-09-29 00:59:42 +00:00
Plugin.Log.Info($"Filtered PF listing from {listing.Name.TextValue} ({reason}): {listing.Description.TextValue}");
}
} catch (Exception ex) {
2023-09-29 00:59:42 +00:00
Plugin.Log.Error($"Error in PF listing event: {ex}");
2020-12-21 02:49:10 +00:00
}
}
private bool FilterMessage(XivChatType type, uint senderId, SeString sender, SeString message) {
2020-12-21 02:49:10 +00:00
if (message == null) {
throw new ArgumentNullException(nameof(message), "SeString cannot be null");
}
return this.MlFilterMessage(type, senderId, sender, message);
2020-12-21 02:49:10 +00:00
}
private bool MlFilterMessage(XivChatType type, uint senderId, SeString sender, SeString message) {
2020-12-21 02:49:10 +00:00
var chatType = ChatTypeExt.FromDalamud(type);
// NOTE: don't filter on user-controlled chat types here because custom filters are supposed to check all
// messages except battle messages
if (chatType.IsBattle()) {
return false;
}
var text = message.TextValue;
2021-05-15 22:19:27 +00:00
var custom = false;
MessageCategory? classification = null;
2020-12-21 02:49:10 +00:00
2021-01-02 18:51:48 +00:00
// step 1. check for custom filters if enabled
2021-05-15 22:19:27 +00:00
var filter = false;
if (this.Plugin.Config.CustomChatFilter && Chat.MatchesCustomFilters(text, this.Plugin.Config)) {
filter = true;
custom = true;
}
2020-12-21 02:49:10 +00:00
2021-01-02 18:51:48 +00:00
// only look at ml if message >= min words
2021-05-17 20:22:19 +00:00
if (!filter && this.Plugin.MlFilter != null && CountWords(text) >= MinWords) {
2021-01-02 18:51:48 +00:00
// step 2. classify the message using the model
var category = this.Plugin.MlFilter.ClassifyMessage((ushort) chatType, text);
// step 2a. only filter if configured to act on this channel
2021-05-15 22:19:27 +00:00
if (category != MessageCategory.Normal && this.Plugin.Config.MlEnabledOn(category, chatType)) {
filter = true;
classification = category;
}
2021-01-02 18:51:48 +00:00
}
2021-05-15 22:19:27 +00:00
var history = new Message(
this.Plugin.MlFilter?.Version,
2020-12-21 02:49:10 +00:00
ChatTypeExt.FromDalamud(type),
senderId,
2020-12-21 02:49:10 +00:00
sender,
message,
2021-05-15 22:19:27 +00:00
classification,
custom,
false,
this.Plugin.Config.CreateFiltersClone()
2021-05-15 22:19:27 +00:00
);
this.Plugin.AddMessageHistory(history);
2020-12-21 02:49:10 +00:00
if (filter && this.Plugin.Config.LogFilteredChat) {
2023-09-29 00:59:42 +00:00
Plugin.Log.Info($"Filtered chat message ({history.FilterReason ?? "unknown"}): {text}");
2020-12-21 02:49:10 +00:00
}
return filter;
}
2021-05-15 22:19:27 +00:00
private (MessageCategory?, string?) MlListingFilterReason(PartyFinderListing listing) {
2020-12-21 02:49:10 +00:00
if (this.Plugin.MlFilter == null) {
2021-05-15 22:19:27 +00:00
return (null, null);
2020-08-04 22:22:57 +00:00
}
2021-03-31 13:46:47 +00:00
// ignore private listings if configured
if (!this.Plugin.Config.ConsiderPrivatePfs && listing[SearchAreaFlags.Private]) {
2021-05-15 22:19:27 +00:00
return (null, null);
}
2021-03-31 13:46:47 +00:00
// step 1. check if pf has an item level that's too high
2021-08-22 22:07:28 +00:00
if (this.Plugin.Config.FilterHugeItemLevelPFs && listing.MinimumItemLevel > FilterUtil.MaxItemLevelAttainable(this.Plugin.DataManager)) {
2021-05-15 22:19:27 +00:00
return (null, "ilvl");
2021-03-31 13:46:47 +00:00
}
2020-08-04 22:22:57 +00:00
2021-05-20 17:49:41 +00:00
var desc = listing.Description.TextValue;
2021-03-31 13:46:47 +00:00
// step 2. check custom filters
if (this.Plugin.Config.CustomPFFilter && PartyFinder.MatchesCustomFilters(desc, this.Plugin.Config)) {
2021-05-15 22:19:27 +00:00
return (null, "custom");
2020-08-04 22:22:57 +00:00
}
2021-03-31 13:46:47 +00:00
// only look at ml for pfs >= min words
2021-05-17 20:22:19 +00:00
if (CountWords(desc) < MinWords) {
2021-05-15 22:19:27 +00:00
return (null, null);
2021-03-31 13:46:47 +00:00
}
2020-08-04 22:22:57 +00:00
2021-03-31 13:46:47 +00:00
var category = this.Plugin.MlFilter.ClassifyMessage((ushort) ChatType.None, desc);
2020-08-04 22:22:57 +00:00
2021-03-31 13:46:47 +00:00
if (category != MessageCategory.Normal && this.Plugin.Config.MlEnabledOn(category, ChatType.None)) {
2023-09-29 00:59:42 +00:00
return (category, Enum.GetName(category));
2021-03-31 13:46:47 +00:00
}
2020-08-04 22:22:57 +00:00
2021-05-15 22:19:27 +00:00
return (null, null);
}
2021-05-17 20:22:19 +00:00
private static int CountWords(string text) {
return text.Spacify().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
2020-08-04 22:22:57 +00:00
}
}