1 package org.ibex.mail.protocol;
5 // Next step: implement both sides using the POP interface
7 class POPException extends IOException {
11 public abstract void userpass(String user, String pass) throws POPException;
12 public abstract void apop(String user, String digest) throws POPException;
13 public abstract BufferedReader top(int m, int maxlines);
14 public abstract BufferedReader retr(int m);
15 public abstract long stat(); // top 32 bits is number of messages, bottom 32 is total size
16 public abstract long[] list(); // top 32 bits is message number, bottom 32 is size
17 public abstract long list(int m);
18 public abstract void dele(int m);
19 public abstract void noop(int m);
20 public abstract void rset(int m);
21 public abstract String uidl(int m);
22 public abstract String[] uidl(); // FIXME, also needs message number
25 public class POP3 extends MessageProtocol {
27 public static void main(String[] args) throws Exception {
28 ServerSocket ss = new ServerSocket(110);
30 System.out.println("listening");
31 final Socket s = ss.accept();
32 System.out.println("connected");
37 } catch (Exception e) {
45 public static void service(Socket s) throws IOException {
46 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
47 PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
48 pw.print("+OK POP3 server ready\r\n");
53 String command = br.readLine().trim();
54 System.out.println("command: " + command);
55 if (command.toUpperCase().startsWith("QUIT ")) {
58 } else if (command.toUpperCase().startsWith("USER ")) {
59 user = command.substring(5).trim();
60 pw.print("+OK now give me your password\r\n");
62 } else if (command.toUpperCase().startsWith("PASS ")) {
64 pw.print("-ERR I need your password first\r\n");
67 pass = command.substring(5).trim();
72 System.out.println("login from " + user + " / " + pass);
73 String server = user.substring(user.indexOf('@') + 1);
74 user = user.substring(0, user.indexOf('@'));
75 Socket pop = new Socket(InetAddress.getByName(server), 117);
77 BufferedReader br2 = new BufferedReader(new InputStreamReader(pop.getInputStream()));
78 PrintWriter pw2 = new PrintWriter(new OutputStreamWriter(pop.getOutputStream()));
80 System.out.println("pop said " + br2.readLine());
81 pw2.print("USER " + user + "\r\n");
83 System.out.println("pop said " + br2.readLine());
84 pw2.print("PASS " + pass + "\r\n");
86 System.out.println("pop said " + br2.readLine());