Some preamble. I wanted to configure OpenSMTPD to masquerade the hostname for mail intended for recipients outside the local domain. This seems to be impossible without using the smtpd-filters capability.
So, how would I write a filter? Some help was needed; a Google search found this. Hmm, written in awk. Interesting choice, but works for me and was extemely helpful to understand what I needed to do. I cloned the repository and set about modifying it for masquerading.
While writing the additional code, I discovered an issue with emacs' awk-mode. When I typed a pattern of the form:
"filter|smtp-in|rcpt-to" == $1_$4_$5
and entered a left-brace to start the action, the brace was positioned on the next line:
"filter|smtp-in|rcpt-to" == $1_$4_$5 {
This is not helpful. A left brace with nothing to its left is considered to be an empty pattern, which matches everything. What should happen is this:
"filter|smtp-in|rcpt-to" == $1_$4_$5 {
Could I modify awk-mode to cater for this kind of pattern (it’s perfectly legitimate)? Well, yes. This is emacs after all.
It takes this elisp, added to the c-mode-common-hook:
(push '(brace-list-open after) (cdr (assoc 'c-hanging-braces-alist (cdr (assoc "awk" c-style-alist)))))
To discover the correct syntax element that preceeds the left brace,
one needs to ensure the cursor is positioned over the left brace
following the misbehaving syntax and type Cntrl-X Cntrl-S
. That command
returned brace-list-open
for this specific case. The after
means
that a newline is emitted after the brace, but not before. The rest of
the command comes from poking about at the the c-style-alist
data
structure, to figure out how the new setting should be inserted.
If you are interested in the filter, it’s on GitHub.