procmail
[Top] [All Lists]

Re: help with "forward everything except..."

1998-08-03 01:41:26
On Sat, 1 Aug 1998 13:14:00 -0500, "White, Eric" <eric(_at_)quadralay(_dot_)com>
wrote:
echo `cat email.lst` | sed -e 's/ /\|/g'

The intent here is to do tr '\012' '|' < email.lst, yes?
  <http://www.iki.fi/~era/unix/award.html>  (okay, well, not really :-)

where email.lst contains something like:
foo(_at_)bar(_dot_)com
bar(_at_)foo(_dot_)com
dev(_at_)null(_dot_)com
thinking that I would come up with a recipe condition (ultimately)
resolving to:
* !^FROM_ .* 
(foo(_at_)bar(_dot_)com|bar(_at_)foo(_dot_)com|dev(_at_)null(_dot_)com)

There is no ^FROM_ macro. (If there was one, the syntax would look
like ! 
^FROM_(foo(_at_)bar\(_dot_)com|bar(_at_)foo\(_dot_)com|dev(_at_)null\(_dot_)com)
 anyway.)

Three options:

 1. More or less what you are describing:

    # Get rid of empty lines in email.lst, then change newline to |
    WHAT=`fgrep -vx '' email.lst | tr '\012' '|'`

    # Make damn sure the resulting expression fits in your LINEBUF, though
    :0
    * $ ! ^From ($WHAT)\>
    { action ... }

    Notice the $ on the condition line; without this; Procmail would
    be looking for the regular expression $ (aka newline) W H A T.

    The lines in email.lst would have to be individual regular
    expressions rather than literal strings. In particular, dots in
    addresses should be backslash-escaped.

    I'm not sure if you were expecting ^FROM_ to match on ^From or
    just ^From: or something else. The below recipe gives you more to
    choose from, by way of the classic formail -rtzxTo: trick. If you
    only want to look at some particular header other than From_, you
    can change the above recipe. (From: is trickier than From_ because
    it can contain RFC822 comments, i.e. you'd have to say something
    like 

      * $ ! ^From:(.*\<)?($WHAT)\>

    to get the same result.)

 2. fgrep email.lst directly on the fly. Avoids LINEBUF problems, 
    and you don't have to use regex syntax in email.lst.

    :0
    * ! ? formail -rtzxTo: | fgrep -ix email.lst
    { action ... }

    You can try formail -rzxTo: instead, or some complicated and
    convoluted way to make formail select from a smaller set of
    headers than it usually does. See the FAQ for a bit of discussion
    around this topic. (You'll find a link to the FAQ at the Links
    page, below. There are several sections which discuss different
    aspects of formail -r.)

    In particular, to make formail disregard Resent-From:, you'd say
    something like 

    formail -IResent-From: -rtzxTo:

 3. The computationally least expensive: Create a Makefile which you
    remember to run each time you update email.lst.

    # GNU Make syntax:
    # $@ is shorthand for forward.rc, $< is short for email.lst
    forward.rc: email.lst
        date +'# Automatically created from $< on %D %T' >$@
        echo '# -- do not edit directly, run make instead to update' >>$@
        echo ':0' >>$@
        echo -n '* ! ^From (' >>$@
        fgrep -vx '' $< | tr '\012' '|' >>$@
        echo ')' >>$@
        echo '{ action ... }' >>$@

    If you're not used to using Make, you might not like this
    solution. OTOH, if you're not used to using Make, you might want
    to get used to it.

    Now, you can just
    INCLUDERC=forward.rc 
    from your regular .procmailrc.

    This avoids wasting cycles on recomputing the value of WHAT, or
    rerunning formail | fgrep, each time you receive a message.
    Instead, the regular expression is computed once, when you run
    make, and the output is stored in a file which is read and parsed
    directly by Procmail.

A good example of the first approach used to be in Panix' spam filters
(but theirs was slightly flawed in that it didn't include protection
against empty lines; they'd actually sometimes ending up with a
regular expression containing '||', which of course matches on any
string) ; I'm not sure if they're still being updated, but you should
find a link to them on the links page (see below).

any pointers, tips, hints are greatly appreciated.

Various pointers to all sorts of stuff are at
    <http://www.iki.fi/~era/procmail/links.html>
You can also search the archives of this list at
    <http://www.rosat.mpe-garching.mpg.de/mailing-lists/procmail/>

Hope this helps,

/* era */

-- 
 Paparazzi of the Net: No matter what you do to protect your privacy,
  they'll hunt you down and spam you. <http://www.iki.fi/~era/spam/>

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