Skip to content
6 changes: 6 additions & 0 deletions Razor/Core/Mobile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Collections.Generic;
using System.Text;
using Assistant.Agents;
using Assistant.Core;
using Assistant.UI;

namespace Assistant
Expand Down Expand Up @@ -597,6 +598,11 @@ internal void OverheadMessageFrom(int hue, string from, string format, params ob

internal void OverheadMessageFrom(int hue, string from, string text, bool ascii)
{
if (TextFilterManager.IsTextFiltered(text, TextFilterType.Overhead) != TextFilterResult.Allow)
{
return;
}

if (Config.GetBool("FilterOverheadMessages"))
{
if (!MessageQueue.Enqueue(this, hue, "O", text))
Expand Down
6 changes: 5 additions & 1 deletion Razor/Core/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,11 @@ internal void SendMessage(MsgLevel lvl, string text)
break;
}

SystemMessages.Add(text);
var filterResult = TextFilterManager.IsTextFiltered(text, TextFilterType.SysMessage);
if (filterResult != TextFilterResult.HideAndBlock)
SystemMessages.Add(text);
if (filterResult != TextFilterResult.Allow)
return;

if (Config.GetBool("FilterRazorMessages"))
{
Expand Down
117 changes: 90 additions & 27 deletions Razor/Core/TextFilterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,73 +20,140 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using Assistant.UI;

namespace Assistant.Core
{
public enum TextFilterResult
{
Allow,
Hide,
HideAndBlock
}

public enum TextFilterType
{
SysMessage,
Overhead,
Speech
}

public class TextFilterEntryModel
{
public string Text { get; set; }

public bool FilterSysMessages { get; set; }

public bool FilterOverhead { get; set; }

public bool FilterSpeech { get; set; }

public bool IgnoreFilteredMessageInScripts { get; set; }

public TextFilterEntryModel()
{
}

public TextFilterEntryModel(XmlElement element)
{
Text = element.GetAttribute("text");
FilterSpeech = ConvertToBool(element.GetAttribute("speech"), true);
FilterOverhead = ConvertToBool(element.GetAttribute("overhead"));
FilterSysMessages = ConvertToBool(element.GetAttribute("sysmessages"));
IgnoreFilteredMessageInScripts = ConvertToBool(element.GetAttribute("ignoreinscripts"), true);
}

private bool ConvertToBool(string attributeString, bool defaultValue = false)
{
if (string.IsNullOrWhiteSpace(attributeString))
return defaultValue;

return Convert.ToBoolean(attributeString);
}
}

public static class TextFilterManager
{
private static ListBox _filterTextList;

public static List<string> FilteredText = new List<string>();
public static List<TextFilterEntryModel> FilteredText = new List<TextFilterEntryModel>();

public static void SetControls(ListBox filterTextList)
{
filterTextList.DataSource = FilteredText;
filterTextList.DisplayMember = nameof(TextFilterEntryModel.Text);
_filterTextList = filterTextList;
}

public static void AddFilter(string filter)
public static void AddFilter(TextFilterEntryModel entry)
{
FilteredText.Add(entry);

RedrawList();
}

public static void RemoveFilter(int index)
{
FilteredText.Add(filter);
FilteredText.RemoveAt(index);

RedrawList();
}

public static void RemoveFilter(string filter)
public static void UpdateFilter(TextFilterEntryModel entry, int index)
{
FilteredText.Remove(filter);
FilteredText[index] = entry;

RedrawList();
}

public static void Save(XmlTextWriter xml)
{
foreach (var text in FilteredText)
foreach (var entry in FilteredText)
{
xml.WriteStartElement("filter");
xml.WriteAttributeString("text", text);
xml.WriteAttributeString("text", entry.Text);
xml.WriteAttributeString("sysmessages", entry.FilterSysMessages.ToString());
xml.WriteAttributeString("overhead", entry.FilterOverhead.ToString());
xml.WriteAttributeString("speech", entry.FilterSpeech.ToString());
xml.WriteAttributeString("ignoreinscripts", entry.IgnoreFilteredMessageInScripts.ToString());
xml.WriteEndElement();
}
}

public static bool IsFiltered(string text)
public static TextFilterResult IsTextFiltered(string text, TextFilterType type)
{
if (!Config.GetBool("EnableTextFilter"))
return false;
return TextFilterResult.Allow;

foreach (string filteredText in FilteredText)
foreach (var entry in FilteredText)
{
if (text.IndexOf(filteredText, StringComparison.OrdinalIgnoreCase) != -1)
if ((type == TextFilterType.Overhead && entry.FilterOverhead ||
type == TextFilterType.Speech && entry.FilterSpeech ||
type == TextFilterType.SysMessage && entry.FilterSysMessages) &&
text.IndexOf(entry.Text, StringComparison.OrdinalIgnoreCase) != -1)
{
return true;
return entry.IgnoreFilteredMessageInScripts ? TextFilterResult.HideAndBlock : TextFilterResult.Hide;
}
}

return false;
return TextFilterResult.Allow;
}

public static void Load(XmlElement node)
{
ClearAll();

try
{
foreach (XmlElement el in node.GetElementsByTagName("filter"))
foreach (var entry in node.ChildNodes.Cast<XmlElement>().Select(el => new TextFilterEntryModel(el)))
{
FilteredText.Add(Convert.ToString(el.GetAttribute("text")));
FilteredText.Add(entry);
}

RedrawList();
Expand All @@ -104,18 +171,14 @@ public static void ClearAll()

public static void RedrawList()
{
_filterTextList?.SafeAction(s =>
_filterTextList?.SafeAction((lb) =>
{
s.BeginUpdate();
s.Items.Clear();

foreach (string text in FilteredText)
{
s.Items.Add(text);
}

s.EndUpdate();
lb.BeginUpdate();
lb.DataSource = null;
lb.DataSource = FilteredText;
lb.DisplayMember = nameof(TextFilterEntryModel.Text);
lb.EndUpdate();
});
}
}
}
}
28 changes: 22 additions & 6 deletions Razor/Network/Handlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1998,8 +1998,12 @@ public static void HandleSpeech(Packet p, PacketHandlerEventArgs args, Serial se
World.Player.ResetCriminalTimer();
}

// Overhead message override
OverheadManager.DisplayOverheadMessage(text);
var filterResult = TextFilterManager.IsTextFiltered(text, TextFilterType.SysMessage);
if (filterResult != TextFilterResult.HideAndBlock)
{
// Overhead message override
OverheadManager.DisplayOverheadMessage(text);
}
}

if (Config.GetBool("ShowContainerLabels") && ser.IsItem)
Expand Down Expand Up @@ -2050,7 +2054,7 @@ public static void HandleSpeech(Packet p, PacketHandlerEventArgs args, Serial se
return;
}

if (ser.IsMobile && TextFilterManager.IsFiltered(text))
if (ser.IsMobile && TextFilterManager.IsTextFiltered(text, TextFilterType.Speech) != TextFilterResult.Allow)
{
args.Block = true;
return;
Expand All @@ -2063,11 +2067,23 @@ public static void HandleSpeech(Packet p, PacketHandlerEventArgs args, Serial se
}
}

if (!ser.IsValid || ser == World.Player.Serial || ser.IsItem)
{
if(ser.IsItem)
SystemMessages.Add(text);
}

// Invalid serial means system message, serial == player means overhead
if (ser == World.Player.Serial || !ser.IsValid)
{
var filterType = !ser.IsValid ? TextFilterType.SysMessage : TextFilterType.Overhead;
var filterResult = TextFilterManager.IsTextFiltered(text, filterType);
if (filterResult != TextFilterResult.HideAndBlock)
SystemMessages.Add(text);
if (filterResult != TextFilterResult.Allow)
{
args.Block = true;
return;
}
}

if (Config.GetBool("FilterSystemMessages") && ser == Serial.MinusOne || ser == Serial.Zero)
{
if (!MessageQueue.Enqueue(ser, null, body, type, hue, font, lang, name, text))
Expand Down
9 changes: 9 additions & 0 deletions Razor/Razor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,12 @@
<Compile Include="UI\SplashScreen.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="UI\TextFilterEntry.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="UI\TextFilterEntry.Designer.cs">
<DependentUpon>TextFilterEntry.cs</DependentUpon>
</Compile>
<Compile Include="UI\WelcomeForm.cs">
<SubType>Form</SubType>
</Compile>
Expand Down Expand Up @@ -576,6 +582,9 @@
<DependentUpon>SplashScreen.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="UI\TextFilterEntry.resx">
<DependentUpon>TextFilterEntry.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="UI\WelcomeForm.resx">
<DependentUpon>WelcomeForm.cs</DependentUpon>
</EmbeddedResource>
Expand Down
25 changes: 19 additions & 6 deletions Razor/UI/Razor.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions Razor/UI/Razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7285,19 +7285,23 @@ private void agentSetHotKey_Click(object sender, EventArgs e)
}
private void addFilterText_Click(object sender, EventArgs e)
{
if (InputBox.Show(this, "Enter text to filter", "Text Filter"))
{
string message = InputBox.GetString();
TextFilterManager.AddFilter(message);
}
new TextFilterEntry().ShowDialog(Engine.MainWindow);
}

private void editFilterText_Click(object sender, EventArgs e)
{
if (textFilterList.SelectedIndex < 0)
return;

new TextFilterEntry(textFilterList.SelectedItem as TextFilterEntryModel, textFilterList.SelectedIndex).ShowDialog(Engine.MainWindow);
}

private void removeFilterText_Click(object sender, EventArgs e)
{
if (textFilterList.SelectedIndex < 0)
return;

TextFilterManager.RemoveFilter((string) textFilterList.SelectedItem);
TextFilterManager.RemoveFilter(textFilterList.SelectedIndex);
}

private void enableTextFilter_CheckedChanged(object sender, EventArgs e)
Expand Down
Loading