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