2019-08-18 11:33:13 +02:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
2022-06-28 23:42:09 +02:00
|
|
|
"bufio"
|
2019-11-03 13:51:14 +01:00
|
|
|
"bytes"
|
2019-08-18 11:33:13 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-06-28 23:42:08 +02:00
|
|
|
"math/rand"
|
2019-08-18 11:33:15 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
2019-08-18 11:33:14 +02:00
|
|
|
"strings"
|
2022-06-28 23:42:08 +02:00
|
|
|
"sync"
|
2019-11-03 13:51:14 +01:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib"
|
|
|
|
"git.sr.ht/~rjarry/aerc/lib/format"
|
2022-07-19 22:31:51 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2020-11-10 20:27:30 +01:00
|
|
|
"github.com/emersion/go-message/mail"
|
2020-04-24 22:31:39 +02:00
|
|
|
|
2019-11-03 13:51:14 +01:00
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
2019-08-18 11:33:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type forward struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(forward{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (forward) Aliases() []string {
|
2019-08-18 11:33:13 +02:00
|
|
|
return []string{"forward"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (forward) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-08-18 11:33:13 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (forward) Execute(aerc *widgets.Aerc, args []string) error {
|
2022-06-28 23:42:08 +02:00
|
|
|
opts, optind, err := getopt.Getopts(args, "AFT:")
|
2019-08-18 11:33:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-28 23:42:08 +02:00
|
|
|
attachAll := false
|
|
|
|
attachFull := false
|
2019-11-03 13:51:14 +01:00
|
|
|
template := ""
|
2019-08-18 11:33:15 +02:00
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 'A':
|
2022-06-28 23:42:08 +02:00
|
|
|
attachAll = true
|
|
|
|
case 'F':
|
|
|
|
attachFull = true
|
2019-11-03 13:51:14 +01:00
|
|
|
case 'T':
|
|
|
|
template = opt.Value
|
2019-08-18 11:33:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 23:42:08 +02:00
|
|
|
if attachAll && attachFull {
|
|
|
|
return errors.New("Options -A and -F are mutually exclusive")
|
|
|
|
}
|
|
|
|
|
2022-07-18 12:54:55 +02:00
|
|
|
widget := aerc.SelectedTabContent().(widgets.ProvidesMessage)
|
2019-08-18 11:33:13 +02:00
|
|
|
acct := widget.SelectedAccount()
|
|
|
|
if acct == nil {
|
|
|
|
return errors.New("No account selected")
|
|
|
|
}
|
|
|
|
store := widget.Store()
|
|
|
|
if store == nil {
|
|
|
|
return errors.New("Cannot perform action. Messages still loading")
|
|
|
|
}
|
|
|
|
msg, err := widget.SelectedMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Infof("Forwarding email %s", msg.Envelope.MessageId)
|
2019-08-18 11:33:13 +02:00
|
|
|
|
2020-11-10 20:27:30 +01:00
|
|
|
h := &mail.Header{}
|
2019-08-18 11:33:13 +02:00
|
|
|
subject := "Fwd: " + msg.Envelope.Subject
|
2020-11-10 20:27:30 +01:00
|
|
|
h.SetSubject(subject)
|
|
|
|
|
2022-06-28 23:42:08 +02:00
|
|
|
var tolist []*mail.Address
|
|
|
|
to := strings.Join(args[optind:], ", ")
|
|
|
|
if strings.Contains(to, "@") {
|
|
|
|
tolist, err = mail.ParseAddressList(to)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("invalid to address(es): %w", err)
|
2022-06-28 23:42:08 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-14 19:13:44 +02:00
|
|
|
if len(tolist) > 0 {
|
2020-11-10 20:27:30 +01:00
|
|
|
h.SetAddressList("to", tolist)
|
2019-08-18 11:33:13 +02:00
|
|
|
}
|
2020-11-10 20:27:30 +01:00
|
|
|
|
2020-11-03 07:39:36 +01:00
|
|
|
original := models.OriginalMail{
|
|
|
|
From: format.FormatAddresses(msg.Envelope.From),
|
|
|
|
Date: msg.Envelope.Date,
|
|
|
|
RFC822Headers: msg.RFC822Headers,
|
|
|
|
}
|
2020-01-08 21:44:14 +01:00
|
|
|
|
2019-11-03 13:51:14 +01:00
|
|
|
addTab := func() (*widgets.Composer, error) {
|
2020-11-10 20:27:30 +01:00
|
|
|
composer, err := widgets.NewComposer(aerc, acct, aerc.Config(),
|
|
|
|
acct.AccountConfig(), acct.Worker(), template, h, original)
|
2019-11-03 13:51:14 +01:00
|
|
|
if err != nil {
|
2020-05-28 16:32:42 +02:00
|
|
|
aerc.PushError("Error: " + err.Error())
|
2019-11-03 13:51:14 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:33:13 +02:00
|
|
|
tab := aerc.NewTab(composer, subject)
|
2020-11-10 20:27:30 +01:00
|
|
|
if !h.Has("to") {
|
2022-03-22 00:23:47 +01:00
|
|
|
composer.FocusEditor("to")
|
2019-08-18 11:33:14 +02:00
|
|
|
} else {
|
|
|
|
composer.FocusTerminal()
|
|
|
|
}
|
2019-08-18 11:33:13 +02:00
|
|
|
composer.OnHeaderChange("Subject", func(subject string) {
|
|
|
|
if subject == "" {
|
|
|
|
tab.Name = "New email"
|
|
|
|
} else {
|
|
|
|
tab.Name = subject
|
|
|
|
}
|
|
|
|
tab.Content.Invalidate()
|
|
|
|
})
|
2019-11-03 13:51:14 +01:00
|
|
|
return composer, nil
|
2019-08-18 11:33:13 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 23:42:08 +02:00
|
|
|
if attachFull {
|
2022-08-17 16:19:45 +02:00
|
|
|
tmpDir, err := os.MkdirTemp("", "aerc-tmp-attachment")
|
2019-08-18 11:33:15 +02:00
|
|
|
if err != nil {
|
2019-11-03 13:51:14 +01:00
|
|
|
return err
|
2019-08-18 11:33:15 +02:00
|
|
|
}
|
|
|
|
tmpFileName := path.Join(tmpDir,
|
|
|
|
strings.ReplaceAll(fmt.Sprintf("%s.eml", msg.Envelope.Subject), "/", "-"))
|
2020-04-24 22:31:39 +02:00
|
|
|
store.FetchFull([]uint32{msg.Uid}, func(fm *types.FullMessage) {
|
2019-11-03 13:51:14 +01:00
|
|
|
tmpFile, err := os.Create(tmpFileName)
|
|
|
|
if err != nil {
|
2022-07-29 22:31:54 +02:00
|
|
|
logging.Warnf("failed to create temporary attachment: %v", err)
|
|
|
|
_, err = addTab()
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed to add tab: %v", err)
|
|
|
|
}
|
2019-11-03 13:51:14 +01:00
|
|
|
return
|
|
|
|
}
|
2019-08-18 11:33:15 +02:00
|
|
|
|
2019-11-03 13:51:14 +01:00
|
|
|
defer tmpFile.Close()
|
2022-07-29 22:31:54 +02:00
|
|
|
_, err = io.Copy(tmpFile, fm.Content.Reader)
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed to write to tmpfile: %w", err)
|
|
|
|
return
|
|
|
|
}
|
2019-11-03 13:51:14 +01:00
|
|
|
composer, err := addTab()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
composer.AddAttachment(tmpFileName)
|
2021-02-26 22:10:26 +01:00
|
|
|
composer.OnClose(func(_ *widgets.Composer) {
|
2019-11-03 13:51:14 +01:00
|
|
|
os.RemoveAll(tmpDir)
|
|
|
|
})
|
2019-08-18 11:33:15 +02:00
|
|
|
})
|
2019-11-03 13:51:14 +01:00
|
|
|
} else {
|
|
|
|
if template == "" {
|
|
|
|
template = aerc.Config().Templates.Forwards
|
2019-08-18 11:33:13 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:58:08 +02:00
|
|
|
part := lib.FindPlaintext(msg.BodyStructure, nil)
|
2020-05-17 13:02:24 +02:00
|
|
|
if part == nil {
|
2020-06-19 17:58:08 +02:00
|
|
|
part = lib.FindFirstNonMultipart(msg.BodyStructure, nil)
|
|
|
|
// if it's still nil here, we don't have a multipart msg, that's fine
|
2020-05-17 13:02:24 +02:00
|
|
|
}
|
2021-02-26 22:08:49 +01:00
|
|
|
err = addMimeType(msg, part, &original)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-17 13:02:24 +02:00
|
|
|
store.FetchBodyPart(msg.Uid, part, func(reader io.Reader) {
|
2019-11-03 13:51:14 +01:00
|
|
|
buf := new(bytes.Buffer)
|
2022-06-28 23:42:09 +02:00
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
for scanner.Scan() {
|
|
|
|
buf.WriteString(scanner.Text() + "\n")
|
|
|
|
}
|
2020-01-08 21:44:14 +01:00
|
|
|
original.Text = buf.String()
|
2022-06-28 23:42:08 +02:00
|
|
|
|
|
|
|
// create composer
|
|
|
|
composer, err := addTab()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add attachments
|
|
|
|
if attachAll {
|
|
|
|
var mu sync.Mutex
|
|
|
|
parts := lib.FindAllNonMultipart(msg.BodyStructure, nil, nil)
|
|
|
|
for _, p := range parts {
|
|
|
|
if lib.EqualParts(p, part) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bs, err := msg.BodyStructure.PartAtIndex(p)
|
|
|
|
if err != nil {
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Errorf("cannot get PartAtIndex %v: %v", p, err)
|
2022-06-28 23:42:08 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
store.FetchBodyPart(msg.Uid, p, func(reader io.Reader) {
|
|
|
|
mime := fmt.Sprintf("%s/%s", bs.MIMEType, bs.MIMESubType)
|
|
|
|
name, ok := bs.Params["name"]
|
|
|
|
if !ok {
|
|
|
|
name = fmt.Sprintf("%s_%s_%d", bs.MIMEType, bs.MIMESubType, rand.Uint64())
|
|
|
|
}
|
|
|
|
mu.Lock()
|
|
|
|
composer.AddPartAttachment(name, mime, bs.Params, reader)
|
|
|
|
mu.Unlock()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-11-03 13:51:14 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
2019-08-18 11:33:13 +02:00
|
|
|
}
|