almost there
[org.ibex.mail.git] / src / org / ibex / mail / Main.java
1 package org.ibex.mail;
2 import org.ibex.mail.target.*;
3 import org.ibex.mail.protocol.*;
4 import org.ibex.util.*;
5 import java.io.*;
6 import java.net.*;
7 import java.util.*;
8
9 public class Main {
10
11     public static void main(String[] s) throws Exception {
12
13         // set up logging
14         String logto = System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
15         logto += File.separatorChar + "log";
16         Log.file(logto);
17
18         new IMAPThread().start();
19         new SMTPThread().start();
20
21     }
22
23     private static class BogusAuthenticator implements IMAP.Server.Authenticator {
24         final Mailbox root = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true);
25         public Mailbox authenticate(String u, String p) {
26             if (u.equals("megacz") && p.equals("pass")) return root;
27             return null;
28         }
29     }
30     
31     private static class IMAPThread extends Thread {
32         final int port = Integer.parseInt(System.getProperty("ibex.mail.imap.port", "143"));
33         public void run() {
34             try {
35                 Log.info(this, "binding to port " + port + "...");
36                 ServerSocket ss = new ServerSocket(port);
37                 Log.info(this, "listening for connections...");
38                 for(;;) {
39                     final Socket sock = ss.accept();
40                     new Thread() {
41                         public void run() {
42                             try {
43                                 new IMAP.Listener(sock, "megacz.com", new BogusAuthenticator()).handle();
44                             } catch (Exception e) {
45                                 Log.warn(this, e);
46                             }
47                         }
48                     }.start();
49                 }
50             } catch (Exception e) {
51                 Log.warn(this, e);
52             }
53         }
54     }
55
56     private static class SMTPThread extends Thread {
57         final int port = Integer.parseInt(System.getProperty("ibex.mail.smtp.port", "25"));
58         public void run() {
59             try {
60                 Log.info(this, "binding to port " + port + "...");
61                 ServerSocket ss = new ServerSocket(port);
62                 Log.info(this, "listening for connections...");
63                 while(true) {
64                     final Socket sock = ss.accept();
65                     final SMTP.Server smtp = new SMTP.Server(sock, "megacz.com");
66                     new Thread() {
67                         public void run() {
68                             try {
69                                 smtp.handle();
70                             } catch (Exception e) {
71                                 Log.warn(this, e);
72                             }
73                         }
74                     }.start();
75                 }
76             } catch (Exception e) {
77                 Log.warn(this, e);
78             }
79         }
80     }
81 }