/// <summary> /// /// </summary> /// <param name="Message"></param> protected override void HandleSaidMessage(SaidMessage Message) { base.HandleSaidMessage(Message); // log chat from players to IRC if (IrcClient.IsRegistered && IrcChannel != null) { // build a str to log // this has a prefix (e.g. "103: ") and a m59 message string chatstr = IRCChatStyle.GetPrefixString(Config.ChatPrefix) + IRCChatStyle.CreateIRCMessageFromChatMessage(Message.Message); // try to log the chatmessage to IRC IrcClient.LocalUser.SendMessage(IrcChannel, chatstr); } }
/// <summary> /// Sends Message to IrcChannel, splitting if necessary to avoid /// IRC character limit. Uses a conservative split limit (280) /// due to color codes seeming to shorten the max length from 450 /// on EsperNet. /// </summary> /// <param name="Message"></param> private void SendSplitIRCChannelServerString(ServerString Message) { // Must have somewhere to send. if (!IrcClient.IsRegistered || IrcChannel == null) { return; } // build a str to log // this has a prefix (e.g. "103: ") and a m59 message string chatstr = IRCChatStyle.GetPrefixString(Config.ChatPrefix) + IRCChatStyle.CreateIRCMessageFromChatMessage(Message); // try to log the chatmessage to IRC // Short path if (chatstr.Length < 280) { IrcClient.LocalUser.SendMessage(IrcChannel, chatstr); return; } try { string lastStyle = string.Empty; int count = 0; while (chatstr.Length > 0 && count++ < 8) { string substr = chatstr.Truncate(IRCChatStyle.GetGoodTruncateIndex(chatstr, 280)); IrcClient.LocalUser.SendMessage(IrcChannel, lastStyle + substr); chatstr = chatstr.Substring(substr.Length); lastStyle = IRCChatStyle.GetLastChatStyleString(substr); } } catch (Exception e) { Log("ERROR", "Failed string splitting: " + e.Message); } }
/// <summary> /// /// </summary> /// <param name="Message"></param> protected override void HandleMessageMessage(MessageMessage Message) { base.HandleMessageMessage(Message); string s = Message.Message.FullString; const string prefix = "[###]"; const string ignore1 = "Your safety is now"; // starts with prefix and does not contain ignores if (s.IndexOf(prefix) == 0 && s.IndexOf(ignore1) == -1 && IrcClient.IsRegistered && IrcChannel != null) { // build a str to log // this has a prefix (e.g. "103: ") and a m59 message string chatstr = IRCChatStyle.GetPrefixString(Config.ChatPrefix) + IRCChatStyle.CreateIRCMessageFromChatMessage(Message.Message); // try to log the chatmessage to IRC IrcClient.LocalUser.SendMessage(IrcChannel, chatstr); } }
/// <summary> /// Invoked when a channel message was received /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void OnMessageReceived(object sender, IrcMessageEventArgs e) { // Ignore message whose length is just @prefix + @ + space length. if (e.Text == null || e.Text.Length <= Config.ChatPrefix.Length + 1 + 1) { return; } // try get channeluser by name IrcChannelUser usr = GetChannelUser(e.Source.Name); if (usr == null) { return; } string s = String.Empty; string banner = String.Empty; string ignoreSystemRegex = String.Empty; bool relayMsg = false; // Relay messages from allowed chatbots foreach (var relayBot in Config.RelayBots) { if (relayBot.Name.Contains(e.Source.Name)) { // Sanity check for our own name if (e.Source.Name.Equals(Config.NickName)) { return; } // Convert the IRC colors back to server styles/colors. s = IRCChatStyle.CreateChatMessageFromIRCMessage(e.Text); // Ignore any messages containing... if (!String.IsNullOrEmpty(relayBot.IgnoreAllRegex) && (Regex.Match(s, relayBot.IgnoreAllRegex)).Success) { return; } // Banner is the second string in the tuple. banner = relayBot.Banner; ignoreSystemRegex = relayBot.IgnoreSystemRegex; relayMsg = true; break; } } if (!relayMsg) { // Ignore messages without @prefix start. if (!relayMsg && e.Text.IndexOf("@" + Config.ChatPrefix) != 0) { return; } // only allowed users and admins can use the bot if (!(Config.AllowedUsers.Contains(e.Source.Name) || Config.Admins.Contains(e.Source.Name))) { // respond user he can't use this feature IrcClient.LocalUser.SendMessage( IrcChannel, e.Source.Name + " you can't use this feature."); return; } if (!IsUserRegistered(e.Source.Name)) { // Must have registered nickname. IrcClient.LocalUser.SendMessage(IrcChannel, e.Source.Name + " you must register your nickname to use this feature."); return; } // now remove the @103 start s = e.Text.Substring(Config.ChatPrefix.Length + 1 + 1); } // used delimiter const char delimiter = ' '; // split up into words string[] words = s.Split(delimiter); if (words.Length > 0) { if (Util.IsDisallowedDMCommand(words[0])) { IrcClient.LocalUser.SendMessage( IrcChannel, e.Source.Name + " you can't use this feature. This incident has been logged."); foreach (string admin in Config.Admins) { // try get channeluser by name usr = GetChannelUser(admin); // online? send! if (usr != null) { IrcClient.LocalUser.SendMessage(admin, String.Format("IRC User {0} has attempted to use a DM command", e.Source.Name)); } } return; } else if (words.Length > 3 && relayMsg) { // First word is the server's header (e.g. 103:) so don't use it. if (words[1].Contains("[###]")) { // Ignore system messages containing.... if (!String.IsNullOrEmpty(ignoreSystemRegex) && (Regex.Match(s, ignoreSystemRegex)).Success) { return; } // Adjust the color codes to display [###] correctly, drop the // existing [###] and add a fixed one here. Doesn't seem to be // possible to fix the one in the message itself. s = "dm gqemote " + banner + " ~U[###]~n ~U"; s += String.Join(delimiter.ToString(), words, 2, words.Length - 2); } else if ((Regex.Match(s, @"(broadcasts,|teilt allen)")).Success) { // Add server header, echo message. s = "dm gqemote " + banner + " ~n~w"; s += String.Join(delimiter.ToString(), words, 1, words.Length - 1); } else if (words[1].Contains("You") && words[2].Contains("broadcast,")) { // Echo messages that the other bot broadcasts to its server. s = "dm gqemote " + banner + " ~n~wHelp broadcasts,"; s += String.Join(delimiter.ToString(), words, 3, words.Length - 3); } else if (words.Length > 5 && words[1].Contains("Du") && words[3].Contains("allen")) { // German bot broadcasting to its server, echo the German translation. s = "dm gqemote " + banner + " ~n~wHelp teilt allen mit,"; s += String.Join(delimiter.ToString(), words, 5, words.Length - 5); } } else { switch (words[0]) { case ChatCommandBroadcast.KEY1: case ChatCommandBroadcast.KEY2: // keep first word s = String.Join(delimiter.ToString(), words, 0, 1); // insert banner + name s += delimiter + Config.Banner; s += e.Source.Name + ": ~n"; // add rest s += String.Join(delimiter.ToString(), words, 1, words.Length - 1); break; case ChatCommandTell.KEY1: case ChatCommandTell.KEY2: // keep first two word s = String.Join(delimiter.ToString(), words, 0, 2); // insert banner + name s += delimiter + Config.Banner; s += e.Source.Name + ": ~n"; // add rest s += String.Join(delimiter.ToString(), words, 2, words.Length - 2); break; } } } // enqueue it for execution ChatCommandQueue.Enqueue(s); }