-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
91 lines (73 loc) · 1.85 KB
/
client.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
package quota
import (
"fmt"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
)
// Client is a QUOTA client.
type Client struct {
c *client.Client
}
// NewClient creates a new client.
func NewClient(c *client.Client) *Client {
return &Client{c: c}
}
// SupportQuota checks if the server supports the QUOTA extension.
func (c *Client) SupportQuota() (bool, error) {
return c.c.Support(Capability)
}
// SetQuota changes the resource limits for the specified quota root. Any
// previous resource limits for the named quota root are discarded.
func (c *Client) SetQuota(root string, resources map[string]uint32) error {
if c.c.State()&imap.AuthenticatedState == 0 {
return client.ErrNotLoggedIn
}
cmd := &SetCommand{
Root: root,
Resources: resources,
}
status, err := c.c.Execute(cmd, nil)
if err != nil {
return err
}
return status.Err()
}
// GetQuota returns a quota root's resource usage and limits.
func (c *Client) GetQuota(root string) (*Status, error) {
if c.c.State()&imap.AuthenticatedState == 0 {
return nil, client.ErrNotLoggedIn
}
cmd := &GetCommand{
Root: root,
}
res := &Response{}
status, err := c.c.Execute(cmd, res)
if err != nil {
return nil, err
}
if err := status.Err(); err != nil {
return nil, err
}
if len(res.Quotas) != 1 {
return nil, fmt.Errorf("Expected exactly one QUOTA response, got %v", len(res.Quotas))
}
return res.Quotas[0], nil
}
// GetQuotaRoot returns the list of quota roots for a mailbox.
func (c *Client) GetQuotaRoot(mailbox string) ([]*Status, error) {
if c.c.State()&imap.AuthenticatedState == 0 {
return nil, client.ErrNotLoggedIn
}
cmd := &GetRootCommand{
Mailbox: mailbox,
}
res := &Response{}
status, err := c.c.Execute(cmd, res)
if err != nil {
return nil, err
}
if err := status.Err(); err != nil {
return nil, err
}
return res.Quotas, nil
}