#!/bin/sh

#
# $Id: greyadmin,v 1.14 2003/06/07 22:46:44 alan Exp alan $
#

# Greylist admin script.
# Run from crontab.
# Or, perhaps, put a a symlink in ~/bin to get it on the path.

# if $1 = "update", the lists are copied to .old and the
# output of full lists is suppressed

# $2 can be the limit on the cut days, eg 7

##########  user configuration  ####################
MARP_DIR="${HOME}/.procmail/marp"
##########  end of use configuration  #############

# MARP_MAIL_DIR needed
# MARP_WHITE_MAILBOX needed
. "${MARP_DIR}/userdata.sh"


UPDATELISTS=${1:-"x"}
# echo ${UPDATELISTS}

CUTDAYS=15
[ $(($2)) -gt 0 ] && CUTDAYS=${2}
CUTDATE=`date +%s -d "${CUTDAYS:-"17"} days ago"`
# check for bad date
CUTDATE=${CUTDATE:-"Bad date"}

# echo ${CUTDAYS}
# echo ${CUTDATE}


GREY_MAILBOX=${MARP_MAIL_DIR}/marp.greymail
GREY_MAILBOX_TMP=${GREY_MAILBOX}.$$


GREYPROCMAILRC=${MARP_DIR}/greyadmin.rc
WHITELIST=${MARP_DIR}/white.list
GREYLIST=${MARP_DIR}/grey.list

GREYLOG=${MARP_DIR}/grey.log


############### functions #########################
listlists()
{
  #list the grey list
  if [ -f "${GREYLIST}" ]; then
    echo "The grey list"
    cat -bs "${GREYLIST}"
  fi

  #list the white list
  if [ -f "${WHITELIST}" ]; then
    echo "The white list"
    cat -bs "${WHITELIST}"
  fi
}


comparelists()
{
  # GREY
  if [ -s "${GREYLIST}" ]; then
    if [ -s "${GREYLIST}.old" ]; then
      # echo "Two grey lists exist"
      # find if there are any new names in the greylist
      NEWADDRESSES=`grep -F -ixvc -f "${GREYLIST}.old" "${GREYLIST}"`
      if [ $NEWADDRESSES -ne 0 ]; then
        echo "New greys:  ${NEWADDRESSES}"
        grep -F -ixvn -f "${GREYLIST}.old" "${GREYLIST}"
        echo
      fi
    fi
  fi
  # WHITE
  if [ -s "${WHITELIST}" ]; then
    if [ -s "${WHITELIST}.old" ]; then
      # echo "Two white lists exist"
      # find if there are any new names in the whitelist
      NEWADDRESSES=`grep -F -ixvc -f "${WHITELIST}.old" "${WHITELIST}"`
      if [ $NEWADDRESSES -ne 0 ]; then
        echo "New whites:  $NEWADDRESSES"
        grep -F -ixvn -f "${WHITELIST}.old" "${WHITELIST}"
        echo
      fi
    fi
  fi
}


updatelists()
{
  # do if update requested
  if [ ${UPDATELISTS} = "update" ]; then
    if [ -s "${GREYLIST}" ]; then
      # copy greylist to old
      cp -f "${GREYLIST}" "${GREYLIST}.old"
      # remove greylist
      lockfile "${GREYLIST}.lock"
      echo "This file should not be empty" > "${GREYLIST}"
      rm -f "${GREYLIST}.lock"
    fi
    if [ -s "${WHITELIST}" ]; then
      # copy whitelist to old
      cp -f "${WHITELIST}" "${WHITELIST}.old"
    fi
  fi
}



tidygreyfolder()
{
  # TIDY THE GREY FOLDER
  # This moves mail out of the grey folder into the white folder
  # if the from name appears in the white list

  # Lock the greylist mailbox whilst moving
  # Lock the temp greylist mailbox whilst processing
  lockfile "${GREY_MAILBOX}.lock" "${GREY_MAILBOX_TMP}.lock"

  # check for existence of file
  if [ -f "${GREY_MAILBOX}" ]; then

    # delete the temporary mailbox if it exists
    rm -f "${GREY_MAILBOX_TMP}"

    # remove the temporary folder mailbox lock
    rm -f "${GREY_MAILBOX_TMP}.lock"
    # echo "${GREY_MAILBOX}.lock removed"

    # Feed each message from the mailbox to the
    # procmail receipes to filter out the whitelisted mail.
    < "${GREY_MAILBOX}" formail -Y -s procmail -m "${GREYPROCMAILRC}" \
    "${MARP_MAIL_DIR}" "${MARP_DIR}" "${GREY_MAILBOX_TMP}" "${GREYLOG}" ${CUTDATE} \
    "${MARP_WHITE_MAILBOX}" "${WHITELIST}"
    
    # copy the temp file to the real file
    if [ -f "${GREY_MAILBOX_TMP}" ]; then
      cp -f "${GREY_MAILBOX_TMP}" "${GREY_MAILBOX}"
    else
      # just remove the folder
      rm -f "${GREY_MAILBOX}"
      # touch so Pine doesn't get upset if the file is missing
      touch "${GREY_MAILBOX}"
      chmod 0600 "${GREY_MAILBOX}"
    fi

  else
    # just remove the temporary mailbox lock
    rm -f "${GREY_MAILBOX_TMP}.lock"
  fi
  # remove the temp file and the real box lock 
  rm -f "${GREY_MAILBOX_TMP}"
  rm -f "${GREY_MAILBOX}.lock"
}



############### end of functions ###################




# Make sure the files exist for grep, with a line, otherwise
# grep, in procmailrc as well, gets upset
if [ ! -s "${WHITELIST}" ]; then
  echo "This file should not be empty" >> "${WHITELIST}"
fi
if [ ! -s "${GREYLIST}" ]; then
  echo "This file should not be empty" >> "${GREYLIST}"
fi


if [ ${UPDATELISTS} = "update" ]; then
  comparelists
  updatelists
  tidygreyfolder
  echo
  echo "marp.greymail log"
  mailstat -tms "${GREYLOG}"
  echo
else
  listlists
  comparelists
  echo
  echo "marp.greymail log"
  mailstat -tmsk "${GREYLOG}"
  echo
fi
 







