2019-08-28 06:39:07 +02:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/emersion/go-imap"
|
2019-09-09 22:37:45 +02:00
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
2019-08-28 06:39:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func parseSearch(args []string) (*imap.SearchCriteria, error) {
|
|
|
|
criteria := imap.NewSearchCriteria()
|
|
|
|
|
2019-09-09 22:37:45 +02:00
|
|
|
opts, optind, err := getopt.Getopts(args, "rubtH:f:")
|
2019-08-28 06:39:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-09 22:37:45 +02:00
|
|
|
body := false
|
|
|
|
text := false
|
2019-08-28 06:39:07 +02:00
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 'r':
|
|
|
|
criteria.WithFlags = append(criteria.WithFlags, imap.SeenFlag)
|
|
|
|
case 'u':
|
|
|
|
criteria.WithoutFlags = append(criteria.WithoutFlags, imap.SeenFlag)
|
|
|
|
case 'H':
|
|
|
|
// TODO
|
2019-09-09 22:37:45 +02:00
|
|
|
case 'f':
|
|
|
|
criteria.Header.Add("From", opt.Value)
|
|
|
|
case 'b':
|
|
|
|
body = true
|
|
|
|
case 't':
|
|
|
|
text = true
|
2019-08-28 06:39:07 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 22:37:45 +02:00
|
|
|
if text {
|
|
|
|
criteria.Text = args[optind:]
|
|
|
|
} else if body {
|
|
|
|
criteria.Body = args[optind:]
|
|
|
|
} else {
|
|
|
|
for _, arg := range args[optind:] {
|
|
|
|
criteria.Header.Add("Subject", arg)
|
|
|
|
}
|
2019-08-28 06:39:07 +02:00
|
|
|
}
|
|
|
|
return criteria, nil
|
|
|
|
}
|