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.

Whois.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace Combot.Modules.Plugins
  4. {
  5. public class Whois : Module
  6. {
  7. public override void Initialize()
  8. {
  9. Bot.CommandReceivedEvent += HandleCommandEvent;
  10. }
  11. public override void ParseCommand(CommandMessage command)
  12. {
  13. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  14. switch (foundCommand.Name)
  15. {
  16. case "Whois":
  17. string option = (command.Arguments.ContainsKey("Option")) ? command.Arguments["Option"] : string.Empty;
  18. string mask = (command.Arguments.ContainsKey("Nickname")) ? command.Arguments["Nickname"] : command.Nick.Nickname;
  19. List<string> nicksList = new List<string>();
  20. List<string> hostList = new List<string>();
  21. findMatches(ref nicksList, ref hostList, mask, mask);
  22. if (nicksList.Any() || hostList.Any())
  23. {
  24. // display results
  25. if (nicksList.Any() && (string.IsNullOrEmpty(option) || option.ToLower() == "nicks"))
  26. {
  27. string nicksFound = string.Format("\u0002{0}\u0002 has been seen as: \u0002{1}\u0002", mask, string.Join(", ", nicksList));
  28. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, nicksFound);
  29. }
  30. if (hostList.Any() && (string.IsNullOrEmpty(option) || option.ToLower() == "hosts"))
  31. {
  32. string hostsFound = string.Format("\u0002{0}\u0002 has used the following hosts: \u0002{1}\u0002", mask, string.Join(", ", hostList));
  33. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, hostsFound);
  34. }
  35. }
  36. else
  37. {
  38. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, string.Format("I have no information about \u0002{0}\u0002", mask));
  39. }
  40. break;
  41. }
  42. }
  43. private void findMatches(ref List<string> nickList, ref List<string> hostList, string nick, string host)
  44. {
  45. List<Dictionary<string, object>> results = findAssociationList(nick, host);
  46. for (int i = 0; i < results.Count; i++)
  47. {
  48. string foundNick = results[i]["nickname"].ToString();
  49. string foundHost = results[i]["host"].ToString();
  50. if (nickList != null && !nickList.Contains(foundNick))
  51. {
  52. nickList.Add(foundNick);
  53. findMatches(ref nickList, ref hostList, foundNick, foundNick);
  54. }
  55. if (hostList != null && !hostList.Contains(foundHost))
  56. {
  57. hostList.Add(foundHost);
  58. findMatches(ref nickList, ref hostList, foundHost, foundHost);
  59. }
  60. }
  61. }
  62. private List<Dictionary<string, object>> findAssociationList(string nick, string host)
  63. {
  64. string search = "SELECT `nickinfo`.`host`, `nicks`.`nickname` FROM `nickinfo` " +
  65. "INNER JOIN `nicks` " +
  66. "ON `nickinfo`.`nick_id` = `nicks`.`id` " +
  67. "INNER JOIN `servers` " +
  68. "ON `nicks`.`server_id` = `servers`.`id` " +
  69. "WHERE `servers`.`name` = {0} AND (`nicks`.`nickname` = {1} OR `nickinfo`.`host` = {2})" +
  70. "GROUP BY `nickinfo`.`host`, `nicks`.`nickname`";
  71. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nick, host });
  72. }
  73. }
  74. }