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