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 static final String STORAGE_ROOT =
19 System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
22 // Required Methods //////////////////////////////////////////////////////////////////////////////
24 public abstract Mailbox.Iterator iterator(Query q);
26 public abstract void insert(Message message, int flags);
27 public abstract void post(Message message);
28 public abstract void move(Query q, Mailbox dest);
29 public abstract void copy(Query q, Mailbox dest);
30 public abstract int count(Query q);
31 public abstract int uidNext();
33 // Thunks ////////////////////////////////////////////////////////////////////////////
35 public void accept(Message m) { insert(m, Flag.RECENT); }
36 public Mailbox.Iterator iterator() { return iterator(Query.all()); }
39 // Default Implementation //////////////////////////////////////////////////////////////////////////////
41 private int randomUidValidity = new Random().nextInt();
42 public int uidValidity() { return randomUidValidity; }
44 /** default, inefficient implementation of Mailbox; only requires a few methods to be implemented */
45 public static abstract class Default extends Mailbox {
46 public Mailbox.Iterator iterator(Query q) { return new Mailbox.Iterator.QueryIterator(q, this); }
47 public void copy(Query q, Mailbox dest) { for(Mailbox.Iterator it = iterator(q); it.next();) dest.insert(it.cur(), it.getFlags()); }
48 public int count(Query q) { int count = 0; for(Mailbox.Iterator it = iterator(q); it.next();) count++; return count; }
49 public void rename(String newName) { throw new MailException("not supported"); }
50 public void destroy(boolean recursive) { throw new MailException("not supported"); }
51 public MailTree slash(String name, boolean create) { return null; }
52 public String[] children() { return new String[] { }; }
53 public void post(Message message) { insert(message, Flag.RECENT); }
54 public void move(Query q, Mailbox dest) {
55 for(Mailbox.Iterator it = iterator(q);it.next();) { dest.insert(it.cur(), it.getFlags()); it.delete(); }
57 public static abstract class Iterator implements Mailbox.Iterator {
58 // FIXME: NNTP spec allows us to use longs (64-bit) here
59 // FIXME: NNTP spec requires that the minimum nntpNumber of a group must never, ever decrease (no, that's not a typo)
60 public int nntpNumber() { throw new MailException("not supported"); }
61 public int getFlags() { return 0; }
62 public void setFlags(int flags) { throw new MailException("not supported"); }
67 // Iterator Definition //////////////////////////////////////////////////////////////////////////////
69 public static interface Iterator {
70 public abstract Message cur();
71 public abstract Headers head();
72 public abstract boolean next();
73 public abstract void delete();
75 /** a unique identifier for this message */
76 public abstract int uid();
79 * Message number according to IMAP semantics.
80 * - no two messages in the same mailbox may have the same imapNumber
81 * - sorting by uid must yield the same order as sorting them by imapNumber
82 * - imapNumber may only change if uidValidity changes
83 * - if uidValidity changes, imapNumbers may change or be reused
85 public abstract int imapNumber();
88 * Message number according to NNTP semantics.
89 * - no two messages in the same mailbox may have the same nntpNumber
90 * - article number may NEVER change or EVER be reused
91 * - uidValidity is irrelevant
93 public abstract int nntpNumber();
95 public abstract int getFlags();
96 public abstract void setFlags(int flags);
98 public static class Wrapper implements Iterator {
100 public Wrapper(Iterator it) { this.it = it; }
101 public Message cur() { return it.cur(); }
102 public Headers head() { return it.head(); }
103 public boolean next() { return it.next(); }
104 public int uid() { return it.uid(); }
105 public int nntpNumber() { return it.nntpNumber(); }
106 public int imapNumber() { return it.imapNumber(); }
107 public void delete() { it.delete(); }
108 public int getFlags() { return it.getFlags(); }
109 public void setFlags(int flags) { it.setFlags(flags); }
112 public static class AclWrapper extends Wrapper {
113 private Acl.Entry acl;
114 public AclWrapper(Iterator it, Acl.Entry acl) { super(it); this.acl = acl; }
115 public Message cur() { if (acl.read && acl.list) return super.cur(); else throw new Acl.PermissionDenied(); }
116 public Headers head() { if (acl.read && acl.list) return super.head(); else throw new Acl.PermissionDenied(); }
117 public boolean next() { if (acl.list) return super.next(); else throw new Acl.PermissionDenied(); }
118 public int uid() { if (acl.list) return super.uid(); else throw new Acl.PermissionDenied(); }
119 public int nntpNumber() { if (acl.list) return super.nntpNumber(); else throw new Acl.PermissionDenied(); }
120 public int imapNumber() { if (acl.list) return super.imapNumber(); else throw new Acl.PermissionDenied(); }
121 public void delete() { if (acl.delete) super.delete(); else throw new Acl.PermissionDenied(); }
122 public int getFlags() { if (acl.list) return super.getFlags(); else throw new Acl.PermissionDenied(); }
123 public void setFlags(int flags) { if (acl.flags) super.setFlags(flags); else throw new Acl.PermissionDenied(); }
126 class QueryIterator extends Mailbox.Iterator.Wrapper {
128 public QueryIterator(Query q, Mailbox m) { super(m.iterator()); this.q = q; }
129 public boolean next() {
130 if (q == null) return false;
131 do { if (!super.next()) return false; } while(!q.match(this)); return true; }
134 public static class NullIterator extends Mailbox.Default.Iterator {
135 public NullIterator() { }
136 public Message cur() { return null; }
137 public Headers head() { return null; }
138 public boolean next() { return false; }
139 public int uid() { return 0; }
140 public void delete() { }
141 public int imapNumber() { return 0; }
142 public int nntpNumber() { throw new RuntimeException("this mailbox does not keep article numbers"); }
146 /** constants for the six IMAP flags */
147 public static class Flag {
148 public static final int DELETED = 0x0001;
149 public static final int SEEN = 0x0002;
150 public static final int FLAGGED = 0x0004;
151 public static final int DRAFT = 0x0008;
152 public static final int ANSWERED = 0x0010;
153 public static final int RECENT = 0x0020;
154 public static final int[] all = new int[] { DELETED, SEEN, FLAGGED, DRAFT, ANSWERED, RECENT };
155 public static final int defaultFlags = RECENT;
158 public static class MailboxWrapper extends Mailbox {
160 public MailboxWrapper(Mailbox m) { this.m = m; }
162 public Mailbox.Iterator iterator() { return m.iterator(); }
163 public Mailbox.Iterator iterator(Query q) { return m.iterator(q); }
164 public void insert(Message message, int flags) { m.insert(message, flags); }
165 public void post(Message message) { m.insert(message, Flag.RECENT); }
166 public void move(Query q, Mailbox dest) { m.move(q, dest); }
167 public void copy(Query q, Mailbox dest) { m.copy(q, dest); }
168 public int count(Query q) { return m.count(q); }
169 public int uidNext() { return m.uidNext(); }
170 public int uidValidity() { return m.uidValidity(); }
173 public static class AclWrapper extends MailboxWrapper {
175 private Acl.Entry acl;
176 public AclWrapper(Mailbox m, Acl.Entry acl) { super(m); this.acl = acl; }
177 public Mailbox.Iterator iterator(Query q) { if (acl.list) return new Mailbox.Iterator.AclWrapper(m.iterator(q), acl); else throw new Acl.PermissionDenied(); }
178 public Mailbox.Iterator iterator() { if (acl.list) return new Mailbox.Iterator.AclWrapper(m.iterator(), acl); else throw new Acl.PermissionDenied(); }
179 public void insert(Message message, int flags) { if (acl.insert) m.insert(message, flags); else throw new Acl.PermissionDenied(); }
180 public int uidValidity() { if (acl.list) return m.uidValidity(); else throw new Acl.PermissionDenied(); }
181 public void post(Message message) { if (acl.post) m.insert(message, Flag.RECENT); else throw new Acl.PermissionDenied(); }
182 public void move(Query q, Mailbox dest) { if (acl.list && acl.read && acl.delete) m.move(q, dest); else throw new Acl.PermissionDenied(); }
183 public void copy(Query q, Mailbox dest) { if (acl.list && acl.read) m.copy(q, dest); else throw new Acl.PermissionDenied(); }
184 public int count(Query q) { if (acl.list) return m.count(q); else throw new Acl.PermissionDenied(); }
185 public int uidNext() { if (acl.list) return m.uidNext(); else throw new Acl.PermissionDenied(); }