1 package org.ibex.mail.target;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.*;
10 /** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
11 public class FileBasedMailbox extends Mailbox.Default {
13 private static final char slash = File.separatorChar;
14 private static final Hashtable instances = new Hashtable();
15 public static FileBasedMailbox getFileBasedMailbox(String path, boolean create) {
16 FileBasedMailbox ret = (FileBasedMailbox)instances.get(path);
17 if (ret != null) return ret;
18 File f = new File(path);
19 if (!create && !f.exists()) return null;
20 instances.put(path, ret = new FileBasedMailbox(path));
24 public static final FilenameFilter filter = new FilenameFilter() {
25 public boolean accept(File f, String s) {
26 return s.indexOf('.') != -1;
30 // Instance //////////////////////////////////////////////////////////////////////////////
34 private FileBasedMailbox(String path) throws MailException {
35 new File(this.path = path).mkdirs();
39 public Mailbox slash(String name, boolean create) {
40 return FileBasedMailbox.getFileBasedMailbox(path + slash + name, create); }
42 public String[] children() {
44 String[] list = new File(path).list();
45 for(int i=0; i<list.length; i++) {
46 if (!(new File(path + slash + list[i]).isDirectory())) continue;
47 vec.addElement(list[i]);
49 String[] ret = new String[vec.size()];
54 public Mailbox.Iterator iterator() { return new FileBasedMailbox.Iterator(); }
55 public int uidValidity() { return (int)(new File(path).lastModified() & 0xffffffL); }
57 public int uidNext() { return uidNext(false); }
58 public int uidNext(boolean inc) {
60 uidNext = new File(path + slash + "UIDNEXT");
61 if (!uidNext.exists()) {
62 File tmp = new File(uidNext + "-");
63 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
67 tmp.renameTo(uidNext);
70 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(uidNext)));
71 int ret = Integer.parseInt(br.readLine());
73 File tmp = new File(uidNext + "-");
74 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
78 tmp.renameTo(uidNext);
81 } catch (IOException e) { throw new MailException.IOException(e); }
84 public synchronized void add(Message message) { add(message, Mailbox.Flag.RECENT); }
85 public synchronized void add(Message message, int flags) {
88 int num = new File(path).list(filter).length;
89 File target = new File(path + slash + uidNext(true) + ".");
90 File f = new File(target.getCanonicalPath() + "-");
91 FileOutputStream fo = new FileOutputStream(f);
95 } catch (IOException e) { throw new MailException.IOException(e); }
98 private class Iterator implements Mailbox.Default.Iterator {
100 private String[] names;
101 private boolean seen = false, deleted = false, draft = false, flagged = false, answered = false, recent = false;
102 public Iterator() { names = new File(path).list(filter); }
104 public Message cur() {
106 File file = new File(path + File.separatorChar + names[cur]);
107 return new Message(/* FIXME */ null, /* FIXME */ new Address[] { },
108 new LineReader(new InputStreamReader(new FileInputStream(file))));
109 } catch (IOException e) { throw new MailException.IOException(e); }
111 public boolean next() {
113 if (cur >= names.length) return false;
114 String name = names[cur].substring(names[cur].indexOf('.') + 1);
115 seen = name.indexOf('s') != -1;
116 deleted = name.indexOf('x') != -1;
117 flagged = name.indexOf('f') != -1;
118 draft = name.indexOf('d') != -1;
119 answered = name.indexOf('a') != -1;
120 recent = name.indexOf('r') != -1;
123 public int num() { return cur; }
125 try { return Integer.parseInt(names[cur].substring(0, names[cur].indexOf('.')));
126 } catch (NumberFormatException nfe) {
127 Log.warn(FileBasedMailbox.class, "NumberFormatException: " + names[cur].substring(0, names[cur].length() - 1));
130 private void fixflags() {
132 names[cur].substring(0, names[cur].indexOf('.') + 1) +
134 (deleted ? "x" : "") +
135 (flagged ? "f" : "") +
137 (recent ? "r" : "") +
138 (answered ? "a" : "");
139 new File(names[cur]).renameTo(new File(path + File.separatorChar + newName));
140 names[cur] = newName;
143 public void delete() { new File(names[cur]).delete(); }
144 public void set(String key, String val) { throw new MailException("not supported"); }
145 public String get(String key) { throw new MailException("not supported"); }
147 public boolean seen() { return seen; }
148 public boolean deleted() { return deleted; }
149 public boolean flagged() { return flagged; }
150 public boolean draft() { return draft; }
151 public boolean answered() { return answered; }
152 public boolean recent() { return recent; }
153 public void seen(boolean on) { seen = on; fixflags(); }
154 public void deleted(boolean on) { deleted = on; fixflags(); }
155 public void flagged(boolean on) { flagged = on; fixflags(); }
156 public void draft(boolean on) { draft = on; fixflags(); }
157 public void answered(boolean on) { answered = on; fixflags(); }
158 public void recent(boolean on) { recent = on; fixflags(); }