summaryrefslogtreecommitdiff
path: root/notifications.go
blob: 28c6a2182b375fbc00a2c833dd4d4df5dc84d2fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main

import (
	"bytes"
	"fmt"
	"git.cacert.org/cacert-boardvoting/boardvoting"
	"github.com/Masterminds/sprig"
	"gopkg.in/gomail.v2"
	"text/template"

	log "github.com/sirupsen/logrus"
)

type headerData struct {
	name  string
	value []string
}

type headerList []headerData

type recipientData struct {
	field, address, name string
}

type notificationContent struct {
	template   string
	data       interface{}
	subject    string
	headers    headerList
	recipients []recipientData
}

type NotificationMail interface {
	GetNotificationContent() *notificationContent
}

var NotifyMailChannel = make(chan NotificationMail, 1)

func MailNotifier(quitMailNotifier chan int) {
	log.Info("Launched mail notifier")
	for {
		select {
		case notification := <-NotifyMailChannel:
			content := notification.GetNotificationContent()
			mailText, err := buildMail(content.template, content.data)
			if err != nil {
				log.Errorf("building mail failed: %v", err)
				continue
			}

			m := gomail.NewMessage()
			m.SetAddressHeader("From", config.NotificationSenderAddress, "CAcert board voting system")
			for _, recipient := range content.recipients {
				m.SetAddressHeader(recipient.field, recipient.address, recipient.name)
			}
			m.SetHeader("Subject", content.subject)
			for _, header := range content.headers {
				m.SetHeader(header.name, header.value...)
			}
			m.SetBody("text/plain", mailText.String())

			d := gomail.NewDialer(config.MailServer.Host, config.MailServer.Port, "", "")
			if err := d.DialAndSend(m); err != nil {
				log.Errorf("sending mail failed: %v", err)
			}
		case <-quitMailNotifier:
			log.Info("Ending mail notifier")
			return
		}
	}
}

func buildMail(templateName string, context interface{}) (mailText *bytes.Buffer, err error) {
	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("")
	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 notificationBase struct{}

func (n *notificationBase) getRecipient() recipientData {
	return recipientData{field: "To", address: config.NoticeMailAddress, name: "CAcert board mailing list"}
}

type decisionReplyBase struct {
	decision Decision
}

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)
}

type notificationClosedDecision struct {
	notificationBase
	decisionReplyBase
	voteSums  VoteSums
	reasoning string
}

func NewNotificationClosedDecision(decision *Decision, voteSums *VoteSums, reasoning string) NotificationMail {
	notification := &notificationClosedDecision{voteSums: *voteSums, reasoning: reasoning}
	notification.decision = *decision
	return notification
}

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) GetNotificationContent() *notificationContent {
	voteURL := fmt.Sprintf("%s/vote/%s", config.BaseURL, n.decision.Tag)
	unvotedURL := fmt.Sprintf("%s/motions/?unvoted=1", config.BaseURL)
	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()},
	}
}

type notificationUpdateMotion struct {
	notificationBase
	decisionReplyBase
	voter Voter
}

func NewNotificationUpdateMotion(decision Decision, voter Voter) NotificationMail {
	notification := notificationUpdateMotion{voter: voter}
	notification.decision = decision
	return &notification
}

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 &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()},
	}
}

type notificationWithDrawMotion struct {
	notificationBase
	decisionReplyBase
	voter Voter
}

func NewNotificationWithDrawMotion(decision *Decision, voter *Voter) NotificationMail {
	notification := &notificationWithDrawMotion{voter: *voter}
	notification.decision = *decision
	return notification
}

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 RemindVoterNotification struct {
	voter     Voter
	decisions []Decision
}

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}},
	}
}

type voteNotificationBase struct{}

func (n *voteNotificationBase) getRecipient() recipientData {
	return recipientData{"To", config.VoteNoticeMailAddress, "CAcert board votes mailing list"}
}

type notificationProxyVote struct {
	voteNotificationBase
	decisionReplyBase
	proxy         Voter
	voter         Voter
	vote          Vote
	justification string
}

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 *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()},
	}
}

type notificationDirectVote struct {
	voteNotificationBase
	decisionReplyBase
	voter Voter
	vote  Vote
}

func NewNotificationDirectVote(decision *Decision, voter *Voter, vote *Vote) NotificationMail {
	notification := &notificationDirectVote{voter: *voter, vote: *vote}
	notification.decision = *decision
	return notification
}

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()},
	}
}