Procmailrc, TRAP and LASTFOLDER

I use procmail and lbdb.

lbdb is invoked at the beginning of procmailrc harvesting From: addresses from all incoming mail, so that they can later be queried with mutt. It's really neat.

However, if it's put at the beginning of procmailrc, it harvests From addresses from spam, too. This causes the ldbd databse to become very big, slow to query, and in some unlucky cases, to return lots of bad results.

Problem is, I run the spam filter AFTER I delivered to the mailing lists. The rationale is that mailing lists already do their own spam filtering to some useable level. At least those I'm subscribed to.

So, my current procmailrc goes like:

feed a copy through lbdb
filter list 1
filter list 2
...
filter list N for N very big
filter out spam
deliver to inbox

Now, is there a way I could invoke lbdb for every mail except what is caught by the spam filter? A way, I mean, that doesn't involve hooking into each and every of the N list rules an lbdb invocation?

Here is the way. In .procmailrc, add:

TRAP=~/bin/scan-nonspam

Then write ~/bin/scan-nonspam:

1
2
3
4
5
6
#!/bin/bash
if [ ${LASTFOLDER:0:4} != 'spam' ]
then
    /usr/bin/lbdb-fetchaddr
fi
exit 0

For reference, this is the documentation of TRAP:

When procmail terminates of its own accord and not because it received a signal, it will execute the contents of this variable. A copy of the mail can be read from stdin. Any output produced by this command will be appended to $LOGFILE. Possible uses for TRAP are: removal of temporary files, logging customised abstracts, etc. See also EXITCODE and LOGABSTRACT.

And this is the documentation of LASTFOLDER:

This variable is assigned to by procmail whenever it is delivering to a folder or program. It always contains the name of the last file (or program) procmail delivered to. If the last delivery was to several directory folders together then $LASTFOLDER will contain the hardlinked filenames as a space separated list.