maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

 1/*
 2Maddy Mail Server - Composable all-in-one email server.
 3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
 4
 5This program is free software: you can redistribute it and/or modify
 6it under the terms of the GNU General Public License as published by
 7the Free Software Foundation, either version 3 of the License, or
 8(at your option) any later version.
 9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program.  If not, see <https://www.gnu.org/licenses/>.
17*/
18
19package address
20
21import (
22	"errors"
23
24	"golang.org/x/net/idna"
25	"golang.org/x/text/unicode/norm"
26)
27
28var ErrUnicodeMailbox = errors.New("address: cannot convert the Unicode local-part to the ACE form")
29
30// ToASCII converts the domain part of the email address to the A-label form and
31// fails with ErrUnicodeMailbox if the local-part contains non-ASCII characters.
32func ToASCII(addr string) (string, error) {
33	mbox, domain, err := Split(addr)
34	if err != nil {
35		return addr, err
36	}
37
38	for _, ch := range mbox {
39		if ch > 128 {
40			return addr, ErrUnicodeMailbox
41		}
42	}
43
44	if domain == "" {
45		return mbox, nil
46	}
47
48	aDomain, err := idna.ToASCII(domain)
49	if err != nil {
50		return addr, err
51	}
52
53	return mbox + "@" + aDomain, nil
54}
55
56// ToUnicode converts the domain part of the email address to the U-label form.
57func ToUnicode(addr string) (string, error) {
58	mbox, domain, err := Split(addr)
59	if err != nil {
60		return norm.NFC.String(addr), err
61	}
62
63	if domain == "" {
64		return mbox, nil
65	}
66
67	uDomain, err := idna.ToUnicode(domain)
68	if err != nil {
69		return norm.NFC.String(addr), err
70	}
71
72	return mbox + "@" + norm.NFC.String(uDomain), nil
73}
74
75// SelectIDNA is a convenience function for conversion of domains in the email
76// addresses to/from the Punycode form.
77//
78// ulabel=true => ToUnicode is used.
79// ulabel=false => ToASCII is used.
80func SelectIDNA(ulabel bool, addr string) (string, error) {
81	if ulabel {
82		return ToUnicode(addr)
83	}
84	return ToASCII(addr)
85}