The easy to use and full featured Irc Bot everyone is talking about!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sed.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using System.Threading;
  6. using Combot.Databases;
  7. using Combot.IRCServices.Messaging;
  8. namespace Combot.Modules.Plugins
  9. {
  10. public class Sed : Module
  11. {
  12. private Dictionary<string, List<string>> LastMessages;
  13. private readonly ReaderWriterLockSlim MessageLock = new ReaderWriterLockSlim();
  14. public override void Initialize()
  15. {
  16. LastMessages = new Dictionary<string, List<string>>();
  17. Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessageEvent;
  18. }
  19. private void HandleChannelMessageEvent(object sender, ChannelMessage message)
  20. {
  21. if (Enabled
  22. && !Bot.ServerConfig.ChannelBlacklist.Contains(message.Channel)
  23. && !Bot.ServerConfig.NickBlacklist.Contains(message.Sender.Nickname)
  24. && !ChannelBlacklist.Contains(message.Channel)
  25. && !NickBlacklist.Contains(message.Sender.Nickname))
  26. {
  27. bool nickEnabled = false;
  28. Boolean.TryParse(GetOptionValue("Nickname Enabled").ToString(), out nickEnabled);
  29. bool channelEnabled = false;
  30. Boolean.TryParse(GetOptionValue("Channel Enabled").ToString(), out channelEnabled);
  31. int maxMessages = 10;
  32. Int32.TryParse(GetOptionValue("Max Messages").ToString(), out maxMessages);
  33. string key = string.Empty;
  34. if (nickEnabled && channelEnabled)
  35. {
  36. key = string.Format("{0} {1}", message.Channel, message.Sender.Nickname);
  37. }
  38. else if (nickEnabled)
  39. {
  40. key = message.Sender.Nickname;
  41. }
  42. else if (channelEnabled)
  43. {
  44. key = message.Channel;
  45. }
  46. else
  47. {
  48. key = string.Empty;
  49. }
  50. Regex sedRegex = new Regex(@"^s\/(?<Match>[^\/\\]*(?:\\.[^\/\\]*)*)\/(?<Replace>[^\/\\]*(?:\\.[^\/\\]*)*)\/(?<Option>[g|I|0-9]*)?");
  51. if (sedRegex.IsMatch(message.Message))
  52. {
  53. Match sedMatch = sedRegex.Match(message.Message);
  54. string match = sedMatch.Groups["Match"].ToString().Replace(@"\/", @"/");
  55. string replace = sedMatch.Groups["Replace"].ToString().Replace(@"\/", @"/");
  56. string option = sedMatch.Groups["Option"].ToString();
  57. RegexOptions matchOptions;
  58. int optionVal;
  59. int replaceNum;
  60. if (int.TryParse(option, out optionVal))
  61. {
  62. matchOptions = RegexOptions.None;
  63. replaceNum = optionVal;
  64. }
  65. else if (option == "g")
  66. {
  67. matchOptions = RegexOptions.None;
  68. replaceNum = 1;
  69. }
  70. else if (option == "I")
  71. {
  72. matchOptions = RegexOptions.IgnoreCase;
  73. replaceNum = 1;
  74. }
  75. else
  76. {
  77. matchOptions = RegexOptions.None;
  78. replaceNum = 1;
  79. }
  80. bool foundResult = false;
  81. MessageLock.EnterWriteLock();
  82. if (LastMessages.ContainsKey(key))
  83. {
  84. foreach (string msg in LastMessages[key])
  85. {
  86. Regex messageRegex = new Regex(match, matchOptions);
  87. if (messageRegex.IsMatch(msg))
  88. {
  89. string newMessage = messageRegex.Replace(msg, replace, replaceNum);
  90. string replacedMessage = string.Format("\u0002{0}\u0002 meant to say: {1}", message.Sender.Nickname, newMessage);
  91. SendResponse(MessageType.Channel, message.Channel, message.Sender.Nickname, replacedMessage);
  92. foundResult = true;
  93. break;
  94. }
  95. }
  96. }
  97. MessageLock.ExitWriteLock();
  98. if (!foundResult)
  99. {
  100. string noMatch = string.Format("You do not have any previous messages that match \u0002{0}\u0002.", match);
  101. SendResponse(MessageType.Channel, message.Channel, message.Sender.Nickname, noMatch, true);
  102. }
  103. }
  104. else
  105. {
  106. // Add or replace the message for the user/channel
  107. MessageLock.EnterWriteLock();
  108. if (LastMessages.ContainsKey(key))
  109. {
  110. List<string> messages = LastMessages[key];
  111. if (messages.Count >= maxMessages)
  112. {
  113. messages.RemoveAt(0);
  114. }
  115. messages.Add(message.Message);
  116. LastMessages[key] = messages;
  117. }
  118. else
  119. {
  120. LastMessages.Add(key, new List<string> { message.Message });
  121. }
  122. MessageLock.ExitWriteLock();
  123. }
  124. }
  125. }
  126. }
  127. }