diff --git a/message/addr.go b/message/addr.go new file mode 100644 index 0000000..e551ae2 --- /dev/null +++ b/message/addr.go @@ -0,0 +1,29 @@ +package message + +import ( + "fmt" + "net/mail" + + "github.com/mjl-/mox/smtp" +) + +// ParseAddressList parses a string as an address list header value +// (potentially multiple addresses, comma-separated, with optional display +// name). +func ParseAddressList(s string) ([]Address, error) { + parser := mail.AddressParser{WordDecoder: &wordDecoder} + addrs, err := parser.ParseList(s) + if err != nil { + return nil, fmt.Errorf("parsing address list: %v", err) + } + r := make([]Address, len(addrs)) + for i, a := range addrs { + addr, err := smtp.ParseNetMailAddress(a.Address) + if err != nil { + return nil, fmt.Errorf("parsing adjusted address %q: %v", a.Address, err) + } + r[i] = Address{a.Name, addr.Localpart.String(), addr.Domain.ASCII} + + } + return r, nil +} diff --git a/message/addr_test.go b/message/addr_test.go new file mode 100644 index 0000000..1d00bbb --- /dev/null +++ b/message/addr_test.go @@ -0,0 +1,11 @@ +package message + +import ( + "testing" +) + +func TestParseAddressList(t *testing.T) { + l, err := ParseAddressList("=?iso-8859-2?Q?Krist=FDna?= , mjl@mox.example") + tcheck(t, err, "parsing address list") + tcompare(t, l, []Address{{"Kristýna", "k", "example.com"}, {"", "mjl", "mox.example"}}) +} diff --git a/message/part.go b/message/part.go index 00d2a91..21b3f47 100644 --- a/message/part.go +++ b/message/part.go @@ -489,7 +489,12 @@ func parseEnvelope(log mlog.Log, h mail.Header) (*Envelope, error) { func parseAddressList(log mlog.Log, h mail.Header, k string) []Address { // todo: possibly work around ios mail generating incorrect q-encoded "phrases" with unencoded double quotes? ../rfc/2047:382 - l, err := h.AddressList(k) + v := h.Get(k) + if v == "" { + return nil + } + parser := mail.AddressParser{WordDecoder: &wordDecoder} + l, err := parser.ParseList(v) if err != nil { return nil } diff --git a/message/part_test.go b/message/part_test.go index 4a42e3d..e8995ee 100644 --- a/message/part_test.go +++ b/message/part_test.go @@ -603,3 +603,10 @@ func TestNetMailAddress(t *testing.T) { tcheck(t, err, "parse") tcompare(t, p.Envelope.From, []Address{{"", `" "`, "example.com"}}) } + +func TestParseQuotedCharset(t *testing.T) { + const s = "From: =?iso-8859-2?Q?Krist=FDna?= \r\n\r\nbody\r\n" + p, err := EnsurePart(pkglog.Logger, false, strings.NewReader(s), int64(len(s))) + tcheck(t, err, "parse") + tcompare(t, p.Envelope.From, []Address{{"Kristýna", "k", "example.com"}}) +} diff --git a/sendmail.go b/sendmail.go index 06e8a08..041e71a 100644 --- a/sendmail.go +++ b/sendmail.go @@ -9,7 +9,6 @@ import ( "io" "log" "net" - "net/mail" "os" "path/filepath" "slices" @@ -19,6 +18,7 @@ import ( "github.com/mjl-/sconf" "github.com/mjl-/mox/dns" + "github.com/mjl-/mox/message" "github.com/mjl-/mox/mox-" "github.com/mjl-/mox/sasl" "github.com/mjl-/mox/smtp" @@ -196,16 +196,12 @@ binary should be setgid that group: } recipient = submitconf.DefaultDestination } else { - addrs, err := mail.ParseAddressList(s) + addrs, err := message.ParseAddressList(s) xcheckf(err, "parsing To address list") if len(addrs) != 1 { log.Fatalf("only single address allowed in To header") } - addr, err := smtp.ParseNetMailAddress(addrs[0].Address) - if err != nil { - log.Fatalf("parsing address: %v", err) - } - recipient = addr.Pack(false) + recipient = addrs[0].User + "@" + addrs[0].Host } } if k == "to" { diff --git a/webmail/api.go b/webmail/api.go index 8705109..c23a83b 100644 --- a/webmail/api.go +++ b/webmail/api.go @@ -517,13 +517,14 @@ type File struct { // parseAddress expects either a plain email address like "user@domain", or a // single address as used in a message header, like "name ". func parseAddress(msghdr string) (message.NameAddress, error) { - a, err := mail.ParseAddress(msghdr) + // todo: parse more fully according to ../rfc/5322:959 + parser := mail.AddressParser{WordDecoder: &wordDecoder} + a, err := parser.Parse(msghdr) if err != nil { return message.NameAddress{}, err } - // todo: parse more fully according to ../rfc/5322:959 - path, err := smtp.ParseAddress(a.Address) + path, err := smtp.ParseNetMailAddress(a.Address) if err != nil { return message.NameAddress{}, err } @@ -1658,12 +1659,12 @@ func recipientSecurity(ctx context.Context, log mlog.Log, resolver dns.Resolver, SecurityResultUnknown, } - msgAddr, err := mail.ParseAddress(messageAddressee) + parser := mail.AddressParser{WordDecoder: &wordDecoder} + msgAddr, err := parser.Parse(messageAddressee) if err != nil { - return rs, fmt.Errorf("parsing message addressee: %v", err) + return rs, fmt.Errorf("parsing addressee: %v", err) } - - addr, err := smtp.ParseAddress(msgAddr.Address) + addr, err := smtp.ParseNetMailAddress(msgAddr.Address) if err != nil { return rs, fmt.Errorf("parsing address: %v", err) }