about summary refs log tree commit diff stats
path: root/worker
diff options
context:
space:
mode:
Diffstat (limited to 'worker')
-rw-r--r--worker/imap/list.go28
-rw-r--r--worker/imap/search.go29
-rw-r--r--worker/types/messages.go4
3 files changed, 50 insertions, 11 deletions
diff --git a/worker/imap/list.go b/worker/imap/list.go
index 42be50e..e69fc07 100644
--- a/worker/imap/list.go
+++ b/worker/imap/list.go
@@ -52,17 +52,29 @@ func canOpen(mbox *imap.MailboxInfo) bool {
 }
 
 func (imapw *IMAPWorker) handleSearchDirectory(msg *types.SearchDirectory) {
-	imapw.worker.Logger.Println("Executing search")
-
-	if uids, err := imapw.client.UidSearch(msg.Criteria); err != nil {
+	emitError := func(err error) {
 		imapw.worker.PostMessage(&types.Error{
 			Message: types.RespondTo(msg),
 			Error:   err,
 		}, nil)
-	} else {
-		imapw.worker.PostMessage(&types.SearchResults{
-			Message: types.RespondTo(msg),
-			Uids:    uids,
-		}, nil)
 	}
+
+	imapw.worker.Logger.Println("Executing search")
+	criteria, err := parseSearch(msg.Argv)
+	if err != nil {
+		emitError(err)
+		return
+	}
+
+	uids, err := imapw.client.UidSearch(criteria)
+	if err != nil {
+		emitError(err)
+		return
+	}
+
+	imapw.worker.PostMessage(&types.SearchResults{
+		Message: types.RespondTo(msg),
+		Uids:    uids,
+	}, nil)
+
 }
diff --git a/worker/imap/search.go b/worker/imap/search.go
new file mode 100644
index 0000000..4decf1b
--- /dev/null
+++ b/worker/imap/search.go
@@ -0,0 +1,29 @@
+package imap
+
+import (
+	"git.sr.ht/~sircmpwn/getopt"
+	"github.com/emersion/go-imap"
+)
+
+func parseSearch(args []string) (*imap.SearchCriteria, error) {
+	criteria := imap.NewSearchCriteria()
+
+	opts, optind, err := getopt.Getopts(args, "ruH:")
+	if err != nil {
+		return nil, err
+	}
+	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
+		}
+	}
+	for _, arg := range args[optind:] {
+		criteria.Header.Add("Subject", arg)
+	}
+	return criteria, nil
+}
diff --git a/worker/types/messages.go b/worker/types/messages.go
index 34a1e5c..7ab94e0 100644
--- a/worker/types/messages.go
+++ b/worker/types/messages.go
@@ -4,8 +4,6 @@ import (
 	"io"
 	"time"
 
-	"github.com/emersion/go-imap"
-
 	"git.sr.ht/~sircmpwn/aerc/config"
 	"git.sr.ht/~sircmpwn/aerc/models"
 )
@@ -84,7 +82,7 @@ type FetchDirectoryContents struct {
 
 type SearchDirectory struct {
 	Message
-	Criteria *imap.SearchCriteria
+	Argv []string
 }
 
 type CreateDirectory struct {