package imap;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Using the IMAP protocol
// This servlet is the one that stores the login information
// to a cookie (for use by the other servlets in this project)
// and also report to you how many new messages are this user's
// inbox (those withthe recent flag attached to them)
// You can only go to mailboxes viewing mode from here
public class ImapLogon extends HttpServlet {

  // Defining resulting object out from request req and response res
  public void doPost(HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException {

    res.setContentType("text/html");
    ServletOutputStream out=res.getOutputStream();

    try {

 	// Retrieve parameter information
      String username=req.getParameter("username");
      String password=req.getParameter("password");
      String hostname=req.getParameter("hostname");

      // Check to make sure entries were made for all request parameters
      if (username.equals("")) {
	throw new ServletException("Missing Username");
      }
      if (password.equals("")) {
	throw new ServletException("Missing Password");
      }
      if (hostname.equals("")) {
	throw new ServletException("Missing Hostname");
      }

      // Open a cookie for username/password
      Cookie usercookie=new Cookie("username", username);
      Cookie passcookie=new Cookie("password", password);
      Cookie hostcookie=new Cookie("hostname", hostname);
      res.addCookie(usercookie);
      res.addCookie(passcookie);
      res.addCookie(hostcookie);

      // Creating a socket to the desired hostname's IMAP port
      Socket socket=new Socket(hostname, 143);
      PrintStream write_socket=new PrintStream(socket.getOutputStream());
      DataInputStream read_socket=new DataInputStream(socket.getInputStream());

      // Make sure target IMAP port is ready
      String imap_res=read_socket.readLine();
      if (! imap_res.startsWith("* OK")) 
	throw new ServletException("IMAP Server was not reachable this time");

      int command=1;

      // Make sure a correct username and/or password is provided
      write_socket.println(command+" login "+username+" "+password);
      imap_res=read_socket.readLine();
      if (! imap_res.startsWith(command+" OK")) 
	throw new ServletException("Invalid Username and/or Password");

      command++;

      String newemails="0";

      // Find out how much mail the user has
      write_socket.println(command+" select inbox");
      imap_res=read_socket.readLine();
      while (! imap_res.startsWith(command+" OK")) {
	StringTokenizer breakline=new StringTokenizer(imap_res, " ");
	breakline.nextToken();
	String secondword=breakline.nextToken();
	String thirdword=breakline.nextToken();
	if (thirdword.equals("RECENT"))
	  newemails=secondword;
	imap_res=read_socket.readLine();
      }

      out.println("<HTML><HEAD>");
      out.println("<TITLE>Login for "+username+" on "+hostname+"</TITLE></HEAD>");
      out.println("<BODY><H1>Login for "+username+" on "+hostname+"</H1><HR>");

      if (newemails.equals("0")) 
		out.println("You have no new mail.");

      else 
		out.println("You have "+newemails+" new mail(s).");

      out.println("<FORM ACTION=\"http://128.96.164.196:8080/servlet/imap.ImapListBox\" METHOD=\"GET\">");
      out.println("<INPUT TYPE=\"submit\" VALUE=\"Click here to select a mailbox\">");
      out.println("</FORM></BODY></HTML>");

      command++;
      write_socket.println(command+" logout");
    }

    catch (ServletException e) {
      out.println("<HTML><HEAD><TITLE>"+e+"</TITLE></HEAD>");
      out.println("<BODY>"+e+"</BODY></HTML>");
    }

    catch (UnknownHostException e) {
      out.println("<HTML><HEAD><TITLE>Unknown Host</TITLE></HEAD>");
      out.println("<BODY>"+e);
      out.println("The hostname supplied is not recognized, try a different hostname.");
      out.println("</BODY></HTML>");
    }

    catch (IOException e) {
      out.println("<HTML><HEAD><TITLE>IOException</TITLE></HEAD>");
      out.println("<BODY>"+e);
      out.println("</BODY></HTML>");
    }
  }
}
