-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.go
330 lines (253 loc) · 9.69 KB
/
mail.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package mailinabox
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)
// MailUsers Represents a set of users for a domain.
type MailUsers struct {
Domain string `json:"domain,omitempty"`
Users []User `json:"users,omitempty"`
}
// User Represents a user.
type User struct {
Email string `json:"email,omitempty"`
Privileges []string `json:"privileges,omitempty"`
Status string `json:"status,omitempty"`
Mailbox string `json:"mailbox,omitempty"`
}
// MailAliases Represents a set of aliases for a domain.
type MailAliases struct {
Domain string `json:"domain,omitempty"`
Aliases []Alias `json:"aliases,omitempty"`
}
// Alias Represents an alias.
type Alias struct {
Address string `json:"address,omitempty"`
AddressDisplay string `json:"address_display,omitempty"`
ForwardsTo []string `json:"forwards_to,omitempty"`
PermittedSenders []string `json:"permitted_senders,omitempty"`
Required bool `json:"required,omitempty"`
}
// MailService Mail API.
// https://mailinabox.email/api-docs.html#tag/Mail
type MailService service
// GetUsers Returns all mail users.
// https://mailinabox.email/api-docs.html#operation/getMailUsers
func (s *MailService) GetUsers(ctx context.Context) ([]MailUsers, error) {
endpoint := s.client.baseURL.JoinPath("admin", "mail", "users")
query := endpoint.Query()
query.Set("format", "json")
endpoint.RawQuery = query.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
var results []MailUsers
err = s.client.doJSON(req, &results)
if err != nil {
return nil, err
}
return results, nil
}
// AddUser Adds a new mail user.
// https://mailinabox.email/api-docs.html#operation/addMailUser
func (s *MailService) AddUser(ctx context.Context, email, password, privilege string) (string, error) {
if email == "" || password == "" || privilege == "" {
return "", errors.New("email, password, and privilege are required")
}
endpoint := s.client.baseURL.JoinPath("admin", "mail", "users", "add")
data := url.Values{}
data.Set("email", email)
data.Set("password", password)
data.Set("privileges", privilege)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(data.Encode()))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// RemoveUser Removes an existing mail user.
// https://mailinabox.email/api-docs.html#operation/removeMailUser
func (s *MailService) RemoveUser(ctx context.Context, email string) (string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "mail", "users", "remove")
data := url.Values{}
data.Set("email", email)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(data.Encode()))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// AddUserPrivilege Adds a privilege to an existing mail user.
// https://mailinabox.email/api-docs.html#operation/addMailUserPrivilege
func (s *MailService) AddUserPrivilege(ctx context.Context, email, privilege string) (string, error) {
if email == "" || privilege == "" {
return "", errors.New("email and privilege are required")
}
endpoint := s.client.baseURL.JoinPath("admin", "mail", "users", "privileges", "add")
data := url.Values{}
data.Set("email", email)
data.Set("privileges", privilege)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(data.Encode()))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// RemoveUserPrivilege Adds a privilege to an existing mail user.
// https://mailinabox.email/api-docs.html#operation/removeMailUserPrivilege
func (s *MailService) RemoveUserPrivilege(ctx context.Context, email, privilege string) (string, error) {
if email == "" {
return "", errors.New("email is required")
}
endpoint := s.client.baseURL.JoinPath("admin", "mail", "users", "privileges", "remove")
data := url.Values{}
data.Set("email", email)
data.Set("privileges", privilege)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(data.Encode()))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// SetUserPassword Sets a password for an existing mail user.
// https://mailinabox.email/api-docs.html#operation/setMailUserPassword
func (s *MailService) SetUserPassword(ctx context.Context, email, password string) (string, error) {
if email == "" || password == "" {
return "", errors.New("email and password are required")
}
endpoint := s.client.baseURL.JoinPath("admin", "mail", "users", "password")
data := url.Values{}
data.Set("email", email)
data.Set("password", password)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(data.Encode()))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// GetUserPrivileges Returns all privileges for an existing mail user.
// https://mailinabox.email/api-docs.html#operation/getMailUserPrivileges
func (s *MailService) GetUserPrivileges(ctx context.Context, email string) (string, error) {
if email == "" {
return "", errors.New("email is required")
}
endpoint := s.client.baseURL.JoinPath("admin", "mail", "users", "privileges")
query := endpoint.Query()
query.Set("email", email)
endpoint.RawQuery = query.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// GetDomains Returns all mail domains.
// https://mailinabox.email/api-docs.html#operation/getMailDomains
func (s *MailService) GetDomains(ctx context.Context) ([]string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "mail", "domains")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
resp, err := s.client.doPlain(req)
if err != nil {
return nil, err
}
domains := strings.Split(strings.TrimSpace(string(resp)), "\n")
return domains, nil
}
// GetAliases Returns all mail aliases.
// https://mailinabox.email/api-docs.html#operation/getMailAliases
func (s *MailService) GetAliases(ctx context.Context) ([]MailAliases, error) {
endpoint := s.client.baseURL.JoinPath("admin", "mail", "aliases")
query := endpoint.Query()
query.Set("format", "json")
endpoint.RawQuery = query.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
var results []MailAliases
err = s.client.doJSON(req, &results)
if err != nil {
return nil, err
}
return results, nil
}
// UpsertAlias Adds or updates a mail alias. If updating, you need to set update_if_exists: 1.
// https://mailinabox.email/api-docs.html#operation/upsertMailAlias
func (s *MailService) UpsertAlias(ctx context.Context, updateIfExists bool, address string, forwardsTo, permittedSenders []string) (string, error) {
if address == "" {
return "", errors.New("address is required")
}
endpoint := s.client.baseURL.JoinPath("admin", "mail", "aliases", "add")
data := url.Values{}
data.Set("update_if_exists", boolToIntStr(updateIfExists))
data.Set("address", address)
data.Set("forwards_to", strings.Join(forwardsTo, ","))
data.Set("permitted_senders", strings.Join(permittedSenders, ","))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(data.Encode()))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// RemoveAliases Removes a mail alias.
// https://mailinabox.email/api-docs.html#operation/removeMailAlias
func (s *MailService) RemoveAliases(ctx context.Context, address string) (string, error) {
if address == "" {
return "", errors.New("address is required")
}
endpoint := s.client.baseURL.JoinPath("admin", "mail", "aliases", "remove")
data := url.Values{}
data.Set("address", address)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(data.Encode()))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}