2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
2018-08-13 01:08:48 +08:00
|
|
|
"strings"
|
2018-05-27 22:40:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var prePlusMatch = regexp.MustCompile(`([^@\+]*)\+?(.*)@.*`)
|
|
|
|
var periodsMatch = regexp.MustCompile(`[\.]`)
|
|
|
|
var postAtMatch = regexp.MustCompile(`[^@]*(@.*)`)
|
|
|
|
|
2018-07-24 15:00:45 +08:00
|
|
|
func emailStrip(email string) string {
|
2018-05-27 22:40:42 +08:00
|
|
|
postAt := postAtMatch.ReplaceAllString(email, `$1`)
|
|
|
|
prePlus := prePlusMatch.ReplaceAllString(email, `$1`)
|
|
|
|
strippedEmail := periodsMatch.ReplaceAllString(prePlus, ``) + postAt
|
|
|
|
|
|
|
|
return strippedEmail
|
|
|
|
}
|
|
|
|
|
|
|
|
var https = regexp.MustCompile(`(https?://)`)
|
2019-11-21 17:09:21 +08:00
|
|
|
var domainTrail = regexp.MustCompile(`(/.*$)`)
|
2018-05-27 22:40:42 +08:00
|
|
|
|
2018-07-24 15:00:45 +08:00
|
|
|
func domainStrip(domain string) string {
|
2019-11-21 17:09:21 +08:00
|
|
|
noProtocol := https.ReplaceAllString(domain, ``)
|
|
|
|
noTrail := domainTrail.ReplaceAllString(noProtocol, ``)
|
2018-05-27 22:40:42 +08:00
|
|
|
|
2019-11-21 17:09:21 +08:00
|
|
|
return noTrail
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
2018-06-03 22:49:36 +08:00
|
|
|
var pathMatch = regexp.MustCompile(`(https?://[^/]*)`)
|
2018-05-27 22:40:42 +08:00
|
|
|
|
2018-07-24 15:00:45 +08:00
|
|
|
func pathStrip(url string) string {
|
2018-06-03 22:49:36 +08:00
|
|
|
strippedPath := pathMatch.ReplaceAllString(url, ``)
|
2018-05-27 22:40:42 +08:00
|
|
|
|
|
|
|
return strippedPath
|
|
|
|
}
|
2018-06-11 01:43:18 +08:00
|
|
|
|
|
|
|
var httpsUrl = regexp.MustCompile(`^https?://`)
|
|
|
|
|
|
|
|
func isHttpsUrl(in string) bool {
|
|
|
|
// Admittedly, this isn't the greatest URL checker. But it does what we need.
|
|
|
|
// I don't care if the user gives an invalid URL, I just want to make sure
|
|
|
|
// they don't do any XSS shenanigans. Hopefully, enforcing a https?:// prefix
|
|
|
|
// solves this. If this function returns false, prefix with "http://"
|
|
|
|
return len(httpsUrl.FindAllString(in, -1)) != 0
|
|
|
|
}
|
2018-08-13 01:08:48 +08:00
|
|
|
|
|
|
|
func addHttpIfAbsent(in string) string {
|
|
|
|
if !strings.HasPrefix(in, "http://") && !strings.HasPrefix(in, "https://") {
|
|
|
|
return "http://" + in
|
|
|
|
}
|
|
|
|
|
|
|
|
return in
|
|
|
|
}
|