almost there
[org.ibex.mail.git] / src / org / ibex / mail / target / Mailbox.java
index a7f32d5..5ff7a8b 100644 (file)
@@ -7,7 +7,20 @@ import java.net.*;
 import java.util.*;
 import java.text.*;
 
-public class Mailbox extends Target {
+public abstract class Mailbox extends Target {
+
+    private 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();
+        }
+    }
 
     // metadata
     public void   set(Message m, String key, String val) { throw new MailException.MetadataNotSupported(); }
@@ -25,148 +38,22 @@ public class Mailbox extends Target {
     public    abstract int  uidValidity();                   // get the uid validity identifier              (see IMAP RFC)
 
     // messages
-    public int     add(Message message)                      throws MailException { throw new Error("not implemented"); }
-    public int     delete(Message message)                   throws MailException { throw new Error("not implemented"); }
-    public void    query(Query q, Message.Visitor v)         throws MailException { throw new Error("not implemented"); }
-    public int     count(Query q)                            throws MailException { throw new Error("not implemented"); }
-    public void    move(Query q, Mailbox dest, boolean copy) throws MailException { throw new Error("not implemented"); }
+    public abstract void visit(Message.Visitor v) throws MailException;   // only abstract method
+    public int  add(Message message)                      throws MailException { throw new MailException("not implemented"); }
+    public int  delete(Message message)                   throws MailException { throw new MailException("not implemented"); }
+    public void move(Query q, Mailbox dest, boolean copy) throws MailException { throw new MailException("not implemented"); }
+    public int  count(Query q) throws MailException { CounterVisitor cv = new CounterVisitor(this,q); visit(cv); return cv.count; }
+    public void query(Query q, final Message.Visitor v) throws MailException {
+        visit(new Message.Visitor() { public void visit(Message m) { if (q.match(Mailbox.this,m)) v.visit(Mailbox.this,m); } }); } 
 
     // submailboxes
-    public void    rename(String newName)                    throws MailException { throw new Error("not implemented"); }
-    public void    destroy()                                 throws MailException { throw new Error("not implemented"); }
-    public Mailbox slash(String name, boolean create)        throws MailException { throw new Error("not implemented"); }
-
-    /** 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 MailException { 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 MailException {
-            try {
-                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;
-                    }
-                }
-                
-                File target = new File(today.getPath() + File.separatorChar + time + ".txt");
-                OutputStream os = new FileOutputStream(target);
-                message.dump(os);
-                os.close();
-                return -1;
-            } catch (IOException e) { throw new MailException.IOException(e); }
-        }
-    }
-
-    public static class FileBased extends Mailbox {
-        private String path;
-        private FileBased(String path) throws MailException { new File(this.path = path).mkdirs(); }
-        public Mailbox slash(String name, boolean create) throws MailException {
-            // FIXME: create
-            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 MailException {
-            try {
-                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;
-            } catch (IOException e) { throw new MailException.IOException(e); }
-        }
+    public void    rename(String newName) throws MailException { throw new MailException("you cannot rename this mailbox"); }
+    public void    destroy() throws MailException { throw new MailException("you cannot destroy this mailbox"); }
+    public Mailbox slash(String name, boolean create) throws MailException { throw new MailException("no submailboxes"); }
 
-        public Message get(int messagenum) throws MailException {
-            File f = new File(path + File.separatorChar + messagenum + ".");        
-            if (!f.exists()) throw new MailException.IOException(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;
-            }
-                */
-            return null;
-        }
-
-        // 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");
-        }
-
-    }
-
-
-    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 MailException {
-        throw new Error(this.getClass().getName() + " does not support the slash() method"); }
-    public synchronized int add(Message message) throws MailException {
-        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;
-    }
-
-
-    private 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();
-        }
+    private static class CounterVisitor implements Message.Visitor {
+        public int count = 0;
+        public void visit(Message m) { if (q.match(Mailbox.this, m)) count++; }
     }
 
     public final void    accept(Message m) throws MailException { add(m); }