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 private static final char slash = File.separatorChar;
18 private static final Hashtable instances = new Hashtable();
19 public static FileBasedMailbox getFileBasedMailbox(String path, boolean create) {
20 FileBasedMailbox ret = (FileBasedMailbox)instances.get(path);
21 if (ret != null) return ret;
22 File f = new File(path);
23 if (!create && !f.exists()) return null;
24 instances.put(path, ret = new FileBasedMailbox(path));
28 public static final FilenameFilter filter = new FilenameFilter() {
29 public boolean accept(File f, String s) {
30 return s.indexOf('.') != -1;
34 // Instance //////////////////////////////////////////////////////////////////////////////
38 private FileBasedMailbox(String path) throws MailException {
39 new File(this.path = path).mkdirs();
43 public Mailbox slash(String name, boolean create) {
44 return FileBasedMailbox.getFileBasedMailbox(path + slash + name, create); }
46 public String[] children() {
48 String[] list = new File(path).list();
49 for(int i=0; i<list.length; i++) {
50 if (!(new File(path + slash + list[i]).isDirectory())) continue;
51 vec.addElement(list[i]);
53 String[] ret = new String[vec.size()];
58 public Mailbox.Iterator iterator() { return new FileBasedMailbox.Iterator(); }
59 public int uidValidity() { return (int)(new File(path).lastModified() & 0xffffffL); }
61 public int uidNext() { return uidNext(false); }
62 public int uidNext(boolean inc) {
64 uidNext = new File(path + slash + "UIDNEXT");
65 if (!uidNext.exists()) {
66 File tmp = new File(uidNext + "-");
67 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
71 tmp.renameTo(uidNext);
74 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(uidNext)));
75 int ret = Integer.parseInt(br.readLine());
77 File tmp = new File(uidNext + "-");
78 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
82 tmp.renameTo(uidNext);
85 } catch (IOException e) { throw new MailException.IOException(e); }
88 public synchronized void add(Message message) { add(message, Mailbox.Flag.RECENT); }
89 public synchronized void add(Message message, int flags) {
91 int num = new File(path).list(filter).length;
92 String name = path + slash + uidNext(true) + "." +
93 ((flags & Mailbox.Flag.DELETED) == Mailbox.Flag.DELETED ? "x" : "") +
94 ((flags & Mailbox.Flag.DRAFT) == Mailbox.Flag.DRAFT ? "d" : "") +
95 ((flags & Mailbox.Flag.RECENT) == Mailbox.Flag.RECENT ? "r" : "") +
96 ((flags & Mailbox.Flag.ANSWERED) == Mailbox.Flag.ANSWERED ? "a" : "") +
97 ((flags & Mailbox.Flag.FLAGGED) == Mailbox.Flag.FLAGGED ? "f" : "") +
98 ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN ? "s" : "");
99 File target = new File(name);
100 File f = new File(target.getCanonicalPath() + "-");
101 FileOutputStream fo = new FileOutputStream(f);
102 message.dump(new Stream(fo));
105 } catch (IOException e) { throw new MailException.IOException(e); }
108 private class Iterator extends Mailbox.Default.Iterator {
110 private String[] names;
111 private boolean seen = false, deleted = false, draft = false, flagged = false, answered = false, recent = false;
112 public Iterator() { names = new File(path).list(filter); }
114 public Message cur() {
115 if (cur >= names.length) return null;
117 File file = new File(path + File.separatorChar + names[cur]);
118 return new Message(null, null, new Stream(new FileInputStream(file)));
119 } catch (IOException e) { throw new MailException.IOException(e); }
121 public boolean next() {
123 if (cur >= names.length) return false;
124 String name = names[cur].substring(names[cur].indexOf('.') + 1);
125 if (!(new File(path + File.separatorChar + names[cur])).exists()) return next();
126 seen = name.indexOf('s') != -1;
127 deleted = name.indexOf('x') != -1;
128 flagged = name.indexOf('f') != -1;
129 draft = name.indexOf('d') != -1;
130 answered = name.indexOf('a') != -1;
131 recent = name.indexOf('r') != -1;
134 public int num() { return cur; }
136 try { return Integer.parseInt(names[cur].substring(0, names[cur].indexOf('.')));
137 } catch (NumberFormatException nfe) {
138 Log.warn(FileBasedMailbox.class, "NumberFormatException: " + names[cur].substring(0, names[cur].length() - 1));
141 private void fixflags() {
143 names[cur].substring(0, names[cur].indexOf('.') + 1) +
145 (deleted ? "x" : "") +
146 (flagged ? "f" : "") +
148 (recent ? "r" : "") +
149 (answered ? "a" : "");
150 new File(path + File.separatorChar + names[cur]).renameTo(new File(path + File.separatorChar + newName));
151 names[cur] = newName;
154 public void delete() {
155 new File(path + File.separatorChar + names[cur]).delete();
156 // FIXME remove from list?
158 public void set(String key, String val) { throw new MailException("not supported"); }
159 public String get(String key) { throw new MailException("not supported"); }
161 public boolean seen() { return seen; }
162 public boolean deleted() { return deleted; }
163 public boolean flagged() { return flagged; }
164 public boolean draft() { return draft; }
165 public boolean answered() { return answered; }
166 public boolean recent() { return recent; }
167 public void seen(boolean on) { seen = on; fixflags(); }
168 public void deleted(boolean on) { deleted = on; fixflags(); }
169 public void flagged(boolean on) { flagged = on; fixflags(); }
170 public void draft(boolean on) { draft = on; fixflags(); }
171 public void answered(boolean on) { answered = on; fixflags(); }
172 public void recent(boolean on) { recent = on; fixflags(); }