<commit_before>/*
 * Autopsy Forensic Browser
 * 
 * Copyright 2013 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.ingest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JPanel;
import org.openide.util.lookup.ServiceProvider;
import org.sleuthkit.autopsy.coreutils.ModuleSettings;
import org.sleuthkit.datamodel.Content;

@ServiceProvider(service = IngestConfigurator.class)
public class GeneralIngestConfigurator implements IngestConfigurator { 
   
    public static final String ENABLED_INGEST_MODULES_KEY = "Enabled_Ingest_Modules";
    public static final String DISABLED_INGEST_MODULES_KEY = "Disabled_Ingest_Modules";
    public static final String PARSE_UNALLOC_SPACE_KEY = "Process_Unallocated_Space";
    private List<Content> contentToIngest;
    private IngestManager manager;
    private IngestDialogPanel ingestDialogPanel;
    private String moduleContext;
    
    public GeneralIngestConfigurator() {
        this.moduleContext = IngestManager.MODULE_PROPERTIES;
        ingestDialogPanel = new IngestDialogPanel();
        ingestDialogPanel.setContext(moduleContext);
        manager = IngestManager.getDefault();
    }
    
    @Override
    public List<String> setContext(String context) {
        moduleContext = context;
        ingestDialogPanel.setContext(moduleContext);
        return loadSettingsForContext();
    }

    private List<String> loadSettingsForContext() {        
        List<String> messages = new ArrayList<>();  
        List<IngestModuleAbstract> allModules = IngestManager.getDefault().enumerateAllModules();
        
        // If there is no enabled ingest modules setting for this user, default to enabling all
        // of the ingest modules the IngestManager has loaded.
        if (ModuleSettings.settingExists(moduleContext, ENABLED_INGEST_MODULES_KEY) == false) {
            String defaultSetting = moduleListToCsv(allModules);
            ModuleSettings.setConfigSetting(moduleContext, ENABLED_INGEST_MODULES_KEY, defaultSetting);
        }        
        
        String[] enabledModuleNames = ModuleSettings.getConfigSetting(moduleContext, ENABLED_INGEST_MODULES_KEY).split(", ");
        ArrayList<String> enabledList = new ArrayList<>(Arrays.asList(enabledModuleNames));
        
        // Check for modules that are missing from the config file
        if (ModuleSettings.settingExists(moduleContext, DISABLED_INGEST_MODULES_KEY)) {
            String[] disabledModuleNames = ModuleSettings.getConfigSetting(moduleContext, DISABLED_INGEST_MODULES_KEY).split(", ");
            for (IngestModuleAbstract module : allModules) {
                boolean found = false;

                // Check enabled first
                for (String moduleName : enabledModuleNames) {
                    if (module.getName().equals(moduleName)) {
                        found = true;
                        break;
                    }
                }                
                
                // Then check disabled
                if (!found) {
                    for (String moduleName : disabledModuleNames) {
                        if (module.getName().equals(moduleName)) {
                            found = true;
                            break;
                        }
                    }
                }
                
                if (!found) {
                    enabledList.add(module.getName());
                    //it will get saved to file later
                }
            }            
        }
        
        // Get the enabled ingest modules setting, check for missing modules, and pass the setting to
        // the UI component.
        List<IngestModuleAbstract> enabledModules = new ArrayList<>();
        for (String moduleName : enabledList) {
            if (moduleName.equals("Thunderbird Parser") 
                    || moduleName.equals("MBox Parser")) {
                moduleName = "Email Parser";
            }<commit_msg>Older ingest props files won't have the disabled list, so don't assume it exists.<commit_after>/*
 * Autopsy Forensic Browser
 * 
 * Copyright 2013 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.ingest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JPanel;
import org.openide.util.lookup.ServiceProvider;
import org.sleuthkit.autopsy.coreutils.ModuleSettings;
import org.sleuthkit.datamodel.Content;

@ServiceProvider(service = IngestConfigurator.class)
public class GeneralIngestConfigurator implements IngestConfigurator { 
   
    public static final String ENABLED_INGEST_MODULES_KEY = "Enabled_Ingest_Modules";
    public static final String DISABLED_INGEST_MODULES_KEY = "Disabled_Ingest_Modules";
    public static final String PARSE_UNALLOC_SPACE_KEY = "Process_Unallocated_Space";
    private List<Content> contentToIngest;
    private IngestManager manager;
    private IngestDialogPanel ingestDialogPanel;
    private String moduleContext;
    
    public GeneralIngestConfigurator() {
        this.moduleContext = IngestManager.MODULE_PROPERTIES;
        ingestDialogPanel = new IngestDialogPanel();
        ingestDialogPanel.setContext(moduleContext);
        manager = IngestManager.getDefault();
    }
    
    @Override
    public List<String> setContext(String context) {
        moduleContext = context;
        ingestDialogPanel.setContext(moduleContext);
        return loadSettingsForContext();
    }

    private List<String> loadSettingsForContext() {        
        List<String> messages = new ArrayList<>();  
        List<IngestModuleAbstract> allModules = IngestManager.getDefault().enumerateAllModules();
        
        // If there is no enabled ingest modules setting for this user, default to enabling all
        // of the ingest modules the IngestManager has loaded.
        if (ModuleSettings.settingExists(moduleContext, ENABLED_INGEST_MODULES_KEY) == false) {
            String defaultSetting = moduleListToCsv(allModules);
            ModuleSettings.setConfigSetting(moduleContext, ENABLED_INGEST_MODULES_KEY, defaultSetting);
        }        
        
        String[] enabledModuleNames = ModuleSettings.getConfigSetting(moduleContext, ENABLED_INGEST_MODULES_KEY).split(", ");
        ArrayList<String> enabledList = new ArrayList<>(Arrays.asList(enabledModuleNames));
        
        // Check for modules that are missing from the config file
        
        String[] disabledModuleNames = null;
        // Older config files won't have the disabled list, so don't assume it exists
        if (ModuleSettings.settingExists(moduleContext, DISABLED_INGEST_MODULES_KEY)) {
            disabledModuleNames = ModuleSettings.getConfigSetting(moduleContext, DISABLED_INGEST_MODULES_KEY).split(", ");
        }
        
        for (IngestModuleAbstract module : allModules) {
            boolean found = false;

            // Check enabled first
            for (String moduleName : enabledModuleNames) {
                if (module.getName().equals(moduleName)) {
                    found = true;
                    break;
                }
            }                

            // Then check disabled
            if (!found && (disabledModuleNames != null)) {
                for (String moduleName : disabledModuleNames) {
                    if (module.getName().equals(moduleName)) {
                        found = true;
                        break;
                    }
                }
            }

            if (!found) {
                enabledList.add(module.getName());
                // It will get saved to file later
            }
        }            
        
        // Get the enabled ingest modules setting, check for missing modules, and pass the setting to
        // the UI component.
        List<IngestModuleAbstract> enabledModules = new ArrayList<>();
        for (String moduleName : enabledList) {
            if (moduleName.equals("Thunderbird Parser") 
                    || moduleName.equals("MBox Parser")) {
                moduleName = "Email Parser";
            }
{'fraction_non_alphanumeric': 0.07623847308599614, 'fraction_numerical': 0.0017156337122024449, 'mean_word_length': 2.412733260153677, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6272770', 'n_tokens_mistral': 2341, 'n_tokens_neox': 2316, 'n_words': 652}