X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fmail%2Ftarget%2FFileBasedMailbox.java;h=0a8c013f1c55d9befaefe420da8db84dd0010863;hb=4ff26332350edaa3c6ebfa29408c61412a6a2e40;hp=5ced682384bcbb26723652920432b9886e518049;hpb=e40fe7d79dd24a342eb9da7b7edae30ba5112165;p=org.ibex.mail.git diff --git a/src/org/ibex/mail/target/FileBasedMailbox.java b/src/org/ibex/mail/target/FileBasedMailbox.java index 5ced682..0a8c013 100644 --- a/src/org/ibex/mail/target/FileBasedMailbox.java +++ b/src/org/ibex/mail/target/FileBasedMailbox.java @@ -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,24 +13,30 @@ 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 instances = new WeakHashMap(); + private static final WeakHashMap instances = new WeakHashMap(); public String toString() { return "[FileBasedMailbox " + path.getAbsolutePath() + "]"; } public Mailbox slash(String name, boolean create) { return getFileBasedMailbox(path.getAbsolutePath()+slash+name, create); } // FIXME: should be a File() - public static synchronized FileBasedMailbox getFileBasedMailbox(String path, boolean create) { + public static synchronized Mailbox getFileBasedMailbox(String path, boolean create) { try { - FileBasedMailbox ret = instances.get(path); + Mailbox ret = instances.get(path); if (ret == null) { if (!create && !(new File(path).exists())) return null; - instances.put(path, ret = new FileBasedMailbox(new File(path))); + if (new File(new File(path)+"/subscribers").exists()) { + ret = new MailingList(new File(path), new FileBasedMailbox(new File(path))); + } else { + ret = new FileBasedMailbox(new File(path)); + } + instances.put(path, ret); } return ret; } catch (Exception e) { @@ -35,125 +45,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 = new Random().nextInt(); - private final Hashtable byname = new Hashtable(); - private final Hashtable byuid = new Hashtable(); - private final ArrayList linear = new ArrayList(); - 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(); - Log.info(this, "initializing maildir " + dir.getParent()); - - // Drop entries whose files have vanished - ArrayList kill = new ArrayList(); - 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 - 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 new Message(new Stream(fis), new Message.Envelope(null, null, new Date())); - } 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; - Log.error(Entry.class, "create with seen = " + seen); - 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 ////////////////////////////////////////////////////////////////////////////// @@ -169,31 +66,48 @@ 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=uidNext) uidNext = n; + } catch(Exception e) { Log.error(this, e); } } - cache.init(prevayler); } + public String[] sort(String[] s) { + Arrays.sort(s); + return s; + } + + public String[] files() { + String[] s = path.list(filter); + Arrays.sort(s, comparator); + return s; + } + + private static Comparator comparator = new Comparator() { + public int compare(String a, String b) { + if (a.indexOf('.')==-1) return a.compareTo(b); + if (b.indexOf('.')==-1) return a.compareTo(a); + int ai = Integer.parseInt(a.substring(0, a.indexOf('.'))); + int bi = Integer.parseInt(b.substring(0, b.indexOf('.'))); + return aibi ? 1 : 0; + } + }; 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(); - String[] list = path.list(); + String[] list = sort(path.list()); for(int i=0; i= cache.size(); } + String[] files = files(); + 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 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 boolean seen() { return false; } + public boolean recent() { return false; } + public int nntpNumber() { return cur+1; } // FIXME: lame + public int imapNumber() { return cur+1; } // EUDORA insists that message numbers start at 1, not 0 + 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.Original(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.getRequestURI(); + PrintWriter pw = new PrintWriter(response.getWriter()); + pw.println(""); + if (top) { + pw.println(" "); + //pw.println(" "); + //pw.println(" "); + pw.println(" "); + pw.println(" "); + } else { + pw.println(" "); + pw.println(" "); + pw.println(" "); + } + pw.println(" "); + pw.println(""); + pw.flush(); + } + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + String frame = request.getParameter("frame"); + String basename = request.getRequestURI(); + + if (frame == null) { frames(request, response, true); return; } + if (frame.equals("top")) { frames(request, response, false); return; } + if (frame.equals("banner")) { banner(request, response); 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(" "," "); + s[3] = it.imapNumber() + ""; + 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(""); + pw.println(" "); + pw.println(" "); + pw.println(" "); + pw.println(" "); + if (request.getParameter("msgnum") != null) { + int target = Integer.parseInt(request.getParameter("msgnum")); + Mailbox.Iterator it = mbox.iterator(); + while(it.next()) + if (it.imapNumber() == target) + break; + if (it.cur() != null) { + pw.println(" "); + pw.println("
"); + Headers h = it.cur().headers; + pw.println(" Subject: " + it.cur().subject +"
"); + pw.println(" From: " + it.cur().from +"
"); + pw.println(" Date: " + it.cur().date +"
"); + /* + for(java.util.Enumeration e = h.names(); e.hasMoreElements();) { + String key = (String)e.nextElement(); + if (key==null || key.length()==0 || key.equals("from") || + key.equals("to") || key.equals("subject") || key.equals("date")) continue; + key = Character.toUpperCase(key.charAt(0)) + key.substring(1); + String val = h.get(key); + for(int i=key.length(); i<15; i++)pw.print(" "); + pw.print(key+": "); + pw.println(val); + } + */ + pw.print("
");
+                    StringBuffer tgt = new StringBuffer();
+                    it.cur().getBody().getStream().transcribe(tgt);
+                    pw.println(tgt.toString());
+                    pw.println("    
"); + } + } + pw.println(" "); + pw.println(""); + pw.flush(); + pw.close(); + } + + private void banner(HttpServletRequest request, HttpServletResponse response) throws IOException { + String basename = request.getServletPath(); + String realpath = getServletContext().getRealPath(basename); + /* + MailingList list = MailingList.getMailingList(realpath); + list.banner(request, response); + */ + banner(request, response); + } + + private void topright(HttpServletRequest request, HttpServletResponse response, String[][] messages) throws IOException { + PrintWriter pw = new PrintWriter(response.getWriter()); + String basename = request.getRequestURI(); + pw.println(""); + pw.println(" "); + pw.println(" "); + pw.println(" "); + pw.println(" "); + pw.println(" "); + pw.println("
"); + pw.println(" "); + pw.println(" "); + pw.println("
"); + pw.flush(); + banner(request, response); + pw.println("
"); + pw.println(" "); + boolean odd=true; + for(int i=0; i"); + pw.println(""); + pw.println(""); + pw.println(""); + pw.println(""); + pw.println(""); + } + pw.println("
"+m[0]+""+m[1]+""+m[2]+"
"); + pw.println("
"); + pw.println(" "); + pw.println(""); + pw.flush(); + pw.close(); + } + } }