From cff4476f3bb61510acefd567deb39f58351de215 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Sun, 17 May 2020 12:08:17 +0200 Subject: [PATCH] msg/reply: fix encoding issues for quoted reply. --- commands/msg/recall.go | 5 +++-- commands/msg/reply.go | 40 ++++++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/commands/msg/recall.go b/commands/msg/recall.go index ef7e859..7c9ac19 100644 --- a/commands/msg/recall.go +++ b/commands/msg/recall.go @@ -107,9 +107,10 @@ func (Recall) Execute(aerc *widgets.Aerc, args []string) error { part *models.BodyStructure ) if len(msgInfo.BodyStructure.Parts) != 0 { - part, path = findPlaintext(msgInfo.BodyStructure, path) + path = findPlaintext(msgInfo.BodyStructure, path) } - if part == nil { + part, err = msgInfo.BodyStructure.PartAtIndex(path) + if part == nil || err != nil { part = msgInfo.BodyStructure path = []int{1} } diff --git a/commands/msg/reply.go b/commands/msg/reply.go index 72c992e..e4c4577 100644 --- a/commands/msg/reply.go +++ b/commands/msg/reply.go @@ -165,7 +165,16 @@ func (reply) Execute(aerc *widgets.Aerc, args []string) error { template = aerc.Config().Templates.QuotedReply } - store.FetchBodyPart(msg.Uid, []int{1}, func(reader io.Reader) { + part := findPlaintext(msg.BodyStructure, nil) + if part == nil { + //mkey... let's get the first thing that isn't a container + part := findFirstNonMultipart(msg.BodyStructure, nil) + if part == nil { + // give up, use whatever is first + part = []int{1} + } + } + store.FetchBodyPart(msg.Uid, part, func(reader io.Reader) { buf := new(bytes.Buffer) buf.ReadFrom(reader) original.Text = buf.String() @@ -188,22 +197,33 @@ func (reply) Execute(aerc *widgets.Aerc, args []string) error { } } -//TODO (RPB): unused function -func findPlaintext(bs *models.BodyStructure, - path []int) (*models.BodyStructure, []int) { - +func findPlaintext(bs *models.BodyStructure, path []int) []int { for i, part := range bs.Parts { cur := append(path, i+1) if strings.ToLower(part.MIMEType) == "text" && strings.ToLower(part.MIMESubType) == "plain" { - return part, cur + return cur } if strings.ToLower(part.MIMEType) == "multipart" { - if part, path := findPlaintext(part, cur); path != nil { - return part, path + if path := findPlaintext(part, cur); path != nil { + return path } } } - - return nil, nil + return nil +} + +func findFirstNonMultipart(bs *models.BodyStructure, path []int) []int { + for i, part := range bs.Parts { + cur := append(path, i+1) + mimetype := strings.ToLower(part.MIMEType) + if mimetype != "multipart" { + return path + } else if mimetype == "multipart" { + if path := findPlaintext(part, cur); path != nil { + return path + } + } + } + return nil }