ec122def4c7c48a8c3c35a0e0ea862aa7a184b7e
[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 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 final    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 flags() {
83                 return 
84                     (deleted() ? Flag.DELETED : 0) |
85                     (seen() ? Flag.SEEN : 0) |
86                     (answered() ? Flag.ANSWERED : 0) |
87                     (draft() ? Flag.DRAFT : 0) |
88                     (recent() ? Flag.RECENT : 0);
89             }
90             public void addFlags(int flags) {
91                 if ((flags & Flag.DELETED) == Flag.DELETED) deleted(true);
92                 if ((flags & Flag.SEEN) == Flag.SEEN) seen(true);
93                 if ((flags & Flag.FLAGGED) == Flag.FLAGGED) flagged(true);
94                 if ((flags & Flag.DRAFT) == Flag.DRAFT) draft(true);
95                 if ((flags & Flag.RECENT) == Flag.RECENT) recent(true);
96             }
97             public void removeFlags(int flags) {
98                 if ((flags & Flag.DELETED) == Flag.DELETED) deleted(false);
99                 if ((flags & Flag.SEEN) == Flag.SEEN) seen(false);
100                 if ((flags & Flag.FLAGGED) == Flag.FLAGGED) flagged(false);
101                 if ((flags & Flag.DRAFT) == Flag.DRAFT) draft(false);
102                 if ((flags & Flag.RECENT) == Flag.RECENT) recent(false);
103             }
104             public void setFlags(int flags) {
105                 if ((flags & Flag.DELETED) == Flag.DELETED) deleted(true); else deleted(false);
106                 if ((flags & Flag.SEEN) == Flag.SEEN) seen(true); else seen(false);
107                 if ((flags & Flag.FLAGGED) == Flag.FLAGGED) flagged(true); else flagged(false);
108                 if ((flags & Flag.DRAFT) == Flag.DRAFT) draft(true); else draft(false);
109                 if ((flags & Flag.RECENT) == Flag.RECENT) recent(true); else recent(false);
110             }
111         }
112     }
113
114
115     // Iterator Definition //////////////////////////////////////////////////////////////////////////////
116
117     public static interface Iterator {
118         public abstract Message cur();
119         public abstract Headers head();
120         public abstract boolean next();
121         public abstract int     uid();
122         public abstract int     num();
123         public abstract void    delete();
124
125         public abstract void    set(String key, String val);
126         public abstract String  get(String key);
127
128         public abstract boolean seen();
129         public abstract boolean deleted();
130         public abstract boolean flagged();
131         public abstract boolean draft();
132         public abstract boolean answered();
133         public abstract boolean recent();
134
135         public abstract void    seen(boolean on);
136         public abstract void    deleted(boolean on);
137         public abstract void    flagged(boolean on);
138         public abstract void    draft(boolean on);
139         public abstract void    answered(boolean on);
140         public abstract void    recent(boolean on);
141
142         public abstract int     flags();
143         public abstract void    addFlags(int flags);
144         public abstract void    removeFlags(int flags);
145         public abstract void    setFlags(int flags);
146
147         public static class Wrapper implements Iterator {
148             private Iterator it;
149             public Wrapper(Iterator it) { this.it = it; }
150             public Message cur() { return it.cur(); }
151             public Headers head() { return it.head(); }
152             public boolean next() { return it.next(); }
153             public int     uid() { return it.uid(); }
154             public int     flags() { return it.flags(); }
155             public int     num() { return it.num(); }
156             public void    set(String key, String val) { it.set(key, val); }
157             public String  get(String key) { return it.get(key); }
158             public void    delete() { it.delete(); }
159             public boolean seen() { return it.seen(); }
160             public boolean deleted() { return it.deleted(); }
161             public boolean flagged() { return it.flagged(); }
162             public boolean draft() { return it.draft(); }
163             public boolean answered() { return it.answered(); }
164             public boolean recent() { return it.recent(); }
165             public void    seen(boolean on) { it.seen(on); }
166             public void    deleted(boolean on) { it.deleted(on); }
167             public void    flagged(boolean on) { it.flagged(on); }
168             public void    draft(boolean on) { it.draft(on); }
169             public void    answered(boolean on) { it.answered(on); }
170             public void    recent(boolean on) { it.recent(on); }
171             public void    addFlags(int flags) { it.addFlags(flags); }
172             public void    removeFlags(int flags) { it.removeFlags(flags); }
173             public void    setFlags(int flags) { it.setFlags(flags); }
174         }
175
176         class QueryIterator extends Mailbox.Iterator.Wrapper {
177             Query q;
178             public QueryIterator(Query q, Mailbox m) { super(m.iterator()); this.q = q; }
179             public boolean next() {
180                 if (q == null) return false;
181                 do { if (!super.next()) return false; } while(!q.match(this)); return true; }
182         }
183
184         public static class NullIterator extends Mailbox.Default.Iterator {
185             public NullIterator() { }
186             public Message cur() { return null; }
187             public Headers head() { return null; }
188             public boolean next() { return false; }
189             public int     uid() { return 0; }
190             public int     flags() { return 0; }
191             public int     num() { return 0; }
192             public void    set(String key, String val) { }
193             public String  get(String key) { return null; }
194             public void    delete() { }
195             public boolean seen() { return false; }
196             public boolean deleted() { return false; }
197             public boolean flagged() { return false; }
198             public boolean draft() { return false; }
199             public boolean answered() { return false; }
200             public boolean recent() { return false; }
201             public void    seen(boolean on) { }
202             public void    deleted(boolean on) { }
203             public void    flagged(boolean on) { }
204             public void    draft(boolean on) { }
205             public void    answered(boolean on) { }
206             public void    recent(boolean on) { }
207         }
208     }
209
210     /** constants for the six IMAP flags */
211     public static class Flag {
212         public static final int DELETED  = 0x0001;
213         public static final int SEEN     = 0x0002;
214         public static final int FLAGGED  = 0x0004;
215         public static final int DRAFT    = 0x0008;
216         public static final int ANSWERED = 0x0010;
217         public static final int RECENT   = 0x0020;
218     }
219     
220 }