diff options
| author | Shulhan <ms@kilabit.info> | 2026-01-20 22:28:36 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-01-20 22:28:36 +0700 |
| commit | d0969869954c299c04ea58ab0fda1eba6a0350da (patch) | |
| tree | 8d316903a98e384ffb7746819618521eb66683eb /worker.go | |
| parent | bf87c6ad4824c7ed1c990aeff2d2883936c1f20a (diff) | |
| download | lilin-d0969869954c299c04ea58ab0fda1eba6a0350da.tar.xz | |
all: support sending notification to SMTP server (email)
In the main configuration, lilin.cfg, user now can add "notif" section with
kind "smtp" to send notification using user's email.
This require renaming "webhook_url" to "remote_url" to minimize duplicate
field, so different kind of notif can use the same "remote_url" value.
Diffstat (limited to 'worker.go')
| -rw-r--r-- | worker.go | 86 |
1 files changed, 85 insertions, 1 deletions
@@ -6,6 +6,7 @@ package lilin import ( "bytes" "encoding/json" + "fmt" "io" "log" "net/http" @@ -14,7 +15,9 @@ import ( "strings" "time" + "git.sr.ht/~shulhan/pakakeh.go/lib/email" "git.sr.ht/~shulhan/pakakeh.go/lib/ini" + "git.sr.ht/~shulhan/pakakeh.go/lib/smtp" ) // worker contains the report of all services. @@ -174,6 +177,8 @@ func (wrk *worker) handlePushNotif() { switch notifConfig.Kind { case notifKindMattermost: wrk.pushNotifMattermost(notifConfig, &scanReport) + case notifKindSMTP: + wrk.pushNotifSMTP(notifConfig, &scanReport) } } } @@ -213,7 +218,7 @@ func (wrk *worker) pushNotifMattermost( var req = &http.Request{ Method: http.MethodPost, - URL: notifConfig.webhookURL, + URL: notifConfig.remoteURL, Header: http.Header{ `Content-Type`: []string{ `application/json`, @@ -242,3 +247,82 @@ func (wrk *worker) pushNotifMattermost( log.Printf(`%s: fail with status code %d: %s`, logp, resp.StatusCode, body) } + +// pushNotifSMTP send notification through SMTP. +func (wrk *worker) pushNotifSMTP(notifConfig *NotifConfig, scanReport *ScanReport) { + var logp = `pushNotifSMTP` + var msg = email.Message{} + var err error + + err = msg.SetFrom(notifConfig.User) + if err != nil { + log.Printf(`%s: %s`, logp, err) + return + } + + for _, recipient := range notifConfig.Recipient { + err = msg.AddTo(recipient) + if err != nil { + log.Printf(`%s: %s`, logp, err) + return + } + } + + var subject string + var text bytes.Buffer + if scanReport.Success { + subject = fmt.Sprintf(`[lilin] Service %s is up!`, scanReport.ID) + err = notifConfig.upTmpl.Execute(&text, scanReport) + if err != nil { + log.Printf(`%s: %s`, logp, err) + return + } + } else { + subject = fmt.Sprintf(`[lilin] Service %s is down!`, scanReport.ID) + err = notifConfig.downTmpl.Execute(&text, scanReport) + if err != nil { + log.Printf(`%s: %s`, logp, err) + return + } + } + + msg.SetSubject(subject) + + err = msg.SetBodyText(text.Bytes()) + if err != nil { + log.Printf(`%s: %s`, logp, err) + return + } + + var data []byte + data, err = msg.Pack() + if err != nil { + log.Printf(`%s: %s`, logp, err) + return + } + + var mailtx = smtp.NewMailTx(notifConfig.User, notifConfig.Recipient, data) + + var clientOpts = smtp.ClientOptions{ + ServerURL: notifConfig.RemoteURL, + AuthUser: notifConfig.User, + AuthPass: notifConfig.Password, + AuthMechanism: smtp.SaslMechanismPlain, + } + var smtpc *smtp.Client + smtpc, err = smtp.NewClient(clientOpts) + if err != nil { + log.Printf(`%s: %s`, logp, err) + return + } + + _, err = smtpc.MailTx(mailtx) + if err != nil { + log.Printf(`%s: %s`, logp, err) + } + + _, err = smtpc.Quit() + if err != nil { + log.Printf(`%s: %s`, logp, err) + } +} |
