~sircmpwn/aerc

Add optional -h flag to the :open command v1 PROPOSED

Noah Kleiner
Noah Kleiner: 1
 Add optional -h flag to the :open command

 2 files changed, 56 insertions(+), 3 deletions(-)
Noah Kleiner
> :open is meant to open up things like say a pdf, or an image.
I didn't realize that's what the command was for, makes sense though.
Next
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/aerc/patches/9743/mbox | git am -3
Learn more about email & git

[PATCH] Add optional -h flag to the :open command Export this patch

Noah Kleiner
If this flag is applied, the file generated from the current part
will contain the raw message headers.

This allows the user to easily inspect the headers of individual
messages without having to manually enable the global `show-headers`
preference for all messages.
---
I have very little experience with Go, so you might want to take
a closer look and make sure I didn't accidentally break anything.

Also note that the code used to iterate over the message headers
is copy-pasted from the msgviewer.go file, so that's probably not great.

 commands/msgview/open.go | 55 ++++++++++++++++++++++++++++++++++++++--
 doc/aerc.1.scd           |  4 ++-
 2 files changed, 56 insertions(+), 3 deletions(-)

diff --git a/commands/msgview/open.go b/commands/msgview/open.go
index b467543..964a936 100644
--- a/commands/msgview/open.go
+++ b/commands/msgview/open.go
@@ -6,9 +6,11 @@ import (
	"io/ioutil"
	"os"
	"time"
	"fmt"

	"git.sr.ht/~sircmpwn/aerc/lib"
	"git.sr.ht/~sircmpwn/aerc/widgets"
	"git.sr.ht/~sircmpwn/getopt"
)

type Open struct{}
@@ -26,8 +28,23 @@ func (Open) Complete(aerc *widgets.Aerc, args []string) []string {
}

func (Open) Execute(aerc *widgets.Aerc, args []string) error {
	if len(args) != 1 {
		return errors.New("Usage: open")
	if len(args) > 2 {
		return errors.New("Usage: open [-h]")
	}

	opts, _, err := getopt.Getopts(args, "h")

	if err != nil {
		return err
	}

	var headers bool

	for _, opt := range opts {
		switch opt.Option {
		case 'h':
			headers = true
		}
	}

	mv := aerc.SelectedTab().(*widgets.MessageViewer)
@@ -35,19 +52,53 @@ func (Open) Execute(aerc *widgets.Aerc, args []string) error {

	p.Store.FetchBodyPart(p.Msg.Uid, p.Msg.BodyStructure, p.Index, func(reader io.Reader) {
		tmpFile, err := ioutil.TempFile(os.TempDir(), "aerc-")

		if err != nil {
			aerc.PushError(" " + err.Error())
			return
		}

		defer tmpFile.Close()

		if headers && p.Msg.RFC822Headers != nil {
			fields := p.Msg.RFC822Headers.Fields()

			for fields.Next() {
				var value string
				var err error

				if value, err = fields.Text(); err != nil {
					// better than nothing, use the non decoded version
					value = fields.Value()
				}

				field := fmt.Sprintf("%s: %s\n", fields.Key(), value)

				_, err = tmpFile.Write([]byte(field))

				if err != nil {
					aerc.PushError(" " + err.Error())
					return
				}
			}

			_, err := tmpFile.Write([]byte("\n"))

			if err != nil {
				aerc.PushError(" " + err.Error())
				return
			}
		}

		_, err = io.Copy(tmpFile, reader)

		if err != nil {
			aerc.PushError(" " + err.Error())
			return
		}

		err = lib.OpenFile(tmpFile.Name())

		if err != nil {
			aerc.PushError(" " + err.Error())
		}
diff --git a/doc/aerc.1.scd b/doc/aerc.1.scd
index 38c0bd4..c8b901a 100644
--- a/doc/aerc.1.scd
+++ b/doc/aerc.1.scd
@@ -236,10 +236,12 @@ message list, the message in the message viewer, etc).
	Cycles between message parts being shown. The list of message parts is shown
	at the bottom of the message viewer.

*open*
*open* [-h]
	Saves the current message part in a temporary file and opens it
	with the system handler.

	*-h*: Include message headers

*save* [-fp] <path>
	Saves the current message part to the given path.
	If the path is not an absolute path, general.default-save-path will be
-- 
2.19.0
Not sure this patch is needed.

:open is meant to open up things like say a pdf, or an image. How would you look at mail headers when you save an image? JPEG doesn't really have a concept of mail headers for example.

If you want to look at headers show them in the message viewer with :toggle-header and then hide it again once your done.
The command is mapped to H by default, so it's quick to enable / disable
NACK, I'd prefer to just use :show-headers.
View this thread in the archives