Only archive the cacert-boardvoting binary and the config example
[cacert-boardvoting.git] / notifications.go
index d4b9715..25234ff 100644 (file)
@@ -3,209 +3,297 @@ package main
 import (
        "bytes"
        "fmt"
+       "git.cacert.org/cacert-boardvoting/boardvoting"
        "github.com/Masterminds/sprig"
        "gopkg.in/gomail.v2"
        "text/template"
 )
 
-type NotificationMail interface {
-       GetData() interface{}
-       GetTemplate() string
-       GetSubject() string
-       GetHeaders() map[string]string
+type headerData struct {
+       name  string
+       value []string
 }
 
-type VoterMail interface {
-       GetData() interface{}
-       GetTemplate() string
-       GetSubject() string
-       GetRecipient() (string, string)
+type headerList []headerData
+
+type recipientData struct {
+       field, address, name string
 }
 
-var notifyMail = make(chan NotificationMail, 1)
-var voterMail = make(chan VoterMail, 1)
-var quitMailNotifier = make(chan int)
+type notificationContent struct {
+       template   string
+       data       interface{}
+       subject    string
+       headers    headerList
+       recipients []recipientData
+}
 
-func CloseMailNotifier() {
-       quitMailNotifier <- 1
+type NotificationMail interface {
+       GetNotificationContent() *notificationContent
 }
 
-func MailNotifier() {
-       logger.Println("Launched mail notifier")
+var NotifyMailChannel = make(chan NotificationMail, 1)
+
+func MailNotifier(quitMailNotifier chan int) {
+       log.Info("Launched mail notifier")
        for {
                select {
-               case notification := <-notifyMail:
-                       mailText, err := buildMail(notification.GetTemplate(), notification.GetData())
+               case notification := <-NotifyMailChannel:
+                       content := notification.GetNotificationContent()
+                       mailText, err := buildMail(content.template, content.data)
                        if err != nil {
-                               logger.Println("ERROR building mail:", err)
+                               log.Errorf("building mail failed: %v", err)
                                continue
                        }
 
                        m := gomail.NewMessage()
-                       m.SetHeader("From", config.NoticeSenderAddress)
-                       m.SetHeader("To", config.BoardMailAddress)
-                       m.SetHeader("Subject", notification.GetSubject())
-                       for header, value := range notification.GetHeaders() {
-                               m.SetHeader(header, value)
+                       m.SetAddressHeader("From", config.NotificationSenderAddress, "CAcert board voting system")
+                       for _, recipient := range content.recipients {
+                               m.SetAddressHeader(recipient.field, recipient.address, recipient.name)
                        }
-                       m.SetBody("text/plain", mailText.String())
-
-                       d := gomail.NewDialer(config.MailServer.Host, config.MailServer.Port, "", "")
-                       if err := d.DialAndSend(m); err != nil {
-                               logger.Println("ERROR sending mail:", err)
-                       }
-               case notification := <-voterMail:
-                       mailText, err := buildMail(notification.GetTemplate(), notification.GetData())
-                       if err != nil {
-                               logger.Println("ERROR building mail:", err)
-                               continue
+                       m.SetHeader("Subject", content.subject)
+                       for _, header := range content.headers {
+                               m.SetHeader(header.name, header.value...)
                        }
-
-                       m := gomail.NewMessage()
-                       m.SetHeader("From", config.ReminderSenderAddress)
-                       address, name := notification.GetRecipient()
-                       m.SetAddressHeader("To", address, name)
-                       m.SetHeader("Subject", notification.GetSubject())
                        m.SetBody("text/plain", mailText.String())
 
-                       d := gomail.NewDialer(config.MailServer.Host, config.MailServer.Port, "", "")
+                       d := gomail.NewPlainDialer(config.MailServer.Host, config.MailServer.Port, "", "")
                        if err := d.DialAndSend(m); err != nil {
-                               logger.Println("ERROR sending mail:", err)
+                               log.Errorf("sending mail failed: %v", err)
                        }
                case <-quitMailNotifier:
-                       fmt.Println("Ending mail notifier")
+                       log.Info("Ending mail notifier")
                        return
                }
        }
 }
 
 func buildMail(templateName string, context interface{}) (mailText *bytes.Buffer, err error) {
-       t, err := template.New(templateName).Funcs(
-               sprig.GenericFuncMap()).ParseFiles(fmt.Sprintf("templates/%s", templateName))
+       b, err := boardvoting.Asset(fmt.Sprintf("templates/%s", templateName))
+       if err != nil {
+               return
+       }
+       t, err := template.New(templateName).Funcs(sprig.GenericFuncMap()).Parse(string(b))
        if err != nil {
                return
        }
 
        mailText = bytes.NewBufferString("")
-       t.Execute(mailText, context)
+       if err := t.Execute(mailText, context); err != nil {
+               log.Errorf("Failed to execute template %s with context %+v: %v", templateName, context, err)
+               return nil, err
+       }
 
        return
 }
 
-type NotificationClosedDecision struct {
+type notificationBase struct{}
+
+func (n *notificationBase) getRecipient() recipientData {
+       return recipientData{field: "To", address: config.NoticeMailAddress, name: "CAcert board mailing list"}
+}
+
+type decisionReplyBase struct {
        decision Decision
-       voteSums VoteSums
 }
 
-func (n *NotificationClosedDecision) GetData() interface{} {
-       return struct {
-               *Decision
-               *VoteSums
-       }{&n.decision, &n.voteSums}
+func (n *decisionReplyBase) getHeaders() headerList {
+       headers := make(headerList, 0)
+       headers = append(headers, headerData{
+               name: "References", value: []string{fmt.Sprintf("<%s>", n.decision.Tag)},
+       })
+       headers = append(headers, headerData{
+               name: "In-Reply-To", value: []string{fmt.Sprintf("<%s>", n.decision.Tag)},
+       })
+       return headers
+}
+
+func (n *decisionReplyBase) getSubject() string {
+       return fmt.Sprintf("Re: %s - %s", n.decision.Tag, n.decision.Title)
 }
 
-func (n *NotificationClosedDecision) GetTemplate() string { return "closed_motion_mail.txt" }
+type notificationClosedDecision struct {
+       notificationBase
+       decisionReplyBase
+       voteSums  VoteSums
+       reasoning string
+}
 
-func (n *NotificationClosedDecision) GetSubject() string {
-       return fmt.Sprintf("Re: %s - %s - finalised", n.decision.Tag, n.decision.Title)
+func NewNotificationClosedDecision(decision *Decision, voteSums *VoteSums, reasoning string) NotificationMail {
+       notification := &notificationClosedDecision{voteSums: *voteSums, reasoning: reasoning}
+       notification.decision = *decision
+       return notification
 }
 
-func (n *NotificationClosedDecision) GetHeaders() map[string]string {
-       return map[string]string{"References": fmt.Sprintf("<%s>", n.decision.Tag)}
+func (n *notificationClosedDecision) GetNotificationContent() *notificationContent {
+       return &notificationContent{
+               template: "closed_motion_mail.txt",
+               data: struct {
+                       *Decision
+                       *VoteSums
+                       Reasoning string
+               }{&n.decision, &n.voteSums, n.reasoning},
+               subject:    fmt.Sprintf("Re: %s - %s - finalised", n.decision.Tag, n.decision.Title),
+               headers:    n.decisionReplyBase.getHeaders(),
+               recipients: []recipientData{n.notificationBase.getRecipient()},
+       }
 }
 
 type NotificationCreateMotion struct {
+       notificationBase
        decision Decision
        voter    Voter
 }
 
-func (n *NotificationCreateMotion) GetData() interface{} {
-       voteURL := fmt.Sprintf("%s/vote", config.BaseURL)
+func (n *NotificationCreateMotion) GetNotificationContent() *notificationContent {
+       voteURL := fmt.Sprintf("%s/vote/%s", config.BaseURL, n.decision.Tag)
        unvotedURL := fmt.Sprintf("%s/motions/?unvoted=1", config.BaseURL)
-       return struct {
-               *Decision
-               Name       string
-               VoteURL    string
-               UnvotedURL string
-       }{&n.decision, n.voter.Name, voteURL, unvotedURL}
-}
-
-func (n *NotificationCreateMotion) GetTemplate() string { return "create_motion_mail.txt" }
-
-func (n *NotificationCreateMotion) GetSubject() string {
-       return fmt.Sprintf("%s - %s", n.decision.Tag, n.decision.Title)
+       return &notificationContent{
+               template: "create_motion_mail.txt",
+               data: struct {
+                       *Decision
+                       Name       string
+                       VoteURL    string
+                       UnvotedURL string
+               }{&n.decision, n.voter.Name, voteURL, unvotedURL},
+               subject:    fmt.Sprintf("%s - %s", n.decision.Tag, n.decision.Title),
+               headers:    headerList{headerData{"Message-ID", []string{fmt.Sprintf("<%s>", n.decision.Tag)}}},
+               recipients: []recipientData{n.notificationBase.getRecipient()},
+       }
 }
 
-func (n *NotificationCreateMotion) GetHeaders() map[string]string {
-       return map[string]string{"Message-ID": fmt.Sprintf("<%s>", n.decision.Tag)}
+type notificationUpdateMotion struct {
+       notificationBase
+       decisionReplyBase
+       voter Voter
 }
 
-type NotificationUpdateMotion struct {
-       decision Decision
-       voter    Voter
+func NewNotificationUpdateMotion(decision Decision, voter Voter) NotificationMail {
+       notification := notificationUpdateMotion{voter: voter}
+       notification.decision = decision
+       return &notification
 }
 
-func (n *NotificationUpdateMotion) GetData() interface{} {
-       voteURL := fmt.Sprintf("%s/vote", config.BaseURL)
+func (n *notificationUpdateMotion) GetNotificationContent() *notificationContent {
+       voteURL := fmt.Sprintf("%s/vote/%s", config.BaseURL, n.decision.Tag)
        unvotedURL := fmt.Sprintf("%s/motions/?unvoted=1", config.BaseURL)
-       return struct {
-               *Decision
-               Name       string
-               VoteURL    string
-               UnvotedURL string
-       }{&n.decision, n.voter.Name, voteURL, unvotedURL}
+       return &notificationContent{
+               template: "update_motion_mail.txt",
+               data: struct {
+                       *Decision
+                       Name       string
+                       VoteURL    string
+                       UnvotedURL string
+               }{&n.decision, n.voter.Name, voteURL, unvotedURL},
+               subject:    n.decisionReplyBase.getSubject(),
+               headers:    n.decisionReplyBase.getHeaders(),
+               recipients: []recipientData{n.notificationBase.getRecipient()},
+       }
 }
 
-func (n *NotificationUpdateMotion) GetTemplate() string { return "update_motion_mail.txt" }
+type notificationWithDrawMotion struct {
+       notificationBase
+       decisionReplyBase
+       voter Voter
+}
 
-func (n *NotificationUpdateMotion) GetSubject() string {
-       return fmt.Sprintf("Re: %s - %s", n.decision.Tag, n.decision.Title)
+func NewNotificationWithDrawMotion(decision *Decision, voter *Voter) NotificationMail {
+       notification := &notificationWithDrawMotion{voter: *voter}
+       notification.decision = *decision
+       return notification
 }
 
-func (n *NotificationUpdateMotion) GetHeaders() map[string]string {
-       return map[string]string{"References": fmt.Sprintf("<%s>", n.decision.Tag)}
+func (n *notificationWithDrawMotion) GetNotificationContent() *notificationContent {
+       return &notificationContent{
+               template: "withdraw_motion_mail.txt",
+               data: struct {
+                       *Decision
+                       Name string
+               }{&n.decision, n.voter.Name},
+               subject:    fmt.Sprintf("Re: %s - %s - withdrawn", n.decision.Tag, n.decision.Title),
+               headers:    n.decisionReplyBase.getHeaders(),
+               recipients: []recipientData{n.notificationBase.getRecipient()},
+       }
 }
 
-type NotificationWithDrawMotion struct {
-       decision Decision
-       voter    Voter
+type RemindVoterNotification struct {
+       voter     Voter
+       decisions []Decision
 }
 
-func (n *NotificationWithDrawMotion) GetData() interface{} {
-       return struct {
-               *Decision
-               Name string
-       }{&n.decision, n.voter.Name}
+func (n *RemindVoterNotification) GetNotificationContent() *notificationContent {
+       return &notificationContent{
+               template: "remind_voter_mail.txt",
+               data: struct {
+                       Decisions []Decision
+                       Name      string
+                       BaseURL   string
+               }{n.decisions, n.voter.Name, config.BaseURL},
+               subject:    "Outstanding CAcert board votes",
+               recipients: []recipientData{{"To", n.voter.Reminder, n.voter.Name}},
+       }
 }
 
-func (n *NotificationWithDrawMotion) GetTemplate() string { return "withdraw_motion_mail.txt" }
+type voteNotificationBase struct{}
 
-func (n *NotificationWithDrawMotion) GetSubject() string {
-       return fmt.Sprintf("Re: %s - %s - withdrawn", n.decision.Tag, n.decision.Title)
+func (n *voteNotificationBase) getRecipient() recipientData {
+       return recipientData{"To", config.VoteNoticeMailAddress, "CAcert board votes mailing list"}
 }
 
-func (n *NotificationWithDrawMotion) GetHeaders() map[string]string {
-       return map[string]string{"References": fmt.Sprintf("<%s>", n.decision.Tag)}
+type notificationProxyVote struct {
+       voteNotificationBase
+       decisionReplyBase
+       proxy         Voter
+       voter         Voter
+       vote          Vote
+       justification string
 }
 
-type RemindVoterNotification struct {
-       voter     Voter
-       decisions []Decision
+func NewNotificationProxyVote(decision *Decision, proxy *Voter, voter *Voter, vote *Vote, justification string) NotificationMail {
+       notification := &notificationProxyVote{proxy: *proxy, voter: *voter, vote: *vote, justification: justification}
+       notification.decision = *decision
+       return notification
 }
 
-func (n *RemindVoterNotification) GetData() interface{} {
-       return struct {
-               Decisions []Decision
-               Name      string
-               BaseURL   string
-       }{n.decisions, n.voter.Name, config.BaseURL}
+func (n *notificationProxyVote) GetNotificationContent() *notificationContent {
+       return &notificationContent{
+               template: "proxy_vote_mail.txt",
+               data: struct {
+                       Proxy         string
+                       Vote          VoteChoice
+                       Voter         string
+                       Decision      *Decision
+                       Justification string
+               }{n.proxy.Name, n.vote.Vote, n.voter.Name, &n.decision, n.justification},
+               subject:    n.decisionReplyBase.getSubject(),
+               headers:    n.decisionReplyBase.getHeaders(),
+               recipients: []recipientData{n.voteNotificationBase.getRecipient()},
+       }
 }
 
-func (n *RemindVoterNotification) GetTemplate() string { return "remind_voter_mail.txt" }
+type notificationDirectVote struct {
+       voteNotificationBase
+       decisionReplyBase
+       voter Voter
+       vote  Vote
+}
 
-func (n *RemindVoterNotification) GetSubject() string { return "Outstanding CAcert board votes" }
+func NewNotificationDirectVote(decision *Decision, voter *Voter, vote *Vote) NotificationMail {
+       notification := &notificationDirectVote{voter: *voter, vote: *vote}
+       notification.decision = *decision
+       return notification
+}
 
-func (n *RemindVoterNotification) GetRecipient() (address string, name string) {
-       address, name = n.voter.Reminder, n.voter.Name
-       return
+func (n *notificationDirectVote) GetNotificationContent() *notificationContent {
+       return &notificationContent{
+               template: "direct_vote_mail.txt",
+               data: struct {
+                       Vote     VoteChoice
+                       Voter    string
+                       Decision *Decision
+               }{n.vote.Vote, n.voter.Name, &n.decision},
+               subject:    n.decisionReplyBase.getSubject(),
+               headers:    n.decisionReplyBase.getHeaders(),
+               recipients: []recipientData{n.voteNotificationBase.getRecipient()},
+       }
 }