aboutsummaryrefslogtreecommitdiffstats
path: root/config/filters.go
blob: c3d10f68687d3265ad165060b38a805cd8f10251 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package config

import (
	"regexp"
	"strings"

	"git.sr.ht/~rjarry/aerc/logging"
	"github.com/go-ini/ini"
)

type FilterType int

const (
	FILTER_MIMETYPE FilterType = iota
	FILTER_HEADER
)

type FilterConfig struct {
	Type    FilterType
	Filter  string
	Command string
	Header  string
	Regex   *regexp.Regexp
}

func (config *AercConfig) parseFilters(file *ini.File) error {
	filters, err := file.GetSection("filters")
	if err != nil {
		goto end
	}

	// TODO: Parse the filter more finely, e.g. parse the regex
	for match, cmd := range filters.KeysHash() {
		filter := FilterConfig{
			Command: cmd,
			Filter:  match,
		}
		switch {
		case strings.Contains(match, ",~"):
			filter.Type = FILTER_HEADER
			//nolint:gocritic // guarded by strings.Contains
			header := filter.Filter[:strings.Index(filter.Filter, ",")]
			regex := filter.Filter[strings.Index(filter.Filter, "~")+1:]
			filter.Header = strings.ToLower(header)
			filter.Regex, err = regexp.Compile(regex)
			if err != nil {
				return err
			}
		case strings.ContainsRune(match, ','):
			filter.Type = FILTER_HEADER
			//nolint:gocritic // guarded by strings.Contains
			header := filter.Filter[:strings.Index(filter.Filter, ",")]
			value := filter.Filter[strings.Index(filter.Filter, ",")+1:]
			filter.Header = strings.ToLower(header)
			filter.Regex, err = regexp.Compile(regexp.QuoteMeta(value))
			if err != nil {
				return err
			}
		default:
			filter.Type = FILTER_MIMETYPE
		}
		config.Filters = append(config.Filters, filter)
	}

end:
	logging.Debugf("aerc.conf: [filters] %#v", config.Filters)
	return nil
}