|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using System.Collections.Generic;
- using System.Linq;
-
- namespace Combot.Modules.Plugins
- {
- public class Whois : Module
- {
- public override void Initialize()
- {
- Bot.CommandReceivedEvent += HandleCommandEvent;
- }
-
- public override void ParseCommand(CommandMessage command)
- {
- Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
- switch (foundCommand.Name)
- {
- case "Whois":
- string option = (command.Arguments.ContainsKey("Option")) ? command.Arguments["Option"] : string.Empty;
- string mask = (command.Arguments.ContainsKey("Nickname")) ? command.Arguments["Nickname"] : command.Nick.Nickname;
- List<string> nicksList = new List<string>();
- List<string> hostList = new List<string>();
- bool detail = (option.ToLower() == "detail");
- findMatches(ref nicksList, ref hostList, mask, mask, detail);
- if (nicksList.Any() || hostList.Any())
- {
- // display results
- if (nicksList.Any() && (string.IsNullOrEmpty(option) || option.ToLower() == "nicks" || option.ToLower() == "detail"))
- {
- string nicksFound = string.Format("\u0002{0}\u0002 has been seen as: \u0002{1}\u0002", mask, string.Join(", ", nicksList));
- SendResponse(command.MessageType, command.Location, command.Nick.Nickname, nicksFound, true);
- }
- if (hostList.Any() && (string.IsNullOrEmpty(option) || option.ToLower() == "hosts" || option.ToLower() == "detail"))
- {
- string hostsFound = string.Format("\u0002{0}\u0002 has used the following hosts: \u0002{1}\u0002", mask, string.Join(", ", hostList));
- SendResponse(command.MessageType, command.Location, command.Nick.Nickname, hostsFound, true);
- }
- }
- else
- {
- SendResponse(command.MessageType, command.Location, command.Nick.Nickname, string.Format("I have no information about \u0002{0}\u0002", mask), true);
- }
- break;
- }
- }
-
- private void findMatches(ref List<string> nickList, ref List<string> hostList, string nick, string host, bool detail)
- {
- List<Dictionary<string, object>> results = findAssociationList(nick, host, detail);
- for (int i = 0; i < results.Count; i++)
- {
- string foundNick = results[i]["nickname"].ToString();
- string foundHost = results[i]["host"].ToString();
- if (nickList != null && !nickList.Contains(foundNick))
- {
- nickList.Add(foundNick);
- findMatches(ref nickList, ref hostList, foundNick, foundNick, detail);
- }
- if (hostList != null && !hostList.Contains(foundHost))
- {
- hostList.Add(foundHost);
- findMatches(ref nickList, ref hostList, foundHost, foundHost, detail);
- }
- }
- }
-
- private List<Dictionary<string, object>> findAssociationList(string nick, string host, bool detail)
- {
- string reg = string.Empty;
- if (detail)
- {
- reg = "(`nicks`.`nickname` = {1} OR `nickinfo`.`host` = {2}) ";
- }
- else
- {
- reg = "(`nicks`.`nickname` = {1} OR (`nickinfo`.`host` = {2} AND `nickinfo`.`registered` = true)) ";
- }
-
- string search = "SELECT `nickinfo`.`host`, `nicks`.`nickname` FROM `nickinfo` " +
- "INNER JOIN `nicks` " +
- "ON `nickinfo`.`nick_id` = `nicks`.`id` " +
- "INNER JOIN `servers` " +
- "ON `nicks`.`server_id` = `servers`.`id` " +
- "WHERE `servers`.`name` = {0} AND " +
- reg +
- "GROUP BY `nickinfo`.`host`, `nicks`.`nickname`";
- return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nick, host });
- }
- }
- }
|