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 <robin@jarry.cc>
Acked-by: Moritz Poldrack <moritz@poldrack.dev>
This commit is contained in:
Robin Jarry 2022-10-13 00:03:42 +02:00
parent 9bd2e0c84f
commit ebfd2a9da3
4 changed files with 21 additions and 1 deletions

View file

@ -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). handler (i.e. `:open gimp` to open attachment in GIMP).
- Restored XOAUTH2 support for IMAP and SMTP. - Restored XOAUTH2 support for IMAP and SMTP.
- Support for attaching files with `mailto:`-links - Support for attaching files with `mailto:`-links
- Filter commands now have the `AERC_MIME_TYPE` and `AERC_FILENAME` variables
defined in their environment.
### Changed ### Changed

View file

@ -310,6 +310,11 @@ reply-to-self=true
# $PREFIX/share/aerc/filters # $PREFIX/share/aerc/filters
# /usr/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 # The first filter which matches the email's mimetype will be used, so order
# them from most to least specific. # them from most to least specific.
# #
@ -323,6 +328,7 @@ message/delivery-status=colorize
message/rfc822=colorize message/rfc822=colorize
#text/html=pandoc -f html -t plain | colorize #text/html=pandoc -f html -t plain | colorize
#text/html=html | colorize #text/html=html | colorize
#text/*=bat -fP --file-name="$AERC_FILENAME"
#application/x-sh=bat -fP -l sh #application/x-sh=bat -fP -l sh
#image/*=catimg -w $(tput cols) - #image/*=catimg -w $(tput cols) -
#subject,~Git(hub|lab)=lolcat -f #subject,~Git(hub|lab)=lolcat -f

View file

@ -520,6 +520,13 @@ $PREFIX/share/aerc/filters
/usr/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 Note that said email body is converted into UTF-8 before being passed to
filters. filters.

View file

@ -541,8 +541,9 @@ func NewPartViewer(acct *AccountView, conf *config.AercConfig,
pager = exec.Command(cmd[0], cmd[1:]...) pager = exec.Command(cmd[0], cmd[1:]...)
info := msg.MessageInfo() info := msg.MessageInfo()
mime := part.FullMIMEType()
for _, f := range conf.Filters { for _, f := range conf.Filters {
mime := part.FullMIMEType()
switch f.FilterType { switch f.FilterType {
case config.FILTER_MIMETYPE: case config.FILTER_MIMETYPE:
if fnmatch.Match(f.Filter, mime, 0) { if fnmatch.Match(f.Filter, mime, 0) {
@ -577,6 +578,10 @@ func NewPartViewer(acct *AccountView, conf *config.AercConfig,
} }
filter.Env = os.Environ() filter.Env = os.Environ()
filter.Env = append(filter.Env, fmt.Sprintf("PATH=%s", path)) 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 { if pipe, err = filter.StdinPipe(); err != nil {
return nil, err return nil, err
} }