import java.io.*;
// Next step: implement both sides using the POP interface
+public interface POP3 {
-class POPException extends IOException {
-}
-
-interface POP {
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 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 class POP3 extends MessageProtocol {
+ 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();
+ 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();
+ }.start();
+ }
}
- }
- 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();
- 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");
+ 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();
+ 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 {
- pass = command.substring(5).trim();
- break;
+ } 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;
+ }
}
}
- }
- 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);
+ 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()));
+ 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());
+ 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();
+ s.close();
+ }
}
}