almost there
[org.ibex.mail.git] / src / org / ibex / mail / target / Mailbox.java
index 8869bca..8d4e286 100644 (file)
@@ -7,145 +7,156 @@ import java.net.*;
 import java.util.*;
 import java.text.*;
 
-// FIXME: appallingly inefficient
-public class Mailbox extends Target {
+/** abstract superclass for mailboxes, which store messages along with their flags */
+public abstract class Mailbox extends Target {
 
-    private static final String STORAGE_ROOT =
+    public static final String STORAGE_ROOT =
         System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
-    public static FileBased root = null;
-    public static Transcript transcript = null;
-    static {
-        try {
-            root = new FileBased(STORAGE_ROOT + File.separatorChar);
-            transcript = new Transcript(STORAGE_ROOT + File.separatorChar + "transcript");
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
 
-    public void accept(Message m) throws IOException { add(m); }
-    public Mailbox slash(String name) throws IOException {
-        throw new Error(this.getClass().getName() + " does not support the slash() method"); }
-    public int[] list() { throw new Error(this.getClass().getName() + " does not support the list() method"); }
-    public int add(Message message) throws IOException {
-        throw new Error(this.getClass().getName() + " does not support the add() method"); }
-    public int delete(Message message) throws IOException {
-        throw new Error(this.getClass().getName() + " does not support the delete() method"); }
-    public Message get(int messagenum) throws IOException {
-        throw new Error(this.getClass().getName() + " does not support the get() method"); }
-    public Message[] query(int maxResults) {
-        throw new Error(this.getClass().getName() + " does not support the query() method"); }
-
-    public static class Mailbox extends Mailbox {
-        String user;
-        private static Hashtable cache = new Hashtable();
-        public static Mailbox getForUser(String user) {
-            Mailbox ret = (Mailbox)cache.get(user);
-            if (ret == null) ret = new Mailbox(user);
-            return ret;
-        }
-        Mailbox(String user) { this.user = user; }
-        public Mailbox slash(String name) throws IOException {
-            throw new Error(this.getClass().getName() + " does not support the slash() method"); }
-        public synchronized int add(Message message) throws IOException {
-            FileOutputStream fos = new FileOutputStream("/var/mail/" + user, true);
-            PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
-            pw.println("From " + message.envelopeFrom);
-            pw.flush();
-            message.dump(fos);
-            fos.close();
-            return -1;
-        }
-    }
 
-    /** a fast-write, slow-read place to stash all messages we touch -- in case of a major f*ckup */
-    public static class Transcript extends Mailbox {
-        private String path;
-        public Transcript(String path) throws IOException { new File(this.path = path).mkdirs(); }
-        private static String lastTime = null;
-        private static int lastCounter = 0;
-
-        /** returns a message identifier */
-        public synchronized int add(Message message) throws IOException {
-            File today = new File(path + File.separatorChar + (new SimpleDateFormat("yy-MMM-dd").format(new Date())));
-            today.mkdirs();
-            
-            String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
-            synchronized (Transcript.class) {
-                if (lastTime != null && lastTime.equals(time)) {
-                    time += "." + (++lastCounter);
-                } else {
-                    lastTime = time;
-                }
+    // 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             add(Message message);
+    public abstract void             add(Message message, int flags);
+    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 int              uidValidity();
+    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 final    void             accept(Message m) { add(m); }
+    public          Mailbox.Iterator iterator() { return iterator(Query.all()); }
+
+
+    // Default Implementation //////////////////////////////////////////////////////////////////////////////
+
+    /** 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.add(it.cur()); }
+        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) { throw new MailException("not supported"); }
+        public String[] children() { return new String[] { }; }
+        public void move(Query q, Mailbox dest) {
+            for(Mailbox.Iterator it = iterator(q);it.next();) { dest.add(it.cur()); it.delete(); }
+        }
+        public static abstract class Iterator implements Mailbox.Iterator {
+            public int flags() {
+                return 
+                    (deleted() ? Flag.DELETED : 0) |
+                    (seen() ? Flag.SEEN : 0) |
+                    (answered() ? Flag.ANSWERED : 0) |
+                    (draft() ? Flag.DRAFT : 0) |
+                    (recent() ? Flag.RECENT : 0);
+            }
+            public void addFlags(int flags) {
+                if ((flags & Flag.DELETED) == Flag.DELETED) deleted(true);
+                if ((flags & Flag.SEEN) == Flag.SEEN) seen(true);
+                if ((flags & Flag.FLAGGED) == Flag.FLAGGED) flagged(true);
+                if ((flags & Flag.DRAFT) == Flag.DRAFT) draft(true);
+                if ((flags & Flag.RECENT) == Flag.RECENT) recent(true);
+            }
+            public void removeFlags(int flags) {
+                if ((flags & Flag.DELETED) == Flag.DELETED) deleted(false);
+                if ((flags & Flag.SEEN) == Flag.SEEN) seen(false);
+                if ((flags & Flag.FLAGGED) == Flag.FLAGGED) flagged(false);
+                if ((flags & Flag.DRAFT) == Flag.DRAFT) draft(false);
+                if ((flags & Flag.RECENT) == Flag.RECENT) recent(false);
+            }
+            public void setFlags(int flags) {
+                if ((flags & Flag.DELETED) == Flag.DELETED) deleted(true); else deleted(false);
+                if ((flags & Flag.SEEN) == Flag.SEEN) seen(true); else seen(false);
+                if ((flags & Flag.FLAGGED) == Flag.FLAGGED) flagged(true); else flagged(false);
+                if ((flags & Flag.DRAFT) == Flag.DRAFT) draft(true); else draft(false);
+                if ((flags & Flag.RECENT) == Flag.RECENT) recent(true); else recent(false);
             }
-            
-            File target = new File(today.getPath() + File.separatorChar + time + ".txt");
-            OutputStream os = new FileOutputStream(target);
-            message.dump(os);
-            os.close();
-            return -1;
         }
     }
 
-    public static class FileBased extends Mailbox {
-        private String path;
-        private FileBased(String path) throws IOException { new File(this.path = path).mkdirs(); }
-        public Mailbox slash(String name) throws IOException { return new FileBased(path + "/" + name); }
-
-        public int[] list() {
-            String[] names = new File(path).list();
-            int[] ret = new int[names.length];
-            for(int i=0, j=0; j<ret.length; i++, j++) {
-                try {
-                    ret[j] = Integer.parseInt(names[i].substring(0, names[i].length() - 1));
-                } catch (NumberFormatException nfe) {
-                    Log.warn(FileBased.class, "NumberFormatException: " + names[i].substring(0, names[i].length() - 1));
-                    j--;
-                    int[] newret = new int[ret.length - 1];
-                    System.arraycopy(ret, 0, newret, 0, newret.length);
-                    ret = newret;
-                }
-            }
-            return ret;
-        }
 
-        /** returns a message identifier */
-        public synchronized int add(Message message) throws IOException {
-            int[] all = list();
-            int max = 0;
-            for(int i=0; i<all.length; i++) max = Math.max(max, all[i]);
-            int target = max++;
-            File f = new File(path + File.separatorChar + max + ".-");
-            FileOutputStream fo = new FileOutputStream(f);
-            message.dump(fo);
-            fo.close();
-            f.renameTo(new File(path + File.separatorChar + max + "."));
-            return target;
-        }
+    // Iterator Definition //////////////////////////////////////////////////////////////////////////////
 
-        public Message get(int messagenum) throws IOException {
-            File f = new File(path + File.separatorChar + messagenum + ".");        
-            if (!f.exists()) throw new FileNotFoundException(f.toString());
-            //try {
-                // FIXME: need to store envelope from/to
-                //Message ret = new Message(new LineReader(new InputStreamReader(new FileInputStream(f))));
-                // FIXME: set answered/read/etc here
-                //return ret;
-                return null;
-                /*
-            } catch (MailException.Malformed malf) {
-                Log.error(this, "This should never happen");
-                Log.error(this, malf);
-                return null;
-            }
-                */
-        }
+    public static interface Iterator {
+        public abstract int flags();
+        public abstract Message cur();
+        public abstract boolean next();
+        public abstract int     uid();
+        public abstract int     num();
+        public abstract void    delete();
+        public abstract void    set(String key, String val);
+        public abstract String  get(String key);
+        public abstract boolean seen();
+        public abstract boolean deleted();
+        public abstract boolean flagged();
+        public abstract boolean draft();
+        public abstract boolean answered();
+        public abstract boolean recent();
+        public abstract void    seen(boolean on);
+        public abstract void    deleted(boolean on);
+        public abstract void    flagged(boolean on);
+        public abstract void    draft(boolean on);
+        public abstract void    answered(boolean on);
+        public abstract void    recent(boolean on);
+        public abstract void    addFlags(int flags);
+        public abstract void    removeFlags(int flags);
+        public abstract void    setFlags(int flags);
 
-        // query types: stringmatch (headers, body), header element, deletion status, date range, message size
-        public Message[] query(int maxResults) {
-            throw new RuntimeException("FileBased.query() not implemented yet");
+        public static class Wrapper implements Iterator {
+            private Iterator it;
+            public Wrapper(Iterator it) { this.it = it; }
+            public Message cur() { return it.cur(); }
+            public boolean next() { return it.next(); }
+            public int     uid() { return it.uid(); }
+            public int     flags() { return it.flags(); }
+            public int     num() { return it.num(); }
+            public void    set(String key, String val) { it.set(key, val); }
+            public String  get(String key) { return it.get(key); }
+            public void    delete() { it.delete(); }
+            public boolean seen() { return it.seen(); }
+            public boolean deleted() { return it.deleted(); }
+            public boolean flagged() { return it.flagged(); }
+            public boolean draft() { return it.draft(); }
+            public boolean answered() { return it.answered(); }
+            public boolean recent() { return it.recent(); }
+            public void    seen(boolean on) { it.seen(on); }
+            public void    deleted(boolean on) { it.deleted(on); }
+            public void    flagged(boolean on) { it.flagged(on); }
+            public void    draft(boolean on) { it.draft(on); }
+            public void    answered(boolean on) { it.answered(on); }
+            public void    recent(boolean on) { it.recent(on); }
+            public void    addFlags(int flags) { it.addFlags(flags); }
+            public void    removeFlags(int flags) { it.removeFlags(flags); }
+            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() { do { if (!super.next()) return false; } while(!q.match(this)); return true; }
+        }
     }
 
+    /** 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;
+    }
+    
 }