add support for standalone operation
[org.ibex.mail.git] / src / org / ibex / mail / Main.java
index 7a58382..e4971ed 100644 (file)
@@ -1,22 +1,65 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
 package org.ibex.mail;
 import org.ibex.mail.target.*;
 import org.ibex.mail.protocol.*;
 import org.ibex.util.*;
 import org.ibex.jinetd.*;
+import org.ibex.jetty.*;
 import org.ibex.io.*;
 import java.io.*;
 import java.net.*;
 import java.util.*;
 import org.ibex.crypto.*;
+import org.ibex.jetty.*;
 
 public class Main implements Listener {
 
-    public void accept(Connection conn) {
+    public static void main(String[] s) throws Exception {
+        new Main().main();
+    }
+
+    public void main() throws Exception {
+        ServerSocket sock143 = new ServerSocket(143);
+        ServerSocket sock119 = new ServerSocket(119);
+        ServerSocket sock25  = new ServerSocket(25);
+        new Acceptor(sock143).start();
+        new Acceptor(sock119).start();
+        new Acceptor(sock25).start();
+    }
+
+    private class Acceptor extends Thread {
+        private ServerSocket ss;
+        public Acceptor(ServerSocket ss) { this.ss = ss; }
+        public void run() {
+            try {
+                while(true) {
+                    final Socket s = ss.accept();
+                    try {
+                        new Thread() {
+                            public void run() {
+                                try {
+                                    accept(new Connection(s, "megacz.com"));
+                                } catch (Exception e) { Log.error(Main.class, e); }
+                            }
+                        }.start();
+                    } catch (Exception e) { Log.error(Main.class, e); }
+                }
+            } catch (Exception e) { Log.error(Main.class, e); }
+        }
+    }
+
+    public boolean accept(Connection conn) {
         try {
             if      (conn.getLocalPort() == 143)  new IMAP.Listener(auth).handleRequest(conn);
-            else if (conn.getLocalPort() == 25)   new SMTP.Server().handleRequest(conn);
             else if (conn.getLocalPort() == 119)  new NNTP.Listener(auth).handleRequest(conn);
-            else if (conn.getLocalPort() == 8099) GMail.handleRequest(conn);
+            else if (conn.getLocalPort() == 25)   new SMTP.Server().handleRequest(conn);
+            //else if (conn.getLocalPort() == 110)  new POP3.Listener(auth).handleRequest(conn);
+            //else if (conn.getLocalPort() == 8099) GMail.handleRequest(conn);
+           else return false;
+           return true;
         } finally {
             conn.close();
         }
@@ -24,32 +67,26 @@ public class Main implements Listener {
 
     private static final Auth auth = new Auth();
     private static class Auth implements Login {
-        public Account anonymous() { return null; }
-        public Object login(String user, String pass, Class protocol) {
-            if (protocol == IMAP.class && user.endsWith("@gmail.com")) return GMail.getGMail(user, pass).getIMAP();
-            return login(user, pass);
+        public Account anonymous() {
+            try {
+                final Mailbox root =
+                    FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/list", false);
+                if (root==null) return null;
+                return new Account("anonymous", null, root){
+                    public Mailbox getMailbox(Class protocol) {
+                        return super.getMailbox(protocol);
+                    }
+                };
+            } catch (Exception e) { throw new RuntimeException(e); }
         }
+        public Object login(String user, String pass, Class protocol) { return login(user, pass); }
         public Account login(String user, String pass) {
-            if (user.indexOf("@gmail.com") != -1) return GMail.getGMail(user, pass);
             if (!EtcPasswd.verify(user, pass)) return null;
             final Mailbox root =
                 FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/user", true);
             return new Account(user, null, root.slash(user, true)){
                     public Mailbox getMailbox(Class protocol) {
-                        if (protocol == NNTP.class) {
-                            final Mailbox arch = new MailmanArchives();
-                            return new Mailbox.Default() {
-                                    public void add(Message m) { throw new RuntimeException("not supported"); }
-                                    public void add(Message m, int i) { throw new RuntimeException("not supported"); }
-                                    public int              uidValidity()  { return 1; }
-                                    public Mailbox.Iterator iterator()     { return null; }
-                                    public int              uidNext()      { return 0; }
-                                    public String[] children() { return new String[] { "us" }; }
-                                    public Mailbox slash(String name, boolean create) { return arch; }
-                                };
-                        } else {
-                            return super.getMailbox(protocol);
-                        }
+                        return super.getMailbox(protocol);
                     }
                 };
         }