-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcatchalluser.go
110 lines (89 loc) · 2.18 KB
/
catchalluser.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
package mailfull
import (
"bufio"
"fmt"
"os"
"path/filepath"
)
// CatchAllUser represents a CatchAllUser.
type CatchAllUser struct {
name string
}
// NewCatchAllUser creates a new CatchAllUser instance.
func NewCatchAllUser(name string) (*CatchAllUser, error) {
if !validCatchAllUserName(name) {
return nil, ErrInvalidCatchAllUserName
}
cu := &CatchAllUser{
name: name,
}
return cu, nil
}
// Name returns name.
func (cu *CatchAllUser) Name() string {
return cu.name
}
// CatchAllUser returns a CatchAllUser that the input name has.
func (r *Repository) CatchAllUser(domainName string) (*CatchAllUser, error) {
domain, err := r.Domain(domainName)
if err != nil {
return nil, err
}
if domain == nil {
return nil, ErrDomainNotExist
}
file, err := os.Open(filepath.Join(r.DirMailDataPath, domainName, FileNameCatchAllUser))
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Scan()
name := scanner.Text()
if err := scanner.Err(); err != nil {
return nil, err
}
if name == "" {
return nil, nil
}
catchAllUser, err := NewCatchAllUser(name)
if err != nil {
return nil, err
}
return catchAllUser, nil
}
// CatchAllUserSet sets a CatchAllUser to the input Domain.
func (r *Repository) CatchAllUserSet(domainName string, catchAllUser *CatchAllUser) error {
existUser, err := r.User(domainName, catchAllUser.Name())
if err != nil {
return err
}
if existUser == nil {
return ErrUserNotExist
}
file, err := os.OpenFile(filepath.Join(r.DirMailDataPath, domainName, FileNameCatchAllUser), os.O_RDWR|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer file.Close()
if _, err := fmt.Fprintf(file, "%s\n", catchAllUser.Name()); err != nil {
return err
}
return nil
}
// CatchAllUserUnset removes a CatchAllUser from the input Domain.
func (r *Repository) CatchAllUserUnset(domainName string) error {
existDomain, err := r.Domain(domainName)
if err != nil {
return err
}
if existDomain == nil {
return ErrDomainNotExist
}
file, err := os.OpenFile(filepath.Join(r.DirMailDataPath, domainName, FileNameCatchAllUser), os.O_RDWR|os.O_TRUNC, 0600)
if err != nil {
return err
}
file.Close()
return nil
}