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 4.0KB

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