9// Extension is an additional key/value pair for a TLSRPT record.
10type Extension struct {
15// Record is a parsed TLSRPT record, to be served under "_smtp._tls.<domain>".
19// v=TLSRPTv1; rua=mailto:tlsrpt@mox.example;
21 Version string // "TLSRPTv1", for "v=".
23 // Aggregate reporting URI, for "rua=". "rua=" can occur multiple times, each can
28 Extensions []Extension
31// RUA is a reporting address with scheme and special characters ",", "!" and
35// String returns the RUA with special characters encoded, for inclusion in a
37func (rua RUA) String() string {
39 s = strings.ReplaceAll(s, ",", "%2C")
40 s = strings.ReplaceAll(s, "!", "%21")
41 s = strings.ReplaceAll(s, ";", "%3B")
45// URI parses a RUA as URI, with either a mailto or https scheme.
46func (rua RUA) URI() (*url.URL, error) {
47 return url.Parse(string(rua))
50// String returns a string or use as a TLSRPT DNS TXT record.
51func (r Record) String() string {
52 b := &strings.Builder{}
53 fmt.Fprint(b, "v="+r.Version)
54 for _, ruas := range r.RUAs {
55 l := make([]string, len(ruas))
56 for i, rua := range ruas {
59 fmt.Fprint(b, "; rua="+strings.Join(l, ","))
61 for _, p := range r.Extensions {
62 fmt.Fprint(b, "; "+p.Key+"="+p.Value)
69func (e parseErr) Error() string {
73var _ error = parseErr("")
75// ParseRecord parses a TLSRPT record.
76func ParseRecord(txt string) (record *Record, istlsrpt bool, err error) {
82 if xerr, ok := x.(parseErr); ok {
84 err = fmt.Errorf("%w: %s", ErrRecordSyntax, xerr)
102 // note: duplicates are allowed.
105 record.RUAs = append(record.RUAs, p.xruas())
108 record.Extensions = append(record.Extensions, Extension{k, v})
110 if !p.delim() || p.empty() {
115 p.xerrorf("leftover chars")
117 if record.RUAs == nil {
118 p.xerrorf("missing rua")
128func newParser(s string) *parser {
132func (p *parser) xerrorf(format string, args ...any) {
133 msg := fmt.Sprintf(format, args...)
135 msg += fmt.Sprintf(" (remain %q)", p.s[p.o:])
140func (p *parser) xtake(s string) string {
142 p.xerrorf("expected %q", s)
148func (p *parser) xdelim() {
150 p.xerrorf("expected semicolon")
154func (p *parser) xtaken(n int) string {
155 r := p.s[p.o : p.o+n]
160func (p *parser) prefix(s string) bool {
161 return strings.HasPrefix(p.s[p.o:], s)
164func (p *parser) take(s string) bool {
172func (p *parser) xtakefn1(fn func(rune, int) bool) string {
173 for i, b := range p.s[p.o:] {
176 p.xerrorf("expected at least one char")
182 p.xerrorf("expected at least 1 char")
184 return p.xtaken(len(p.s) - p.o)
188func (p *parser) xkey() string {
189 return p.xtakefn1(func(b rune, i int) bool {
190 return i < 32 && (b >= 'a' && b <= 'z' || b >= 'A' && b <= 'Z' || b >= '0' && b <= '9' || (i > 0 && b == '_' || b == '-' || b == '.'))
195func (p *parser) xvalue() string {
196 return p.xtakefn1(func(b rune, i int) bool {
197 return b > ' ' && b < 0x7f && b != '=' && b != ';'
202func (p *parser) delim() bool {
205 for o < e && (p.s[o] == ' ' || p.s[o] == '\t') {
208 if o >= e || p.s[o] != ';' {
212 for o < e && (p.s[o] == ' ' || p.s[o] == '\t') {
219func (p *parser) empty() bool {
220 return p.o >= len(p.s)
223func (p *parser) wsp() {
224 for p.o < len(p.s) && (p.s[p.o] == ' ' || p.s[p.o] == '\t') {
230func (p *parser) xruas() []RUA {
235 l = append(l, p.xuri())
242func (p *parser) xuri() RUA {
243 v := p.xtakefn1(func(b rune, i int) bool {
244 return b != ',' && b != '!' && b != ' ' && b != '\t' && b != ';'
246 u, err := url.Parse(v)
248 p.xerrorf("parsing uri %q: %s", v, err)
251 p.xerrorf("missing scheme in uri")