#################################################################
#
# $Id: mainline,v 1.13 1998/05/22 21:07:22 bcwhite Exp $
#
# Spam Filter -- Mainline
#
#################################################################


# WARNING: If the global lockfile has been disabled, it is vital that
# every delivery rule in the sub-files (special.rules, list.rules,
# spam.rules, and user.rules) have a local lockfile (trailing ":").
# Otherwise, you could get corrupted mail.  The default delivery rules
# in this file all have local lockfiles where appropriate except for
# the spambox which is excluded because it is often set to /dev/null.
# Besides, who cares if spam gets corrupted?

# If you are fairly new to procmail and plan to experiment a little
# bit it often helps to have a safety net of some sort.  Inserting the
# following two recipes above all other recipes will make sure that of
# all arriving mail always the last 356 messages will be preserved.
# In order for it to work as intended, you have to create a directory
# named $PM_BACKUP prior to inserting these two recipes.

:0 c: $BACKUPLOCK
* ? test x$DEBUG = xyes
$PM_BACKUP

:0 ic: $BACKUPLOCK
* ? test x$DEBUG = xyes
| cd $PM_BACKUP && rm -f dummy `ls -t | sed -e 1,200d`


#
# Provide some useful variables:
#
#	OBVIOUSLYTO	Similar to procmail's builtin "TO" but doesn't catch
#			some headers sometimes added when BCCing ourselves.
#
OBVIOUSLYTO=(((Original-)?(Resent-)?(To|Cc)):(.*[^a-zA-Z])?)


#
# Run the priority rules
#
INCLUDERC=$PM_RULES/priority.rules



#
# See if this mail is _from_ me, and if so, use it to update the greylist
#
:0
* $ ^Received:.*from.*$MYHOST
* $ ^Message-ID:.*$MYHOST
* $ ^From:.*$MYADDR
* ! ^Resent-From:
* ! ^X-Loop:
* ! ^X-Mailing-List:
{
	#
	# Since this message wasn't sent to me, just try to add people to the greylist
	#

	:0
	* !$ ^$OBVIOUSLYTO$MYADDR
	| addpat --append $GREYLIST --header "To|Cc|Bcc|Reply-To" --mail $USER --from $OFFADDR --dest "greylist" $WHITELIST $GREYLIST

	#
	# This message was sent to me, so try to add people to the greylist and then
	# let the message through to the rest of the filter
	#

	:0 c
	| addpat --append $GREYLIST --header "To|Cc|Bcc|Reply-To" --mail $USER --from $OFFADDR --dest "greylist" $WHITELIST $GREYLIST
}



#
# See if we've received this email before and discard it if so.
# Allowing duplicates to pass through just causes the same message
# to get filtered multiple times into the same location, which
# is not useful.
#
:0 Wh: $CACHELOCK
| formail -D 8192 $CACHE


#
# Rules that make changes to the incoming message
#
INCLUDERC=$PM_RULES/adjust.rules



#
# Mailing lists. Most mailing lists have some characteristic pattern that
# can be tested. Failing that, test ^TOmailinglistaddress.
#
# This section requires heavy configuration and you _MUST_ get it right.
# You _MUST_ catch every mailing list you are on in this section.
#
:0 c
* $ ^$OBVIOUSLYTO($MYADDR|$PWDADDR)
* ? test x$LISTCC = xboth
{
	INCLUDERC=$PM_RULES/list.rules

	:0
	/dev/null
}

:0
* 1^0 !$ ^$OBVIOUSLYTO($MYADDR|$PWDADDR)
* 1^0 ?  test x$LISTCC = xlist
{
	INCLUDERC=$PM_RULES/list.rules
}



#
# Blacklist. The file blacklist file contains a list of addresses (one per
# line) that are matched against the sender. If there is a match, then
# the sender is blacklisted and we discard the mail.
#
# Including the following two lines after the ":0 Whic" line will allow
# blacklisted people to still get through by providing the password.
#
# * !$ ^$OBVIOUSLYTO$PWDADDR
# * !$ ^Subject:.*$PASSWORD
#
# You may also wish to remove the "INCLUDERC" line so blacklisted
# address just go directly into bit-bucket.
#

:0 Whic
* !$ ^$OBVIOUSLYTO$PWDADDR
* !$ ^Subject:.*$PASSWORD
| chkmail --header "Received|From|Sender|Reply-To|Message-ID" $BLACKLIST

:0 a
{
	LOG="Black List: "

	INCLUDERC=$PM_RULES/black.rules

	:0
	$SPAMBOX
}


#
# Handle any user rules for non-blacklisted mail.
#

INCLUDERC=$PM_RULES/user.rules


#
# Whitelist. This is similar to the blacklist but with the opposite
# effect.  If we get this far, the letter is personal but it might
# still be spam, so we check the whitelist to see if it is from
# someone we know.  If it is, filter it accordingly.
#

:0 Whic
| chkmail --header "From|Sender" $WHITELIST $GREYLIST

:0 a
{
	LOG="White/Grey List: "

	INCLUDERC=$PM_RULES/white.rules

	:0:
	$DEFAULT
}



#
# Okay, at this point it hasn't been caught by any of the lists (black, white,
# or grey) so just try to do some intelligent processing.
#



#
# It wasn't from anyone we know. Does it have the password? If so, assume it
# is valid and filter it appropriately.
#

:0
* $ (^Subject:.*$PASSWORD)|(^$OBVIOUSLYTO$PWDADDR)
{
	LOG="Password Provided: "

	INCLUDERC=$PM_RULES/password.rules

	:0:
	$DEFAULT
}



#
# Valid, but no password.
#
LOG="Unknown: "

INCLUDERC=$PM_RULES/nopassword.rules

LOG="(default) "

:0:
$NOPWBOX
