change num() to imapNumber() and nntpNumber(), add comments about semantics
[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     public abstract void             add(Message message);
32     public abstract void             add(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
43     // Thunks ////////////////////////////////////////////////////////////////////////////
44
45     public          void             accept(Message m) { add(m); }
46     public          Mailbox.Iterator iterator() { return iterator(Query.all()); }
47
48
49     // Default Implementation //////////////////////////////////////////////////////////////////////////////
50
51     private int randomUidValidity = new Random().nextInt();
52     public  int uidValidity()  { return randomUidValidity; }
53
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.add(it.cur()); }
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 move(Query q, Mailbox dest) {
65             for(Mailbox.Iterator it = iterator(q);it.next();) { dest.add(it.cur()); it.delete(); }
66         }
67         public static abstract class Iterator implements Mailbox.Iterator {
68             public boolean seen() { return false; }
69             public boolean deleted() { return false; }
70             public boolean flagged() { return false; }
71             public boolean draft() { return false; }
72             public boolean answered() { return false; }
73             public boolean recent() { return false; }
74             public void    seen(boolean on) { }
75             public void    deleted(boolean on) { }
76             public void    flagged(boolean on) { }
77             public void    draft(boolean on) { }
78             public void    answered(boolean on) { }
79             public void    recent(boolean on) { }
80             public void    set(String key, String val) { throw new MailException("not supported"); }
81             public String  get(String key) { throw new MailException("not supported"); }
82             public int     nntpNumber() { throw new MailException("not supported"); }
83             public int flags() {
84                 return 
85                     (deleted() ? Flag.DELETED : 0) |
86                     (seen() ? Flag.SEEN : 0) |
87                     (answered() ? Flag.ANSWERED : 0) |
88                     (draft() ? Flag.DRAFT : 0) |
89                     (recent() ? Flag.RECENT : 0);
90             }
91             public void addFlags(int flags) {
92                 if ((flags & Flag.DELETED) == Flag.DELETED) deleted(true);
93                 if ((flags & Flag.SEEN) == Flag.SEEN) seen(true);
94                 if ((flags & Flag.FLAGGED) == Flag.FLAGGED) flagged(true);
95                 if ((flags & Flag.DRAFT) == Flag.DRAFT) draft(true);
96                 if ((flags & Flag.RECENT) == Flag.RECENT) recent(true);
97             }
98             public void removeFlags(int flags) {
99                 if ((flags & Flag.DELETED) == Flag.DELETED) deleted(false);
100                 if ((flags & Flag.SEEN) == Flag.SEEN) seen(false);
101                 if ((flags & Flag.FLAGGED) == Flag.FLAGGED) flagged(false);
102                 if ((flags & Flag.DRAFT) == Flag.DRAFT) draft(false);
103                 if ((flags & Flag.RECENT) == Flag.RECENT) recent(false);
104             }
105             public void setFlags(int flags) {
106                 if ((flags & Flag.DELETED) == Flag.DELETED) deleted(true); else deleted(false);
107                 if ((flags & Flag.SEEN) == Flag.SEEN) seen(true); else seen(false);
108                 if ((flags & Flag.FLAGGED) == Flag.FLAGGED) flagged(true); else flagged(false);
109                 if ((flags & Flag.DRAFT) == Flag.DRAFT) draft(true); else draft(false);
110                 if ((flags & Flag.RECENT) == Flag.RECENT) recent(true); else recent(false);
111             }
112         }
113     }
114
115
116     // Iterator Definition //////////////////////////////////////////////////////////////////////////////
117
118     public static interface Iterator {
119         public abstract Message cur();
120         public abstract Headers head();
121         public abstract boolean next();
122         public abstract void    delete();
123
124         /** a unique identifier for this message */
125         public abstract int     uid();
126
127         /**
128          *  Message number according to IMAP semantics.
129          *    - no two messages in the same mailbox may have the same imapNumber
130          *    - sorting by uid must yield the same order as sorting them by imapNumber
131          *    - imapNumber may only change if uidValidity changes
132          *    - if uidValidity changes, imapNumbers may change or be reused
133          */
134         public abstract int     imapNumber();
135
136         /**
137          *  Message number according to NNTP semantics.
138          *    - no two messages in the same mailbox may have the same nntpNumber
139          *    - article number may NEVER change or EVER be reused
140          *    - uidValidity is irrelevant
141          */
142         public abstract int     nntpNumber();
143
144         public abstract void    set(String key, String val);
145         public abstract String  get(String key);
146
147         public abstract boolean seen();
148         public abstract boolean deleted();
149         public abstract boolean flagged();
150         public abstract boolean draft();
151         public abstract boolean answered();
152         public abstract boolean recent();
153
154         public abstract void    seen(boolean on);
155         public abstract void    deleted(boolean on);
156         public abstract void    flagged(boolean on);
157         public abstract void    draft(boolean on);
158         public abstract void    answered(boolean on);
159         public abstract void    recent(boolean on);
160
161         public abstract int     flags();
162         public abstract void    addFlags(int flags);
163         public abstract void    removeFlags(int flags);
164         public abstract void    setFlags(int flags);
165
166         public static class Wrapper implements Iterator {
167             private Iterator it;
168             public Wrapper(Iterator it) { this.it = it; }
169             public Message cur() { return it.cur(); }
170             public Headers head() { return it.head(); }
171             public boolean next() { return it.next(); }
172             public int     uid() { return it.uid(); }
173             public int     flags() { return it.flags(); }
174             public int     nntpNumber() { return it.nntpNumber(); }
175             public int     imapNumber() { return it.imapNumber(); }
176             public void    set(String key, String val) { it.set(key, val); }
177             public String  get(String key) { return it.get(key); }
178             public void    delete() { it.delete(); }
179             public boolean seen() { return it.seen(); }
180             public boolean deleted() { return it.deleted(); }
181             public boolean flagged() { return it.flagged(); }
182             public boolean draft() { return it.draft(); }
183             public boolean answered() { return it.answered(); }
184             public boolean recent() { return it.recent(); }
185             public void    seen(boolean on) { it.seen(on); }
186             public void    deleted(boolean on) { it.deleted(on); }
187             public void    flagged(boolean on) { it.flagged(on); }
188             public void    draft(boolean on) { it.draft(on); }
189             public void    answered(boolean on) { it.answered(on); }
190             public void    recent(boolean on) { it.recent(on); }
191             public void    addFlags(int flags) { it.addFlags(flags); }
192             public void    removeFlags(int flags) { it.removeFlags(flags); }
193             public void    setFlags(int flags) { it.setFlags(flags); }
194         }
195
196         class QueryIterator extends Mailbox.Iterator.Wrapper {
197             Query q;
198             public QueryIterator(Query q, Mailbox m) { super(m.iterator()); this.q = q; }
199             public boolean next() {
200                 if (q == null) return false;
201                 do { if (!super.next()) return false; } while(!q.match(this)); return true; }
202         }
203
204         public static class NullIterator extends Mailbox.Default.Iterator {
205             public NullIterator() { }
206             public Message cur() { return null; }
207             public Headers head() { return null; }
208             public boolean next() { return false; }
209             public int     uid() { return 0; }
210             public int     flags() { return 0; }
211             public int     imapNumber() { return 0; }
212             public int     nntpNumber() { throw new RuntimeException("this mailbox does not keep article numbers"); }
213             public void    set(String key, String val) { }
214             public String  get(String key) { return null; }
215             public void    delete() { }
216             public boolean seen() { return false; }
217             public boolean deleted() { return false; }
218             public boolean flagged() { return false; }
219             public boolean draft() { return false; }
220             public boolean answered() { return false; }
221             public boolean recent() { return false; }
222             public void    seen(boolean on) { }
223             public void    deleted(boolean on) { }
224             public void    flagged(boolean on) { }
225             public void    draft(boolean on) { }
226             public void    answered(boolean on) { }
227             public void    recent(boolean on) { }
228         }
229     }
230
231     /** constants for the six IMAP flags */
232     public static class Flag {
233         public static final int DELETED  = 0x0001;
234         public static final int SEEN     = 0x0002;
235         public static final int FLAGGED  = 0x0004;
236         public static final int DRAFT    = 0x0008;
237         public static final int ANSWERED = 0x0010;
238         public static final int RECENT   = 0x0020;
239     }
240
241     public static class MailboxWrapper extends Mailbox {
242         
243         private Mailbox m;
244         public MailboxWrapper(Mailbox m) { this.m = m; }
245
246         public boolean          phantom() { return m.phantom(); }
247         public Mailbox.Iterator iterator(Query q) { return m.iterator(q); }
248         public void             add(Message message) { m.add(message); }
249         public void             add(Message message, int flags) { m.add(message, flags); }
250         public void             move(Query q, Mailbox dest) { m.move(q, dest); }
251         public void             copy(Query q, Mailbox dest) { m.copy(q, dest); }
252         public int              count(Query q) { return m.count(q); }
253         public int              uidNext() { return m.uidNext(); }
254         public void             rename(String newName) { m.rename(newName); }
255         public void             destroy(boolean recursive) { m.destroy(recursive); }
256         public Mailbox          slash(String name, boolean create) { return m.slash(name, create); }
257         public String[]         children() { return m.children(); }
258         public Mailbox.Iterator iterator() { return m.iterator(); }
259         public int              uidValidity()  { return m.uidValidity(); }
260     }
261
262 }