summaryrefslogtreecommitdiff
path: root/notifications.go
diff options
context:
space:
mode:
authorJan Dittberner <jandd@cacert.org>2017-04-19 23:32:12 +0200
committerJan Dittberner <jan@dittberner.info>2017-04-22 00:12:38 +0200
commit2de96dc13dc5e2843ebe204a7e1dfe6197ab7de0 (patch)
treee554bd4986e8803ed13daab5b61b35f11e06e826 /notifications.go
parent0ce9ad6dcc01375cb3f2d57f7bbdf701e605bea1 (diff)
downloadcacert-boardvoting-2de96dc13dc5e2843ebe204a7e1dfe6197ab7de0.tar.gz
cacert-boardvoting-2de96dc13dc5e2843ebe204a7e1dfe6197ab7de0.tar.xz
cacert-boardvoting-2de96dc13dc5e2843ebe204a7e1dfe6197ab7de0.zip
Implement vote closing, refactor notifications
Diffstat (limited to 'notifications.go')
-rw-r--r--notifications.go171
1 files changed, 171 insertions, 0 deletions
diff --git a/notifications.go b/notifications.go
new file mode 100644
index 0000000..beb9b3f
--- /dev/null
+++ b/notifications.go
@@ -0,0 +1,171 @@
+package main
+
+import (
+ "bytes"
+ "fmt"
+ "github.com/Masterminds/sprig"
+ "gopkg.in/gomail.v2"
+ "text/template"
+)
+
+type NotificationMail interface {
+ GetData() interface{}
+ GetTemplate() string
+ GetSubject() string
+ GetHeaders() map[string]string
+}
+
+var notifyMail = make(chan NotificationMail, 1)
+var quitMailNotifier = make(chan int)
+
+func CloseMailNotifier() {
+ quitMailNotifier <- 1
+}
+
+func MailNotifier() {
+ logger.Println("Launched mail notifier")
+ for {
+ select {
+ case notification := <-notifyMail:
+ mailText, err := buildMail(notification.GetTemplate(), notification.GetData())
+ if err != nil {
+ logger.Println("ERROR building mail:", 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.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 <-quitMailNotifier:
+ fmt.Println("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))
+ if err != nil {
+ return
+ }
+
+ mailText = bytes.NewBufferString("")
+ t.Execute(mailText, context)
+
+ return
+}
+
+type NotificationClosedDecision struct {
+ decision Decision
+ voteSums VoteSums
+}
+
+func (n *NotificationClosedDecision) GetData() interface{} {
+ return struct {
+ *Decision
+ *VoteSums
+ }{&n.decision, &n.voteSums}
+}
+
+func (n *NotificationClosedDecision) GetTemplate() string {
+ return "closed_motion_mail.txt"
+}
+
+func (n *NotificationClosedDecision) GetSubject() string {
+ return fmt.Sprintf("Re: %s - %s - finalised", n.decision.Tag, n.decision.Title)
+}
+
+func (n *NotificationClosedDecision) GetHeaders() map[string]string {
+ return map[string]string{"References": fmt.Sprintf("<%s>", n.decision.Tag)}
+}
+
+type NotificationCreateMotion struct {
+ decision Decision
+ voter Voter
+}
+
+func (n *NotificationCreateMotion) GetData() interface{} {
+ voteURL := fmt.Sprintf("%s/vote", config.BaseURL)
+ 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)
+}
+
+func (n *NotificationCreateMotion) GetHeaders() map[string]string {
+ return map[string]string{"Message-ID": fmt.Sprintf("<%s>", n.decision.Tag)}
+}
+
+type NotificationUpdateMotion struct {
+ decision Decision
+ voter Voter
+}
+
+func (n *NotificationUpdateMotion) GetData() interface{} {
+ voteURL := fmt.Sprintf("%s/vote", config.BaseURL)
+ 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 *NotificationUpdateMotion) GetTemplate() string {
+ return "update_motion_mail.txt"
+}
+
+func (n *NotificationUpdateMotion) GetSubject() string {
+ return fmt.Sprintf("Re: %s - %s", n.decision.Tag, n.decision.Title)
+}
+
+func (n *NotificationUpdateMotion) GetHeaders() map[string]string {
+ return map[string]string{"References": fmt.Sprintf("<%s>", n.decision.Tag)}
+}
+
+type NotificationWithDrawMotion struct {
+ decision Decision
+ voter Voter
+}
+
+func (n *NotificationWithDrawMotion) GetData() interface{} {
+ return struct {
+ *Decision
+ Name string
+ }{&n.decision, n.voter.Name}
+}
+
+func (n *NotificationWithDrawMotion) GetTemplate() string {
+ return "withdraw_motion_mail.txt"
+}
+
+func (n *NotificationWithDrawMotion) GetSubject() string {
+ return fmt.Sprintf("Re: %s - %s - withdrawn", n.decision.Tag, n.decision.Title)
+}
+
+func (n *NotificationWithDrawMotion) GetHeaders() map[string]string {
+ return map[string]string{"References": fmt.Sprintf("<%s>", n.decision.Tag)}
+}