2019-03-30 19:12:04 +01:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2019-06-07 17:14:40 +02:00
|
|
|
"bufio"
|
2019-07-10 02:04:21 +02:00
|
|
|
"errors"
|
2019-03-31 18:14:37 +02:00
|
|
|
"fmt"
|
2019-03-30 19:12:04 +01:00
|
|
|
"io"
|
|
|
|
"os/exec"
|
2019-06-07 17:14:40 +02:00
|
|
|
"regexp"
|
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-12-06 00:58:54 +01:00
|
|
|
message "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"
|
2019-07-08 04:43:56 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-03-30 19:12:04 +01:00
|
|
|
)
|
|
|
|
|
2019-06-07 17:14:40 +02:00
|
|
|
var ansi = regexp.MustCompile("^\x1B\\[[0-?]*[ -/]*[@-~]")
|
|
|
|
|
2019-12-18 06:33:58 +01:00
|
|
|
var _ ProvidesMessages = (*MessageViewer)(nil)
|
|
|
|
|
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-07-08 04:43:56 +02:00
|
|
|
msg *models.MessageInfo
|
2019-05-20 22:42:44 +02:00
|
|
|
switcher *PartSwitcher
|
|
|
|
store *lib.MessageStore
|
|
|
|
}
|
|
|
|
|
|
|
|
type PartSwitcher struct {
|
|
|
|
ui.Invalidatable
|
2019-07-17 22:51:02 +02:00
|
|
|
parts []*PartViewer
|
|
|
|
selected int
|
|
|
|
showHeaders bool
|
|
|
|
alwaysShowMime bool
|
2019-09-06 00:32:36 +02:00
|
|
|
|
|
|
|
height int
|
|
|
|
mv *MessageViewer
|
2019-03-30 19:12:04 +01:00
|
|
|
}
|
|
|
|
|
2019-06-02 07:15:04 +02:00
|
|
|
func NewMessageViewer(acct *AccountView, conf *config.AercConfig,
|
2019-07-08 04:43:56 +02:00
|
|
|
store *lib.MessageStore, msg *models.MessageInfo) *MessageViewer {
|
2019-07-23 01:29:07 +02:00
|
|
|
|
2019-12-23 12:51:59 +01:00
|
|
|
hf := HeaderLayoutFilter{
|
|
|
|
layout: HeaderLayout(conf.Viewer.HeaderLayout),
|
|
|
|
keep: func(msg *models.MessageInfo, header string) bool {
|
|
|
|
if fmtHeader(msg, header, "2") != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
}
|
|
|
|
layout := hf.forMessage(msg)
|
2019-07-23 01:29:07 +02:00
|
|
|
header, headerHeight := layout.grid(
|
|
|
|
func(header string) ui.Drawable {
|
|
|
|
return &HeaderView{
|
|
|
|
Name: header,
|
2019-12-18 19:26:25 +01:00
|
|
|
Value: fmtHeader(msg, header, conf.Ui.TimestampFormat),
|
2019-07-23 01:29:07 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-03-30 19:12:04 +01:00
|
|
|
|
|
|
|
grid := ui.NewGrid().Rows([]ui.GridSpec{
|
2019-07-15 20:56:44 +02:00
|
|
|
{ui.SIZE_EXACT, headerHeight},
|
2019-03-30 19:12:04 +01:00
|
|
|
{ui.SIZE_WEIGHT, 1},
|
|
|
|
}).Columns([]ui.GridSpec{
|
|
|
|
{ui.SIZE_WEIGHT, 1},
|
|
|
|
})
|
|
|
|
|
2019-05-20 22:42:44 +02:00
|
|
|
switcher := &PartSwitcher{}
|
2019-11-15 21:28:34 +01:00
|
|
|
err := createSwitcher(acct, switcher, conf, store, msg)
|
2019-06-07 10:26:14 +02:00
|
|
|
if err != nil {
|
2019-07-15 20:56:44 +02:00
|
|
|
return &MessageViewer{
|
|
|
|
err: err,
|
|
|
|
grid: grid,
|
|
|
|
msg: msg,
|
|
|
|
}
|
2019-05-20 20:56:52 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 20:56:44 +02:00
|
|
|
grid.AddChild(header).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-09-06 00:32:36 +02:00
|
|
|
mv := &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
|
|
|
}
|
2019-09-06 00:32:36 +02:00
|
|
|
switcher.mv = mv
|
|
|
|
|
|
|
|
return mv
|
2019-07-15 20:56:44 +02:00
|
|
|
}
|
2019-05-20 20:56:52 +02:00
|
|
|
|
2019-12-18 19:26:25 +01:00
|
|
|
func fmtHeader(msg *models.MessageInfo, header string, timefmt string) string {
|
2019-07-15 20:56:44 +02:00
|
|
|
switch header {
|
|
|
|
case "From":
|
|
|
|
return models.FormatAddresses(msg.Envelope.From)
|
|
|
|
case "To":
|
|
|
|
return models.FormatAddresses(msg.Envelope.To)
|
|
|
|
case "Cc":
|
|
|
|
return models.FormatAddresses(msg.Envelope.Cc)
|
|
|
|
case "Bcc":
|
|
|
|
return models.FormatAddresses(msg.Envelope.Bcc)
|
|
|
|
case "Date":
|
2019-12-18 19:26:25 +01:00
|
|
|
return msg.Envelope.Date.Local().Format(timefmt)
|
2019-07-15 20:56:44 +02:00
|
|
|
case "Subject":
|
|
|
|
return msg.Envelope.Subject
|
2019-12-23 12:51:59 +01:00
|
|
|
case "Labels":
|
|
|
|
return strings.Join(msg.Labels, ", ")
|
2019-07-15 20:56:44 +02:00
|
|
|
default:
|
|
|
|
return msg.RFC822Headers.Get(header)
|
2019-05-20 20:56:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 21:28:34 +01:00
|
|
|
func enumerateParts(acct *AccountView, conf *config.AercConfig, store *lib.MessageStore,
|
2019-07-08 04:43:58 +02:00
|
|
|
msg *models.MessageInfo, body *models.BodyStructure,
|
2019-07-17 22:49:28 +02:00
|
|
|
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-11-15 21:28:34 +01:00
|
|
|
acct, conf, store, msg, part, curindex)
|
2019-05-20 22:42:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
parts = append(parts, subParts...)
|
|
|
|
continue
|
|
|
|
}
|
2019-11-15 21:28:34 +01:00
|
|
|
pv, err := NewPartViewer(acct, conf, store, msg, part, curindex)
|
2019-05-20 22:42:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
parts = append(parts, pv)
|
|
|
|
}
|
|
|
|
return parts, nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 21:28:34 +01:00
|
|
|
func createSwitcher(acct *AccountView, switcher *PartSwitcher, conf *config.AercConfig,
|
2019-07-17 22:49:28 +02:00
|
|
|
store *lib.MessageStore, msg *models.MessageInfo) error {
|
2019-07-19 23:26:43 +02:00
|
|
|
|
2019-06-07 10:26:14 +02:00
|
|
|
var err error
|
2019-07-19 23:26:43 +02:00
|
|
|
switcher.selected = -1
|
2019-07-17 22:49:28 +02:00
|
|
|
switcher.showHeaders = conf.Viewer.ShowHeaders
|
2019-07-17 22:51:02 +02:00
|
|
|
switcher.alwaysShowMime = conf.Viewer.AlwaysShowMime
|
2019-06-07 10:26:14 +02:00
|
|
|
|
|
|
|
if len(msg.BodyStructure.Parts) == 0 {
|
2019-07-17 23:09:35 +02:00
|
|
|
switcher.selected = 0
|
2019-11-15 21:28:34 +01:00
|
|
|
pv, err := NewPartViewer(acct, conf, store, msg, msg.BodyStructure, []int{1})
|
2019-06-07 10:26:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switcher.parts = []*PartViewer{pv}
|
|
|
|
pv.OnInvalidate(func(_ ui.Drawable) {
|
|
|
|
switcher.Invalidate()
|
|
|
|
})
|
|
|
|
} else {
|
2019-11-15 21:28:34 +01:00
|
|
|
switcher.parts, err = enumerateParts(acct, conf, store,
|
2019-07-17 22:49:28 +02:00
|
|
|
msg, msg.BodyStructure, []int{})
|
2019-06-07 10:26:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-08 20:29:26 +02:00
|
|
|
selectedPriority := -1
|
2019-07-19 23:26:43 +02:00
|
|
|
fmt.Printf("Selecting best message from %v\n", conf.Viewer.Alternatives)
|
2019-06-07 10:26:14 +02:00
|
|
|
for i, pv := range switcher.parts {
|
|
|
|
pv.OnInvalidate(func(_ ui.Drawable) {
|
|
|
|
switcher.Invalidate()
|
|
|
|
})
|
2019-06-08 20:29:26 +02:00
|
|
|
// Switch to user's preferred mimetype
|
2019-06-07 10:26:14 +02:00
|
|
|
if switcher.selected == -1 && pv.part.MIMEType != "multipart" {
|
|
|
|
switcher.selected = i
|
2019-07-19 23:26:43 +02:00
|
|
|
}
|
2019-08-01 22:19:57 +02:00
|
|
|
mime := strings.ToLower(pv.part.MIMEType) +
|
|
|
|
"/" + strings.ToLower(pv.part.MIMESubType)
|
|
|
|
for idx, m := range conf.Viewer.Alternatives {
|
|
|
|
if m != mime {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
priority := len(conf.Viewer.Alternatives) - idx
|
|
|
|
if priority > selectedPriority {
|
|
|
|
selectedPriority = priority
|
|
|
|
switcher.selected = i
|
2019-06-08 20:29:26 +02:00
|
|
|
}
|
2019-06-07 10:26:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-09-06 00:32:36 +02:00
|
|
|
func (mv *MessageViewer) MouseEvent(localX int, localY int, event tcell.Event) {
|
|
|
|
if mv.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mv.grid.MouseEvent(localX, localY, event)
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:56:52 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-10 02:04:21 +02:00
|
|
|
func (mv *MessageViewer) SelectedMessage() (*models.MessageInfo, error) {
|
|
|
|
if mv.msg == nil {
|
|
|
|
return nil, errors.New("no message selected")
|
|
|
|
}
|
|
|
|
return mv.msg, nil
|
2019-06-02 07:15:04 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 06:33:58 +01:00
|
|
|
func (mv *MessageViewer) MarkedMessages() ([]*models.MessageInfo, error) {
|
|
|
|
store := mv.Store()
|
|
|
|
return msgInfoFromUids(store, store.Marked())
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:26:14 +02:00
|
|
|
func (mv *MessageViewer) ToggleHeaders() {
|
|
|
|
switcher := mv.switcher
|
2019-07-17 22:49:28 +02:00
|
|
|
mv.conf.Viewer.ShowHeaders = !mv.conf.Viewer.ShowHeaders
|
2019-06-07 17:14:40 +02:00
|
|
|
err := createSwitcher(
|
2019-11-15 21:28:34 +01:00
|
|
|
mv.acct, switcher, mv.conf, mv.store, mv.msg)
|
2019-06-07 10:26:14 +02:00
|
|
|
if err != nil {
|
2019-06-07 17:14:40 +02:00
|
|
|
mv.acct.Logger().Printf(
|
|
|
|
"warning: error during create switcher - %v", err)
|
2019-06-07 10:26:14 +02:00
|
|
|
}
|
|
|
|
switcher.Invalidate()
|
|
|
|
}
|
|
|
|
|
2019-07-05 18:21:12 +02:00
|
|
|
func (mv *MessageViewer) SelectedMessagePart() *PartInfo {
|
2019-05-26 23:37:39 +02:00
|
|
|
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-10-15 11:01:47 +02:00
|
|
|
func (mv *MessageViewer) Close() {
|
|
|
|
mv.switcher.Cleanup()
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-11-15 21:28:34 +01:00
|
|
|
return ps.parts[ps.selected].Event(event)
|
2019-05-20 20:56:52 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 22:42:44 +02:00
|
|
|
func (ps *PartSwitcher) Draw(ctx *ui.Context) {
|
|
|
|
height := len(ps.parts)
|
2019-07-17 22:51:02 +02:00
|
|
|
if height == 1 && !ps.alwaysShowMime {
|
2019-05-20 22:42:44 +02:00
|
|
|
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
|
2019-09-06 00:32:36 +02:00
|
|
|
ps.height = ctx.Height()
|
2019-05-20 22:42:44 +02:00
|
|
|
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)
|
2019-12-07 19:58:12 +01:00
|
|
|
} else if filename, ok := part.part.Params["name"]; ok {
|
|
|
|
// workaround golang not supporting RFC2231 besides ASCII and UTF8
|
|
|
|
name += fmt.Sprintf(" (%s)", filename)
|
2019-05-20 23:03:37 +02:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2019-09-06 00:32:36 +02:00
|
|
|
func (ps *PartSwitcher) MouseEvent(localX int, localY int, event tcell.Event) {
|
|
|
|
switch event := event.(type) {
|
|
|
|
case *tcell.EventMouse:
|
|
|
|
switch event.Buttons() {
|
|
|
|
case tcell.Button1:
|
|
|
|
height := len(ps.parts)
|
|
|
|
y := ps.height - height
|
2019-11-15 20:02:50 +01:00
|
|
|
if localY < y && ps.parts[ps.selected].term != nil {
|
2019-09-06 00:32:36 +02:00
|
|
|
ps.parts[ps.selected].term.MouseEvent(localX, localY, event)
|
|
|
|
}
|
|
|
|
for i, _ := range ps.parts {
|
|
|
|
if localY != y+i {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ps.parts[i].part.MIMEType == "multipart" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ps.parts[ps.selected].term != nil {
|
|
|
|
ps.parts[ps.selected].term.Focus(false)
|
|
|
|
}
|
|
|
|
ps.selected = i
|
|
|
|
ps.Invalidate()
|
|
|
|
if ps.parts[ps.selected].term != nil {
|
|
|
|
ps.parts[ps.selected].term.Focus(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case tcell.WheelDown:
|
|
|
|
height := len(ps.parts)
|
|
|
|
y := ps.height - height
|
|
|
|
if localY < y {
|
|
|
|
ps.parts[ps.selected].term.MouseEvent(localX, localY, event)
|
|
|
|
}
|
|
|
|
if ps.parts[ps.selected].term != nil {
|
|
|
|
ps.parts[ps.selected].term.Focus(false)
|
|
|
|
}
|
|
|
|
ps.mv.NextPart()
|
|
|
|
if ps.parts[ps.selected].term != nil {
|
|
|
|
ps.parts[ps.selected].term.Focus(true)
|
|
|
|
}
|
|
|
|
case tcell.WheelUp:
|
|
|
|
height := len(ps.parts)
|
|
|
|
y := ps.height - height
|
|
|
|
if localY < y {
|
|
|
|
ps.parts[ps.selected].term.MouseEvent(localX, localY, event)
|
|
|
|
}
|
|
|
|
if ps.parts[ps.selected].term != nil {
|
|
|
|
ps.parts[ps.selected].term.Focus(false)
|
|
|
|
}
|
|
|
|
ps.mv.PreviousPart()
|
|
|
|
if ps.parts[ps.selected].term != nil {
|
|
|
|
ps.parts[ps.selected].term.Focus(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 11:01:47 +02:00
|
|
|
func (ps *PartSwitcher) Cleanup() {
|
|
|
|
for _, partViewer := range ps.parts {
|
|
|
|
partViewer.Cleanup()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 22:42:44 +02:00
|
|
|
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
|
2019-07-08 04:43:56 +02:00
|
|
|
msg *models.MessageInfo
|
2019-06-07 10:26:14 +02:00
|
|
|
pager *exec.Cmd
|
|
|
|
pagerin io.WriteCloser
|
2019-07-08 04:43:58 +02:00
|
|
|
part *models.BodyStructure
|
2019-06-07 10:26:14 +02:00
|
|
|
showHeaders bool
|
|
|
|
sink io.WriteCloser
|
|
|
|
source io.Reader
|
|
|
|
store *lib.MessageStore
|
|
|
|
term *Terminal
|
2019-11-15 21:28:34 +01:00
|
|
|
selecter *Selecter
|
|
|
|
grid *ui.Grid
|
2019-05-20 20:56:52 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 21:28:34 +01:00
|
|
|
func NewPartViewer(acct *AccountView, conf *config.AercConfig,
|
2019-07-08 04:43:56 +02:00
|
|
|
store *lib.MessageStore, msg *models.MessageInfo,
|
2019-07-17 22:49:28 +02:00
|
|
|
part *models.BodyStructure,
|
2019-06-07 10:26:14 +02:00
|
|
|
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":
|
2019-07-08 04:43:58 +02:00
|
|
|
header = models.FormatAddresses(msg.Envelope.From)
|
2019-03-31 20:42:18 +02:00
|
|
|
case "to":
|
2019-07-08 04:43:58 +02:00
|
|
|
header = models.FormatAddresses(msg.Envelope.To)
|
2019-03-31 20:42:18 +02:00
|
|
|
case "cc":
|
2019-07-08 04:43:58 +02:00
|
|
|
header = models.FormatAddresses(msg.Envelope.Cc)
|
2019-03-31 20:42:18 +02:00
|
|
|
}
|
|
|
|
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-11-15 21:28:34 +01:00
|
|
|
grid := ui.NewGrid().Rows([]ui.GridSpec{
|
|
|
|
{ui.SIZE_EXACT, 3}, // Message
|
|
|
|
{ui.SIZE_EXACT, 1}, // Selector
|
|
|
|
{ui.SIZE_WEIGHT, 1},
|
|
|
|
}).Columns([]ui.GridSpec{
|
|
|
|
{ui.SIZE_WEIGHT, 1},
|
|
|
|
})
|
|
|
|
|
|
|
|
selecter := NewSelecter([]string{"Save message", "Pipe to command"}, 0).
|
|
|
|
OnChoose(func(option string) {
|
|
|
|
switch option {
|
|
|
|
case "Save message":
|
|
|
|
acct.aerc.BeginExCommand("save ")
|
|
|
|
case "Pipe to command":
|
|
|
|
acct.aerc.BeginExCommand("pipe ")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
grid.AddChild(selecter).At(2, 0)
|
|
|
|
|
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,
|
2019-07-17 22:49:28 +02:00
|
|
|
showHeaders: conf.Viewer.ShowHeaders,
|
2019-06-07 10:26:14 +02:00
|
|
|
sink: pipe,
|
|
|
|
store: store,
|
|
|
|
term: term,
|
2019-11-15 21:28:34 +01:00
|
|
|
selecter: selecter,
|
|
|
|
grid: grid,
|
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() {
|
2019-11-02 03:36:01 +01:00
|
|
|
if pv.source != nil && pv.pager != 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)
|
2019-12-06 00:58:54 +01:00
|
|
|
header.SetContentType(fmt.Sprintf("%s/%s", pv.part.MIMEType, pv.part.MIMESubType), pv.part.Params)
|
2019-05-20 20:56:52 +02:00
|
|
|
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
|
2019-10-15 11:01:49 +02:00
|
|
|
pv.filter.Wait()
|
2019-05-26 17:18:51 +02:00
|
|
|
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() {
|
2019-06-07 17:14:40 +02:00
|
|
|
field := fmt.Sprintf(
|
|
|
|
"%s: %s\n", fields.Key(), fields.Value())
|
2019-06-07 10:26:14 +02:00
|
|
|
pv.sink.Write([]byte(field))
|
|
|
|
}
|
2019-12-23 12:51:59 +01:00
|
|
|
// virtual header
|
|
|
|
if len(pv.msg.Labels) != 0 {
|
|
|
|
labels := fmtHeader(pv.msg, "Labels", "")
|
|
|
|
pv.sink.Write([]byte(fmt.Sprintf("Labels: %s\n", labels)))
|
|
|
|
}
|
2019-06-07 10:26:14 +02:00
|
|
|
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-06-07 17:14:40 +02:00
|
|
|
if pv.part.MIMEType == "text" {
|
|
|
|
scanner := bufio.NewScanner(part.Body)
|
|
|
|
for scanner.Scan() {
|
|
|
|
text := scanner.Text()
|
|
|
|
text = ansi.ReplaceAllString(text, "")
|
|
|
|
io.WriteString(pv.sink, text+"\n")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
io.Copy(pv.sink, part.Body)
|
|
|
|
}
|
2019-05-20 20:56:52 +02:00
|
|
|
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")
|
2019-11-15 21:28:34 +01:00
|
|
|
ctx.Printf(0, 2, tcell.StyleDefault,
|
|
|
|
"You can still :save the message or :pipe it to an external command")
|
|
|
|
pv.selecter.Focus(true)
|
|
|
|
pv.grid.Draw(ctx)
|
2019-05-20 22:42:44 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-15 11:01:47 +02:00
|
|
|
func (pv *PartViewer) Cleanup() {
|
|
|
|
if pv.pager != nil && pv.pager.Process != nil {
|
|
|
|
pv.pager.Process.Kill()
|
|
|
|
pv.pager = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 21:28:34 +01:00
|
|
|
func (pv *PartViewer) Event(event tcell.Event) bool {
|
|
|
|
if pv.term != nil {
|
|
|
|
return pv.term.Event(event)
|
|
|
|
}
|
|
|
|
return pv.selecter.Event(event)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|