Skip to content

Commit

Permalink
commands: allow to forward from eml
Browse files Browse the repository at this point in the history
Forward messages from the message viewer when they are opened with :eml (e.g.
rfc822 attachments). Those messages have no associated message store and
currently :forward would complain about that.

References: https://todo.sr.ht/~rjarry/aerc/227
Signed-off-by: Koni Marti <[email protected]>
Acked-by: Robin Jarry <[email protected]>
  • Loading branch information
konimarti authored and rjarry committed Feb 14, 2024
1 parent 6afc2d5 commit d56e949
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions commands/msg/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ func (f forward) Execute(args []string) error {
if acct == nil {
return errors.New("No account selected")
}
store := widget.Store()
if store == nil {
return errors.New("Cannot perform action. Messages still loading")
}
msg, err := widget.SelectedMessage()
if err != nil {
return err
Expand Down Expand Up @@ -110,14 +106,37 @@ func (f forward) Execute(args []string) error {
return composer, nil
}

mv, isMsgViewer := widget.(*app.MessageViewer)
store := widget.Store()
noStore := store == nil
if noStore && !isMsgViewer {
return errors.New("Cannot perform action. Messages still loading")
}

if f.AttachFull {
tmpDir, err := os.MkdirTemp("", "aerc-tmp-attachment")
if err != nil {
return err
}
tmpFileName := path.Join(tmpDir,
strings.ReplaceAll(fmt.Sprintf("%s.eml", msg.Envelope.Subject), "/", "-"))
store.FetchFull([]uint32{msg.Uid}, func(fm *types.FullMessage) {

var fetchFull func(func(io.Reader))

if isMsgViewer {
fetchFull = mv.MessageView().FetchFull
} else {
fetchFull = func(cb func(io.Reader)) {
store.FetchFull([]uint32{msg.Uid}, func(fm *types.FullMessage) {
if fm == nil || (fm != nil && fm.Content == nil) {
return
}
cb(fm.Content.Reader)
})
}
}

fetchFull(func(r io.Reader) {
tmpFile, err := os.Create(tmpFileName)
if err != nil {
log.Warnf("failed to create temporary attachment: %v", err)
Expand All @@ -129,7 +148,7 @@ func (f forward) Execute(args []string) error {
}

defer tmpFile.Close()
_, err = io.Copy(tmpFile, fm.Content.Reader)
_, err = io.Copy(tmpFile, r)
if err != nil {
log.Warnf("failed to write to tmpfile: %v", err)
return
Expand All @@ -150,7 +169,6 @@ func (f forward) Execute(args []string) error {

var fetchBodyPart func([]int, func(io.Reader))

mv, isMsgViewer := widget.(*app.MessageViewer)
if isMsgViewer {
fetchBodyPart = mv.MessageView().FetchBodyPart
} else {
Expand Down

0 comments on commit d56e949

Please sign in to comment.