e4971ed8872734bba4f1fba521580e56cec79617
[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 static void main(String[] s) throws Exception {
21         new Main().main();
22     }
23
24     public void main() throws Exception {
25         ServerSocket sock143 = new ServerSocket(143);
26         ServerSocket sock119 = new ServerSocket(119);
27         ServerSocket sock25  = new ServerSocket(25);
28         new Acceptor(sock143).start();
29         new Acceptor(sock119).start();
30         new Acceptor(sock25).start();
31     }
32
33     private class Acceptor extends Thread {
34         private ServerSocket ss;
35         public Acceptor(ServerSocket ss) { this.ss = ss; }
36         public void run() {
37             try {
38                 while(true) {
39                     final Socket s = ss.accept();
40                     try {
41                         new Thread() {
42                             public void run() {
43                                 try {
44                                     accept(new Connection(s, "megacz.com"));
45                                 } catch (Exception e) { Log.error(Main.class, e); }
46                             }
47                         }.start();
48                     } catch (Exception e) { Log.error(Main.class, e); }
49                 }
50             } catch (Exception e) { Log.error(Main.class, e); }
51         }
52     }
53
54     public boolean accept(Connection conn) {
55         try {
56             if      (conn.getLocalPort() == 143)  new IMAP.Listener(auth).handleRequest(conn);
57             else if (conn.getLocalPort() == 119)  new NNTP.Listener(auth).handleRequest(conn);
58             else if (conn.getLocalPort() == 25)   new SMTP.Server().handleRequest(conn);
59             //else if (conn.getLocalPort() == 110)  new POP3.Listener(auth).handleRequest(conn);
60             //else if (conn.getLocalPort() == 8099) GMail.handleRequest(conn);
61             else return false;
62             return true;
63         } finally {
64             conn.close();
65         }
66     }
67
68     private static final Auth auth = new Auth();
69     private static class Auth implements Login {
70         public Account anonymous() {
71             try {
72                 final Mailbox root =
73                     FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/list", false);
74                 if (root==null) return null;
75                 return new Account("anonymous", null, root){
76                     public Mailbox getMailbox(Class protocol) {
77                         return super.getMailbox(protocol);
78                     }
79                 };
80             } catch (Exception e) { throw new RuntimeException(e); }
81         }
82         public Object login(String user, String pass, Class protocol) { return login(user, pass); }
83         public Account login(String user, String pass) {
84             if (!EtcPasswd.verify(user, pass)) return null;
85             final Mailbox root =
86                 FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/user", true);
87             return new Account(user, null, root.slash(user, true)){
88                     public Mailbox getMailbox(Class protocol) {
89                         return super.getMailbox(protocol);
90                     }
91                 };
92         }
93     }
94
95 }