#!/bin/bash

# Description:
#   This script is designed to check a list of Hotmail addresses
#   for 'validity', that is, if the addresses are actually real and
#   'active'. The script does this without sending an test email to
#   to the addresses. The script uses a java application 'checkhotmail.class'
#   which sends a series of raw SMTP strings to the Microsoft Hotmail
#   SMTP server and watches the responses.
#
# 
# Notes:
#   This script, as usual, took a large amount of time to get working. (probably about
#   4 hours, after the 'checkhotmail.class' java program had been written. This was because
#   if various 'gotcha' mainly involving the way the lines are terminated in various 
#   environments. Especially the way taht the 'procgi' script interprets textarea newline
#   characters is a little surprising.
#
#   It is possible that this script running for a browser on a Unix type operating system
#   will have some problems. This is because of some of the strange 'line ending' translations
#   which have been done, and because of the '<pre>' tags used. (Although the man2html script
#   which converts unix man pages to HTML also uses '<pre>' tags, so maybe they are not such
#   a problem.
#
#   In migrating from Debian to Redhat Linux, the following things had to be fixed: The 
#   path to the java executable had to be altered, and the directory in which the 
#   script resides had to be given 'write' permissions for 'others'.
# Ideas:
# Dependencies:
#   checkhotmail.class
#
# See Also:
# Author:
#    mjb   matth3wbishop@yahoo.com

JAVA="/usr/java/j2sdk1.4.1_02/bin/java"

# The line below parses the querystring or posted environment 
# variables. It uses a program which was found at.
#   http://www.fpx.de/fp/Software/ProcCGIsh.html
# 
eval "`./procgi $*`"

  sEmailAddressList=$FORM_emailaddresslist

  echo "Content-type: text/html"
  echo ""
  echo "
    <html>
    <head><title>A Page To Check Hotmail Email Addresses</title></head>
    <body>
    <h2>A Page To Check Hotmail Email Addresses (i.e. See if they exist)</h2>
    P L E A S E&nbsp;&nbsp;&nbsp;&nbsp;N O T E: This program does <em>not</em> send 'test' emails to the email 
    addresses which you
    enter in the box below. The owners of the email addresses will <em>not</em> receive anything in their
    inbox from this program.<br><br>

    H I N T:  The Hotmail email address that you 'cut-and-paste' into this text box
    can be separated by semicolons (;) or commas (,) or spaces ( )  or they can be on separate lines or
    you can use a combination of these techniques<br><br>

    <em>please send questions to:</em> matthew@ella-associates.org or matth3wbishop@yahoo.com
    <form method = \"post\" action = \"\">
    <strong>Please enter (or cut-and-paste) the Hotmail Email Addresses which you wish to check</strong><br>
    <textarea  rows = \"15\"      cols = \"70\" name = \"emailaddresslist\">"


  if [ "$sEmailAddressList" = "" ] 
  then
    echo ""
  else
    echo $sEmailAddressList
  fi
  
  echo "
    </textarea><br>
    <input     type = \"submit\" value = \"C H E C K   T H E   A D D R E S S E S   N O W\"
               name = \"submit\"><br>
              
    </form>
       "

  # This second empty echo line IS necessary. A 'server error' will
  # be generated by the web-server if it is not present.


  if [ "$sEmailAddressList" = "" ] 
  then
       
    echo ""
  else
    #-- We need to get rid of the MS Line Ending format, if the 
    #-- text was entered on an MS Windows computer.
    #-- finding the -a switch for fromdos took some time.
    #-- For some reason the 'fromdos' command gets rid of all
    #-- line breaks. It is possible that the 'proccgi' program is 
    #-- doing something unpleasant to the FORM values

    #--sEmailAddressList=$(echo $sEmailAddressList | tr ';' '\r')
    echo $sEmailAddressList | expand | \
      tr ';' '\r' | tr ',' '\r' | tr ' ' '\r' | \
      tr '\r' '\n' | tr -s '\n' | \
      sed -e "/^[[:space:]]*$/d" \
          -e "s/^[[:space:]]*//g" \
	  -e "s/[[:space:]]*$//g" > hotmail-address-list.temp
    #-- Another 'gotcha', because the cgi process does not have the same
    #-- path as the bash shell it needs the full path to the java executable.
    #-- More gotchas: The textarea value, as interpreted by the 'procCgi' script above
    #-- returns a value in which each line is separate by a 'carriage return' as in
    #-- '\r'. Also the Hotmail SMTP server returns results which contain carriage returns
    #-- as in \r\n (or this might be my Java program.
    #--
    #-- Also, when I put to sed -e sections on different lines below (with a line
    #-- continuation character, the script 'crapped out'. Perhaps this is because I had
    #-- a 'space' character after the line continuation character (ie \) YEP
    echo "<h3>Checking Addresses, please wait a few moments...</h3>"
    
    $JAVA checkhotmail hotmail-address-list.temp | \
      tr -d '\r' | \
      sed -e "1,/mail from/d"  \
          -e "/rcpt to/{N;s/\n/ /g;s/SENDING: rcpt to:[[:space:]]*//g;}" \
          -e "s/RESPONSE:[[:space:]]*\(2[0-9][0-9]\).*/ADDRESS OK \[code=\1\]/g" \
          -e "s/RESPONSE:[[:space:]]*\(5[0-9][0-9]\).*/ADDRESS INVALID \[code=\1\]/g" | \
      tr -s '\n' > results.temp 
    if [ $(grep "ADDRESS INVALID" results.temp | wc -l | tr -d ' ') -gt 0 ]
    then
      echo "<strong>The following addresses are not valid (don't exist currently)</strong><br>"
      echo "<pre>"
      grep "ADDRESS INVALID" results.temp
      echo "</pre>"
    else
      echo "<strong>There are NO invalid addresses. ALL the email addresses are ok</strong><br>"
    fi
    
    echo "<hr><strong>Here are the results of for ALL the email addresses checked</strong><br>"
    echo "<pre>"
    cat results.temp
    echo "</pre>"
  fi


  echo "
    </body></html>"
  
  rm -f results.temp
  rm -f hotmail-address-list.temp