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

/*****************************************************************************
Name: Update.java

Input:
3 parameters from POP.java by using Cookies
user: username 
pass: password 
host: hostname
1 parameter from POP.java by querystring 
msg: total # of msgs that the user has

Functionality:
- Fetches cookies & the msg parameter
- Logs into the POP server
- Decide which emails to delete and delete them
- Logs out from the server

Output:
A HTML page stating how many emails were deleted and how many were left on
the server.

Error Checks:
Same as POP.java
****************************************************************************/

public class Update 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
	    int msg=Integer.parseInt(req.getParameter("msg"));
	    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();
                }
            }
 
	    // 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");
	    }

	    // Figure out which emails to delete
	    int numDel=0, i;
	    String del;
	    for(i=1; i<=msg; i++){
		del=req.getParameter("del"+i);
		if (del != null && del.equals("on")){
		    in_sock.println("DELE "+i);
		    pop_res=out_sock.readLine();
		    if (! pop_res.startsWith("+OK")){
			throw new ServletException("POP3 Server Error: msg "+i+"cannot be deleted");
		    }
		    numDel++;
		}
	    }
	    in_sock.println("QUIT");

	    // Output HTML page
	    out.println("<HTML><HEAD><TITLE>"+numDel+" Messages Deleted</TITLE></HEAD>");
	    out.println("<BODY><H1>"+numDel+" Messages Deleted</H1>");
	    int mailLeft=msg-numDel;
	    out.println("You have "+mailLeft+" mail(s) left on server</BODY></HTML>");
	}
	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>"); 
        } 
    }
}

