Skip to content

Commit

Permalink
feat(GODT-2201): RFC5322 AddressList parser rewritten in Go
Browse files Browse the repository at this point in the history
  • Loading branch information
LBeernaertProton committed Feb 21, 2023
1 parent 09ab705 commit a6bbc98
Show file tree
Hide file tree
Showing 16 changed files with 2,622 additions and 19 deletions.
25 changes: 11 additions & 14 deletions imap/envelope.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package imap

import (
"github.com/ProtonMail/gluon/rfc5322"
"net/mail"
"strings"

"github.com/ProtonMail/gluon/internal/parser"
"github.com/ProtonMail/gluon/rfc822"
"github.com/sirupsen/logrus"
)
Expand All @@ -29,47 +29,44 @@ func envelope(header *rfc822.Header, c *paramList, writer parListWriter) error {
addString(writer, header.Get("Date")).
addString(writer, header.Get("Subject"))

addressParser := parser.NewRFC5322AddressListParser()
defer addressParser.Close()

if v, ok := header.GetChecked("From"); !ok {
fields.addString(writer, "")
} else {
fields.addAddresses(writer, tryParseAddressList(addressParser, v))
fields.addAddresses(writer, tryParseAddressList(v))
}

if v, ok := header.GetChecked("Sender"); ok {
fields.addAddresses(writer, tryParseAddressList(addressParser, v))
fields.addAddresses(writer, tryParseAddressList(v))
} else if v, ok := header.GetChecked("From"); ok {
fields.addAddresses(writer, tryParseAddressList(addressParser, v))
fields.addAddresses(writer, tryParseAddressList(v))
} else {
fields.addString(writer, "")
}

if v, ok := header.GetChecked("Reply-To"); ok {
fields.addAddresses(writer, tryParseAddressList(addressParser, v))
fields.addAddresses(writer, tryParseAddressList(v))
} else if v, ok := header.GetChecked("From"); ok {
fields.addAddresses(writer, tryParseAddressList(addressParser, v))
fields.addAddresses(writer, tryParseAddressList(v))
} else {
fields.addString(writer, "")
}

if v, ok := header.GetChecked("To"); !ok {
fields.addString(writer, "")
} else {
fields.addAddresses(writer, tryParseAddressList(addressParser, v))
fields.addAddresses(writer, tryParseAddressList(v))
}

if v, ok := header.GetChecked("Cc"); !ok {
fields.addString(writer, "")
} else {
fields.addAddresses(writer, tryParseAddressList(addressParser, v))
fields.addAddresses(writer, tryParseAddressList(v))
}

if v, ok := header.GetChecked("Bcc"); !ok {
fields.addString(writer, "")
} else {
fields.addAddresses(writer, tryParseAddressList(addressParser, v))
fields.addAddresses(writer, tryParseAddressList(v))
}

fields.addString(writer, header.Get("In-Reply-To"))
Expand All @@ -79,8 +76,8 @@ func envelope(header *rfc822.Header, c *paramList, writer parListWriter) error {
return nil
}

func tryParseAddressList(parser *parser.RFC5322AddressListParser, val string) []*mail.Address {
addr, err := parser.Parse(val)
func tryParseAddressList(val string) []*mail.Address {
addr, err := rfc5322.ParseAddressList(val)
if err != nil {
logrus.WithError(err).Error("Failed to parse address")
return []*mail.Address{{Name: val}}
Expand Down
Loading

0 comments on commit a6bbc98

Please sign in to comment.