got IMAP compiling
[org.ibex.mail.git] / src / org / ibex / mail / target / Mailbox.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: appallingly inefficient
11 public class Mailbox extends Target {
12
13     private static final String STORAGE_ROOT =
14         System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
15     public static FileBased root = null;
16     public static Transcript transcript = null;
17     static {
18         try {
19             root = new FileBased(STORAGE_ROOT + File.separatorChar);
20             transcript = new Transcript(STORAGE_ROOT + File.separatorChar + "transcript");
21         } catch (Exception e) {
22             e.printStackTrace();
23         }
24     }
25
26     public int uidvalidity;
27     public int uidnext;
28
29     public void moveAllMessagesTo(Mailbox m) throws MailException { throw new Error("moveAllMessagesTo() not implemented"); }
30     public String getName() throws MailException { throw new Error("Mailbox.getName() not implemented"); }
31     public void rename(String newName) throws MailException { throw new Error("Mailbox.rename() not implemented"); }
32     public void destroy() throws MailException { throw new Error("Mailbox.destroy() not implemented"); }
33     public void accept(Message m) throws MailException { add(m); }
34     public Mailbox slash(String name) throws MailException { return slash(name, false); }
35     public Mailbox slash(String name, boolean create) throws MailException {
36         throw new Error(this.getClass().getName() + " does not support the slash() method"); }
37     public int[] list() { throw new Error(this.getClass().getName() + " does not support the list() method"); }
38     public int add(Message message) throws MailException {
39         throw new Error(this.getClass().getName() + " does not support the add() method"); }
40     public int delete(Message message) throws MailException {
41         throw new Error(this.getClass().getName() + " does not support the delete() method"); }
42     public Message get(int messagenum) throws MailException {
43         throw new Error(this.getClass().getName() + " does not support the get() method"); }
44     public Message[] query(int maxResults) {
45         throw new Error(this.getClass().getName() + " does not support the query() method"); }
46     /*
47     public static class Mailbox extends Mailbox {
48         String user;
49         private static Hashtable cache = new Hashtable();
50         public static Mailbox getForUser(String user) {
51             Mailbox ret = (Mailbox)cache.get(user);
52             if (ret == null) ret = new Mailbox(user);
53             return ret;
54         }
55         Mailbox(String user) { this.user = user; }
56         public Mailbox slash(String name) throws MailException {
57             throw new Error(this.getClass().getName() + " does not support the slash() method"); }
58         public synchronized int add(Message message) throws MailException {
59             FileOutputStream fos = new FileOutputStream("/var/mail/" + user, true);
60             PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
61             pw.println("From " + message.envelopeFrom);
62             pw.flush();
63             message.dump(fos);
64             fos.close();
65             return -1;
66         }
67     }
68     */
69     /** a fast-write, slow-read place to stash all messages we touch -- in case of a major f*ckup */
70     public static class Transcript extends Mailbox {
71         private String path;
72         public Transcript(String path) throws MailException { new File(this.path = path).mkdirs(); }
73         private static String lastTime = null;
74         private static int lastCounter = 0;
75
76         /** returns a message identifier */
77         public synchronized int add(Message message) throws MailException {
78             try {
79                 File today = new File(path + File.separatorChar + (new SimpleDateFormat("yy-MMM-dd").format(new Date())));
80                 today.mkdirs();
81                 
82                 String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
83                 synchronized (Transcript.class) {
84                     if (lastTime != null && lastTime.equals(time)) {
85                         time += "." + (++lastCounter);
86                     } else {
87                         lastTime = time;
88                     }
89                 }
90                 
91                 File target = new File(today.getPath() + File.separatorChar + time + ".txt");
92                 OutputStream os = new FileOutputStream(target);
93                 message.dump(os);
94                 os.close();
95                 return -1;
96             } catch (IOException e) { throw new MailException.IOException(e); }
97         }
98     }
99
100     public static class FileBased extends Mailbox {
101         private String path;
102         private FileBased(String path) throws MailException { new File(this.path = path).mkdirs(); }
103         public Mailbox slash(String name, boolean create) throws MailException {
104             // FIXME: create
105             return new FileBased(path + "/" + name);
106         }
107
108         public int[] list() {
109             String[] names = new File(path).list();
110             int[] ret = new int[names.length];
111             for(int i=0, j=0; j<ret.length; i++, j++) {
112                 try {
113                     ret[j] = Integer.parseInt(names[i].substring(0, names[i].length() - 1));
114                 } catch (NumberFormatException nfe) {
115                     Log.warn(FileBased.class, "NumberFormatException: " + names[i].substring(0, names[i].length() - 1));
116                     j--;
117                     int[] newret = new int[ret.length - 1];
118                     System.arraycopy(ret, 0, newret, 0, newret.length);
119                     ret = newret;
120                 }
121             }
122             return ret;
123         }
124
125         /** returns a message identifier */
126         public synchronized int add(Message message) throws MailException {
127             try {
128                 int[] all = list();
129                 int max = 0;
130                 for(int i=0; i<all.length; i++) max = Math.max(max, all[i]);
131                 int target = max++;
132                 File f = new File(path + File.separatorChar + max + ".-");
133                 FileOutputStream fo = new FileOutputStream(f);
134                 message.dump(fo);
135                 fo.close();
136                 f.renameTo(new File(path + File.separatorChar + max + "."));
137                 return target;
138             } catch (IOException e) { throw new MailException.IOException(e); }
139         }
140
141         public Message get(int messagenum) throws MailException {
142             File f = new File(path + File.separatorChar + messagenum + ".");        
143             if (!f.exists()) throw new MailException.IOException(new FileNotFoundException(f.toString()));
144             //try {
145                 // FIXME: need to store envelope from/to
146                 //Message ret = new Message(new LineReader(new InputStreamReader(new FileInputStream(f))));
147                 // FIXME: set answered/read/etc here
148                 //return ret;
149             //return null;
150                 /*
151             } catch (MailException.Malformed malf) {
152                 Log.error(this, "This should never happen");
153                 Log.error(this, malf);
154                 return null;
155             }
156                 */
157             return null;
158         }
159
160         // query types: stringmatch (headers, body), header element, deletion status, date range, message size
161         public Message[] query(int maxResults) {
162             throw new RuntimeException("FileBased.query() not implemented yet");
163         }
164
165     }
166
167 }