-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresponses.go
176 lines (145 loc) · 3.55 KB
/
responses.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package quota
import (
"errors"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/responses"
"github.com/emersion/go-imap/utf7"
)
const (
responseName = "QUOTA"
rootResponseName = "QUOTAROOT"
)
// A quota status.
type Status struct {
// The quota name.
Name string
// The quota resources. Each resource is indexed by its name and contains its
// current usage as well as its limit.
Resources map[string][2]uint32
}
func (rs *Status) Parse(fields []interface{}) error {
if len(fields) < 2 {
return errors.New("No enough arguments")
}
var ok bool
if rs.Name, ok = fields[0].(string); !ok {
return errors.New("Quota root must be a string")
}
resources, ok := fields[1].([]interface{})
if !ok {
return errors.New("Resources must be a list")
}
var name string
var usage, limit uint32
var err error
for i, v := range resources {
if ii := i % 3; ii == 0 {
if name, ok = v.(string); !ok {
return errors.New("Resource name must be a string")
}
} else if ii == 1 {
if usage, err = imap.ParseNumber(v); err != nil {
return err
}
} else {
if limit, err = imap.ParseNumber(v); err != nil {
return err
}
rs.Resources[name] = [2]uint32{usage, limit}
}
}
return nil
}
func (rs *Status) Format() (fields []interface{}) {
fields = append(fields, rs.Name)
var resources []interface{}
for k, v := range rs.Resources {
resources = append(resources, imap.RawString(k), v[0], v[1])
}
fields = append(fields, resources)
return
}
// A QUOTA response. See RFC 2087 section 5.1.
type Response struct {
Quotas []*Status
}
func (r *Response) Handle(resp imap.Resp) error {
name, fields, ok := imap.ParseNamedResp(resp)
if !ok || name != responseName {
return responses.ErrUnhandled
}
quota := &Status{Resources: make(map[string][2]uint32)}
if err := quota.Parse(fields); err != nil {
return err
}
r.Quotas = append(r.Quotas, quota)
return nil
}
func (r *Response) WriteTo(w *imap.Writer) error {
for _, quota := range r.Quotas {
fields := []interface{}{imap.RawString(responseName)}
fields = append(fields, quota.Format()...)
res := imap.NewUntaggedResp(fields)
if err := res.WriteTo(w); err != nil {
return err
}
}
return nil
}
type MailboxRoots struct {
Name string
Roots []string
}
func (m *MailboxRoots) Parse(fields []interface{}) error {
if len(fields) < 1 {
return errors.New("No enough arguments")
}
mailbox, ok := fields[0].(string)
if !ok {
return errors.New("Mailbox name must be a string")
}
var err error
if m.Name, err = utf7.Encoding.NewDecoder().String(mailbox); err != nil {
return err
}
for _, f := range fields[1:] {
root, ok := f.(string)
if !ok {
return errors.New("Quota root must be a string")
}
m.Roots = append(m.Roots, root)
}
return nil
}
func (m *MailboxRoots) Format() (fields []interface{}) {
fields = append(fields, m.Name)
for _, root := range m.Roots {
fields = append(fields, root)
}
return
}
// A QUOTAROOT response. See RFC 2087 section 5.1.
type RootResponse struct {
Mailbox *MailboxRoots
}
func (r *RootResponse) Handle(resp imap.Resp) error {
name, fields, ok := imap.ParseNamedResp(resp)
if !ok || name != rootResponseName {
return responses.ErrUnhandled
}
m := &MailboxRoots{}
if err := m.Parse(fields); err != nil {
return err
}
r.Mailbox = m
return nil
}
func (r *RootResponse) WriteTo(w *imap.Writer) (err error) {
fields := []interface{}{imap.RawString(rootResponseName)}
fields = append(fields, r.Mailbox.Format()...)
res := imap.NewUntaggedResp(fields)
if err = res.WriteTo(w); err != nil {
return
}
return
}