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.
6 import org.ibex.mail.*;
7 import org.ibex.util.*;
8 import org.ibex.mail.*;
15 /** abstract superclass for mailboxes, which store messages along with their flags */
16 public abstract class Mailbox extends JS.Obj implements Target {
18 public JS get(JS key) throws JSExn {
19 return slash(JSU.toString(key), true);
22 public static final String STORAGE_ROOT =
23 System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
26 // Required Methods //////////////////////////////////////////////////////////////////////////////
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);
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 abstract Mailbox slash(String name, boolean create);
41 public abstract String[] children();
43 // Thunks ////////////////////////////////////////////////////////////////////////////
45 public void accept(Message m) { insert(m, Flag.RECENT); }
46 public Mailbox.Iterator iterator() { return iterator(Query.all()); }
49 // Default Implementation //////////////////////////////////////////////////////////////////////////////
51 private int randomUidValidity = new Random().nextInt();
52 public int uidValidity() { return randomUidValidity; }
54 /** default, inefficient implementation of Mailbox; only requires a few methods to be implemented */
55 public static abstract class Default extends Mailbox {
56 public Mailbox.Iterator iterator(Query q) { return new Mailbox.Iterator.QueryIterator(q, this); }
57 public boolean phantom() { return false; }
58 public void copy(Query q, Mailbox dest) { for(Mailbox.Iterator it = iterator(q); it.next();) dest.insert(it.cur(), it.getFlags()); }
59 public int count(Query q) { int count = 0; for(Mailbox.Iterator it = iterator(q); it.next();) count++; return count; }
60 public void rename(String newName) { throw new MailException("not supported"); }
61 public void destroy(boolean recursive) { throw new MailException("not supported"); }
62 public Mailbox slash(String name, boolean create) { return null; }
63 public String[] children() { return new String[] { }; }
64 public void post(Message message) { insert(message, Flag.RECENT); }
65 public void move(Query q, Mailbox dest) {
66 for(Mailbox.Iterator it = iterator(q);it.next();) { dest.insert(it.cur(), it.getFlags()); it.delete(); }
68 public static abstract class Iterator implements Mailbox.Iterator {
69 // FIXME: NNTP spec allows us to use longs (64-bit) here
70 public int nntpNumber() { throw new MailException("not supported"); }
71 public int getFlags() { return 0; }
72 public void setFlags(int flags) { throw new MailException("not supported"); }
77 // Iterator Definition //////////////////////////////////////////////////////////////////////////////
79 public static interface Iterator {
80 public abstract Message cur();
81 public abstract Headers head();
82 public abstract boolean next();
83 public abstract void delete();
85 /** a unique identifier for this message */
86 public abstract int uid();
89 * Message number according to IMAP semantics.
90 * - no two messages in the same mailbox may have the same imapNumber
91 * - sorting by uid must yield the same order as sorting them by imapNumber
92 * - imapNumber may only change if uidValidity changes
93 * - if uidValidity changes, imapNumbers may change or be reused
95 public abstract int imapNumber();
98 * Message number according to NNTP semantics.
99 * - no two messages in the same mailbox may have the same nntpNumber
100 * - article number may NEVER change or EVER be reused
101 * - uidValidity is irrelevant
103 public abstract int nntpNumber();
105 public abstract int getFlags();
106 public abstract void setFlags(int flags);
108 public static class Wrapper implements Iterator {
110 public Wrapper(Iterator it) { this.it = it; }
111 public Message cur() { return it.cur(); }
112 public Headers head() { return it.head(); }
113 public boolean next() { return it.next(); }
114 public int uid() { return it.uid(); }
115 public int nntpNumber() { return it.nntpNumber(); }
116 public int imapNumber() { return it.imapNumber(); }
117 public void delete() { it.delete(); }
118 public int getFlags() { return it.getFlags(); }
119 public void setFlags(int flags) { it.setFlags(flags); }
122 class QueryIterator extends Mailbox.Iterator.Wrapper {
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; }
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"); }
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;
154 public static class MailboxWrapper extends Mailbox {
157 public MailboxWrapper(Mailbox m) { this.m = m; }
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 post(Message message) { m.insert(message, Flag.RECENT); }
163 public void move(Query q, Mailbox dest) { m.move(q, dest); }
164 public void copy(Query q, Mailbox dest) { m.copy(q, dest); }
165 public int count(Query q) { return m.count(q); }
166 public int uidNext() { return m.uidNext(); }
167 public void rename(String newName) { m.rename(newName); }
168 public void destroy(boolean recursive) { m.destroy(recursive); }
169 public Mailbox slash(String name, boolean create) { return m.slash(name, create); }
170 public String[] children() { return m.children(); }
171 public Mailbox.Iterator iterator() { return m.iterator(); }
172 public int uidValidity() { return m.uidValidity(); }