almost there
[org.ibex.mail.git] / src / org / ibex / mail / target / Mailbox.java
1 package org.ibex.mail.target;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.*;
5 import java.io.*;
6 import java.net.*;
7 import java.util.*;
8 import java.text.*;
9
10 public abstract class Mailbox extends Target {
11
12     private static final String STORAGE_ROOT =
13         System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
14     public static FileBased root = null;
15     public static Transcript transcript = null;
16     static {
17         try {
18             root = new FileBased(STORAGE_ROOT + File.separatorChar);
19             transcript = new Transcript(STORAGE_ROOT + File.separatorChar + "transcript");
20         } catch (Exception e) {
21             e.printStackTrace();
22         }
23     }
24
25     // metadata
26     public void   set(Message m, String key, String val) { throw new MailException.MetadataNotSupported(); }
27     public String get(Message m, String key)             { throw new MailException.MetadataNotSupported(); }
28
29     // flags
30     public final boolean getFlag(Message m, int flags)   { return (flags(m) & flag) == flags; }
31     public final void    setFlag(Message m, int flag)   { flags(m, flags | flag); }
32     public final void    clearFlag(Message m, int flag) { flags(m, flags & ~flag); }
33     protected abstract void flags(Message m, int newFlags);  // set the flags for a given message
34     protected abstract int  flags(Message m);                // get the flags for a given message
35     public    abstract int  uid(Message m);                  // get the uid for a given message              (see IMAP RFC)
36     public    abstract int  num(Message m);                  // get the "message number" for a given message (see IMAP RFC)
37     public    abstract int  uidNext();                       // get the next uid to be assigned 
38     public    abstract int  uidValidity();                   // get the uid validity identifier              (see IMAP RFC)
39
40     // messages
41     public abstract void visit(Message.Visitor v) throws MailException;   // only abstract method
42     public int  add(Message message)                      throws MailException { throw new MailException("not implemented"); }
43     public int  delete(Message message)                   throws MailException { throw new MailException("not implemented"); }
44     public void move(Query q, Mailbox dest, boolean copy) throws MailException { throw new MailException("not implemented"); }
45     public int  count(Query q) throws MailException { CounterVisitor cv = new CounterVisitor(this,q); visit(cv); return cv.count; }
46     public void query(Query q, final Message.Visitor v) throws MailException {
47         visit(new Message.Visitor() { public void visit(Message m) { if (q.match(Mailbox.this,m)) v.visit(Mailbox.this,m); } }); } 
48
49     // submailboxes
50     public void    rename(String newName) throws MailException { throw new MailException("you cannot rename this mailbox"); }
51     public void    destroy() throws MailException { throw new MailException("you cannot destroy this mailbox"); }
52     public Mailbox slash(String name, boolean create) throws MailException { throw new MailException("no submailboxes"); }
53
54     private static class CounterVisitor implements Message.Visitor {
55         public int count = 0;
56         public void visit(Message m) { if (q.match(Mailbox.this, m)) count++; }
57     }
58
59     public final void    accept(Message m) throws MailException { add(m); }
60 }