From 89642c258e4b11ec9e7e3e76d6bf289dc2ca3084 Mon Sep 17 00:00:00 2001 From: Alexander Weinhold Date: Wed, 16 Aug 2017 18:17:26 +0200 Subject: [PATCH] reordering for better structure --- libUnreadMail.go | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/libUnreadMail.go b/libUnreadMail.go index e6593d3..a98801c 100644 --- a/libUnreadMail.go +++ b/libUnreadMail.go @@ -89,6 +89,15 @@ func decodeSubject(subject string) (string, error) { return dec.DecodeHeader(subject) } +func attachmentName(contentDisposition string) string { + _, params, err := mime.ParseMediaType(contentDisposition) + if filename, ok := params["filename"]; err == nil && ok { + return filename + } else { + return "" + } +} + func getPlainParts(mail *mail.Message) ([]string, []error) { parts := make([]string, 0) errs := make([]error, 0) @@ -104,26 +113,23 @@ func getPlainParts(mail *mail.Message) ([]string, []error) { if err != nil { errs = append(errs, err) } - if contentType := p.Header.Get("Content-Type"); strings.HasPrefix(contentType, "text/plain") { - if contentDisposition := p.Header.Get("Content-Disposition"); !strings.HasPrefix(contentDisposition, "attachment") { - body, _ := ioutil.ReadAll(p) - encoding := p.Header.Get("Content-Transfer-Encoding") - if encoding != "" { - body, err = normalize(body, encoding) - if err != nil { - body = nil - errs = append(errs, err) - } - } - parts = append(parts, string(body)) - } else { - _, params, errDispo := mime.ParseMediaType(contentDisposition) - if filename, ok := params["filename"]; errDispo == nil && ok { - parts = append(parts, "Plain attachment: "+filename) - } + if contentType := p.Header.Get("Content-Type"); !strings.HasPrefix(contentType, "text/plain") { + parts = append(parts, "Non-plain part: "+contentType) + } else if contentDisposition := p.Header.Get("Content-Disposition"); strings.HasPrefix(contentDisposition, "attachment") { + if filename := attachmentName(contentDisposition); filename != "" { + parts = append(parts, "Plain attachment: "+filename) } } else { - parts = append(parts, "Non-plain part: "+contentType) + body, _ := ioutil.ReadAll(p) + encoding := p.Header.Get("Content-Transfer-Encoding") + if encoding != "" { + body, err = normalize(body, encoding) + if err != nil { + body = nil + errs = append(errs, err) + } + } + parts = append(parts, string(body)) } } } else {