it compiles
[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: uids and messagenums are all messed up
11
12 public class FileBasedMailbox extends Mailbox {
13
14     private String path;
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); }
17
18     // FIXME
19     public String getName() { return "FOO"; }
20     
21     protected synchronized void flags(Message m, int newFlags) throws MailException {
22         try {
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 ");
31             pw.close();
32         } catch (IOException e) { throw new MailException.IOException(e); }
33     }
34
35     protected synchronized int flags(Message m) {
36         try {
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);
41             int ret = 0;
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;
50             }
51             return ret;
52         } catch (IOException e) { throw new MailException.IOException(e); }
53     }
54
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) + "."); }
61
62     public int delete(Message message) throws MailException {
63         messageToFile(message).delete();
64         uidMap.remove(message);
65         numMap.remove(message);
66         return -1;
67     }
68
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() { }
80
81         /*
82             try {
83                 //= Integer.parseInt(names[i].substring(0, names[i].length() - 1));
84                 // FIXME
85             } catch (NumberFormatException nfe) {
86                 Log.warn(FileBasedMailbox.class, "NumberFormatException: " + names[i].substring(0, names[i].length() - 1));
87                 j--;
88                 int[] newret = new int[ret.length - 1];
89                 System.arraycopy(ret, 0, newret, 0, newret.length);
90                 ret = newret;
91             }
92         }
93         */
94     }
95
96     private Hashtable uidMap = new Hashtable();
97     private Hashtable numMap = new Hashtable();
98     private int numNext = 0;
99
100     public synchronized int add(Message message) throws MailException {
101         try {
102             File target = messageToFile(message);
103             File f = new File(target.getCanonicalPath() + ".-");
104             FileOutputStream fo = new FileOutputStream(f);
105             message.dump(fo);
106             fo.close();
107             f.renameTo(target);
108             uidMap.put(new Integer(uidNext++), message);
109             numMap.put(new Integer(numNext++), message);
110             return numNext - 1;
111         } catch (IOException e) { throw new MailException.IOException(e); }
112     }
113
114 }