mail overhaul
[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.io.*;
5 import org.ibex.net.*;
6 import java.io.*;
7 import java.net.*;
8 import java.util.*;
9 import java.text.*;
10
11 // FIXME: we can omit UIDNEXT!
12 // FIXME use directory date/time as UIDNEXT and file date/time as UID; need to 'correct' file date/time after changes
13
14 /** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
15 public class FileBasedMailbox extends Mailbox.Default {
16
17     private static final char slash = File.separatorChar;
18     private static final Hashtable instances = new Hashtable();
19     public static FileBasedMailbox getFileBasedMailbox(String path, boolean create) {
20         FileBasedMailbox ret = (FileBasedMailbox)instances.get(path);
21         if (ret != null) return ret;
22         File f = new File(path);
23         if (!create && !f.exists()) return null;
24         instances.put(path, ret = new FileBasedMailbox(path));
25         return ret;
26     }
27
28     public static final FilenameFilter filter = new FilenameFilter() {
29             public boolean accept(File f, String s) {
30                 return s.indexOf('.') != -1;
31             } };
32
33
34     // Instance //////////////////////////////////////////////////////////////////////////////
35
36     private String path;
37     private File uidNext;
38     private FileBasedMailbox(String path) throws MailException {
39         new File(this.path = path).mkdirs();
40         uidNext(false);
41     }
42
43     public Mailbox          slash(String name, boolean create) {
44         return FileBasedMailbox.getFileBasedMailbox(path + slash + name, create); }
45
46     public String[] children() {
47         Vec vec = new Vec();
48         String[] list = new File(path).list();
49         for(int i=0; i<list.length; i++) {
50             if (!(new File(path + slash + list[i]).isDirectory())) continue;
51             vec.addElement(list[i]);
52         }
53         String[] ret = new String[vec.size()];
54         vec.copyInto(ret);
55         return ret;
56     }
57
58     public Mailbox.Iterator iterator() { return new FileBasedMailbox.Iterator(); }
59     public int              uidValidity() { return (int)(new File(path).lastModified() & 0xffffffL); }
60
61     public int uidNext() { return uidNext(false); }
62     public int uidNext(boolean inc) {
63         try {
64             uidNext = new File(path + slash + "UIDNEXT");
65             if (!uidNext.exists()) {
66                 File tmp = new File(uidNext + "-");
67                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
68                 pw.println("1");
69                 pw.flush();
70                 pw.close();
71                 tmp.renameTo(uidNext);
72                 return 1;
73             }
74             BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(uidNext)));
75             int ret = Integer.parseInt(br.readLine());
76             if (inc) {
77                 File tmp = new File(uidNext + "-");
78                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
79                 pw.println(ret+1);
80                 pw.flush();
81                 pw.close();
82                 tmp.renameTo(uidNext);
83             }
84             return ret;
85         } catch (IOException e) { throw new MailException.IOException(e); }
86     }        
87
88     public synchronized void add(Message message) { add(message, Mailbox.Flag.RECENT); }
89     public synchronized void add(Message message, int flags) {
90         try {
91             int num = new File(path).list(filter).length;
92             String name = path + slash + uidNext(true) + "." +
93                 ((flags & Mailbox.Flag.DELETED) == Mailbox.Flag.DELETED ? "x" : "") +
94                 ((flags & Mailbox.Flag.DRAFT) == Mailbox.Flag.DRAFT ? "d" : "") +
95                 ((flags & Mailbox.Flag.RECENT) == Mailbox.Flag.RECENT ? "r" : "") +
96                 ((flags & Mailbox.Flag.ANSWERED) == Mailbox.Flag.ANSWERED ? "a" : "") +
97                 ((flags & Mailbox.Flag.FLAGGED) == Mailbox.Flag.FLAGGED ? "f" : "") +
98                 ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN ? "s" : "");
99             File target = new File(name);
100             File f = new File(target.getCanonicalPath() + "-");
101             FileOutputStream fo = new FileOutputStream(f);
102             message.dump(new Stream(fo));
103             fo.close();
104             f.renameTo(target);
105         } catch (IOException e) { throw new MailException.IOException(e); }
106     }
107
108     private class Iterator extends Mailbox.Default.Iterator {
109         int cur = -1;
110         private String[] names;
111         private boolean seen = false, deleted = false, draft = false, flagged = false, answered = false, recent = false;
112         public Iterator() { names = new File(path).list(filter); }
113
114         public Message cur() {
115             if (cur >= names.length) return null;
116             try {
117                 File file = new File(path + File.separatorChar + names[cur]);
118                 return new Message(null, null, new Stream(new FileInputStream(file)));
119             } catch (IOException e) { throw new MailException.IOException(e); }
120         }
121         public boolean next() {
122             cur++;
123             if (cur >= names.length) return false;
124             String name = names[cur].substring(names[cur].indexOf('.') + 1);
125             if (!(new File(path + File.separatorChar + names[cur])).exists()) return next();
126             seen     = name.indexOf('s') != -1;
127             deleted  = name.indexOf('x') != -1;
128             flagged  = name.indexOf('f') != -1;
129             draft    = name.indexOf('d') != -1;
130             answered = name.indexOf('a') != -1;
131             recent   = name.indexOf('r') != -1;
132             return true;
133         }
134         public int num() { return cur; }
135         public int uid() {
136             try { return Integer.parseInt(names[cur].substring(0, names[cur].indexOf('.')));
137             } catch (NumberFormatException nfe) {
138                 Log.warn(FileBasedMailbox.class, "NumberFormatException: " + names[cur].substring(0, names[cur].length() - 1));
139                 return -1; } }
140
141         private void fixflags() {
142             String newName =
143                 names[cur].substring(0, names[cur].indexOf('.') + 1) +
144                 (seen ? "s" : "") +
145                 (deleted ? "x" : "") +
146                 (flagged ? "f" : "") +
147                 (draft ? "d" : "") +
148                 (recent ? "r" : "") +
149                 (answered ? "a" : "");
150             new File(path + File.separatorChar + names[cur]).renameTo(new File(path + File.separatorChar + newName));
151             names[cur] = newName;
152         }
153
154         public void    delete() {
155             new File(path + File.separatorChar + names[cur]).delete();
156             // FIXME remove from list?
157         }
158         public void    set(String key, String val) { throw new MailException("not supported"); }
159         public String  get(String key) { throw new MailException("not supported"); }
160
161         public boolean seen() { return seen; }
162         public boolean deleted() { return deleted; }
163         public boolean flagged() { return flagged; }
164         public boolean draft() { return draft; }
165         public boolean answered() { return answered; }
166         public boolean recent() { return recent; }
167         public void    seen(boolean on) { seen = on; fixflags(); }
168         public void    deleted(boolean on) { deleted = on; fixflags(); }
169         public void    flagged(boolean on) { flagged = on; fixflags(); }
170         public void    draft(boolean on) { draft = on; fixflags(); }
171         public void    answered(boolean on) { answered = on; fixflags(); }
172         public void    recent(boolean on) { recent = on; fixflags(); }
173
174     }
175 }