Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imapclient: add support for MULTIAPPEND #531

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions imapclient/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}