#!/bin/sh
#############################################
# listproc.badsubs
#############################################
# Finds potentially Bad Subscriber addresses in 
# ListProc's .subscribers files
#############################################
# by: David Rosenthal <davidr@shamash.org>
# Version 1.0 Date: November 1998 David Rosenthal <davidr@shamash.org>
# Version 1.1 Modified at OPN by John David Duncan <jdd@efn.org> 
# Version 1.2 Date: April 18, 1999 David Rosenthal <davidr@shamash.org>
#############################################
# Please note that this is a "fast and dirty" way to check for illegal
# addresses.  These rules, generally work, but sometimes I've hard coded
# exceptions into the rules below for my site... you'll have to do the
# same.
#
# Version 1.2 is virtually a complete rewrite, which is faster, and
# which looks for many more potential error causing symbols.  Its also
# easier to add "exceptions" to, and to add new searches to.  It does
# create  a few temp files, one of which $SUBSCRIBERS is fairly large.
#############################################

## Define Variables Based on your configuration
# Your ListProc directory
LPDIR="/var/listproc"

# This script creates 3 temporary files. Where should those go?
#   They will be deleted when the script is done running.
TMP="/tmp"

######################################
## Preset Variables 
SUBSCRIBERS="$TMP/listproc.badsubs.subscribers"
ERRORS="$TMP/listproc.badsubs.errors"
RESULTS="$TMP/listproc.badsubs.results"

if [ ! -d $LPDIR ] ; then
  echo "The directory $LPDIR does not exist on your system."   
  echo "Please define the \$LPDIR variable in $0"
  exit
fi

## Make sure that each file has protective permissions
touch $SUBSCRIBERS
chmod 700 $SUBSCRIBERS
touch $ERRORS
chmod 700 $ERRORS
touch $RESULTS
chmod 700 $RESULTS


##Get all the .subscribers files
echo "Obtaining necessary data (this is the slow part)..."
cut -d" " -f1 $LPDIR/lists/*/.subscribers \
  | grep -v "^$" | grep -v "^help$" \
  > $SUBSCRIBERS

## Checks for illegal symbols
echo "Checking for common syntax errors in subscribers files..."
echo "  Looking for brackets in addresses..."
# Find the triangular brackets (these can crash ListProc)
  grep ">" $SUBSCRIBERS >> $ERRORS
  grep "<" $SUBSCRIBERS >> $ERRORS
# Find the square brackets
  grep "\[" $SUBSCRIBERS >> $ERRORS
  grep "\]" $SUBSCRIBERS >> $ERRORS
# Find the curvy brackets (these symbols may not be universally illegal)
  grep "{" $SUBSCRIBERS | grep -v cstat.co.za  >> $ERRORS
  grep "}" $SUBSCRIBERS | grep -v cstat.co.za  >> $ERRORS

echo "  Looking for quotes, colons, semicolons, & commas in addresses..."
# Find the quotes
  grep "\"" $SUBSCRIBERS >> $ERRORS
# Find semicolon and colon (a colon at the end of an address crashs ListProc)
  grep "\;" $SUBSCRIBERS >> $ERRORS
  grep "\:" $SUBSCRIBERS >> $ERRORS
# Find commas
  grep "," $SUBSCRIBERS >> $ERRORS

echo "  Looking for periods at the ends of addresses..."
# Find a period at the end of an email address
  grep "\.$" $SUBSCRIBERS >> $ERRORS

echo "  Looking for @ signs in addresses..."
# Check to make sure that each email address has an "@" in it, and that 
# it is not the last character.  This ensures addresses are fully qualified,
# not on our machine. I have many errors when list owners add addresses like
# "someone" when they mean to add "someone@aol.com".  This typically
# results in an error message, so I check for this now.
  grep "@$" $SUBSCRIBERS >> $ERRORS
  grep -v "@" $SUBSCRIBERS >> $ERRORS


## Get full information about each entry in the $ERRORS file
  for line in `cat $ERRORS` ; do
    grep "^$line " $LPDIR/lists/*/.subscribers >> $RESULTS
  done

## Sort and Present the data
echo "Sorting data alphabetically and removing duplicates...."
echo 
cat $RESULTS | sort | uniq
echo
echo "If you edit a .subscribers file by hand, be sure to run"
echo "    $LPDIR/revdb update <listname>" 
echo "for each listname you edit by hand, if you use ListProc 8.2 or higher"

## Cleanup temp files

rm $SUBSCRIBERS
rm $ERRORS
rm $RESULTS 
