pretty much working
[org.ibex.mail.git] / src / org / ibex / mail / target / FileSystem.java
index ab277a2..8be7d8f 100644 (file)
@@ -8,7 +8,7 @@ import java.util.*;
 import java.text.*;
 
 // FIXME: appallingly inefficient
-public class FileSystem {
+public class FileSystem extends Target {
 
     private static final String STORAGE_ROOT =
         System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
@@ -23,6 +23,7 @@ public class FileSystem {
         }
     }
 
+    public void accept(Message m) throws IOException { add(m); }
     public FileSystem 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"); }
@@ -35,6 +36,28 @@ public class FileSystem {
     public Message[] query(int maxResults) {
         throw new Error(this.getClass().getName() + " does not support the query() method"); }
 
+    public static class Mailbox extends FileSystem {
+        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 FileSystem 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 FileSystem {
         private String path;