procmail
[Top] [All Lists]

Re: question on scoring

1997-03-31 07:04:09
On Mon, 31 Mar 1997 06:11:20 -0500 (EST),
Wotan <wotan(_at_)netcom(_dot_)com> wrote:
Would something like this work for keeping someone from mailing you more
than 9 times in a day?

Your example doesn't show that you would actually add the FROM line to
the log at any point but I'm assuming you do. (I guess the LOG= line
is two ideas in one :-) Also, you need a double $ on NUM. Given that,
I think it looks OK.
  You might want to check for exact matches using fgrep, too -- let's
say you get a mail from "era" in your own domain; this would then,
according to your grep, match on "operator(_at_)earthlink(_dot_)net" for 
instace.
Add the -x flag to fgrep for 100% matches.

FROM=`$FORMAIL -zrx To:`
DATE=`date +%m%d%y`
LOG=$PMDIR/$DATE.log
NUM=`grep -c $FROM $LOG`

:0
* -9^0
* $NUM^0

(Change this to "* $$NUM^0".)

/dev/null

I haven't measured whether the following is in fact more
resource-friendly; since I start up only one shell to do both the
logging and the grepping, I guess it's probably a bit more economical
than what you had. (You could try to do even more stuff in one shell
invocation if you don't care about the output of intermediate
commands.)
  I would also +guess+ that the ! ? construct is less filling than
backquotes, but as usual ,-) I haven't measured that. (OTOH I actually
take care to lock the log file, as discussed recently. This will of
course consume some resources but it might be worth it. The LOCKFILE
maneuver was posted by David Tamkin.)

    DATE=`date +%m%d%y`
    DATELOG=$PMDIR/$DATE.log
    LOCKFILE=$DATELOG.lock

    :0
    * -9^0
    * 1^1 ! ? echo "$FROM" >>$DATELOG; exit `fgrep -cx "$FROM" $DATELOG`
    /dev/null

    LOCKFILE

The ! ? trick says to the scoring system that the exit code of the
shell command is to be taken as the number of matches. I artificially
put the result of the fgrep into the exit code using "exit `...`. 
  In this particular case you don't really need the LOCKFILE magic but
if you store in a file intead of /dev/null, you are going to want
double locking.

/* era */

Here's a somewhat less contrived version, with the logging untangled
into its own part. I still think it's a good idea to lock the log file
here. It's not completely necessary if you don't care if you
occasionally allow one or two messages too many from the same place.
(Without locking you might get two Procmail processes writing to the
log at the same time, resulting in general porridge in the log.)

    # Do the logging

    #  (Sorry, if we are going to be "less contrived" 
    #  we can't use American date order ;^)
    DATE=`date +%y%m%d`
    DATELOG=$PMDIR/$DATE.log
    FROM=`formail -zrx To:`

    OLDLOG=$LOGFILE
    LOCKFILE=$DATELOG.lock
    LOGFILE=$DATELOG
    LOG="$FROM
"
    LOGFILE=$OLDLOG
    LOCKFILE

    # Do the grepping

    :0
    * -9^0
    * 1^1 ! ? exit `fgrep -cx "$FROM" $DATELOG`
    /dev/null

-- 
Defin-i-t-e-ly. Sep-a-r-a-te. Gram-m-a-r.  <http://www.iki.fi/~era/>
 * Enjoy receiving spam? Register at <http://www.iki.fi/~era/spam.html>

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