From 8e5ed2a161bf654888a8b48af2d9fdf6fbc0c7e0 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 31 Mar 2019 14:42:18 -0400 Subject: [PATCH] Implement header-regex-match filters --- config/aerc.conf | 6 +++--- config/config.go | 22 ++++++++++++++++++---- widgets/msgviewer.go | 19 ++++++++++++++++++- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/config/aerc.conf b/config/aerc.conf index 3a89151..c310dfc 100644 --- a/config/aerc.conf +++ b/config/aerc.conf @@ -78,9 +78,9 @@ alternatives=text/plain,text/html # them from most to least specific. # # You can also match on non-mimetypes, by prefixing with the header to match -# against (non-case-sensitive) and a colon, e.g. subject:text will match a -# subject which contains "text". Use header~:regex to match against a regex. -subject~:PATCH=contrib/hldiff.py +# against (non-case-sensitive) and a comma, e.g. subject,text will match a +# subject which contains "text". Use header,~regex to match against a regex. +subject,~PATCH=contrib/hldiff.py text/html=w3m -T text/html -cols $(tput cols) -dump -o display_image=false -o display_link_number=true text/*=contrib/plaintext.py diff --git a/config/config.go b/config/config.go index 8d460ca..bff188e 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "path" + "regexp" "strings" "unicode" @@ -25,7 +26,6 @@ type UIConfig struct { const ( FILTER_MIMETYPE = iota FILTER_HEADER - FILTER_HEADER_REGEX ) type AccountConfig struct { @@ -48,6 +48,8 @@ type FilterConfig struct { FilterType int Filter string Command string + Header string + Regex *regexp.Regexp } type ViewerConfig struct { @@ -161,10 +163,22 @@ func LoadConfig(root *string) (*AercConfig, error) { Command: cmd, Filter: match, } - if strings.Contains(match, "~:") { - filter.FilterType = FILTER_HEADER_REGEX - } else if strings.ContainsRune(match, ':') { + fmt.Println(match) + if strings.Contains(match, ",~") { filter.FilterType = FILTER_HEADER + 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 { + panic(err) + } + } else if strings.ContainsRune(match, ',') { + filter.FilterType = FILTER_HEADER + 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)) } else { filter.FilterType = FILTER_MIMETYPE } diff --git a/widgets/msgviewer.go b/widgets/msgviewer.go index 67fd0cc..56c3897 100644 --- a/widgets/msgviewer.go +++ b/widgets/msgviewer.go @@ -117,8 +117,25 @@ func NewMessageViewer(conf *config.AercConfig, store *lib.MessageStore, case config.FILTER_MIMETYPE: if fnmatch.Match(f.Filter, mime, 0) { filter = exec.Command(cmd[0], cmd[1:]...) - fmt.Printf("Using filter for %s: %s\n", mime, f.Command) } + case config.FILTER_HEADER: + var header string + switch f.Header { + case "subject": + header = msg.Envelope.Subject + case "from": + header = formatAddresses(msg.Envelope.From) + case "to": + header = formatAddresses(msg.Envelope.To) + case "cc": + header = formatAddresses(msg.Envelope.Cc) + } + if f.Regex.Match([]byte(header)) { + filter = exec.Command(cmd[0], cmd[1:]...) + } + } + if filter != nil { + break } } if filter != nil {