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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. if (Commands.Find(cmd => cmd.Name == "Version Check").Triggers.Contains(command.Command))
  23. {
  24. VersionItem tmpItem = new VersionItem();
  25. tmpItem.Location = command.Location;
  26. tmpItem.LocationType = command.LocationType;
  27. tmpItem.Nick = command.Arguments["Nickname"];
  28. listLock.EnterWriteLock();
  29. if (versionList.Exists(item => item.Nick == command.Arguments["Nickname"]))
  30. {
  31. versionList.RemoveAll(item => item.Nick == command.Arguments["Nickname"]);
  32. }
  33. versionList.Add(tmpItem);
  34. listLock.ExitWriteLock();
  35. Bot.IRC.SendCTCPMessage(command.Arguments["Nickname"], "VERSION");
  36. }
  37. }
  38. public void HandleVersionQuery(object sender, CTCPMessage message)
  39. {
  40. if (message.Command.ToLower() == "version")
  41. {
  42. Bot.IRC.SendCTCPNotice(message.Sender.Nickname, "VERSION", string.Format("Combot v{0} on {1}", Assembly.GetExecutingAssembly().GetName().Version, GetOptionValue("Machine Reply")));
  43. }
  44. }
  45. public void HandleVersionResponse(object sender, CTCPMessage message)
  46. {
  47. if (message.Command == "VERSION")
  48. {
  49. listLock.EnterReadLock();
  50. VersionItem versionItem = versionList.Find(item => item.Nick == message.Sender.Nickname);
  51. listLock.ExitReadLock();
  52. if (versionItem != null)
  53. {
  54. switch (versionItem.LocationType)
  55. {
  56. case LocationType.Channel:
  57. Bot.IRC.SendPrivateMessage(versionItem.Location, string.Format("[{0}] Using version: {1}", versionItem.Nick, message.Arguments));
  58. break;
  59. case LocationType.Query:
  60. Bot.IRC.SendPrivateMessage(versionItem.Nick, string.Format("[{0}] Using version: {1}", versionItem.Nick, message.Arguments));
  61. break;
  62. case LocationType.Notice:
  63. Bot.IRC.SendNotice(versionItem.Nick, string.Format("[{0}] Using version: {1}", versionItem.Nick, message.Arguments));
  64. break;
  65. }
  66. listLock.EnterWriteLock();
  67. versionList.RemoveAll(item => item.Nick == versionItem.Nick);
  68. listLock.ExitWriteLock();
  69. }
  70. }
  71. }
  72. }
  73. public class VersionItem
  74. {
  75. public string Nick { get; set; }
  76. public string Location { get; set; }
  77. public LocationType LocationType { get; set; }
  78. public VersionItem()
  79. {
  80. Nick = string.Empty;
  81. Location = string.Empty;
  82. LocationType = LocationType.Channel;
  83. }
  84. }
  85. }