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