1 package org.ibex.mail.target;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.*;
10 // FIXME: uids and messagenums are all messed up
12 public class FileBasedMailbox extends Mailbox {
15 public FileBasedMailbox(String path) throws MailException { new File(this.path = path).mkdirs(); }
16 public Mailbox slash(String name, boolean create) throws MailException { return new FileBasedMailbox(path + "/" + name); }
19 public String getName() { return "FOO"; }
21 protected synchronized void flags(Message m, int newFlags) throws MailException {
23 File f = new File(messageToFile(m).getCanonicalPath() + ".flags");
24 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f)));
25 if ((newFlags & Flag.DELETED) != 0) pw.print("\\Deleted ");
26 if ((newFlags & Flag.SEEN) != 0) pw.print("\\Seen ");
27 if ((newFlags & Flag.FLAGGED) != 0) pw.print("\\Flagged ");
28 if ((newFlags & Flag.DRAFT) != 0) pw.print("\\Draft ");
29 if ((newFlags & Flag.ANSWERED) != 0) pw.print("\\Answered ");
30 if ((newFlags & Flag.RECENT) != 0) pw.print("\\Recent ");
32 } catch (IOException e) { throw new MailException.IOException(e); }
35 protected synchronized int flags(Message m) {
37 File f = new File(messageToFile(m).getCanonicalPath() + ".flags");
38 if (!f.exists()) return 0;
39 String s = new BufferedReader(new InputStreamReader(new FileInputStream(f))).readLine();
40 StringTokenizer st = new StringTokenizer(s);
42 while(st.hasMoreTokens()) {
43 String s2 = st.nextToken();
44 if (s2.equals("\\Deleted")) ret |= Flag.DELETED;
45 if (s2.equals("\\Seen")) ret |= Flag.SEEN;
46 if (s2.equals("\\Flagged")) ret |= Flag.FLAGGED;
47 if (s2.equals("\\Draft")) ret |= Flag.DRAFT;
48 if (s2.equals("\\Answered")) ret |= Flag.ANSWERED;
49 if (s2.equals("\\Recent")) ret |= Flag.RECENT;
52 } catch (IOException e) { throw new MailException.IOException(e); }
55 private int uidNext = 0;
56 public int uidNext() { return uidNext; }
57 public int uidValidity() { return 1; }
58 public int uid(Message m) { Integer i = (Integer)uidMap.get(m); if (i == null) return -1; return i.intValue(); }
59 public int num(Message m) { Integer i = (Integer)numMap.get(m); if (i == null) return -1; return i.intValue(); }
60 private File messageToFile(Message message) { return new File(path + File.separatorChar + num(message) + "."); }
62 public int delete(Message message) throws MailException {
63 messageToFile(message).delete();
64 uidMap.remove(message);
65 numMap.remove(message);
69 public Mailbox.Iterator iterator() { return new FileBasedMailbox.Iterator(); }
70 private class Iterator extends Mailbox.Iterator {
71 private String[] names;
72 public Iterator() { names = new File(path).list(); }
73 public Message cur() { return null; }
74 public boolean next() { return false; }
75 protected int flags() { return 0; }
76 protected void flags(int newFlags) { }
77 public int uid() { return -1; }
78 public int num() { return -1; }
79 public void delete() { }
83 //= Integer.parseInt(names[i].substring(0, names[i].length() - 1));
85 } catch (NumberFormatException nfe) {
86 Log.warn(FileBasedMailbox.class, "NumberFormatException: " + names[i].substring(0, names[i].length() - 1));
88 int[] newret = new int[ret.length - 1];
89 System.arraycopy(ret, 0, newret, 0, newret.length);
96 private Hashtable uidMap = new Hashtable();
97 private Hashtable numMap = new Hashtable();
98 private int numNext = 0;
100 public synchronized int add(Message message) throws MailException {
102 File target = messageToFile(message);
103 File f = new File(target.getCanonicalPath() + ".-");
104 FileOutputStream fo = new FileOutputStream(f);
108 uidMap.put(new Integer(uidNext++), message);
109 numMap.put(new Integer(numNext++), message);
111 } catch (IOException e) { throw new MailException.IOException(e); }