LIST sorta working but broken
[org.ibex.mail.git] / src / org / ibex / mail / target / FileBasedMailbox.java
1 package org.ibex.mail.target;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.*;
5 import java.io.*;
6 import java.net.*;
7 import java.util.*;
8 import java.text.*;
9
10 /** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
11 public class FileBasedMailbox extends Mailbox.Default {
12
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));
21         return ret;
22     }
23
24     public static final FilenameFilter filter = new FilenameFilter() {
25             public boolean accept(File f, String s) {
26                 return s.indexOf('.') != -1;
27             } };
28
29
30     // Instance //////////////////////////////////////////////////////////////////////////////
31
32     private String path;
33     private File uidNext;
34     private FileBasedMailbox(String path) throws MailException {
35         new File(this.path = path).mkdirs();
36         uidNext(false);
37     }
38
39     public Mailbox          slash(String name, boolean create) {
40         return FileBasedMailbox.getFileBasedMailbox(path + slash + name, create); }
41
42     public String[] children() {
43         Vec vec = new Vec();
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]);
48         }
49         String[] ret = new String[vec.size()];
50         vec.copyInto(ret);
51         return ret;
52     }
53
54     public Mailbox.Iterator iterator() { return new FileBasedMailbox.Iterator(); }
55     public int              uidValidity() { return (int)(new File(path).lastModified() & 0xffffffL); }
56
57     public int uidNext() { return uidNext(false); }
58     public int uidNext(boolean inc) {
59         try {
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)));
64                 pw.println("1");
65                 pw.flush();
66                 pw.close();
67                 tmp.renameTo(uidNext);
68                 return 1;
69             }
70             BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(uidNext)));
71             int ret = Integer.parseInt(br.readLine());
72             if (inc) {
73                 File tmp = new File(uidNext + "-");
74                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
75                 pw.println(ret+1);
76                 pw.flush();
77                 pw.close();
78                 tmp.renameTo(uidNext);
79             }
80             return ret;
81         } catch (IOException e) { throw new MailException.IOException(e); }
82     }        
83
84     public synchronized int add(Message message) {
85         try {
86             int num = new File(path).list(filter).length;
87             File target = new File(path + slash + uidNext(true) + ".");
88             File f = new File(target.getCanonicalPath() + "-");
89             FileOutputStream fo = new FileOutputStream(f);
90             message.dump(fo);
91             fo.close();
92             f.renameTo(target);
93             return num;
94         } catch (IOException e) { throw new MailException.IOException(e); }
95     }
96
97     private class Iterator implements Mailbox.Default.Iterator {
98         int cur = -1;
99         private String[] names;
100         private boolean seen = false, deleted = false, draft = false, flagged = false, answered = false, recent = false;
101         public Iterator() { names = new File(path).list(filter); }
102
103         public Message cur() {
104             try {
105                 File file = new File(path + File.separatorChar + names[cur]);
106                 return new Message(/* FIXME */ null, /* FIXME */ new Address[] { },
107                                    new LineReader(new InputStreamReader(new FileInputStream(file))));
108             } catch (IOException e) { throw new MailException.IOException(e); }
109         }
110         public boolean next() {
111             cur++;
112             if (cur >= names.length) return false;
113             String name = names[cur].substring(names[cur].indexOf('.') + 1);
114             seen     = name.indexOf('s') != -1;
115             deleted  = name.indexOf('x') != -1;
116             flagged  = name.indexOf('f') != -1;
117             draft    = name.indexOf('d') != -1;
118             answered = name.indexOf('a') != -1;
119             recent   = name.indexOf('r') != -1;
120             return true;
121         }
122         public int num() { return cur; }
123         public int uid() {
124             try { return Integer.parseInt(names[cur].substring(0, names[cur].indexOf('.')));
125             } catch (NumberFormatException nfe) {
126                 Log.warn(FileBasedMailbox.class, "NumberFormatException: " + names[cur].substring(0, names[cur].length() - 1));
127                 return -1; } }
128
129         private void fixflags() {
130             String newName =
131                 names[cur].substring(0, names[cur].indexOf('.') + 1) +
132                 (seen ? "s" : "") +
133                 (deleted ? "x" : "") +
134                 (flagged ? "f" : "") +
135                 (draft ? "d" : "") +
136                 (recent ? "r" : "") +
137                 (answered ? "a" : "");
138             new File(names[cur]).renameTo(new File(path + File.separatorChar + newName));
139             names[cur] = newName;
140         }
141
142         public void    delete() { new File(names[cur]).delete(); }
143         public void    set(String key, String val) { throw new MailException("not supported"); }
144         public String  get(String key) { throw new MailException("not supported"); }
145
146         public boolean seen() { return seen; }
147         public boolean deleted() { return deleted; }
148         public boolean flagged() { return flagged; }
149         public boolean draft() { return draft; }
150         public boolean answered() { return answered; }
151         public boolean recent() { return recent; }
152         public void    seen(boolean on) { seen = on; fixflags(); }
153         public void    deleted(boolean on) { deleted = on; fixflags(); }
154         public void    flagged(boolean on) { flagged = on; fixflags(); }
155         public void    draft(boolean on) { draft = on; fixflags(); }
156         public void    answered(boolean on) { answered = on; fixflags(); }
157         public void    recent(boolean on) { recent = on; fixflags(); }
158
159     }
160 }