The easy to use and full featured Irc Bot everyone is talking about!
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Sed.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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" || 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. List<string> msgList = new List<string>();
  85. msgList.AddRange(LastMessages[key]);
  86. msgList.Reverse();
  87. foreach (string msg in msgList)
  88. {
  89. Regex messageRegex = new Regex(match, matchOptions);
  90. if (messageRegex.IsMatch(msg))
  91. {
  92. string newMessage = string.Empty;
  93. if (replaceNum < 0)
  94. {
  95. newMessage = messageRegex.Replace(msg, replace);
  96. }
  97. else
  98. {
  99. newMessage = messageRegex.Replace(msg, replace, replaceNum);
  100. }
  101. string replacedMessage = string.Format("\u0002{0}\u0002 meant to say: {1}", message.Sender.Nickname, newMessage);
  102. SendResponse(MessageType.Channel, message.Channel, message.Sender.Nickname, replacedMessage);
  103. foundResult = true;
  104. break;
  105. }
  106. }
  107. }
  108. MessageLock.ExitWriteLock();
  109. if (!foundResult)
  110. {
  111. string noMatch = string.Format("You do not have any previous messages that match \u0002{0}\u0002.", match);
  112. SendResponse(MessageType.Channel, message.Channel, message.Sender.Nickname, noMatch, true);
  113. }
  114. }
  115. else
  116. {
  117. // Add or replace the message for the user/channel
  118. MessageLock.EnterWriteLock();
  119. if (LastMessages.ContainsKey(key))
  120. {
  121. List<string> messages = LastMessages[key];
  122. if (messages.Count >= maxMessages)
  123. {
  124. messages.RemoveAt(0);
  125. }
  126. messages.Add(message.Message);
  127. LastMessages[key] = messages;
  128. }
  129. else
  130. {
  131. LastMessages.Add(key, new List<string> { message.Message });
  132. }
  133. MessageLock.ExitWriteLock();
  134. }
  135. }
  136. }
  137. }
  138. }