1 package org.ibex.mail.target;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
10 // FIXME: we can omit UIDNEXT!
11 // FIXME use directory date/time as UIDNEXT and file date/time as UID; need to 'correct' file date/time after changes
13 /** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
14 public class FileBasedMailbox extends Mailbox.Default {
16 public String toString() { return "[FileBasedMailbox " + path + "]"; }
18 private static final char slash = File.separatorChar;
19 private static final Hashtable instances = new Hashtable();
20 public static FileBasedMailbox getFileBasedMailbox(String path, boolean create) {
21 FileBasedMailbox ret = (FileBasedMailbox)instances.get(path);
22 if (ret != null) return ret;
23 File f = new File(path);
24 if (!create && !f.exists()) return null;
25 instances.put(path, ret = new FileBasedMailbox(path));
29 public static final FilenameFilter filter = new FilenameFilter() {
30 public boolean accept(File f, String s) {
31 return s.indexOf('.') != -1;
35 // Instance //////////////////////////////////////////////////////////////////////////////
39 private FileBasedMailbox(String path) throws MailException {
40 new File(this.path = path).mkdirs();
44 public Mailbox slash(String name, boolean create) {
45 return FileBasedMailbox.getFileBasedMailbox(path + slash + name, create); }
47 public String[] children() {
49 String[] list = new File(path).list();
50 for(int i=0; i<list.length; i++) {
51 if (!(new File(path + slash + list[i]).isDirectory())) continue;
52 vec.addElement(list[i]);
54 String[] ret = new String[vec.size()];
59 public Mailbox.Iterator iterator() { return new FileBasedMailbox.Iterator(); }
60 public int uidValidity() { return (int)(new File(path).lastModified() & 0xffffffL); }
62 public int uidNext() { return uidNext(false); }
63 public int uidNext(boolean inc) {
65 uidNext = new File(path + slash + "UIDNEXT");
66 if (!uidNext.exists()) {
67 File tmp = new File(uidNext + "-");
68 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
72 tmp.renameTo(uidNext);
75 FileInputStream fis = null;
78 fis = new FileInputStream(uidNext);
79 BufferedReader br = new BufferedReader(new InputStreamReader(fis));
80 ret = Integer.parseInt(br.readLine());
82 File tmp = new File(uidNext + "-");
83 FileOutputStream fos = null;
85 fos = new FileOutputStream(tmp);
86 PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
89 } finally { if (fos != null) fos.close(); }
90 tmp.renameTo(uidNext);
92 } finally { if (fis != null) fis.close(); }
94 } catch (IOException e) { throw new MailException.IOException(e); }
97 public synchronized void add(Message message) { add(message, Mailbox.Flag.RECENT); }
98 public synchronized void add(Message message, int flags) {
99 Log.info(path, message.summary());
101 int num = new File(path).list(filter).length;
102 String name = path + slash + uidNext(true) + "." +
103 ((flags & Mailbox.Flag.DELETED) == Mailbox.Flag.DELETED ? "x" : "") +
104 ((flags & Mailbox.Flag.DRAFT) == Mailbox.Flag.DRAFT ? "d" : "") +
105 ((flags & Mailbox.Flag.RECENT) == Mailbox.Flag.RECENT ? "r" : "") +
106 ((flags & Mailbox.Flag.ANSWERED) == Mailbox.Flag.ANSWERED ? "a" : "") +
107 ((flags & Mailbox.Flag.FLAGGED) == Mailbox.Flag.FLAGGED ? "f" : "") +
108 ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN ? "s" : "");
109 //Log.info(this, " with chosen filename " + name);
110 File target = new File(name);
111 File f = new File(target.getCanonicalPath() + "-");
112 FileOutputStream fo = new FileOutputStream(f);
113 Stream stream = new Stream(fo);
114 stream.println("X-org.ibex.mail.headers.envelope.From: " + message.envelope.from);
115 stream.println("X-org.ibex.mail.headers.envelope.To: " + message.envelope.to);
116 message.dump(stream);
119 //Log.info(this, " done writing.");
120 } catch (IOException e) { throw new MailException.IOException(e); }
123 private class Iterator extends Mailbox.Default.Iterator {
125 private String[] names;
126 private boolean seen = false, deleted = false, draft = false, flagged = false, answered = false, recent = false;
127 public Iterator() { names = new File(path).list(filter); }
129 public Message head() { return cur(); }
130 public Message cur() {
131 if (cur >= names.length) return null;
133 File file = new File(path + File.separatorChar + names[cur]);
134 FileInputStream fis = null;
136 fis = new FileInputStream(file);
137 return new Message(new Stream(fis), new Message.Envelope(null, null, new Date(file.lastModified())));
138 } finally { if (fis != null) fis.close(); }
139 } catch (IOException e) { throw new MailException.IOException(e);
140 } catch (Message.Malformed e) { throw new MailException(e.getMessage()); }
142 public boolean next() {
144 if (cur >= names.length) return false;
145 String name = names[cur].substring(names[cur].indexOf('.') + 1);
146 if (!(new File(path + File.separatorChar + names[cur])).exists()) return next();
147 seen = name.indexOf('s') != -1;
148 deleted = name.indexOf('x') != -1;
149 flagged = name.indexOf('f') != -1;
150 draft = name.indexOf('d') != -1;
151 answered = name.indexOf('a') != -1;
152 recent = name.indexOf('r') != -1;
155 public int num() { return cur; }
157 try { return Integer.parseInt(names[cur].substring(0, names[cur].indexOf('.')));
158 } catch (NumberFormatException nfe) {
159 Log.warn(FileBasedMailbox.class, "NumberFormatException: " + names[cur].substring(0, names[cur].length() - 1));
162 private void fixflags() {
164 names[cur].substring(0, names[cur].indexOf('.') + 1) +
166 (deleted ? "x" : "") +
167 (flagged ? "f" : "") +
169 (recent ? "r" : "") +
170 (answered ? "a" : "");
171 new File(path + File.separatorChar + names[cur]).renameTo(new File(path + File.separatorChar + newName));
172 names[cur] = newName;
175 public void delete() {
176 new File(path + File.separatorChar + names[cur]).delete();
177 // FIXME remove from list?
179 public void set(String key, String val) { throw new MailException("not supported"); }
180 public String get(String key) { throw new MailException("not supported"); }
182 public boolean seen() { return seen; }
183 public boolean deleted() { return deleted; }
184 public boolean flagged() { return flagged; }
185 public boolean draft() { return draft; }
186 public boolean answered() { return answered; }
187 public boolean recent() { return recent; }
188 public void seen(boolean on) { seen = on; fixflags(); }
189 public void deleted(boolean on) { deleted = on; fixflags(); }
190 public void flagged(boolean on) { flagged = on; fixflags(); }
191 public void draft(boolean on) { draft = on; fixflags(); }
192 public void answered(boolean on) { answered = on; fixflags(); }
193 public void recent(boolean on) { recent = on; fixflags(); }