preliminary authentication support
[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 org.ibex.jinetd.*;
6 import org.ibex.io.*;
7 import java.io.*;
8 import java.net.*;
9 import java.util.*;
10 import org.ibex.crypto.*;
11
12 public class Main implements Listener {
13
14     public void accept(Connection conn) {
15         try {
16             if (conn.getLocalPort() == 25)       new SMTP.Server().handleRequest(conn);
17             else if (conn.getLocalPort() == 143) new IMAP.Listener(auth).handleRequest(conn);
18             else if (conn.getLocalPort() == 119) new NNTP.Listener(auth).handleRequest(conn);
19         } finally {
20             conn.close();
21         }
22     }
23
24     private static final Auth auth = new Auth();
25     private static class Auth implements Login {
26         final Mailbox root = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true).slash("user", true);
27         public Account anonymous() { return null; }
28         public Account login(String user, String pass) {
29             if (!EtcPasswd.verify(user, pass)) return null;
30             return new Account(user, null, root.slash(user, true)){
31                     public Mailbox getMailbox(Class protocol) {
32                         if (protocol == NNTP.class) {
33                             final Mailbox arch = new MailmanArchives();
34                             return new Mailbox.Default() {
35                                     public void add(Message m) { throw new RuntimeException("not supported"); }
36                                     public void add(Message m, int i) { throw new RuntimeException("not supported"); }
37                                     public int              uidValidity()  { return 1; }
38                                     public Mailbox.Iterator iterator()     { return null; }
39                                     public int              uidNext()      { return 0; }
40                                     public String[] children() { return new String[] { "us" }; }
41                                     public Mailbox slash(String name, boolean create) { return arch; }
42                                 };
43                         } else {
44                             return super.getMailbox(protocol);
45                         }
46                     }
47                 };
48         }
49     }
50
51 }