-
Notifications
You must be signed in to change notification settings - Fork 3
/
root.go
268 lines (245 loc) · 6.96 KB
/
root.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
package config
import (
"encoding/json"
"fmt"
"html/template"
"net/mail"
"sort"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/vanti-dev/sc-bos/pkg/auto"
"github.com/vanti-dev/sc-bos/pkg/util/jsontypes"
)
func ReadBytes(data []byte) (cfg Root, err error) {
err = json.Unmarshal(data, &cfg)
if cfg.Debounce != nil {
for i, source := range cfg.Sources {
if source.Debounce == nil {
source.Debounce = cfg.Debounce
cfg.Sources[i] = source
}
}
}
if cfg.Destination.Host == "" {
err = fmt.Errorf("destination.host not specified")
}
if len(cfg.Destination.To) == 0 {
err = fmt.Errorf("destination.recipients is empty")
}
// validate email addresses
parsed, err := cfg.Destination.Parse()
if err != nil {
return
}
cfg.Destination.Parsed = parsed
for i, recipient := range cfg.Destination.To {
_, err = mail.ParseAddress(recipient)
if err != nil {
err = fmt.Errorf("destination.recipients[%d] is invalid: %w", i, err)
return
}
}
return
}
type Root struct {
auto.Config
// Configuration information for how to send the email.
Destination Destination `json:"destination,omitempty"`
// If true, all devices on the current node that implement Status will be monitored.
// Additional sources may be defined via Sources.
DiscoverSources bool `json:"discoverSources,omitempty"`
// Name of the devices that implement Status trait and that are monitored.
Sources []Source `json:"sources,omitempty"`
// Delay querying the status of devices by this much, to allow them to boot up.
DelayStart *jsontypes.Duration `json:"delayStart,omitempty"`
// Default debounce time for all sources.
Debounce *jsontypes.Duration `json:"debounce,omitempty"`
// Device name prefixes to ignore.
// Only used if DiscoverSources is true.
IgnorePrefixes []string `json:"ignorePrefixes,omitempty"`
}
type Destination struct {
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
Username string `json:"username,omitempty"` // defaults to From.Address
jsontypes.Password
// todo: TLS config
From string `json:"from,omitempty"` // RFC 5322 address, the address part used for auth against Host
To []string `json:"to,omitempty"` // RFC 5322 address
MinInterval *jsontypes.Duration `json:"minInterval,omitempty"` // minimum time between emails, to avoid spamming
SubjectTemplate jsontypes.String `json:"subjectTemplate,omitempty"`
BodyTemplate jsontypes.String `json:"bodyTemplate,omitempty"`
Parsed *ParsedDestination `json:"-"`
}
type ParsedDestination struct {
Addr string
Username string
Password string
From *mail.Address
To []*mail.Address
SubjectTemplate *template.Template
BodyTemplate *template.Template
}
func (d Destination) Parse() (*ParsedDestination, error) {
p := &ParsedDestination{}
var err error
p.Addr = d.Addr()
p.Username = d.Username
p.Password, err = d.Password.Read()
if err != nil {
return nil, fmt.Errorf("destination.password: %w", err)
}
p.From, err = mail.ParseAddress(d.From)
if err != nil {
return nil, fmt.Errorf("destination.from: %w", err)
}
if p.Username == "" {
p.Username = p.From.Address
}
for i, to := range d.To {
a, err := mail.ParseAddress(to)
if err != nil {
return nil, fmt.Errorf("destination.to[%d]: %w", i, err)
}
p.To = append(p.To, a)
}
sort.Slice(p.To, func(i, j int) bool {
return p.To[i].Address < p.To[j].Address
})
p.SubjectTemplate, err = d.ReadSubjectTemplate()
if err != nil {
return nil, fmt.Errorf("destination.subjectTemplate: %w", err)
}
p.BodyTemplate, err = d.ReadBodyTemplate()
if err != nil {
return nil, fmt.Errorf("destination.bodyTemplate: %w", err)
}
return p, nil
}
// Addr returns the combination of Host and Port, taking defaults into account.
// Suitable for smtp.Dial.
func (d Destination) Addr() string {
p := d.Port
if p == 0 {
p = 587
}
return fmt.Sprintf("%s:%d", d.Host, p)
}
func (d Destination) ReadSubjectTemplate() (*template.Template, error) {
s, err := d.SubjectTemplate.Read()
if err != nil {
return nil, err
}
if s == "" {
s = DefaultEmailSubject
}
return template.New("subject").Parse(s)
}
func (d Destination) ReadBodyTemplate() (*template.Template, error) {
s, err := d.BodyTemplate.Read()
if err != nil {
return nil, err
}
if s == "" {
s = DefaultEmailBody
}
return template.New("body").
Funcs(template.FuncMap{
"printTime": func(t any) string {
format := time.Stamp
switch t := t.(type) {
case time.Time:
return t.Format(format)
case *time.Time:
return t.Format(format)
case *timestamppb.Timestamp:
return t.AsTime().Format(format)
case nil:
return ""
default:
return fmt.Sprintf("%v", t)
}
},
}).
Parse(s)
}
type Source struct {
Name string `json:"name,omitempty"`
Title string `json:"title,omitempty"`
Floor string `json:"floor,omitempty"`
Zone string `json:"zone,omitempty"`
Subsystem string `json:"subsystem,omitempty"`
// Don't send emails until after this time expires, reduces noise.
Debounce *jsontypes.Duration `json:"debounce,omitempty"`
}
const DefaultDebounce = 15 * time.Second
func (s Source) DebounceOrDefault() time.Duration {
return s.Debounce.Or(DefaultDebounce)
}
const DefaultEmailSubject = "Smart Core Notification"
const DefaultEmailBody = `<html lang="en">
<head>
<title>Smart Core Notification</title>
<style>
table {border-collapse: collapse;}
td, th {border: 1px solid black; padding: 0.2em 0.5em;}
</style>
</head>
<body>
<h1>A Smart Core status notification has been triggered</h1>
<p>
{{with len .WorseLogs}}{{.}} notifications have worsened.{{end}}
{{with len .BetterLogs}}{{.}} notifications have improved.{{end}}
</p>
<table>
<thead>
<tr>
<td>Status</td>
<td>Device</td>
<td>Description</td>
<td>Change Time</td>
<td>Last Status</td>
</tr>
</thead>
<tbody>
{{if .WorseLogs}}
<tr>
<td colspan="5">The following notifications have worsened:</td>
</tr>
{{range .WorseLogs}}
<tr>
<td>{{.Read.Level}}</td>
{{if .Source.Title}}
<td title="{{.Source.Name}}">{{.Source.Floor}} {{.Source.Zone}} {{.Source.Subsystem}} {{.Source.Title}}</td>
{{else}}
<td>{{.Source.Name}}</td>
{{end}}
<td>{{.Read.Description}}</td>
<td>{{printTime .Read.RecordTime}}</td>
<td>{{.Sent.Level}}</td>
</tr>
{{end}}
{{end}}
{{if .BetterLogs}}
<tr style="border-top: 1px solid currentColor">
<td colspan="5">The following notifications have improved:</td>
</tr>
{{range .BetterLogs}}
<tr>
<td>{{.Read.Level}}</td>
{{if .Source.Title}}
<td title="{{.Source.Name}}">{{.Source.Floor}} {{.Source.Zone}} {{.Source.Subsystem}} {{.Source.Title}}</td>
{{else}}
<td>{{.Source.Name}}</td>
{{end}}
<td>{{.Read.Description}}</td>
<td>{{printTime .Read.RecordTime}}</td>
<td>{{.Sent.Level}}</td>
</tr>
{{end}}
{{end}}
</tbody>
</table>
</body>
</html>
`