don't print attachments that are text/plain

This commit is contained in:
Alexander Weinhold 2017-08-16 17:04:56 +02:00
parent e816fe2f60
commit 036990f946
2 changed files with 18 additions and 10 deletions

View File

@ -119,7 +119,7 @@ func fetch(c *connection, ids []int) stateFunc {
c.write(cmd(fmt.Sprintf("uid fetch %d body[]", id))) c.write(cmd(fmt.Sprintf("uid fetch %d body[]", id)))
if c.err == nil { if c.err == nil {
s := c.read("uid fetch of " + strconv.Itoa(id)) s := c.read("uid fetch of " + strconv.Itoa(id))
s = strings.TrimRight(s, ")\n") s = strings.TrimRight(s, ")\r\n")
if c.err == nil { if c.err == nil {
mail, err := parseMail(s) mail, err := parseMail(s)
if err == nil { if err == nil {

View File

@ -93,7 +93,8 @@ func getPlainParts(mail *mail.Message) ([]string, []error) {
parts := make([]string, 0) parts := make([]string, 0)
errs := make([]error, 0) errs := make([]error, 0)
mediaType, params, err := mime.ParseMediaType(mail.Header.Get("Content-Type")) 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"]) mr := multipart.NewReader(mail.Body, params["boundary"])
for { for {
p, err := mr.NextPart() p, err := mr.NextPart()
@ -104,6 +105,7 @@ func getPlainParts(mail *mail.Message) ([]string, []error) {
errs = append(errs, err) errs = append(errs, err)
} }
if contentType := p.Header.Get("Content-Type"); strings.HasPrefix(contentType, "text/plain") { 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) body, _ := ioutil.ReadAll(p)
encoding := p.Header.Get("Content-Transfer-Encoding") encoding := p.Header.Get("Content-Transfer-Encoding")
if encoding != "" { if encoding != "" {
@ -114,6 +116,12 @@ func getPlainParts(mail *mail.Message) ([]string, []error) {
} }
} }
parts = append(parts, string(body)) 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)
}
}
} else { } else {
parts = append(parts, "Non-plain part: "+contentType) parts = append(parts, "Non-plain part: "+contentType)
} }