From 276ff4ae1590b400da1a34fd9f3f5d4663a3bb3f Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Thu, 29 Jun 2023 10:34:23 +0200 Subject: [PATCH] imapclient: add support for MULTIAPPEND --- imapclient/append.go | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/imapclient/append.go b/imapclient/append.go index 4102ed1e..903f8e9e 100644 --- a/imapclient/append.go +++ b/imapclient/append.go @@ -54,3 +54,57 @@ func (cmd *AppendCommand) Close() error { func (cmd *AppendCommand) Wait() (*imap.AppendData, error) { return &cmd.data, cmd.cmd.Wait() } + +// MultiAppend sends an APPEND command with multiple messages. +// +// The caller must call MultiAppendCommand.Close. +// +// This command requires support for the MULTIAPPEND extension. +func (c *Client) MultiAppend(mailbox string) *MultiAppendCommand { + cmd := &MultiAppendCommand{} + cmd.enc = c.beginCommand("APPEND", cmd) + cmd.enc.SP().Mailbox(mailbox) + return cmd +} + +// MultiAppendCommand is an APPEND command with multiple messages. +type MultiAppendCommand struct { + cmd + enc *commandEncoder + wc io.WriteCloser +} + +// CreateMessage appends a new message. +func (cmd *MultiAppendCommand) CreateMessage(size int64, options *imap.AppendOptions) io.Writer { + if cmd.wc != nil { + // TODO: handle error + cmd.wc.Close() + cmd.wc = nil + } + + cmd.enc.SP() + if options != nil && len(options.Flags) > 0 { + cmd.enc.List(len(options.Flags), func(i int) { + cmd.enc.Flag(options.Flags[i]) + }).SP() + } + if options != nil && !options.Time.IsZero() { + cmd.enc.String(options.Time.Format(internal.DateTimeLayout)).SP() + } + cmd.wc = cmd.enc.Literal(size) + return cmd.wc +} + +// Close ends the APPEND command. +func (cmd *MultiAppendCommand) Close() error { + err := cmd.wc.Close() + if cmd.enc != nil { + cmd.enc.end() + cmd.enc = nil + } + return err +} + +func (cmd *MultiAppendCommand) Wait() error { + return cmd.cmd.Wait() +}