This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
124 lines (100 loc) · 2.23 KB
/
user.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 multi
import (
"errors"
"strings"
"github.com/emersion/go-imap/backend"
)
type user struct {
be *Backend
childs map[string][]backend.User
username, password string
}
func (u *user) ensureLoggedIn(reference string, i int) error {
if u.childs[reference][i] != nil {
return nil
}
be := u.be.childs[reference][i]
if child, err := be.Login(u.username, u.password); err != nil {
return err
} else {
u.childs[reference][i] = child
return nil
}
}
func (u *user) Username() string {
return u.username
}
func (u *user) ListMailboxes(subscribed bool) ([]backend.Mailbox, error) {
var mailboxes []backend.Mailbox
for ref, childs := range u.childs {
for i, child := range childs {
if err := u.ensureLoggedIn(ref, i); err != nil {
return nil, err
}
childMailboxes, err := child.ListMailboxes(subscribed)
if err != nil {
return nil, err
}
for _, m := range childMailboxes {
if ref != "" {
m = &mailbox{m, ref}
}
mailboxes = append(mailboxes, m)
}
}
}
return mailboxes, nil
}
func (u *user) GetMailbox(name string) (backend.Mailbox, error) {
for ref, childs := range u.childs {
if !strings.HasPrefix(name, ref) {
continue
}
name := strings.TrimPrefix(name, ref)
for i, child := range childs {
if err := u.ensureLoggedIn(ref, i); err != nil {
return nil, err
}
if mailbox, _ := child.GetMailbox(name); mailbox != nil {
return mailbox, nil
}
}
}
return nil, errors.New("No such mailbox")
}
func (u *user) CreateMailbox(name string) error {
child := u.childs[""][0]
for ref, childs := range u.childs {
if len(childs) == 0 {
continue
}
if ref != "" && strings.HasPrefix(name, ref) {
if err := u.ensureLoggedIn(ref, 0); err != nil {
return nil
}
child = childs[0]
name = strings.TrimPrefix(name, ref)
break
}
}
return child.CreateMailbox(name)
}
func (u *user) DeleteMailbox(name string) error {
return nil // TODO
}
func (u *user) RenameMailbox(existingName, newName string) error {
return nil // TODO
}
func (u *user) Logout() error {
for _, childs := range u.childs {
for _, child := range childs {
if child == nil {
continue
}
if err := child.Logout(); err != nil {
return err
}
}
}
return nil
}