예제 #1
0
        private static List <FtpDirectory> FindDirectory(List <FtpDirectory> directories, Box?box, string name)
        {
            FtpDirectory?       idMatch      = null;
            FtpDirectory?       directMatch  = null;
            List <FtpDirectory> roughMatches = new List <FtpDirectory>();

            foreach (FtpDirectory directory in directories)
            {
                if (box != null && BoxDatabase.FindBoxes(directory.Name).Any(x => x == box))
                {
                    idMatch = directory;
                    break;
                }
                else if (directory.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
                {
                    directMatch = directory;
                }
                else if (directory.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    roughMatches.Add(directory);
                }
            }
            return(idMatch == null && directMatch == null ? roughMatches : new List <FtpDirectory> {
                idMatch ?? directMatch !
            });
예제 #2
0
        public void Run(IrcMessage theMessage)
        {
            bool          recovery     = false;
            bool          source       = false;
            bool          firmware     = false;
            Box           box          = BoxDatabase.FindBoxes(theMessage.CommandLine).FirstOrDefault();
            List <string> nameSegments = new List <string>();
            string        rawName;

            if (theMessage.CommandArgs.Count > 1)
            {
                foreach (string argument in theMessage.CommandArgs)
                {
                    if (argument.Equals("all", StringComparison.OrdinalIgnoreCase) || argument.Equals("alles", StringComparison.OrdinalIgnoreCase))
                    {
                        firmware = true;
                        recovery = true;
                        source   = true;
                        break;
                    }
                    else if (argument.Equals("source", StringComparison.OrdinalIgnoreCase) || argument.Equals("sources", StringComparison.OrdinalIgnoreCase) || argument.Equals("src", StringComparison.OrdinalIgnoreCase))
                    {
                        source = true;
                    }
                    else if (argument.Equals("recovery", StringComparison.OrdinalIgnoreCase) || argument.Equals("recoveries", StringComparison.OrdinalIgnoreCase))
                    {
                        recovery = true;
                    }
                    else if (argument.Equals("firmware", StringComparison.OrdinalIgnoreCase) || argument.Equals("firmwares", StringComparison.OrdinalIgnoreCase))
                    {
                        firmware = true;
                    }
                    else
                    {
                        nameSegments.Add(argument);
                    }
                }
                rawName = nameSegments.Join(" ");
            }
            else
            {
                firmware = true;
                rawName  = theMessage.CommandLine;
            }

            FtpDirectory?       Scan    = LastScan;
            FtpDirectory?       match   = null;
            List <FtpDirectory>?matches = null;
            bool shouldRefresh          = false;

            if (Scan != null)
            {
                matches = FindDirectory(Scan.Folders, box, rawName);
                if (matches.Count > 1)
                {
                    theMessage.Answer("Bitte genauer spezifizieren: " + OnlyReadableNames(matches.Select(x => x.Name)).Join(", "));
                    return;
                }
                match         = matches.FirstOrDefault();
                shouldRefresh = true;
            }
            if (match == null)
            {
                List <FtpDirectory> FreshScanned;
                using (FtpClient client = GetClient())
                {
                    FreshScanned = client.GetListing("/fritz.box").Where(x => x.Type == FtpFileSystemObjectType.Directory).Select(x => new FtpDirectory {
                        FullName = x.FullName
                    }).ToList();
                    matches = FindDirectory(FreshScanned, box, rawName);
                    if (matches.Count > 1)
                    {
                        theMessage.Answer("Bitte genauer spezifizieren: " + OnlyReadableNames(matches.Select(x => x.Name)).Join(", "));
                        return;
                    }
                    match = matches.FirstOrDefault();
                    if (match != null)
                    {
                        match         = RecurseFTP(client, match.FullName);
                        shouldRefresh = false;
                    }
                }
            }

            if (match == null)
            {
                theMessage.Answer("Ich habe zu deiner Suche leider kein Verzeichnis gefunden");
                return;
            }

            string output = FormatResult(match, recovery, source, firmware);

            if (!String.IsNullOrEmpty(output))
            {
                theMessage.Answer(output);
            }

            if (shouldRefresh)
            {
                using (FtpClient client = GetClient())
                {
                    match = RecurseFTP(client, match.FullName);
                    string refreshedOutput = FormatResult(match, recovery, source, firmware);
                    if (String.IsNullOrEmpty(output) && !String.IsNullOrEmpty(refreshedOutput))
                    {
                        theMessage.Answer(refreshedOutput);
                        return;
                    }
                    if (output != refreshedOutput)
                    {
                        theMessage.Answer("Wups, meine Angabe war nicht mehr Up-to-Date, hier kommen die aktuellen Ergebnisse:");
                        theMessage.Answer(refreshedOutput);
                    }
                }
            }
        }