1 package org.ibex.mail.target;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
11 // FIXME: we can omit UIDNEXT!
12 // FIXME use directory date/time as UIDNEXT and file date/time as UID; need to 'correct' file date/time after changes
14 /** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
15 public class FileBasedMailbox extends Mailbox.Default {
17 public String toString() { return "[FileBasedMailbox " + path + "]"; }
19 private static final char slash = File.separatorChar;
20 private static final Hashtable instances = new Hashtable();
21 public static FileBasedMailbox getFileBasedMailbox(String path, boolean create) {
22 FileBasedMailbox ret = (FileBasedMailbox)instances.get(path);
23 if (ret != null) return ret;
24 File f = new File(path);
25 if (!create && !f.exists()) return null;
26 instances.put(path, ret = new FileBasedMailbox(path));
30 public static final FilenameFilter filter = new FilenameFilter() {
31 public boolean accept(File f, String s) {
32 return s.indexOf('.') != -1;
36 // Instance //////////////////////////////////////////////////////////////////////////////
40 private FileBasedMailbox(String path) throws MailException {
41 new File(this.path = path).mkdirs();
45 public Mailbox slash(String name, boolean create) {
46 return FileBasedMailbox.getFileBasedMailbox(path + slash + name, create); }
48 public String[] children() {
50 String[] list = new File(path).list();
51 for(int i=0; i<list.length; i++) {
52 if (!(new File(path + slash + list[i]).isDirectory())) continue;
53 vec.addElement(list[i]);
55 String[] ret = new String[vec.size()];
60 public Mailbox.Iterator iterator() { return new FileBasedMailbox.Iterator(); }
61 public int uidValidity() { return (int)(new File(path).lastModified() & 0xffffffL); }
63 public int uidNext() { return uidNext(false); }
64 public int uidNext(boolean inc) {
66 uidNext = new File(path + slash + "UIDNEXT");
67 if (!uidNext.exists()) {
68 File tmp = new File(uidNext + "-");
69 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
73 tmp.renameTo(uidNext);
76 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(uidNext)));
77 int ret = Integer.parseInt(br.readLine());
79 File tmp = new File(uidNext + "-");
80 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
84 tmp.renameTo(uidNext);
87 } catch (IOException e) { throw new MailException.IOException(e); }
90 public synchronized void add(Message message) { add(message, Mailbox.Flag.RECENT); }
91 public synchronized void add(Message message, int flags) {
92 Log.error(this, "adding message to ["+toString()+"]:\n" + message.summary());
94 int num = new File(path).list(filter).length;
95 String name = path + slash + uidNext(true) + "." +
96 ((flags & Mailbox.Flag.DELETED) == Mailbox.Flag.DELETED ? "x" : "") +
97 ((flags & Mailbox.Flag.DRAFT) == Mailbox.Flag.DRAFT ? "d" : "") +
98 ((flags & Mailbox.Flag.RECENT) == Mailbox.Flag.RECENT ? "r" : "") +
99 ((flags & Mailbox.Flag.ANSWERED) == Mailbox.Flag.ANSWERED ? "a" : "") +
100 ((flags & Mailbox.Flag.FLAGGED) == Mailbox.Flag.FLAGGED ? "f" : "") +
101 ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN ? "s" : "");
102 File target = new File(name);
103 File f = new File(target.getCanonicalPath() + "-");
104 FileOutputStream fo = new FileOutputStream(f);
105 message.dump(new Stream(fo));
108 Log.error(this, " done writing to " + target);
109 } catch (IOException e) { throw new MailException.IOException(e); }
112 private class Iterator extends Mailbox.Default.Iterator {
114 private String[] names;
115 private boolean seen = false, deleted = false, draft = false, flagged = false, answered = false, recent = false;
116 public Iterator() { names = new File(path).list(filter); }
118 public Message cur() {
119 if (cur >= names.length) return null;
121 File file = new File(path + File.separatorChar + names[cur]);
122 return new Message(null, null, new Stream(new FileInputStream(file)));
123 } catch (IOException e) { throw new MailException.IOException(e);
124 } catch (Message.Malformed e) { throw new MailException(e.getMessage()); }
126 public boolean next() {
128 if (cur >= names.length) return false;
129 String name = names[cur].substring(names[cur].indexOf('.') + 1);
130 if (!(new File(path + File.separatorChar + names[cur])).exists()) return next();
131 seen = name.indexOf('s') != -1;
132 deleted = name.indexOf('x') != -1;
133 flagged = name.indexOf('f') != -1;
134 draft = name.indexOf('d') != -1;
135 answered = name.indexOf('a') != -1;
136 recent = name.indexOf('r') != -1;
139 public int num() { return cur; }
141 try { return Integer.parseInt(names[cur].substring(0, names[cur].indexOf('.')));
142 } catch (NumberFormatException nfe) {
143 Log.warn(FileBasedMailbox.class, "NumberFormatException: " + names[cur].substring(0, names[cur].length() - 1));
146 private void fixflags() {
148 names[cur].substring(0, names[cur].indexOf('.') + 1) +
150 (deleted ? "x" : "") +
151 (flagged ? "f" : "") +
153 (recent ? "r" : "") +
154 (answered ? "a" : "");
155 new File(path + File.separatorChar + names[cur]).renameTo(new File(path + File.separatorChar + newName));
156 names[cur] = newName;
159 public void delete() {
160 new File(path + File.separatorChar + names[cur]).delete();
161 // FIXME remove from list?
163 public void set(String key, String val) { throw new MailException("not supported"); }
164 public String get(String key) { throw new MailException("not supported"); }
166 public boolean seen() { return seen; }
167 public boolean deleted() { return deleted; }
168 public boolean flagged() { return flagged; }
169 public boolean draft() { return draft; }
170 public boolean answered() { return answered; }
171 public boolean recent() { return recent; }
172 public void seen(boolean on) { seen = on; fixflags(); }
173 public void deleted(boolean on) { deleted = on; fixflags(); }
174 public void flagged(boolean on) { flagged = on; fixflags(); }
175 public void draft(boolean on) { draft = on; fixflags(); }
176 public void answered(boolean on) { answered = on; fixflags(); }
177 public void recent(boolean on) { recent = on; fixflags(); }