merger
[org.ibex.mail.git] / src / org / ibex / mail / Main.java
index f607f5f..46e4e34 100644 (file)
+// 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 {
+public class Main implements Listener {
 
-    /*
-    public static class Resin {
-        new extends com.caucho.server.port.Protocol() {
-            public String getProtocolName() { return "imap"; }
-            public com.caucho.server.port.ServerRequest createRequest(com.caucho.server.connection.Connection conn) {
-                try {
-                    return new Listener(conn);
-                } catch (Exception e) {
-                    Log.error(this, e);
-                    return null;
-                }
-            }
+    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() == 8080) Jetty.instance().accept(conn);
+            else if (conn.getLocalPort() == 80)   Jetty.instance().accept(conn);
+           else return false;
+           return true;
+        } finally {
+            conn.close();
         }
-
-    public SMTP() { }
-    public String getProtocolName() { return "smtp"; }
-    public com.caucho.server.port.ServerRequest createRequest(com.caucho.server.connection.Connection conn) {
-       try {
-           return new Server(conn);
-       } catch (Exception e) {
-           Log.error(this, e);
-           return null;
-       }
-    }
-
     }
 
-    public static void main(String[] s) throws Exception {
-
-        // set up logging
-        String logto = System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
-        logto += File.separatorChar + "log";
-
-        //Log.file(logto);
-       Log.color = true;
-
-        new IMAPThread().start();
-        new SMTPThread().start();
-
-    }
-    */
-    public static class BogusAuthenticator implements IMAP.Server.Authenticator {
-        final Mailbox root =
-           FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true).slash("user", true).slash("megacz", true);
-        public Mailbox authenticate(String u, String p) {
-            if (u.equals("megacz") && p.equals("pass")) return root;
-            return null;
+    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);
         }
-    }
-    /*
-    private static class IMAPThread extends Thread {
-        final int port = Integer.parseInt(System.getProperty("ibex.mail.imap.port", "143"));
-        public void run() {
-            try {
-                Log.info(this, "binding to port " + port + "...");
-                ServerSocket ss = new ServerSocket(port);
-                Log.info(this, "listening for connections...");
-                for(;;) {
-                    final Socket sock = ss.accept();
-                    new Thread() {
-                        public void run() {
-                            try {
-                                new IMAP.Listener(sock, "megacz.com", new BogusAuthenticator()).handle();
-                            } catch (Exception e) {
-                                Log.warn(this, e);
-                            }
-                        }
-                    }.start();
-                }
-            } catch (Exception e) {
-                Log.warn(this, e);
-            }
+        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);
+                            //}
+                    }
+                };
         }
     }
 
-    private static class SMTPThread extends Thread {
-        final int port = Integer.parseInt(System.getProperty("ibex.mail.smtp.port", "25"));
-        public void run() {
-            try {
-                Log.info(this, "binding to port " + port + "...");
-                ServerSocket ss = new ServerSocket(port);
-                Log.info(this, "listening for connections...");
-                while(true) {
-                    final Socket sock = ss.accept();
-                    final SMTP.Server smtp = new SMTP.Server(sock, "megacz.com");
-                    new Thread() {
-                        public void run() {
-                            try {
-                                smtp.handle();
-                            } catch (Exception e) {
-                                Log.warn(this, e);
-                            }
-                        }
-                    }.start();
-                }
-            } catch (Exception e) {
-                Log.warn(this, e);
-            }
-        }
-    }
-    */
 }