procmail
[Top] [All Lists]

Re: Incrementing a counter

1997-10-10 01:11:29
On Thu, 09 Oct 1997 12:37:12 -0700, Bill Moseley <moseley(_at_)netcom(_dot_)com>
wrote:
I have a few questions.  Basically, I get confused on how to properly do
things in procmail that aren't actually dealing with the message.

I have a few marginal comments in addition to the excellent advice
Edward posted. 

1) I have a file that keeps track of the number of spams (an ID number that
I will use to extract a spam at a later date if desired).  I need to
increment it each time.

This sounds an awful lot like you could save it to a directory with
numbered messages and let Procmail do the incrementing automagically.
This prototypically means you'd save each message to its own file, but
if you only want it for the side effect (the autoincrement), I'm sure
workarounds can be devised. (Message-per-file format is nice for a lot
of things if you can afford it, but it eats tons of disk space. I
frequently use it for little Procmail games of a temporary nature
because mbox format is rather unwieldy in many situations.)

2) This is where I get confused.  I want to append some extracted headers
from the current mail to a file.  I don't want to alter the current mail.
What bothers me is doesn't procmail provide the current mail to STDIN of
the external command?  Is it better to do something like
     :0ic
     | (echo "Message Subject: $SUBJECT; \
       echo "           Date: $DATE; \
       echo "          MsgID: $MESSAGEID; \
       echo "Rejected Reason: $REJECT_REASON; \
       echo " ") >> summary.spam

This looks an awful lot like you are writing to a log of some sort.
You could reset LOGFILE temporarily and use LOG= directives to write
out the stats you want. See a recent message to Denis Roegel about
this. 

3) How do I compare two variables?  Now this is embarrassing and clunky:
I want to mail my spam summary at the start of each day.  When I get my
first message of the day I see if the current date is the same as I have
recorded in a file.  (Easier way?  is there a simple way to just look at
the actual summary.spam file date and then mail it if it isn't
today's date.) 

This sounds an awful lot like something you'd actually want done when
you log in, not necessarily when the date changes. (You could format
the report to insert a few empty lines between days if you haven't
logged in for more than one day.) Just spawn the report generator as a
background job in your login script and have it mail the results to
you when it's done. (Okay, this is probably not ideal. Often, when I
implement automatic solutions like this, I find I want them to behave
differently after all. Like, when you want to know whether you have
been spammed, should you have to log in and wait for the report which
might take a lot of time if it's been a long time or you've received a
lot of spam? Important user interface considerations.)

Anyhow, the spam reporting you had devised could be changed so that
you save each day's spam to a different file and run a cron job at
midnight (or whenever) to summarize the spam folder of yesterday. (The
question "how do I get yesterday's date [elegantly]" is a FAQ in
comp.unix.shell and the only really good answer I've seen is to
install a date(1) program which has the logic built in. GNU date is
what I use myself.)

As for how to save to a file with today's date in it, Ed's
Procmail-only solution for date handling is certainly efficient, but I
wouldn't balk at simply doing

    :0:
    * yeah, this is spam
    spam.`date +%y%m%d`

and be done with it.

One last comment: remember that for things that interlock a lot, you
could use a regional lockfile with the LOCKFILE= directive. Just
remember to unset LOCKFILE to remove the lock when you're done with
the critical section.

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