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.

Version.cs 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Threading;
  5. using Combot.IRCServices.Messaging;
  6. namespace Combot.Modules.ModuleClasses
  7. {
  8. public class Version : Module
  9. {
  10. private List<VersionItem> versionList;
  11. private ReaderWriterLockSlim listLock;
  12. public override void Initialize()
  13. {
  14. listLock = new ReaderWriterLockSlim();
  15. versionList = new List<VersionItem>();
  16. Bot.IRC.Message.CTCPMessageRecievedEvent += HandleVersionQuery;
  17. Bot.IRC.Message.CTCPNoticeRecievedEvent += HandleVersionResponse;
  18. Bot.CommandReceivedEvent += HandleCommandEvent;
  19. }
  20. public override void ParseCommand(CommandMessage command)
  21. {
  22. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  23. if (foundCommand.Name == "Version Check")
  24. {
  25. VersionItem tmpItem = new VersionItem();
  26. tmpItem.Location = command.Location;
  27. tmpItem.MessageType = command.MessageType;
  28. tmpItem.Nick = command.Arguments["Nickname"];
  29. listLock.EnterWriteLock();
  30. if (versionList.Exists(item => item.Nick == command.Arguments["Nickname"]))
  31. {
  32. versionList.RemoveAll(item => item.Nick == command.Arguments["Nickname"]);
  33. }
  34. versionList.Add(tmpItem);
  35. listLock.ExitWriteLock();
  36. Bot.IRC.SendCTCPMessage(command.Arguments["Nickname"], "VERSION");
  37. }
  38. }
  39. public void HandleVersionQuery(object sender, CTCPMessage message)
  40. {
  41. if (message.Command.ToLower() == "version")
  42. {
  43. Bot.IRC.SendCTCPNotice(message.Sender.Nickname, "VERSION", string.Format("Combot v{0} on {1}", Assembly.GetExecutingAssembly().GetName().Version, GetOptionValue("Machine Reply")));
  44. }
  45. }
  46. public void HandleVersionResponse(object sender, CTCPMessage message)
  47. {
  48. if (message.Command == "VERSION")
  49. {
  50. listLock.EnterReadLock();
  51. VersionItem versionItem = versionList.Find(item => item.Nick.ToLower() == message.Sender.Nickname.ToLower());
  52. listLock.ExitReadLock();
  53. if (versionItem != null)
  54. {
  55. switch (versionItem.MessageType)
  56. {
  57. case MessageType.Channel:
  58. Bot.IRC.SendPrivateMessage(versionItem.Location, string.Format("[{0}] Using version: {1}", versionItem.Nick, message.Arguments));
  59. break;
  60. case MessageType.Query:
  61. Bot.IRC.SendPrivateMessage(message.Sender.Nickname, string.Format("[{0}] Using version: {1}", versionItem.Nick, message.Arguments));
  62. break;
  63. case MessageType.Notice:
  64. Bot.IRC.SendNotice(message.Sender.Nickname, string.Format("[{0}] Using version: {1}", versionItem.Nick, message.Arguments));
  65. break;
  66. }
  67. listLock.EnterWriteLock();
  68. versionList.RemoveAll(item => item.Nick == versionItem.Nick);
  69. listLock.ExitWriteLock();
  70. }
  71. }
  72. }
  73. private class VersionItem
  74. {
  75. public string Nick { get; set; }
  76. public string Location { get; set; }
  77. public MessageType MessageType { get; set; }
  78. public VersionItem()
  79. {
  80. Nick = string.Empty;
  81. Location = string.Empty;
  82. MessageType = MessageType.Channel;
  83. }
  84. }
  85. }
  86. }