Skip to content
Permalink
Browse files

Chat filters should apply for in-game -> IRC chat

  • Loading branch information...
UnknownShadow200 committed Sep 26, 2018
1 parent 21f6dce commit c39642a5c45d0782c5678d5c8c3b7a2dc4738d4c
Showing with 34 additions and 51 deletions.
  1. +6 −6 fCraft/Commands/ChatCommands.cs
  2. +1 −0 fCraft/Network/IRC.cs
  3. +7 −11 fCraft/Player/Chat.cs
  4. +19 −33 fCraft/Player/ChatFilter.cs
  5. +1 −1 fCraft/System/Server.cs
@@ -855,10 +855,10 @@ static void TimerHandler(Player player, CommandReader cmd)
"Removes a filter with the given ID number. " +
"To see a list of filters and their IDs, type &H/filters" }
},
Handler = SwearHandler
Handler = FiltersHandler
};

private static void SwearHandler(Player player, CommandReader cmd) {
static void FiltersHandler(Player player, CommandReader cmd) {
if (!player.IsStaff || cmd.CountRemaining == 0) {
if (ChatFilter.Filters.Count == 0) {
player.Message("There are no filters.");
@@ -889,7 +889,7 @@ static void TimerHandler(Player player, CommandReader cmd)
ChatFilter dFilter = ChatFilter.Find(dID);
if (dFilter != null) {
if (cmd.IsConfirmed) {
ChatFilter.RemoveFilter(dID);
ChatFilter.Remove(dID);
Server.Message("&Y[Filters] {0}&Y removed the filter \"{1}\" -> \"{2}\"",
player.ClassyName, dFilter.Word, dFilter.Replacement);
break;
@@ -911,7 +911,7 @@ static void TimerHandler(Player player, CommandReader cmd)
}
if (!ChatFilter.Exists(word)) {
Server.Message("&Y[Filters] \"{0}\" is now replaced by \"{1}\"", word, replacement);
ChatFilter.CreateFilter(getNewFilterId(), word, replacement);
ChatFilter.Add(getNewFilterId(), word, replacement);
} else {
player.Message("A filter with that word already exists!");
}
@@ -948,12 +948,12 @@ static void TimerHandler(Player player, CommandReader cmd)
}
Server.Message("&Y[Filters] {0}&Y edited a filter from &N(\"{1}\" -> \"{2}\") &Nto (\"{3}\" -> \"{2}\")",
player.ClassyName, oldWord, oldReplacement, eFilter.Word, eFilter.Replacement);
ChatFilter.RemoveFilter(eID.ToString());
ChatFilter.Remove(eID.ToString());
ChatFilter.Filters.Add(eFilter);
break;
case "reload":
if (player.Info.Rank == RankManager.HighestRank) {
ChatFilter.ReloadAll();
ChatFilter.Reload();
player.Message("Reloaded filters from file");
}
break;
@@ -956,6 +956,7 @@ static string ProcessMessageToIRC([NotNull] string message)
if (message == null) throw new ArgumentNullException("message");
bool useColor = ConfigKey.IRCShowColorsFromServer.Enabled();
bool useEmotes = ConfigKey.IRCShowEmotesFromServer.Enabled();
message = Chat.Filter(message, null);

if (useEmotes)
message = Chat.ReplaceEmotesWithUnicode(message);
@@ -54,7 +54,13 @@ public static class Chat
}

public static string Filter(string rawMessage, Player player) {
if (player != null && !player.IsStaff) {
rawMessage = ChatFilter.Apply(rawMessage);
if (player == null || !player.IsStaff) {
rawMessage = RegexIPMatcher.Replace(rawMessage, "<Redacted IP>");
}
if (player == null) return rawMessage; // Checks below only work on in-game players

if (!player.IsStaff) {
string trimmedMsg = new string(rawMessage.Where(c => !char.IsWhiteSpace(c)).ToArray());
if (player.lastMsg.CaselessEquals(trimmedMsg)) {
if (player.lastMsgCount >= 2) {
@@ -67,16 +73,6 @@ public static class Chat
player.lastMsgCount = 0;
}
}

foreach (ChatFilter Swear in ChatFilter.Filters) {
if (rawMessage.CaselessContains(Swear.Word)) {
rawMessage = rawMessage.ReplaceString(Swear.Word, Swear.Replacement, StringComparison.InvariantCultureIgnoreCase);
}
}
if (player == null || !player.IsStaff) {
rawMessage = RegexIPMatcher.Replace(rawMessage, "<Redacted IP>");
}
if (player == null) return rawMessage; // Checks below only work in-game

if (rawMessage.Length >= 10 && player.Info.Rank.MaxCaps > 0) {
int caps = 0;
@@ -33,30 +33,39 @@ public sealed class ChatFilter {
return false;
}

public static void CreateFilter(int id, string word, string replacement) {
public static void Add(int id, string word, string replacement) {
ChatFilter filter = new ChatFilter();
filter.Id = id;
filter.Word = word;
filter.Replacement = replacement;
Filters.Add(filter);
SaveAll(false);
Save(false);
}

public static void RemoveFilter(string id) {
public static void Remove(string id) {
ChatFilter filter = Find(id);
if (filter != null) {
Filters.Remove(filter);
SaveAll(false);
Save(false);
}
}


public static void ReloadAll() {
public static string Apply(string msg) {
foreach (ChatFilter filter in Filters) {
if (msg.CaselessContains(filter.Word)) {
msg = msg.ReplaceString(filter.Word, filter.Replacement, StringComparison.InvariantCultureIgnoreCase);
}
}
return msg;
}

public static void Reload() {
Filters.Clear();
LoadAll();
Load();
}

public static void SaveAll(bool verbose) {
public static void Save(bool verbose) {
try {
Stopwatch sw = Stopwatch.StartNew();
using (Stream s = File.Create(Paths.FiltersFileName)) {
@@ -71,12 +80,7 @@ public sealed class ChatFilter {
}
}

public static void LoadAll() {
if (Directory.Exists("Filters")) {
OldLoad();
Directory.Delete("Filters", true);
SaveAll(false);
}
public static void Load() {
if (!File.Exists(Paths.FiltersFileName)) return;

try {
@@ -86,37 +90,19 @@ public sealed class ChatFilter {
}
int count = 0;
for (int i = 0; i < Filters.Count; i++) {
if (Filters[i] == null)
continue;
if (Filters[i] == null) continue;
// fixup for servicestack not writing out null entries
if (Filters[i].Word == null) {
Filters[i] = null; continue;
}
count++;
}
Logger.Log(LogType.SystemActivity, "ChatFilter.Load: Loaded " + count + " filters");
SaveAll(true);
Save(true);
} catch (Exception ex) {
Filters = null;
Logger.Log(LogType.Error, "ChatFilter.Load: " + ex);
}
}

public static void OldLoad() {
string[] files = Directory.GetFiles("./Filters");
foreach (string filename in files) {
if (Path.GetExtension(filename) != ".txt") continue;
string idString = Path.GetFileNameWithoutExtension(filename);
int id;
if (!int.TryParse(idString, out id)) continue;

string[] data = File.ReadAllLines(filename);
ChatFilter filter = new ChatFilter();
filter.Id = id;
filter.Word = data[0];
filter.Replacement = data[1];
Filters.Add(filter);
}
}
}
}
@@ -425,7 +425,7 @@ public static partial class Server {

ChatTimer.LoadAll();
Report.LoadAll();
ChatFilter.LoadAll();
ChatFilter.Load();
Entity.LoadAll();
DownloadResources();

0 comments on commit c39642a

Please sign in to comment.
You can’t perform that action at this time.