Mailbox -> MailboxTree separation
authoradam <adam@megacz.com>
Sun, 8 Jul 2007 22:50:48 +0000 (22:50 +0000)
committeradam <adam@megacz.com>
Sun, 8 Jul 2007 22:50:48 +0000 (22:50 +0000)
darcs-hash:20070708225048-5007d-e0d031ff9d7602608d86d8212a77be3d705012e9.gz

src/org/ibex/mail/Account.java
src/org/ibex/mail/IMAP.java
src/org/ibex/mail/Mailbox.java
src/org/ibex/mail/MailboxTree.java [new file with mode: 0644]
src/org/ibex/mail/Main.java
src/org/ibex/mail/NNTP.java
src/org/ibex/mail/SMTP.java

index 14b5940..76fe06e 100644 (file)
@@ -9,11 +9,11 @@ public class Account {
 
     public final String user;
     public final Address address;
-    protected Mailbox root = null;
+    protected MailboxTree root = null;
 
-    public Mailbox getMailbox(Class protocol) { return this.root; }
+    public MailboxTree getMailbox(Class protocol) { return this.root; }
 
     protected Account(String user, Address address) { this.user = user; this.address = address; }
-    public Account(String user, Address address, Mailbox root) { this.user = user; this.address = address; this.root = root; }
+    public Account(String user, Address address, MailboxTree root) { this.user = user; this.address = address; this.root = root; }
 
 }
index 5f0d11e..4f60d0d 100644 (file)
@@ -102,7 +102,7 @@ public class IMAP {
 
         Mailbox inbox = null;
         Mailbox selected = null;
-        Mailbox root = null;
+        MailboxTree root = null;
         Mailbox selected() { if (selected == null) throw new Bad("no mailbox selected"); return selected; }
         final Login auth;
         final Client client;
@@ -113,13 +113,13 @@ public class IMAP {
         private Mailbox mailbox(String name, boolean create) { return mailbox(name, create, true); }
         private Mailbox mailbox(String name, boolean create, boolean throwexn) {
             if (name.equalsIgnoreCase("inbox")) return inbox;
-            Mailbox m = root;
+            MailboxTree m = root;
             for(StringTokenizer st = new StringTokenizer(name, sep + ""); st.hasMoreTokens();)
                 if ((m = m.slash(st.nextToken(), create)) == null) {
                     if (throwexn) throw new Server.No("no such mailbox " + name);
                     return null;
                 }
-            return m;
+            return m.getMailbox();
         }
 
         // FEATURE: not accurate when a wildcard and subsequent non-wildcards both match a single component
@@ -251,7 +251,8 @@ public class IMAP {
     /** takes an IMAP.Server and exposes it to the world as an IMAP server on a TCP socket */
     public static class Listener implements Client {
         String selectedName = null;
-        Mailbox inbox = null, root = null;
+        Mailbox inbox = null;
+        MailboxTree root = null;
         Server api;
         Parser parser = null;
         Connection conn = null;
@@ -261,7 +262,7 @@ public class IMAP {
         Parser.Token token(boolean freak) { return parser.token(freak); }
         void println(String s) {
             conn.println(s);
-            Log.info("", s);
+            //Log.info("", s);
         }
         void newline() { parser.newline(); }
         Query query(int max) { return parser.query(max, maxn(true)); }
@@ -275,9 +276,8 @@ public class IMAP {
             } else {
                 Account account = (Account)ret;
                 ((MailboxWrapper)api).root = root = account.getMailbox(IMAP.class);
-                Log.warn(this, "logged in, root="+root);
-                ((MailboxWrapper)api).inbox = inbox = root.slash("INBOX", false);
-                if (inbox == null) ((MailboxWrapper)api).inbox = inbox = root;
+                ((MailboxWrapper)api).inbox = inbox = root.slash("INBOX", false).getMailbox();
+                if (inbox == null) ((MailboxWrapper)api).inbox = inbox = root.getMailbox();
             }
         }
 
@@ -389,8 +389,6 @@ public class IMAP {
                 }
             } catch (Server.Bad b) { println(tag==null ? "* BAD Invalid tag":(tag + " Bad " + b.toString())); Log.warn(this,b);
             } catch (Server.No n)  { println(tag==null?"* BAD Invalid tag":(tag+" No "  + n.toString())); Log.warn(this,n);
-            } finally {
-                //Log.warn(this, conn.dumpLog()+"\n");
             }
         }
 
index 1baf9f0..a0dc665 100644 (file)
@@ -13,7 +13,7 @@ import java.util.*;
 import java.text.*;
 
 /** abstract superclass for mailboxes, which store messages along with their flags */
-public abstract class Mailbox extends JS.Obj implements Target {
+public abstract class Mailbox extends MailboxTree implements Target {
 
     public JS get(JS key) throws JSExn {
         return slash(JSU.toString(key), true);
@@ -37,8 +37,7 @@ public abstract class Mailbox extends JS.Obj implements Target {
     public abstract int              uidNext();
     public abstract void             rename(String newName);      /* FIXME: IMAP semantics require creating parent dirs */
     public abstract void             destroy(boolean recursive);
-    public abstract Mailbox          slash(String name, boolean create);
-    public abstract String[]         children();
+    public          Mailbox          getMailbox() { return this; }
 
     // Thunks ////////////////////////////////////////////////////////////////////////////
 
@@ -59,7 +58,7 @@ public abstract class Mailbox extends JS.Obj implements Target {
         public int count(Query q) { int count = 0; for(Mailbox.Iterator it = iterator(q); it.next();) count++; return count; }
         public void rename(String newName) { throw new MailException("not supported"); }
         public void  destroy(boolean recursive) { throw new MailException("not supported"); }
-        public Mailbox slash(String name, boolean create) { return null; }
+        public MailboxTree slash(String name, boolean create) { return null; }
         public String[] children() { return new String[] { }; }
         public void post(Message message) { insert(message, Flag.RECENT); }
         public void move(Query q, Mailbox dest) {
@@ -166,7 +165,7 @@ public abstract class Mailbox extends JS.Obj implements Target {
         public int              uidNext() { return m.uidNext(); }
         public void             rename(String newName) { m.rename(newName); }
         public void             destroy(boolean recursive) { m.destroy(recursive); }
-        public Mailbox          slash(String name, boolean create) { return m.slash(name, create); }
+        public MailboxTree      slash(String name, boolean create) { return m.slash(name, create); }
         public String[]         children() { return m.children(); }
         public Mailbox.Iterator iterator() { return m.iterator(); }
         public int              uidValidity()  { return m.uidValidity(); }
diff --git a/src/org/ibex/mail/MailboxTree.java b/src/org/ibex/mail/MailboxTree.java
new file mode 100644 (file)
index 0000000..9e0bafe
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
+package org.ibex.mail;
+import org.ibex.mail.*;
+import org.ibex.util.*;
+import org.ibex.mail.*;
+import org.ibex.js.*;
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import java.text.*;
+
+/** a node on the "mailbox tree" */
+public abstract class MailboxTree extends JS.Obj {
+    public abstract MailboxTree  slash(String name, boolean create);
+    public abstract String[]     children();
+    public          Mailbox      getMailbox() { return null; }
+}
index b11584c..db9a36b 100644 (file)
@@ -19,6 +19,9 @@ import javax.net.ssl.SSLSocket;
 // restrict the maximum number of open connections from any given source IP (probably max 4)
 public class Main {
 
+    public static final ThreadPool threadPool = new ThreadPool(10);
+    public static final Cron       cron       = new Cron(threadPool);
+
     public static void main(String[] s) throws Exception {
         try {
             File f = new File(Mailbox.STORAGE_ROOT + "/restart");
@@ -44,20 +47,19 @@ public class Main {
         ServerSocket sock563  = (ServerSocket) sslserversocketfactory.createServerSocket(563);
         //ServerSocket sock995  = new ServerSocket(995);
         //ServerSocket sock113  = new ServerSocket(113);
-        new Thread() {
+
+        cron.executeLater(500, new Runnable() {
             public void run() {
-                while(true) {
-                    try { Thread.sleep(500); } catch (Exception e) { }
-                    try {
-                        File f = new File(Mailbox.STORAGE_ROOT + "/restart");
-                        if (f.exists()) {
-                            Log.error("RESTART", "restarting...");
-                            System.exit(0);
-                        }
-                    } catch (Exception e) { Log.error(this, e); }
-                }
-            }
-        }.start();
+                try {
+                    File f = new File(Mailbox.STORAGE_ROOT + "/restart");
+                    if (f.exists()) {
+                        Log.error("RESTART", "restarting...");
+                        System.exit(0);
+                    }
+                } catch (Exception e) { Log.error(this, e); }
+                cron.executeLater(500, this);
+            } });
+            
         new Acceptor(sock143).start();
         new Acceptor(sock119).start();
         new Acceptor(sock25).start();
@@ -77,13 +79,13 @@ public class Main {
                 try {
                     ss.setReuseAddress(true);
                     final Socket s = ss.accept();
-                    new Thread() {
+                    threadPool.start(new Runnable() {
                         public void run() {
                             try {
                                 accept(new Connection(s, "megacz.com"));
                             } catch (Throwable t) { Log.error(Main.class, t); }
                         }
-                    }.start();
+                    });
                 } catch (Throwable t) {
                     Log.error(Main.class, t);
                     Misc.sleep(1000);
@@ -123,7 +125,7 @@ public class Main {
                     FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/list", false);
                 if (root==null) return null;
                 return new Account("anonymous", null, root){
-                    public Mailbox getMailbox(Class protocol) {
+                    public MailboxTree getMailbox(Class protocol) {
                         return super.getMailbox(protocol);
                     }
                 };
@@ -134,10 +136,10 @@ public class Main {
             //if (!EtcPasswd.verify(user, pass)) return null;
             //if (!CheckPassword.verify(user, pass)) return null;
             if (!ka.auth(user, pass)) return null;
-            final Mailbox root =
+            final MailboxTree root =
                 FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/user", true);
             return new Account(user, null, root.slash(user, true)){
-                    public Mailbox getMailbox(Class protocol) {
+                    public MailboxTree getMailbox(Class protocol) {
                         return super.getMailbox(protocol);
                     }
                 };
index c91c902..d9f6d20 100644 (file)
@@ -59,12 +59,12 @@ public class NNTP {
     }
 
     public static class MailboxWrapper implements Server {
-        private final Mailbox root;
+        private final MailboxTree root;
         private Mailbox current;
         private int ptr = 0;
         private boolean post;
-        public MailboxWrapper(Mailbox root) { this(root, false); }
-        public MailboxWrapper(Mailbox root, boolean post) { this.root = root; this.post = post; }
+        public MailboxWrapper(MailboxTree root) { this(root, false); }
+        public MailboxWrapper(MailboxTree root, boolean post) { this.root = root; this.post = post; }
         public boolean  postok() { return post; }
         public void     post(Message m) throws IOException { current.post(m); }
         public Group    group(String s) {
@@ -91,13 +91,13 @@ public class NNTP {
             } catch (Exception e) { return null; }
         }
         public Group[]  list() { return list(root, ""); }
-        private Group[] list(Mailbox who, String prefix) {
+        private Group[] list(MailboxTree who, String prefix) {
             Vec v = new Vec();
             if (who == null) who = root;
             String[] s = who.children();
             for(int i=0; i<s.length; i++) {
                 v.addElement(new Group(prefix + s[i], true, 0, 0, 0)); // FIXME numbers
-                Group[] g2 = list(who.slash(s[i], false), prefix + s[i] + ".");
+                Group[] g2 = list(who.slash(s[i], false).getMailbox(), prefix + s[i] + ".");
                 for(int j=0; j<g2.length; j++) v.addElement(g2[j]);
             }
             Group[] ret = new Group[v.size()];
@@ -106,19 +106,19 @@ public class NNTP {
         }
 
         private void setgroup(String s) {
-            Mailbox ncurrent = root;
+            MailboxTree ncurrent = root;
             for(StringTokenizer st = new StringTokenizer(s, ".");
                 ncurrent != null && st.hasMoreTokens();
                 ncurrent = ncurrent.slash(st.nextToken(), false));
-            if (ncurrent!=null) current=ncurrent;
+            if (ncurrent!=null) current=ncurrent.getMailbox();
         }
         private Group getgroup(String s) {
-            Mailbox box = root;
+            MailboxTree box = root;
             for(StringTokenizer st = new StringTokenizer(s, ".");
                 box!=null && st.hasMoreTokens();
                 box = box.slash(st.nextToken(), false));
             if (box==null) return null;
-            return new Group(s, true, 1, box.count(Query.all()), box.count(Query.all()));
+            return new Group(s, true, 1, box.getMailbox().count(Query.all()), box.getMailbox().count(Query.all()));
         }
 
         public Group[]  newgroups(Date d, String[] distributions) { /* FEATURE */ return new Group[] { }; }
@@ -183,8 +183,7 @@ public class NNTP {
                         pass = st.nextToken();
                         account = login.login(user, pass);
                         if (account == null) { println("502 Invalid"); continue; }
-                        Mailbox box = account.getMailbox(NNTP.class);
-                        this.api = new MailboxWrapper(box, true);
+                        this.api = new MailboxWrapper(account.getMailbox(NNTP.class), true);
                         println("281 Good to go");
                         continue;
                     }
index 4cfeb02..039b3a7 100644 (file)
@@ -56,7 +56,7 @@ public class SMTP {
         Integer.parseInt(System.getProperty("org.ibex.mail.smtp.maxMessageSize", "-1"));
 
     private static final Mailbox spool =
-        FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT,false).slash("spool",true).slash("smtp",true);
+        FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT,false).slash("spool",true).slash("smtp",true).getMailbox();
 
     static {
         for(int i=0; i<numOutgoingThreads; i++)