procmail
[Top] [All Lists]

RE: Returning a value to a recipe

2009-03-09 04:02:34
Ed Blackman wrote Monday, March 09, 2009 7:44:

On Mon, Mar 09, 2009 at 03:55:11PM +1100, Nigel Allen wrote:

I am aware of how the basic whitelisting works from the 
docs on the web  thus:

# whitelist
FGREP=/usr/bin/fgrep
FROM=`formail -x From:`
DEFAULT=/var/mail/cannon

:0E
* ? (echo "$FROM" | $FGREP -i -f $HOME/ok)
${DEFAULT}

However I need to do something a little different. I need to
extract a customer header and look for it in a text file - so
far so good. I then need to return a token from the file. For
example, if my header contains 001 and the file contains:

001:fred
002:jim

I need to return the value "fred" back to my recipe.

The following assumes that the header key will never contain a :,
and the key/value map is : delimited, and values are never empty.

AWK=/usr/bin/awk
KEY=`formail -x X-The-Header:`  # or whatever
VAL=`$AWK "BEGIN { FS=\":\" } /^$KEY:/ { print \$2 }" < 
$HOME/valuemap`

:0
* VAL ?? .+                      # key matched
{
   # use $VAL somehow
}

Untested, but should at least get you on the path.

Oh, my.

Let me first say that that's clever -- for the shell.  I have to
tip my hat these days to anybody who remembers enough awk to do
something useful.  I myself like to dust off my awk and brush it up
occasionally too.

But that said, this is procmail, and procmail is perfectly capable
of doing the greater part of what the OP wanted without any of
these shell gyrations.  So let's stick to showing how to do this in
procmail -- where, in any case, it ensues much more efficiently.
We'll add a bit of fgrep as the OP inferred we might want to do.
We don't even need formail here.

Btw, let me start by wondering why you are bothering to define
$AWK here, or why the OP (or rather the web-page source at which
he found the whitelist example) is bothering to define $FGREP.  I
would expect /usr/bin/awk to be the awk in the default path you
have compiled into your procmail; and I would expect /usr/bin/fgrep
to be the fgrep in the default path compiled into procmail.  I
wouldn't bother setting explicit paths to vars unless there is an
unusual need.  That, imho, is what the path was compiled in to
do.  Now, if one is going to use $HOME/bin/awk or $HOME/bin/fgrep
instead, for example, then I would agree with setting a var.

Another thing to clarify before we begin is that the OP left things
a bit murky as to whether the intent to look for the string "001"
in the "customer header" means that he will accept it anywhere in
the entire message header, or whether he means (though he did not
state this explicitly) to find it in one specific field (line) of
the header.  You have taken his intent to be the former.  I took
his intent to be the latter, and I based my interpretation on his
example of grabbing the From: line with "formail -x" and on his
use of the possessive pronoun "my" with "header."  So I will show
it that way in my answer.

But suppose we did want to search the entire message header.  Well,
we don't need to us 'formail -x ""' to extract the entire header in
order to feed it to awk.  Procmail's condition lines look only to the
header by default.  Even if they didn't, we could specify that they
should with the H-flag.  (But they do, so that's not necessary here.)


####################################
 # env help
 SPACE = ' '
 TAB   = '      '
 WS    = $SPACE$TAB

 # strings to look for in the headerline
 #
 # example is identical to:
 # MYSTR = (001|002|003|004|005|006|007|008|009|348|349)

 MYSTR = (00[1-9]|34[89])


 # find our headerline

 :0
 * $ ^X-Headername:.*\<\/$MYSTR\>
 * MATCH ?? ^^\/[0-9]+
 {
   # if we're here, "X-Headername:" was found

   KEY     = $MATCH
   KEYLINE = `fgrep -w $KEY /path/to/hashfile`

   :0
   * $ KEYLINE ?? :\/[^$WS]+ 
   {
     MYVAL = $MATCH
     LOG   = "We matched on \"$MYVAL\"$NL"
   }
 }
####################################

I tested the above.  Here's the run:

  % procmail -m DEFAULT=/dev/null test.rc < test.msg
  We matched on "Pete"

"hashfile" contains:

  % cat hashfile 
  001:John
  348:Pete
  569:Sally

"test.msg" contains:

  % fgrep -w 348 test.msg
  X-Customer-Key: 348


Dallman
(who may be looking for sysadmin work - write with leads)

____________________________________________________________
procmail mailing list   Procmail homepage: http://www.procmail.org/
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail