Implement header-regex-match filters

This commit is contained in:
Drew DeVault 2019-03-31 14:42:18 -04:00
parent f9262e4b06
commit 8e5ed2a161
3 changed files with 39 additions and 8 deletions

View File

@ -78,9 +78,9 @@ alternatives=text/plain,text/html
# them from most to least specific. # them from most to least specific.
# #
# You can also match on non-mimetypes, by prefixing with the header to match # 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 # 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 which contains "text". Use header,~regex to match against a regex.
subject~:PATCH=contrib/hldiff.py 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/html=w3m -T text/html -cols $(tput cols) -dump -o display_image=false -o display_link_number=true
text/*=contrib/plaintext.py text/*=contrib/plaintext.py

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"path" "path"
"regexp"
"strings" "strings"
"unicode" "unicode"
@ -25,7 +26,6 @@ type UIConfig struct {
const ( const (
FILTER_MIMETYPE = iota FILTER_MIMETYPE = iota
FILTER_HEADER FILTER_HEADER
FILTER_HEADER_REGEX
) )
type AccountConfig struct { type AccountConfig struct {
@ -48,6 +48,8 @@ type FilterConfig struct {
FilterType int FilterType int
Filter string Filter string
Command string Command string
Header string
Regex *regexp.Regexp
} }
type ViewerConfig struct { type ViewerConfig struct {
@ -161,10 +163,22 @@ func LoadConfig(root *string) (*AercConfig, error) {
Command: cmd, Command: cmd,
Filter: match, Filter: match,
} }
if strings.Contains(match, "~:") { fmt.Println(match)
filter.FilterType = FILTER_HEADER_REGEX if strings.Contains(match, ",~") {
} else if strings.ContainsRune(match, ':') {
filter.FilterType = FILTER_HEADER 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 { } else {
filter.FilterType = FILTER_MIMETYPE filter.FilterType = FILTER_MIMETYPE
} }

View File

@ -117,8 +117,25 @@ func NewMessageViewer(conf *config.AercConfig, store *lib.MessageStore,
case config.FILTER_MIMETYPE: case config.FILTER_MIMETYPE:
if fnmatch.Match(f.Filter, mime, 0) { if fnmatch.Match(f.Filter, mime, 0) {
filter = exec.Command(cmd[0], cmd[1:]...) 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 { if filter != nil {