2019-08-18 11:33:13 +02:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
2019-11-03 13:51:14 +01:00
|
|
|
"bytes"
|
2019-08-18 11:33:13 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-08-18 11:33:15 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2019-08-18 11:33:14 +02:00
|
|
|
"strings"
|
2019-11-03 13:51:14 +01:00
|
|
|
|
2020-06-19 17:58:08 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/lib"
|
2020-08-19 12:01:45 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/lib/format"
|
2019-11-03 13:51:14 +01:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
2020-04-24 22:31:39 +02:00
|
|
|
"git.sr.ht/~sircmpwn/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 {
|
2019-11-03 13:51:14 +01:00
|
|
|
opts, optind, err := getopt.Getopts(args, "AT:")
|
2019-08-18 11:33:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
attach := 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':
|
|
|
|
attach = true
|
2019-11-03 13:51:14 +01:00
|
|
|
case 'T':
|
|
|
|
template = opt.Value
|
2019-08-18 11:33:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:33:13 +02:00
|
|
|
widget := aerc.SelectedTab().(widgets.ProvidesMessage)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
acct.Logger().Println("Forwarding email " + msg.Envelope.MessageId)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
to := strings.Join(args[optind:], ", ")
|
|
|
|
tolist, err := mail.ParseAddressList(to)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid to address(es): %v", err)
|
|
|
|
}
|
|
|
|
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") {
|
2019-08-18 11:33:14 +02:00
|
|
|
composer.FocusRecipient()
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2019-08-18 11:33:15 +02:00
|
|
|
if attach {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "aerc-tmp-attachment")
|
|
|
|
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 {
|
|
|
|
println(err)
|
|
|
|
// TODO: Do something with the error
|
|
|
|
addTab()
|
|
|
|
return
|
|
|
|
}
|
2019-08-18 11:33:15 +02:00
|
|
|
|
2019-11-03 13:51:14 +01:00
|
|
|
defer tmpFile.Close()
|
2020-04-24 22:31:39 +02:00
|
|
|
io.Copy(tmpFile, fm.Content.Reader)
|
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
|
|
|
}
|
|
|
|
|
2019-11-03 13:51:14 +01:00
|
|
|
// TODO: add attachments!
|
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
|
|
|
}
|
|
|
|
store.FetchBodyPart(msg.Uid, part, func(reader io.Reader) {
|
2019-11-03 13:51:14 +01:00
|
|
|
buf := new(bytes.Buffer)
|
2020-01-04 21:13:51 +01:00
|
|
|
buf.ReadFrom(reader)
|
2020-01-08 21:44:14 +01:00
|
|
|
original.Text = buf.String()
|
2019-11-03 13:51:14 +01:00
|
|
|
addTab()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
2019-08-18 11:33:13 +02:00
|
|
|
}
|