X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fmail%2Ftarget%2FFileBasedMailbox.java;h=c700ae0bd7296282fcd41ea926909f822d803f74;hb=899bb02c7f7cbddf0671f1babd7a161aeace8dee;hp=a934fac088861027b8a9bf3482cba81fb30bbde8;hpb=2dc579e8aa98e6823094d326d6447aaa647478e1;p=org.ibex.mail.git diff --git a/src/org/ibex/mail/target/FileBasedMailbox.java b/src/org/ibex/mail/target/FileBasedMailbox.java index a934fac..c700ae0 100644 --- a/src/org/ibex/mail/target/FileBasedMailbox.java +++ b/src/org/ibex/mail/target/FileBasedMailbox.java @@ -1,203 +1,287 @@ +// 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.*; import org.ibex.util.*; import org.ibex.io.*; import java.io.*; +import java.nio.*; +import java.nio.channels.*; 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 +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 String toString() { return "[FileBasedMailbox " + path + "]"; } - + public static final long MAGIC_DATE = 0; private static final char slash = File.separatorChar; - private static final Hashtable instances = new Hashtable(); - public static FileBasedMailbox getFileBasedMailbox(String path, boolean create) { - FileBasedMailbox ret = (FileBasedMailbox)instances.get(path); - if (ret != null) return ret; - File f = new File(path); - if (!create && !f.exists()) return null; - instances.put(path, ret = new FileBasedMailbox(path)); - return ret; + 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) { + try { + FileBasedMailbox ret = instances.get(path); + if (ret == null) { + if (!create && !(new File(path).exists())) return null; + instances.put(path, ret = new FileBasedMailbox(new File(path))); + } + return ret; + } catch (Exception e) { + Log.error(FileBasedMailbox.class, e); + return null; + } } - public static final FilenameFilter filter = new FilenameFilter() { - public boolean accept(File f, String s) { - return s.indexOf('.') != -1; - } }; + // Instance ////////////////////////////////////////////////////////////////////////////// + + private File path; + private FileLock lock; + private int uidNext; + private int uidValidity; + // Helpers ////////////////////////////////////////////////////////////////////////////// - // Instance ////////////////////////////////////////////////////////////////////////////// + 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(Exception e) { /* DELIBERATE */ } + } } - public Mailbox slash(String name, boolean create) { - return FileBasedMailbox.getFileBasedMailbox(path + slash + name, create); } + public Mailbox.Iterator iterator() { return new Iterator(); } + public synchronized void add(Message message) { add(message, Mailbox.Flag.RECENT); } public String[] children() { Vec vec = new Vec(); - String[] list = new File(path).list(); + String[] list = path.list(); for(int i=0; i= files.length; } + public boolean next() { cur++; return !done(); } + 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 : 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() { - if (cur >= names.length) return null; + FileInputStream fis = null; try { - File file = new File(path + File.separatorChar + names[cur]); - FileInputStream fis = new FileInputStream(file); - Stream stream = new Stream(fis); - Address envelopeFrom = null; - Address envelopeTo = null; - /* - for(String s = stream.readln(); s != null; s = stream.readln()) { - if (s.startsWith("X-org.ibex.mail.headers.envelope.From: ")) - envelopeFrom = Address.parse(s.substring(38).trim()); - else if (s.startsWith("X-org.ibex.mail.headers.envelope.To: ")) - envelopeTo = Address.parse(s.substring(36).trim()); - else { - stream.unread(s + "\r\n"); + 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(" "); + } 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.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(""); + pw.println(" "); + pw.println("
");
+            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;
                     }
                 }
-                */
-                Message ret = new Message(stream, new Message.Envelope(null, null, new Date(file.lastModified())));
-                fis.close();
-                return ret;
-            } 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;
-            draft    = name.indexOf('d') != -1;
-            answered = name.indexOf('a') != -1;
-            recent   = name.indexOf('r') != -1;
-            return true;
+            }
+            pw.println("    
"); + pw.println(" "); + pw.println(""); + pw.flush(); + pw.close(); } - public int num() { return cur; } - public int uid() { - try { return Integer.parseInt(names[cur].substring(0, names[cur].indexOf('.'))); - } catch (NumberFormatException nfe) { - Log.warn(FileBasedMailbox.class, "NumberFormatException: " + names[cur].substring(0, names[cur].length() - 1)); - return -1; } } - - private void fixflags() { - String newName = - names[cur].substring(0, names[cur].indexOf('.') + 1) + - (seen ? "s" : "") + - (deleted ? "x" : "") + - (flagged ? "f" : "") + - (draft ? "d" : "") + - (recent ? "r" : "") + - (answered ? "a" : ""); - new File(path + File.separatorChar + names[cur]).renameTo(new File(path + File.separatorChar + newName)); - names[cur] = newName; + + 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); } - 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"); } - - public boolean seen() { return seen; } - public boolean deleted() { return deleted; } - public boolean flagged() { return flagged; } - public boolean draft() { return draft; } - public boolean answered() { return answered; } - public boolean recent() { return recent; } - public void seen(boolean on) { seen = on; fixflags(); } - public void deleted(boolean on) { deleted = on; fixflags(); } - public void flagged(boolean on) { flagged = on; fixflags(); } - public void draft(boolean on) { draft = on; fixflags(); } - public void answered(boolean on) { answered = on; fixflags(); } - public void recent(boolean on) { recent = on; fixflags(); } - + 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(" "); + boolean odd=false; + for(int i=0; i"); + pw.println(""); + 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(); + } } }