bugfixes
[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
17         //Log.file(logto);
18         Log.color = true;
19
20         new IMAPThread().start();
21         new SMTPThread().start();
22
23     }
24
25     private static class BogusAuthenticator implements IMAP.Server.Authenticator {
26         final Mailbox root =
27             FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true).slash("user", true).slash("megacz", true);
28         public Mailbox authenticate(String u, String p) {
29             if (u.equals("megacz") && p.equals("pass")) return root;
30             return null;
31         }
32     }
33     
34     private static class IMAPThread extends Thread {
35         final int port = Integer.parseInt(System.getProperty("ibex.mail.imap.port", "143"));
36         public void run() {
37             try {
38                 Log.info(this, "binding to port " + port + "...");
39                 ServerSocket ss = new ServerSocket(port);
40                 Log.info(this, "listening for connections...");
41                 for(;;) {
42                     final Socket sock = ss.accept();
43                     new Thread() {
44                         public void run() {
45                             try {
46                                 new IMAP.Listener(sock, "megacz.com", new BogusAuthenticator()).handle();
47                             } catch (Exception e) {
48                                 Log.warn(this, e);
49                             }
50                         }
51                     }.start();
52                 }
53             } catch (Exception e) {
54                 Log.warn(this, e);
55             }
56         }
57     }
58
59     private static class SMTPThread extends Thread {
60         final int port = Integer.parseInt(System.getProperty("ibex.mail.smtp.port", "25"));
61         public void run() {
62             try {
63                 Log.info(this, "binding to port " + port + "...");
64                 ServerSocket ss = new ServerSocket(port);
65                 Log.info(this, "listening for connections...");
66                 while(true) {
67                     final Socket sock = ss.accept();
68                     final SMTP.Server smtp = new SMTP.Server(sock, "megacz.com");
69                     new Thread() {
70                         public void run() {
71                             try {
72                                 smtp.handle();
73                             } catch (Exception e) {
74                                 Log.warn(this, e);
75                             }
76                         }
77                     }.start();
78                 }
79             } catch (Exception e) {
80                 Log.warn(this, e);
81             }
82         }
83     }
84 }