Mailbox -> MailboxTree separation
[org.ibex.mail.git] / src / org / ibex / mail / Mailbox.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.mail;
6 import org.ibex.mail.*;
7 import org.ibex.util.*;
8 import org.ibex.mail.*;
9 import org.ibex.js.*;
10 import java.io.*;
11 import java.net.*;
12 import java.util.*;
13 import java.text.*;
14
15 /** abstract superclass for mailboxes, which store messages along with their flags */
16 public abstract class Mailbox extends MailboxTree implements Target {
17
18     public JS get(JS key) throws JSExn {
19         return slash(JSU.toString(key), true);
20     }
21
22     public static final String STORAGE_ROOT =
23         System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
24
25
26     // Required Methods //////////////////////////////////////////////////////////////////////////////
27
28     /** phantom mailboxes merely contain others; like the alt.binaries in alt.binaries.warez */
29     public abstract boolean          phantom();
30     public abstract Mailbox.Iterator iterator(Query q);
31
32     public abstract void             insert(Message message, int flags);
33     public abstract void             post(Message message);
34     public abstract void             move(Query q, Mailbox dest);
35     public abstract void             copy(Query q, Mailbox dest);
36     public abstract int              count(Query q);
37     public abstract int              uidNext();
38     public abstract void             rename(String newName);      /* FIXME: IMAP semantics require creating parent dirs */
39     public abstract void             destroy(boolean recursive);
40     public          Mailbox          getMailbox() { return this; }
41
42     // Thunks ////////////////////////////////////////////////////////////////////////////
43
44     public          void             accept(Message m) { insert(m, Flag.RECENT); }
45     public          Mailbox.Iterator iterator() { return iterator(Query.all()); }
46
47
48     // Default Implementation //////////////////////////////////////////////////////////////////////////////
49
50     private int randomUidValidity = new Random().nextInt();
51     public  int uidValidity()  { return randomUidValidity; }
52
53     /** default, inefficient implementation of Mailbox; only requires a few methods to be implemented */
54     public static abstract class Default extends Mailbox {
55         public Mailbox.Iterator iterator(Query q) { return new Mailbox.Iterator.QueryIterator(q, this); }
56         public boolean phantom() { return false; }
57         public void copy(Query q, Mailbox dest) { for(Mailbox.Iterator it = iterator(q); it.next();) dest.insert(it.cur(), it.getFlags()); }
58         public int count(Query q) { int count = 0; for(Mailbox.Iterator it = iterator(q); it.next();) count++; return count; }
59         public void rename(String newName) { throw new MailException("not supported"); }
60         public void  destroy(boolean recursive) { throw new MailException("not supported"); }
61         public MailboxTree slash(String name, boolean create) { return null; }
62         public String[] children() { return new String[] { }; }
63         public void post(Message message) { insert(message, Flag.RECENT); }
64         public void move(Query q, Mailbox dest) {
65             for(Mailbox.Iterator it = iterator(q);it.next();) { dest.insert(it.cur(), it.getFlags()); it.delete(); }
66         }
67         public static abstract class Iterator implements Mailbox.Iterator {
68             // FIXME: NNTP spec allows us to use longs (64-bit) here
69             public int     nntpNumber() { throw new MailException("not supported"); }
70             public int     getFlags() { return 0; }
71             public void    setFlags(int flags) { throw new MailException("not supported"); }
72         }
73     }
74
75
76     // Iterator Definition //////////////////////////////////////////////////////////////////////////////
77
78     public static interface Iterator {
79         public abstract Message cur();
80         public abstract Headers head();
81         public abstract boolean next();
82         public abstract void    delete();
83
84         /** a unique identifier for this message */
85         public abstract int     uid();
86
87         /**
88          *  Message number according to IMAP semantics.
89          *    - no two messages in the same mailbox may have the same imapNumber
90          *    - sorting by uid must yield the same order as sorting them by imapNumber
91          *    - imapNumber may only change if uidValidity changes
92          *    - if uidValidity changes, imapNumbers may change or be reused
93          */
94         public abstract int     imapNumber();
95
96         /**
97          *  Message number according to NNTP semantics.
98          *    - no two messages in the same mailbox may have the same nntpNumber
99          *    - article number may NEVER change or EVER be reused
100          *    - uidValidity is irrelevant
101          */
102         public abstract int     nntpNumber();
103
104         public abstract int     getFlags();
105         public abstract void    setFlags(int flags);
106
107         public static class Wrapper implements Iterator {
108             private Iterator it;
109             public Wrapper(Iterator it) { this.it = it; }
110             public Message cur() { return it.cur(); }
111             public Headers head() { return it.head(); }
112             public boolean next() { return it.next(); }
113             public int     uid() { return it.uid(); }
114             public int     nntpNumber() { return it.nntpNumber(); }
115             public int     imapNumber() { return it.imapNumber(); }
116             public void    delete() { it.delete(); }
117             public int     getFlags() { return it.getFlags(); }
118             public void    setFlags(int flags) { it.setFlags(flags); }
119         }
120
121         class QueryIterator extends Mailbox.Iterator.Wrapper {
122             Query q;
123             public QueryIterator(Query q, Mailbox m) { super(m.iterator()); this.q = q; }
124             public boolean next() {
125                 if (q == null) return false;
126                 do { if (!super.next()) return false; } while(!q.match(this)); return true; }
127         }
128
129         public static class NullIterator extends Mailbox.Default.Iterator {
130             public NullIterator() { }
131             public Message cur() { return null; }
132             public Headers head() { return null; }
133             public boolean next() { return false; }
134             public int     uid() { return 0; }
135             public void    delete() { }
136             public int     imapNumber() { return 0; }
137             public int     nntpNumber() { throw new RuntimeException("this mailbox does not keep article numbers"); }
138         }
139     }
140
141     /** constants for the six IMAP flags */
142     public static class Flag {
143         public static final int DELETED  = 0x0001;
144         public static final int SEEN     = 0x0002;
145         public static final int FLAGGED  = 0x0004;
146         public static final int DRAFT    = 0x0008;
147         public static final int ANSWERED = 0x0010;
148         public static final int RECENT   = 0x0020;
149         public static final int[] all = new int[] { DELETED, SEEN, FLAGGED, DRAFT, ANSWERED, RECENT };
150         public static final int defaultFlags = RECENT;
151     }
152
153     public static class MailboxWrapper extends Mailbox {
154         
155         private Mailbox m;
156         public MailboxWrapper(Mailbox m) { this.m = m; }
157
158         public boolean          phantom() { return m.phantom(); }
159         public Mailbox.Iterator iterator(Query q) { return m.iterator(q); }
160         public void             insert(Message message, int flags) { m.insert(message, flags); }
161         public void             post(Message message) { m.insert(message, Flag.RECENT); }
162         public void             move(Query q, Mailbox dest) { m.move(q, dest); }
163         public void             copy(Query q, Mailbox dest) { m.copy(q, dest); }
164         public int              count(Query q) { return m.count(q); }
165         public int              uidNext() { return m.uidNext(); }
166         public void             rename(String newName) { m.rename(newName); }
167         public void             destroy(boolean recursive) { m.destroy(recursive); }
168         public MailboxTree      slash(String name, boolean create) { return m.slash(name, create); }
169         public String[]         children() { return m.children(); }
170         public Mailbox.Iterator iterator() { return m.iterator(); }
171         public int              uidValidity()  { return m.uidValidity(); }
172     }
173
174 }