From ebfd2a9da3d63390a9d4a0c227b2513a0dac19a4 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Thu, 13 Oct 2022 00:03:42 +0200 Subject: [PATCH] filters: export mime type and filename in env Export AERC_MIME_TYPE and AERC_FILENAME in the filters command environment. This allows dynamic coloring with tools that require a filename and/or a mime type to determine the syntax. Update docs and add example use in the default config file. Signed-off-by: Robin Jarry Acked-by: Moritz Poldrack --- CHANGELOG.md | 2 ++ config/aerc.conf | 6 ++++++ doc/aerc-config.5.scd | 7 +++++++ widgets/msgviewer.go | 7 ++++++- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fed1e0..107f8df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). handler (i.e. `:open gimp` to open attachment in GIMP). - Restored XOAUTH2 support for IMAP and SMTP. - Support for attaching files with `mailto:`-links +- Filter commands now have the `AERC_MIME_TYPE` and `AERC_FILENAME` variables + defined in their environment. ### Changed diff --git a/config/aerc.conf b/config/aerc.conf index 66aae82..a980c70 100644 --- a/config/aerc.conf +++ b/config/aerc.conf @@ -310,6 +310,11 @@ reply-to-self=true # $PREFIX/share/aerc/filters # /usr/share/aerc/filters # +# The following variables are defined in the filter command environment: +# +# AERC_MIME_TYPE the part MIME type/subtype +# AERC_FILENAME the attachment filename (if any) +# # The first filter which matches the email's mimetype will be used, so order # them from most to least specific. # @@ -323,6 +328,7 @@ message/delivery-status=colorize message/rfc822=colorize #text/html=pandoc -f html -t plain | colorize #text/html=html | colorize +#text/*=bat -fP --file-name="$AERC_FILENAME" #application/x-sh=bat -fP -l sh #image/*=catimg -w $(tput cols) - #subject,~Git(hub|lab)=lolcat -f diff --git a/doc/aerc-config.5.scd b/doc/aerc-config.5.scd index 9be0cb2..2c5f809 100644 --- a/doc/aerc-config.5.scd +++ b/doc/aerc-config.5.scd @@ -520,6 +520,13 @@ $PREFIX/share/aerc/filters /usr/share/aerc/filters ``` +The following variables are defined in the filter command environment: + +_AERC_MIME_TYPE_ + the part MIME type/subtype +_AERC_FILENAME_ + the attachment filename (if any) + Note that said email body is converted into UTF-8 before being passed to filters. diff --git a/widgets/msgviewer.go b/widgets/msgviewer.go index 70c079a..a32e039 100644 --- a/widgets/msgviewer.go +++ b/widgets/msgviewer.go @@ -541,8 +541,9 @@ func NewPartViewer(acct *AccountView, conf *config.AercConfig, pager = exec.Command(cmd[0], cmd[1:]...) info := msg.MessageInfo() + mime := part.FullMIMEType() + for _, f := range conf.Filters { - mime := part.FullMIMEType() switch f.FilterType { case config.FILTER_MIMETYPE: if fnmatch.Match(f.Filter, mime, 0) { @@ -577,6 +578,10 @@ func NewPartViewer(acct *AccountView, conf *config.AercConfig, } filter.Env = os.Environ() filter.Env = append(filter.Env, fmt.Sprintf("PATH=%s", path)) + filter.Env = append(filter.Env, + fmt.Sprintf("AERC_MIME_TYPE=%s", mime)) + filter.Env = append(filter.Env, + fmt.Sprintf("AERC_FILENAME=%s", part.FileName())) if pipe, err = filter.StdinPipe(); err != nil { return nil, err }