From b3896476a0e6978c0d7f6fedfb64588934a78f1e Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 10 Mar 2019 23:45:00 -0400 Subject: Fetch valid UIDs from server after opening dir --- worker/imap/open.go | 34 ++++++++++++++++++ worker/imap/worker.go | 12 +++++-- worker/messages.go | 91 ------------------------------------------------ worker/types/messages.go | 33 ++++++++++++++++++ 4 files changed, 76 insertions(+), 94 deletions(-) delete mode 100644 worker/messages.go (limited to 'worker') diff --git a/worker/imap/open.go b/worker/imap/open.go index d90a292..0f25c5e 100644 --- a/worker/imap/open.go +++ b/worker/imap/open.go @@ -1,6 +1,8 @@ package imap import ( + "github.com/emersion/go-imap" + "git.sr.ht/~sircmpwn/aerc2/worker/types" ) @@ -18,3 +20,35 @@ func (imapw *IMAPWorker) handleOpenDirectory(msg *types.OpenDirectory) { } }() } + +func (imapw *IMAPWorker) handleFetchDirectoryContents( + msg *types.FetchDirectoryContents) { + + imapw.worker.Logger.Printf("Fetching UID list") + + go func() { + seqSet := &imap.SeqSet{} + seqSet.AddRange(1, imapw.selected.Messages) + uid32, err := imapw.client.UidSearch(&imap.SearchCriteria{ + SeqNum: seqSet, + }) + if err != nil { + imapw.worker.PostMessage(&types.Error{ + Message: types.RespondTo(msg), + Error: err, + }, nil) + } else { + imapw.worker.Logger.Printf("Found %d UIDs", len(uid32)) + var uids []uint64 + for _, uid := range uid32 { + uids = append(uids, + (uint64(imapw.selected.UidValidity)<<32)|uint64(uid)) + } + imapw.worker.PostMessage(&types.DirectoryContents{ + Message: types.RespondTo(msg), + Uids: uids, + }, nil) + imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil) + } + }() +} diff --git a/worker/imap/worker.go b/worker/imap/worker.go index f6685c1..51cb221 100644 --- a/worker/imap/worker.go +++ b/worker/imap/worker.go @@ -29,9 +29,10 @@ type IMAPWorker struct { user *url.Userinfo } - worker *types.Worker - client *imapClient - updates chan client.Update + client *imapClient + selected imap.MailboxStatus + updates chan client.Update + worker *types.Worker } func NewIMAPWorker(worker *types.Worker) *IMAPWorker { @@ -151,6 +152,8 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error { w.handleListDirectories(msg) case *types.OpenDirectory: w.handleOpenDirectory(msg) + case *types.FetchDirectoryContents: + w.handleFetchDirectoryContents(msg) default: return errUnsupported } @@ -162,6 +165,9 @@ func (w *IMAPWorker) handleImapUpdate(update client.Update) { switch update := update.(type) { case *client.MailboxUpdate: status := update.Mailbox + if w.selected.Name == status.Name { + w.selected = *status + } w.worker.PostMessage(&types.DirectoryInfo{ Flags: status.Flags, Name: status.Name, diff --git a/worker/messages.go b/worker/messages.go deleted file mode 100644 index 90fcfa0..0000000 --- a/worker/messages.go +++ /dev/null @@ -1,91 +0,0 @@ -package worker - -import ( - "crypto/x509" - - "git.sr.ht/~sircmpwn/aerc2/config" -) - -type WorkerMessage interface { - InResponseTo() WorkerMessage -} - -type Message struct { - inResponseTo WorkerMessage -} - -func RespondTo(msg WorkerMessage) Message { - return Message{ - inResponseTo: msg, - } -} - -func (m Message) InResponseTo() WorkerMessage { - return m.inResponseTo -} - -// Meta-messages - -type Done struct { - Message -} - -type Error struct { - Message - Error error -} - -type Unsupported struct { - Message -} - -// Actions - -type ApproveCertificate struct { - Message - Approved bool -} - -type Configure struct { - Message - Config *config.AccountConfig -} - -type Connect struct { - Message -} - -type Disconnect struct { - Message -} - -type ListDirectories struct { - Message -} - -type OpenDirectory struct { - Message - Directory string -} - -// Messages - -type CertificateApprovalRequest struct { - Message - CertPool *x509.CertPool -} - -type Directory struct { - Message - Attributes []string - Name string -} - -type DirectoryInfo struct { - Message - Flags []string - Name string - ReadOnly bool - - Exists, Recent, Unseen int -} diff --git a/worker/types/messages.go b/worker/types/messages.go index 02a3119..d44624d 100644 --- a/worker/types/messages.go +++ b/worker/types/messages.go @@ -2,6 +2,10 @@ package types import ( "crypto/x509" + "net/mail" + "time" + + "github.com/emersion/go-imap" "git.sr.ht/~sircmpwn/aerc2/config" ) @@ -68,6 +72,20 @@ type OpenDirectory struct { Directory string } +type FetchDirectoryContents struct { + Message +} + +type FetchMessageHeaders struct { + Message + Uids imap.SeqSet +} + +type FetchMessageBodies struct { + Message + Uids imap.SeqSet +} + // Messages type CertificateApprovalRequest struct { @@ -89,3 +107,18 @@ type DirectoryInfo struct { Exists, Recent, Unseen int } + +type DirectoryContents struct { + Message + Uids []uint64 +} + +type MessageInfo struct { + Message + Envelope *imap.Envelope + Flags []string + InternalDate time.Time + Mail *mail.Message + Size uint32 + Uid uint64 +} -- cgit v1.2.3