forked from flashmob/go-guerrilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
46 lines (40 loc) · 1.31 KB
/
util.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
package guerrilla
import (
"errors"
"regexp"
"strings"
"github.com/flashmob/go-guerrilla/mail"
"github.com/flashmob/go-guerrilla/response"
)
var extractEmailRegex, _ = regexp.Compile(`<(.+?)@(.+?)>`) // go home regex, you're drunk!
func extractEmail(str string) (mail.Address, error) {
email := mail.Address{}
var err error
if len(str) > RFC2821LimitPath {
return email, errors.New(response.Canned.FailPathTooLong)
}
if matched := extractEmailRegex.FindStringSubmatch(str); len(matched) > 2 {
email.User = matched[1]
email.Host = validHost(matched[2])
} else if res := strings.Split(str, "@"); len(res) > 1 {
email.User = strings.TrimSpace(res[0])
email.Host = validHost(res[1])
}
err = nil
if email.User == "" || email.Host == "" {
err = errors.New(response.Canned.FailInvalidAddress)
} else if len(email.User) > RFC2832LimitLocalPart {
err = errors.New(response.Canned.FailLocalPartTooLong)
} else if len(email.Host) > RFC2821LimitDomain {
err = errors.New(response.Canned.FailDomainTooLong)
}
return email, err
}
var validhostRegex, _ = regexp.Compile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`)
func validHost(host string) string {
host = strings.Trim(host, " ")
if validhostRegex.MatchString(host) {
return host
}
return ""
}