-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
205 lines (168 loc) · 4.89 KB
/
parse.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
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
package mailaddress
import (
"errors"
"mime"
"regexp"
"strings"
"unicode"
"unicode/utf8"
"github.com/teamwork/toutf8"
)
var (
reSanitizeWhitespace = regexp.MustCompile(`\s+`)
reRemoveComment = regexp.MustCompile(`\s+\(.*?\)$`)
reFindEmail = regexp.MustCompile(`[^\s<>]+@[^\s<>]+\.[^\s<>]+`)
// Note: this is repeated in helpers/form.coffee
reValidEmail = regexp.MustCompile(`` +
// Anchor
`^` +
// Local part; allow almost everything
`[^\s<>@;]+` +
// @
`@` +
// Domain part
//
// See RFC 1034, section 3.1, RFC 1035, secion 2.3.1
//
// - Only allow letters, numbers
// - Max size of a single label is 63 characters (RFC specifies bytes, but that's
// not so easy to check AFAIK).
// - Need at least two labels
`[\p{L}\d-]{1,63}` + // Label
`(\.[\p{L}\d-]{1,63})+` + // More labels
// Anchor
`$`)
// ErrInvalidEncoding is used when we can't decode an address because the
// encoding is invalid (>95% of the time this means it's spam).
ErrInvalidEncoding = errors.New("invalid or incomplete multibyte or wide character")
// ErrNoEmail is used when we can't find an email address at all.
ErrNoEmail = errors.New("unable to find an email address")
// ErrTooManyEmails is used when too many email addresses were found.
ErrTooManyEmails = errors.New("only one address expected")
// ErrInvalidCharacter is used when unexpected data is encountered.
ErrInvalidCharacter = errors.New("invalid character")
)
func parse(str string) (list List, haveError bool) {
// Sanitize whitespace
str = reSanitizeWhitespace.ReplaceAllString(str, " ")
list = List{}
addr := Address{}
inAddress := false
inQuote := false
for i, code := range str {
chr := string(code)
switch {
case code == utf8.RuneError:
addr.Raw += chr
addr.err = ErrInvalidEncoding
haveError = true
// Don't allow unprintable characters.
case code < 0x09 || (code >= 0x0b && code < 0x20):
addr.Raw += chr
addr.err = ErrInvalidCharacter
haveError = true
case chr == `\`:
// Ignore
addr.Raw += `\`
// Quote
// TODO: support quoting the local part too.
case chr == `"`:
addr.Raw += chr
// Escaped
if inQuote && i > 0 && str[i-1] == '\\' {
if inAddress {
addr.Address += chr
} else {
addr.Name += chr
}
continue
}
inQuote = !inQuote
// Start <angl-addr>
case !inQuote && chr == "<":
addr.Raw += "<"
inAddress = true
// End <angl-addr>
case !inQuote && chr == ">":
addr.Raw += ">"
// we've observed name including `<>`
if i < len(str)-1 && !addr.Valid() {
addr.Name += " " + addr.Address
addr.Address = ""
addr.err = nil // valid is not idempoent
}
inAddress = false
// Next <address>
case !inQuote && (chr == "," || chr == ";" || inAddress && unicode.IsSpace(code)): // ';' introduced by outlook
haveError = end(&addr) || haveError
if addr.Name != "" || addr.Address != "" || addr.err != nil {
list = append(list, addr)
}
addr = Address{}
// We've seen <angl-addr> but more data :-/
case !inQuote && !inAddress && addr.Address != "" && !unicode.IsSpace(code):
// Set error and read over it.
if addr.err == nil {
addr.err = ErrInvalidCharacter
haveError = true
}
// Append to address.
case inAddress:
addr.Raw += chr
addr.Address += chr
// Append to name.
default:
addr.Raw += chr
addr.Name += chr
}
}
haveError = end(&addr) || haveError
if addr.Name != "" || addr.Address != "" || addr.err != nil {
list = append(list, addr)
}
return list, haveError
}
func end(a *Address) (goterror bool) {
a.Name = strings.TrimSpace(a.Name)
a.Raw = strings.TrimSpace(a.Raw)
// remove single quotes if they are only around the name
if len(a.Name) > 2 && !strings.Contains(a.Name[1:len(a.Name)-1], "'") {
a.Name = strings.Trim(a.Name, "'")
}
// Remove any RFC 2047 encoding. Any encoded word is a single <atom>
// (i.e. characters such as comma, <, ", etc. don't get interpreted in
// their special meaning), so this is why we do this here.
decoder := mime.WordDecoder{CharsetReader: toutf8.Reader}
decoded, err := decoder.DecodeHeader(a.Name)
if err != nil {
a.err = err
a.Name = ""
return true
}
a.Name = decoded
// It was just an <addr-spec> and not a <angle-addr> or <name-addr>.
if a.Address == "" && a.Name != "" {
// Remove the "comment" part: "[email protected] (Mailer Daemon)".
a.Name = reRemoveComment.ReplaceAllString(a.Name, "")
// Technically "martin" is also a valid address (a local one) but this
// is not something people are going to send emails from.
mail := reFindEmail.FindString(a.Name)
if mail != "" {
a.Address = mail
if len(mail) != len(a.Name) {
a.err = ErrInvalidCharacter
goterror = true
}
} else {
a.err = ErrNoEmail
goterror = true
}
a.Name = ""
}
// Includes some sanity checks; it sets Error.
if a.Address != "" {
e := a.Valid()
goterror = goterror && e
}
return goterror
}