Example #1
0
func (c *RuleCache) buildAll(refresh bool) (*RuleSet, time.Time, error) {
	matcher := adblock.NewMatcher()
	rules := []string{}
	read := 0
	oldest := time.Time{}
	for _, url := range c.urls {
		r, date, err := c.load(url, refresh)
		if err != nil {
			return nil, oldest, err
		}
		if oldest.After(date) {
			oldest = date
		}
		log.Printf("building rules from %s", url)
		built, n, err := buildOne(r, matcher)
		r.Close()
		if err != nil {
			return nil, oldest, err
		}
		rules = append(rules, built...)
		read += n
	}
	log.Printf("blacklists built: %d / %d added\n", len(rules), read)
	return &RuleSet{
		Rules:   rules,
		Matcher: matcher,
	}, oldest, nil
}
Example #2
0
func check() error {
	verbose := flag.Bool("v", false, "print rejected rules")
	dump := flag.Bool("dump", false, "print parsed rules")
	flag.Parse()
	args := flag.Args()
	if len(args) != 1 {
		return fmt.Errorf("one input rule file expected")
	}
	fp, err := os.Open(args[0])
	if err != nil {
		return err
	}
	defer fp.Close()

	ok := true
	rules := adblock.NewMatcher()
	scanner := bufio.NewScanner(fp)
	for scanner.Scan() {
		rule, err := adblock.ParseRule(scanner.Text())
		if err != nil {
			fmt.Fprintf(os.Stderr, "error: could not parse rule:\n  %s\n  %s\n",
				scanner.Text(), err)
			ok = false
			continue
		}
		if rule == nil {
			continue
		}
		err = rules.AddRule(rule, 0)
		if *verbose && err != nil {
			fmt.Fprintf(os.Stderr, "error: could not add rule:\n  %s\n  %s\n",
				scanner.Text(), err)
			ok = false
		}
	}
	if !ok {
		return fmt.Errorf("some rules could not be parsed")
	}
	if *dump {
		fmt.Printf("%s\n", rules)
	}
	return nil
}