From b1eb7ad18d2e0bbeecaf61a58825bbc794ceb40c Mon Sep 17 00:00:00 2001 From: Srivathsan Murali Date: Mon, 25 May 2020 16:59:48 +0200 Subject: Set AnsweredFlag on successful reply --- worker/maildir/message.go | 20 ++++++++++++++++++++ worker/maildir/worker.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) (limited to 'worker/maildir') diff --git a/worker/maildir/message.go b/worker/maildir/message.go index 9d622b45..5c6c9307 100644 --- a/worker/maildir/message.go +++ b/worker/maildir/message.go @@ -53,6 +53,26 @@ func (m Message) SetFlags(flags []maildir.Flag) error { return m.dir.SetFlags(m.key, flags) } +// MarkReplied either adds or removes the maildir.FlagReplied flag from the +// message. +func (m Message) MarkReplied(answered bool) error { + flags, err := m.Flags() + if err != nil { + return fmt.Errorf("could not read previous flags: %v", err) + } + if answered { + flags = append(flags, maildir.FlagReplied) + return m.SetFlags(flags) + } + var newFlags []maildir.Flag + for _, flag := range flags { + if flag != maildir.FlagReplied { + newFlags = append(newFlags, flag) + } + } + return m.SetFlags(newFlags) +} + // MarkRead either adds or removes the maildir.FlagSeen flag from the message. func (m Message) MarkRead(seen bool) error { flags, err := m.Flags() diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go index dbdb9252..f14672e5 100644 --- a/worker/maildir/worker.go +++ b/worker/maildir/worker.go @@ -195,6 +195,8 @@ func (w *Worker) handleMessage(msg types.WorkerMessage) error { return w.handleDeleteMessages(msg) case *types.ReadMessages: return w.handleReadMessages(msg) + case *types.AnsweredMessages: + return w.handleAnsweredMessages(msg) case *types.CopyMessages: return w.handleCopyMessages(msg) case *types.AppendMessage: @@ -438,6 +440,39 @@ func (w *Worker) handleDeleteMessages(msg *types.DeleteMessages) error { return nil } +func (w *Worker) handleAnsweredMessages(msg *types.AnsweredMessages) error { + for _, uid := range msg.Uids { + m, err := w.c.Message(*w.selected, uid) + if err != nil { + w.worker.Logger.Printf("could not get message: %v", err) + w.err(msg, err) + continue + } + if err := m.MarkReplied(msg.Answered); err != nil { + w.worker.Logger.Printf( + "could not mark message as answered: %v", err) + w.err(msg, err) + continue + } + info, err := m.MessageInfo() + if err != nil { + w.worker.Logger.Printf("could not get message info: %v", err) + w.err(msg, err) + continue + } + + w.worker.PostMessage(&types.MessageInfo{ + Message: types.RespondTo(msg), + Info: info, + }, nil) + + w.worker.PostMessage(&types.DirectoryInfo{ + Info: w.getDirectoryInfo(w.selectedName), + }, nil) + } + return nil +} + func (w *Worker) handleReadMessages(msg *types.ReadMessages) error { for _, uid := range msg.Uids { m, err := w.c.Message(*w.selected, uid) -- cgit