<commit_before>/*
 Copyright (C) 2009-2013 <NAME> <<EMAIL>>
 Copyright (C) 2009-2012 <NAME> <<EMAIL>>

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 See the file COPYING for the full license text.
*/

#if HAVE_WEBKIT2
namespace Adblock {
    enum Directive {
        ALLOW,
        BLOCK
    }

    public class Filter {
        bool debug_match;
        HashTable<string, Directive?> cache;
        HashTable<string, Regex?> pattern;
        HashTable<string, Regex?> keys;
        HashTable<string, string?> optslist;
        List<Regex> blacklist;

        public Filter (WebKit.WebExtension web_extension) {
            debug_match = "adblock:match" in (Environment.get_variable ("MIDORI_DEBUG") ?? "");
            stdout.printf ("WebKit2Adblock%s\n", debug_match ? " debug" : "");
            reload_rules ();
            web_extension.page_created.connect (page_created);
        }

        void reload_rules () {
            cache = new HashTable<string, Directive?> (str_hash, str_equal);
            pattern = new HashTable<string, Regex?> (str_hash, str_equal);
            keys = new HashTable<string, Regex?> (str_hash, str_equal);
            optslist = new HashTable<string, string?> (str_hash, str_equal);
            blacklist = new List<Regex> ();

            // FIXME
            string filename = GLib.Path.build_filename (GLib.Environment.get_home_dir (), ".config", "midori", "extensions", "libadblock.so", "config"); // use midori vapi
            var keyfile = new GLib.KeyFile ();
            try {
                keyfile.load_from_file (filename, GLib.KeyFileFlags.NONE);
                string[] filters = keyfile.get_string_list ("settings", "filters");
                foreach (string filter in filters) {
                    try {
                        string filter_filename = GLib.Path.build_filename (GLib.Environment.get_home_dir (), ".cache", "midori", "adblock", Checksum.compute_for_string (ChecksumType.MD5, filter, -1)); // FIXME
                        stdout.printf ("Parsing %s (%s)\n", filter, filter_filename);
                        var filter_file = File.new_for_path (filter_filename);
                        var stream = new DataInputStream (filter_file.read ());
                        string? line;
                        while ((line = stream.read_line (null)) != null)
                            parse_line (line.chomp ());
                    }
                    catch (GLib.Error io_error) {
                    }
                }
            }
            catch (GLib.Error io_error) {
            }
        }

        void parse_line (string line) {<commit_msg>Add error messages for failure to read filter or settings<commit_after>/*
 Copyright (C) 2009-2013 <NAME> <<EMAIL>>
 Copyright (C) 2009-2012 <NAME> <<EMAIL>>

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 See the file COPYING for the full license text.
*/

#if HAVE_WEBKIT2
namespace Adblock {
    enum Directive {
        ALLOW,
        BLOCK
    }

    public class Filter {
        bool debug_match;
        HashTable<string, Directive?> cache;
        HashTable<string, Regex?> pattern;
        HashTable<string, Regex?> keys;
        HashTable<string, string?> optslist;
        List<Regex> blacklist;

        public Filter (WebKit.WebExtension web_extension) {
            debug_match = "adblock:match" in (Environment.get_variable ("MIDORI_DEBUG") ?? "");
            stdout.printf ("WebKit2Adblock%s\n", debug_match ? " debug" : "");
            reload_rules ();
            web_extension.page_created.connect (page_created);
        }

        void reload_rules () {
            cache = new HashTable<string, Directive?> (str_hash, str_equal);
            pattern = new HashTable<string, Regex?> (str_hash, str_equal);
            keys = new HashTable<string, Regex?> (str_hash, str_equal);
            optslist = new HashTable<string, string?> (str_hash, str_equal);
            blacklist = new List<Regex> ();

            // FIXME
            string filename = GLib.Path.build_filename (GLib.Environment.get_home_dir (), ".config", "midori", "extensions", "libadblock.so", "config"); // use midori vapi
            var keyfile = new GLib.KeyFile ();
            try {
                keyfile.load_from_file (filename, GLib.KeyFileFlags.NONE);
                string[] filters = keyfile.get_string_list ("settings", "filters");
                foreach (string filter in filters) {
                    try {
                        string filter_filename = GLib.Path.build_filename (GLib.Environment.get_home_dir (), ".cache", "midori", "adblock", Checksum.compute_for_string (ChecksumType.MD5, filter, -1)); // FIXME
                        stdout.printf ("Parsing %s (%s)\n", filter, filter_filename);
                        var filter_file = File.new_for_path (filter_filename);
                        var stream = new DataInputStream (filter_file.read ());
                        string? line;
                        while ((line = stream.read_line (null)) != null)
                            parse_line (line.chomp ());
                    }
                    catch (GLib.Error io_error) {
                        stdout.printf ("Error reading file for %s: %s\n", filter, io_error.message);
                    }
                }
            }
            catch (GLib.Error settings_error) {
                stdout.printf ("Error reading settings: %s\n", settings_error.message);
            }
        }

        void parse_line (string line) {
{'fraction_non_alphanumeric': 0.10650887573964497, 'fraction_numerical': 0.007438715131022823, 'mean_word_length': 2.0076258261311644, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '219680', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1566, 'n_words': 448}