From 2750f99a6049322b1361b5bfa4dff3b1df2b36ab Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 13 Jan 2019 16:18:10 -0500 Subject: [PATCH] Issue IMAP SELECT command --- widgets/directories.go | 1 + worker/imap/open.go | 20 ++++++++++++++++++++ worker/imap/worker.go | 20 +++++++++++++++++++- worker/types/messages.go | 13 +++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 worker/imap/open.go diff --git a/widgets/directories.go b/widgets/directories.go index 1602b3c..13018bb 100644 --- a/widgets/directories.go +++ b/widgets/directories.go @@ -47,6 +47,7 @@ func (dirlist *DirectoryList) UpdateList(done func(dirs []string)) { func (dirlist *DirectoryList) Select(name string) { dirlist.selected = name + dirlist.worker.PostAction(&types.OpenDirectory{Directory: name}, nil) dirlist.Invalidate() } diff --git a/worker/imap/open.go b/worker/imap/open.go new file mode 100644 index 0000000..d90a292 --- /dev/null +++ b/worker/imap/open.go @@ -0,0 +1,20 @@ +package imap + +import ( + "git.sr.ht/~sircmpwn/aerc2/worker/types" +) + +func (imapw *IMAPWorker) handleOpenDirectory(msg *types.OpenDirectory) { + imapw.worker.Logger.Printf("Opening %s", msg.Directory) + go func() { + _, err := imapw.client.Select(msg.Directory, false) + if err != nil { + imapw.worker.PostMessage(&types.Error{ + Message: types.RespondTo(msg), + Error: err, + }, nil) + } else { + imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil) + } + }() +} diff --git a/worker/imap/worker.go b/worker/imap/worker.go index d75dbb0..4381e22 100644 --- a/worker/imap/worker.go +++ b/worker/imap/worker.go @@ -149,12 +149,30 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error { w.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil) case *types.ListDirectories: w.handleListDirectories(msg) + case *types.OpenDirectory: + w.handleOpenDirectory(msg) default: return errUnsupported } return nil } +func (w *IMAPWorker) handleImapUpdate(update client.Update) { + w.worker.Logger.Printf("(= %T", update) + switch update := update.(type) { + case *client.MailboxUpdate: + status := update.Mailbox + w.worker.PostMessage(&types.DirectoryInfo{ + ReadOnly: status.ReadOnly, + Flags: status.Flags, + + Exists: int(status.Messages), + Recent: int(status.Recent), + Unseen: int(status.Unseen), + }, nil) + } +} + func (w *IMAPWorker) Run() { for { select { @@ -171,7 +189,7 @@ func (w *IMAPWorker) Run() { }, nil) } case update := <-w.updates: - w.worker.Logger.Printf("(= %T", update) + w.handleImapUpdate(update) } } } diff --git a/worker/types/messages.go b/worker/types/messages.go index 69df6ca..27937ee 100644 --- a/worker/types/messages.go +++ b/worker/types/messages.go @@ -63,6 +63,11 @@ type ListDirectories struct { Message } +type OpenDirectory struct { + Message + Directory string +} + // Messages type CertificateApprovalRequest struct { @@ -75,3 +80,11 @@ type Directory struct { Attributes []string Name string } + +type DirectoryInfo struct { + Message + ReadOnly bool + Flags []string + + Exists, Recent, Unseen int +}