procmail
[Top] [All Lists]

Re: Grabbing a field in a variable and then matching on this variable...

2000-02-10 09:12:44
On 10 Feb 2000 16:50:05 +0100, Francois Felix Ingrand <felix(_at_)laas(_dot_)fr>
wrote:
era eriksson <era(_at_)iki(_dot_)fi> writes:
:0 # The \/ says, "if we have a match, save the part that follows"
* ^Delivered-To:[   ]*\/[^  ]+(_at_)foo\(_dot_)com
{
    # The string that matched the regex after \/ is now in $MATCH
Great... I even added a couple of "improvements/bugs"
:0
* ^Delivered-To:[    ]*\/[^  ]+(_at_)ingrand\(_dot_)net
{
     DELIVERED=`echo $MATCH | sed -e 's/@ingrand\.net$//' -e 
's/^redirect-//' -e 's/^ingrand\.net%//'`
        # now DELIVERED contains the "user" name only

You can tweak the original grab so it grabs less:

    * ^Delivered-To:[   ]*(redirect-)?\/[a-z]+

(not sure if that will work out of the box, there are some issues
related to the greediness of matching and I'm too lazy to test it) or
simply use another grab to grab only part of what gets grabbed at
first:

    :0
    * ^Delivered-To:[   ]*\/[^  ]+(_at_)ingrand\(_dot_)net
    * MATCH ?? ^^\/[^(_at_)]+
    {
        DELIVERED=$MATCH
        :0
        * DELIVERED ?? ^^redirect-\/.+
        { DELIVERED=$MATCH }

        ...

You can keep on going like this in additional recipes. Just keep in
mind that the grabbing is a side effect, and that the actual condition
needs to be met in order for the action part of a recipe to be taken.
And be careful with leading backslashes -- Procmail's partsing of
those is weird, so it's customary to change a leading \/ to ()\/ as a
simple workaround.

I would imagine that the @ingrand.net part is something you can count
on (your upstream should hopefully be zapping old Delivered-To headers
before adding their own) so you can simplify the first regex.
Additionally, if you can count on the redirect- always being there,
you can get by with something reasonably close to the original
proposal.

    :0
    * ^Delivered-To:[   ]*redirect-\/[^(_at_)]+
    {
        DELIVERED=$MATCH

Otherwise, you will have to pick off the redirect- separately, or
verify that greediness works to your favor (which frankly I suspect it
won't) or modify the regular expression so you can make it omit the
redirect- if it's there and still grab the relevant part directly.
(This can be tricky.)

Hope this helps,

/* era */

-- 
 Too much to say to fit into this .signature anyway: <http://www.iki.fi/era/>
  Fight spam in Europe: <http://www.euro.cauce.org/> * Sign the EU petition

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