aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/emersion/go-imap/responses/list.go
blob: e080fc16dbb279293c661726c185952105eeeef3 (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
54
55
56
57
package responses

import (
	"github.com/emersion/go-imap"
)

const (
	listName = "LIST"
	lsubName = "LSUB"
)

// A LIST response.
// If Subscribed is set to true, LSUB will be used instead.
// See RFC 3501 section 7.2.2
type List struct {
	Mailboxes  chan *imap.MailboxInfo
	Subscribed bool
}

func (r *List) Name() string {
	if r.Subscribed {
		return lsubName
	} else {
		return listName
	}
}

func (r *List) Handle(resp imap.Resp) error {
	name, fields, ok := imap.ParseNamedResp(resp)
	if !ok || name != r.Name() {
		return ErrUnhandled
	}

	mbox := &imap.MailboxInfo{}
	if err := mbox.Parse(fields); err != nil {
		return err
	}

	r.Mailboxes <- mbox
	return nil
}

func (r *List) WriteTo(w *imap.Writer) error {
	respName := r.Name()

	for mbox := range r.Mailboxes {
		fields := []interface{}{imap.RawString(respName)}
		fields = append(fields, mbox.Format()...)

		resp := imap.NewUntaggedResp(fields)
		if err := resp.WriteTo(w); err != nil {
			return err
		}
	}

	return nil
}