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

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