mirror of
https://github.com/foxcpp/maddy.git
synced 2025-04-06 14:37:37 +03:00
Root package now contains only initialization code and 'dummy' module. Each module now got its own package. Module packages are grouped by their main purpose (storage/, target/, auth/, etc). Shared code is placed in these "group" packages. Parser for module references in config is moved into config/module. Code shared by tests (mock modules, etc) is placed in testutils.
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package address
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
/*
|
|
Rules for validation are subset of rules listed here:
|
|
https://emailregex.com/email-validation-summary/
|
|
*/
|
|
|
|
// Valid checks whether ths string is valid as a email address.
|
|
func Valid(addr string) bool {
|
|
if len(addr) > 320 { // RFC 3696 says it's 320, not 255.
|
|
return false
|
|
}
|
|
parts := strings.Split(addr, "@")
|
|
switch len(parts) {
|
|
case 1:
|
|
return strings.EqualFold(addr, "postmaster")
|
|
case 2:
|
|
return ValidMailboxName(parts[0]) && ValidDomain(parts[1])
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// ValidMailboxName checks whether the specified string is a valid mailbox-name
|
|
// element of e-mail address (left part of it, before at-sign).
|
|
func ValidMailboxName(mbox string) bool {
|
|
return true // TODO
|
|
}
|
|
|
|
// ValidDomain checks whether the specified string is a valid DNS domain.
|
|
func ValidDomain(domain string) bool {
|
|
if len(domain) > 255 {
|
|
return false
|
|
}
|
|
if strings.HasPrefix(domain, ".") || strings.HasSuffix(domain, ".") {
|
|
return false
|
|
}
|
|
if strings.Contains(domain, "..") {
|
|
return false
|
|
}
|
|
if strings.Contains(domain, "--") {
|
|
return false
|
|
}
|
|
|
|
labels := strings.Split(domain, ".")
|
|
for _, label := range labels {
|
|
if len(label) > 64 {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|