procmail
[Top] [All Lists]

Re: Adding fields conditionally to formail call?

1998-01-16 02:53:55
Thu 98-01-15 dattier(_at_)wwa(_dot_)com (David W. Tamkin) list.procmail
| The major breakthrough came when Philip suggested putting "-A" next to
| "X-Headerwhichever:" so that we wouldn't have two different kinds of spaces
| in the definitions of field1 and field2.


Exellent. Guess what, with this help I could remove 6 extra formail
calls from the new MPFS server.

Thanks to all and inpired by the help I added chapter below to pm-tips.txt
Further comment are welcome.

jari

    12.14 Reducing formail calls (conditionally adding fields)

        Suppose you want add fields to the message when some condition is met:

            :0              # compose initial reply
            | formail -rt

            :0
            * condition1
            | formail -A "X-Header1: value1"

            :0
            * condition2
            | formail -A "X-Header2: value2"

        Hm, we have three processes called here, can we minimize the calls?
        Yes, this is idea from [philip] and [david]. Notice that there is
        only ONE process needed.

            :0
            * condition1
            { hdr1 = "-AX-Header1:value" }

            :0
            * condition2
            { hdr2 = "-AX-Header2: value" }

            :0 fh
            | formail -rt  ${hdr1+"$hdr1"} ${hdr2+"$hdr2"}

        And if you want to stack all headers to only one variable, it is
        a bit of extra work. Below we use short variable names only because
        of the line space: the calls fit on one line.

        o   f  = all (f)ields stacked to one string.
        o   nl = continuation newline terminator of previous field

        The recipe says: if *f* has previous value, set `nl' to newline
        separator, later concat previous contents of *f* with possible
        newline and new header field.

            f       # kill variable
            :0
            { nl nl=${f+"$NL"}  f="$f${nl}X-Header1: value" }

            :0
            { nl nl=${f+"$NL"}  f="$f${nl}X-Header2: value" }

            #   If we have something in *f*, call formail
            :0 fh
            * f ?? ^^^^
            | formail ${f+-A"$f"}

        The above recipe was the most general one, each recipe determined
        by itself if the *f* existed previously or not. But if you know
        that *f* is already set, you can write simpler recipe:

            # We know f has value before our module
            :0
            { f = "$f${NL}X-Header1: value" }