From 3384c39a1773c90eb4575d4017fcd92b9e9ab6f9 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 24 Jan 2020 20:27:05 +0100 Subject: [PATCH] plugins/base: delete previous draft --- plugins/base/imap.go | 16 ++++++++++++++++ plugins/base/routes.go | 23 ++++++++++++++++++----- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/plugins/base/imap.go b/plugins/base/imap.go index 980a8aa..280a06b 100755 --- a/plugins/base/imap.go +++ b/plugins/base/imap.go @@ -436,3 +436,19 @@ func appendMessage(c *imapclient.Client, msg *OutgoingMessage, mboxType mailboxT return true, nil } + +func deleteMessage(c *imapclient.Client, mboxName string, uid uint32) error { + if err := ensureMailboxSelected(c, mboxName); err != nil { + return err + } + + seqSet := new(imap.SeqSet) + seqSet.AddNum(uid) + item := imap.FormatFlagsOp(imap.AddFlags, true) + flags := []interface{}{imap.DeletedFlag} + if err := c.UidStore(seqSet, item, flags, nil); err != nil { + return err + } + + return c.Expunge(nil) +} diff --git a/plugins/base/routes.go b/plugins/base/routes.go index 49f7674..bf0bc5a 100644 --- a/plugins/base/routes.go +++ b/plugins/base/routes.go @@ -288,7 +288,7 @@ type messagePath struct { // Send message, append it to the Sent mailbox, mark the original message as // answered -func submitCompose(ctx *koushin.Context, msg *OutgoingMessage, inReplyTo *messagePath) error { +func submitCompose(ctx *koushin.Context, msg *OutgoingMessage, draft *messagePath, inReplyTo *messagePath) error { err := ctx.Session.DoSMTP(func(c *smtp.Client) error { return sendMessage(c, msg) }) @@ -309,8 +309,15 @@ func submitCompose(ctx *koushin.Context, msg *OutgoingMessage, inReplyTo *messag } err = ctx.Session.DoIMAP(func(c *imapclient.Client) error { - _, err := appendMessage(c, msg, mailboxSent) - return err + if _, err := appendMessage(c, msg, mailboxSent); err != nil { + return err + } + if draft != nil { + if err := deleteMessage(c, draft.Mailbox, draft.Uid); err != nil { + return err + } + } + return nil }) if err != nil { return fmt.Errorf("failed to save message to Sent mailbox: %v", err) @@ -319,7 +326,7 @@ func submitCompose(ctx *koushin.Context, msg *OutgoingMessage, inReplyTo *messag return ctx.Redirect(http.StatusFound, "/mailbox/INBOX") } -func handleCompose(ctx *koushin.Context, msg *OutgoingMessage, source *messagePath, inReplyTo *messagePath) error { +func handleCompose(ctx *koushin.Context, msg *OutgoingMessage, draft *messagePath, inReplyTo *messagePath) error { if msg.From == "" && strings.ContainsRune(ctx.Session.Username(), '@') { msg.From = ctx.Session.Username() } @@ -352,13 +359,19 @@ func handleCompose(ctx *koushin.Context, msg *OutgoingMessage, source *messagePa if !copied { return fmt.Errorf("no Draft mailbox found") } + if draft != nil { + if err := deleteMessage(c, draft.Mailbox, draft.Uid); err != nil { + return err + } + } return nil }) if err != nil { return fmt.Errorf("failed to save message to Draft mailbox: %v", err) } + return ctx.Redirect(http.StatusFound, "/mailbox/INBOX") } else { - return submitCompose(ctx, msg, inReplyTo) + return submitCompose(ctx, msg, draft, inReplyTo) } }