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.

Spam_Control.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using Combot.IRCServices;
  6. using Combot.IRCServices.Messaging;
  7. namespace Combot.Modules.Plugins
  8. {
  9. public class Spam_Control : Module
  10. {
  11. private List<SpamMessageInfo> SpamMessageList;
  12. private List<SpamHighlightInfo> SpamHighlightList;
  13. private ReaderWriterLockSlim SpamMessageLock;
  14. private ReaderWriterLockSlim SpamHighlightLock;
  15. public override void Initialize()
  16. {
  17. SpamMessageList = new List<SpamMessageInfo>();
  18. SpamHighlightList = new List<SpamHighlightInfo>();
  19. SpamMessageLock = new ReaderWriterLockSlim();
  20. SpamHighlightLock = new ReaderWriterLockSlim();
  21. Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessage;
  22. }
  23. private void HandleChannelMessage(object sender, ChannelMessage message)
  24. {
  25. if (!ChannelBlacklist.Contains(message.Channel) && !NickBlacklist.Contains(message.Sender.Nickname))
  26. {
  27. // Check for line spam
  28. if (SpamMessageList.Exists(msg => msg.Channel == message.Channel && msg.Nick == message.Sender.Nickname))
  29. {
  30. SpamMessageLock.EnterReadLock();
  31. SpamMessageInfo info = SpamMessageList.Find(msg => msg.Channel == message.Channel && msg.Nick == message.Sender.Nickname);
  32. SpamMessageLock.ExitReadLock();
  33. TimeSpan difference = message.TimeStamp.Subtract(info.FirstMessageTime);
  34. if (difference.TotalMilliseconds < (int) GetOptionValue("Time Threshold"))
  35. {
  36. info.Lines++;
  37. if (info.Lines > (int) GetOptionValue("Max Messages"))
  38. {
  39. Bot.IRC.SendKick(info.Channel, info.Nick, string.Format("Please do not spam. You have messaged {0} times within {1}ms.", info.Lines, GetOptionValue("Time Threshold")));
  40. SpamMessageLock.EnterWriteLock();
  41. SpamMessageList.Remove(info);
  42. SpamMessageLock.ExitWriteLock();
  43. }
  44. }
  45. else
  46. {
  47. SpamMessageLock.EnterWriteLock();
  48. info.Lines = 1;
  49. info.FirstMessageTime = message.TimeStamp;
  50. SpamMessageLock.ExitWriteLock();
  51. }
  52. }
  53. else
  54. {
  55. SpamMessageInfo info = new SpamMessageInfo();
  56. info.Channel = message.Channel;
  57. info.Nick = message.Sender.Nickname;
  58. info.Lines = 1;
  59. info.FirstMessageTime = message.TimeStamp;
  60. SpamMessageLock.EnterWriteLock();
  61. SpamMessageList.Add(info);
  62. SpamMessageLock.ExitWriteLock();
  63. }
  64. // Check for highlight spam
  65. List<string> splitMessage = message.Message.Split(new [] {' '}, StringSplitOptions.RemoveEmptyEntries).ToList();
  66. Channel channel = Bot.IRC.Channels.Find(chan => chan.Name == message.Channel);
  67. if (channel != null)
  68. {
  69. List<string> foundNicks = splitMessage.FindAll(msg => channel.Nicks.Exists(nick => msg.Contains(nick.Nickname)));
  70. if (foundNicks.Any())
  71. {
  72. if (!SpamHighlightList.Exists(msg => msg.Channel == message.Channel && msg.Nick == message.Sender.Nickname))
  73. {
  74. SpamHighlightInfo newInfo = new SpamHighlightInfo();
  75. newInfo.Channel = message.Channel;
  76. newInfo.Nick = message.Sender.Nickname;
  77. newInfo.Highlights = 0;
  78. newInfo.FirstHighlightTime = message.TimeStamp;
  79. SpamHighlightLock.EnterWriteLock();
  80. SpamHighlightList.Add(newInfo);
  81. SpamHighlightLock.ExitWriteLock();
  82. }
  83. SpamHighlightLock.EnterReadLock();
  84. SpamHighlightInfo info = SpamHighlightList.Find(highlight => highlight.Channel == message.Channel && highlight.Nick == message.Sender.Nickname);
  85. SpamHighlightLock.ExitReadLock();
  86. TimeSpan difference = message.TimeStamp.Subtract(info.FirstHighlightTime);
  87. if (difference.TotalMilliseconds < (int) GetOptionValue("Time Threshold"))
  88. {
  89. info.Highlights += foundNicks.Count;
  90. if (info.Highlights > (int) GetOptionValue("Maximum Highlights"))
  91. {
  92. Bot.IRC.SendKick(info.Channel, info.Nick, string.Format("Please do not highlight spam. You have highlighted {0} nicks within {1}ms.", info.Highlights, GetOptionValue("Time Threshold")));
  93. SpamHighlightLock.EnterWriteLock();
  94. SpamHighlightList.Remove(info);
  95. SpamHighlightLock.ExitWriteLock();
  96. }
  97. }
  98. else
  99. {
  100. SpamHighlightLock.EnterWriteLock();
  101. info.Highlights = foundNicks.Count;
  102. info.FirstHighlightTime = message.TimeStamp;
  103. SpamHighlightLock.ExitWriteLock();
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }