cfad5c1a152e0c72e04f29f4d99338409fc9d102
[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 import org.ibex.jetty.*;
17
18 public class Main implements Listener {
19
20     public boolean accept(Connection conn) {
21         try {
22             if      (conn.getLocalPort() == 143)  new IMAP.Listener(auth).handleRequest(conn);
23             else if (conn.getLocalPort() == 25)   new SMTP.Server().handleRequest(conn);
24             else if (conn.getLocalPort() == 8080)   new SMTP.Server().handleRequest(conn);
25             else if (conn.getLocalPort() == 119)  new NNTP.Listener(auth).handleRequest(conn);
26             //else if (conn.getLocalPort() == 110)  new POP3.Listener(auth).handleRequest(conn);
27             else if (conn.getLocalPort() == 8099) GMail.handleRequest(conn);
28             //            else if (conn.getLocalPort() == 8080) Jetty.instance().accept(conn);
29             //else if (conn.getLocalPort() == 443) Jetty.instance().accept(conn);
30             //else if (conn.getLocalPort() == 80)   Jetty.instance().accept(conn);
31             else return false;
32             return true;
33         } finally {
34             conn.close();
35         }
36     }
37
38     private static final Auth auth = new Auth();
39     private static class Auth implements Login {
40         public Account anonymous() { return null; }
41         public Object login(String user, String pass, Class protocol) {
42             //if (protocol == IMAP.class && user.endsWith("@gmail.com")) return GMail.getGMail(user, pass).getIMAP();
43             return login(user, pass);
44         }
45         public Account login(String user, String pass) {
46             //if (user.indexOf("@gmail.com") != -1) return GMail.getGMail(user, pass);
47             if (!EtcPasswd.verify(user, pass)) return null;
48             final Mailbox root =
49                 FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/user", true);
50             return new Account(user, null, root.slash(user, true)){
51                     public Mailbox getMailbox(Class protocol) {
52                         //if (protocol == IMAP.class) return super.getMailbox(protocol).slash("newmail", false).slash("pending", false);
53                         /*
54                         if (protocol == NNTP.class) {
55                             final Mailbox arch = new MailmanArchives();
56                             return new Mailbox.Default() {
57                                     public void add(Message m) { throw new RuntimeException("not supported"); }
58                                     public void add(Message m, int i) { throw new RuntimeException("not supported"); }
59                                     public int              uidValidity()  { return 1; }
60                                     public Mailbox.Iterator iterator()     { return null; }
61                                     public int              uidNext()      { return 0; }
62                                     public String[] children() { return new String[] { "us" }; }
63                                     public Mailbox slash(String name, boolean create) { return arch; }
64                                 };
65                         } else {
66                         */
67                             return super.getMailbox(protocol);
68                             //}
69                     }
70                 };
71         }
72     }
73
74 }