procmail
[Top] [All Lists]

Re: Conditional subject

1997-09-03 09:20:30
wwgrol(_at_)sparc01(_dot_)fw(_dot_)hac(_dot_)com (W. Wesley Groleau x4923) 
writes:
I have a lot of repetition in my recipes that I'd like to factor into an
INCLUDERC.  One tricky part is to supply a specific subject sometimes, and
let formail generate an "Re: xxxx" other times.  Here is what I've tried:


 SUBJECT     = "Mail Delivery Refused: Too Large"
 COPY_FOLDER = $SPAMFOLDER
 COMMENT     = "Huge"
 RESPONSE    = "too_large.msg"

 :0:
 * >10000
 * !^X-Loop: noloops_for_wwgrol(_at_)fw(_dot_)hac(_dot_)com
 { INCLUDERC=autorespond.rc }


where autorespond.rc contains (thanks to this list):

 :0c:
 $COPY_FOLDER

 :0fhw  # double the head; second head is now part of body
 | sed -e H -e '$ G'

 :0fhw  # invert first head and slip in message after it (before second head)
 | formail -Y -ri"Subject: $SUBJECT"                  \
           -tA"X-Loop: noloops_for_wwgrol(_at_)fw(_dot_)hac(_dot_)com" \
           -A"X-Comment: $COMMENT" ;                  \
   cat $RESPONSE

 :0     # send it all out
 ! -t


Would it work if I were to sometimes put 

 SUBJECT = "-i\"Subject: yadda yadda\""

and other times

 SUBJECT = ""

and change the line above to

 | formail -Y -r $SUBJECT                  \

If you don't double quote the $SUBJECT in the action line, the value
will be split on the whitespace in it, regardless of the quoting in the
value.  On the otherhand, if you do quote it, then when it's empty
there will be any empty value in the argument list to formail which
it'll choke on.

The solution is to attache this to the -r argument.  Using some shell
variable expansion trickery we can make it even simpler to use:

        SUBJECT = "yaddah yaddah"
        # or get the default via
        SUBJECT = ""
        # which would have the same effect as unsetting it with
        SUBJECT

Then use an action line that starts:

        | formail -Yr${SUBJECT:+i'Subject: }${SUBJECT}${SUBJECT:+'} \
                ...


The phrase 
        ${SUBJECT:+i'Subject: }
is an example of the shell's ${variable:+text} form.  That form expands
to nothing if the variable (SUBJECT in this case) is empty or unset,
while if it's set it expands to the given text (<<i'Subject: >> in this
case).  The result is that if SUBJECT is empty or unset, the beginning
of the command will be:

        formail -Yr \
        ...

while if SUBJECT is set to "some text", then the beginning of the
command will be:

        formail -Yri'Subject: some text' \
        ...


Sneaky, eh?  Shell quoting may be horrible, grotesque, un-intuitive,
and twisted, but dang, can it be expressive!


Philip Guenther

<Prev in Thread] Current Thread [Next in Thread>