Decode messages before rendering them

This commit is contained in:
Drew DeVault 2019-03-31 13:36:37 -04:00
parent 56ea7f0e43
commit 711d22891b

View file

@ -7,6 +7,8 @@ import (
"os/exec" "os/exec"
"github.com/emersion/go-imap" "github.com/emersion/go-imap"
"github.com/emersion/go-message"
"github.com/emersion/go-message/mail"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/mattn/go-runewidth" "github.com/mattn/go-runewidth"
@ -17,6 +19,7 @@ import (
type MessageViewer struct { type MessageViewer struct {
cmd *exec.Cmd cmd *exec.Cmd
msg *types.MessageInfo
source io.Reader source io.Reader
sink io.WriteCloser sink io.WriteCloser
grid *ui.Grid grid *ui.Grid
@ -94,8 +97,9 @@ func NewMessageViewer(store *lib.MessageStore,
viewer := &MessageViewer{ viewer := &MessageViewer{
cmd: cmd, cmd: cmd,
sink: pipe,
grid: grid, grid: grid,
msg: msg,
sink: pipe,
term: term, term: term,
} }
@ -113,8 +117,24 @@ func NewMessageViewer(store *lib.MessageStore,
func (mv *MessageViewer) attemptCopy() { func (mv *MessageViewer) attemptCopy() {
if mv.source != nil && mv.cmd.Process != nil { if mv.source != nil && mv.cmd.Process != nil {
header := make(message.Header)
header.Set("Content-Transfer-Encoding", mv.msg.BodyStructure.Encoding)
header.SetContentType(
mv.msg.BodyStructure.MIMEType, mv.msg.BodyStructure.Params)
header.SetContentDescription(mv.msg.BodyStructure.Description)
go func() { go func() {
io.Copy(mv.sink, mv.source) entity, err := message.New(header, mv.source)
if err != nil {
io.WriteString(mv.sink, err.Error())
return
}
reader := mail.NewReader(entity)
part, err := reader.NextPart()
if err != nil {
io.WriteString(mv.sink, err.Error())
return
}
io.Copy(mv.sink, part.Body)
mv.sink.Close() mv.sink.Close()
}() }()
} }