Skip to content

Commit

Permalink
Chat filters should apply for in-game -> IRC chat
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Sep 26, 2018
1 parent 21f6dce commit c39642a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 51 deletions.
12 changes: 6 additions & 6 deletions fCraft/Commands/ChatCommands.cs
Expand Up @@ -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.");
Expand Down Expand Up @@ -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;
Expand All @@ -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!");
}
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions fCraft/Network/IRC.cs
Expand Up @@ -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);
Expand Down
18 changes: 7 additions & 11 deletions fCraft/Player/Chat.cs
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
52 changes: 19 additions & 33 deletions fCraft/Player/ChatFilter.cs
Expand Up @@ -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)) {
Expand All @@ -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 {
Expand All @@ -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);
}
}
}
}
2 changes: 1 addition & 1 deletion fCraft/System/Server.cs
Expand Up @@ -425,7 +425,7 @@ public static partial class Server {

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

Expand Down

0 comments on commit c39642a

Please sign in to comment.