procmail
[Top] [All Lists]

Re: limit mesages per box

1998-07-22 08:43:24
On Wed, 22 Jul 1998 09:19:31 -0400, Tim Vanderhoek <ac199(_at_)hwcn(_dot_)org>
wrote:
To count the number of messages:
formail -s echo "message" < /var/mail/tim | wc -l

Actually you can probably get away with

    grep -c '^From ' $MAIL

And to delete the oldest message,
formail +1 -s cat < /var/mail/tim > /var/mail/tim~
mv /var/mail/tim~
<...>
And you'll need to be very careful to use appropriate lockfiles.
I'm not sure exactly what will happen if the above tries to delete a
message while you're using your favourite mail reader to read from the
same mailbox...

That's why you need locking (the above script is +very+ dangerous to
use!!!) and why you should be using a mail reader which understands
locking. 

Briefly, 

    lockfile -ml
    TMP=/tmp/tim.mbox
    trap 'rm -f $TMP; exit 2' 1 2 15
    formail +1 -s < $MAIL >$TMP && mv -f $TMP $MAIL
    lockfile -mu

I'm not sure I haven't overlooked something, but that's the general
idea. If you have permission to write to the mail spool, it's probably
quicker to use that than to use /tmp

You could easily develop this further so it deletes $1 messages
instead of just the first (where $1 is the first argument passed to
the shell script, of course).

It gets more interesting when you try to e.g. delete only messages
older than a week. This has been covered on this list many times
before; try the search function of the browsable list archive at
<http://www.rosat.mpe-garching.mpg.de/mailing-lists/procmail/> and
also Jari's mail expiry tips (you'll find those at the archive as
well; search for "expire mail").

Hope this helps,

/* era */

Yes, "formail -s cat" is redundant, and does what "formail -s" alone
does.

-- 
 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>