cacert-boardvoting/cmd/boardvoting/notifications.go

231 lines
6.4 KiB
Go
Raw Normal View History

2022-05-21 12:09:19 +00:00
/*
Copyright 2022 CAcert Inc.
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2022-05-15 18:10:49 +00:00
package main
import (
"bytes"
"fmt"
"path"
"text/template"
"github.com/Masterminds/sprig/v3"
"gopkg.in/mail.v2"
2022-05-21 12:09:19 +00:00
"git.cacert.org/cacert-boardvoting/internal"
"git.cacert.org/cacert-boardvoting/internal/models"
2022-05-15 18:10:49 +00:00
)
type recipientData struct {
field, address, name string
}
type NotificationContent struct {
template string
data interface{}
subject string
2022-05-21 11:51:17 +00:00
headers map[string][]string
2022-05-15 18:10:49 +00:00
recipients []recipientData
}
type NotificationMail interface {
2022-05-21 11:51:17 +00:00
GetNotificationContent(*mailConfig) *NotificationContent
2022-05-15 18:10:49 +00:00
}
type MailNotifier struct {
notifyChannel chan NotificationMail
senderAddress string
dialer *mail.Dialer
2022-05-21 12:09:19 +00:00
quitChannel chan struct{}
2022-05-15 18:10:49 +00:00
}
func (app *application) NewMailNotifier() {
app.mailNotifier = &MailNotifier{
notifyChannel: make(chan NotificationMail, 1),
senderAddress: app.mailConfig.NotificationSenderAddress,
dialer: mail.NewDialer(app.mailConfig.SMTPHost, app.mailConfig.SMTPPort, "", ""),
2022-05-21 12:09:19 +00:00
quitChannel: make(chan struct{}),
2022-05-15 18:10:49 +00:00
}
}
func (app *application) StartMailNotifier() {
app.infoLog.Print("Launching mail notifier")
for {
select {
case notification := <-app.mailNotifier.notifyChannel:
2022-05-21 11:51:17 +00:00
content := notification.GetNotificationContent(app.mailConfig)
2022-05-15 18:10:49 +00:00
mailText, err := content.buildMail(app.mailConfig.BaseURL)
2022-05-15 18:10:49 +00:00
if err != nil {
app.errorLog.Printf("building mail failed: %v", err)
continue
}
m := mail.NewMessage()
2022-05-21 11:51:17 +00:00
m.SetHeaders(content.headers)
2022-05-15 18:10:49 +00:00
m.SetAddressHeader("From", app.mailNotifier.senderAddress, "CAcert board voting system")
for _, recipient := range content.recipients {
m.SetAddressHeader(recipient.field, recipient.address, recipient.name)
}
m.SetHeader("Subject", content.subject)
m.SetBody("text/plain", mailText.String())
if err = app.mailNotifier.dialer.DialAndSend(m); err != nil {
app.errorLog.Printf("sending mail failed: %v", err)
}
2022-05-21 12:09:19 +00:00
case <-app.mailNotifier.quitChannel:
2022-05-15 18:10:49 +00:00
app.infoLog.Print("ending mail notifier")
return
}
}
}
2022-05-21 12:09:19 +00:00
func (m *MailNotifier) Quit() {
m.quitChannel <- struct{}{}
}
func (n *NotificationContent) buildMail(baseURL string) (fmt.Stringer, error) {
b, err := internal.MailTemplates.ReadFile(path.Join("mailtemplates", n.template))
2022-05-15 18:10:49 +00:00
if err != nil {
return nil, fmt.Errorf("could not read mail template %s: %w", n.template, err)
}
t, err := template.New(n.template).Funcs(sprig.GenericFuncMap()).Parse(string(b))
if err != nil {
return nil, fmt.Errorf("could not parse mail template %s: %w", n.template, err)
}
data := struct {
Data any
BaseURL string
2022-05-21 12:09:19 +00:00
}{Data: n.data, BaseURL: baseURL}
2022-05-15 18:10:49 +00:00
mailText := bytes.NewBuffer(make([]byte, 0))
if err = t.Execute(mailText, data); err != nil {
return nil, fmt.Errorf(
"failed to execute template %s with context %v: %w", n.template, n.data, err)
}
return mailText, nil
}
type RemindVoterNotification struct {
voter *models.User
decisions []*models.Motion
2022-05-15 18:10:49 +00:00
}
2022-05-21 11:51:17 +00:00
func (r RemindVoterNotification) GetNotificationContent(*mailConfig) *NotificationContent {
2022-05-15 18:10:49 +00:00
return &NotificationContent{
template: "remind_voter_mail.txt",
data: struct {
Decisions []*models.Motion
2022-05-15 18:10:49 +00:00
Name string
}{Decisions: r.decisions, Name: r.voter.Name},
subject: "Outstanding CAcert board votes",
recipients: []recipientData{{"To", r.voter.Reminder, r.voter.Name}},
}
}
2022-05-21 11:51:17 +00:00
func defaultRecipient(mc *mailConfig) recipientData {
return recipientData{field: "To", address: mc.NoticeMailAddress, name: "CAcert board mailing list"}
}
2022-05-21 11:51:17 +00:00
type ClosedDecisionNotification struct {
decision *models.ClosedMotion
2022-05-21 11:51:17 +00:00
}
func (c *ClosedDecisionNotification) GetNotificationContent(mc *mailConfig) *NotificationContent {
return &NotificationContent{
template: "closed_motion_mail.txt",
data: c.decision,
subject: fmt.Sprintf("Re: %s - %s - finalised", c.decision.Decision.Tag, c.decision.Decision.Title),
headers: c.getHeaders(),
recipients: []recipientData{defaultRecipient(mc)},
2022-05-21 11:51:17 +00:00
}
}
func (c *ClosedDecisionNotification) getHeaders() map[string][]string {
return map[string][]string{
"References": {fmt.Sprintf("<%s>", c.decision.Decision.Tag)},
"In-Reply-To": {fmt.Sprintf("<%s>", c.decision.Decision.Tag)},
}
}
type NewDecisionNotification struct {
2022-05-26 19:04:47 +00:00
Decision *models.NewMotion
}
func (n NewDecisionNotification) GetNotificationContent(mc *mailConfig) *NotificationContent {
2022-05-26 19:04:47 +00:00
voteURL := fmt.Sprintf("/vote/%s", n.Decision.Decision.Tag)
unvotedURL := "/motions/?unvoted=1"
return &NotificationContent{
template: "create_motion_mail.txt",
data: struct {
*models.Motion
Name string
VoteURL string
UnvotedURL string
2022-05-26 19:04:47 +00:00
}{n.Decision.Decision, n.Decision.Proposer.Name, voteURL, unvotedURL},
subject: fmt.Sprintf("%s - %s", n.Decision.Decision.Tag, n.Decision.Decision.Title),
headers: n.getHeaders(),
recipients: []recipientData{defaultRecipient(mc)},
}
}
func (n NewDecisionNotification) getHeaders() map[string][]string {
return map[string][]string{
2022-05-26 19:04:47 +00:00
"Message-ID": {fmt.Sprintf("<%s>", n.Decision.Decision.Tag)},
}
}
type UpdateDecisionNotification struct {
Decision *models.UpdatedMotion
}
func (u UpdateDecisionNotification) GetNotificationContent(mc *mailConfig) *NotificationContent {
voteURL := fmt.Sprintf("/vote/%s", u.Decision.Decision.Tag)
unvotedURL := "/motions/?unvoted=1"
return &NotificationContent{
template: "update_motion_mail.txt",
data: struct {
*models.Motion
Name string
VoteURL string
UnvotedURL string
}{u.Decision.Decision, u.Decision.User.Name, voteURL, unvotedURL},
subject: fmt.Sprintf("%s - %s", u.Decision.Decision.Tag, u.Decision.Decision.Title),
headers: u.getHeaders(),
recipients: []recipientData{defaultRecipient(mc)},
}
}
func (u UpdateDecisionNotification) getHeaders() map[string][]string {
return map[string][]string{
"References": {fmt.Sprintf("<%s>", u.Decision.Decision.Tag)},
"In-Reply-To": {fmt.Sprintf("<%s>", u.Decision.Decision.Tag)},
}
2022-05-21 11:51:17 +00:00
}