minor changes for Jetty
[org.ibex.mail.git] / src / org / ibex / mail / Main.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.mail;
6 import org.ibex.mail.target.*;
7 import org.ibex.mail.protocol.*;
8 import org.ibex.util.*;
9 import org.ibex.jinetd.*;
10 import org.ibex.jetty.*;
11 import org.ibex.io.*;
12 import java.io.*;
13 import java.net.*;
14 import java.util.*;
15 import org.ibex.crypto.*;
16
17 public class Main implements Listener {
18
19     public void accept(Connection conn) {
20         try {
21             if      (conn.getLocalPort() == 143)  new IMAP.Listener(auth).handleRequest(conn);
22             else if (conn.getLocalPort() == 25)   new SMTP.Server().handleRequest(conn);
23             else if (conn.getLocalPort() == 119)  new NNTP.Listener(auth).handleRequest(conn);
24             else if (conn.getLocalPort() == 8099) GMail.handleRequest(conn);
25             else if (conn.getLocalPort() == 8080) Jetty.instance().accept(conn);
26             else if (conn.getLocalPort() == 80)   Jetty.instance().accept(conn);
27         } finally {
28             conn.close();
29         }
30     }
31
32     private static final Auth auth = new Auth();
33     private static class Auth implements Login {
34         public Account anonymous() { return null; }
35         public Object login(String user, String pass, Class protocol) {
36             //if (protocol == IMAP.class && user.endsWith("@gmail.com")) return GMail.getGMail(user, pass).getIMAP();
37             return login(user, pass);
38         }
39         public Account login(String user, String pass) {
40             //if (user.indexOf("@gmail.com") != -1) return GMail.getGMail(user, pass);
41             if (!EtcPasswd.verify(user, pass)) return null;
42             final Mailbox root =
43                 FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/user", true);
44             return new Account(user, null, root.slash(user, true)){
45                     public Mailbox getMailbox(Class protocol) {
46                         /*
47                         if (protocol == NNTP.class) {
48                             final Mailbox arch = new MailmanArchives();
49                             return new Mailbox.Default() {
50                                     public void add(Message m) { throw new RuntimeException("not supported"); }
51                                     public void add(Message m, int i) { throw new RuntimeException("not supported"); }
52                                     public int              uidValidity()  { return 1; }
53                                     public Mailbox.Iterator iterator()     { return null; }
54                                     public int              uidNext()      { return 0; }
55                                     public String[] children() { return new String[] { "us" }; }
56                                     public Mailbox slash(String name, boolean create) { return arch; }
57                                 };
58                         } else {
59                         */
60                             return super.getMailbox(protocol);
61                             //}
62                     }
63                 };
64         }
65     }
66
67 }