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.

Seen.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Combot.Databases;
  5. namespace Combot.Modules.Plugins
  6. {
  7. public class Seen : Module
  8. {
  9. public override void Initialize()
  10. {
  11. Bot.CommandReceivedEvent += HandleCommandEvent;
  12. }
  13. public override void ParseCommand(CommandMessage command)
  14. {
  15. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  16. switch (foundCommand.Name)
  17. {
  18. case "Seen":
  19. GetLastSeen(command);
  20. break;
  21. }
  22. }
  23. private void GetLastSeen(CommandMessage command)
  24. {
  25. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : null;
  26. Database database = new Database(Bot.ServerConfig.Database);
  27. List<Dictionary<string, object>> channelList = GetChannelList(database, channel, command.Arguments["Nickname"]);
  28. List<Dictionary<string, object>> partList = GetPartList(database, channel, command.Arguments["Nickname"]);
  29. List<Dictionary<string, object>> joinList = GetJoinList(database, channel, command.Arguments["Nickname"]);
  30. List<Dictionary<string, object>> kickList = GetKickList(database, channel, command.Arguments["Nickname"]);
  31. List<Dictionary<string, object>> quitList = GetQuitList(database, command.Arguments["Nickname"]);
  32. List<Dictionary<DateTime, string>> lastSeenList = new List<Dictionary<DateTime, string>>();
  33. if (channelList.Any())
  34. {
  35. DateTime chanTime = (DateTime)channelList.First()["date_added"];
  36. TimeSpan difference = DateTime.Now.Subtract(chanTime);
  37. string message = string.Format("I last saw \u0002{0}\u0002 {1} ago saying the following in \u0002{2}\u0002: {3}", command.Arguments["Nickname"], ConvertToDifference(difference), channelList.First()["name"], channelList.First()["message"]);
  38. lastSeenList.Add(new Dictionary<DateTime, string>() { { chanTime, message } });
  39. }
  40. if (partList.Any())
  41. {
  42. DateTime partTime = (DateTime)partList.First()["date_added"];
  43. TimeSpan difference = DateTime.Now.Subtract(partTime);
  44. string message = string.Format("I last saw \u0002{0}\u0002 {1} ago leaving \u0002{2}\u0002.", command.Arguments["Nickname"], ConvertToDifference(difference), channelList.First()["name"]);
  45. lastSeenList.Add(new Dictionary<DateTime, string>() { { partTime, message } });
  46. }
  47. if (joinList.Any())
  48. {
  49. DateTime joinTime = (DateTime)joinList.First()["date_added"];
  50. TimeSpan difference = DateTime.Now.Subtract(joinTime);
  51. string message = string.Format("I last saw \u0002{0}\u0002 {1} ago joining \u0002{2}\u0002.", command.Arguments["Nickname"], ConvertToDifference(difference), channelList.First()["name"]);
  52. lastSeenList.Add(new Dictionary<DateTime, string>() { { joinTime, message } });
  53. }
  54. if (kickList.Any())
  55. {
  56. DateTime kickTime = (DateTime)kickList.First()["date_added"];
  57. TimeSpan difference = DateTime.Now.Subtract(kickTime);
  58. string message = string.Format("I last saw \u0002{0}\u0002 {1} ago being kicked from \u0002{2}\u0002 with the reason: {3}", command.Arguments["Nickname"], ConvertToDifference(difference), channelList.First()["name"], kickList.First()["message"]);
  59. lastSeenList.Add(new Dictionary<DateTime, string>() { { kickTime, message } });
  60. }
  61. if (quitList.Any())
  62. {
  63. DateTime quitTime = (DateTime)quitList.First()["date_added"];
  64. TimeSpan difference = DateTime.Now.Subtract(quitTime);
  65. string message = string.Format("I last saw \u0002{0}\u0002 {1} ago quiting.", command.Arguments["Nickname"], ConvertToDifference(difference));
  66. lastSeenList.Add(new Dictionary<DateTime, string>() { { quitTime, message } });
  67. }
  68. if (lastSeenList.Count > 0)
  69. {
  70. DateTime bestTime = new DateTime(1990);
  71. string seenMessage = string.Empty;
  72. for (int i = 0; i < lastSeenList.Count; i++)
  73. {
  74. if (lastSeenList[i].Keys.First().CompareTo(bestTime) > 0)
  75. {
  76. bestTime = lastSeenList[i].Keys.First();
  77. seenMessage = lastSeenList[i].Values.First();
  78. }
  79. }
  80. switch (command.MessageType)
  81. {
  82. case MessageType.Channel:
  83. Bot.IRC.SendPrivateMessage(command.Location, seenMessage);
  84. break;
  85. case MessageType.Query:
  86. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, seenMessage);
  87. break;
  88. case MessageType.Notice:
  89. Bot.IRC.SendNotice(command.Nick.Nickname, seenMessage);
  90. break;
  91. }
  92. }
  93. else
  94. {
  95. string notFound = string.Empty;
  96. if (channel != null)
  97. {
  98. notFound = string.Format("I have not seen \u0002{0}\u0002 in \u0002{1}\u0002.", command.Arguments["Nickname"], channel);
  99. }
  100. else
  101. {
  102. notFound = string.Format("I have not seen \u0002{0}\u0002.", command.Arguments["Nickname"]);
  103. }
  104. switch (command.MessageType)
  105. {
  106. case MessageType.Channel:
  107. Bot.IRC.SendPrivateMessage(command.Location, notFound);
  108. break;
  109. case MessageType.Query:
  110. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, notFound);
  111. break;
  112. case MessageType.Notice:
  113. Bot.IRC.SendNotice(command.Nick.Nickname, notFound);
  114. break;
  115. }
  116. }
  117. }
  118. private List<Dictionary<string, object>> GetChannelList(Database database, string channel, string nickname)
  119. {
  120. if (channel != null)
  121. {
  122. string search = "SELECT `channelmessages`.`message`, `channelmessages`.`date_added`, `channels`.`name` FROM `channelmessages` " +
  123. "INNER JOIN `nicks` " +
  124. "ON `channelmessages`.`nick_id` = `nicks`.`id` " +
  125. "INNER JOIN `channels` " +
  126. "ON `channelmessages`.`channel_id` = `channels`.`id` " +
  127. "INNER JOIN `servers` " +
  128. "ON `channelmessages`.`server_id` = `servers`.`id` " +
  129. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2} " +
  130. "ORDER BY date_added DESC LIMIT 1";
  131. return database.Query(search, new object[] {Bot.ServerConfig.Name, channel, nickname});
  132. }
  133. else
  134. {
  135. string search = "SELECT `channelmessages`.`message`, `channelmessages`.`date_added`, `channels`.`name` FROM `channelmessages` " +
  136. "INNER JOIN `nicks` " +
  137. "ON `channelmessages`.`nick_id` = `nicks`.`id` " +
  138. "INNER JOIN `channels` " +
  139. "ON `channelmessages`.`channel_id` = `channels`.`id` " +
  140. "INNER JOIN `servers` " +
  141. "ON `channelmessages`.`server_id` = `servers`.`id` " +
  142. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1} " +
  143. "ORDER BY date_added DESC LIMIT 1";
  144. return database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
  145. }
  146. }
  147. private List<Dictionary<string, object>> GetPartList(Database database, string channel, string nickname)
  148. {
  149. if (channel != null)
  150. {
  151. string search = "SELECT `channelparts`.`date_added`, `channels`.`name` FROM `channelparts` " +
  152. "INNER JOIN `nicks` " +
  153. "ON `channelparts`.`nick_id` = `nicks`.`id` " +
  154. "INNER JOIN `channels` " +
  155. "ON `channelparts`.`channel_id` = `channels`.`id` " +
  156. "INNER JOIN `servers` " +
  157. "ON `channelparts`.`server_id` = `servers`.`id` " +
  158. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2} " +
  159. "ORDER BY date_added DESC LIMIT 1";
  160. return database.Query(search, new object[] {Bot.ServerConfig.Name, channel, nickname});
  161. }
  162. else
  163. {
  164. string search = "SELECT `channelparts`.`date_added`, `channels`.`name` FROM `channelparts` " +
  165. "INNER JOIN `nicks` " +
  166. "ON `channelparts`.`nick_id` = `nicks`.`id` " +
  167. "INNER JOIN `channels` " +
  168. "ON `channelparts`.`channel_id` = `channels`.`id` " +
  169. "INNER JOIN `servers` " +
  170. "ON `channelparts`.`server_id` = `servers`.`id` " +
  171. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1} " +
  172. "ORDER BY date_added DESC LIMIT 1";
  173. return database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
  174. }
  175. }
  176. private List<Dictionary<string, object>> GetJoinList(Database database, string channel, string nickname)
  177. {
  178. if (channel != null)
  179. {
  180. string search = "SELECT `channeljoins`.`date_added`, `channels`.`name` FROM `channeljoins` " +
  181. "INNER JOIN `nicks` " +
  182. "ON `channeljoins`.`nick_id` = `nicks`.`id` " +
  183. "INNER JOIN `channels` " +
  184. "ON `channeljoins`.`channel_id` = `channels`.`id` " +
  185. "INNER JOIN `servers` " +
  186. "ON `channeljoins`.`server_id` = `servers`.`id` " +
  187. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2} " +
  188. "ORDER BY date_added DESC LIMIT 1";
  189. return database.Query(search, new object[] {Bot.ServerConfig.Name, channel, nickname});
  190. }
  191. else
  192. {
  193. string search = "SELECT `channeljoins`.`date_added`, `channels`.`name` FROM `channeljoins` " +
  194. "INNER JOIN `nicks` " +
  195. "ON `channeljoins`.`nick_id` = `nicks`.`id` " +
  196. "INNER JOIN `channels` " +
  197. "ON `channeljoins`.`channel_id` = `channels`.`id` " +
  198. "INNER JOIN `servers` " +
  199. "ON `channeljoins`.`server_id` = `servers`.`id` " +
  200. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1} " +
  201. "ORDER BY date_added DESC LIMIT 1";
  202. return database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
  203. }
  204. }
  205. private List<Dictionary<string, object>> GetKickList(Database database, string channel, string nickname)
  206. {
  207. if (channel != null)
  208. {
  209. string search = "SELECT `channelkicks`.`date_added`, `channelkicks`.`reason`, `channels`.`name` FROM `channelkicks` " +
  210. "INNER JOIN `nicks` " +
  211. "ON `channelkicks`.`kicked_nick_id` = `nicks`.`id` " +
  212. "INNER JOIN `channels` " +
  213. "ON `channelkicks`.`channel_id` = `channels`.`id` " +
  214. "INNER JOIN `servers` " +
  215. "ON `channelkicks`.`server_id` = `servers`.`id` " +
  216. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2} " +
  217. "ORDER BY date_added DESC LIMIT 1";
  218. return database.Query(search, new object[] {Bot.ServerConfig.Name, channel, nickname});
  219. }
  220. else
  221. {
  222. string search = "SELECT `channelkicks`.`date_added`, `channelkicks`.`reason`, `channels`.`name` FROM `channelkicks` " +
  223. "INNER JOIN `nicks` " +
  224. "ON `channelkicks`.`kicked_nick_id` = `nicks`.`id` " +
  225. "INNER JOIN `channels` " +
  226. "ON `channelkicks`.`channel_id` = `channels`.`id` " +
  227. "INNER JOIN `servers` " +
  228. "ON `channelkicks`.`server_id` = `servers`.`id` " +
  229. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1} " +
  230. "ORDER BY date_added DESC LIMIT 1";
  231. return database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
  232. }
  233. }
  234. private List<Dictionary<string, object>> GetQuitList(Database database, string nickname)
  235. {
  236. string search = "SELECT `quits`.`date_added` FROM `quits` " +
  237. "INNER JOIN `nicks` " +
  238. "ON `quits`.`nick_id` = `nicks`.`id` " +
  239. "INNER JOIN `servers` " +
  240. "ON `quits`.`server_id` = `servers`.`id` " +
  241. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1} " +
  242. "ORDER BY date_added DESC LIMIT 1";
  243. return database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
  244. }
  245. private string ConvertToDifference(TimeSpan time)
  246. {
  247. string timeString = string.Empty;
  248. if (time.Days != 0)
  249. {
  250. string plural = string.Empty;
  251. if (time.Days > 1)
  252. {
  253. plural = "s";
  254. }
  255. timeString += string.Format("{0} Day{1} ", time.Days, plural);
  256. }
  257. if (time.Hours != 0)
  258. {
  259. string plural = string.Empty;
  260. if (time.Hours > 1)
  261. {
  262. plural = "s";
  263. }
  264. timeString += string.Format("{0} Hour{1} ", time.Hours, plural);
  265. }
  266. if (time.Minutes != 0)
  267. {
  268. string plural = string.Empty;
  269. if (time.Minutes > 1)
  270. {
  271. plural = "s";
  272. }
  273. timeString += string.Format("{0} Minute{1} ", time.Minutes, plural);
  274. }
  275. if (time.Seconds != 0)
  276. {
  277. string plural = string.Empty;
  278. if (time.Seconds > 1)
  279. {
  280. plural = "s";
  281. }
  282. timeString += string.Format("{0} Second{1} ", time.Seconds, plural);
  283. }
  284. return timeString.Trim();
  285. }
  286. }
  287. }