Jan Dittberner
57fd5364fc
- fix spelling - mark false positives of bodyclose (see https://github.com/timakin/bodyclose/issues/30)
360 lines
9.1 KiB
Go
360 lines
9.1 KiB
Go
/*
|
|
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.
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"path"
|
|
"text/template"
|
|
|
|
"github.com/Masterminds/sprig/v3"
|
|
"gopkg.in/mail.v2"
|
|
|
|
"git.cacert.org/cacert-boardvoting/internal"
|
|
"git.cacert.org/cacert-boardvoting/internal/models"
|
|
)
|
|
|
|
type recipientData struct {
|
|
field, address, name string
|
|
}
|
|
|
|
type NotificationContent struct {
|
|
template string
|
|
data interface{}
|
|
subject string
|
|
headers map[string][]string
|
|
recipients []recipientData
|
|
}
|
|
|
|
type NotificationMail interface {
|
|
GetNotificationContent(*mailConfig) *NotificationContent
|
|
}
|
|
|
|
type MailNotifier struct {
|
|
notifyChannel chan NotificationMail
|
|
senderAddress string
|
|
dialer *mail.Dialer
|
|
quitChannel chan struct{}
|
|
infoLog, errorLog *log.Logger
|
|
mailConfig *mailConfig
|
|
}
|
|
|
|
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, "", ""),
|
|
quitChannel: make(chan struct{}),
|
|
infoLog: app.infoLog,
|
|
errorLog: app.errorLog,
|
|
mailConfig: app.mailConfig,
|
|
}
|
|
}
|
|
|
|
func (mn *MailNotifier) Start() {
|
|
mn.infoLog.Print("Launching mail notifier")
|
|
|
|
for {
|
|
select {
|
|
case notification := <-mn.notifyChannel:
|
|
content := notification.GetNotificationContent(mn.mailConfig)
|
|
|
|
mailText, err := content.buildMail(mn.mailConfig.BaseURL)
|
|
if err != nil {
|
|
mn.errorLog.Printf("building mail failed: %v", err)
|
|
|
|
continue
|
|
}
|
|
|
|
m := mail.NewMessage()
|
|
|
|
m.SetHeaders(content.headers)
|
|
m.SetAddressHeader("From", mn.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 = mn.dialer.DialAndSend(m); err != nil {
|
|
mn.errorLog.Printf("sending mail failed: %v", err)
|
|
}
|
|
case <-mn.quitChannel:
|
|
mn.infoLog.Print("ending mail notifier")
|
|
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (mn *MailNotifier) Quit() {
|
|
mn.quitChannel <- struct{}{}
|
|
}
|
|
|
|
func (mn *MailNotifier) Notify(w NotificationMail) {
|
|
mn.notifyChannel <- w
|
|
}
|
|
|
|
func (mn *MailNotifier) Ping() error {
|
|
conn, err := net.DialTimeout(
|
|
"tcp",
|
|
fmt.Sprintf("%s:%d", mn.mailConfig.SMTPHost, mn.mailConfig.SMTPPort),
|
|
mn.mailConfig.SMTPTimeOut,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("could not connect to SMTP server: %w", err)
|
|
}
|
|
|
|
defer func(conn net.Conn) {
|
|
_ = conn.Close()
|
|
}(conn)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (n *NotificationContent) buildMail(baseURL string) (fmt.Stringer, error) {
|
|
// TODO: implement a template cache for mail templates too
|
|
b, err := internal.MailTemplates.ReadFile(path.Join("mailtemplates", n.template))
|
|
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
|
|
}{Data: n.data, BaseURL: baseURL}
|
|
|
|
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
|
|
}
|
|
|
|
func defaultRecipient(mc *mailConfig) recipientData {
|
|
return recipientData{
|
|
field: "To",
|
|
address: mc.NoticeMailAddress,
|
|
name: "CAcert board mailing list",
|
|
}
|
|
}
|
|
|
|
func voteNoticeRecipient(mc *mailConfig) recipientData {
|
|
return recipientData{
|
|
field: "To",
|
|
address: mc.VoteNoticeMailAddress,
|
|
name: "CAcert board votes mailing list",
|
|
}
|
|
}
|
|
|
|
func motionReplyHeaders(m *models.Motion) map[string][]string {
|
|
return map[string][]string{
|
|
"References": {fmt.Sprintf("<%s>", m.Tag)},
|
|
"In-Reply-To": {fmt.Sprintf("<%s>", m.Tag)},
|
|
}
|
|
}
|
|
|
|
type RemindVoterNotification struct {
|
|
voter *models.User
|
|
decisions []*models.Motion
|
|
}
|
|
|
|
func (r RemindVoterNotification) GetNotificationContent(*mailConfig) *NotificationContent {
|
|
recipientAddress := make([]recipientData, 0)
|
|
|
|
if r.voter.Reminder.Valid {
|
|
recipientAddress = append(recipientAddress, recipientData{
|
|
field: "To",
|
|
address: r.voter.Reminder.String,
|
|
name: r.voter.Name,
|
|
})
|
|
}
|
|
|
|
return &NotificationContent{
|
|
template: "remind_voter_mail.txt",
|
|
data: struct {
|
|
Decisions []*models.Motion
|
|
Name string
|
|
}{Decisions: r.decisions, Name: r.voter.Name},
|
|
subject: "Outstanding CAcert board votes",
|
|
recipients: recipientAddress,
|
|
}
|
|
}
|
|
|
|
type ClosedDecisionNotification struct {
|
|
Decision *models.Motion
|
|
}
|
|
|
|
func (c *ClosedDecisionNotification) GetNotificationContent(mc *mailConfig) *NotificationContent {
|
|
return &NotificationContent{
|
|
template: "closed_motion_mail.txt",
|
|
data: struct {
|
|
*models.Motion
|
|
}{Motion: c.Decision},
|
|
subject: fmt.Sprintf("Re: %s - %s - finalized", c.Decision.Tag, c.Decision.Title),
|
|
headers: motionReplyHeaders(c.Decision),
|
|
recipients: []recipientData{defaultRecipient(mc)},
|
|
}
|
|
}
|
|
|
|
type NewDecisionNotification struct {
|
|
Decision *models.Motion
|
|
Proposer *models.User
|
|
}
|
|
|
|
func (n NewDecisionNotification) GetNotificationContent(mc *mailConfig) *NotificationContent {
|
|
voteURL := fmt.Sprintf("/vote/%s", n.Decision.Tag)
|
|
unvotedURL := "/motions/?unvoted=1"
|
|
|
|
return &NotificationContent{
|
|
template: "create_motion_mail.txt",
|
|
data: struct {
|
|
*models.Motion
|
|
Name string
|
|
VoteURL string
|
|
UnvotedURL string
|
|
}{
|
|
Motion: n.Decision,
|
|
Name: n.Proposer.Name,
|
|
VoteURL: voteURL,
|
|
UnvotedURL: unvotedURL,
|
|
},
|
|
subject: fmt.Sprintf("%s - %s", n.Decision.Tag, n.Decision.Title),
|
|
headers: n.getHeaders(),
|
|
recipients: []recipientData{defaultRecipient(mc)},
|
|
}
|
|
}
|
|
|
|
func (n NewDecisionNotification) getHeaders() map[string][]string {
|
|
return map[string][]string{
|
|
"Message-ID": {fmt.Sprintf("<%s>", n.Decision.Tag)},
|
|
}
|
|
}
|
|
|
|
type UpdateDecisionNotification struct {
|
|
Decision *models.Motion
|
|
User *models.User
|
|
}
|
|
|
|
func (u UpdateDecisionNotification) GetNotificationContent(mc *mailConfig) *NotificationContent {
|
|
voteURL := fmt.Sprintf("/vote/%s", u.Decision.Tag)
|
|
unvotedURL := "/motions/?unvoted=1"
|
|
|
|
return &NotificationContent{
|
|
template: "update_motion_mail.txt",
|
|
data: struct {
|
|
*models.Motion
|
|
Name string
|
|
VoteURL string
|
|
UnvotedURL string
|
|
}{
|
|
Motion: u.Decision,
|
|
Name: u.User.Name,
|
|
VoteURL: voteURL,
|
|
UnvotedURL: unvotedURL,
|
|
},
|
|
subject: fmt.Sprintf("%s - %s", u.Decision.Tag, u.Decision.Title),
|
|
headers: motionReplyHeaders(u.Decision),
|
|
recipients: []recipientData{defaultRecipient(mc)},
|
|
}
|
|
}
|
|
|
|
type DirectVoteNotification struct {
|
|
Decision *models.Motion
|
|
User *models.User
|
|
Choice *models.VoteChoice
|
|
}
|
|
|
|
func (d DirectVoteNotification) GetNotificationContent(mc *mailConfig) *NotificationContent {
|
|
return &NotificationContent{
|
|
template: "direct_vote_mail.txt",
|
|
data: struct {
|
|
*models.Motion
|
|
Name string
|
|
Choice *models.VoteChoice
|
|
}{
|
|
Motion: d.Decision,
|
|
Name: d.User.Name,
|
|
Choice: d.Choice,
|
|
},
|
|
subject: fmt.Sprintf("Re: %s - %s", d.Decision.Tag, d.Decision.Title),
|
|
headers: motionReplyHeaders(d.Decision),
|
|
recipients: []recipientData{voteNoticeRecipient(mc)},
|
|
}
|
|
}
|
|
|
|
type ProxyVoteNotification struct {
|
|
Decision *models.Motion
|
|
User *models.User
|
|
Voter *models.User
|
|
Choice *models.VoteChoice
|
|
Justification string
|
|
}
|
|
|
|
func (p ProxyVoteNotification) GetNotificationContent(mc *mailConfig) *NotificationContent {
|
|
return &NotificationContent{
|
|
template: "proxy_vote_mail.txt",
|
|
data: struct {
|
|
*models.Motion
|
|
Name string
|
|
Voter string
|
|
Choice *models.VoteChoice
|
|
Justification string
|
|
}{
|
|
Motion: p.Decision,
|
|
Name: p.User.Name,
|
|
Voter: p.Voter.Name,
|
|
Choice: p.Choice,
|
|
Justification: p.Justification,
|
|
},
|
|
subject: fmt.Sprintf("Re: %s - %s", p.Decision.Tag, p.Decision.Title),
|
|
headers: motionReplyHeaders(p.Decision),
|
|
recipients: []recipientData{voteNoticeRecipient(mc)},
|
|
}
|
|
}
|
|
|
|
type WithDrawMotionNotification struct {
|
|
Motion *models.Motion
|
|
Voter *models.User
|
|
}
|
|
|
|
func (w WithDrawMotionNotification) GetNotificationContent(mc *mailConfig) *NotificationContent {
|
|
return &NotificationContent{
|
|
template: "withdraw_motion_mail.txt",
|
|
data: struct {
|
|
*models.Motion
|
|
Name string
|
|
}{Motion: w.Motion, Name: w.Voter.Name},
|
|
subject: fmt.Sprintf("Re: %s - %s", w.Motion.Tag, w.Motion.Title),
|
|
headers: motionReplyHeaders(w.Motion),
|
|
recipients: []recipientData{defaultRecipient(mc)},
|
|
}
|
|
}
|