2019-06-16 20:52:44 +00:00
|
|
|
package msgview
|
|
|
|
|
|
|
|
import (
|
2020-04-02 13:54:45 +00:00
|
|
|
"fmt"
|
2019-06-16 20:52:44 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2020-04-02 13:54:45 +00:00
|
|
|
"mime"
|
2019-06-16 20:52:44 +00:00
|
|
|
"os"
|
2020-05-28 14:32:32 +00:00
|
|
|
"time"
|
2019-06-16 20:52:44 +00:00
|
|
|
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib"
|
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
2019-06-16 20:52:44 +00:00
|
|
|
)
|
|
|
|
|
2019-06-27 17:33:11 +00:00
|
|
|
type Open struct{}
|
|
|
|
|
2019-06-16 20:52:44 +00:00
|
|
|
func init() {
|
2019-06-27 17:33:11 +00:00
|
|
|
register(Open{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Open) Aliases() []string {
|
2019-06-27 17:33:11 +00:00
|
|
|
return []string{"open"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Open) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-06-27 17:33:11 +00:00
|
|
|
return nil
|
2019-06-16 20:52:44 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Open) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-06-16 20:52:44 +00:00
|
|
|
mv := aerc.SelectedTab().(*widgets.MessageViewer)
|
2019-07-05 16:21:12 +00:00
|
|
|
p := mv.SelectedMessagePart()
|
2019-06-16 20:52:44 +00:00
|
|
|
|
2020-03-06 15:33:44 +00:00
|
|
|
store := mv.Store()
|
2020-05-17 09:44:38 +00:00
|
|
|
store.FetchBodyPart(p.Msg.Uid, p.Index, func(reader io.Reader) {
|
2020-04-02 13:54:45 +00: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 20:52:44 +00:00
|
|
|
if err != nil {
|
2021-01-30 12:51:32 +00:00
|
|
|
aerc.PushError(err.Error())
|
2019-06-16 20:52:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer tmpFile.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(tmpFile, reader)
|
|
|
|
if err != nil {
|
2021-01-30 12:51:32 +00:00
|
|
|
aerc.PushError(err.Error())
|
2019-06-16 20:52:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-30 10:33:31 +00: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 12:51:32 +00:00
|
|
|
aerc.PushError(err.Error())
|
2021-01-30 10:33:31 +00:00
|
|
|
}
|
|
|
|
}()
|
2019-06-16 20:52:44 +00:00
|
|
|
|
2020-05-28 14:32:32 +00:00
|
|
|
aerc.PushStatus("Opened", 10*time.Second)
|
2019-06-16 20:52:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|