commenting in Mailbox.java
[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 JS.Obj implements Target {
17
18     public static final String STORAGE_ROOT =
19         System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
20
21
22     // Required Methods //////////////////////////////////////////////////////////////////////////////
23
24     public abstract Mailbox.Iterator iterator(Query q);
25
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();
32
33     // Thunks ////////////////////////////////////////////////////////////////////////////
34
35     public          void             accept(Message m) { insert(m, Flag.RECENT); }
36     public          Mailbox.Iterator iterator() { return iterator(Query.all()); }
37
38
39     // Default Implementation //////////////////////////////////////////////////////////////////////////////
40
41     private int randomUidValidity = new Random().nextInt();
42     public  int uidValidity()  { return randomUidValidity; }
43
44     public int maxuid() {
45         int ret = -1;
46         for(Mailbox.Iterator it = iterator(); it.next(); ) ret = Math.max(ret, it.uid());
47         return ret;
48     }
49
50     /** default, inefficient implementation of Mailbox; only requires a few methods to be implemented */
51     public static abstract class Default extends Mailbox {
52         public Mailbox.Iterator iterator(Query q) { return new Mailbox.Iterator.QueryIterator(q, this); }
53         public void copy(Query q, Mailbox dest) { for(Mailbox.Iterator it = iterator(q); it.next();) dest.insert(it.cur(), it.getFlags()); }
54         public int count(Query q) { int count = 0; for(Mailbox.Iterator it = iterator(q); it.next();) count++; return count; }
55         public MailTree slash(String name, boolean create) { return null; }
56         public String[] children() { return new String[] { }; }
57         public void post(Message message) { insert(message, Flag.RECENT); }
58         public void move(Query q, Mailbox dest) {
59             for(Mailbox.Iterator it = iterator(q);it.next();) { dest.insert(it.cur(), it.getFlags()); it.delete(); }
60         }
61         public static abstract class Iterator implements Mailbox.Iterator {
62             // FIXME: NNTP spec allows us to use longs (64-bit) here
63             // FIXME: NNTP spec requires that the minimum nntpNumber of a group must never, ever decrease (no, that's not a typo)
64             public int     nntpNumber() { throw new MailException("not supported"); }
65             public int     getFlags() { return 0; }
66             public void    setFlags(int flags) { throw new MailException("not supported"); }
67         }
68     }
69
70
71     // Iterator Definition //////////////////////////////////////////////////////////////////////////////
72
73     public static interface Iterator {
74         public abstract Message cur();
75         public abstract Headers head();
76
77         // FIXME: change all code to actually use this convention!
78         /** JDBC convention: iterator starts "before the first element" */
79         public abstract boolean next();
80
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          *    - must range from 1..numMessagesInMailbox
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 changes when messages with lower imapNumbers are deleted
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 void    setFlags(int flags);
105
106         public static class Wrapper implements Iterator {
107             private Iterator it;
108             public Wrapper(Iterator it) { this.it = it; }
109             public Message cur() { return it.cur(); }
110             public Headers head() { return it.head(); }
111             public boolean next() { return it.next(); }
112             public int     uid() { return it.uid(); }
113             public int     nntpNumber() { return it.nntpNumber(); }
114             public int     imapNumber() { return it.imapNumber(); }
115             public void    delete() { it.delete(); }
116             public int     getFlags() { return it.getFlags(); }
117             public void    setFlags(int flags) { it.setFlags(flags); }
118         }
119
120         public static class AclWrapper extends Wrapper {
121             private Acl.Entry acl;
122             public AclWrapper(Iterator it, Acl.Entry acl) { super(it); this.acl = acl; }
123             public Message cur() { if (acl.read && acl.list) return super.cur(); else throw new Acl.PermissionDenied(); }
124             public Headers head() { if (acl.read && acl.list) return super.head(); else throw new Acl.PermissionDenied(); }
125             public boolean next() { if (acl.list) return super.next(); else throw new Acl.PermissionDenied(); }
126             public int     uid() { if (acl.list) return super.uid(); else throw new Acl.PermissionDenied(); }
127             public int     nntpNumber() { if (acl.list) return super.nntpNumber(); else throw new Acl.PermissionDenied(); }
128             public int     imapNumber() { if (acl.list) return super.imapNumber(); else throw new Acl.PermissionDenied(); }
129             public void    delete() { if (acl.delete) super.delete(); else throw new Acl.PermissionDenied(); }
130             public int     getFlags() { if (acl.list) return super.getFlags(); else throw new Acl.PermissionDenied(); }
131             public void    setFlags(int flags) { if (acl.flags) super.setFlags(flags); else throw new Acl.PermissionDenied(); }
132         }
133
134         class QueryIterator extends Mailbox.Iterator.Wrapper {
135             Query q;
136             public QueryIterator(Query q, Mailbox m) { super(m.iterator()); this.q = q; }
137             public boolean next() {
138                 if (q == null) return false;
139                 do { if (!super.next()) return false; } while(!q.match(this)); return true; }
140         }
141
142         public static class NullIterator extends Mailbox.Default.Iterator {
143             public NullIterator() { }
144             public Message cur() { return null; }
145             public Headers head() { return null; }
146             public boolean next() { return false; }
147             public int     uid() { return 0; }
148             public void    delete() { }
149             public int     imapNumber() { return 0; }
150             public int     nntpNumber() { throw new RuntimeException("this mailbox does not keep article numbers"); }
151         }
152     }
153
154     /** constants for the six IMAP flags */
155     public static class Flag {
156         public static final int DELETED  = 0x0001;
157         public static final int SEEN     = 0x0002;
158         public static final int FLAGGED  = 0x0004;
159         public static final int DRAFT    = 0x0008;
160         public static final int ANSWERED = 0x0010;
161         public static final int RECENT   = 0x0020;
162         public static final int[] all = new int[] { DELETED, SEEN, FLAGGED, DRAFT, ANSWERED, RECENT };
163         public static final int defaultFlags = RECENT;
164     }
165
166     public static class MailboxWrapper extends Mailbox {
167         private Mailbox m;
168         public MailboxWrapper(Mailbox m) { this.m = m; }
169
170         public Mailbox.Iterator iterator() { return m.iterator(); }
171         public Mailbox.Iterator iterator(Query q) { return m.iterator(q); }
172         public void             insert(Message message, int flags) { m.insert(message, flags); }
173         public void             post(Message message) { m.insert(message, Flag.RECENT); }
174         public void             move(Query q, Mailbox dest) { m.move(q, dest); }
175         public void             copy(Query q, Mailbox dest) { m.copy(q, dest); }
176         public int              count(Query q) { return m.count(q); }
177         public int              uidNext() { return m.uidNext(); }
178         public int              uidValidity()  { return m.uidValidity(); }
179     }
180
181     public static class AclWrapper extends MailboxWrapper {
182         private Mailbox m;
183         private Acl.Entry acl;
184         public AclWrapper(Mailbox m, Acl.Entry acl) { super(m); this.acl = acl; }
185         public Mailbox.Iterator iterator(Query q) { if (acl.list) return new Mailbox.Iterator.AclWrapper(m.iterator(q), acl); else throw new Acl.PermissionDenied(); }
186         public Mailbox.Iterator iterator() { if (acl.list) return new Mailbox.Iterator.AclWrapper(m.iterator(), acl); else throw new Acl.PermissionDenied(); }
187         public void             insert(Message message, int flags) { if (acl.insert) m.insert(message, flags); else throw new Acl.PermissionDenied(); }
188         public int              uidValidity()  { if (acl.list) return m.uidValidity(); else throw new Acl.PermissionDenied(); }
189         public void             post(Message message) { if (acl.post) m.insert(message, Flag.RECENT); else throw new Acl.PermissionDenied(); }
190         public void             move(Query q, Mailbox dest) { if (acl.list && acl.read && acl.delete) m.move(q, dest); else throw new Acl.PermissionDenied(); }
191         public void             copy(Query q, Mailbox dest) { if (acl.list && acl.read) m.copy(q, dest); else throw new Acl.PermissionDenied(); }
192         public int              count(Query q) { if (acl.list) return m.count(q); else throw new Acl.PermissionDenied(); }
193         public int              uidNext() { if (acl.list) return m.uidNext(); else throw new Acl.PermissionDenied(); }
194     }
195 }