-
Notifications
You must be signed in to change notification settings - Fork 7
/
email_template.go
59 lines (54 loc) · 1.38 KB
/
email_template.go
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
package main
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"net/url"
"os"
"os/exec"
"strings"
"github.com/bradleyjkemp/abwhose/matchers"
)
func emailTemplateConfigured() bool {
_, configured := os.LookupEnv("ABWHOSE_MAILTO_TEMPLATE")
return configured
}
func offerToSendEmail(u *url.URL, contact matchers.AbuseEmail) {
emailTemplateFile, _ := os.LookupEnv("ABWHOSE_MAILTO_TEMPLATE")
emailTemplateContents, err := ioutil.ReadFile(emailTemplateFile)
if err != nil {
fmt.Printf("Failed reading email template: %v\n", err)
return
}
mailto := &bytes.Buffer{}
err = template.Must(template.New("email").Parse(string(emailTemplateContents))).Execute(mailto, map[string]interface{}{
"domain": strings.Replace(u.Hostname(), ".", "[.]", -1),
"url": strings.Replace(u.Hostname(), ".", "[.]", -1) + u.RawPath + u.RawQuery,
"recipient": contact.Email,
})
if err != nil {
fmt.Printf("Error templating email: %v\n", err)
return
}
fmt.Printf(" Send email to %s? [Y/n] ", contact.Email)
if userSaysYes() {
exec.Command("open", mailto.String()).Run()
}
}
func userSaysYes() bool {
var response string
_, err := fmt.Scanln(&response)
if err != nil && err.Error() != "unexpected newline" {
panic(err)
}
okayResponses := map[string]bool{
"": true,
"y": true,
"yes": true,
}
if okayResponses[strings.ToLower(response)] {
return true
}
return false
}