aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/emersion/go-imap/responses/status.go
blob: 6a8570c9b226b8f4218939d31236dcdbb6e86d59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package responses

import (
	"errors"

	"github.com/emersion/go-imap"
	"github.com/emersion/go-imap/utf7"
)

const statusName = "STATUS"

// A STATUS response.
// See RFC 3501 section 7.2.4
type Status struct {
	Mailbox *imap.MailboxStatus
}

func (r *Status) Handle(resp imap.Resp) error {
	if r.Mailbox == nil {
		r.Mailbox = &imap.MailboxStatus{}
	}
	mbox := r.Mailbox

	name, fields, ok := imap.ParseNamedResp(resp)
	if !ok || name != statusName {
		return ErrUnhandled
	} else if len(fields) < 2 {
		return errNotEnoughFields
	}

	if name, err := imap.ParseString(fields[0]); err != nil {
		return err
	} else if name, err := utf7.Encoding.NewDecoder().String(name); err != nil {
		return err
	} else {
		mbox.Name = imap.CanonicalMailboxName(name)
	}

	var items []interface{}
	if items, ok = fields[1].([]interface{}); !ok {
		return errors.New("STATUS response expects a list as second argument")
	}

	mbox.Items = nil
	return mbox.Parse(items)
}

func (r *Status) WriteTo(w *imap.Writer) error {
	mbox := r.Mailbox
	name, _ := utf7.Encoding.NewEncoder().String(mbox.Name)
	fields := []interface{}{imap.RawString(statusName), imap.FormatMailboxName(name), mbox.Format()}
	return imap.NewUntaggedResp(fields).WriteTo(w)
}