aboutsummaryrefslogtreecommitdiffstats
path: root/worker/jmap/search.go
blob: 17b5ca1194a123fa52bd392554ebda3fc2b80ac3 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
package jmap

import (
	"strings"

	"git.sr.ht/~rjarry/aerc/lib/parse"
	"git.sr.ht/~rjarry/aerc/log"
	"git.sr.ht/~rockorager/go-jmap/mail/email"
	"git.sr.ht/~sircmpwn/getopt"
)

func parseSearch(args []string) (*email.FilterCondition, error) {
	f := new(email.FilterCondition)
	if len(args) == 0 {
		return f, nil
	}

	opts, optind, err := getopt.Getopts(args, "rubax:X:t:H:f:c:d:")
	if err != nil {
		return nil, err
	}
	body := false
	text := false
	for _, opt := range opts {
		switch opt.Option {
		case 'r':
			f.HasKeyword = "$seen"
		case 'u':
			f.NotKeyword = "$seen"
		case 'f':
			f.From = opt.Value
		case 't':
			f.To = opt.Value
		case 'c':
			f.Cc = opt.Value
		case 'b':
			body = true
		case 'a':
			text = true
		case 'd':
			start, end, err := parse.DateRange(opt.Value)
			if err != nil {
				log.Errorf("failed to parse start date: %v", err)
				continue
			}
			if !start.IsZero() {
				f.After = &start
			}
			if !end.IsZero() {
				f.Before = &end
			}
		}
	}
	switch {
	case text:
		f.Text = strings.Join(args[optind:], " ")
	case body:
		f.Body = strings.Join(args[optind:], " ")
	default:
		f.Subject = strings.Join(args[optind:], " ")
	}
	return f, nil
}