procmail
[Top] [All Lists]

Re: Counting headers?

1998-03-10 13:20:06
Excerpts from mail: (10-Mar-98) Counting headers? by Andrew 
What I'd like to do is come up with a recipe that'll detect these.
I.e. count the number of received headers and if there's only one,
dump it. If there's more than one, check to see which ones are just
passing through my local mail servers, and if none remain after
eliminating those, dump it.

This sounds like a bad idea to me. (What about e-mail received from local
users?) But it sounded like fun to code, so here is one possible solution:

This is really just an alteration to a recipe I wrote and posted last year.
(See 
<http://www.rosat.mpe-garching.mpg.de/mailing-lists/procmail/1997-09/msg00436.html>
 for details.)

This could probably be implemented using a non-recursive algorithm, but since
I already had the aforementioned receipe on hand, I chose to do it this way.

In your .procmailrc, put the following:
#--------------------------------------------------------------------------
RECEIVEDHDR = "(Received:[      ]*)"
# Change the mail host addresses in the next line to match your local servers.
LOCALRECEIVEDHDR = "(Received:.* by ((mail|mx1|mx2)\.your-domain\.com))"

:0
* $ ^\/$RECEIVEDHDR(.*$)+
{
     HEADERLINES = $MATCH
     COUNT_RECEIVED = 0
     COUNT_NONLOCAL_RECEIVED = 0

     INCLUDERC = count-received.rc

     :0:
     * COUNT_RECEIVED ?? ^^1^^
     mbox.possible-spam
     # or /dev/null or whatever you want to do with it...

     :0:
     * COUNT_NONLOCAL_RECEIVED ?? ^^0^^
     mbox.possible-spam
     # or /dev/null or whatever you want to do with it...
}
#--------------------------------------------------------------------------

Then, make a file named count-received.rc with the following recipe in it:

#--------------------------------------------------------------------------
:0
* HEADERLINES ?? $ ^^(.*$)+\/$RECEIVEDHDR(.*$)+
{ REMAININGLINES = $MATCH }

:0E
{ REMAININGLINES }

:0
* HEADERLINES ?? $ ^^$RECEIVEDHDR
{
     # Increment COUNT_RECEIVED by 1.
     :0
     * $ $COUNT_RECEIVED^0
     * 1^0
     { COUNT_RECEIVED = $= }

     :0
     * HEADERLINES ?? $ ! ^^$LOCALRECEIVEDHDR
     {
          # Increment COUNT_NONLOCAL_RECEIVED by 1.
          :0
          * $ $COUNT_NONLOCAL_RECEIVED^0
          * 1^0
          { COUNT_NONLOCAL_RECEIVED = $= }
     }
}

# Now, do recursion, but only if REMAININGLINES is not an empty string.
:0
* REMAININGLINES ?? .
{
     HEADERLINES = $REMAININGLINES

     INCLUDERC = $_

}
#--------------------------------------------------------------------------

Note: The above code has been tested.

Later,
Ed

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