reshuffling of file locations to make package structure flatter
[org.ibex.mail.git] / src / org / ibex / mail / Mailbox.java
diff --git a/src/org/ibex/mail/Mailbox.java b/src/org/ibex/mail/Mailbox.java
new file mode 100644 (file)
index 0000000..6905692
--- /dev/null
@@ -0,0 +1,174 @@
+// 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.*;
+
+/** abstract superclass for mailboxes, which store messages along with their flags */
+public abstract class Mailbox extends JS.Obj implements Target {
+
+    public JS get(JS key) throws JSExn {
+        return slash(JSU.toString(key), true);
+    }
+
+    public static final String STORAGE_ROOT =
+        System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
+
+
+    // Required Methods //////////////////////////////////////////////////////////////////////////////
+
+    /** phantom mailboxes merely contain others; like the alt.binaries in alt.binaries.warez */
+    public abstract boolean          phantom();
+    public abstract Mailbox.Iterator iterator(Query q);
+
+    public abstract void             insert(Message message, int flags);
+    public abstract void             post(Message message);
+    public abstract void             move(Query q, Mailbox dest);
+    public abstract void             copy(Query q, Mailbox dest);
+    public abstract int              count(Query q);
+    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();
+
+    // Thunks ////////////////////////////////////////////////////////////////////////////
+
+    public          void             accept(Message m) { insert(m, Flag.RECENT); }
+    public          Mailbox.Iterator iterator() { return iterator(Query.all()); }
+
+
+    // Default Implementation //////////////////////////////////////////////////////////////////////////////
+
+    private int randomUidValidity = new Random().nextInt();
+    public  int uidValidity()  { return randomUidValidity; }
+
+    /** default, inefficient implementation of Mailbox; only requires a few methods to be implemented */
+    public static abstract class Default extends Mailbox {
+        public Mailbox.Iterator iterator(Query q) { return new Mailbox.Iterator.QueryIterator(q, this); }
+        public boolean phantom() { return false; }
+        public void copy(Query q, Mailbox dest) { for(Mailbox.Iterator it = iterator(q); it.next();) dest.insert(it.cur(), it.getFlags()); }
+        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 String[] children() { return new String[] { }; }
+        public void post(Message message) { insert(message, Flag.RECENT); }
+        public void move(Query q, Mailbox dest) {
+            for(Mailbox.Iterator it = iterator(q);it.next();) { dest.insert(it.cur(), it.getFlags()); it.delete(); }
+        }
+        public static abstract class Iterator implements Mailbox.Iterator {
+            public int     nntpNumber() { throw new MailException("not supported"); }
+            public int     getFlags() { return 0; }
+            public void    setFlags(int flags) { throw new MailException("not supported"); }
+        }
+    }
+
+
+    // Iterator Definition //////////////////////////////////////////////////////////////////////////////
+
+    public static interface Iterator {
+        public abstract Message cur();
+        public abstract Headers head();
+        public abstract boolean next();
+        public abstract void    delete();
+
+        /** a unique identifier for this message */
+        public abstract int     uid();
+
+        /**
+         *  Message number according to IMAP semantics.
+         *    - no two messages in the same mailbox may have the same imapNumber
+         *    - sorting by uid must yield the same order as sorting them by imapNumber
+         *    - imapNumber may only change if uidValidity changes
+         *    - if uidValidity changes, imapNumbers may change or be reused
+         */
+        public abstract int     imapNumber();
+
+        /**
+         *  Message number according to NNTP semantics.
+         *    - no two messages in the same mailbox may have the same nntpNumber
+         *    - article number may NEVER change or EVER be reused
+         *    - uidValidity is irrelevant
+         */
+        public abstract int     nntpNumber();
+
+        public abstract int     getFlags();
+        public abstract void    setFlags(int flags);
+
+        public static class Wrapper implements Iterator {
+            private Iterator it;
+            public Wrapper(Iterator it) { this.it = it; }
+            public Message cur() { return it.cur(); }
+            public Headers head() { return it.head(); }
+            public boolean next() { return it.next(); }
+            public int     uid() { return it.uid(); }
+            public int     nntpNumber() { return it.nntpNumber(); }
+            public int     imapNumber() { return it.imapNumber(); }
+            public void    delete() { it.delete(); }
+            public int     getFlags() { return it.getFlags(); }
+            public void    setFlags(int flags) { it.setFlags(flags); }
+        }
+
+        class QueryIterator extends Mailbox.Iterator.Wrapper {
+            Query q;
+            public QueryIterator(Query q, Mailbox m) { super(m.iterator()); this.q = q; }
+            public boolean next() {
+               if (q == null) return false;
+               do { if (!super.next()) return false; } while(!q.match(this)); return true; }
+        }
+
+        public static class NullIterator extends Mailbox.Default.Iterator {
+            public NullIterator() { }
+            public Message cur() { return null; }
+            public Headers head() { return null; }
+            public boolean next() { return false; }
+            public int     uid() { return 0; }
+            public void    delete() { }
+            public int     imapNumber() { return 0; }
+            public int     nntpNumber() { throw new RuntimeException("this mailbox does not keep article numbers"); }
+        }
+    }
+
+    /** constants for the six IMAP flags */
+    public static class Flag {
+        public static final int DELETED  = 0x0001;
+        public static final int SEEN     = 0x0002;
+        public static final int FLAGGED  = 0x0004;
+        public static final int DRAFT    = 0x0008;
+        public static final int ANSWERED = 0x0010;
+        public static final int RECENT   = 0x0020;
+        public static final int[] all = new int[] { DELETED, SEEN, FLAGGED, DRAFT, ANSWERED, RECENT };
+        public static final int defaultFlags = RECENT;
+    }
+
+    public static class MailboxWrapper extends Mailbox {
+        
+        private Mailbox m;
+        public MailboxWrapper(Mailbox m) { this.m = m; }
+
+        public boolean          phantom() { return m.phantom(); }
+        public Mailbox.Iterator iterator(Query q) { return m.iterator(q); }
+        public void             insert(Message message, int flags) { m.insert(message, flags); }
+        public void             post(Message message) { m.insert(message, Flag.RECENT); }
+        public void             move(Query q, Mailbox dest) { m.move(q, dest); }
+        public void             copy(Query q, Mailbox dest) { m.copy(q, dest); }
+        public int              count(Query q) { return m.count(q); }
+        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 String[]         children() { return m.children(); }
+        public Mailbox.Iterator iterator() { return m.iterator(); }
+        public int              uidValidity()  { return m.uidValidity(); }
+    }
+
+}