From b6bcf89784605aa8baa982677de9d8d8ddbaa404 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Sat, 12 Sep 2020 15:05:02 +0200 Subject: [PATCH] imap: add sort support --- go.mod | 3 ++- go.sum | 10 +++++++-- worker/imap/open.go | 49 +++++++++++++++++++++++++++++++++++++++++-- worker/imap/worker.go | 4 +++- 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index c9a4aa3..f5b0ba5 100644 --- a/go.mod +++ b/go.mod @@ -9,10 +9,11 @@ require ( github.com/ddevault/go-libvterm v0.0.0-20190526194226-b7d861da3810 github.com/emersion/go-imap v1.0.6-0.20200914131512-88f167c1e6f7 github.com/emersion/go-imap-idle v0.0.0-20190519112320-2704abd7050e + github.com/emersion/go-imap-sortthread v1.1.0 github.com/emersion/go-maildir v0.2.0 github.com/emersion/go-message v0.12.1-0.20200824204225-9094bd0b8bc0 github.com/emersion/go-pgpmail v0.0.0-20200303213726-db035a3a4139 - github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b + github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 github.com/emersion/go-smtp v0.12.1 github.com/fsnotify/fsnotify v1.4.7 github.com/gdamore/tcell v1.3.0 diff --git a/go.sum b/go.sum index ef0ff79..68171a1 100644 --- a/go.sum +++ b/go.sum @@ -15,21 +15,27 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/ddevault/go-libvterm v0.0.0-20190526194226-b7d861da3810 h1:VlHKuIrEvuGlED53TkovT4AVUjrqTyeCt3wiqw1OsFc= github.com/ddevault/go-libvterm v0.0.0-20190526194226-b7d861da3810/go.mod h1:Ow1oE1Hr4xE7eWY2/Ih2kbcOyyXDH7G0XKv/I4yiCYs= +github.com/emersion/go-imap v1.0.0/go.mod h1:MEiDDwwQFcZ+L45Pa68jNGv0qU9kbW+SJzwDpvSfX1s= github.com/emersion/go-imap v1.0.6-0.20200914131512-88f167c1e6f7 h1:MemZhXjDIzBGeBcSddtW+YYdzInMrQW2BOWOIujO9F4= github.com/emersion/go-imap v1.0.6-0.20200914131512-88f167c1e6f7/go.mod h1:yKASt+C3ZiDAiCSssxg9caIckWF/JG7ZQTO7GAmvicU= github.com/emersion/go-imap-idle v0.0.0-20190519112320-2704abd7050e h1:L7bswVJZcf2YHofgom49oFRwVqmBj/qZqDy9/SJpZMY= github.com/emersion/go-imap-idle v0.0.0-20190519112320-2704abd7050e/go.mod h1:o14zPKCmEH5WC1vU5SdPoZGgNvQx7zzKSnxPQlobo78= +github.com/emersion/go-imap-sortthread v1.1.0 h1:uRbmnQkeRny5ihKfLWBPJ/1jJdTZnCdh1zYpOagbubw= +github.com/emersion/go-imap-sortthread v1.1.0/go.mod h1:opHOzblOHZKQM1JEy+GPk1217giNLa7kleyWTN06qnc= github.com/emersion/go-maildir v0.2.0 h1:fC4+UVGl8GcQGbFF7AWab2JMf4VbKz+bMNv07xxhzs8= github.com/emersion/go-maildir v0.2.0/go.mod h1:I2j27lND/SRLgxROe50Vam81MSaqPFvJ0OHNnDZ7n84= -github.com/emersion/go-message v0.11.1 h1:0C/S4JIXDTSfXB1vpqdimAYyK4+79fgEAMQ0dSL+Kac= +github.com/emersion/go-message v0.10.4-0.20190609165112-592ace5bc1ca/go.mod h1:3h+HsGTCFHmk4ngJ2IV/YPhdlaOcR6hcgqM3yca9v7c= github.com/emersion/go-message v0.11.1/go.mod h1:C4jnca5HOTo4bGN9YdqNQM9sITuT3Y0K6bSUw9RklvY= github.com/emersion/go-message v0.12.1-0.20200824204225-9094bd0b8bc0 h1:G2VV/Wp2opDvR0ecue3UY/IX1/8OlTmMKKi+ENe1nG0= github.com/emersion/go-message v0.12.1-0.20200824204225-9094bd0b8bc0/go.mod h1:C4jnca5HOTo4bGN9YdqNQM9sITuT3Y0K6bSUw9RklvY= github.com/emersion/go-pgpmail v0.0.0-20200303213726-db035a3a4139 h1:JTUbkRuQFtDrl5KHWR2jrh9SUeSDEEEjUcHJkXdAE2Q= github.com/emersion/go-pgpmail v0.0.0-20200303213726-db035a3a4139/go.mod h1:+Ovy1VQCUKPdjWkOiWvFoiFaWXkqn1PA793VvfEYWQU= +github.com/emersion/go-sasl v0.0.0-20190520160400-47d427600317/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k= github.com/emersion/go-sasl v0.0.0-20190817083125-240c8404624e/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k= github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b h1:uhWtEWBHgop1rqEk2klKaxPAkVDCXexai6hSuRQ7Nvs= github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k= +github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ= +github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ= github.com/emersion/go-smtp v0.12.1 h1:1R8BDqrR2HhlGwgFYcOi+BVTvK1bMjAB65QcVpJ5sNA= github.com/emersion/go-smtp v0.12.1/go.mod h1:SD9V/xa4ndMw77lR3Mf7htkp8RBNYuPh9UeuBs9tpUQ= github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe h1:40SWqY0zE3qCi6ZrtTf5OUdNm5lDnGnjRSq9GgmeTrg= @@ -58,6 +64,7 @@ github.com/kyoh86/xdg v1.2.0/go.mod h1:/mg8zwu1+qe76oTFUBnyS7rJzk7LLC0VGEzJyJ19D github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s= github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac= github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/martinlindhe/base36 v0.0.0-20190418230009-7c6542dfbb41/go.mod h1:+AtEs8xrBpCeYgSLoY/aJ6Wf37jtBuR0s35750M27+8= github.com/martinlindhe/base36 v1.0.0 h1:eYsumTah144C0A8P1T/AVSUk5ZoLnhfYFM3OGQxB52A= github.com/martinlindhe/base36 v1.0.0/go.mod h1:+AtEs8xrBpCeYgSLoY/aJ6Wf37jtBuR0s35750M27+8= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= @@ -106,7 +113,6 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So= golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/worker/imap/open.go b/worker/imap/open.go index 0602a7f..891b8a2 100644 --- a/worker/imap/open.go +++ b/worker/imap/open.go @@ -2,6 +2,7 @@ package imap import ( "github.com/emersion/go-imap" + sortthread "github.com/emersion/go-imap-sortthread" "git.sr.ht/~sircmpwn/aerc/worker/types" ) @@ -30,9 +31,29 @@ func (imapw *IMAPWorker) handleFetchDirectoryContents( seqSet := &imap.SeqSet{} seqSet.AddRange(1, imapw.selected.Messages) - uids, err := imapw.client.UidSearch(&imap.SearchCriteria{ + + searchCriteria := &imap.SearchCriteria{ SeqNum: seqSet, - }) + } + sortCriteria := translateSortCriterions(msg.SortCriteria) + + var uids []uint32 + + // If the server supports the SORT extension, do the sorting server side + ok, err := imapw.client.sort.SupportSort() + if err == nil && ok && len(sortCriteria) > 0 { + uids, err = imapw.client.sort.UidSort(sortCriteria, searchCriteria) + // copy in reverse as msgList displays backwards + for i, j := 0, len(uids)-1; i < j; i, j = i+1, j-1 { + uids[i], uids[j] = uids[j], uids[i] + } + } else { + if err != nil { + // Non fatal, but we do want to print to get some debug info + imapw.worker.Logger.Printf("can't check for SORT support: %v", err) + } + uids, err = imapw.client.UidSearch(searchCriteria) + } if err != nil { imapw.worker.PostMessage(&types.Error{ Message: types.RespondTo(msg), @@ -50,3 +71,27 @@ func (imapw *IMAPWorker) handleFetchDirectoryContents( imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil) } } + +type sortFieldMapT map[types.SortField]sortthread.SortField + +// caution, incomplete mapping +var sortFieldMap sortFieldMapT = sortFieldMapT{ + types.SortArrival: sortthread.SortArrival, + types.SortCc: sortthread.SortCc, + types.SortDate: sortthread.SortDate, + types.SortFrom: sortthread.SortFrom, + types.SortSize: sortthread.SortSize, + types.SortSubject: sortthread.SortSubject, + types.SortTo: sortthread.SortTo, +} + +func translateSortCriterions( + cs []*types.SortCriterion) []sortthread.SortCriterion { + result := make([]sortthread.SortCriterion, 0, len(cs)) + for _, c := range cs { + if f, ok := sortFieldMap[c.Field]; ok { + result = append(result, sortthread.SortCriterion{f, c.Reverse}) + } + } + return result +} diff --git a/worker/imap/worker.go b/worker/imap/worker.go index c016af6..dab0afb 100644 --- a/worker/imap/worker.go +++ b/worker/imap/worker.go @@ -8,6 +8,7 @@ import ( "github.com/emersion/go-imap" idle "github.com/emersion/go-imap-idle" + sortthread "github.com/emersion/go-imap-sortthread" "github.com/emersion/go-imap/client" "golang.org/x/oauth2" @@ -27,6 +28,7 @@ var errUnsupported = fmt.Errorf("unsupported command") type imapClient struct { *client.Client idle *idle.IdleClient + sort *sortthread.SortClient } type IMAPWorker struct { @@ -155,7 +157,7 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error { } c.Updates = w.updates - w.client = &imapClient{c, idle.NewClient(c)} + w.client = &imapClient{c, idle.NewClient(c), sortthread.NewSortClient(c)} w.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil) case *types.ListDirectories: w.handleListDirectories(msg)