diff options
author | Drew DeVault <sir@cmpwn.com> | 2019-06-01 19:47:09 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2019-06-01 19:47:09 -0400 |
commit | cf50b987681d00a37f9457ddfa6f8bffb962af3b (patch) | |
tree | 396c91eff25f70c60874acffe78b8ce2fb423854 | |
parent | 56b84d3da5db01c97ec201390dadef2191dda7f4 (diff) | |
download | aerc-cf50b987681d00a37f9457ddfa6f8bffb962af3b.zip |
Fetch plaintext part when replying
-rw-r--r-- | commands/account/reply.go | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/commands/account/reply.go b/commands/account/reply.go index 0e43b88..b0bd346 100644 --- a/commands/account/reply.go +++ b/commands/account/reply.go @@ -180,14 +180,24 @@ func Reply(aerc *widgets.Aerc, args []string) error { }) } else { if quote { - // TODO: something more intelligent than fetching the 1st part - store.FetchBodyPart(msg.Uid, []int{1}, func(reader io.Reader) { + var ( + path []int + part *imap.BodyStructure + ) + if len(msg.BodyStructure.Parts) != 0 { + part, path = findPlaintext(msg.BodyStructure, path) + } + if part == nil { + part = msg.BodyStructure + path = []int{1} + } + + store.FetchBodyPart(msg.Uid, path, func(reader io.Reader) { header := message.Header{} header.SetText( - "Content-Transfer-Encoding", msg.BodyStructure.Encoding) - header.SetContentType( - msg.BodyStructure.MIMEType, msg.BodyStructure.Params) - header.SetText("Content-Description", msg.BodyStructure.Description) + "Content-Transfer-Encoding", part.Encoding) + header.SetContentType(part.MIMEType, part.Params) + header.SetText("Content-Description", part.Description) entity, err := message.New(header, reader) if err != nil { // TODO: Do something with the error @@ -223,3 +233,21 @@ func Reply(aerc *widgets.Aerc, args []string) error { return nil } + +func findPlaintext(bs *imap.BodyStructure, + path []int) (*imap.BodyStructure, []int) { + + for i, part := range bs.Parts { + cur := append(path, i+1) + if part.MIMEType == "text" && part.MIMESubType == "plain" { + return part, cur + } + if part.MIMEType == "multipart" { + if part, path := findPlaintext(bs, cur); path != nil { + return part, path + } + } + } + + return nil, nil +} |