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
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib"
|
|
|
|
"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 {
|
2019-06-27 19:33:11 +02:00
|
|
|
return []string{"open"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Open) Complete(aerc *widgets.Aerc, args []string) []string {
|
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
|
|
|
|
2020-03-06 16:33:44 +01:00
|
|
|
store := mv.Store()
|
2020-05-17 11:44:38 +02:00
|
|
|
store.FetchBodyPart(p.Msg.Uid, 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)
|
|
|
|
|
|
|
|
if exts, _ := mime.ExtensionsByType(mimeType); exts != nil && len(exts) > 0 {
|
|
|
|
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() {
|
|
|
|
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
|
|
|
|
}
|