-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommands.go
124 lines (101 loc) · 2.35 KB
/
commands.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
package quota
import (
"errors"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/utf7"
)
const (
setCommandName = "SETQUOTA"
getCommandName = "GETQUOTA"
getRootCommandName = "GETQUOTAROOT"
)
// The SETQUOTA command. See RFC 2087 section 4.1.
type SetCommand struct {
Root string
Resources map[string]uint32
}
func (cmd *SetCommand) Command() *imap.Command {
args := []interface{}{cmd.Root}
for k, v := range cmd.Resources {
args = append(args, k, v)
}
return &imap.Command{
Name: setCommandName,
Arguments: args,
}
}
func (cmd *SetCommand) Parse(fields []interface{}) error {
if len(fields) < 2 {
return errors.New("No enough arguments")
}
var ok bool
if cmd.Root, 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
for i, v := range resources {
if i%2 == 0 {
name, ok = v.(string)
if !ok {
return errors.New("Resource name must be a string")
}
} else {
var err error
cmd.Resources[name], err = imap.ParseNumber(v)
if err != nil {
return err
}
}
}
return nil
}
// The GETQUOTA command. See RFC 2087 section 4.2.
type GetCommand struct {
Root string
}
func (cmd *GetCommand) Command() *imap.Command {
return &imap.Command{
Name: getCommandName,
Arguments: []interface{}{cmd.Root},
}
}
func (cmd *GetCommand) Parse(fields []interface{}) error {
if len(fields) < 1 {
return errors.New("No enough arguments")
}
var ok bool
if cmd.Root, ok = fields[0].(string); !ok {
return errors.New("Quota root must be a string")
}
return nil
}
// The GETQUOTAROOT command. See RFC 2087 section 4.3.
type GetRootCommand struct {
Mailbox string
}
func (cmd *GetRootCommand) Command() *imap.Command {
mailbox, _ := utf7.Encoding.NewEncoder().String(cmd.Mailbox)
return &imap.Command{
Name: getRootCommandName,
Arguments: []interface{}{mailbox},
}
}
func (cmd *GetRootCommand) Parse(fields []interface{}) error {
if len(fields) < 1 {
return errors.New("No enough arguments")
}
var ok bool
mailbox, ok := fields[0].(string)
if !ok {
return errors.New("Quota root must be a string")
}
var err error
if cmd.Mailbox, err = utf7.Encoding.NewDecoder().String(mailbox); err != nil {
return err
}
return nil
}