-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.go
157 lines (127 loc) · 2.94 KB
/
server.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
package quota
import (
"errors"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/server"
)
var (
ErrUnsupportedBackend = errors.New("quota: backend not supported")
)
type User interface {
// GetQuota returns the quota with the specified name.
GetQuota(name string) (*Status, error)
// SetQuota registers or updates a quota for this user with a set of resources
// and their limit. The resource limits for the named quota root are changed
// to be the specified limits. Any previous resource limits for the named
// quota root are discarded.
SetQuota(name string, resources map[string]uint32) error
}
type Mailbox interface {
// ListQuotas returns the currently active quotas for this mailbox.
ListQuotas() ([]string, error)
}
type SetHandler struct {
SetCommand
}
func (h *SetHandler) Handle(conn server.Conn) error {
if conn.Context().User == nil {
return server.ErrNotAuthenticated
}
u, ok := conn.Context().User.(User)
if !ok {
return ErrUnsupportedBackend
}
if err := u.SetQuota(h.Root, h.Resources); err != nil {
return err
}
inner := &GetHandler{}
inner.Root = h.Root
return inner.Handle(conn)
}
type GetHandler struct {
GetCommand
}
func (h *GetHandler) Handle(conn server.Conn) error {
if conn.Context().User == nil {
return server.ErrNotAuthenticated
}
u, ok := conn.Context().User.(User)
if !ok {
return ErrUnsupportedBackend
}
status, err := u.GetQuota(h.Root)
if err != nil {
return err
}
res := &Response{Quotas: []*Status{status}}
return conn.WriteResp(res)
}
type GetRootHandler struct {
GetRootCommand
}
func (h *GetRootHandler) Handle(conn server.Conn) error {
if conn.Context().User == nil {
return server.ErrNotAuthenticated
}
u, ok := conn.Context().User.(User)
if !ok {
return ErrUnsupportedBackend
}
mbox, err := conn.Context().User.GetMailbox(h.Mailbox)
if err != nil {
return err
}
qmbox, ok := mbox.(Mailbox)
if !ok {
return ErrUnsupportedBackend
}
roots, err := qmbox.ListQuotas()
if err != nil {
return err
}
rootRes := &RootResponse{
Mailbox: &MailboxRoots{
Name: h.Mailbox,
Roots: roots,
},
}
if err := conn.WriteResp(rootRes); err != nil {
return err
}
res := &Response{}
for _, name := range roots {
status, err := u.GetQuota(name)
if err != nil {
return err
}
res.Quotas = append(res.Quotas, status)
}
return conn.WriteResp(res)
}
type extension struct{}
func NewExtension() server.Extension {
return &extension{}
}
func (ext *extension) Capabilities(c server.Conn) []string {
if c.Context().State&imap.AuthenticatedState != 0 {
return []string{Capability}
}
return nil
}
func (ext *extension) Command(name string) server.HandlerFactory {
switch name {
case setCommandName:
return func() server.Handler {
return &SetHandler{}
}
case getCommandName:
return func() server.Handler {
return &GetHandler{}
}
case getRootCommandName:
return func() server.Handler {
return &GetRootHandler{}
}
}
return nil
}