2019-07-05 18:21:12 +02:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-07-09 00:19:08 +02:00
|
|
|
"os/exec"
|
2022-02-24 21:10:30 +01:00
|
|
|
"sort"
|
2020-05-28 16:32:32 +02:00
|
|
|
"time"
|
2019-07-05 18:21:12 +02:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/commands"
|
2022-03-22 09:52:27 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2019-07-09 00:19:08 +02:00
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
2019-07-05 18:21:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Pipe struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(Pipe{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Pipe) Aliases() []string {
|
2019-07-05 18:21:12 +02:00
|
|
|
return []string{"pipe"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Pipe) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-07-05 18:21:12 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Pipe) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-07-05 18:21:12 +02:00
|
|
|
var (
|
2019-07-09 00:19:08 +02:00
|
|
|
background bool
|
|
|
|
pipeFull bool
|
|
|
|
pipePart bool
|
2019-07-05 18:21:12 +02:00
|
|
|
)
|
|
|
|
// TODO: let user specify part by index or preferred mimetype
|
2019-07-09 00:19:08 +02:00
|
|
|
opts, optind, err := getopt.Getopts(args, "bmp")
|
2019-07-05 18:21:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
2019-07-09 00:19:08 +02:00
|
|
|
case 'b':
|
|
|
|
background = true
|
2019-07-05 18:21:12 +02:00
|
|
|
case 'm':
|
|
|
|
if pipePart {
|
|
|
|
return errors.New("-m and -p are mutually exclusive")
|
|
|
|
}
|
|
|
|
pipeFull = true
|
|
|
|
case 'p':
|
|
|
|
if pipeFull {
|
|
|
|
return errors.New("-m and -p are mutually exclusive")
|
|
|
|
}
|
|
|
|
pipePart = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmd := args[optind:]
|
|
|
|
if len(cmd) == 0 {
|
|
|
|
return errors.New("Usage: pipe [-mp] <cmd> [args...]")
|
|
|
|
}
|
|
|
|
|
|
|
|
provider := aerc.SelectedTab().(widgets.ProvidesMessage)
|
|
|
|
if !pipeFull && !pipePart {
|
|
|
|
if _, ok := provider.(*widgets.MessageViewer); ok {
|
|
|
|
pipePart = true
|
|
|
|
} else if _, ok := provider.(*widgets.AccountView); ok {
|
|
|
|
pipeFull = true
|
|
|
|
} else {
|
|
|
|
return errors.New(
|
|
|
|
"Neither -m nor -p specified and cannot infer default")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 00:19:08 +02:00
|
|
|
doTerm := func(reader io.Reader, name string) {
|
|
|
|
term, err := commands.QuickTerm(aerc, cmd, reader)
|
|
|
|
if err != nil {
|
2021-01-30 13:51:32 +01:00
|
|
|
aerc.PushError(err.Error())
|
2019-07-09 00:19:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
aerc.NewTab(term, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
doExec := func(reader io.Reader) {
|
|
|
|
ecmd := exec.Command(cmd[0], cmd[1:]...)
|
2019-07-09 00:50:40 +02:00
|
|
|
pipe, err := ecmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
2022-03-22 09:52:27 +01:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2019-07-09 00:50:40 +02:00
|
|
|
defer pipe.Close()
|
|
|
|
io.Copy(pipe, reader)
|
|
|
|
}()
|
|
|
|
err = ecmd.Run()
|
2019-07-09 00:19:08 +02:00
|
|
|
if err != nil {
|
2021-01-30 13:51:32 +01:00
|
|
|
aerc.PushError(err.Error())
|
2019-07-09 00:19:08 +02:00
|
|
|
} else {
|
2019-07-09 00:32:31 +02:00
|
|
|
if ecmd.ProcessState.ExitCode() != 0 {
|
2020-07-27 10:03:55 +02:00
|
|
|
aerc.PushError(fmt.Sprintf(
|
|
|
|
"%s: completed with status %d", cmd[0],
|
|
|
|
ecmd.ProcessState.ExitCode()))
|
|
|
|
} else {
|
|
|
|
aerc.PushStatus(fmt.Sprintf(
|
|
|
|
"%s: completed with status %d", cmd[0],
|
|
|
|
ecmd.ProcessState.ExitCode()), 10*time.Second)
|
2019-07-09 00:32:31 +02:00
|
|
|
}
|
2019-07-09 00:19:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 18:21:12 +02:00
|
|
|
if pipeFull {
|
2022-02-24 21:10:30 +01:00
|
|
|
var uids []uint32
|
|
|
|
var title string
|
|
|
|
|
|
|
|
h := newHelper(aerc)
|
|
|
|
store, err := h.store()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-14 09:42:24 +02:00
|
|
|
}
|
2022-02-24 21:10:30 +01:00
|
|
|
uids, err = h.markedOrSelectedUids()
|
2019-07-10 02:04:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-24 21:10:30 +01:00
|
|
|
|
|
|
|
if len(uids) == 1 {
|
|
|
|
info := store.Messages[uids[0]]
|
|
|
|
if info != nil {
|
|
|
|
envelope := info.Envelope
|
|
|
|
if envelope != nil {
|
|
|
|
title = envelope.Subject
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if title == "" {
|
|
|
|
title = fmt.Sprintf("%d messages", len(uids))
|
|
|
|
}
|
|
|
|
|
|
|
|
var messages []*types.FullMessage
|
|
|
|
done := make(chan bool, 1)
|
|
|
|
|
|
|
|
store.FetchFull(uids, func(fm *types.FullMessage) {
|
|
|
|
messages = append(messages, fm)
|
|
|
|
if len(messages) == len(uids) {
|
|
|
|
done <- true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
2022-03-22 09:52:27 +01:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2022-02-24 21:10:30 +01:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
break
|
|
|
|
case <-time.After(30 * time.Second):
|
|
|
|
// TODO: find a better way to determine if store.FetchFull()
|
|
|
|
// has finished with some errors.
|
|
|
|
aerc.PushError("Failed to fetch all messages")
|
|
|
|
if len(messages) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort all messages by increasing Message-Id header.
|
|
|
|
// This will ensure that patch series are applied in order.
|
|
|
|
sort.Slice(messages, func(i, j int) bool {
|
|
|
|
infoi := store.Messages[messages[i].Content.Uid]
|
|
|
|
infoj := store.Messages[messages[j].Content.Uid]
|
|
|
|
if infoi == nil || infoj == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return infoi.Envelope.MessageId < infoj.Envelope.MessageId
|
|
|
|
})
|
|
|
|
|
|
|
|
reader := newMessagesReader(messages)
|
2019-07-09 00:19:08 +02:00
|
|
|
if background {
|
2022-02-24 21:10:30 +01:00
|
|
|
doExec(reader)
|
2019-07-09 00:19:08 +02:00
|
|
|
} else {
|
2022-02-24 21:10:30 +01:00
|
|
|
doTerm(reader, fmt.Sprintf("%s <%s", cmd[0], title))
|
2019-07-05 18:21:12 +02:00
|
|
|
}
|
2022-02-24 21:10:30 +01:00
|
|
|
}()
|
2019-07-05 18:21:12 +02:00
|
|
|
} else if pipePart {
|
|
|
|
p := provider.SelectedMessagePart()
|
2020-07-05 14:27:21 +02:00
|
|
|
if p == nil {
|
|
|
|
return fmt.Errorf("could not fetch message part")
|
|
|
|
}
|
2020-03-06 16:33:44 +01:00
|
|
|
store := provider.Store()
|
2020-05-17 11:44:38 +02:00
|
|
|
store.FetchBodyPart(p.Msg.Uid, p.Index, func(reader io.Reader) {
|
2019-07-09 00:19:08 +02:00
|
|
|
if background {
|
|
|
|
doExec(reader)
|
|
|
|
} else {
|
|
|
|
name := fmt.Sprintf("%s <%s/[%d]",
|
|
|
|
cmd[0], p.Msg.Envelope.Subject, p.Index)
|
|
|
|
doTerm(reader, name)
|
2019-07-05 18:21:12 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-06-11 00:24:12 +02:00
|
|
|
provider.Store().ClearVisualMark()
|
2019-07-05 18:21:12 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-02-24 21:10:30 +01:00
|
|
|
|
|
|
|
// The actual sender address does not matter, nor does the date. This is mostly indended
|
|
|
|
// for git am which requires separators to look like something valid.
|
|
|
|
// https://github.com/git/git/blame/v2.35.1/builtin/mailsplit.c#L15-L44
|
|
|
|
var mboxSeparator []byte = []byte("From ???@??? Tue Jun 23 16:32:49 1981\n")
|
|
|
|
|
|
|
|
type messagesReader struct {
|
|
|
|
messages []*types.FullMessage
|
|
|
|
mbox bool
|
|
|
|
separatorNeeded bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMessagesReader(messages []*types.FullMessage) io.Reader {
|
|
|
|
needMboxSeparator := len(messages) > 1
|
|
|
|
return &messagesReader{messages, needMboxSeparator, needMboxSeparator}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mr *messagesReader) Read(p []byte) (n int, err error) {
|
|
|
|
for len(mr.messages) > 0 {
|
|
|
|
if mr.separatorNeeded {
|
|
|
|
offset := copy(p, mboxSeparator)
|
|
|
|
n, err = mr.messages[0].Content.Reader.Read(p[offset:])
|
|
|
|
n += offset
|
|
|
|
mr.separatorNeeded = false
|
|
|
|
} else {
|
|
|
|
n, err = mr.messages[0].Content.Reader.Read(p)
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
mr.messages = mr.messages[1:]
|
|
|
|
mr.separatorNeeded = mr.mbox
|
|
|
|
}
|
|
|
|
if n > 0 || err != io.EOF {
|
|
|
|
if err == io.EOF && len(mr.messages) > 0 {
|
|
|
|
// Don't return EOF yet. More messages remain.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|