2019-06-16 22:52:44 +02:00
|
|
|
package msgview
|
|
|
|
|
|
|
|
import (
|
2020-04-02 15:54:45 +02:00
|
|
|
"fmt"
|
2019-06-16 22:52:44 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2020-04-02 15:54:45 +02:00
|
|
|
"mime"
|
2019-06-16 22:52:44 +02:00
|
|
|
"os"
|
2020-05-28 16:32:32 +02:00
|
|
|
"time"
|
2019-06-16 22:52:44 +02:00
|
|
|
|
2022-06-14 21:10:48 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/commands"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib"
|
2022-03-22 09:52:27 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
2019-06-16 22:52:44 +02:00
|
|
|
)
|
|
|
|
|
2019-06-27 19:33:11 +02:00
|
|
|
type Open struct{}
|
|
|
|
|
2019-06-16 22:52:44 +02:00
|
|
|
func init() {
|
2019-06-27 19:33:11 +02:00
|
|
|
register(Open{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Open) Aliases() []string {
|
2022-06-14 21:10:48 +02:00
|
|
|
return []string{"open", "open-link"}
|
2019-06-27 19:33:11 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Open) Complete(aerc *widgets.Aerc, args []string) []string {
|
2022-06-14 21:10:48 +02:00
|
|
|
mv := aerc.SelectedTab().(*widgets.MessageViewer)
|
|
|
|
if mv != nil {
|
|
|
|
if p := mv.SelectedMessagePart(); p != nil {
|
|
|
|
return commands.CompletionFromList(aerc, p.Links, args)
|
|
|
|
}
|
|
|
|
}
|
2019-06-27 19:33:11 +02:00
|
|
|
return nil
|
2019-06-16 22:52:44 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Open) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-06-16 22:52:44 +02:00
|
|
|
mv := aerc.SelectedTab().(*widgets.MessageViewer)
|
2019-07-05 18:21:12 +02:00
|
|
|
p := mv.SelectedMessagePart()
|
2019-06-16 22:52:44 +02:00
|
|
|
|
2022-06-14 21:10:48 +02:00
|
|
|
if args[0] == "open-link" && len(args) > 1 {
|
|
|
|
if link := args[1]; link != "" {
|
|
|
|
go func() {
|
|
|
|
if err := lib.NewXDGOpen(link).Start(); err != nil {
|
|
|
|
aerc.PushError(fmt.Sprintf("%s: %s", args[0], err.Error()))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-23 18:26:19 +02:00
|
|
|
mv.MessageView().FetchBodyPart(p.Index, func(reader io.Reader) {
|
2020-04-02 15:54:45 +02:00
|
|
|
extension := ""
|
|
|
|
// try to determine the correct extension based on mimetype
|
|
|
|
if part, err := p.Msg.BodyStructure.PartAtIndex(p.Index); err == nil {
|
|
|
|
mimeType := fmt.Sprintf("%s/%s", part.MIMEType, part.MIMESubType)
|
|
|
|
|
2022-03-09 22:48:00 +01:00
|
|
|
if exts, _ := mime.ExtensionsByType(mimeType); len(exts) > 0 {
|
2020-04-02 15:54:45 +02:00
|
|
|
extension = exts[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpFile, err := ioutil.TempFile(os.TempDir(), "aerc-*"+extension)
|
2019-06-16 22:52:44 +02:00
|
|
|
if err != nil {
|
2021-01-30 13:51:32 +01:00
|
|
|
aerc.PushError(err.Error())
|
2019-06-16 22:52:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer tmpFile.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(tmpFile, reader)
|
|
|
|
if err != nil {
|
2021-01-30 13:51:32 +01:00
|
|
|
aerc.PushError(err.Error())
|
2019-06-16 22:52:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-30 11:33:31 +01:00
|
|
|
xdg := lib.NewXDGOpen(tmpFile.Name())
|
|
|
|
// pass through any arguments the user provided to the underlying handler
|
|
|
|
if len(args) > 1 {
|
|
|
|
xdg.SetArgs(args[1:])
|
|
|
|
}
|
|
|
|
err = xdg.Start()
|
|
|
|
if err != nil {
|
|
|
|
aerc.PushError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
2022-03-22 09:52:27 +01:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2021-01-30 11:33:31 +01:00
|
|
|
err := xdg.Wait()
|
|
|
|
if err != nil {
|
2021-01-30 13:51:32 +01:00
|
|
|
aerc.PushError(err.Error())
|
2021-01-30 11:33:31 +01:00
|
|
|
}
|
|
|
|
}()
|
2019-06-16 22:52:44 +02:00
|
|
|
|
2020-05-28 16:32:32 +02:00
|
|
|
aerc.PushStatus("Opened", 10*time.Second)
|
2019-06-16 22:52:44 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|