/
path.go
86 lines (76 loc) · 2.05 KB
/
path.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
package smtp
import (
"strconv"
"strings"
"github.com/mjl-/mox/dns"
)
// Path is an SMTP forward/reverse path, as used in MAIL FROM and RCPT TO
// commands.
type Path struct {
Localpart Localpart
IPDomain dns.IPDomain
}
func (p Path) IsZero() bool {
return p.Localpart == "" && p.IPDomain.IsZero()
}
// String returns a string representation with ASCII-only domain name.
func (p Path) String() string {
return p.XString(false)
}
// LogString returns both the ASCII-only and optional UTF-8 representation.
func (p Path) LogString() string {
if p.Localpart == "" && p.IPDomain.IsZero() {
return ""
}
s := p.XString(true)
lp := p.Localpart.String()
qlp := strconv.QuoteToASCII(lp)
escaped := qlp != `"`+lp+`"`
if p.IPDomain.Domain.Unicode != "" || escaped {
if escaped {
lp = qlp
}
s += "/" + lp + "@" + p.IPDomain.XString(false)
}
return s
}
// XString is like String, but returns unicode UTF-8 domain names if utf8 is
// true.
func (p Path) XString(utf8 bool) string {
if p.Localpart == "" && p.IPDomain.IsZero() {
return ""
}
return p.Localpart.String() + "@" + p.IPDomain.XString(utf8)
}
// ASCIIExtra returns an ascii-only path if utf8 is true and the ipdomain is a
// unicode domain. Otherwise returns an empty string.
//
// For use in comments in message headers added during SMTP.
func (p Path) ASCIIExtra(utf8 bool) string {
if utf8 && p.IPDomain.Domain.Unicode != "" {
return p.XString(false)
}
return ""
}
// DSNString returns a string representation as used with DSN with/without
// UTF-8 support.
//
// If utf8 is false, the domain is represented as US-ASCII (IDNA), and the
// localpart is encoded with in 7bit according to RFC 6533.
func (p Path) DSNString(utf8 bool) string {
if utf8 {
return p.XString(utf8)
}
return p.Localpart.DSNString(utf8) + "@" + p.IPDomain.XString(utf8)
}
func (p Path) Equal(o Path) bool {
if p.Localpart != o.Localpart {
return false
}
d0 := p.IPDomain
d1 := o.IPDomain
if len(d0.IP) > 0 || len(d1.IP) > 0 {
return d0.IP.Equal(d1.IP)
}
return strings.EqualFold(d0.Domain.ASCII, d1.Domain.ASCII)
}