Making slight progress on EML parsing

This commit is contained in:
Winni Neessen 2023-09-21 13:50:36 +02:00
parent d8d2a6e714
commit d733b6e17d
Signed by: wneessen
GPG key ID: 385AC9889632126E

42
eml.go
View file

@ -3,6 +3,7 @@ package mail
import ( import (
"errors" "errors"
"fmt" "fmt"
"mime"
nm "net/mail" nm "net/mail"
"os" "os"
) )
@ -22,10 +23,19 @@ func EMLToMsg(fp string) (*Msg, error) {
return m, fmt.Errorf("failed to parse EML file: %w", err) return m, fmt.Errorf("failed to parse EML file: %w", err)
} }
// Parse the header
if err := parseEMLHeaders(&pm.Header, m); err != nil { if err := parseEMLHeaders(&pm.Header, m); err != nil {
return m, fmt.Errorf("failed to parse EML headers: %w", err) return m, fmt.Errorf("failed to parse EML headers: %w", err)
} }
// Extract the transfer encoding of the body
x, y, err := mime.ParseMediaType(pm.Header.Get(HeaderContentType.String()))
if err != nil {
return m, fmt.Errorf("failed to extract content type: %w", err)
}
fmt.Printf("Encoding: %s\n", x)
fmt.Printf("Params: %+v\n", y)
return m, nil return m, nil
} }
@ -45,6 +55,8 @@ func readEML(fp string) (*nm.Message, error) {
return pm, nil return pm, nil
} }
// parseEMLHeaders will check the EML headers for the most common headers and set the
// according settings in the Msg
func parseEMLHeaders(mh *nm.Header, m *Msg) error { func parseEMLHeaders(mh *nm.Header, m *Msg) error {
commonHeaders := []Header{ commonHeaders := []Header{
HeaderContentType, HeaderImportance, HeaderInReplyTo, HeaderListUnsubscribe, HeaderContentType, HeaderImportance, HeaderInReplyTo, HeaderListUnsubscribe,
@ -59,17 +71,24 @@ func parseEMLHeaders(mh *nm.Header, m *Msg) error {
return fmt.Errorf(`failed to parse "From:" header: %w`, err) return fmt.Errorf(`failed to parse "From:" header: %w`, err)
} }
} }
if v := mh.Get(HeaderTo.String()); v != "" { ahl := map[AddrHeader]func(...string) error{
var als []string HeaderTo: m.To,
pal, err := nm.ParseAddressList(v) HeaderCc: m.Cc,
if err != nil { HeaderBcc: m.Bcc,
return fmt.Errorf(`failed to parse address list: %w`, err) }
} for h, f := range ahl {
for _, a := range pal { if v := mh.Get(h.String()); v != "" {
als = append(als, a.String()) var als []string
} pal, err := nm.ParseAddressList(v)
if err := m.To(als...); err != nil { if err != nil {
return fmt.Errorf(`failed to parse "To:" header: %w`, err) return fmt.Errorf(`failed to parse address list: %w`, err)
}
for _, a := range pal {
als = append(als, a.String())
}
if err := f(als...); err != nil {
return fmt.Errorf(`failed to parse "To:" header: %w`, err)
}
} }
} }
@ -93,5 +112,6 @@ func parseEMLHeaders(mh *nm.Header, m *Msg) error {
m.SetGenHeader(h, v) m.SetGenHeader(h, v)
} }
} }
return nil return nil
} }