From 312a53e5ff721e0a29e34aaeceb0eece1203002d Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 20 Mar 2019 23:23:38 -0400 Subject: Implement :delete-message --- worker/imap/fetch.go | 1 + worker/imap/flags.go | 43 +++++++++++++++++++++++++++++++++++++++++++ worker/imap/open.go | 1 + worker/imap/worker.go | 6 +++++- 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 worker/imap/flags.go (limited to 'worker/imap') diff --git a/worker/imap/fetch.go b/worker/imap/fetch.go index 383a8a8..489dbe4 100644 --- a/worker/imap/fetch.go +++ b/worker/imap/fetch.go @@ -25,6 +25,7 @@ func (imapw *IMAPWorker) handleFetchMessageHeaders( }() go func() { for msg := range messages { + imapw.seqMap[msg.SeqNum-1] = msg.Uid imapw.worker.PostMessage(&types.MessageInfo{ Envelope: msg.Envelope, Flags: msg.Flags, diff --git a/worker/imap/flags.go b/worker/imap/flags.go new file mode 100644 index 0000000..cb9b3b1 --- /dev/null +++ b/worker/imap/flags.go @@ -0,0 +1,43 @@ +package imap + +import ( + "github.com/emersion/go-imap" + + "git.sr.ht/~sircmpwn/aerc2/worker/types" +) + +func (imapw *IMAPWorker) handleDeleteMessages(msg *types.DeleteMessages) { + item := imap.FormatFlagsOp(imap.AddFlags, true) + flags := []interface{}{imap.DeletedFlag} + if err := imapw.client.UidStore(&msg.Uids, item, flags, nil); err != nil { + imapw.worker.PostMessage(&types.Error{ + Message: types.RespondTo(msg), + Error: err, + }, nil) + return + } + var deleted []uint32 + ch := make(chan uint32) + done := make(chan interface{}) + go func() { + for seqNum := range ch { + i := seqNum - 1 + deleted = append(deleted, imapw.seqMap[i]) + imapw.seqMap = append(imapw.seqMap[:i], imapw.seqMap[i+1:]...) + } + done <- nil + }() + if err := imapw.client.Expunge(ch); err != nil { + imapw.worker.PostMessage(&types.Error{ + Message: types.RespondTo(msg), + Error: err, + }, nil) + } else { + <-done + imapw.worker.PostMessage(&types.MessagesDeleted{ + Message: types.RespondTo(msg), + Uids: deleted, + }, nil) + imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil) + } +} diff --git a/worker/imap/open.go b/worker/imap/open.go index 87c4fb3..3705bc0 100644 --- a/worker/imap/open.go +++ b/worker/imap/open.go @@ -39,6 +39,7 @@ func (imapw *IMAPWorker) handleFetchDirectoryContents( }, nil) } else { imapw.worker.Logger.Printf("Found %d UIDs", len(uids)) + imapw.seqMap = make([]uint32, len(uids)) imapw.worker.PostMessage(&types.DirectoryContents{ Message: types.RespondTo(msg), Uids: uids, diff --git a/worker/imap/worker.go b/worker/imap/worker.go index 1646165..ea7f317 100644 --- a/worker/imap/worker.go +++ b/worker/imap/worker.go @@ -33,12 +33,14 @@ type IMAPWorker struct { selected imap.MailboxStatus updates chan client.Update worker *types.Worker + // Map of sequence numbers to UIDs, index 0 is seq number 1 + seqMap []uint32 } func NewIMAPWorker(worker *types.Worker) *IMAPWorker { return &IMAPWorker{ - worker: worker, updates: make(chan client.Update, 50), + worker: worker, } } @@ -156,6 +158,8 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error { w.handleFetchDirectoryContents(msg) case *types.FetchMessageHeaders: w.handleFetchMessageHeaders(msg) + case *types.DeleteMessages: + w.handleDeleteMessages(msg) default: return errUnsupported } -- cgit v1.2.3