#!/usr/bin/bash
# DSPAM Maintenance
# Go through the list of users and generate a DSPAM PENDING list for any
# user that is using DSPAM (has a .dspam directory)
#
DEBUG=0
DOIT=1
DAYLIMIT='14'
DATE=`date '+%b %d, %Y'`
HOSTNAME=`hostname`
IFS=":";

## Make sure we can figure out MTA
#if [ ! -f /etc/dspamrc ]; then
#  echo "No /etc/dspamrc"
#  exit 1
#fi

# Figure out MTA from /etc/dspamrc
mta="postfix"
#qmailproc=`grep MAIL_TRANSFER_AGENT /etc/dspamrc | grep qmail`
#if [ "$qmailproc"x != "x" ]; then
#  mta="qmail"
#fi

getent passwd |
(
while read LOGINID PASSWD PUID PGID NAME HOME USERSHELL
   do
   if [ -d $HOME/.dspam ]; then
      EMAIL="$LOGINID@$HOSTNAME"
      LOCKFILE="/var/tmp/dspam-pending.$LOGINID.lock"

      if [ "$DEBUG"x != "0"x ]; then
         echo "Processing $EMAIL"
      fi

      # Create dspam extension address if it doesn't exist.
      if [ "$mta" = "qmail" ]; then
        if [ ! -f $HOME/.qmail-dspam-default ]; then
          echo "| /usr/sbin/dspam-mail-proc" > $HOME/.qmail-dspam-default
          chown $LOGINID "$HOME/.qmail-dspam-default"
        fi
      else
        if [ ! -f $HOME/.forward+dspam ]; then
          echo "| /usr/sbin/dspam-mail-proc" > $HOME/.forward+dspam
          chown $LOGINID "$HOME/.forward+dspam"
        fi
      fi

      if [ "$DOIT"x = "1"x ]; then
         # silently and immediately delete all messages older than DAYLIMIT days
         /bin/su - $LOGINID --command="cd $HOME/Maildir/.Pending && find cur new tmp -type f -mtime +$DAYLIMIT -delete"

         # mail a summary report of all new pending messages
         /bin/su - $LOGINID --command="setlock -n -X $LOCKFILE /usr/sbin/dspam-pending-mail $LOGINID"
         if [ $? != "0" ]; then
           echo "Can't set lock \"$LOCKFILE\".  Exiting."
         fi
         rm -rf $LOCKFILE
      fi

      # If there is a delivery error, notify me
      FAILURE_NOTICE=$HOME/TMDA_DELIVERY_FAILURE
      SUBECT="TMDA DELIVERY FAILURE ($DATE)"
      if [ -f $FAILURE_NOTICE ]; then
        mail -s "$SUBJECT" $EMAIL < $FAILURE_NOTICE
        rm -f $FAILURE_NOTICE
      fi

      # Rotate the .dspam/incoming.log file.
      day=$DAYLIMIT
      while [ "$day"x != "0"x ];
      do
        printday=`printf "%02d" $day`
        currentfile="$HOME/.dspam/incoming.log.$printday"
        if [ "$day"x != "1"x ]; then
          previousnum=`expr $day - 1`
          previousnum=`printf "%02d" $previousnum`
          previousfile="$HOME/.dspam/incoming.log.$previousnum"
        else
          previousfile="$HOME/.dspam/incoming.log";
        fi
        if [ -f $previousfile ]; then
          mv $previousfile $currentfile
        fi
      day=`expr $day - 1`
      done
   else
     # Remove dspam extension address because .dspam does not exist.
     if [ "$mta" = "qmail" ]; then
       if [ -f $HOME/.qmail-dspam-default ]; then
         rm -f $HOME/.qmail-dspam-default
       fi
     else
       if [ -f $HOME/.forward+dspam ]; then
         rm -f $HOME/.forward+dspam
       fi
     fi
   fi
   done
)
