major simplifications to Iterator api
[org.ibex.mail.git] / src / org / ibex / mail / target / 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.target;
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 JS.Obj 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             move(Query q, Mailbox dest);
34     public abstract void             copy(Query q, Mailbox dest);
35     public abstract int              count(Query q);
36     public abstract int              uidNext();
37     public abstract void             rename(String newName);      /* FIXME: IMAP semantics require creating parent dirs */
38     public abstract void             destroy(boolean recursive);
39     public abstract Mailbox          slash(String name, boolean create);
40     public abstract String[]         children();
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 Mailbox slash(String name, boolean create) { return null; }
62         public String[] children() { return new String[] { }; }
63         public void move(Query q, Mailbox dest) {
64             for(Mailbox.Iterator it = iterator(q);it.next();) { dest.insert(it.cur(), it.getFlags()); it.delete(); }
65         }
66         public static abstract class Iterator implements Mailbox.Iterator {
67             public int     nntpNumber() { throw new MailException("not supported"); }
68             public int     getFlags() { return 0; }
69             public boolean getFlag(int flag) { return false; }
70             public void    setFlag(int flag, boolean set) { throw new RuntimeException("not implemented"); }
71         }
72     }
73
74
75     // Iterator Definition //////////////////////////////////////////////////////////////////////////////
76
77     public static interface Iterator {
78         public abstract Message cur();
79         public abstract Headers head();
80         public abstract boolean next();
81         public abstract void    delete();
82
83         /** a unique identifier for this message */
84         public abstract int     uid();
85
86         /**
87          *  Message number according to IMAP semantics.
88          *    - no two messages in the same mailbox may have the same imapNumber
89          *    - sorting by uid must yield the same order as sorting them by imapNumber
90          *    - imapNumber may only change if uidValidity changes
91          *    - if uidValidity changes, imapNumbers may change or be reused
92          */
93         public abstract int     imapNumber();
94
95         /**
96          *  Message number according to NNTP semantics.
97          *    - no two messages in the same mailbox may have the same nntpNumber
98          *    - article number may NEVER change or EVER be reused
99          *    - uidValidity is irrelevant
100          */
101         public abstract int     nntpNumber();
102
103         public abstract int     getFlags();
104         public abstract boolean getFlag(int flag);
105         public abstract void    setFlag(int flag, boolean set);
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 boolean getFlag(int flag) { return it.getFlag(flag); }
119             public void    setFlag(int flag, boolean set) { it.setFlag(flag, set); }
120         }
121
122         class QueryIterator extends Mailbox.Iterator.Wrapper {
123             Query q;
124             public QueryIterator(Query q, Mailbox m) { super(m.iterator()); this.q = q; }
125             public boolean next() {
126                 if (q == null) return false;
127                 do { if (!super.next()) return false; } while(!q.match(this)); return true; }
128         }
129
130         public static class NullIterator extends Mailbox.Default.Iterator {
131             public NullIterator() { }
132             public Message cur() { return null; }
133             public Headers head() { return null; }
134             public boolean next() { return false; }
135             public int     uid() { return 0; }
136             public void    delete() { }
137             public int     imapNumber() { return 0; }
138             public int     nntpNumber() { throw new RuntimeException("this mailbox does not keep article numbers"); }
139         }
140     }
141
142     /** constants for the six IMAP flags */
143     public static class Flag {
144         public static final int DELETED  = 0x0001;
145         public static final int SEEN     = 0x0002;
146         public static final int FLAGGED  = 0x0004;
147         public static final int DRAFT    = 0x0008;
148         public static final int ANSWERED = 0x0010;
149         public static final int RECENT   = 0x0020;
150         public static final int[] all = new int[] { DELETED, SEEN, FLAGGED, DRAFT, ANSWERED, RECENT };
151         public static final int defaultFlags = RECENT;
152     }
153
154     public static class MailboxWrapper extends Mailbox {
155         
156         private Mailbox m;
157         public MailboxWrapper(Mailbox m) { this.m = m; }
158
159         public boolean          phantom() { return m.phantom(); }
160         public Mailbox.Iterator iterator(Query q) { return m.iterator(q); }
161         public void             insert(Message message, int flags) { m.insert(message, flags); }
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 Mailbox          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 }