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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 (Enabled && !ChannelBlacklist.Contains(message.Channel) && !NickBlacklist.Contains(message.Sender.Nickname))
  26. {
  27. int timeThreshold = Convert.ToInt32(GetOptionValue("Time Threshold"));
  28. int maxMessages = Convert.ToInt32(GetOptionValue("Max Messages"));
  29. int maxHighlights = Convert.ToInt32(GetOptionValue("Max Highlights"));
  30. // Check for line spam
  31. if (SpamMessageList.Exists(msg => msg.Channel == message.Channel && msg.Nick == message.Sender.Nickname))
  32. {
  33. SpamMessageLock.EnterReadLock();
  34. SpamMessageInfo info = SpamMessageList.Find(msg => msg.Channel == message.Channel && msg.Nick == message.Sender.Nickname);
  35. SpamMessageLock.ExitReadLock();
  36. TimeSpan difference = message.TimeStamp.Subtract(info.FirstMessageTime);
  37. if (difference.TotalMilliseconds < timeThreshold)
  38. {
  39. info.Lines++;
  40. if (info.Lines > maxMessages)
  41. {
  42. Bot.IRC.Command.SendKick(info.Channel, info.Nick, string.Format("Please do not spam. You have messaged {0} times within {1}ms.", info.Lines, timeThreshold));
  43. SpamMessageLock.EnterWriteLock();
  44. SpamMessageList.Remove(info);
  45. SpamMessageLock.ExitWriteLock();
  46. }
  47. }
  48. else
  49. {
  50. SpamMessageLock.EnterWriteLock();
  51. info.Lines = 1;
  52. info.FirstMessageTime = message.TimeStamp;
  53. SpamMessageLock.ExitWriteLock();
  54. }
  55. }
  56. else
  57. {
  58. SpamMessageInfo info = new SpamMessageInfo();
  59. info.Channel = message.Channel;
  60. info.Nick = message.Sender.Nickname;
  61. info.Lines = 1;
  62. info.FirstMessageTime = message.TimeStamp;
  63. SpamMessageLock.EnterWriteLock();
  64. SpamMessageList.Add(info);
  65. SpamMessageLock.ExitWriteLock();
  66. }
  67. // Check for highlight spam
  68. List<string> splitMessage = message.Message.Split(new [] {' '}, StringSplitOptions.RemoveEmptyEntries).ToList();
  69. Channel channel = Bot.IRC.Channels.Find(chan => chan.Name == message.Channel);
  70. if (channel != null)
  71. {
  72. List<string> foundNicks = splitMessage.FindAll(msg => channel.Nicks.Exists(nick => msg.Contains(nick.Nickname)));
  73. if (foundNicks.Any())
  74. {
  75. if (!SpamHighlightList.Exists(msg => msg.Channel == message.Channel && msg.Nick == message.Sender.Nickname))
  76. {
  77. SpamHighlightInfo newInfo = new SpamHighlightInfo();
  78. newInfo.Channel = message.Channel;
  79. newInfo.Nick = message.Sender.Nickname;
  80. newInfo.Highlights = 0;
  81. newInfo.FirstHighlightTime = message.TimeStamp;
  82. SpamHighlightLock.EnterWriteLock();
  83. SpamHighlightList.Add(newInfo);
  84. SpamHighlightLock.ExitWriteLock();
  85. }
  86. SpamHighlightLock.EnterReadLock();
  87. SpamHighlightInfo info = SpamHighlightList.Find(highlight => highlight.Channel == message.Channel && highlight.Nick == message.Sender.Nickname);
  88. SpamHighlightLock.ExitReadLock();
  89. TimeSpan difference = message.TimeStamp.Subtract(info.FirstHighlightTime);
  90. if (difference.TotalMilliseconds < timeThreshold)
  91. {
  92. info.Highlights += foundNicks.Count;
  93. if (info.Highlights > maxHighlights)
  94. {
  95. Bot.IRC.Command.SendKick(info.Channel, info.Nick, string.Format("Please do not highlight spam. You have highlighted {0} nicks within {1}ms.", info.Highlights, timeThreshold));
  96. SpamHighlightLock.EnterWriteLock();
  97. SpamHighlightList.Remove(info);
  98. SpamHighlightLock.ExitWriteLock();
  99. }
  100. }
  101. else
  102. {
  103. SpamHighlightLock.EnterWriteLock();
  104. info.Highlights = foundNicks.Count;
  105. info.FirstHighlightTime = message.TimeStamp;
  106. SpamHighlightLock.ExitWriteLock();
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }