/
notify_params.go
161 lines (127 loc) · 3.27 KB
/
notify_params.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
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
package notify
import (
"bytes"
"encoding/json"
"io"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/cloudfoundry-incubator/notifications/v1/web/webutil"
)
const InvalidEmail = "<>InvalidEmail<>"
var (
validOrganizationRoles = []string{"OrgManager", "OrgAuditor", "BillingManager"}
emailRegexp = regexp.MustCompile("[^<]*<([^@]*@[^@]*)>|([^<][^@]*@[^@]*)")
)
type NotifyParams struct {
ReplyTo string `json:"reply_to"`
Subject string `json:"subject"`
Text string `json:"text"`
RawHTML string `json:"html"`
KindID string `json:"kind_id"`
To string `json:"to"`
Role string `json:"role"`
ParsedHTML HTML
KindDescription string
SourceDescription string
Errors []string
}
type HTML struct {
BodyContent string
BodyAttributes string
Head string
Doctype string
}
func NewNotifyParams(body io.ReadCloser) (NotifyParams, error) {
notify := NotifyParams{}
err := notify.parseRequestBody(body)
if err != nil {
return notify, err
}
err = notify.FormatEmailAndExtractHTML()
if err != nil {
return notify, err
}
return notify, nil
}
func (notify *NotifyParams) parseRequestBody(body io.ReadCloser) error {
defer body.Close()
buffer := bytes.NewBuffer([]byte{})
buffer.ReadFrom(body)
if buffer.Len() > 0 {
err := json.Unmarshal(buffer.Bytes(), ¬ify)
if err != nil {
return webutil.ParseError{}
}
}
return nil
}
func (notify *NotifyParams) FormatEmailAndExtractHTML() error {
notify.To = EmailFormatter{}.Format(notify.To)
doctype, head, bodyContent, bodyAttributes, err := HTMLExtractor{}.Extract(notify.RawHTML)
if err != nil {
return err
}
notify.ParsedHTML.Doctype = doctype
notify.ParsedHTML.Head = head
notify.ParsedHTML.BodyContent = bodyContent
notify.ParsedHTML.BodyAttributes = bodyAttributes
return nil
}
type EmailFormatter struct{}
func (EmailFormatter) Format(email string) string {
if email == "" {
return email
}
matches := emailRegexp.FindStringSubmatch(email)
if len(matches) == 0 {
return InvalidEmail
}
if matches[1] != "" {
return matches[1]
}
return matches[2]
}
type HTMLExtractor struct{}
func (HTMLExtractor) Extract(rawHTML string) (string, string, string, string, error) {
reader := strings.NewReader(rawHTML)
document, err := goquery.NewDocumentFromReader(reader)
if err != nil {
return "", "", "", "", err
}
doctype, err := extractDoctype(rawHTML)
if err != nil {
return "", "", "", "", err
}
head, err := extractHead(document)
if err != nil {
return "", "", "", "", err
}
bodyAttributes := ""
for _, attribute := range document.Find("body").Nodes[0].Attr {
bodyAttributes += " " + attribute.Key + `="` + attribute.Val + `"`
}
bodyAttributes = strings.TrimPrefix(bodyAttributes, " ")
bodyContent, err := document.Find("body").Html()
if err != nil {
return "", "", "", "", err
}
return doctype, head, bodyContent, bodyAttributes, nil
}
func extractDoctype(rawHTML string) (string, error) {
r, err := regexp.Compile("<!DOCTYPE[^>]*>")
if err != nil {
return "", err
}
return r.FindString(rawHTML), nil
}
func extractHead(document *goquery.Document) (string, error) {
htmlHead, err := document.Find("head").Html()
if err != nil {
return "", err
}
if htmlHead == "" {
return "", nil
}
return htmlHead, nil
}