package org.ibex.mail.target;
import org.ibex.mail.*;
import org.ibex.util.*;
-import org.ibex.mail.*;
+import org.ibex.io.*;
import java.io.*;
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
+
/** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
public class FileBasedMailbox extends Mailbox.Default {
+ public String toString() { return "[FileBasedMailbox " + path + "]"; }
+
private static final char slash = File.separatorChar;
private static final Hashtable instances = new Hashtable();
public static FileBasedMailbox getFileBasedMailbox(String path, boolean create) {
}
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(uidNext)));
int ret = Integer.parseInt(br.readLine());
+ br.close();
if (inc) {
File tmp = new File(uidNext + "-");
PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
} catch (IOException e) { throw new MailException.IOException(e); }
}
- public synchronized int add(Message message) {
+ public synchronized void add(Message message) { add(message, Mailbox.Flag.RECENT); }
+ public synchronized void add(Message message, int flags) {
+ Log.info(this, "adding message to ["+toString()+"]:\n" + message.summary());
try {
int num = new File(path).list(filter).length;
- File target = new File(path + slash + uidNext(true) + ".");
+ String name = path + slash + uidNext(true) + "." +
+ ((flags & Mailbox.Flag.DELETED) == Mailbox.Flag.DELETED ? "x" : "") +
+ ((flags & Mailbox.Flag.DRAFT) == Mailbox.Flag.DRAFT ? "d" : "") +
+ ((flags & Mailbox.Flag.RECENT) == Mailbox.Flag.RECENT ? "r" : "") +
+ ((flags & Mailbox.Flag.ANSWERED) == Mailbox.Flag.ANSWERED ? "a" : "") +
+ ((flags & Mailbox.Flag.FLAGGED) == Mailbox.Flag.FLAGGED ? "f" : "") +
+ ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN ? "s" : "");
+ Log.info(this, " with chosen filename " + name);
+ File target = new File(name);
File f = new File(target.getCanonicalPath() + "-");
FileOutputStream fo = new FileOutputStream(f);
- message.dump(fo);
+ message.dump(new Stream(fo));
fo.close();
f.renameTo(target);
- return num;
+ Log.info(this, " done writing.");
} catch (IOException e) { throw new MailException.IOException(e); }
}
- private class Iterator implements Mailbox.Default.Iterator {
+ private class Iterator extends Mailbox.Default.Iterator {
int cur = -1;
private String[] names;
private boolean seen = false, deleted = false, draft = false, flagged = false, answered = false, recent = false;
public Iterator() { names = new File(path).list(filter); }
+ public Message head() { return cur(); }
public Message cur() {
+ if (cur >= names.length) return null;
try {
File file = new File(path + File.separatorChar + names[cur]);
- return new Message(/* FIXME */ null, /* FIXME */ new Address[] { },
- new LineReader(new InputStreamReader(new FileInputStream(file))));
- } catch (IOException e) { throw new MailException.IOException(e); }
+ FileInputStream fis = new FileInputStream(file);
+ Message ret = new Message(null, null, new Stream(fis));
+ 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 ? "d" : "") +
(recent ? "r" : "") +
(answered ? "a" : "");
- new File(names[cur]).renameTo(new File(path + File.separatorChar + newName));
+ new File(path + File.separatorChar + names[cur]).renameTo(new File(path + File.separatorChar + newName));
names[cur] = newName;
}
- public void delete() { new File(names[cur]).delete(); }
+ 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"); }