a8988a483d757eb207cc0858468a7179287d8e66
[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 public interface POP3 {
7
8     public abstract void userpass(String user, String pass) throws POPException;
9     public abstract void apop(String user, String digest) throws POPException;
10     public abstract BufferedReader top(int m, int maxlines);
11     public abstract BufferedReader retr(int m);
12     public abstract long stat();        // top 32 bits is number of messages, bottom 32 is total size
13     public abstract long[] list();      // top 32 bits is message number, bottom 32 is size
14     public abstract long list(int m);
15     public abstract void dele(int m);
16     public abstract void noop(int m);
17     public abstract void rset(int m);
18     public abstract String uidl(int m);
19     public abstract String[] uidl();    // FIXME, also needs message number
20     public static class POPException extends IOException { }
21
22     public static class Server {
23
24         public static void main(String[] args) throws Exception {
25             ServerSocket ss = new ServerSocket(110);
26             while(true) {
27                 System.out.println("listening");
28                 final Socket s = ss.accept();
29                 System.out.println("connected");
30                 new Thread() {
31                     public void run() {
32                         try {
33                             service(s);
34                         } catch (Exception e) {
35                             e.printStackTrace();
36                         }
37                     }
38                 }.start();
39             }
40         }
41
42         public static void service(Socket s) throws IOException {
43             BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
44             PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
45             pw.print("+OK POP3 server ready\r\n");
46             pw.flush();
47             String user = null;
48             String pass = null;
49             while(true) {
50                 String command = br.readLine().trim();
51                 System.out.println("command: " + command);
52                 if (command.toUpperCase().startsWith("QUIT ")) {
53                     s.close();
54                     return;
55                 } else if (command.toUpperCase().startsWith("USER ")) {
56                     user = command.substring(5).trim();
57                     pw.print("+OK now give me your password\r\n");
58                     pw.flush();
59                 } else if (command.toUpperCase().startsWith("PASS ")) {
60                     if (user == null) {
61                         pw.print("-ERR I need your password first\r\n");
62                         pw.flush();
63                     } else {
64                         pass = command.substring(5).trim();
65                         break;
66                     }
67                 }
68             }
69             System.out.println("login from " + user + " / " + pass);
70             String server = user.substring(user.indexOf('@') + 1);
71             user = user.substring(0, user.indexOf('@'));
72             Socket pop = new Socket(InetAddress.getByName(server), 117);
73
74             BufferedReader br2 = new BufferedReader(new InputStreamReader(pop.getInputStream()));
75             PrintWriter pw2 = new PrintWriter(new OutputStreamWriter(pop.getOutputStream()));
76
77             System.out.println("pop said " + br2.readLine());
78             pw2.print("USER " + user + "\r\n");
79             pw2.flush();
80             System.out.println("pop said " + br2.readLine());
81             pw2.print("PASS " + pass + "\r\n");
82             pw2.flush();
83             System.out.println("pop said " + br2.readLine());
84
85             s.close();
86         }
87     }
88 }