From b0bf09b98fc038c1bc9921d568c06260b7448a15 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 15 May 2019 19:41:21 -0400 Subject: Copy sent emails to the Sent folder Or rather, to a user-specified folder --- worker/imap/movecopy.go | 27 +++++++++++++++++++++++++++ worker/imap/worker.go | 4 ++++ worker/types/messages.go | 20 ++++++++++++++++++++ worker/types/worker.go | 16 ++++++++++++---- 4 files changed, 63 insertions(+), 4 deletions(-) (limited to 'worker') diff --git a/worker/imap/movecopy.go b/worker/imap/movecopy.go index 8c42a2e..1234e60 100644 --- a/worker/imap/movecopy.go +++ b/worker/imap/movecopy.go @@ -1,6 +1,8 @@ package imap import ( + "io" + "git.sr.ht/~sircmpwn/aerc2/worker/types" ) @@ -14,3 +16,28 @@ func (imapw *IMAPWorker) handleCopyMessages(msg *types.CopyMessages) { imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil) } } + +type appendLiteral struct { + io.Reader + Length int +} + +func (m appendLiteral) Len() int { + return m.Length +} + +func (imapw *IMAPWorker) handleAppendMessage(msg *types.AppendMessage) { + if err := imapw.client.Append(msg.Destination, msg.Flags, msg.Date, + &appendLiteral{ + Reader: msg.Reader, + Length: msg.Length, + }); 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 1fab3bb..5dce18e 100644 --- a/worker/imap/worker.go +++ b/worker/imap/worker.go @@ -153,6 +153,8 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error { } } + c.SetDebug(w.worker.Logger.Writer()) + if _, err := c.Select(imap.InboxName, false); err != nil { return err } @@ -176,6 +178,8 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error { w.handleDeleteMessages(msg) case *types.CopyMessages: w.handleCopyMessages(msg) + case *types.AppendMessage: + w.handleAppendMessage(msg) default: return errUnsupported } diff --git a/worker/types/messages.go b/worker/types/messages.go index e97ae02..8687b5e 100644 --- a/worker/types/messages.go +++ b/worker/types/messages.go @@ -12,10 +12,13 @@ import ( type WorkerMessage interface { InResponseTo() WorkerMessage + getId() int + setId(id int) } type Message struct { inResponseTo WorkerMessage + id int } func RespondTo(msg WorkerMessage) Message { @@ -28,6 +31,14 @@ func (m Message) InResponseTo() WorkerMessage { return m.inResponseTo } +func (m Message) getId() int { + return m.id +} + +func (m Message) setId(id int) { + m.id = id +} + // Meta-messages type Done struct { @@ -103,6 +114,15 @@ type CopyMessages struct { Uids imap.SeqSet } +type AppendMessage struct { + Message + Destination string + Flags []string + Date time.Time + Reader io.Reader + Length int +} + // Messages type CertificateApprovalRequest struct { diff --git a/worker/types/worker.go b/worker/types/worker.go index 0ed216a..e2af61f 100644 --- a/worker/types/worker.go +++ b/worker/types/worker.go @@ -5,6 +5,8 @@ import ( "sync" ) +var nextId int = 1 + type Backend interface { Run() } @@ -15,7 +17,7 @@ type Worker struct { Messages chan WorkerMessage Logger *log.Logger - callbacks map[WorkerMessage]func(msg WorkerMessage) // protected by mutex + callbacks map[int]func(msg WorkerMessage) // protected by mutex mutex sync.Mutex } @@ -24,16 +26,19 @@ func NewWorker(logger *log.Logger) *Worker { Actions: make(chan WorkerMessage, 50), Messages: make(chan WorkerMessage, 50), Logger: logger, - callbacks: make(map[WorkerMessage]func(msg WorkerMessage)), + callbacks: make(map[int]func(msg WorkerMessage)), } } func (worker *Worker) setCallback(msg WorkerMessage, cb func(msg WorkerMessage)) { + msg.setId(nextId) + nextId++ + if cb != nil { worker.mutex.Lock() - worker.callbacks[msg] = cb + worker.callbacks[msg.getId()] = cb worker.mutex.Unlock() } } @@ -41,8 +46,11 @@ func (worker *Worker) setCallback(msg WorkerMessage, func (worker *Worker) getCallback(msg WorkerMessage) (func(msg WorkerMessage), bool) { + if msg == nil { + return nil, false + } worker.mutex.Lock() - cb, ok := worker.callbacks[msg] + cb, ok := worker.callbacks[msg.getId()] worker.mutex.Unlock() return cb, ok -- cgit v1.2.3