-
Notifications
You must be signed in to change notification settings - Fork 0
/
libemail.go
575 lines (515 loc) · 18.1 KB
/
libemail.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
Copyright © 2022 ITRS Group
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package main
import "C"
import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
htmltemplate "html/template"
"io"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"text/template"
"time"
"github.com/go-mail/mail/v2"
)
//go:embed text.gotmpl
var defTextTemplate string
//go:embed html.gotmpl
var defHTMLTemplate string
//go:embed css.gotmpl
var defCSSTemplate string
//go:embed textmsteams.gotmpl
var defMsTeamsTextTemplate string
//go:embed htmlmsteams.gotmpl
var defMsTeamsHTMLTemplate string
//go:embed css.gotmpl
var defMsTeamsCSSTemplate string
//go:embed logo.png
var logo []byte
const msTeamsMessageCard = "MessageCard"
const geneosThemecolor = "#46e1d7"
const DefaultWebhookURLValidationPattern = `^https:\/\/(?:.*\.webhook|outlook)\.office(?:365)?\.com`
const DefaultMsTeamsTimeout = 2000
type msTeamsBasicTextNotifPostData struct {
Type string `json:"@type"`
Title string `json:"title,omitempty"`
Text string `json:"text,omitempty"`
ThemeColor string `json:"themeColor,omitempty"`
}
// SendMail tries to duplicate the exact behaviour of libemail.so's SendMail function
// but with the addition of more modern SMTP and TLS / authentication
//
// text only, using formats, either defaults or passed in
// for security all "meta" parameters to do with the SMTP configuration and other headers are removed
// unless _DEBUG is set to "true"
//export SendMail
func SendMail(n C.int, args **C.char) C.int {
conf := parseArgs(n, args)
d, err := dialServer(conf)
if err != nil {
log.Println(err)
return 1
}
m, err := setupMail(conf)
if err != nil {
log.Println(err)
return 1
}
// From doc:
// "If an _ALERT parameter is present libemail assumes it is being called as part of a gateway alert
// and will use the appropriate format depending on the value of _ALERT_TYPE (Alert, Clear, Suspend,
// or Resume). If no _ALERT parameter is specified libemail assumes it is being called as part of an
// action and uses _FORMAT."
//
// A user defined format will always override the default format. If the _FORMAT parameter is
// specified by the user then this will override any default formats whether or not _ALERT is present.
//
// Subjects behave in the same way as formats."
//
// Note: "ThrottleSummary" is also mentioned later, but is the same as above
var format, subject string
subject = getWithDefault("_SUBJECT", conf, defaultSubject[_SUBJECT])
if _, ok := conf["_FORMAT"]; ok {
format = conf["_FORMAT"]
} else if _, ok = conf["_ALERT"]; ok {
switch conf["_ALERT_TYPE"] {
case "Alert":
format = getWithDefault("_ALERT_FORMAT", conf, defaultFormat[_ALERT_FORMAT])
subject = getWithDefault("_ALERT_SUBJECT", conf, defaultSubject[_ALERT_SUBJECT])
case "Clear":
format = getWithDefault("_CLEAR_FORMAT", conf, defaultFormat[_CLEAR_FORMAT])
subject = getWithDefault("_CLEAR_SUBJECT", conf, defaultSubject[_CLEAR_SUBJECT])
case "Suspend":
format = getWithDefault("_SUSPEND_FORMAT", conf, defaultFormat[_SUSPEND_FORMAT])
subject = getWithDefault("_SUSPEND_SUBJECT", conf, defaultSubject[_SUSPEND_SUBJECT])
case "Resume":
format = getWithDefault("_RESUME_FORMAT", conf, defaultFormat[_RESUME_FORMAT])
subject = getWithDefault("_RESUME_SUBJECT", conf, defaultSubject[_RESUME_SUBJECT])
case "ThrottleSummary":
format = getWithDefault("_SUMMARY_FORMAT", conf, defaultFormat[_SUMMARY_FORMAT])
subject = getWithDefault("_SUMMARY_SUBJECT", conf, defaultSubject[_SUMMARY_SUBJECT])
default:
format = defaultFormat[_FORMAT]
}
} else {
format = defaultFormat[_FORMAT]
}
if !debug(conf) {
keys := []string{"_FORMAT", "_ALERT_FORMAT", "_CLEAR_FORMAT", "_SUSPEND_FORMAT", "_RESUME_FORMAT", "_SUMMARY_FORMAT",
"_SUBJECT", "_ALERT_SUBJECT", "_CLEAR_SUBJECT", "_SUSPEND_SUBJECT", "_RESUME_TEMPLATE", "_SUMMARY_TEMPLATE",
}
for _, key := range keys {
delete(conf, key)
}
}
body := replArgs(format, conf)
m.SetHeader("Subject", subject)
m.SetBody("text/plain", body)
if err = d.DialAndSend(m); err != nil {
log.Println(err)
return 1
}
return 0
}
var replArgsRE = regexp.MustCompile(`%\([^\)]*\)`)
// substitute placeholder of the form %(XXX) for the value of XXX or empty and
// return the result as a new string
func replArgs(format string, conf EMailConfig) string {
result := replArgsRE.ReplaceAllStringFunc(format, func(key string) string {
// strip containing "%(...)" - as we are here, the regexp must have matched OK
// so no further check required. No match returns empty string.
return conf[key[2:len(key)-1]]
})
return result
}
//export GoSendMail
func GoSendMail(n C.int, args **C.char) C.int {
conf := parseArgs(n, args)
d, err := dialServer(conf)
if err != nil {
log.Println(err)
return 1
}
m, err := setupMail(conf)
if err != nil {
log.Println(err)
return 1
}
// The subject follows the same rules as the original SendMail function
subject := getWithDefault("_SUBJECT", conf, defaultSubject[_SUBJECT])
// there is a default template that contains embedded tests for which type of
// alert, if any. This can be overridden with a template file or a template string
//
// first grab a suitable Subject if this is an Alert, overridden below if
// a template file or string is specified
if _, ok := conf["_ALERT"]; ok {
switch conf["_ALERT_TYPE"] {
case "Alert":
subject = getWithDefault("_ALERT_SUBJECT", conf, defaultSubject[_ALERT_SUBJECT])
case "Clear":
subject = getWithDefault("_CLEAR_SUBJECT", conf, defaultSubject[_CLEAR_SUBJECT])
case "Suspend":
subject = getWithDefault("_SUSPEND_SUBJECT", conf, defaultSubject[_SUSPEND_SUBJECT])
case "Resume":
subject = getWithDefault("_RESUME_SUBJECT", conf, defaultSubject[_RESUME_SUBJECT])
case "ThrottleSummary":
subject = getWithDefault("_SUMMARY_SUBJECT", conf, defaultSubject[_SUMMARY_SUBJECT])
}
}
// run the subject through text template to allow variable subjects
subjtmpl := template.New("subject")
subjtmpl, err = subjtmpl.Parse(subject)
if err == nil {
var subjbuf bytes.Buffer
err = subjtmpl.Execute(&subjbuf, conf)
if err == nil {
subject = subjbuf.String()
}
}
m.SetHeader("Subject", subject)
tmpl := template.New("text")
if _, ok := conf["_TEMPLATE_TEXT_FILE"]; ok {
tmpl, err = tmpl.ParseFiles(conf["_TEMPLATE_TEXT_FILE"])
} else if _, ok := conf["_TEMPLATE_TEXT"]; ok {
tmpl, err = tmpl.Parse(conf["_TEMPLATE_TEXT"])
} else {
tmpl, err = tmpl.Parse(defTextTemplate)
}
if err != nil {
log.Println(err)
return 1
}
// save a text only flag now so we can delete the key
textOnly := false
_, textOnly = conf["_TEMPLATE_TEXT_ONLY"]
var html *htmltemplate.Template
// conditionally set-up non-text templates
if !textOnly {
html = htmltemplate.New("base")
var contents string
if _, ok := conf["_TEMPLATE_HTML_FILE"]; ok {
contents, err = readFileString(conf["_TEMPLATE_HTML_FILE"])
if err != nil {
log.Println(err)
return 1
}
} else if _, ok := conf["_TEMPLATE_HTML"]; ok {
contents = conf["_TEMPLATE_HTML"]
} else {
contents = defHTMLTemplate
}
html, err = html.New("html").Parse(contents)
if err != nil {
log.Println(err)
return 1
}
if _, ok := conf["_TEMPLATE_CSS_FILE"]; ok {
contents, err = readFileString(conf["_TEMPLATE_CSS_FILE"])
if err != nil {
log.Println(err)
return 1
}
} else if _, ok := conf["_TEMPLATE_CSS"]; ok {
contents = conf["_TEMPLATE_CSS"]
} else {
contents = defCSSTemplate
}
html, err = html.New("css").Parse(contents)
if err != nil {
log.Println(err)
return 1
}
// if _TEMPLATE_LOGO_FILE is defined, load that, else use embedded
if logopath, ok := conf["_TEMPLATE_LOGO_FILE"]; ok {
logofile, err := os.Open(logopath)
if err != nil {
return 1
}
m.EmbedReader("logo.png", logofile)
} else {
// use var path and a default of the embedded logo
m.Embed("logo.png", mail.SetCopyFunc(func(w io.Writer) error {
_, err := w.Write(logo)
return err
}))
}
}
if !debug(conf) {
keys := []string{
"_TEMPLATE_TEXT", "_TEMPLATE_TEXT_FILE",
"_TEMPLATE_HTML", "_TEMPLATE_HTML_FILE",
"_TEMPLATE_CSS", "_TEMPLATE_CSS_FILE",
"_TEMPLATE_TEXT_ONLY", "_TEMPLATE_LOGO_FILE",
"_SUBJECT", "_ALERT_SUBJECT", "_CLEAR_SUBJECT",
"_SUSPEND_SUBJECT", "_RESUME_SUBJECT", "_SUMMARY_SUBJECT",
}
for _, key := range keys {
delete(conf, key)
}
}
// now that we've removed meta params, execute the templates and add the output to
// the email
var output bytes.Buffer
err = tmpl.Execute(&output, conf)
if err != nil {
log.Println(err)
return 1
}
m.SetBody("text/plain", output.String())
if !textOnly {
var htmlBody bytes.Buffer
err = html.ExecuteTemplate(&htmlBody, "html", conf)
if err != nil {
log.Println(err)
return 1
}
m.AddAlternative("text/html", htmlBody.String())
}
err = d.DialAndSend(m)
if err != nil {
log.Println(err)
return 1
}
return 0
}
//export GoSendToMsTeamsChannel
func GoSendToMsTeamsChannel(n C.int, args **C.char) C.int {
var msTeamsWebhooksValidity = make(map[string]bool)
var clientTimeout time.Duration
// Parse arguments
// ---------------
conf := parseArgs(n, args)
// Check validity of msTeams incoming webhooks
// -------------------------------------------
// Error if no webhooks defined
if _, ok := conf["_TO"]; !ok || len(conf["_TO"]) == 0 {
log.Println("ERR: No MsTeams webhooks defined in _TO. Abort GoSendToMsTeamsChannel().")
return 1
}
// Split webhooks, provided in _TO as a pipe ("|") separate list
msTeamsWebhooks := strings.Split(conf["_TO"], "|")
validityWebhooksCount := 0
// Browse through webhooks & check validity usng a regex match.
// Invalid webhooks are ignored.
regex, err := regexp.Compile(DefaultWebhookURLValidationPattern)
if err != nil {
log.Println("ERR: Cannot compile regex to validate msTeams webhooks. Abort GoSendToMsTeamsChannel().")
return 1
}
for i, s := range msTeamsWebhooks {
if match := regex.MatchString(s); !match {
log.Printf("WARN: msTeams weekhook #%d (%s) is not valid. Ignoring it.\n", i+1, s)
msTeamsWebhooksValidity[strings.TrimSpace(s)] = false
} else {
msTeamsWebhooksValidity[strings.TrimSpace(s)] = true
validityWebhooksCount++
}
}
// Error if no valid webhooks defined
if validityWebhooksCount == 0 {
log.Println("ERR: No valid msTeams webhooks defined in _TO. Abort GoSendToMsTeamsChannel().")
return 1
}
// Attempt at having a compatibility with the base / default Geneos e-mail formatting
var subject string
var header, body string
// Define the notification subject / title
// ---------------------------------------
subject = defaultMsTeamsSubject[_SUBJECT]
if _, ok := conf["_SUBJECT"]; ok && len(conf["_SUBJECT"]) != 0 {
subject = getWithDefault("_SUBJECT", conf, defaultMsTeamsSubject[_SUBJECT])
} else if _, ok = conf["_ALERT"]; ok {
switch conf["_ALERT_TYPE"] {
case "Alert":
subject = getWithDefault("_ALERT_SUBJECT", conf, defaultMsTeamsSubject[_ALERT_SUBJECT])
case "Clear":
subject = getWithDefault("_CLEAR_SUBJECT", conf, defaultMsTeamsSubject[_CLEAR_SUBJECT])
case "Suspend":
subject = getWithDefault("_SUSPEND_SUBJECT", conf, defaultMsTeamsSubject[_SUSPEND_SUBJECT])
case "Resume":
subject = getWithDefault("_RESUME_SUBJECT", conf, defaultMsTeamsSubject[_RESUME_SUBJECT])
case "ThrottleSummary":
subject = getWithDefault("_SUMMARY_SUBJECT", conf, defaultMsTeamsSubject[_SUMMARY_SUBJECT])
default:
subject = getWithDefault("_SUBJECT", conf, defaultMsTeamsSubject[_SUBJECT])
}
}
// Run to subject through text template to allow variable subject
subjtmpl := template.New("subject")
subjtmpl, err = subjtmpl.Parse(subject)
if err == nil {
var subjbuf bytes.Buffer
err = subjtmpl.Execute(&subjbuf, conf)
if err == nil {
subject = subjbuf.String()
}
}
header = replArgs(subject, conf)
// Define the notification text / body
// -----------------------------------
var htmltmpl *htmltemplate.Template
var texttmpl *template.Template
var textOnly, useHtmlTmpl bool
var htmlOutput, textOutput bytes.Buffer
var contents string
_, textOnly = conf["_TEMPLATE_TEXT_ONLY"]
useHtmlTmpl = false
// Identify the template to use and parse it
if _, ok := conf["_TEMPLATE_HTML_FILE"]; ok && len(conf["_TEMPLATE_HTML_FILE"]) != 0 {
// Use of HTML template file defined in _TEMPLATE_HTML_FILE
useHtmlTmpl = true
contents, err = readFileString(conf["_TEMPLATE_HTML_FILE"])
if err != nil {
log.Println("ERR: Error reading HTML Template file defined in _TEMPLATE_HTML_FILE. Abort GoSendToMsTeamsChannel().", err)
return 1
}
htmltmpl, err = htmltemplate.New("html").Parse(contents)
if err != nil {
log.Panicln("ERR: Error parsing template file defined in _TEMPLATE_HTML_FILE. Abort GoSendToMsTeamsChannel().", err)
return 1
}
} else if _, ok := conf["_TEMPLATE_HTML"]; ok && len(conf["_TEMPLATE_HTML"]) != 0 {
// Use manually defined HTML template found in _TEMPLATE_HTML
useHtmlTmpl = true
htmltmpl, err = htmltemplate.New("html").Parse(conf["_TEMPLATE_HTML"])
if err != nil {
log.Println("ERR: Error executing html template in _TO. Abort GoSendToMsTeamsChannel().", err)
return 1
}
} else if _, ok := conf["_TEMPLATE_TEXT_FILE"]; ok && len(conf["_TEMPLATE_TEXT_FILE"]) != 0 {
// Use of text template file defined in _TEMPLATE_TEXT_FILE
useHtmlTmpl = false
texttmpl, err = template.ParseFiles(conf["_TEMPLATE_TEXT_FILE"])
if err != nil {
log.Println("ERR: Error parsing text template file defined in _TEMPLATE_TEXT_FILE. Abort GoSendToMsTeamsChannel().", err)
return 1
}
} else if _, ok := conf["_TEMPLATE_TEXT"]; ok && len(conf["_TEMPLATE_TEXT"]) != 0 {
// Use manually defined text template found in _TEMPLATE_TEXT
useHtmlTmpl = false
texttmpl, err = template.New("text").Parse(conf["_TEMPLATE_TEXT"])
if err != nil {
log.Println("ERR: Error parsing text template defined in _TEMPLATE_TEXT. Abort GoSendToMsTeamsChannel().", err)
return 1
}
} else if _, ok := conf["_FORMAT"]; ok && len(conf["_FORMAT"]) != 0 {
// _FORMAT defined and interpreted as a html template
if textOnly {
useHtmlTmpl = false
texttmpl, err = template.New("text").Parse(conf["_FORMAT"])
if err != nil {
log.Println("ERR: Error parsing text template in _TO. Abort GoSendToMsTeamsChannel().", err)
return 1
}
} else {
useHtmlTmpl = true
htmltmpl, err = htmltemplate.New("html").Parse(conf["_FORMAT"])
if err != nil {
log.Println("ERR: Error parsing html template in _TO. Abort GoSendToMsTeamsChannel().", err)
return 1
}
}
} else if textOnly {
// Use default text template file
useHtmlTmpl = false
texttmpl, err = template.New("text").Parse(defMsTeamsTextTemplate)
if err != nil {
log.Println("ERR: Error parsing default text template. Abort GoSendToMsTeamsChannel().", err)
return 1
}
} else {
// Use default HTML template file
useHtmlTmpl = true
contents = defMsTeamsHTMLTemplate
htmltmpl, err = htmltemplate.New("html").Parse(contents)
if err != nil {
log.Println("ERR: Error persing default HTML template. Abort GoSendToMsTeamsChannel().", err)
return 1
}
}
// Execute the template & account for older/legacy inputs formats from Geneos
if useHtmlTmpl {
// Template used is HTML
err = htmltmpl.ExecuteTemplate(&htmlOutput, "html", conf)
if err != nil {
log.Println("ERR: Error executing HTML template. Abort GoSendToMsTeamsChannel().", err)
return 1
}
body = replArgs(htmlOutput.String(), conf)
} else {
// Template used is text
err = texttmpl.Execute(&textOutput, conf)
if err != nil {
log.Println("ERR: Error executing text template. Abort GoSendToMsTeamsChannel().", err)
return 1
}
body = replArgs(textOutput.String(), conf)
}
// Process MsTeams API call
// ------------------------
// Define POST data for REST API call
var postData msTeamsBasicTextNotifPostData
postData.Type = msTeamsMessageCard
postData.Title = header
postData.Text = body
postData.ThemeColor = geneosThemecolor
// Build JSON data
jsonValue, err := json.Marshal(postData)
if err != nil {
log.Println("ERR: Cannot generate JSON data for msTeams API. Abort GoSendToMsTeamsChannel().", err)
return 1
}
jsonBody := bytes.NewReader(jsonValue)
// Define timeout for RST API call
if timeout, err := strconv.Atoi(getWithDefault("_TIMEOUT", conf, fmt.Sprintf("%d", DefaultMsTeamsTimeout))); err != nil {
clientTimeout = DefaultMsTeamsTimeout * time.Millisecond
} else {
clientTimeout = time.Duration(timeout) * time.Millisecond
}
// Call REST API for each target msTeams Webhook
client := &http.Client{
Timeout: clientTimeout,
}
for k, v := range msTeamsWebhooksValidity {
if v {
// Webhook is valid, proceed with REST API call / HTTP POST command
request, err := http.NewRequest("POST", k, jsonBody)
if err != nil {
log.Printf("ERR: Cannot create HTTP POST request to msTeams on URL %s. Continue. %v", k, err)
continue
}
request.Header.Set("Content-Type", "application/json")
resp, err := client.Do(request)
if err != nil {
log.Printf("ERR: Cannot complete HTTP POST request to MsTeams (target %s). Continue. %v", k, err)
} else {
log.Printf("INFO: Message sent to %s, return code %d\n", k, resp.StatusCode)
}
}
}
log.Println("INFO: GoSendToMsTeamsChannel() completed.")
return 0
} // End of GoSendToMsTeamsChannel()