bogus patch
[org.ibex.mail.git] / src / org / ibex / mail / target / FileBasedMailbox.java
index c0fd74b..a7eccd4 100644 (file)
@@ -1,15 +1,21 @@
 package org.ibex.mail.target;
 import org.ibex.mail.*;
 import org.ibex.util.*;
-import org.ibex.mail.*;
+import org.ibex.io.*;
+import org.ibex.net.*;
 import java.io.*;
 import java.net.*;
 import java.util.*;
 import java.text.*;
 
+// FIXME: we can omit UIDNEXT!
+// FIXME use directory date/time as UIDNEXT and file date/time as UID; need to 'correct' file date/time after changes
+
 /** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
 public class FileBasedMailbox extends Mailbox.Default {
 
+    public String toString() { return "[FileBasedMailbox " + path + "]"; }
+
     private static final char slash = File.separatorChar;
     private static final Hashtable instances = new Hashtable();
     public static FileBasedMailbox getFileBasedMailbox(String path, boolean create) {
@@ -81,36 +87,47 @@ public class FileBasedMailbox extends Mailbox.Default {
         } catch (IOException e) { throw new MailException.IOException(e); }
     }        
 
-    public synchronized int add(Message message) {
+    public synchronized void add(Message message) { add(message, Mailbox.Flag.RECENT); }
+    public synchronized void add(Message message, int flags) {
+        Log.error(this, "adding message to ["+toString()+"]:\n" + message.summary());
         try {
             int num = new File(path).list(filter).length;
-            File target = new File(path + slash + uidNext(true) + ".");
+           String name = path + slash + uidNext(true) + "." +
+                ((flags & Mailbox.Flag.DELETED) == Mailbox.Flag.DELETED ? "x" : "") +
+                ((flags & Mailbox.Flag.DRAFT) == Mailbox.Flag.DRAFT ? "d" : "") +
+                ((flags & Mailbox.Flag.RECENT) == Mailbox.Flag.RECENT ? "r" : "") +
+                ((flags & Mailbox.Flag.ANSWERED) == Mailbox.Flag.ANSWERED ? "a" : "") +
+                ((flags & Mailbox.Flag.FLAGGED) == Mailbox.Flag.FLAGGED ? "f" : "") +
+                ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN ? "s" : "");
+            File target = new File(name);
             File f = new File(target.getCanonicalPath() + "-");
             FileOutputStream fo = new FileOutputStream(f);
-            message.dump(fo);
+            message.dump(new Stream(fo));
             fo.close();
             f.renameTo(target);
-            return num;
+            Log.error(this, " done writing to " + target);
         } catch (IOException e) { throw new MailException.IOException(e); }
     }
 
-    private class Iterator implements Mailbox.Default.Iterator {
+    private class Iterator extends Mailbox.Default.Iterator {
         int cur = -1;
         private String[] names;
         private boolean seen = false, deleted = false, draft = false, flagged = false, answered = false, recent = false;
         public Iterator() { names = new File(path).list(filter); }
 
         public Message cur() {
+           if (cur >= names.length) return null;
             try {
                 File file = new File(path + File.separatorChar + names[cur]);
-                return new Message(/* FIXME */ null, /* FIXME */ new Address[] { },
-                                   new LineReader(new InputStreamReader(new FileInputStream(file))));
-            } catch (IOException e) { throw new MailException.IOException(e); }
+                return new Message(null, null, new Stream(new FileInputStream(file)));
+            } catch (IOException e) { throw new MailException.IOException(e);
+            } catch (Message.Malformed e) { throw new MailException(e.getMessage()); }
         }
         public boolean next() {
             cur++;
             if (cur >= names.length) return false;
             String name = names[cur].substring(names[cur].indexOf('.') + 1);
+           if (!(new File(path + File.separatorChar + names[cur])).exists()) return next();
             seen     = name.indexOf('s') != -1;
             deleted  = name.indexOf('x') != -1;
             flagged  = name.indexOf('f') != -1;
@@ -135,11 +152,14 @@ public class FileBasedMailbox extends Mailbox.Default {
                 (draft ? "d" : "") +
                 (recent ? "r" : "") +
                 (answered ? "a" : "");
-            new File(names[cur]).renameTo(new File(path + File.separatorChar + newName));
+            new File(path + File.separatorChar + names[cur]).renameTo(new File(path + File.separatorChar + newName));
             names[cur] = newName;
         }
 
-        public void    delete() { new File(names[cur]).delete(); }
+        public void    delete() {
+           new File(path + File.separatorChar + names[cur]).delete();
+           // FIXME remove from list?
+       }
         public void    set(String key, String val) { throw new MailException("not supported"); }
         public String  get(String key) { throw new MailException("not supported"); }