1package smtp
2
3import (
4 "strconv"
5 "strings"
6
7 "github.com/mjl-/mox/dns"
8)
9
10// Path is an SMTP forward/reverse path, as used in MAIL FROM and RCPT TO
11// commands.
12type Path struct {
13 Localpart Localpart
14 IPDomain dns.IPDomain
15}
16
17func (p Path) IsZero() bool {
18 return p.Localpart == "" && p.IPDomain.IsZero()
19}
20
21// String returns a string representation with ASCII-only domain name.
22func (p Path) String() string {
23 return p.XString(false)
24}
25
26// LogString returns both the ASCII-only and optional UTF-8 representation.
27func (p Path) LogString() string {
28 if p.Localpart == "" && p.IPDomain.IsZero() {
29 return ""
30 }
31 s := p.XString(true)
32 lp := p.Localpart.String()
33 qlp := strconv.QuoteToASCII(lp)
34 escaped := qlp != `"`+lp+`"`
35 if p.IPDomain.Domain.Unicode != "" || escaped {
36 if escaped {
37 lp = qlp
38 }
39 s += "/" + lp + "@" + p.IPDomain.XString(false)
40 }
41 return s
42}
43
44// XString is like String, but returns unicode UTF-8 domain names if utf8 is
45// true.
46func (p Path) XString(utf8 bool) string {
47 if p.Localpart == "" && p.IPDomain.IsZero() {
48 return ""
49 }
50 return p.Localpart.String() + "@" + p.IPDomain.XString(utf8)
51}
52
53// ASCIIExtra returns an ascii-only path if utf8 is true and the ipdomain is a
54// unicode domain. Otherwise returns an empty string.
55//
56// For use in comments in message headers added during SMTP.
57func (p Path) ASCIIExtra(utf8 bool) string {
58 if utf8 && p.IPDomain.Domain.Unicode != "" {
59 return p.XString(false)
60 }
61 return ""
62}
63
64// DSNString returns a string representation as used with DSN with/without
65// UTF-8 support.
66//
67// If utf8 is false, the domain is represented as US-ASCII (IDNA), and the
68// localpart is encoded with in 7bit according to RFC 6533.
69func (p Path) DSNString(utf8 bool) string {
70 if utf8 {
71 return p.XString(utf8)
72 }
73 return p.Localpart.DSNString(utf8) + "@" + p.IPDomain.XString(utf8)
74}
75
76func (p Path) Equal(o Path) bool {
77 if p.Localpart != o.Localpart {
78 return false
79 }
80 d0 := p.IPDomain
81 d1 := o.IPDomain
82 if len(d0.IP) > 0 || len(d1.IP) > 0 {
83 return d0.IP.Equal(d1.IP)
84 }
85 return strings.EqualFold(d0.Domain.ASCII, d1.Domain.ASCII)
86}
87