2019-07-05 16:21:12 +00:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-07-08 22:19:08 +00:00
|
|
|
"os/exec"
|
2022-07-28 21:10:30 +00:00
|
|
|
"regexp"
|
2022-02-24 20:10:30 +00:00
|
|
|
"sort"
|
2022-07-28 21:10:30 +00:00
|
|
|
"strconv"
|
2020-05-28 14:32:32 +00:00
|
|
|
"time"
|
2019-07-05 16:21:12 +00:00
|
|
|
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/commands"
|
2022-03-22 08:52:27 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
2022-07-11 18:11:22 +00:00
|
|
|
mboxer "git.sr.ht/~rjarry/aerc/worker/mbox"
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2019-07-08 22:19:08 +00:00
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
2019-07-05 16:21:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Pipe struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(Pipe{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Pipe) Aliases() []string {
|
2019-07-05 16:21:12 +00:00
|
|
|
return []string{"pipe"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Pipe) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-07-05 16:21:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Pipe) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-07-05 16:21:12 +00:00
|
|
|
var (
|
2019-07-08 22:19:08 +00:00
|
|
|
background bool
|
|
|
|
pipeFull bool
|
|
|
|
pipePart bool
|
2019-07-05 16:21:12 +00:00
|
|
|
)
|
|
|
|
// TODO: let user specify part by index or preferred mimetype
|
2019-07-08 22:19:08 +00:00
|
|
|
opts, optind, err := getopt.Getopts(args, "bmp")
|
2019-07-05 16:21:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
2019-07-08 22:19:08 +00:00
|
|
|
case 'b':
|
|
|
|
background = true
|
2019-07-05 16:21:12 +00: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...]")
|
|
|
|
}
|
|
|
|
|
2022-07-18 10:54:55 +00:00
|
|
|
provider := aerc.SelectedTabContent().(widgets.ProvidesMessage)
|
2019-07-05 16:21:12 +00:00
|
|
|
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-08 22:19:08 +00:00
|
|
|
doTerm := func(reader io.Reader, name string) {
|
|
|
|
term, err := commands.QuickTerm(aerc, cmd, reader)
|
|
|
|
if err != nil {
|
2021-01-30 12:51:32 +00:00
|
|
|
aerc.PushError(err.Error())
|
2019-07-08 22:19:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
aerc.NewTab(term, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
doExec := func(reader io.Reader) {
|
|
|
|
ecmd := exec.Command(cmd[0], cmd[1:]...)
|
2019-07-08 22:50:40 +00:00
|
|
|
pipe, err := ecmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
2022-03-22 08:52:27 +00:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2019-07-08 22:50:40 +00:00
|
|
|
defer pipe.Close()
|
2022-07-29 20:31:54 +00:00
|
|
|
_, err := io.Copy(pipe, reader)
|
|
|
|
if err != nil {
|
|
|
|
logging.Errorf("failed to send data to pipe: %w", err)
|
|
|
|
}
|
2019-07-08 22:50:40 +00:00
|
|
|
}()
|
|
|
|
err = ecmd.Run()
|
2019-07-08 22:19:08 +00:00
|
|
|
if err != nil {
|
2021-01-30 12:51:32 +00:00
|
|
|
aerc.PushError(err.Error())
|
2019-07-08 22:19:08 +00:00
|
|
|
} else {
|
2019-07-08 22:32:31 +00:00
|
|
|
if ecmd.ProcessState.ExitCode() != 0 {
|
2020-07-27 08:03:55 +00: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-08 22:32:31 +00:00
|
|
|
}
|
2019-07-08 22:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 16:21:12 +00:00
|
|
|
if pipeFull {
|
2022-02-24 20:10:30 +00:00
|
|
|
var uids []uint32
|
|
|
|
var title string
|
|
|
|
|
|
|
|
h := newHelper(aerc)
|
|
|
|
store, err := h.store()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-14 07:42:24 +00:00
|
|
|
}
|
2022-02-24 20:10:30 +00:00
|
|
|
uids, err = h.markedOrSelectedUids()
|
2019-07-10 00:04:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-24 20:10:30 +00: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 08:52:27 +00:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2022-02-24 20:10:30 +00: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 21:10:30 +00:00
|
|
|
is_git_patches := true
|
|
|
|
for _, msg := range messages {
|
|
|
|
info := store.Messages[msg.Content.Uid]
|
|
|
|
if info == nil || !gitMessageIdRe.MatchString(info.Envelope.MessageId) {
|
|
|
|
is_git_patches = false
|
|
|
|
break
|
2022-02-24 20:10:30 +00:00
|
|
|
}
|
2022-07-28 21:10:30 +00:00
|
|
|
}
|
|
|
|
if is_git_patches {
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
msgidi := padGitMessageId(infoi.Envelope.MessageId)
|
|
|
|
msgidj := padGitMessageId(infoj.Envelope.MessageId)
|
|
|
|
return msgidi < msgidj
|
|
|
|
})
|
|
|
|
}
|
2022-02-24 20:10:30 +00:00
|
|
|
|
2022-07-11 18:11:22 +00:00
|
|
|
reader := newMessagesReader(messages, len(messages) > 1)
|
2019-07-08 22:19:08 +00:00
|
|
|
if background {
|
2022-02-24 20:10:30 +00:00
|
|
|
doExec(reader)
|
2019-07-08 22:19:08 +00:00
|
|
|
} else {
|
2022-02-24 20:10:30 +00:00
|
|
|
doTerm(reader, fmt.Sprintf("%s <%s", cmd[0], title))
|
2019-07-05 16:21:12 +00:00
|
|
|
}
|
2022-02-24 20:10:30 +00:00
|
|
|
}()
|
2019-07-05 16:21:12 +00:00
|
|
|
} else if pipePart {
|
2022-06-23 16:26:19 +00:00
|
|
|
mv, ok := provider.(*widgets.MessageViewer)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("can only pipe message part from a message view")
|
|
|
|
}
|
2019-07-05 16:21:12 +00:00
|
|
|
p := provider.SelectedMessagePart()
|
2020-07-05 12:27:21 +00:00
|
|
|
if p == nil {
|
|
|
|
return fmt.Errorf("could not fetch message part")
|
|
|
|
}
|
2022-06-23 16:26:19 +00:00
|
|
|
mv.MessageView().FetchBodyPart(p.Index, func(reader io.Reader) {
|
2019-07-08 22:19:08 +00: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 16:21:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-06-10 22:24:12 +00:00
|
|
|
provider.Store().ClearVisualMark()
|
2019-07-05 16:21:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-02-24 20:10:30 +00:00
|
|
|
|
2022-07-11 18:11:22 +00:00
|
|
|
func newMessagesReader(messages []*types.FullMessage, useMbox bool) io.Reader {
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer pw.Close()
|
|
|
|
for _, msg := range messages {
|
2022-07-29 20:31:54 +00:00
|
|
|
var err error
|
2022-07-11 18:11:22 +00:00
|
|
|
if useMbox {
|
2022-07-29 20:31:54 +00:00
|
|
|
err = mboxer.Write(pw, msg.Content.Reader, "", time.Now())
|
2022-07-11 18:11:22 +00:00
|
|
|
} else {
|
2022-07-29 20:31:54 +00:00
|
|
|
_, err = io.Copy(pw, msg.Content.Reader)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed to write data: %v", err)
|
2022-02-24 20:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-11 18:11:22 +00:00
|
|
|
}()
|
|
|
|
return pr
|
2022-02-24 20:10:30 +00:00
|
|
|
}
|
2022-07-28 21:10:30 +00:00
|
|
|
|
|
|
|
var gitMessageIdRe = regexp.MustCompile(`^(\d+\.\d+)-(\d+)-(.+)$`)
|
|
|
|
|
|
|
|
// Git send-email Message-Id headers have the following format:
|
2022-08-04 08:34:21 +00:00
|
|
|
//
|
|
|
|
// DATETIME.PID-NUM-COMMITTER
|
|
|
|
//
|
2022-07-28 21:10:30 +00:00
|
|
|
// Return a copy of the message id with NUM zero-padded to three characters.
|
|
|
|
func padGitMessageId(msgId string) string {
|
|
|
|
matches := gitMessageIdRe.FindStringSubmatch(msgId)
|
|
|
|
if matches == nil {
|
|
|
|
return msgId
|
|
|
|
}
|
|
|
|
number, err := strconv.Atoi(matches[2])
|
|
|
|
if err != nil {
|
|
|
|
return msgId
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s-%03d-%s", matches[1], number, matches[3])
|
|
|
|
}
|