#!/bin/csh -f

# This script sets up a basic spam filtering scheme using procmail.
# The .procmailrc script is commented so the user can figure out
# what's going on.

# daw, 3/5/04

switch (`uname -s``uname -r`)
case SunOS5*:
	breaksw
default:
	exec echo ${0}: does not run on `uname -s` `uname -r`
endsw

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	default:
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

cd

if ($?VERBOSE) echo `date +%T` Checking for existing files

set EXISTS

foreach FILE ( .forward .procmailrc mail )
    if (-d $FILE && $FILE == "mail") continue
    if (-e $FILE) set EXISTS = ( $EXISTS $FILE )
end

if ("$EXISTS" != "") then
    if ($#EXISTS > 1) then
	echo The files $EXISTS already exist in ~
	/usr/ucb/echo -n "I need to rewrite these files.  Shall I rename them and proceed? "
    else
	echo The file $EXISTS already exists in ~
	/usr/ucb/echo -n "I need to rewrite this file.  Shall I rename it and proceed? "
    endif

    set INPUT = $<
    set INPUT = `echo "$INPUT" | tr "[A-Z]" "[a-z]"`

    switch ("$INPUT")
    case n:
    case no:
	exec echo "[Not confirmed]"
	breaksw
    case y:
    case ye:
    case yes:
	breaksw
    default:
	exec echo '    Type "yes" to confirm'
	breaksw
    endsw

    if ($?VERBOSE) echo `date +%T` "Renaming file(s)"

    set TSTMP = `date +%y%m%d`

    foreach FILE ( $EXISTS )
	if (-e $FILE.$TSTMP) set TSTMP = `date +%y%m%d.%H%M`
    end

    foreach FILE ( $EXISTS )

	echo "    $FILE => $FILE.$TSTMP"
	/bin/mv $FILE $FILE.$TSTMP

    end

endif

if (! -e "mail") then
    if ($?VERBOSE) echo `date +%T` Setting up mail directory
    mkdir mail
    chmod 700 mail
endif

if ($?VERBOSE) echo `date +%T` Setting up .procmailrc

set ME = `/usr/ucb/whoami`

cat >> .procmailrc <<EOF
# This file created by $0 
#    `date`

# First, distribute mail you *know* is legit:

# The following recipe routes the CommuniGate Pro Discussion Digest
# to my normal inbox, even though the spam filter sometimes thinks
# it looks like spam.  (You could also specify "From:", "Sender:",
# or any other header line to route on.)

:0:
* ^Subject: CGatePro Digest
\$ORGMAIL

# The following recipe sorts all email from dragon notifying you of
# attempted virus deliveries to a file called virus in the mail
# subdirectory of my home directory.

:0:
* ^From: automated_antivirus_software@dragon.rutgers.edu
\$HOME/mail/virusnotices

# Then, check for possible spam:

# While all mail sent through dragon is scanned for spam, there are
# other routes.  Make sure *all* mail has been scanned.

:0fw
* !^X-Spam-Checker-Version: 
| /usr/bin/spamc -d spamfilter.rutgers.edu -p 7831
:0fw
* ! [ 	]*spamfilter.*.rutgers.edu
| /usr/bin/spamc -d spamfilter.rutgers.edu -p 7831

# The following recipe sorts all email tagged as spam to a file called
# spam in the mail subdirectory of my home directory.

:0:
* ^X-Spam-Flag: YES
\$HOME/mail/spam

# The following recipe discards all email tagged as spam.  Uncomment
# this recipe and comment out or remove the above recipe to use this
# one. 
# 
#  *** NOTE: YOU WILL PROBABLY LOSE VALID EMAIL IF YOU USE THIS. ***

#:
#* ^X-Spam-Flag: YES
#/dev/null

# The following recipe will reply to email when you're on vacation.
# (See "man vacation" for details)
# To enable vacation responses, do the following:
#
#    mv .forward .forward.hld	# vacation won't set up an auto-response
				#  if there is already a .forward there
#    vacation			# create vacation response message
				#  and initialize the vacation database
#    mv .forward.hld .forward	# restore the .forward which runs
				#  procmail (which will in turn run
				#  vacation on mail which is not spam)
#
# and finally, uncomment the recipe below.
#
# (To turn off vacation responses, simply comment the below recipe again)

#:0c
#* !^X-Spam-FLAG: YES
#| /usr/bin/vacation $ME

EOF

if ($?VERBOSE) echo `date +%T` Setting up .forward

cat >> .forward <<EOF
"|IFS=' ';exec /usr/local/bin/procmail #$ME"
EOF

chmod 600 .forward

cat <<EOF

    Ok, potential spam will now be filed in mail/spam.  Virus notices
    from dragon will be filed in mail/virusnotices.

    It is VERY IMPORTANT that you review your spam folder periodically.
    The spam agging is not perfect, and THERE WILL BE FALSE POSITIVES
    (mail tagged as spam which is legitimate).  You should edit your
    .procmailrc to make exceptions for any false positives you find.

EOF
