aboutsummaryrefslogtreecommitdiffstats
path: root/worker/imap/search.go
blob: 0305a80fef7f242e4d5d13f8bdaa8c29bb74ee26 (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
package imap

import (
	"github.com/emersion/go-imap"

	"git.sr.ht/~rjarry/aerc/worker/types"
	"git.sr.ht/~rjarry/go-opt"
)

func translateSearch(c *types.SearchCriteria) *imap.SearchCriteria {
	criteria := imap.NewSearchCriteria()
	if c == nil {
		return criteria
	}
	criteria.WithFlags = translateFlags(c.WithFlags)
	criteria.WithoutFlags = translateFlags(c.WithoutFlags)

	if !c.StartDate.IsZero() {
		criteria.SentSince = c.StartDate
	}
	if !c.StartDate.IsZero() {
		criteria.SentBefore = c.EndDate
	}
	for k, v := range c.Headers {
		criteria.Header[k] = v
	}
	for _, f := range c.From {
		criteria.Header.Add("From", f)
	}
	for _, t := range c.To {
		criteria.Header.Add("To", t)
	}
	for _, c := range c.Cc {
		criteria.Header.Add("Cc", c)
	}
	terms := opt.LexArgs(c.Terms)
	if terms.Count() > 0 {
		switch {
		case c.SearchAll:
			criteria.Text = terms.Args()
		case c.SearchBody:
			criteria.Body = terms.Args()
		default:
			for _, term := range terms.Args() {
				criteria.Header.Add("Subject", term)
			}
		}
	}
	return criteria
}