package group2.shenyang;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/***************************************************************************
Name: RetrieveMail.java

Input:
3 parameters from POP.java by using Cookies
> user: username
> pass: password
> host: hostname
> 1 parameter from POP.java by querystring
> mail: which msg to get
>
> Functionality:
> - Fetches cookies & the mail parameter
> - Logs into the POP server
> - Retrieves the appropriate msg
> - Bold the fields Subject, From, To, and Date
> - Logs out the server
>
> Output:
> The appropriate email
>
> Error Checks:
> Same as POP.java
> ****************************************************************************/

public class RetrieveMail extends HttpServlet{

     public void doGet(HttpServletRequest req, HttpServletResponse res)
     throws ServletException, IOException{

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

        try{
            // Determine fields + Get Cookies
            Cookie[] cookie=new Cookie[3];
            String[] name=new String[3];
            String user="blah", pass="blah", host="blah";
            cookie=req.getCookies();
            for(int j=0; j<=2; j++){
                name[j]=cookie[j].getName();
                if (name[j].equals("hostname")){
                    host=cookie[j].getValue();
                }
                else if (name[j].equals("password")){
                     pass=cookie[j].getValue();
                 }
                 else if (name[j].equals("username")){
                     user=cookie[j].getValue();
                 }
             }
             String mail=req.getParameter("mail");

             // Create socket and log in
             Socket sock=new Socket(host, 110);
             PrintStream in_sock=new PrintStream(sock.getOutputStream());
             DataInputStream out_sock=new DataInputStream(sock.getInputStream());
             String pop_res;
             pop_res=out_sock.readLine();
             if (! pop_res.startsWith("+OK")){
                 throw new ServletException("POP3 Server Not Ready");
             }
             in_sock.println("USER "+user);
             pop_res=out_sock.readLine();
             if (! pop_res.startsWith("+OK")){
                 throw new ServletException("Incorrect Username");
             }
             in_sock.println("PASS "+pass);
             pop_res=out_sock.readLine();
             if (! pop_res.startsWith("+OK")){
                 throw new ServletException("Incorrect Password");
             }

             // HTML tags
             out.println("<HTML><HEAD><TITLE>Your Email</TITLE></HEAD><BODY>");

             // Retrieve mail
             in_sock.println("RETR "+mail);
             String tmp=out_sock.readLine();
             boolean header=true;
             boolean body=false;
             while (! tmp.equals(".")){
                 tmp=out_sock.readLine();

                 // Boldface the Subject lines
                 if (header){
                     if (tmp.startsWith("Date:") || tmp.startsWith("From:") || tmp.startsWith("To:") || tmp.startsWith("Subject:")){
                         out.println("<B>"+tmp+"</B><BR>");
                     }
                     else{
                         out.println(tmp+"<BR>");
                     }
                 }
                 if (body) out.println(tmp);

                 if (tmp.equals("") && header){
                     body=true;
                     header=false;
                     out.println("<PRE>");
                 }
             }
             out.println("</PRE></BODY></HTML>");
             in_sock.println("QUIT");
         }
         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><H1>"+e+"</H1>");
             out.println("The hostname you supplied is unknown, try again!");
             out.println("</BODY></HTML>");
         }
         catch (IOException e){
             out.println("<HTML><HEAD><TITLE>IOException</TITLE></HEAD>");
             out.println("<BODY><H1>"+e+"</H1>");
             out.println("</BODY></HTML>");
         }
     }
 }
