more fixups to MailingList
[org.ibex.mail.git] / src / org / ibex / mail / target / FileBasedMailbox.java
index aaedf52..2fe5996 100644 (file)
@@ -1,3 +1,7 @@
+// 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.target;
 import org.prevayler.*;
 import org.ibex.mail.*;
@@ -9,12 +13,13 @@ import java.nio.channels.*;
 import java.net.*;
 import java.util.*;
 import java.text.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
 
 /** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
 public class FileBasedMailbox extends Mailbox.Default {
 
     public static final long MAGIC_DATE = 0;
-
     private static final char slash = File.separatorChar;
     private static final WeakHashMap<String,FileBasedMailbox> instances = new WeakHashMap<String,FileBasedMailbox>();
     public String toString() { return "[FileBasedMailbox " + path.getAbsolutePath() + "]"; }
@@ -35,126 +40,12 @@ public class FileBasedMailbox extends Mailbox.Default {
         }
     }
 
-
     // Instance //////////////////////////////////////////////////////////////////////////////
 
     private File path;
     private FileLock lock;
-    private Prevayler prevayler;
-    private Cache cache;
-
-    public static class Cache implements Serializable {
-        public            final int uidValidity = Math.abs(new Random().nextInt());
-        private           final Hashtable<String,Entry> byname = new Hashtable<String,Entry>();
-        private           final Hashtable<Integer,Entry> byuid = new Hashtable<Integer,Entry>();
-        private           final ArrayList<Entry>        linear = new ArrayList<Entry>();
-        public            final File dir;
-        private                 int uidNext = 0;
-
-        public Cache(File dir) { this.dir = dir; }
-
-        public void init(final Prevayler prevayler) throws IOException {
-            dir.mkdirs();
-            long time = System.currentTimeMillis();
-            Log.info(this, "initializing maildir " + dir.getParent());
-
-            // Drop entries whose files have vanished
-            ArrayList<Transaction> kill = new ArrayList<Transaction>();
-            for(String s : byname.keySet())
-                if (!new File(dir.getParent() + slash + s).exists())
-                    kill.add(byname.get(s).delete(null));
-            for(Transaction t : kill) prevayler.execute(t);
-
-            // Make entries for new files which have appeared
-            for(String file : new File(dir.getParent()).list()) {
-                File f = new File(dir.getParent() + slash + file);
-                if (file.charAt(0)!='.' && !f.isDirectory()) {
-                    Entry e = get(file);
-                    if (e == null)
-                        prevayler.execute(Entry.create(f));
-                    else if ((f.lastModified() == MAGIC_DATE) != e.seen())
-                        prevayler.execute(e.seen(f, !e.seen()));
-                }
-            }
-
-            // Take a snapshot for posterity
-            if (System.currentTimeMillis() - time > 1000 * 5)
-                Log.info(this, "  done initializing maildir " + dir.getParent());
-            new Thread() { public void run() {
-                try { prevayler.takeSnapshot(); } catch (Exception e) { Log.error(this, e); } } }.start();
-        }
-
-       
-        public synchronized int size() { return linear.size(); }
-        public synchronized int uidNext() { return uidNext; }
-        public synchronized Entry get(int uid) { return byuid.get(uid); }
-        public synchronized Entry getLinear(int num) { return linear.get(num); }
-        public synchronized Entry get(String name) { return byname.get(name); }
-
-        public static class Entry implements Serializable {
-            private final byte[] header;
-            public final String name;
-            public final int uid;
-            private boolean seen;
-            private Entry(String name, boolean seen, int uid, byte[] header) {
-                this.name = name; this.seen = seen; this.uid = uid; this.header = header; }
-
-            // Accessors //////////////////////////////////////////////////////////////////////////////
-
-            public boolean seen() { return seen; }
-            public MIME.Headers headers() { return new MIME.Headers(new Stream(new ByteArrayInputStream(header)), true); }
-            public Message message(Cache cache) { try {
-                FileInputStream fis = null;
-                try {
-                    fis = new FileInputStream(cache.dir.getParent() + slash + name);
-                    return Message.newMessage(new Stream(fis));
-                } finally { if (fis != null) fis.close(); }
-            } catch (IOException e) { throw new MailException.IOException(e);
-            } catch (Message.Malformed e) { throw new MailException(e.getMessage()); }
-            }
-
-            // Transactions //////////////////////////////////////////////////////////////////////////////
-
-            public static Transaction create(File f) throws IOException {
-                final boolean seen = f.lastModified() == MAGIC_DATE;
-                final String name = f.getName();
-                final byte[] header = new MIME.Headers(new Stream(new FileInputStream(f)), true).toString().getBytes();
-                return new Transaction() {
-                        public void executeOn(Object o, Date now) {
-                            Cache cache = (Cache)o;
-                            synchronized(cache) {
-                                Entry e = new Entry(name, seen, cache.uidNext++, header);
-                                cache.linear.add(e);
-                                cache.byuid.put(e.uid, e);
-                                cache.byname.put(e.name, e);
-                            } } };
-            }
-
-            public Transaction seen(File f, final boolean seen) {
-                f.setLastModified(seen ? MAGIC_DATE : System.currentTimeMillis());
-                final int uid = this.uid;
-                return new Transaction() {
-                        public void executeOn(Object o, Date now) {
-                            Cache cache = (Cache)o;
-                            synchronized(cache) {
-                                cache.get(uid).seen = seen;
-                            } } };
-            }
-            public Transaction delete(File f) {
-                if (f != null && f.exists()) f.delete();
-                final int uid = this.uid;
-                return new Transaction() {
-                        public void executeOn(Object o, Date now) {
-                            Cache cache = (Cache)o;
-                            synchronized(cache) {
-                                Cache.Entry e = cache.get(uid);
-                                cache.byname.remove(e.name);
-                                cache.linear.remove(e);
-                                cache.byuid.remove(uid);
-                            } } };
-            }
-        }
-    }
+    private int uidNext;
+    private int uidValidity;
 
     // Helpers //////////////////////////////////////////////////////////////////////////////
 
@@ -170,27 +61,23 @@ public class FileBasedMailbox extends Mailbox.Default {
         path.mkdirs();
 
         // acquire lock
-        lock = new RandomAccessFile(this.path.getAbsolutePath() + slash + ".lock", "rw").getChannel().tryLock();
+        File lockfile = new File(this.path.getAbsolutePath() + slash + ".lock");
+        lock = new RandomAccessFile(lockfile, "rw").getChannel().tryLock();
         if (lock == null) throw new IOException("unable to lock FileBasedMailbox");
-
-        File cacheDir = new File(path.getAbsolutePath() + slash + ".cache");
-        try {
-            prevayler = PrevaylerFactory.createPrevayler(new Cache(cacheDir), cacheDir.getAbsolutePath());
-            cache = (Cache)prevayler.prevalentSystem();
-        } catch (Throwable t) {
-            Log.warn(this, "error while attempting to reconstitute a FileBasedMailbox.cache:");
-            Log.warn(this, t);
-            Log.warn(this, "discarding cache...");
-            rmDashRf(cacheDir);
-            prevayler = PrevaylerFactory.createPrevayler(new Cache(cacheDir), cacheDir.getAbsolutePath());
-            cache = (Cache)prevayler.prevalentSystem();
+        uidValidity = (int)(lockfile.lastModified() & 0xffffffff);
+        uidNext = 0;
+        String[] files = path.list();
+        for(int i=0; i<files.length; i++) {
+            try {
+                if (files[i].indexOf('.') != -1) files[i] = files[i].substring(0, files[i].indexOf('.'));
+                int n = Integer.parseInt(files[i]);
+                if (n>=uidNext) uidNext = n;
+            } catch(Exception e) { /* DELIBERATE */ }
         }
-        cache.init(prevayler);
     }
 
 
     public Mailbox.Iterator  iterator()           { return new Iterator(); }
-    public int               uidValidity()        { return cache.uidValidity; }
     public synchronized void add(Message message) { add(message, Mailbox.Flag.RECENT); }
     public String[] children() {
         Vec vec = new Vec();
@@ -202,11 +89,12 @@ public class FileBasedMailbox extends Mailbox.Default {
         return (String[])vec.copyInto(new String[vec.size()]);
     }
 
-    public int uidNext() { return cache.uidNext(); }
+    public int uidValidity() { return uidValidity; }
+    public int uidNext() {  return uidNext; }
     public synchronized void add(Message message, int flags) {
         try {
             String name, fullname; File target, f;
-            for(int i = cache.uidNext(); ; i++) {
+            for(int i = uidNext; ; i++) {
                 name = i + ".";
                 fullname = path.getAbsolutePath() + slash + name;
                 target = new File(fullname);
@@ -214,31 +102,173 @@ public class FileBasedMailbox extends Mailbox.Default {
                 if (!f.exists() && !target.exists()) break;
                 Log.error(this, "aieeee!!!! target of add() already exists: " + target.getAbsolutePath());
             }
-            FileOutputStream fo = new FileOutputStream(f);
-            Stream stream = new Stream(fo);
-            message.dump(stream);
-            fo.close();
+            Stream stream = new Stream(new FileOutputStream(f));
+            message.getStream().transcribe(stream);
+            stream.close();
             f.renameTo(new File(fullname));
+            uidNext++;
             f = new File(fullname);
             if ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN) f.setLastModified(MAGIC_DATE);
-            prevayler.execute(Cache.Entry.create(f));
         } catch (IOException e) { throw new MailException.IOException(e); }
         Log.info(this, path + " <= " + message.summary());
     }
 
     private class Iterator extends Mailbox.Default.Iterator {
         int cur = -1;
-        private Cache.Entry entry() { return cache.getLinear(cur); }
-        private File file() { return new File(cache.dir.getParent() + slash + entry().name); }
-        public MIME.Headers head() { return done() ? null : entry().headers(); }
-        public boolean done() { return cur >= cache.size(); }
+        String[] files = path.list(new FilenameFilter() { public boolean accept(File dir, String name) {
+            return name.endsWith(".");
+        } });
+        private File file() { return new File(path.getAbsolutePath() + slash + files[cur]); }
+        public boolean done() { return cur >= files.length; }
         public boolean next() { cur++; return !done(); }
-        public boolean seen() { return done() ? false : entry().seen(); }
+        public boolean seen() { return false; }
+        public boolean recent() { return false; }
         public int num() { return cur+1; }  // EUDORA insists that message numbers start at 1, not 0
-        public int uid() { return done() ? -1 : entry().uid; }
-        public void delete() { prevayler.execute(entry().delete(file())); }
-        public void seen(boolean seen) { prevayler.execute(entry().seen(file(), seen)); }
-        public Message cur() { return entry().message(cache); }
+        public int uid() { return done() ? -1 : Integer.parseInt(files[cur].substring(0, files[cur].length()-1)); }
+        public void delete() { File f = file(); if (f != null && f.exists()) f.delete(); }
+        public void seen(boolean seen) { }
+        public Headers head() {
+            if (done()) return null;
+            FileInputStream fis = null;
+            try {
+                return new Headers(new Stream(new FileInputStream(file())));
+            } catch (IOException e) { throw new MailException.IOException(e);
+            } finally { if (fis != null) try { fis.close(); } catch (Exception e) { /* DELIBERATE */ } }
+        }
+        public Message cur() {
+            FileInputStream fis = null;
+            try {
+                return Message.newMessage(new Fountain.File(file()));
+                //} catch (IOException e) { throw new MailException.IOException(e);
+            } catch (Message.Malformed e) { throw new MailException(e.getMessage());
+            } finally { if (fis != null) try { fis.close(); } catch (Exception e) { /* DELIBERATE */ } }
+        }
     }
 
+    public static class Servlet extends HttpServlet {
+        public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {doGet(request, response);}
+        private void frames(HttpServletRequest request, HttpServletResponse response, boolean top) throws IOException {
+            String basename = request.getServletPath();
+            PrintWriter pw = new PrintWriter(response.getWriter());
+            pw.println("<html>");
+            if (top) {
+                pw.println("  <frameset rows='30%,*'>");
+                pw.println("    <frame src='"+basename+"?frame=top' marginwidth=0 marginheight=0 name=top/>");
+                pw.println("    <frame src='"+basename+"?frame=bottom' marginwidth=0 marginheight=0 name='bottom'/>");
+            } else {
+                pw.println("  <frameset cols='150,*'>");
+                pw.println("    <frame src='"+basename+"?frame=topleft' marginwidth=0 marginheight=0 name=topleft/>");
+                pw.println("    <frame src='"+basename+"?frame=topright' marginwidth=0 marginheight=0 name=topright/>");
+            }
+            pw.println("  </frameset>");
+            pw.println("</html>");
+            pw.flush();
+        }
+
+        public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
+            String frame = request.getParameter("frame");
+            String basename = request.getServletPath();
+
+            if (frame == null) { frames(request, response, true); return; }
+            if (frame.equals("top")) { frames(request, response, false); return; }
+            if (frame.equals("topleft")) { return; }
+
+            if (request.getServletPath().indexOf("..") != -1) throw new IOException(".. not allowed in image paths");
+            ServletContext cx = getServletContext();
+            String path = cx.getRealPath(request.getServletPath());
+            Mailbox mbox = FileBasedMailbox.getFileBasedMailbox(path, false);
+            if (mbox == null) throw new IOException("no such mailbox: " + path);
+
+            Vec msgs = new Vec();
+            for(Mailbox.Iterator it = mbox.iterator(); it.next();) {
+                String[] s = new String[4];
+                Message m = it.cur();
+                s[0] = (m.from==null?"":m.from.toString(true));
+                s[1] = m.subject;
+                s[2] = (m.date + "").trim().replaceAll(" ","&nbsp;");
+                s[3] = it.num() + "";
+                msgs.addElement(s);
+            }
+            String[][] messages;
+            msgs.copyInto(messages = new String[msgs.size()][]);
+
+            if ("bottom".equals(frame)) { bottom(request, response, messages, mbox); return; }
+            if ("topright".equals(frame)) { topright(request, response, messages); return; }
+        }
+
+        private void bottom(HttpServletRequest request, HttpServletResponse response, String[][] messages, Mailbox mbox)
+            throws IOException {
+            PrintWriter pw = new PrintWriter(response.getWriter());
+            pw.println("<html>");
+            pw.println("  <body>");
+            pw.println("    <pre>");
+            if (request.getParameter("msgnum") != null) {
+                int target = Integer.parseInt(request.getParameter("msgnum"));
+                for(Mailbox.Iterator it = mbox.iterator(); it.next();) {
+                    if (it.num() == target) {
+                        StringBuffer tgt = new StringBuffer();
+                        it.cur().getBody().getStream().transcribe(tgt);
+                        pw.println(tgt.toString());
+                        break;
+                    }
+                }
+            }
+            pw.println("    </pre>");
+            pw.println("  </body>");
+            pw.println("</html>");
+        }
+        
+        private void topright(HttpServletRequest request, HttpServletResponse response, String[][] messages) throws IOException {
+            PrintWriter pw = new PrintWriter(response.getWriter());
+            String basename = request.getServletPath();
+            pw.println("<html>");
+            pw.println("  <head>");
+            pw.println("    <style>");
+            pw.println("      a:link    { color: #000; text-decoration: none; }");
+            pw.println("      a:active  { color: #f00; text-decoration: none; }");
+            pw.println("      a:visited { color: #777; text-decoration: none; }");
+            pw.println("      a:hover   { color: #000; text-decoration: none; }");
+            pw.println("      /* a:hover   { color: #00f; text-decoration: none; border-bottom: 1px dotted; } */");
+            pw.println("    </style>");
+            pw.println("  </head>");
+            pw.println("  <body onKeyPress='doKey(event.keyCode)'>");
+            pw.println("    <script>");
+            pw.println("      var chosen = null;");
+            pw.println("      var all = [];");
+            pw.println("      function doKey(x) {");
+            pw.println("          if (chosen == null) { choose(all[0]); return; }");
+            pw.println("          switch(x) {");
+            pw.println("              case 112: if (chosen.id > 0) choose(all[chosen.id-1]); break;");
+            pw.println("              case 110: if (chosen.id < (all.length-1)) choose(all[1+(1*chosen.id)]); break;");
+            pw.println("          }");
+            pw.println("      }");
+            pw.println("      function choose(who) {");
+            pw.println("          who.style.background = '#ffc';");
+            pw.println("          if (chosen != null) chosen.style.background = '#aaa';");
+            pw.println("          top.bottom.location='"+basename+"?frame=bottom&msgnum='+who.id;");
+            pw.println("          chosen = who;");
+            pw.println("      }");
+            pw.println("    </script>");
+            pw.println("    <table width=100% border=0 cellpadding=0 cellspacing=0>");
+            boolean odd=false;
+            for(int i=0; i<messages.length; i++) {
+                odd = !odd;
+                String[] m = messages[i];
+                pw.println("      <tr style='background: "+(odd?"#e8eef7":"white")+"' id='"+m[3]+"' "+
+                           "onmouseover='this.style.color=\"blue\"' "+
+                           "onmouseout='this.style.color=\"black\"' "+
+                           "onclick='choose(this);'>");
+                pw.println("<td style='padding:5px; padding-bottom:2px'>"+m[0]+"</td>");
+                pw.println("<td style='padding:5px; padding-bottom:2px'>"+m[1]+"</td>");
+                pw.println("<td style='padding:5px; padding-bottom:2px'>"+m[2]+"</td>");
+                pw.println("</tr>");
+                pw.println("<script> all["+i+"] = document.getElementById('"+i+"'); </script>");
+                pw.println("<tr height=1 bgcolor=black><td colspan=3>");
+                pw.println("    <img src=http://www.xwt.org/images/clearpixel.gif></td></tr>");
+            }
+            pw.println("    </table>");
+            pw.println("  </body>");
+            pw.println("</html>");
+        }
+    }
 }