2019-06-16 22:52:44 +02:00
|
|
|
package msgview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
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
|
|
|
|
2019-07-03 00:21:34 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/lib"
|
2019-06-16 22:52:44 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
if len(args) != 1 {
|
|
|
|
return errors.New("Usage: open")
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-05-28 16:32:42 +02: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 {
|
2020-05-28 16:32:42 +02:00
|
|
|
aerc.PushError(" " + err.Error())
|
2019-06-16 22:52:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:14:15 +02:00
|
|
|
lib.OpenFile(tmpFile.Name(), func(err error) {
|
2020-05-28 16:32:42 +02:00
|
|
|
aerc.PushError(" " + err.Error())
|
2020-07-06 21:14:15 +02: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
|
|
|
|
}
|