Skip to content
Permalink
Browse files

Apply chat and IP filters to messages from IRC.

  • Loading branch information...
UnknownShadow200 committed Jul 18, 2017
1 parent d836984 commit 8666b66aa85a549a8d50e690ee34634f7017db2d
Showing with 45 additions and 33 deletions.
  1. +8 −6 fCraft/Network/IRC.cs
  2. +37 −27 fCraft/Player/Chat.cs
@@ -373,7 +373,7 @@ void HandleMessage([NotNull] string message)

if (ConfigKey.IRCBotForwardFromIRC.Enabled()) {
if (msg.Type == IRCMessageType.ChannelAction) {
TryExpandUrls(text);
ProcessChatMessage(ref text);

foreach (Player player in Server.Players.Where(pl => pl.Info.ReadIRC)) {
player.Message("&I(IRC) * {0} {1}", msg.Nick, text);
@@ -383,7 +383,7 @@ void HandleMessage([NotNull] string message)
} else if (IRCHandlers.HandleCommand(ActualBotNick, msg.Nick, rawMessage)) {
} else if (IRCHandlers.HandlePM(ActualBotNick, msg.Nick, rawMessage)) {
} else {
TryExpandUrls(text);
ProcessChatMessage(ref text);
string discordBotNick = ConfigKey.DiscordBotNick.GetString();
string prefix = msg.Nick == discordBotNick ? "(Discord)" : "(IRC) " + msg.Nick;

@@ -394,7 +394,7 @@ void HandleMessage([NotNull] string message)
Logger.Log(LogType.IrcChat, "{0}: {1}: {2}", msg.Channel, msg.Nick,
IRCColorsAndNonStandardCharsExceptEmotes.Replace(rawMessage, ""));
} else if (msg.Message.StartsWith("#")) {
TryExpandUrls(text);
ProcessChatMessage(ref text);
string discordBotNick = ConfigKey.DiscordBotNick.GetString();
string prefix = msg.Nick == discordBotNick ? "(Discord)" : "(IRC) " + msg.Nick;

@@ -406,10 +406,12 @@ void HandleMessage([NotNull] string message)
}
}

void TryExpandUrls(string msg) {
void ProcessChatMessage(ref string message) {
message = Chat.Filter(message, null);
bool elapsed = DateTime.UtcNow.Subtract(IRCHandlers.lastUrlExpand).TotalSeconds > 5;
if (elapsed) {
Scheduler.NewTask(t => Chat.getUrls(msg)).RunOnce();
string chatMsg = message; // cannot use message because it is ref
Scheduler.NewTask(t => Chat.getUrls(chatMsg)).RunOnce();
IRCHandlers.lastUrlExpand = DateTime.UtcNow;
}
}
@@ -937,7 +939,7 @@ static string ProcessMessageFromIRC([NotNull] string message)
// strips minecraft colors and newlines
message = Color.StripColors(message, false);
}

message = Chat.UnescapeBackslashes(message);
return message.Trim();
}
@@ -28,11 +28,40 @@ public static class Chat
if (player == null) throw new ArgumentNullException("player");
if (rawMessage == null) throw new ArgumentNullException("rawMessage");

if (!player.IsStaff) {
rawMessage = Filter(rawMessage, player);
if (rawMessage == null) return false;

var recipientList = Server.Players.NotIgnoring(player);

string formattedMessage = string.Format("{0}&F: {1}",
player.ClassyName,
rawMessage);

var e = new ChatSendingEventArgs(player,
rawMessage,
formattedMessage,
ChatMessageType.Global,
recipientList);


if (!SendInternal(e)) return false;

Scheduler.NewTask(t => getUrls(rawMessage)).RunOnce();

rawMessage = Color.StripColors(rawMessage, false);
checkBotResponses(player, rawMessage);

Logger.Log(LogType.GlobalChat,
"(global){0}: {1}", player.Info.Rank.Color + player.Name + Color.White, rawMessage);
return true;
}

public static string Filter(string rawMessage, Player player) {
if (player != null && !player.IsStaff) {
if (player.LastMessage == new string(rawMessage.ToLower().Where(c => !char.IsWhiteSpace(c)).ToArray())) {
if (player.MessageSpam >= 2) {
player.Message("Please refrain from repeating yourself!");
return false;
return null;
}
player.MessageSpam++;
} else {
@@ -46,16 +75,19 @@ public static class Chat
rawMessage = rawMessage.ReplaceString(Swear.Word, Swear.Replacement, StringComparison.InvariantCultureIgnoreCase);
}
}
if (!player.IsStaff) {
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;
for (int i = 0; i < rawMessage.Length; i++) {
if (char.IsUpper(rawMessage[i])) {
caps++;
}
}

if (player.Info.Rank.MaxCaps == 1) {
if (caps > (rawMessage.Length / 2)) {
rawMessage = rawMessage.ToLower().UppercaseFirst();
@@ -66,35 +98,13 @@ public static class Chat
player.Message("Max uppercase letters reached. Message set to lowercase");
}
}

if (player.ChatRainbows) {
rawMessage = Colorize(rawMessage, RainbowChars);
} else if (player.ChatBWRainbows) {
rawMessage = Colorize(rawMessage, BWRainbowChars);
}

var recipientList = Server.Players.NotIgnoring(player);

string formattedMessage = string.Format("{0}&F: {1}",
player.ClassyName,
rawMessage);

var e = new ChatSendingEventArgs(player,
rawMessage,
formattedMessage,
ChatMessageType.Global,
recipientList);


if (!SendInternal(e)) return false;

Scheduler.NewTask(t => getUrls(rawMessage)).RunOnce();

rawMessage = Color.StripColors(rawMessage, false);
checkBotResponses(player, rawMessage);

Logger.Log(LogType.GlobalChat,
"(global){0}: {1}", player.Info.Rank.Color + player.Name + Color.White, rawMessage);
return true;
return rawMessage;
}

public static void getUrls(string rawMessage) {

0 comments on commit 8666b66

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