From 94b9d557dee0fd13853b1883cc2730c5cbdbcd3f Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Wed, 28 Aug 2019 06:39:07 +0200 Subject: [PATCH] extract search criteria parsing into the backends --- Makefile | 2 ++ commands/account/search.go | 27 +-------------------------- doc/aerc-search.1.scd | 11 +++++++++++ doc/aerc.1.scd | 11 ++++------- lib/msgstore.go | 6 ++---- worker/imap/list.go | 28 ++++++++++++++++++++-------- worker/imap/search.go | 29 +++++++++++++++++++++++++++++ worker/types/messages.go | 4 +--- 8 files changed, 70 insertions(+), 48 deletions(-) create mode 100644 doc/aerc-search.1.scd create mode 100644 worker/imap/search.go diff --git a/Makefile b/Makefile index a9b5c85..c2e6d1b 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ aerc.conf: config/aerc.conf.in DOCS := \ aerc.1 \ + aerc-search.1 \ aerc-config.5 \ aerc-imap.5 \ aerc-maildir.5 \ @@ -60,6 +61,7 @@ install: all $(SHAREDIR) $(SHAREDIR)/filters install -m755 aerc $(BINDIR)/aerc install -m644 aerc.1 $(MANDIR)/man1/aerc.1 + install -m644 aerc-search.1 $(MANDIR)/man1/aerc-search.1 install -m644 aerc-config.5 $(MANDIR)/man5/aerc-config.5 install -m644 aerc-imap.5 $(MANDIR)/man5/aerc-imap.5 install -m644 aerc-maildir.5 $(MANDIR)/man5/aerc-maildir.5 diff --git a/commands/account/search.go b/commands/account/search.go index da7ab03..ca51917 100644 --- a/commands/account/search.go +++ b/commands/account/search.go @@ -3,9 +3,6 @@ package account import ( "errors" - "git.sr.ht/~sircmpwn/getopt" - "github.com/emersion/go-imap" - "git.sr.ht/~sircmpwn/aerc/widgets" ) @@ -24,28 +21,6 @@ func (_ SearchFilter) Complete(aerc *widgets.Aerc, args []string) []string { } func (_ SearchFilter) Execute(aerc *widgets.Aerc, args []string) error { - var ( - criteria *imap.SearchCriteria = imap.NewSearchCriteria() - ) - - opts, optind, err := getopt.Getopts(args, "ruH:") - if err != nil { - return 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) - } - acct := aerc.SelectedAccount() if acct == nil { return errors.New("No account selected") @@ -73,6 +48,6 @@ func (_ SearchFilter) Execute(aerc *widgets.Aerc, args []string) error { acct.Messages().Scroll() } } - store.Search(criteria, cb) + store.Search(args, cb) return nil } diff --git a/doc/aerc-search.1.scd b/doc/aerc-search.1.scd new file mode 100644 index 0000000..58da44b --- /dev/null +++ b/doc/aerc-search.1.scd @@ -0,0 +1,11 @@ +aerc-search(1) + +# IMAP + +*search* [-ru] + Searches the current folder for . Each separate term is searched + case-insensitively among subject lines. + + *-r*: Search for read messages + + *-u*: Search for unread messages diff --git a/doc/aerc.1.scd b/doc/aerc.1.scd index 225ded7..c3be01b 100644 --- a/doc/aerc.1.scd +++ b/doc/aerc.1.scd @@ -175,13 +175,10 @@ message list, the message in the message viewer, etc). *next-result*, *prev-result* Selects the next or previous search result. -*search* [-ru] - Searches the current folder for . Each separate term is searched - case-insensitively among subject lines. - - *-r*: Search for read messages - - *-u*: Search for unread messages +*search* + Searches the current folder. + The search syntax is dependant on the underlying backend. + Refer to *aerc-search*(1) for details *select* Selects the nth message in the message list (and scrolls it into view if diff --git a/lib/msgstore.go b/lib/msgstore.go index 169d51d..77f0fe5 100644 --- a/lib/msgstore.go +++ b/lib/msgstore.go @@ -4,8 +4,6 @@ import ( "io" "time" - "github.com/emersion/go-imap" - "git.sr.ht/~sircmpwn/aerc/models" "git.sr.ht/~sircmpwn/aerc/worker/types" ) @@ -368,9 +366,9 @@ func (store *MessageStore) Prev() { store.NextPrev(-1) } -func (store *MessageStore) Search(c *imap.SearchCriteria, cb func([]uint32)) { +func (store *MessageStore) Search(args []string, cb func([]uint32)) { store.worker.PostAction(&types.SearchDirectory{ - Criteria: c, + Argv: args, }, func(msg types.WorkerMessage) { switch msg := msg.(type) { case *types.SearchResults: 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 {