package imap;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Using the IMAP protocol
// This servlet does not need any parameters
// It will bring you in mailboxes viewing mode
// and print all the available mailboxes of this
// user, and allow for creation and deletion of
// these mailboxes, as well as to get into them
public class ImapListBox extends HttpServlet {

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

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

    try {

      // Retrieve login information from preset cookie
      Cookie[] cookie=new Cookie[3];
      String[] login=new String[3];
      cookie=req.getCookies();

      String username="0", password="0", hostname="0";

      for(int i=0; i<=2; i++) {
        login[i]=cookie[i].getName();
        if (login[i].equals("username")) 
          username=cookie[i].getValue();
        else if (login[i].equals("password"))
          password=cookie[i].getValue();
        else if (login[i].equals("hostname"))
          hostname=cookie[i].getValue();
      }

      // 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++;

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

      command++;

      // Retrieve the list of mailboxes
      write_socket.println(command+" list \"\" *");
      imap_res=read_socket.readLine();
      while (! imap_res.startsWith(command+" OK")) {
	  if (imap_res.startsWith("* LIST (\\NoInferiors")) {
	    StringTokenizer breakline=new StringTokenizer(imap_res, " ");
	    for(int piece=1; piece<5; piece++)
	      breakline.nextToken();
          String mailbox=breakline.nextToken();
	    if (mailbox.equals("\"/\""))
		mailbox=breakline.nextToken();
	    out.println("<A HREF=\"http://128.96.164.196:8080/servlet/imap.ImapView?mailbox="+mailbox+"\"><B>MAILBOX :"+mailbox+"</B></A>");
	    out.println("<A HREF=\"http://128.96.164.196:8080/servlet/imap.ImapDelBox?mailbox="+mailbox+"\"> delete</A>");
          out.println("<FORM ACTION=\"http://128.96.164.196:8080/servlet/imap.ImapRenBox\" METHOD=\"GET\">Change Mailbox Name:<INPUT TYPE=\"text\" NAME=\"tomailbox\" SIZE=40>");
	    out.println("<INPUT TYPE=\"hidden\" NAME=\"mailbox\" VALUE=\""+mailbox+"\">");
	    out.println("<INPUT TYPE=\"submit\" VALUE=\"Change\">");
	    out.println("</FORM>");
	    out.println("<HR>");
	  }
	imap_res=read_socket.readLine();
      }

      out.println("<FORM ACTION=\"http://128.96.164.196:8080/servlet/imap.ImapCreateBox\" METHOD=\"GET\">Create a New Mailbox <INPUT TYPE=\"text\" NAME=\"mailbox\" SIZE=40>");
      out.println("<INPUT TYPE=\"submit\" VALUE=\"Create\">");
      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>");
    }
  }
}
