aboutsummaryrefslogtreecommitdiffstats
path: root/worker/imap/extensions/liststatus.go
blob: 44b93a761fe314a62109cbc52c0edb07c0d39855 (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package extensions

import (
	"fmt"
	"strings"

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

// A LIST-STATUS client
type ListStatusClient struct {
	c *client.Client
}

func NewListStatusClient(c *client.Client) *ListStatusClient {
	return &ListStatusClient{c}
}

// SupportListStatus checks if the server supports the LIST-STATUS extension.
func (c *ListStatusClient) SupportListStatus() (bool, error) {
	return c.c.Support("LIST-STATUS")
}

// ListStatus performs a LIST-STATUS command, listing mailboxes and also
// retrieving the requested status items. A nil channel can be passed in order
// to only retrieve the STATUS responses
func (c *ListStatusClient) ListStatus(
	ref string,
	name string,
	items []imap.StatusItem,
	ch chan *imap.MailboxInfo,
) ([]*imap.MailboxStatus, error) {
	if ch != nil {
		defer close(ch)
	}

	if c.c.State() != imap.AuthenticatedState && c.c.State() != imap.SelectedState {
		return nil, client.ErrNotLoggedIn
	}

	cmd := &ListStatusCommand{
		Reference: ref,
		Mailbox:   name,
		Items:     items,
	}
	res := &ListStatusResponse{Mailboxes: ch}

	status, err := c.c.Execute(cmd, res)
	if err != nil {
		return nil, err
	}
	return res.Statuses, status.Err()
}

// ListStatusCommand is a LIST command, as defined in RFC 3501 section 6.3.8. If
// Subscribed is set to true, LSUB will be used instead. Mailbox statuses will
// be returned if Items is not nil
type ListStatusCommand struct {
	Reference string
	Mailbox   string

	Subscribed bool
	Items      []imap.StatusItem
}

func (cmd *ListStatusCommand) Command() *imap.Command {
	name := "LIST"
	if cmd.Subscribed {
		name = "LSUB"
	}

	enc := utf7.Encoding.NewEncoder()
	ref, _ := enc.String(cmd.Reference)
	mailbox, _ := enc.String(cmd.Mailbox)

	items := make([]string, len(cmd.Items))
	if cmd.Items != nil {
		for i, item := range cmd.Items {
			items[i] = string(item)
		}
	}

	args := fmt.Sprintf("RETURN (STATUS (%s))", strings.Join(items, " "))
	return &imap.Command{
		Name:      name,
		Arguments: []interface{}{ref, mailbox, imap.RawString(args)},
	}
}

// A LIST-STATUS response
type ListStatusResponse struct {
	Mailboxes  chan *imap.MailboxInfo
	Subscribed bool
	Statuses   []*imap.MailboxStatus
}

func (r *ListStatusResponse) Name() string {
	if r.Subscribed {
		return "LSUB"
	} else {
		return "LIST"
	}
}

func (r *ListStatusResponse) Handle(resp imap.Resp) error {
	name, _, ok := imap.ParseNamedResp(resp)
	if !ok {
		return responses.ErrUnhandled
	}
	switch name {
	case "LIST":
		if r.Mailboxes == nil {
			return nil
		}
		res := responses.List{Mailboxes: r.Mailboxes}
		return res.Handle(resp)
	case "STATUS":
		res := responses.Status{
			Mailbox: new(imap.MailboxStatus),
		}
		err := res.Handle(resp)
		if err != nil {
			return err
		}
		r.Statuses = append(r.Statuses, res.Mailbox)
	default:
		return responses.ErrUnhandled
	}

	return nil
}

func (r *ListStatusResponse) 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
}