import java.io.*;
import java.net.*;
import java.util.*;

public class checkhotmail
{
  public static void main (String[]args) throws IOException
  {
   // NOTES: This program did not initially work when I transfered it from 
   // MS Windows to Unix. My initial reaction was to write bitter asides about
   // 'write once, run nowhere' however, with some investigation it turns out
   // to be a DOS/ UNIX line ending problem (again). The Hotmail server expects
   // MS DOS style line endings but the java environment on a Unix computer will
   // by default send unix style line endings when doing a 'println' or something
   // similar.
   // To run this on MS Windows, you need to remove all the "\r" from the
   // command strings. By adding the '\r' in combination with the standard \n
   // which the unix println generates we obtain an MS DOS new-line combination
    Socket soTcpSocket = null;
    PrintWriter pwTcpSocket = null;
    BufferedReader brServerResponseStream = null;
    ArrayList aaLineList;
    String sMailServerHost = "mx3.hotmail.com";
    String sFilePath = "addresses.txt";

    aaLineList = new ArrayList ();

    if (args.length == 1)
    {
      sFilePath = args[0];
    }
    else if (args.length == 0)
    {
      System.out.
      println ("check-hotmail:                                  \n" +
       "-------------------------------                 \n" +
       "  Command line usage:                           \n" +
       "  java check-hotmail email-account-list-file  \n\n" +
       "  The purpose of this program is to check the validity of a     \n" +
       "  list of hotmail accounts. The list is contained in a text file \n" +
       "  where each line of the text file contains one hotmail email \n" +
       "  address.               \n" +
       "                         \n" +
       "  Examples:              \n" +
       "  ---------              \n" +
       "    java check-hotmail the-addresses.txt                \n" +
       "      The program checks each of the addresses contained in the \n" +
       "      file 'the-addresses.txt' and returns the server responses. \n" +
       "                            \n");

       System.exit (-1);

    }  //-- if, else [Command Line Arguments]


    try
    {
      FileReader firTheFile = new FileReader (sFilePath);
      BufferedReader buffrTextFile = new BufferedReader (firTheFile);

      String sTextLine;

      sTextLine = buffrTextFile.readLine();
      while (sTextLine != null)
      {
        aaLineList.add (sTextLine);
        sTextLine = buffrTextFile.readLine();
      }
    }
    catch (IOException e)
    {
      System.err.println ("The text file could not be read: " + sFilePath);
      System.exit (1);
    }        //-- try,catch

    try
    {
      soTcpSocket = new Socket (sMailServerHost, 25);
      pwTcpSocket = new PrintWriter(soTcpSocket.getOutputStream(), true);
      brServerResponseStream = new BufferedReader(
        new InputStreamReader (soTcpSocket.getInputStream ()));
    }
    catch (UnknownHostException e)
    {
      System.err.println ("It was not possible to resolve the host-name: " + sMailServerHost);
      System.exit (1);
    }
    catch (IOException e)
    {
      System.err.println ("Couldn't get I/O for "
        + "the connection to: " + sMailServerHost);
      System.exit (1);
    }

    BufferedReader stdIn =
      new BufferedReader (new InputStreamReader(System.in));
    String userInput;
    String sServerResponse;

    sServerResponse = brServerResponseStream.readLine();
    System.out.println ("INITIAL RESPONSE: " + sServerResponse);
    sServerResponse = "";
    String sCommand = "";



    sCommand = "ehlo www.ella-associates.org \r";
    pwTcpSocket.println(sCommand);
    System.out.println("SENDING= " + sCommand);
    sServerResponse = brServerResponseStream.readLine() + "\n";

    System.out.println("brServerResponseStream.ready() = " + brServerResponseStream.ready());
    while(brServerResponseStream.ready())
    {
      sServerResponse += brServerResponseStream.readLine() + "\n";
    }

    System.out.println ("RESPONSE: " + sServerResponse);


    sServerResponse = "";
    sCommand = "mail from: <matthew@ella-associates.org> \r";
    pwTcpSocket.println(sCommand);
    System.out.println("SENDING: " + sCommand);

    for (int ii = 0; ii < 1; ii++)
    {
      sServerResponse += brServerResponseStream.readLine () + "\n";
    }

    System.out.println ("RESPONSE: " + sServerResponse);


    for (int ii = 0; ii < aaLineList.size(); ii++)
    {
      //System.out.println((String)aaLineList.get(ii));

      sServerResponse = "";
      sCommand = "rcpt to: " + (String) aaLineList.get(ii) + "\r";
      pwTcpSocket.println(sCommand);
      System.out.println("SENDING: " + sCommand);

      sServerResponse += brServerResponseStream.readLine () + "\n";

      while (brServerResponseStream.ready())
      {
	  sServerResponse += brServerResponseStream.readLine () + "\n";
      }

      System.out.println ("RESPONSE: " + sServerResponse);
    }        //-- for   

    sServerResponse = "";
    sCommand = "quit \r";
    pwTcpSocket.println (sCommand);
    System.out.println ("SENDING: " + sCommand);

    for (int ii = 0; ii < 1; ii++)
    {
      sServerResponse += brServerResponseStream.readLine () + "\n";
    }

    System.out.println ("RESPONSE: " + sServerResponse);


    pwTcpSocket.close ();
    brServerResponseStream.close ();
    stdIn.close ();
    soTcpSocket.close ();
  }   // main
}   // class: checkhotmail 
