package org.ibex.mail.protocol;
-import java.net.*;
+import org.ibex.util.*;
+import org.ibex.io.*;
+import org.ibex.mail.*;
+import org.ibex.mail.target.*;
+import org.ibex.jinetd.*;
import java.io.*;
+import java.net.*;
+import java.util.*;
-// Next step: implement both sides using the POP interface
public interface POP3 {
- public abstract void userpass(String user, String pass) throws POPException;
- public abstract void apop(String user, String digest) throws POPException;
- public abstract BufferedReader top(int m, int maxlines);
- public abstract BufferedReader retr(int m);
- public abstract long stat(); // top 32 bits is number of messages, bottom 32 is total size
- public abstract long[] list(); // top 32 bits is message number, bottom 32 is size
- public abstract long list(int m);
- public abstract void dele(int m);
- public abstract void noop(int m);
- public abstract void rset(int m);
- public abstract String uidl(int m);
- public abstract String[] uidl(); // FIXME, also needs message number
- public static class POPException extends IOException { }
-
- public static class Server {
-
- public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(110);
- while(true) {
- System.out.println("listening");
- final Socket s = ss.accept();
- System.out.println("connected");
- new Thread() {
- public void run() {
- try {
- service(s);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }.start();
- }
- }
+ public static interface Server {
+ public void userpass(String user, String pass);
+ public void apop(String user, String digest);
+ public Stream top(int m, int maxlines);
+ public Stream retr(int m);
+ public long stat(); // top 32 bits is number of messages, bottom 32 is total size
+ public long[] list(); // top 32 bits is message number, bottom 32 is size
+ public long list(int m);
+ public void dele(int m);
+ public void noop(int m);
+ public void rset(int m);
+ public String uidl(int m);
+ public String[] uidl(); // FIXME, also needs message number
+ }
- public static void service(Socket s) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
- pw.print("+OK POP3 server ready\r\n");
- pw.flush();
+ public static class Listener implements Worker {
+ Server api = null;
+ public void handleRequest(Connection conn) {
+ conn.setTimeout(30 * 60 * 1000);
+ conn.println("+OK " + conn.vhost + " [" + POP3.class.getName() + "] ready");
String user = null;
String pass = null;
- while(true) {
- String command = br.readLine().trim();
- System.out.println("command: " + command);
- if (command.toUpperCase().startsWith("QUIT ")) {
- s.close();
- return;
- } else if (command.toUpperCase().startsWith("USER ")) {
- user = command.substring(5).trim();
- pw.print("+OK now give me your password\r\n");
- pw.flush();
- } else if (command.toUpperCase().startsWith("PASS ")) {
- if (user == null) {
- pw.print("-ERR I need your password first\r\n");
- pw.flush();
- } else {
- pass = command.substring(5).trim();
- break;
- }
+ for(String line = conn.readln(); line != null; line = conn.readln()) {
+ StringTokenizer st = new StringTokenizer(line, " ");
+ String command = st.nextToken().toUpperCase();
+ if (command.equals("USER")) {
+ } else if (command.equals("PASS")) {
+ } else if (command.equals("QUIT")) {
}
}
- System.out.println("login from " + user + " / " + pass);
- String server = user.substring(user.indexOf('@') + 1);
- user = user.substring(0, user.indexOf('@'));
- Socket pop = new Socket(InetAddress.getByName(server), 117);
-
- BufferedReader br2 = new BufferedReader(new InputStreamReader(pop.getInputStream()));
- PrintWriter pw2 = new PrintWriter(new OutputStreamWriter(pop.getOutputStream()));
-
- System.out.println("pop said " + br2.readLine());
- pw2.print("USER " + user + "\r\n");
- pw2.flush();
- System.out.println("pop said " + br2.readLine());
- pw2.print("PASS " + pass + "\r\n");
- pw2.flush();
- System.out.println("pop said " + br2.readLine());
-
- s.close();
}
}
}