From 494bd674a98bc9f2889acad0fda3ff4c77c641b5 Mon Sep 17 00:00:00 2001 From: Tobias Wölfel Date: Fri, 24 Jul 2020 10:36:19 +0200 Subject: Add flag based search options Provide search and filter with the option to specify more flag based conditions. Use '-x ' to search for messages with a flag (seen, answered, flagged) and '-X ' to search for messages without a flag. --- worker/imap/search.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'worker/imap') diff --git a/worker/imap/search.go b/worker/imap/search.go index 42e155b6..f866b1cc 100644 --- a/worker/imap/search.go +++ b/worker/imap/search.go @@ -1,6 +1,9 @@ package imap import ( + "errors" + "strings" + "github.com/emersion/go-imap" "git.sr.ht/~sircmpwn/getopt" @@ -9,7 +12,7 @@ import ( func parseSearch(args []string) (*imap.SearchCriteria, error) { criteria := imap.NewSearchCriteria() - opts, optind, err := getopt.Getopts(args, "rubat:H:f:c:") + opts, optind, err := getopt.Getopts(args, "rubax:X:t:H:f:c:") if err != nil { return nil, err } @@ -21,6 +24,14 @@ func parseSearch(args []string) (*imap.SearchCriteria, error) { criteria.WithFlags = append(criteria.WithFlags, imap.SeenFlag) case 'u': criteria.WithoutFlags = append(criteria.WithoutFlags, imap.SeenFlag) + case 'x': + if f, err := getParsedFlag(opt.Value); err == nil { + criteria.WithFlags = append(criteria.WithFlags, f) + } + case 'X': + if f, err := getParsedFlag(opt.Value); err == nil { + criteria.WithoutFlags = append(criteria.WithoutFlags, f) + } case 'H': // TODO case 'f': @@ -46,3 +57,15 @@ func parseSearch(args []string) (*imap.SearchCriteria, error) { } return criteria, nil } + +func getParsedFlag(name string) (string, error) { + switch strings.ToLower(name) { + case "seen": + return imap.SeenFlag, nil + case "flagged": + return imap.FlaggedFlag, nil + case "answered": + return imap.AnsweredFlag, nil + } + return imap.FlaggedFlag, errors.New("Flag not suppored") +} -- cgit