Skip to content

Commit c39642a

Browse files
Chat filters should apply for in-game -> IRC chat
1 parent 21f6dce commit c39642a

File tree

5 files changed

+34
-51
lines changed

5 files changed

+34
-51
lines changed

fCraft/Commands/ChatCommands.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -855,10 +855,10 @@ static void TimerHandler(Player player, CommandReader cmd)
855855
"Removes a filter with the given ID number. " +
856856
"To see a list of filters and their IDs, type &H/filters" }
857857
},
858-
Handler = SwearHandler
858+
Handler = FiltersHandler
859859
};
860860

861-
private static void SwearHandler(Player player, CommandReader cmd) {
861+
static void FiltersHandler(Player player, CommandReader cmd) {
862862
if (!player.IsStaff || cmd.CountRemaining == 0) {
863863
if (ChatFilter.Filters.Count == 0) {
864864
player.Message("There are no filters.");
@@ -889,7 +889,7 @@ private static void SwearHandler(Player player, CommandReader cmd) {
889889
ChatFilter dFilter = ChatFilter.Find(dID);
890890
if (dFilter != null) {
891891
if (cmd.IsConfirmed) {
892-
ChatFilter.RemoveFilter(dID);
892+
ChatFilter.Remove(dID);
893893
Server.Message("&Y[Filters] {0}&Y removed the filter \"{1}\" -> \"{2}\"",
894894
player.ClassyName, dFilter.Word, dFilter.Replacement);
895895
break;
@@ -911,7 +911,7 @@ private static void SwearHandler(Player player, CommandReader cmd) {
911911
}
912912
if (!ChatFilter.Exists(word)) {
913913
Server.Message("&Y[Filters] \"{0}\" is now replaced by \"{1}\"", word, replacement);
914-
ChatFilter.CreateFilter(getNewFilterId(), word, replacement);
914+
ChatFilter.Add(getNewFilterId(), word, replacement);
915915
} else {
916916
player.Message("A filter with that word already exists!");
917917
}
@@ -948,12 +948,12 @@ private static void SwearHandler(Player player, CommandReader cmd) {
948948
}
949949
Server.Message("&Y[Filters] {0}&Y edited a filter from &N(\"{1}\" -> \"{2}\") &Nto (\"{3}\" -> \"{2}\")",
950950
player.ClassyName, oldWord, oldReplacement, eFilter.Word, eFilter.Replacement);
951-
ChatFilter.RemoveFilter(eID.ToString());
951+
ChatFilter.Remove(eID.ToString());
952952
ChatFilter.Filters.Add(eFilter);
953953
break;
954954
case "reload":
955955
if (player.Info.Rank == RankManager.HighestRank) {
956-
ChatFilter.ReloadAll();
956+
ChatFilter.Reload();
957957
player.Message("Reloaded filters from file");
958958
}
959959
break;

fCraft/Network/IRC.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ static string ProcessMessageToIRC([NotNull] string message)
956956
if (message == null) throw new ArgumentNullException("message");
957957
bool useColor = ConfigKey.IRCShowColorsFromServer.Enabled();
958958
bool useEmotes = ConfigKey.IRCShowEmotesFromServer.Enabled();
959+
message = Chat.Filter(message, null);
959960

960961
if (useEmotes)
961962
message = Chat.ReplaceEmotesWithUnicode(message);

fCraft/Player/Chat.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ public static bool SendGlobal([NotNull] Player player, [NotNull] string rawMessa
5454
}
5555

5656
public static string Filter(string rawMessage, Player player) {
57-
if (player != null && !player.IsStaff) {
57+
rawMessage = ChatFilter.Apply(rawMessage);
58+
if (player == null || !player.IsStaff) {
59+
rawMessage = RegexIPMatcher.Replace(rawMessage, "<Redacted IP>");
60+
}
61+
if (player == null) return rawMessage; // Checks below only work on in-game players
62+
63+
if (!player.IsStaff) {
5864
string trimmedMsg = new string(rawMessage.Where(c => !char.IsWhiteSpace(c)).ToArray());
5965
if (player.lastMsg.CaselessEquals(trimmedMsg)) {
6066
if (player.lastMsgCount >= 2) {
@@ -67,16 +73,6 @@ public static string Filter(string rawMessage, Player player) {
6773
player.lastMsgCount = 0;
6874
}
6975
}
70-
71-
foreach (ChatFilter Swear in ChatFilter.Filters) {
72-
if (rawMessage.CaselessContains(Swear.Word)) {
73-
rawMessage = rawMessage.ReplaceString(Swear.Word, Swear.Replacement, StringComparison.InvariantCultureIgnoreCase);
74-
}
75-
}
76-
if (player == null || !player.IsStaff) {
77-
rawMessage = RegexIPMatcher.Replace(rawMessage, "<Redacted IP>");
78-
}
79-
if (player == null) return rawMessage; // Checks below only work in-game
8076

8177
if (rawMessage.Length >= 10 && player.Info.Rank.MaxCaps > 0) {
8278
int caps = 0;

fCraft/Player/ChatFilter.cs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,39 @@ public static bool Exists(string word) {
3333
return false;
3434
}
3535

36-
public static void CreateFilter(int id, string word, string replacement) {
36+
public static void Add(int id, string word, string replacement) {
3737
ChatFilter filter = new ChatFilter();
3838
filter.Id = id;
3939
filter.Word = word;
4040
filter.Replacement = replacement;
4141
Filters.Add(filter);
42-
SaveAll(false);
42+
Save(false);
4343
}
4444

45-
public static void RemoveFilter(string id) {
45+
public static void Remove(string id) {
4646
ChatFilter filter = Find(id);
4747
if (filter != null) {
4848
Filters.Remove(filter);
49-
SaveAll(false);
49+
Save(false);
5050
}
5151
}
5252

5353

54-
public static void ReloadAll() {
54+
public static string Apply(string msg) {
55+
foreach (ChatFilter filter in Filters) {
56+
if (msg.CaselessContains(filter.Word)) {
57+
msg = msg.ReplaceString(filter.Word, filter.Replacement, StringComparison.InvariantCultureIgnoreCase);
58+
}
59+
}
60+
return msg;
61+
}
62+
63+
public static void Reload() {
5564
Filters.Clear();
56-
LoadAll();
65+
Load();
5766
}
5867

59-
public static void SaveAll(bool verbose) {
68+
public static void Save(bool verbose) {
6069
try {
6170
Stopwatch sw = Stopwatch.StartNew();
6271
using (Stream s = File.Create(Paths.FiltersFileName)) {
@@ -71,12 +80,7 @@ public static void SaveAll(bool verbose) {
7180
}
7281
}
7382

74-
public static void LoadAll() {
75-
if (Directory.Exists("Filters")) {
76-
OldLoad();
77-
Directory.Delete("Filters", true);
78-
SaveAll(false);
79-
}
83+
public static void Load() {
8084
if (!File.Exists(Paths.FiltersFileName)) return;
8185

8286
try {
@@ -86,37 +90,19 @@ public static void LoadAll() {
8690
}
8791
int count = 0;
8892
for (int i = 0; i < Filters.Count; i++) {
89-
if (Filters[i] == null)
90-
continue;
93+
if (Filters[i] == null) continue;
9194
// fixup for servicestack not writing out null entries
9295
if (Filters[i].Word == null) {
9396
Filters[i] = null; continue;
9497
}
9598
count++;
9699
}
97100
Logger.Log(LogType.SystemActivity, "ChatFilter.Load: Loaded " + count + " filters");
98-
SaveAll(true);
101+
Save(true);
99102
} catch (Exception ex) {
100103
Filters = null;
101104
Logger.Log(LogType.Error, "ChatFilter.Load: " + ex);
102105
}
103106
}
104-
105-
public static void OldLoad() {
106-
string[] files = Directory.GetFiles("./Filters");
107-
foreach (string filename in files) {
108-
if (Path.GetExtension(filename) != ".txt") continue;
109-
string idString = Path.GetFileNameWithoutExtension(filename);
110-
int id;
111-
if (!int.TryParse(idString, out id)) continue;
112-
113-
string[] data = File.ReadAllLines(filename);
114-
ChatFilter filter = new ChatFilter();
115-
filter.Id = id;
116-
filter.Word = data[0];
117-
filter.Replacement = data[1];
118-
Filters.Add(filter);
119-
}
120-
}
121107
}
122108
}

fCraft/System/Server.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public static bool StartServer() {
425425

426426
ChatTimer.LoadAll();
427427
Report.LoadAll();
428-
ChatFilter.LoadAll();
428+
ChatFilter.Load();
429429
Entity.LoadAll();
430430
DownloadResources();
431431

0 commit comments

Comments
 (0)