diff options
author | 2020-01-24 17:49:50 +0100 | |
---|---|---|
committer | 2020-01-24 17:49:50 +0100 | |
commit | bfc617b70267f299e3117eae33b5fd8f23e25500 (patch) | |
tree | 7f85ed17d33cd6eb7111f568fa0829aab76d4593 /plugins/base/imap.go | |
parent | 267999b6e5604a95bcd86417853d4569c7ec5b33 (diff) | |
download | alps-bfc617b70267f299e3117eae33b5fd8f23e25500.tar.gz alps-bfc617b70267f299e3117eae33b5fd8f23e25500.tar.bz2 alps-bfc617b70267f299e3117eae33b5fd8f23e25500.zip |
plugins/base: save message as draft
Diffstat (limited to 'plugins/base/imap.go')
-rwxr-xr-x | plugins/base/imap.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/plugins/base/imap.go b/plugins/base/imap.go index 0c39ca6..a9de217 100755 --- a/plugins/base/imap.go +++ b/plugins/base/imap.go @@ -2,12 +2,15 @@ package koushinbase import ( "bufio" + "bytes" "fmt" "sort" "strconv" "strings" + "time" "github.com/emersion/go-imap" + imapspecialuse "github.com/emersion/go-imap-specialuse" imapclient "github.com/emersion/go-imap/client" "github.com/emersion/go-message" "github.com/emersion/go-message/textproto" @@ -375,3 +378,45 @@ func markMessageAnswered(conn *imapclient.Client, mboxName string, uid uint32) e flags := []interface{}{imap.AnsweredFlag} return conn.UidStore(seqSet, item, flags, nil) } + +type mailboxType int + +const ( + mailboxSent mailboxType = iota + mailboxDrafts +) + +func appendMessage(c *imapclient.Client, msg *OutgoingMessage, mboxType mailboxType) (saved bool, err error) { + var mboxAttr string + switch mboxType { + case mailboxSent: + mboxAttr = imapspecialuse.Sent + case mailboxDrafts: + mboxAttr = imapspecialuse.Drafts + } + + mbox, err := getMailboxByAttribute(c, mboxAttr) + if err != nil { + return false, err + } + if mbox == nil { + return false, nil + } + + // IMAP needs to know in advance the final size of the message, so + // there's no way around storing it in a buffer here. + var buf bytes.Buffer + if err := msg.WriteTo(&buf); err != nil { + return false, err + } + + flags := []string{imap.SeenFlag} + if mboxType == mailboxDrafts { + flags = append(flags, imap.DraftFlag) + } + if err := c.Append(mbox.Name, flags, time.Now(), &buf); err != nil { + return false, err + } + + return true, nil +} |