From 036990f946bd6e79c428cb83fde5b08301a3fee3 Mon Sep 17 00:00:00 2001 From: Alexander Weinhold Date: Wed, 16 Aug 2017 17:04:56 +0200 Subject: [PATCH] don't print attachments that are text/plain --- imap/imap.go | 2 +- libUnreadMail.go | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/imap/imap.go b/imap/imap.go index 3f7da80..a2e83e1 100644 --- a/imap/imap.go +++ b/imap/imap.go @@ -119,7 +119,7 @@ func fetch(c *connection, ids []int) stateFunc { c.write(cmd(fmt.Sprintf("uid fetch %d body[]", id))) if c.err == nil { s := c.read("uid fetch of " + strconv.Itoa(id)) - s = strings.TrimRight(s, ")\n") + s = strings.TrimRight(s, ")\r\n") if c.err == nil { mail, err := parseMail(s) if err == nil { diff --git a/libUnreadMail.go b/libUnreadMail.go index d551911..e6593d3 100644 --- a/libUnreadMail.go +++ b/libUnreadMail.go @@ -93,7 +93,8 @@ func getPlainParts(mail *mail.Message) ([]string, []error) { parts := make([]string, 0) errs := make([]error, 0) mediaType, params, err := mime.ParseMediaType(mail.Header.Get("Content-Type")) - if err == nil && strings.HasPrefix(mediaType, "multipart/") { + _, hasBoundary := params["boundary"] + if err == nil && strings.HasPrefix(mediaType, "multipart/") && hasBoundary { mr := multipart.NewReader(mail.Body, params["boundary"]) for { p, err := mr.NextPart() @@ -104,16 +105,23 @@ func getPlainParts(mail *mail.Message) ([]string, []error) { errs = append(errs, err) } if contentType := p.Header.Get("Content-Type"); strings.HasPrefix(contentType, "text/plain") { - 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) + 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) } } - parts = append(parts, string(body)) } else { parts = append(parts, "Non-plain part: "+contentType) }