2019-08-18 11:33:13 +02:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-08-18 11:33:15 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/lib"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-08-18 11:33:13 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
2019-08-18 11:33:15 +02:00
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
2019-08-18 11:33:13 +02:00
|
|
|
"github.com/emersion/go-message"
|
|
|
|
"github.com/emersion/go-message/mail"
|
|
|
|
"io"
|
2019-08-18 11:33:15 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2019-08-18 11:33:14 +02:00
|
|
|
"strings"
|
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-08-18 11:33:15 +02:00
|
|
|
opts, optind, err := getopt.Getopts(args, "A")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
attach := false
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 'A':
|
|
|
|
attach = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:33:14 +02:00
|
|
|
to := ""
|
2019-08-18 11:33:13 +02:00
|
|
|
if len(args) != 1 {
|
2019-08-18 11:33:15 +02:00
|
|
|
to = strings.Join(args[optind:], ", ")
|
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)
|
|
|
|
|
|
|
|
subject := "Fwd: " + msg.Envelope.Subject
|
|
|
|
defaults := map[string]string{
|
2019-08-18 11:33:15 +02:00
|
|
|
"To": to,
|
2019-08-18 11:33:13 +02:00
|
|
|
"Subject": subject,
|
|
|
|
}
|
2019-09-11 21:28:14 +02:00
|
|
|
composer := widgets.NewComposer(aerc, aerc.Config(), acct.AccountConfig(),
|
2019-08-18 11:33:13 +02:00
|
|
|
acct.Worker(), defaults)
|
|
|
|
|
|
|
|
addTab := func() {
|
|
|
|
tab := aerc.NewTab(composer, subject)
|
2019-08-18 11:33:15 +02:00
|
|
|
if 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-08-18 11:33:15 +02:00
|
|
|
if attach {
|
|
|
|
forwardAttach(store, composer, msg, addTab)
|
|
|
|
} else {
|
|
|
|
forwardBodyPart(store, composer, msg, addTab)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func forwardAttach(store *lib.MessageStore, composer *widgets.Composer,
|
|
|
|
msg *models.MessageInfo, addTab func()) {
|
|
|
|
|
|
|
|
store.FetchFull([]uint32{msg.Uid}, func(reader io.Reader) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "aerc-tmp-attachment")
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Do something with the error
|
|
|
|
addTab()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tmpFileName := path.Join(tmpDir,
|
|
|
|
strings.ReplaceAll(fmt.Sprintf("%s.eml", msg.Envelope.Subject), "/", "-"))
|
|
|
|
tmpFile, err := os.Create(tmpFileName)
|
|
|
|
if err != nil {
|
|
|
|
println(err)
|
|
|
|
// TODO: Do something with the error
|
|
|
|
addTab()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer tmpFile.Close()
|
|
|
|
io.Copy(tmpFile, reader)
|
|
|
|
composer.AddAttachment(tmpFileName)
|
|
|
|
composer.OnClose(func(composer *widgets.Composer) {
|
|
|
|
os.RemoveAll(tmpDir)
|
|
|
|
})
|
|
|
|
addTab()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func forwardBodyPart(store *lib.MessageStore, composer *widgets.Composer,
|
|
|
|
msg *models.MessageInfo, addTab func()) {
|
2019-08-18 11:33:13 +02:00
|
|
|
// TODO: something more intelligent than fetching the 1st part
|
|
|
|
// TODO: add attachments!
|
|
|
|
store.FetchBodyPart(msg.Uid, []int{1}, func(reader io.Reader) {
|
|
|
|
header := message.Header{}
|
|
|
|
header.SetText(
|
|
|
|
"Content-Transfer-Encoding", msg.BodyStructure.Encoding)
|
|
|
|
header.SetContentType(
|
|
|
|
msg.BodyStructure.MIMEType, msg.BodyStructure.Params)
|
|
|
|
header.SetText("Content-Description", msg.BodyStructure.Description)
|
|
|
|
entity, err := message.New(header, reader)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Do something with the error
|
|
|
|
addTab()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mreader := mail.NewReader(entity)
|
|
|
|
part, err := mreader.NextPart()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Do something with the error
|
|
|
|
addTab()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeout, pipein := io.Pipe()
|
|
|
|
scanner := bufio.NewScanner(part.Body)
|
2019-09-11 21:28:14 +02:00
|
|
|
go composer.PrependContents(pipeout)
|
2019-08-18 11:33:13 +02:00
|
|
|
// TODO: Let user customize the date format used here
|
|
|
|
io.WriteString(pipein, fmt.Sprintf("Forwarded message from %s on %s:\n\n",
|
|
|
|
msg.Envelope.From[0].Name,
|
|
|
|
msg.Envelope.Date.Format("Mon Jan 2, 2006 at 3:04 PM")))
|
|
|
|
for scanner.Scan() {
|
|
|
|
io.WriteString(pipein, fmt.Sprintf("%s\n", scanner.Text()))
|
|
|
|
}
|
|
|
|
pipein.Close()
|
|
|
|
pipeout.Close()
|
|
|
|
addTab()
|
|
|
|
})
|
|
|
|
}
|