| 1 |
package imap |
| 2 |
|
| 3 |
import ( |
| 4 |
"maps" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/emersion/go-imap" |
| 8 |
|
| 9 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 10 |
"git.sr.ht/~rjarry/go-opt/v2" |
| 11 |
) |
| 12 |
|
| 13 |
func translateSearch(c *types.SearchCriteria) *imap.SearchCriteria { |
| 14 |
if c == nil { |
| 15 |
return imap.NewSearchCriteria() |
| 16 |
} |
| 17 |
criteria := translateSearchPart(c.Match) |
| 18 |
if c.Exclude != nil { |
| 19 |
criteria.Not = append(criteria.Not, translateSearchPart(c.Exclude)) |
| 20 |
} |
| 21 |
return criteria |
| 22 |
} |
| 23 |
|
| 24 |
func translateSearchPart(c *types.SearchCriteriaPart) *imap.SearchCriteria { |
| 25 |
criteria := imap.NewSearchCriteria() |
| 26 |
if c == nil { |
| 27 |
return criteria |
| 28 |
} |
| 29 |
criteria.WithFlags = translateFlags(c.WithFlags) |
| 30 |
criteria.WithoutFlags = translateFlags(c.WithoutFlags) |
| 31 |
|
| 32 |
if !c.StartDate.IsZero() { |
| 33 |
criteria.SentSince = c.StartDate |
| 34 |
} |
| 35 |
if !c.EndDate.IsZero() { |
| 36 |
criteria.SentBefore = c.EndDate |
| 37 |
} |
| 38 |
maps.Copy(criteria.Header, c.Headers) |
| 39 |
for _, f := range c.From { |
| 40 |
criteria.Header.Add("From", f) |
| 41 |
} |
| 42 |
for _, t := range c.To { |
| 43 |
criteria.Header.Add("To", t) |
| 44 |
} |
| 45 |
for _, c := range c.Cc { |
| 46 |
criteria.Header.Add("Cc", c) |
| 47 |
} |
| 48 |
terms := opt.LexArgs(strings.Join(c.Terms, " ")) |
| 49 |
if terms.Count() > 0 { |
| 50 |
switch { |
| 51 |
case c.SearchAll: |
| 52 |
criteria.Text = terms.Args() |
| 53 |
case c.SearchBody: |
| 54 |
criteria.Body = terms.Args() |
| 55 |
default: |
| 56 |
for _, term := range terms.Args() { |
| 57 |
criteria.Header.Add("Subject", term) |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
return criteria |
| 62 |
} |