2019-03-30 19:12:04 +01:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-03-31 18:14:37 +02:00
|
|
|
"fmt"
|
2019-03-30 19:12:04 +01:00
|
|
|
"io"
|
|
|
|
"os/exec"
|
2019-04-07 16:34:38 +02:00
|
|
|
"strings"
|
2019-03-30 19:12:04 +01:00
|
|
|
|
2019-03-31 20:24:53 +02:00
|
|
|
"github.com/danwakefield/fnmatch"
|
2019-03-31 18:14:37 +02:00
|
|
|
"github.com/emersion/go-imap"
|
2019-03-31 19:36:37 +02:00
|
|
|
"github.com/emersion/go-message"
|
2019-05-14 19:07:48 +02:00
|
|
|
_ "github.com/emersion/go-message/charset"
|
2019-03-31 19:36:37 +02:00
|
|
|
"github.com/emersion/go-message/mail"
|
2019-03-30 19:12:04 +01:00
|
|
|
"github.com/gdamore/tcell"
|
2019-03-31 20:24:53 +02:00
|
|
|
"github.com/google/shlex"
|
2019-03-30 19:12:04 +01:00
|
|
|
"github.com/mattn/go-runewidth"
|
|
|
|
|
2019-05-18 02:57:10 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/config"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/lib"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/lib/ui"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/worker/types"
|
2019-03-30 19:12:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type MessageViewer struct {
|
2019-05-20 22:42:44 +02:00
|
|
|
ui.Invalidatable
|
2019-06-02 07:15:04 +02:00
|
|
|
acct *AccountView
|
2019-05-20 20:56:52 +02:00
|
|
|
conf *config.AercConfig
|
|
|
|
err error
|
|
|
|
grid *ui.Grid
|
2019-05-20 22:42:44 +02:00
|
|
|
msg *types.MessageInfo
|
|
|
|
switcher *PartSwitcher
|
|
|
|
store *lib.MessageStore
|
|
|
|
}
|
|
|
|
|
|
|
|
type PartSwitcher struct {
|
|
|
|
ui.Invalidatable
|
2019-06-07 10:26:14 +02:00
|
|
|
parts []*PartViewer
|
|
|
|
selected int
|
|
|
|
showHeaders bool
|
2019-03-30 19:12:04 +01:00
|
|
|
}
|
|
|
|
|
2019-03-31 18:14:37 +02:00
|
|
|
func formatAddresses(addrs []*imap.Address) string {
|
|
|
|
val := bytes.Buffer{}
|
|
|
|
for i, addr := range addrs {
|
|
|
|
if addr.PersonalName != "" {
|
|
|
|
val.WriteString(fmt.Sprintf("%s <%s@%s>",
|
|
|
|
addr.PersonalName, addr.MailboxName, addr.HostName))
|
|
|
|
} else {
|
|
|
|
val.WriteString(fmt.Sprintf("%s@%s",
|
|
|
|
addr.MailboxName, addr.HostName))
|
|
|
|
}
|
|
|
|
if i != len(addrs)-1 {
|
|
|
|
val.WriteString(", ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val.String()
|
|
|
|
}
|
2019-03-30 19:12:04 +01:00
|
|
|
|
2019-06-02 07:15:04 +02:00
|
|
|
func NewMessageViewer(acct *AccountView, conf *config.AercConfig,
|
2019-05-20 22:42:44 +02:00
|
|
|
store *lib.MessageStore, msg *types.MessageInfo) *MessageViewer {
|
2019-03-30 19:12:04 +01:00
|
|
|
|
|
|
|
grid := ui.NewGrid().Rows([]ui.GridSpec{
|
2019-06-02 16:23:53 +02:00
|
|
|
{ui.SIZE_EXACT, 4}, // TODO: Based on number of header rows
|
2019-03-30 19:12:04 +01:00
|
|
|
{ui.SIZE_WEIGHT, 1},
|
|
|
|
}).Columns([]ui.GridSpec{
|
|
|
|
{ui.SIZE_WEIGHT, 1},
|
|
|
|
})
|
|
|
|
|
2019-03-31 18:14:37 +02:00
|
|
|
// TODO: let user specify additional headers to show by default
|
2019-03-30 19:12:04 +01:00
|
|
|
headers := ui.NewGrid().Rows([]ui.GridSpec{
|
|
|
|
{ui.SIZE_EXACT, 1},
|
|
|
|
{ui.SIZE_EXACT, 1},
|
|
|
|
{ui.SIZE_EXACT, 1},
|
2019-06-02 16:23:53 +02:00
|
|
|
{ui.SIZE_EXACT, 1},
|
2019-03-30 19:12:04 +01:00
|
|
|
}).Columns([]ui.GridSpec{
|
|
|
|
{ui.SIZE_WEIGHT, 1},
|
|
|
|
{ui.SIZE_WEIGHT, 1},
|
|
|
|
})
|
|
|
|
headers.AddChild(
|
|
|
|
&HeaderView{
|
|
|
|
Name: "From",
|
2019-03-31 18:14:37 +02:00
|
|
|
Value: formatAddresses(msg.Envelope.From),
|
2019-03-30 19:12:04 +01:00
|
|
|
}).At(0, 0)
|
|
|
|
headers.AddChild(
|
|
|
|
&HeaderView{
|
|
|
|
Name: "To",
|
2019-03-31 18:14:37 +02:00
|
|
|
Value: formatAddresses(msg.Envelope.To),
|
2019-03-30 19:12:04 +01:00
|
|
|
}).At(0, 1)
|
2019-06-02 16:23:53 +02:00
|
|
|
headers.AddChild(
|
|
|
|
&HeaderView{
|
|
|
|
Name: "Date",
|
|
|
|
Value: msg.Envelope.Date.Format("Mon Jan 2, 2006 at 3:04 PM"),
|
|
|
|
}).At(1, 0).Span(1, 2)
|
2019-03-30 19:12:04 +01:00
|
|
|
headers.AddChild(
|
|
|
|
&HeaderView{
|
2019-03-31 18:14:37 +02:00
|
|
|
Name: "Subject",
|
|
|
|
Value: msg.Envelope.Subject,
|
2019-06-02 16:23:53 +02:00
|
|
|
}).At(2, 0).Span(1, 2)
|
|
|
|
headers.AddChild(ui.NewFill(' ')).At(3, 0).Span(1, 2)
|
2019-03-30 20:55:21 +01:00
|
|
|
|
2019-05-20 22:42:44 +02:00
|
|
|
switcher := &PartSwitcher{}
|
2019-06-07 10:26:14 +02:00
|
|
|
err := createSwitcher(switcher, conf, store, msg, conf.Viewer.ShowHeaders)
|
|
|
|
if err != nil {
|
|
|
|
goto handle_error
|
2019-05-20 20:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
grid.AddChild(headers).At(0, 0)
|
2019-06-02 15:33:41 +02:00
|
|
|
grid.AddChild(switcher).At(1, 0)
|
2019-05-20 20:56:52 +02:00
|
|
|
|
2019-06-02 15:33:41 +02:00
|
|
|
return &MessageViewer{
|
2019-06-02 07:15:04 +02:00
|
|
|
acct: acct,
|
2019-06-07 10:26:14 +02:00
|
|
|
conf: conf,
|
2019-05-20 22:42:44 +02:00
|
|
|
grid: grid,
|
|
|
|
msg: msg,
|
|
|
|
store: store,
|
|
|
|
switcher: switcher,
|
2019-05-20 20:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handle_error:
|
|
|
|
return &MessageViewer{
|
|
|
|
err: err,
|
|
|
|
grid: grid,
|
|
|
|
msg: msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 22:42:44 +02:00
|
|
|
func enumerateParts(conf *config.AercConfig, store *lib.MessageStore,
|
|
|
|
msg *types.MessageInfo, body *imap.BodyStructure,
|
2019-06-07 10:26:14 +02:00
|
|
|
showHeaders bool, index []int) ([]*PartViewer, error) {
|
2019-05-20 22:42:44 +02:00
|
|
|
|
|
|
|
var parts []*PartViewer
|
|
|
|
for i, part := range body.Parts {
|
|
|
|
curindex := append(index, i+1)
|
|
|
|
if part.MIMEType == "multipart" {
|
|
|
|
// Multipart meta-parts are faked
|
|
|
|
pv := &PartViewer{part: part}
|
|
|
|
parts = append(parts, pv)
|
|
|
|
subParts, err := enumerateParts(
|
2019-06-07 10:26:14 +02:00
|
|
|
conf, store, msg, part, showHeaders, curindex)
|
2019-05-20 22:42:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
parts = append(parts, subParts...)
|
|
|
|
continue
|
|
|
|
}
|
2019-06-07 10:26:14 +02:00
|
|
|
pv, err := NewPartViewer(conf, store, msg, part, showHeaders, curindex)
|
2019-05-20 22:42:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
parts = append(parts, pv)
|
|
|
|
}
|
|
|
|
return parts, nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:26:14 +02:00
|
|
|
func createSwitcher(switcher *PartSwitcher, conf *config.AercConfig,
|
|
|
|
store *lib.MessageStore, msg *types.MessageInfo, showHeaders bool) error {
|
|
|
|
var err error
|
|
|
|
switcher.showHeaders = showHeaders
|
|
|
|
|
|
|
|
if len(msg.BodyStructure.Parts) == 0 {
|
|
|
|
pv, err := NewPartViewer(conf, store, msg, msg.BodyStructure,
|
|
|
|
showHeaders, []int{1})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switcher.parts = []*PartViewer{pv}
|
|
|
|
pv.OnInvalidate(func(_ ui.Drawable) {
|
|
|
|
switcher.Invalidate()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
switcher.parts, err = enumerateParts(conf, store,
|
|
|
|
msg, msg.BodyStructure, showHeaders, []int{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switcher.selected = -1
|
|
|
|
for i, pv := range switcher.parts {
|
|
|
|
pv.OnInvalidate(func(_ ui.Drawable) {
|
|
|
|
switcher.Invalidate()
|
|
|
|
})
|
|
|
|
// TODO: switch to user's preferred mimetype, if configured
|
|
|
|
if switcher.selected == -1 && pv.part.MIMEType != "multipart" {
|
|
|
|
switcher.selected = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:56:52 +02:00
|
|
|
func (mv *MessageViewer) Draw(ctx *ui.Context) {
|
|
|
|
if mv.err != nil {
|
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
|
|
|
|
ctx.Printf(0, 0, tcell.StyleDefault, "%s", mv.err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mv.grid.Draw(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mv *MessageViewer) Invalidate() {
|
|
|
|
mv.grid.Invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mv *MessageViewer) OnInvalidate(fn func(d ui.Drawable)) {
|
|
|
|
mv.grid.OnInvalidate(func(_ ui.Drawable) {
|
|
|
|
fn(mv)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-02 07:15:04 +02:00
|
|
|
func (mv *MessageViewer) Store() *lib.MessageStore {
|
|
|
|
return mv.store
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mv *MessageViewer) SelectedAccount() *AccountView {
|
|
|
|
return mv.acct
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mv *MessageViewer) SelectedMessage() *types.MessageInfo {
|
|
|
|
return mv.msg
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:26:14 +02:00
|
|
|
func (mv *MessageViewer) ToggleHeaders() {
|
|
|
|
switcher := mv.switcher
|
|
|
|
err := createSwitcher(switcher, mv.conf, mv.store, mv.msg, !switcher.showHeaders)
|
|
|
|
if err != nil {
|
|
|
|
mv.acct.Logger().Printf("warning: error during create switcher - %v", err)
|
|
|
|
}
|
|
|
|
switcher.Invalidate()
|
|
|
|
}
|
|
|
|
|
2019-05-26 23:37:39 +02:00
|
|
|
func (mv *MessageViewer) CurrentPart() *PartInfo {
|
|
|
|
switcher := mv.switcher
|
|
|
|
part := switcher.parts[switcher.selected]
|
|
|
|
|
|
|
|
return &PartInfo{
|
|
|
|
Index: part.index,
|
|
|
|
Msg: part.msg,
|
|
|
|
Part: part.part,
|
|
|
|
Store: part.store,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 22:49:39 +02:00
|
|
|
func (mv *MessageViewer) PreviousPart() {
|
|
|
|
switcher := mv.switcher
|
|
|
|
for {
|
|
|
|
switcher.selected--
|
|
|
|
if switcher.selected < 0 {
|
|
|
|
switcher.selected = len(switcher.parts) - 1
|
|
|
|
}
|
|
|
|
if switcher.parts[switcher.selected].part.MIMEType != "multipart" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mv.Invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mv *MessageViewer) NextPart() {
|
|
|
|
switcher := mv.switcher
|
|
|
|
for {
|
|
|
|
switcher.selected++
|
|
|
|
if switcher.selected >= len(switcher.parts) {
|
|
|
|
switcher.selected = 0
|
|
|
|
}
|
|
|
|
if switcher.parts[switcher.selected].part.MIMEType != "multipart" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mv.Invalidate()
|
|
|
|
}
|
|
|
|
|
2019-05-20 22:42:44 +02:00
|
|
|
func (ps *PartSwitcher) Invalidate() {
|
|
|
|
ps.DoInvalidate(ps)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *PartSwitcher) Focus(focus bool) {
|
|
|
|
if ps.parts[ps.selected].term != nil {
|
|
|
|
ps.parts[ps.selected].term.Focus(focus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *PartSwitcher) Event(event tcell.Event) bool {
|
|
|
|
if ps.parts[ps.selected].term != nil {
|
|
|
|
return ps.parts[ps.selected].term.Event(event)
|
2019-05-20 20:56:52 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-20 22:42:44 +02:00
|
|
|
func (ps *PartSwitcher) Draw(ctx *ui.Context) {
|
|
|
|
height := len(ps.parts)
|
|
|
|
if height == 1 {
|
|
|
|
ps.parts[ps.selected].Draw(ctx)
|
|
|
|
return
|
2019-05-20 20:56:52 +02:00
|
|
|
}
|
2019-05-20 22:42:44 +02:00
|
|
|
// TODO: cap height and add scrolling for messages with many parts
|
|
|
|
y := ctx.Height() - height
|
|
|
|
for i, part := range ps.parts {
|
|
|
|
style := tcell.StyleDefault.Reverse(ps.selected == i)
|
|
|
|
ctx.Fill(0, y+i, ctx.Width(), 1, ' ', style)
|
2019-05-20 23:03:37 +02:00
|
|
|
name := fmt.Sprintf("%s/%s",
|
2019-05-20 22:42:44 +02:00
|
|
|
strings.ToLower(part.part.MIMEType),
|
|
|
|
strings.ToLower(part.part.MIMESubType))
|
2019-05-20 23:03:37 +02:00
|
|
|
if filename, ok := part.part.DispositionParams["filename"]; ok {
|
|
|
|
name += fmt.Sprintf(" (%s)", filename)
|
|
|
|
}
|
|
|
|
ctx.Printf(len(part.index)*2, y+i, style, "%s", name)
|
2019-05-20 22:42:44 +02:00
|
|
|
}
|
|
|
|
ps.parts[ps.selected].Draw(ctx.Subcontext(
|
|
|
|
0, 0, ctx.Width(), ctx.Height()-height))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mv *MessageViewer) Event(event tcell.Event) bool {
|
|
|
|
return mv.switcher.Event(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mv *MessageViewer) Focus(focus bool) {
|
|
|
|
mv.switcher.Focus(focus)
|
2019-05-20 20:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type PartViewer struct {
|
2019-05-20 22:42:44 +02:00
|
|
|
ui.Invalidatable
|
2019-06-07 10:26:14 +02:00
|
|
|
err error
|
|
|
|
fetched bool
|
|
|
|
filter *exec.Cmd
|
|
|
|
index []int
|
|
|
|
msg *types.MessageInfo
|
|
|
|
pager *exec.Cmd
|
|
|
|
pagerin io.WriteCloser
|
|
|
|
part *imap.BodyStructure
|
|
|
|
showHeaders bool
|
|
|
|
sink io.WriteCloser
|
|
|
|
source io.Reader
|
|
|
|
store *lib.MessageStore
|
|
|
|
term *Terminal
|
2019-05-20 20:56:52 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 23:37:39 +02:00
|
|
|
type PartInfo struct {
|
|
|
|
Index []int
|
|
|
|
Msg *types.MessageInfo
|
|
|
|
Part *imap.BodyStructure
|
|
|
|
Store *lib.MessageStore
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:56:52 +02:00
|
|
|
func NewPartViewer(conf *config.AercConfig,
|
2019-05-20 22:42:44 +02:00
|
|
|
store *lib.MessageStore, msg *types.MessageInfo,
|
2019-06-07 10:26:14 +02:00
|
|
|
part *imap.BodyStructure, showHeaders bool,
|
|
|
|
index []int) (*PartViewer, error) {
|
2019-05-20 20:56:52 +02:00
|
|
|
|
2019-03-31 20:24:53 +02:00
|
|
|
var (
|
|
|
|
filter *exec.Cmd
|
|
|
|
pager *exec.Cmd
|
|
|
|
pipe io.WriteCloser
|
|
|
|
pagerin io.WriteCloser
|
2019-03-31 20:32:26 +02:00
|
|
|
term *Terminal
|
2019-03-31 20:24:53 +02:00
|
|
|
)
|
|
|
|
cmd, err := shlex.Split(conf.Viewer.Pager)
|
|
|
|
if err != nil {
|
2019-05-20 20:56:52 +02:00
|
|
|
return nil, err
|
2019-03-31 20:24:53 +02:00
|
|
|
}
|
2019-05-20 20:56:52 +02:00
|
|
|
|
2019-03-31 20:24:53 +02:00
|
|
|
pager = exec.Command(cmd[0], cmd[1:]...)
|
|
|
|
|
|
|
|
for _, f := range conf.Filters {
|
2019-05-20 20:56:52 +02:00
|
|
|
mime := strings.ToLower(part.MIMEType) +
|
|
|
|
"/" + strings.ToLower(part.MIMESubType)
|
2019-03-31 20:24:53 +02:00
|
|
|
switch f.FilterType {
|
|
|
|
case config.FILTER_MIMETYPE:
|
|
|
|
if fnmatch.Match(f.Filter, mime, 0) {
|
2019-03-31 21:21:04 +02:00
|
|
|
filter = exec.Command("sh", "-c", f.Command)
|
2019-03-31 20:24:53 +02:00
|
|
|
}
|
2019-03-31 20:42:18 +02:00
|
|
|
case config.FILTER_HEADER:
|
|
|
|
var header string
|
|
|
|
switch f.Header {
|
|
|
|
case "subject":
|
|
|
|
header = msg.Envelope.Subject
|
|
|
|
case "from":
|
|
|
|
header = formatAddresses(msg.Envelope.From)
|
|
|
|
case "to":
|
|
|
|
header = formatAddresses(msg.Envelope.To)
|
|
|
|
case "cc":
|
|
|
|
header = formatAddresses(msg.Envelope.Cc)
|
|
|
|
}
|
|
|
|
if f.Regex.Match([]byte(header)) {
|
2019-03-31 21:21:04 +02:00
|
|
|
filter = exec.Command("sh", "-c", f.Command)
|
2019-03-31 20:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter != nil {
|
|
|
|
break
|
2019-03-31 20:24:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter != nil {
|
2019-05-20 20:56:52 +02:00
|
|
|
if pipe, err = filter.StdinPipe(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if pagerin, _ = pager.StdinPipe(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-20 22:42:44 +02:00
|
|
|
if term, err = NewTerminal(pager); err != nil {
|
2019-05-20 20:56:52 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-03-31 20:24:53 +02:00
|
|
|
|
2019-05-20 20:56:52 +02:00
|
|
|
pv := &PartViewer{
|
2019-06-07 10:26:14 +02:00
|
|
|
filter: filter,
|
|
|
|
index: index,
|
|
|
|
msg: msg,
|
|
|
|
pager: pager,
|
|
|
|
pagerin: pagerin,
|
|
|
|
part: part,
|
|
|
|
showHeaders: showHeaders,
|
|
|
|
sink: pipe,
|
|
|
|
store: store,
|
|
|
|
term: term,
|
2019-03-31 18:14:37 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 22:42:44 +02:00
|
|
|
if term != nil {
|
|
|
|
term.OnStart = func() {
|
|
|
|
pv.attemptCopy()
|
|
|
|
}
|
|
|
|
term.OnInvalidate(func(_ ui.Drawable) {
|
|
|
|
pv.Invalidate()
|
|
|
|
})
|
2019-03-31 18:35:51 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 20:56:52 +02:00
|
|
|
return pv, nil
|
|
|
|
}
|
2019-03-31 20:32:26 +02:00
|
|
|
|
2019-05-20 20:56:52 +02:00
|
|
|
func (pv *PartViewer) SetSource(reader io.Reader) {
|
|
|
|
pv.source = reader
|
|
|
|
pv.attemptCopy()
|
2019-03-30 19:12:04 +01:00
|
|
|
}
|
|
|
|
|
2019-05-20 20:56:52 +02:00
|
|
|
func (pv *PartViewer) attemptCopy() {
|
|
|
|
if pv.source != nil && pv.pager.Process != nil {
|
2019-05-14 19:07:48 +02:00
|
|
|
header := message.Header{}
|
2019-05-20 20:56:52 +02:00
|
|
|
header.SetText("Content-Transfer-Encoding", pv.part.Encoding)
|
|
|
|
header.SetContentType(pv.part.MIMEType, pv.part.Params)
|
|
|
|
header.SetText("Content-Description", pv.part.Description)
|
|
|
|
if pv.filter != nil {
|
|
|
|
stdout, _ := pv.filter.StdoutPipe()
|
2019-05-26 17:18:51 +02:00
|
|
|
stderr, _ := pv.filter.StderrPipe()
|
2019-05-20 20:56:52 +02:00
|
|
|
pv.filter.Start()
|
2019-05-26 17:18:51 +02:00
|
|
|
ch := make(chan interface{})
|
2019-03-31 20:24:53 +02:00
|
|
|
go func() {
|
2019-05-20 20:56:52 +02:00
|
|
|
_, err := io.Copy(pv.pagerin, stdout)
|
2019-03-31 20:24:53 +02:00
|
|
|
if err != nil {
|
2019-05-20 20:56:52 +02:00
|
|
|
pv.err = err
|
|
|
|
pv.Invalidate()
|
2019-03-31 20:24:53 +02:00
|
|
|
}
|
2019-03-31 20:32:26 +02:00
|
|
|
stdout.Close()
|
2019-05-26 17:18:51 +02:00
|
|
|
ch <- nil
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(pv.pagerin, stderr)
|
|
|
|
if err != nil {
|
|
|
|
pv.err = err
|
|
|
|
pv.Invalidate()
|
|
|
|
}
|
|
|
|
stderr.Close()
|
|
|
|
ch <- nil
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
<-ch
|
|
|
|
<-ch
|
|
|
|
pv.pagerin.Close()
|
2019-03-31 20:24:53 +02:00
|
|
|
}()
|
|
|
|
}
|
2019-03-31 18:35:51 +02:00
|
|
|
go func() {
|
2019-06-07 10:26:14 +02:00
|
|
|
if pv.showHeaders && pv.msg.RFC822Headers != nil {
|
|
|
|
fields := pv.msg.RFC822Headers.Fields()
|
|
|
|
for fields.Next() {
|
|
|
|
field := fmt.Sprintf("%s: %s\n", fields.Key(), fields.Value())
|
|
|
|
pv.sink.Write([]byte(field))
|
|
|
|
}
|
|
|
|
pv.sink.Write([]byte{'\n'})
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:56:52 +02:00
|
|
|
entity, err := message.New(header, pv.source)
|
2019-03-31 19:36:37 +02:00
|
|
|
if err != nil {
|
2019-05-20 20:56:52 +02:00
|
|
|
pv.err = err
|
|
|
|
pv.Invalidate()
|
2019-03-31 19:36:37 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
reader := mail.NewReader(entity)
|
|
|
|
part, err := reader.NextPart()
|
|
|
|
if err != nil {
|
2019-05-20 20:56:52 +02:00
|
|
|
pv.err = err
|
|
|
|
pv.Invalidate()
|
2019-03-31 19:36:37 +02:00
|
|
|
return
|
|
|
|
}
|
2019-05-20 20:56:52 +02:00
|
|
|
io.Copy(pv.sink, part.Body)
|
|
|
|
pv.sink.Close()
|
2019-03-31 18:35:51 +02:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:56:52 +02:00
|
|
|
func (pv *PartViewer) Invalidate() {
|
2019-05-20 22:42:44 +02:00
|
|
|
pv.DoInvalidate(pv)
|
2019-03-30 19:12:04 +01:00
|
|
|
}
|
|
|
|
|
2019-05-20 20:56:52 +02:00
|
|
|
func (pv *PartViewer) Draw(ctx *ui.Context) {
|
2019-05-20 22:42:44 +02:00
|
|
|
if pv.filter == nil {
|
|
|
|
// TODO: Let them download it directly or something
|
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
|
2019-05-20 23:05:37 +02:00
|
|
|
ctx.Printf(0, 0, tcell.StyleDefault.Foreground(tcell.ColorRed),
|
2019-05-20 22:42:44 +02:00
|
|
|
"No filter configured for this mimetype")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !pv.fetched {
|
|
|
|
pv.store.FetchBodyPart(pv.msg.Uid, pv.index, pv.SetSource)
|
|
|
|
pv.fetched = true
|
|
|
|
}
|
2019-05-20 20:56:52 +02:00
|
|
|
if pv.err != nil {
|
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
|
|
|
|
ctx.Printf(0, 0, tcell.StyleDefault, "%s", pv.err.Error())
|
|
|
|
return
|
2019-03-31 20:32:26 +02:00
|
|
|
}
|
2019-05-20 20:56:52 +02:00
|
|
|
pv.term.Draw(ctx)
|
2019-03-30 19:12:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type HeaderView struct {
|
2019-04-27 18:47:59 +02:00
|
|
|
ui.Invalidatable
|
2019-03-30 19:12:04 +01:00
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hv *HeaderView) Draw(ctx *ui.Context) {
|
2019-03-31 18:14:37 +02:00
|
|
|
name := hv.Name
|
|
|
|
size := runewidth.StringWidth(name)
|
|
|
|
lim := ctx.Width() - size - 1
|
|
|
|
value := runewidth.Truncate(" "+hv.Value, lim, "…")
|
2019-03-30 21:50:14 +01:00
|
|
|
var (
|
|
|
|
hstyle tcell.Style
|
|
|
|
vstyle tcell.Style
|
|
|
|
)
|
|
|
|
// TODO: Make this more robust and less dumb
|
2019-03-30 20:55:21 +01:00
|
|
|
if hv.Name == "PGP" {
|
2019-03-30 21:50:14 +01:00
|
|
|
vstyle = tcell.StyleDefault.Foreground(tcell.ColorGreen)
|
|
|
|
hstyle = tcell.StyleDefault.Bold(true)
|
2019-03-30 20:55:21 +01:00
|
|
|
} else {
|
2019-03-30 21:50:14 +01:00
|
|
|
vstyle = tcell.StyleDefault
|
|
|
|
hstyle = tcell.StyleDefault.Bold(true)
|
2019-03-30 20:55:21 +01:00
|
|
|
}
|
2019-03-30 21:50:14 +01:00
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', vstyle)
|
2019-03-31 18:14:37 +02:00
|
|
|
ctx.Printf(0, 0, hstyle, name)
|
|
|
|
ctx.Printf(size, 0, vstyle, value)
|
2019-03-30 19:12:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hv *HeaderView) Invalidate() {
|
2019-04-27 18:47:59 +02:00
|
|
|
hv.DoInvalidate(hv)
|
2019-03-30 19:12:04 +01:00
|
|
|
}
|