-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup.go
223 lines (211 loc) · 4.56 KB
/
setup.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
package mailout
import (
"errors"
"strconv"
"time"
"github.com/SchumacherFM/mailout/maillog"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
)
func init() {
caddy.RegisterPlugin("mailout", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
// setup used internally by Caddy to set up this middleware
func setup(c *caddy.Controller) error {
mc, err := parse(c)
if err != nil {
return err
}
if c.ServerBlockKeyIndex == 0 {
// only run when the first hostname has been loaded.
if mc.maillog, err = mc.maillog.Init(c.ServerBlockKeys...); err != nil {
return err
}
if err = mc.loadFromEnv(); err != nil {
return err
}
if err = mc.loadPGPKeys(); err != nil {
return err
}
if err = mc.loadTemplate(); err != nil {
return err
}
if err = mc.pingSMTP(); err != nil {
return err
}
c.ServerBlockStorage = newHandler(mc, startMailDaemon(mc))
}
c.OnShutdown(func() error {
if moh, ok := c.ServerBlockStorage.(*handler); ok {
if moh.reqPipe != nil {
close(moh.reqPipe)
moh.reqPipe = nil
}
}
return nil
})
if moh, ok := c.ServerBlockStorage.(*handler); ok { // moh = mailOutHandler ;-)
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
moh.Next = next
return moh
})
return nil
}
return errors.New("[mailout] Could not create the middleware handler")
}
func parse(c *caddy.Controller) (mc *config, _ error) {
// This parses the following config blocks
mc = newConfig()
for c.Next() {
args := c.RemainingArgs()
switch len(args) {
case 1:
mc.endpoint = args[0]
}
for c.NextBlock() {
var err error
switch c.Val() {
case "publickeyAttachmentFileName":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.pgpAttachmentName = c.Val()
case "maillog":
if !c.NextArg() {
return nil, c.ArgErr()
}
if mc.maillog.IsNil() {
mc.maillog = maillog.New(c.Val(), "")
} else {
mc.maillog.MailDir = c.Val()
}
case "errorlog":
if !c.NextArg() {
return nil, c.ArgErr()
}
if mc.maillog.IsNil() {
mc.maillog = maillog.New("", c.Val())
} else {
mc.maillog.ErrDir = c.Val()
}
case "from_email":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.fromEmail = c.Val()
case "from_name":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.fromName = c.Val()
case "to":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.to, err = splitEmailAddresses(c.Val())
if err != nil {
return nil, err
}
case "cc":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.cc, err = splitEmailAddresses(c.Val())
if err != nil {
return nil, err
}
case "bcc":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.bcc, err = splitEmailAddresses(c.Val())
if err != nil {
return nil, err
}
case "subject":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.subject = c.Val()
case "body":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.body = c.Val()
case "username":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.username = c.Val()
case "password":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.password = c.Val()
case "host":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.host = c.Val()
case "port":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.portRaw = c.Val()
case "skip_tls_verify":
mc.skipTLSVerify = true
case "redirect_field":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.redirectField = c.Val()
case "captcha":
mc.Captcha = true
case "recaptcha":
mc.ReCaptcha = true
case "recaptcha_secret":
if !c.NextArg() {
return nil, c.ArgErr()
}
mc.ReCaptchaSecret = c.Val()
case "ratelimit_interval":
if !c.NextArg() {
return nil, c.ArgErr()
}
var rli time.Duration
rli, err = time.ParseDuration(c.Val())
if err != nil {
return nil, err
}
if rli.Nanoseconds() != 0 {
mc.rateLimitInterval = rli
}
case "ratelimit_capacity":
if !c.NextArg() {
return nil, c.ArgErr()
}
var rlc int64
rlc, err = strconv.ParseInt(c.Val(), 10, 64)
if err != nil {
return nil, err
}
if rlc > 0 {
mc.rateLimitCapacity = rlc
}
default:
anyKey := c.Val()
if isValidEmail(anyKey) {
if !c.NextArg() {
return nil, c.ArgErr()
}
pgpPublicKey := c.Val()
mc.pgpEmailKeys = append(mc.pgpEmailKeys, anyKey, pgpPublicKey)
}
}
}
}
return
}