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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Combot.Databases;
  5. using Combot.IRCServices.Messaging;
  6. namespace Combot.Modules.Plugins
  7. {
  8. public class Messaging : Module
  9. {
  10. public override void Initialize()
  11. {
  12. Bot.CommandReceivedEvent += HandleCommandEvent;
  13. Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessage;
  14. Bot.IRC.Message.PrivateMessageReceivedEvent += HandlePrivateMessage;
  15. Bot.IRC.Message.ChannelNoticeReceivedEvent += HandleChannelNotice;
  16. Bot.IRC.Message.PrivateNoticeReceivedEvent += HandlePrivateNotice;
  17. }
  18. public override void ParseCommand(CommandMessage command)
  19. {
  20. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  21. switch (foundCommand.Name)
  22. {
  23. case "Message":
  24. AddMessage(command);
  25. break;
  26. case "Anonymous Message":
  27. AddMessage(command, true);
  28. break;
  29. }
  30. }
  31. private void HandleChannelMessage(object sender, ChannelMessage message)
  32. {
  33. if (Enabled)
  34. {
  35. CheckMessages(message.Sender.Nickname);
  36. }
  37. }
  38. private void HandlePrivateMessage(object sender, PrivateMessage message)
  39. {
  40. if (Enabled)
  41. {
  42. CheckMessages(message.Sender.Nickname);
  43. }
  44. }
  45. private void HandleChannelNotice(object sender, ChannelNotice message)
  46. {
  47. if (Enabled)
  48. {
  49. CheckMessages(message.Sender.Nickname);
  50. }
  51. }
  52. private void HandlePrivateNotice(object sender, PrivateNotice message)
  53. {
  54. if (Enabled)
  55. {
  56. CheckMessages(message.Sender.Nickname);
  57. }
  58. }
  59. private void AddMessage(CommandMessage command, bool anonymous = false)
  60. {
  61. List<Dictionary<string, object>> currentMessages = GetSentMessages(command.Arguments["Nickname"]);
  62. int numMessages = currentMessages.Select(msg => GetNickname((int) msg["nick_id"]) == command.Nick.Nickname).Count();
  63. int maxMessages = Convert.ToInt32(GetOptionValue("Max Messages"));
  64. if (numMessages < maxMessages)
  65. {
  66. AddNick(command.Nick.Nickname);
  67. AddNick(command.Arguments["Nickname"]);
  68. string query = "INSERT INTO `messages` SET " +
  69. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  70. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nickname` = {2}), " +
  71. "`sender_nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nickname` = {4}), " +
  72. "`message` = {5}, " +
  73. "`anonymous` = {6}, " +
  74. "`date_posted` = {7}";
  75. 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 });
  76. string message = string.Format("I will send your message to \u0002{0}\u0002 as soon as I see them.", command.Arguments["Nickname"]);
  77. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, message);
  78. }
  79. else
  80. {
  81. 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.");
  82. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, maxMessage);
  83. }
  84. }
  85. private void CheckMessages(string nickname)
  86. {
  87. List<Dictionary<string, object>> receivedMessages = GetReceivedMessages(nickname);
  88. if (receivedMessages.Any())
  89. {
  90. for (int i = 0; i < receivedMessages.Count; i++)
  91. {
  92. DateTime dateSent = (DateTime)receivedMessages[i]["date_posted"];
  93. string message = receivedMessages[i]["message"].ToString();
  94. if ((bool) receivedMessages[i]["anonymous"])
  95. {
  96. 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")));
  97. Bot.IRC.Command.SendPrivateMessage(nickname, string.Format("\"{0}\"", message));
  98. }
  99. else
  100. {
  101. string sentNick = GetNickname((int) receivedMessages[i]["sender_nick_id"]);
  102. 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")));
  103. Bot.IRC.Command.SendPrivateMessage(nickname, string.Format("\"{0}\"", message));
  104. 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));
  105. }
  106. DeleteMessage((int) receivedMessages[i]["id"]);
  107. }
  108. }
  109. }
  110. private List<Dictionary<string, object>> GetSentMessages(string nick)
  111. {
  112. string search = "SELECT `messages`.`message`, `messages`.`nick_id`, `messages`.`date_posted`, `messages`.`anonymous` FROM `messages` " +
  113. "INNER JOIN `nicks` " +
  114. "ON `messages`.`sender_nick_id` = `nicks`.`id` " +
  115. "INNER JOIN `servers` " +
  116. "ON `messages`.`server_id` = `servers`.`id` " +
  117. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1}";
  118. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nick });
  119. }
  120. private List<Dictionary<string, object>> GetReceivedMessages(string nick)
  121. {
  122. string search = "SELECT `messages`.`id`, `messages`.`message`, `messages`.`sender_nick_id`, `messages`.`date_posted`, `messages`.`anonymous` FROM `messages` " +
  123. "INNER JOIN `nicks` " +
  124. "ON `messages`.`nick_id` = `nicks`.`id` " +
  125. "INNER JOIN `servers` " +
  126. "ON `messages`.`server_id` = `servers`.`id` " +
  127. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1}";
  128. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nick });
  129. }
  130. private void DeleteMessage(int messageId)
  131. {
  132. string query = "DELETE FROM `messages` " +
  133. "WHERE `id` = {0}";
  134. Bot.Database.Execute(query, new object[] { messageId });
  135. }
  136. }
  137. }