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.

Sed.cs 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using Combot.Databases;
  6. using Combot.IRCServices.Messaging;
  7. namespace Combot.Modules.Plugins
  8. {
  9. public class Sed : Module
  10. {
  11. public override void Initialize()
  12. {
  13. Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessageEvent;
  14. }
  15. private void HandleChannelMessageEvent(object sender, ChannelMessage message)
  16. {
  17. if (Enabled
  18. && !Bot.ServerConfig.ChannelBlacklist.Contains(message.Channel)
  19. && !Bot.ServerConfig.NickBlacklist.Contains(message.Sender.Nickname)
  20. && !ChannelBlacklist.Contains(message.Channel)
  21. && !NickBlacklist.Contains(message.Sender.Nickname))
  22. {
  23. Regex sedRegex = new Regex(@"^s\/(?<Match>[^\/\\]*(?:\\.[^\/\\]*)*)\/(?<Replace>[^\/\\]*(?:\\.[^\/\\]*)*)\/(?<Option>[g|I|0-9]*)?");
  24. if (sedRegex.IsMatch(message.Message))
  25. {
  26. Match sedMatch = sedRegex.Match(message.Message);
  27. string match = sedMatch.Groups["Match"].ToString().Replace(@"\/", @"/");
  28. string replace = sedMatch.Groups["Replace"].ToString().Replace(@"\/", @"/");
  29. string option = sedMatch.Groups["Option"].ToString();
  30. string mysqlCase;
  31. RegexOptions matchOptions;
  32. int optionVal;
  33. int replaceNum;
  34. if (int.TryParse(option, out optionVal))
  35. {
  36. matchOptions = RegexOptions.None;
  37. replaceNum = optionVal;
  38. mysqlCase = "CAST(`channelmessages`.`message` AS BINARY)";
  39. }
  40. else if (option == "g")
  41. {
  42. matchOptions = RegexOptions.None;
  43. replaceNum = 1;
  44. mysqlCase = "CAST(`channelmessages`.`message` AS BINARY)";
  45. }
  46. else if (option == "I")
  47. {
  48. matchOptions = RegexOptions.IgnoreCase;
  49. replaceNum = 1;
  50. mysqlCase = "`channelmessages`.`message`";
  51. }
  52. else
  53. {
  54. matchOptions = RegexOptions.None;
  55. replaceNum = 1;
  56. mysqlCase = "CAST(`channelmessages`.`message` AS BINARY)";
  57. }
  58. string mysqlMatch = match.Replace(@"\s", "[:space:]").Replace(@"\", @"\\");
  59. List<Dictionary<string, object>> resultList = GetMessageList(message.Channel, message.Sender.Nickname, mysqlMatch, mysqlCase);
  60. if (resultList.Any())
  61. {
  62. IEnumerable<Dictionary<string, object>> validList = resultList.Where(item => item["message"].ToString() != message.Message);
  63. if (validList.Any())
  64. {
  65. string oldMessage = validList.First()["message"].ToString();
  66. Regex messageRegex = new Regex(match, matchOptions);
  67. string newMessage = messageRegex.Replace(oldMessage, replace, replaceNum);
  68. string replacedMessage = string.Format("\u0002{0}\u0002 meant to say: {1}", message.Sender.Nickname, newMessage);
  69. SendResponse(MessageType.Channel, message.Channel, message.Sender.Nickname, replacedMessage);
  70. }
  71. else
  72. {
  73. string noMatch = string.Format("You do not have any previous messages that match \u0002{0}\u0002.", match);
  74. SendResponse(MessageType.Channel, message.Channel, message.Sender.Nickname, noMatch);
  75. }
  76. }
  77. else
  78. {
  79. string noMatch = string.Format("You do not have any previous messages that match \u0002{0}\u0002.", match);
  80. SendResponse(MessageType.Channel, message.Channel, message.Sender.Nickname, noMatch);
  81. }
  82. }
  83. }
  84. }
  85. private List<Dictionary<string, object>> GetMessageList(string channel, string nickname, string regex, string caseString)
  86. {
  87. Database database = new Database(Bot.ServerConfig.Database);
  88. string search = "SELECT `channelmessages`.`message`, `channelmessages`.`date_added` FROM `channelmessages` " +
  89. "INNER JOIN `nicks` " +
  90. "ON `channelmessages`.`nick_id` = `nicks`.`id` " +
  91. "INNER JOIN `channels` " +
  92. "ON `channelmessages`.`channel_id` = `channels`.`id` " +
  93. "INNER JOIN `servers` " +
  94. "ON `channelmessages`.`server_id` = `servers`.`id` " +
  95. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2} AND " + caseString + " REGEXP {3} " +
  96. "ORDER BY date_added DESC";
  97. return database.Query(search, new object[] { Bot.ServerConfig.Name, channel, nickname, regex });
  98. }
  99. }
  100. }