1 package org.ibex.mail.target;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.*;
10 // FIXME: appallingly inefficient
11 public class Mailbox extends Target {
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;
19 root = new FileBased(STORAGE_ROOT + File.separatorChar);
20 transcript = new Transcript(STORAGE_ROOT + File.separatorChar + "transcript");
21 } catch (Exception e) {
26 public int uidvalidity;
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"); }
47 public static class Mailbox extends Mailbox {
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);
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);
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 {
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;
76 /** returns a message identifier */
77 public synchronized int add(Message message) throws MailException {
79 File today = new File(path + File.separatorChar + (new SimpleDateFormat("yy-MMM-dd").format(new Date())));
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);
91 File target = new File(today.getPath() + File.separatorChar + time + ".txt");
92 OutputStream os = new FileOutputStream(target);
96 } catch (IOException e) { throw new MailException.IOException(e); }
100 public static class FileBased extends Mailbox {
102 private FileBased(String path) throws MailException { new File(this.path = path).mkdirs(); }
103 public Mailbox slash(String name, boolean create) throws MailException {
105 return new FileBased(path + "/" + name);
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++) {
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));
117 int[] newret = new int[ret.length - 1];
118 System.arraycopy(ret, 0, newret, 0, newret.length);
125 /** returns a message identifier */
126 public synchronized int add(Message message) throws MailException {
130 for(int i=0; i<all.length; i++) max = Math.max(max, all[i]);
132 File f = new File(path + File.separatorChar + max + ".-");
133 FileOutputStream fo = new FileOutputStream(f);
136 f.renameTo(new File(path + File.separatorChar + max + "."));
138 } catch (IOException e) { throw new MailException.IOException(e); }
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()));
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
151 } catch (MailException.Malformed malf) {
152 Log.error(this, "This should never happen");
153 Log.error(this, malf);
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");