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.

Ping_Me.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 Ping_Me : Module
  10. {
  11. private List<PingItem> pingList;
  12. private ReaderWriterLockSlim listLock;
  13. public override void Initialize()
  14. {
  15. listLock = new ReaderWriterLockSlim();
  16. pingList = new List<PingItem>();
  17. Bot.IRC.Message.CTCPNoticeRecievedEvent += HandlePingResponse;
  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 == "Ping Me")
  24. {
  25. int epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
  26. PingItem tmpItem = new PingItem();
  27. tmpItem.Nick = command.Nick.Nickname;
  28. tmpItem.Location = command.Location;
  29. tmpItem.MessageType = command.MessageType;
  30. tmpItem.Timestamp = DateTime.Now;
  31. listLock.EnterWriteLock();
  32. if (pingList.Exists(item => item.Nick == command.Nick.Nickname))
  33. {
  34. pingList.RemoveAll(item => item.Nick == command.Nick.Nickname);
  35. }
  36. pingList.Add(tmpItem);
  37. listLock.ExitWriteLock();
  38. Bot.IRC.SendCTCPMessage(command.Nick.Nickname, "PING", epoch.ToString());
  39. }
  40. }
  41. private void HandlePingResponse(object sender, CTCPMessage e)
  42. {
  43. if (e.Command == "PING")
  44. {
  45. listLock.EnterReadLock();
  46. PingItem pingItem = pingList.Find(item => item.Nick == e.Sender.Nickname);
  47. listLock.ExitReadLock();
  48. if (pingItem != null)
  49. {
  50. DateTime curTime = DateTime.Now;
  51. DateTime prevTime = pingItem.Timestamp;
  52. TimeSpan difTime = curTime.Subtract(prevTime);
  53. string timeString = string.Empty;
  54. if (difTime.Days > 0)
  55. {
  56. timeString += difTime.Days.ToString() + " Days, ";
  57. }
  58. if (difTime.Hours > 0)
  59. {
  60. timeString += difTime.Hours.ToString() + " Hours, ";
  61. }
  62. if (difTime.Minutes > 0)
  63. {
  64. timeString += difTime.Minutes.ToString() + " Minutes, ";
  65. }
  66. if (difTime.Seconds > 0)
  67. {
  68. timeString += difTime.Seconds.ToString() + " Seconds, ";
  69. }
  70. if (difTime.Milliseconds > 0)
  71. {
  72. timeString += difTime.Milliseconds.ToString() + " Milliseconds";
  73. }
  74. switch (pingItem.MessageType)
  75. {
  76. case MessageType.Channel:
  77. Bot.IRC.SendPrivateMessage(pingItem.Location, string.Format("{0}, your ping is {1}", pingItem.Nick, timeString));
  78. break;
  79. case MessageType.Notice:
  80. Bot.IRC.SendNotice(pingItem.Nick, string.Format("Your ping is {0}", timeString));
  81. break;
  82. case MessageType.Query:
  83. Bot.IRC.SendPrivateMessage(pingItem.Nick, string.Format("Your ping is {0}", timeString));
  84. break;
  85. }
  86. listLock.EnterWriteLock();
  87. pingList.RemoveAll(item => item.Nick == pingItem.Nick);
  88. listLock.ExitWriteLock();
  89. }
  90. }
  91. }
  92. private class PingItem
  93. {
  94. public string Nick { get; set; }
  95. public string Location { get; set; }
  96. public MessageType MessageType { get; set; }
  97. public DateTime Timestamp { get; set; }
  98. public PingItem()
  99. {
  100. Nick = string.Empty;
  101. Location = string.Empty;
  102. MessageType = MessageType.Channel;
  103. Timestamp = DateTime.Now;
  104. }
  105. }
  106. }
  107. }