2019-06-16 22:52:44 +02:00
|
|
|
package msgview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2020-04-02 15:54:45 +02:00
|
|
|
"mime"
|
2019-06-16 22:52:44 +02:00
|
|
|
"os"
|
|
|
|
|
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"
|
|
|
|
"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-07-18 12:54:55 +02:00
|
|
|
mv := aerc.SelectedTabContent().(*widgets.MessageViewer)
|
2022-06-14 21:10:48 +02:00
|
|
|
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 {
|
2022-07-18 12:54:55 +02:00
|
|
|
mv := aerc.SelectedTabContent().(*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() {
|
2022-09-30 10:52:49 +02:00
|
|
|
if err := lib.XDGOpen(link); err != nil {
|
|
|
|
aerc.PushError("open: " + err.Error())
|
2022-06-14 21:10:48 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
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 := ""
|
2022-09-30 14:12:07 +02:00
|
|
|
mimeType := ""
|
|
|
|
|
2020-04-02 15:54:45 +02:00
|
|
|
// try to determine the correct extension based on mimetype
|
2022-06-25 14:40:10 +02:00
|
|
|
if part, err := mv.MessageView().BodyStructure().PartAtIndex(p.Index); err == nil {
|
2022-10-12 23:52:45 +02:00
|
|
|
mimeType = part.FullMIMEType()
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:19:45 +02:00
|
|
|
tmpFile, err := os.CreateTemp(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
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(tmpFile, reader)
|
2022-09-30 10:52:49 +02:00
|
|
|
tmpFile.Close()
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-30 11:33:31 +01:00
|
|
|
go func() {
|
2022-09-30 14:12:07 +02:00
|
|
|
openers := aerc.Config().Openers
|
|
|
|
err = lib.XDGOpenMime(tmpFile.Name(), mimeType, openers, args[1:])
|
2021-01-30 11:33:31 +01:00
|
|
|
if err != nil {
|
2022-09-30 10:52:49 +02:00
|
|
|
aerc.PushError("open: " + err.Error())
|
2021-01-30 11:33:31 +01:00
|
|
|
}
|
|
|
|
}()
|
2019-06-16 22:52:44 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|