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.

Messaging.cs 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Combot.Databases;
  5. using Combot.IRCServices;
  6. using Combot.IRCServices.Messaging;
  7. using System.IO;
  8. namespace Combot.Modules.Plugins
  9. {
  10. public class Messaging : Module
  11. {
  12. public override void Initialize()
  13. {
  14. InitializeTable();
  15. Bot.CommandReceivedEvent += HandleCommandEvent;
  16. Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessage;
  17. Bot.IRC.Message.PrivateMessageReceivedEvent += HandlePrivateMessage;
  18. Bot.IRC.Message.ChannelNoticeReceivedEvent += HandleChannelNotice;
  19. Bot.IRC.Message.PrivateNoticeReceivedEvent += HandlePrivateNotice;
  20. }
  21. private void InitializeTable()
  22. {
  23. string sqlPath = Path.Combine(Directory.GetCurrentDirectory(), ConfigPath, "CreateTable.sql");
  24. if (File.Exists(sqlPath))
  25. {
  26. string query = File.ReadAllText(sqlPath);
  27. Bot.Database.Execute(query);
  28. }
  29. }
  30. public override void ParseCommand(CommandMessage command)
  31. {
  32. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  33. switch (foundCommand.Name)
  34. {
  35. case "Message":
  36. AddMessage(command);
  37. break;
  38. case "Anonymous Message":
  39. AddMessage(command, true);
  40. break;
  41. }
  42. }
  43. private void HandleChannelMessage(object sender, ChannelMessage message)
  44. {
  45. if (Enabled)
  46. {
  47. CheckMessages(message.Sender.Nickname);
  48. }
  49. }
  50. private void HandlePrivateMessage(object sender, PrivateMessage message)
  51. {
  52. if (Enabled)
  53. {
  54. CheckMessages(message.Sender.Nickname);
  55. }
  56. }
  57. private void HandleChannelNotice(object sender, ChannelNotice message)
  58. {
  59. if (Enabled)
  60. {
  61. CheckMessages(message.Sender.Nickname);
  62. }
  63. }
  64. private void HandlePrivateNotice(object sender, PrivateNotice message)
  65. {
  66. if (Enabled)
  67. {
  68. CheckMessages(message.Sender.Nickname);
  69. }
  70. }
  71. private void AddMessage(CommandMessage command, bool anonymous = false)
  72. {
  73. List<Dictionary<string, object>> currentMessages = GetSentMessages(command.Arguments["Nickname"], command.Nick.Nickname);
  74. int numMessages = currentMessages.Count();
  75. int maxMessages = Convert.ToInt32(GetOptionValue("Max Messages"));
  76. if (numMessages < maxMessages)
  77. {
  78. AddNick(command.Nick);
  79. Nick newNick = new Nick();
  80. Channel foundChannel = Bot.IRC.Channels.Find(chan => chan.Nicks.Exists(nick => nick.Nickname == command.Arguments["Nickname"]));
  81. if (foundChannel != null)
  82. {
  83. newNick = foundChannel.GetNick(command.Arguments["Nickname"]);
  84. }
  85. else
  86. {
  87. newNick.Nickname = command.Arguments["Nickname"];
  88. }
  89. AddNick(newNick);
  90. string query = "INSERT INTO `messages` SET " +
  91. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  92. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nicks`.`nickname` = {2}), " +
  93. "`sender_nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nicks`.`nickname` = {4}), " +
  94. "`message` = {5}, " +
  95. "`anonymous` = {6}, " +
  96. "`date_posted` = {7}";
  97. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, command.Arguments["Nickname"], Bot.ServerConfig.Name, command.Nick.Nickname, command.Arguments["Message"], anonymous, command.TimeStamp });
  98. string message = string.Format("I will send your message to \u0002{0}\u0002 as soon as I see them.", command.Arguments["Nickname"]);
  99. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, message);
  100. }
  101. else
  102. {
  103. string maxMessage = string.Format("You already have sent the maximum number of messages to \u0002{0}\u0002. Wait until they have read their messages before sending another.", command.Arguments["Nickname"]);
  104. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, maxMessage, true);
  105. }
  106. }
  107. private void CheckMessages(string nickname)
  108. {
  109. List<Dictionary<string, object>> receivedMessages = GetReceivedMessages(nickname);
  110. if (receivedMessages.Any())
  111. {
  112. for (int i = 0; i < receivedMessages.Count; i++)
  113. {
  114. DateTime dateSent = (DateTime)receivedMessages[i]["date_posted"];
  115. string message = receivedMessages[i]["message"].ToString();
  116. if ((bool) receivedMessages[i]["anonymous"])
  117. {
  118. Bot.IRC.Command.SendPrivateMessage(nickname, string.Format("An anonymous sender has left you a message on \u0002{0}\u0002", dateSent.ToString("MMMM d, yyyy h:mm:ss tt")));
  119. Bot.IRC.Command.SendPrivateMessage(nickname, string.Format("\"{0}\"", message));
  120. }
  121. else
  122. {
  123. string sentNick = GetNickname((int) receivedMessages[i]["sender_nick_id"]);
  124. Bot.IRC.Command.SendPrivateMessage(nickname, string.Format("\u0002{0}\u0002 has left you a message on \u0002{1}\u0002", sentNick, dateSent.ToString("MMMM d, yyyy h:mm:ss tt")));
  125. Bot.IRC.Command.SendPrivateMessage(nickname, string.Format("\"{0}\"", message));
  126. Bot.IRC.Command.SendPrivateMessage(nickname, string.Format("If you would like to reply to them, please type \u0002{0}{1} {2} \u001FMessage\u001F\u0002", Bot.ServerConfig.CommandPrefix, Commands.Find(cmd => cmd.Name == "Message").Triggers.First(), sentNick));
  127. }
  128. DeleteMessage((int) receivedMessages[i]["id"]);
  129. }
  130. }
  131. }
  132. private List<Dictionary<string, object>> GetSentMessages(string nick, string sender)
  133. {
  134. string search = "SELECT `messages`.`message`, `messages`.`nick_id`, `messages`.`date_posted`, `messages`.`anonymous` FROM `messages` WHERE " +
  135. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}) AND " +
  136. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nickname` = {2}) AND " +
  137. "`sender_nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nickname` = {4})";
  138. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, nick, Bot.ServerConfig.Name, sender });
  139. }
  140. private List<Dictionary<string, object>> GetReceivedMessages(string nick)
  141. {
  142. string search = "SELECT `messages`.`id`, `messages`.`message`, `messages`.`sender_nick_id`, `messages`.`date_posted`, `messages`.`anonymous` FROM `messages` " +
  143. "INNER JOIN `nicks` " +
  144. "ON `messages`.`nick_id` = `nicks`.`id` " +
  145. "INNER JOIN `servers` " +
  146. "ON `messages`.`server_id` = `servers`.`id` " +
  147. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1}";
  148. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nick });
  149. }
  150. private void DeleteMessage(int messageId)
  151. {
  152. string query = "DELETE FROM `messages` " +
  153. "WHERE `id` = {0}";
  154. Bot.Database.Execute(query, new object[] { messageId });
  155. }
  156. }
  157. }