procmail
[Top] [All Lists]

Re: Incrementing a counter

1997-10-09 15:26:41
Excerpts from mail: (09-Oct-97) Incrementing a counter by Bill Moseley
So is that a good way to increment, or would a scoring recipe be better?

Normally, I would say that using scoring would be better, but I'm concerned
that $MESSAGENUM might contain a trailing newline. I don't know how that
would affect the scoring recipe. (It might cause problems with your recipes
that use $MESSAGENUM as an argument to external commands.) You might have to
strip the trailing newline, if there is one, like so:

:0
* MESSAGENUM ?? ^^[ ]*\/[0-9]+
{ MESSAGENUM = $MATCH }

Or, perhaps alternatively, you could use `echo -n' instead of `echo'.

:0
* $MESSAGENUM^0
* 1^0
{ }
MESSAGENUM = $=

There needs to be a "$ " before "$MESSAGENUM^0".

What bothers me is doesn't procmail provide the current mail to STDIN of
the external command?

Yes.

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

Yes. There should be a local lockfile on this recipe though.

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

TODAY=`date '+%m%y%d'`

Instead of forking a `date' process, I suggest parsing the date from the
From_ header. (Warning: I don't think all From_ headers look exactly the
same, so you should test this first.)

# Get current month and day from From_ header.
:0
* ^From [^ ]+ +... \/...  ?[0-9]+
{ DATE = $MATCH }

:0 # Get the month.
* DATE ?? ^^\/...
{ MONTH = $MATCH }

:0 # Get the day.
* DATE ?? ^^... +\/[0-9]+
{ DAY = $MATCH }

# If $DAY has only one digit, prepend a 0.
:0
* DAY ?? ^^.^^
{ DAY = "0$DAY" }

# Get the year from From_ header.
:0
* ^From [^ ]+ +... ...  ?[0-9]+ [0-9]+:[0-9]+:[0-9]+ \/[0-9]+
{ YEAR = $MATCH }

TODAY = "$YEAR-$MONTH-$DAY"

# update the current date as today
DUMMY=`echo $CURRENT > $PMDIR/currentdate.spam

I think you want:

  DUMMY=`echo -n "$TODAY" > $PMDIR/currentdate.spam

# now send yesterday's spam summary
:0
* ? test -f $PMDIR/summary.spam
{
   DUMMY=`cat $PMDIR/summary.spam | mail moseley`
   DUMMY=`rm %PMDIR/summary.spam`
}

Congratulations! You win the Prize of the Month for Unnecessary Usage of
`cat'. :-) I would suggest changing this to:

:0icr:summary.spam$LOCKEXT
* ? test -f $PMDIR/summary.spam
| Mail -s "Spam Summary for $CURRENT" moseley < $PMDIR/summary.spam && rm -f 
$PMDIR/summary.spam

You might want to change "moseley" to "$LOGNAME" just to be totally generic.

- How should I compare the current date with the date in the file.
Would it be better to grep TODAY against currentdate.spam?

That's a valid approach, but I wouldn't say it is any better (or worse, for
that matter.)

- What's the best way to mail a file to myself?  Do I need to WAIT for
the mail command to finish before removing the file?

See above.

- Is   * ! $ CURRENT ?? $TODAY the correct syntax?  Do I need the first "$"
expand $TODAY?

A generic inequality comparison between any two variables $FOO and $BAR is
best (IMHO) written as:

          * FOO ?? ! $ ^^$\BAR^^

Later,
Ed