// 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; import org.prevayler.*; import org.ibex.mail.*; import org.ibex.util.*; import org.ibex.js.*; import org.ibex.io.*; import org.ibex.io.Fountain; import java.io.*; import java.nio.*; 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 implements MailTree { public static final long MAGIC_DATE = 0; private static final char slash = File.separatorChar; // FIXME: ideally this should be weak, but we end up getting duplicates of SqliteMailboxes private static final HashMap instances = new HashMap(); public String toString() { return "[FileBasedMailbox " + path.getAbsolutePath() + "]"; } public MailTree slash(String name, boolean create) { return getFileBasedMailbox(path.getAbsolutePath()+slash+name, create); } public void rmdir(String subdir) { throw new RuntimeException("FIXME not implemented"); } public void rename(String subdir, MailTree newParent, String newName) { throw new RuntimeException("FIXME not implemented"); } public Mailbox getMailbox() { return this; } // FIXME: should be a File() public static synchronized MailTree getFileBasedMailbox(String path, boolean create) { if (path.endsWith(".sqlite")) path = path.substring(0, path.length()-".sqlite".length()); path = new File(path).getAbsolutePath().toString(); try { MailTree ret = instances.get(path); if (ret == null) { Log.error("n", "no match for " + path + " in " + instances.hashCode()); if (new File(path+".sqlite").exists()) ret = new SqliteMailbox(path+".sqlite"); else if (new File(path).exists()) ret = new FileBasedMailbox(new File(path)); else if (create) ret = new SqliteMailbox(path+".sqlite"); else return null; instances.put(path, (MailTree)ret); Log.error("n", "filling " + path + " with " + instances.get(path)); } return ret; } catch (Exception e) { Log.error(FileBasedMailbox.class, e); return null; } } // Instance ////////////////////////////////////////////////////////////////////////////// private File path; private FileLock lock; private int uidNext; private int uidValidity; // Helpers ////////////////////////////////////////////////////////////////////////////// private static void rmDashRf(File f) throws IOException { if (!f.isDirectory()) { f.delete(); return; } String[] children = f.list(); for(int i=0; i=uidNext) uidNext = n; } catch (NumberFormatException nfe) { continue; } } catch(Exception e) { Log.error(this, e); } } } 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 String[] children() { Vec vec = new Vec(); String[] list = sort(path.list()); for(int i=0; i= files.length; } public boolean next() { cur++; return !done(); } 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 // UGLY: Apple Mail doesn't like UID=0, so we add one public int uid() { return done() ? -1 : 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 int getFlags() { return file().lastModified()==MAGIC_DATE ? 0 : Flag.SEEN; } public void setFlags(int flags) { File f = file(); if ((flags & Mailbox.Flag.SEEN) == 0) f.setLastModified(MAGIC_DATE); else if (f.lastModified()==MAGIC_DATE) f.setLastModified(System.currentTimeMillis()); // FIXME: other flags? // FIXME: definitely need DELETED flag in order to do message moves! } public Headers head() { return done() ? null : new Headers(new Fountain.File(file())); } public Message cur() { return Message.newMessage(new Fountain.File(file())); } } // there's no reason this has to operate on a FileBasedMailbox -- it could operate on arbitrary mailboxes! // use this for file attachments: http://jakarta.apache.org/commons/fileupload/ 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).getMailbox(); 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(); } } }