| 1 |
package commands |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
|
| 9 |
"git.sr.ht/~rjarry/aerc/app" |
| 10 |
"git.sr.ht/~rjarry/aerc/lib" |
| 11 |
"git.sr.ht/~rjarry/aerc/lib/xdg" |
| 12 |
) |
| 13 |
|
| 14 |
type Eml struct { |
| 15 |
Path string `opt:"path" required:"false" complete:"CompletePath" desc:"EML file path."` |
| 16 |
} |
| 17 |
|
| 18 |
func init() { |
| 19 |
Register(Eml{}) |
| 20 |
} |
| 21 |
|
| 22 |
func (Eml) Description() string { |
| 23 |
return "Open an eml file into the message viewer." |
| 24 |
} |
| 25 |
|
| 26 |
func (Eml) Context() CommandContext { |
| 27 |
return GLOBAL |
| 28 |
} |
| 29 |
|
| 30 |
func (Eml) Aliases() []string { |
| 31 |
return []string{"eml", "preview"} |
| 32 |
} |
| 33 |
|
| 34 |
func (*Eml) CompletePath(arg string) []string { |
| 35 |
return CompletePath(arg, false) |
| 36 |
} |
| 37 |
|
| 38 |
func (e Eml) Execute(args []string) error { |
| 39 |
acct := app.SelectedAccount() |
| 40 |
if acct == nil { |
| 41 |
return fmt.Errorf("no account selected") |
| 42 |
} |
| 43 |
|
| 44 |
showEml := func(r io.Reader) { |
| 45 |
data, err := io.ReadAll(r) |
| 46 |
if err != nil { |
| 47 |
app.PushError(err.Error()) |
| 48 |
return |
| 49 |
} |
| 50 |
lib.NewEmlMessageView(data, app.CryptoProvider(), app.DecryptKeys, |
| 51 |
func(view lib.MessageView, err error) { |
| 52 |
if err != nil { |
| 53 |
app.PushError(err.Error()) |
| 54 |
return |
| 55 |
} |
| 56 |
msgView, err := app.NewMessageViewer(acct, view) |
| 57 |
if err != nil { |
| 58 |
app.PushError(err.Error()) |
| 59 |
return |
| 60 |
} |
| 61 |
app.NewTab(msgView, |
| 62 |
view.MessageInfo().Envelope.Subject) |
| 63 |
}) |
| 64 |
} |
| 65 |
|
| 66 |
if e.Path == "" { |
| 67 |
switch tab := app.SelectedTabContent().(type) { |
| 68 |
case *app.MessageViewer: |
| 69 |
part := tab.SelectedMessagePart() |
| 70 |
tab.MessageView().FetchBodyPart(part.Index, showEml) |
| 71 |
case *app.Composer: |
| 72 |
var buf bytes.Buffer |
| 73 |
h, err := tab.PrepareHeader() |
| 74 |
if err != nil { |
| 75 |
return err |
| 76 |
} |
| 77 |
if err := tab.WriteMessage(h, &buf); err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
showEml(&buf) |
| 81 |
default: |
| 82 |
return fmt.Errorf("unsupported operation") |
| 83 |
} |
| 84 |
} else { |
| 85 |
f, err := os.Open(xdg.ExpandHome(e.Path)) |
| 86 |
if err != nil { |
| 87 |
return err |
| 88 |
} |
| 89 |
defer f.Close() |
| 90 |
showEml(f) |
| 91 |
} |
| 92 |
return nil |
| 93 |
} |