import
[org.ibex.mail.git] / src / org / ibex / mail / protocol / POP3.java
1 package org.ibex.mail.protocol;
2 import java.net.*;
3 import java.io.*;
4
5 // Next step: implement both sides using the POP interface
6
7 class POPException extends IOException {
8 }
9
10 interface POP {
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
23 }
24
25 public class POP3 extends MessageProtocol {
26
27     public static void main(String[] args) throws Exception {
28         ServerSocket ss = new ServerSocket(110);
29         while(true) {
30             System.out.println("listening");
31             final Socket s = ss.accept();
32             System.out.println("connected");
33             new Thread() {
34                 public void run() {
35                     try {
36                         service(s);
37                     } catch (Exception e) {
38                         e.printStackTrace();
39                     }
40                 }
41             }.start();
42         }
43     }
44
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");
49         pw.flush();
50         String user = null;
51         String pass = null;
52         while(true) {
53             String command = br.readLine().trim();
54             System.out.println("command: " + command);
55             if (command.toUpperCase().startsWith("QUIT ")) {
56                 s.close();
57                 return;
58             } else if (command.toUpperCase().startsWith("USER ")) {
59                 user = command.substring(5).trim();
60                 pw.print("+OK now give me your password\r\n");
61                 pw.flush();
62             } else if (command.toUpperCase().startsWith("PASS ")) {
63                 if (user == null) {
64                     pw.print("-ERR I need your password first\r\n");
65                     pw.flush();
66                 } else {
67                     pass = command.substring(5).trim();
68                     break;
69                 }
70             }
71         }
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);
76
77         BufferedReader br2 = new BufferedReader(new InputStreamReader(pop.getInputStream()));
78         PrintWriter pw2 = new PrintWriter(new OutputStreamWriter(pop.getOutputStream()));
79
80         System.out.println("pop said " + br2.readLine());
81         pw2.print("USER " + user + "\r\n");
82         pw2.flush();
83         System.out.println("pop said " + br2.readLine());
84         pw2.print("PASS " + pass + "\r\n");
85         pw2.flush();
86         System.out.println("pop said " + br2.readLine());
87
88         s.close();
89     }
90 }