Ejemplo n.º 1
0
        /// <summary>
        ///     Parses the .xml file and performs the actions specified
        /// </summary>
        /// <param name="name">Scanner name</param>
        /// <param name="pluginFile">Path to .xml file</param>
        public void ScanPlugin(string name, string pluginFile)
        {
            if (!File.Exists(pluginFile))
            {
                return;
            }

            var pluginFunctions = new PluginFunctions();

            try
            {
                using (var xmlReader = XmlReader.Create(pluginFile))
                {
                    while (xmlReader.ReadToFollowing("IsRunning"))
                    {
                        if (CancellationToken.IsCancellationRequested)
                        {
                            return;
                        }

                        var procName = xmlReader.ReadElementContentAsString();

                        if (RunningMsg.DisplayRunningMsg(name, procName).GetValueOrDefault() == false)
                        {
                            return;
                        }
                    }
                }

                using (var xmlReader = XmlReader.Create(pluginFile))
                {
                    if (xmlReader.ReadToFollowing("Action"))
                    {
                        while (xmlReader.Read())
                        {
                            if (CancellationToken.IsCancellationRequested)
                            {
                                return;
                            }

                            if (xmlReader.NodeType != XmlNodeType.Element)
                            {
                                continue;
                            }

                            if (xmlReader.Name == "DeleteKey")
                            {
                                var regPath = xmlReader.ReadElementContentAsString();
                                var regKey  = Utils.RegOpenKey(regPath);
                                var recurse = xmlReader.GetAttribute("Recursive") == "Y";

                                pluginFunctions.DeleteKey(regKey, recurse);
                            }

                            if (xmlReader.Name == "DeleteValue")
                            {
                                var regPath        = xmlReader.GetAttribute("RegKey");
                                var valueNameRegEx = xmlReader.GetAttribute("ValueName");

                                var regKey = Utils.RegOpenKey(regPath, false);

                                pluginFunctions.DeleteValue(regKey, valueNameRegEx);
                            }

                            if (xmlReader.Name == "DeleteFile")
                            {
                                var filePath = MiscFunctions.ExpandVars(xmlReader.ReadElementContentAsString());

                                pluginFunctions.DeleteFile(filePath);
                            }

                            if (xmlReader.Name == "DeleteFolder")
                            {
                                var folderPath = MiscFunctions.ExpandVars(xmlReader.ReadElementContentAsString());
                                var recurse    = xmlReader.GetAttribute("Recursive") == "Y";

                                pluginFunctions.DeleteFolder(folderPath, recurse);
                            }

                            if (xmlReader.Name == "DeleteFileList")
                            {
                                var searchPath        = MiscFunctions.ExpandVars(xmlReader.GetAttribute("Path"));
                                var searchText        = xmlReader.GetAttribute("SearchText");
                                var includeSubFolders = xmlReader.GetAttribute("IncludeSubFolders") == "Y"
                                    ? SearchOption.AllDirectories
                                    : SearchOption.TopDirectoryOnly;

                                pluginFunctions.DeleteFileList(searchPath, searchText, includeSubFolders);
                            }

                            if (xmlReader.Name == "DeleteFolderList")
                            {
                                var searchPath        = MiscFunctions.ExpandVars(xmlReader.GetAttribute("Path"));
                                var searchText        = xmlReader.GetAttribute("SearchText");
                                var includeSubFolders = xmlReader.GetAttribute("IncludeSubFolders") == "Y"
                                    ? SearchOption.AllDirectories
                                    : SearchOption.TopDirectoryOnly;

                                pluginFunctions.DeleteFolderList(searchPath, searchText, includeSubFolders);
                            }

                            if (xmlReader.Name == "FindRegKey")
                            {
                                var regKey         = xmlReader.GetAttribute("RegKey");
                                var includeSubKeys = xmlReader.GetAttribute("IncludeSubKeys") == "Y";

                                var rk          = Utils.RegOpenKey(regKey, false);
                                var xmlChildren = xmlReader.ReadSubtree();

                                pluginFunctions.DeleteFoundRegKeys(rk, includeSubKeys, xmlChildren);
                            }

                            if (xmlReader.Name == "FindPath")
                            {
                                var searchPath        = MiscFunctions.ExpandVars(xmlReader.GetAttribute("Path"));
                                var searchText        = xmlReader.GetAttribute("SearchText");
                                var includeSubFolders = xmlReader.GetAttribute("IncludeSubFolders") == "Y"
                                    ? SearchOption.AllDirectories
                                    : SearchOption.TopDirectoryOnly;

                                var xmlChildren = xmlReader.ReadSubtree();

                                pluginFunctions.DeleteFoundPaths(searchPath, searchText, includeSubFolders, xmlChildren);
                            }

                            if (xmlReader.Name == "RemoveINIValue")
                            {
                                var filePath     = MiscFunctions.ExpandVars(xmlReader.GetAttribute("Path"));
                                var sectionRegEx = xmlReader.GetAttribute("Section");
                                var valueRegEx   = xmlReader.GetAttribute("Name");

                                pluginFunctions.DeleteIniValue(filePath, sectionRegEx, valueRegEx);
                            }

                            if (xmlReader.Name == "RemoveINISection")
                            {
                                var filePath     = MiscFunctions.ExpandVars(xmlReader.GetAttribute("Path"));
                                var sectionRegEx = xmlReader.GetAttribute("Section");

                                pluginFunctions.DeleteIniSection(filePath, sectionRegEx);
                            }

                            if (xmlReader.Name == "RemoveXML")
                            {
                                var filePath = MiscFunctions.ExpandVars(xmlReader.GetAttribute("Path"));
                                var xPath    = xmlReader.GetAttribute("XPath");

                                pluginFunctions.DeleteXml(filePath, xPath);
                            }
                        }
                    }
                }
            }
            catch (SecurityException ex)
            {
                Debug.WriteLine("The following error occurred: {0}\nUnable to load plugin file ({1})", ex.Message,
                                pluginFile);
            }
            catch (UriFormatException ex)
            {
                Debug.WriteLine("The following error occurred: {0}\nUnable to load plugin file ({1})", ex.Message,
                                pluginFile);
            }

            if (pluginFunctions.RegistrySubKeys.Count > 0)
            {
                Wizard.StoreBadRegKeySubKeys(name, pluginFunctions.RegistrySubKeys);
            }

            if (pluginFunctions.RegistryValueNames.Count > 0)
            {
                Wizard.StoreBadRegKeyValueNames(name, pluginFunctions.RegistryValueNames);
            }

            if (pluginFunctions.FilePaths.Count > 0)
            {
                Wizard.StoreBadFileList(name, pluginFunctions.FilePaths.ToArray());
            }

            if (pluginFunctions.Folders.Count > 0)
            {
                Wizard.StoreBadFolderList(name, pluginFunctions.Folders);
            }

            if (pluginFunctions.IniList.Count > 0)
            {
                Wizard.StoreIniKeys(name, pluginFunctions.IniList.ToArray());
            }

            if (pluginFunctions.XmlPaths.Count > 0)
            {
                Wizard.StoreXml(name, pluginFunctions.XmlPaths);
            }
        }