add mailbox acls, further separate Mailbox and MailboxTree
[org.ibex.mail.git] / src / org / ibex / mail / IMAP.java
index 4f60d0d..dc3b8f3 100644 (file)
@@ -110,16 +110,23 @@ public class IMAP {
         public MailboxWrapper(Login auth, Client c) { this.auth=auth; this.client=c;}
         public void setClient(IMAP.Client client) { }
 
+        private String dirname(String name) { return name.substring(0, name.lastIndexOf(sep)); }
+        private String basename(String name) { return name.substring(name.lastIndexOf(sep)+1); }
         private Mailbox mailbox(String name, boolean create) { return mailbox(name, create, true); }
         private Mailbox mailbox(String name, boolean create, boolean throwexn) {
             if (name.equalsIgnoreCase("inbox")) return inbox;
+            MailboxTree mt =  mailboxTree(name, create, throwexn);
+            return mt==null ? null : mt.getMailbox();
+        }
+        private MailboxTree mailboxTree(String name, boolean create) { return mailboxTree(name, create, true); }
+        private MailboxTree mailboxTree(String name, boolean create, boolean throwexn) {
             MailboxTree m = root;
             for(StringTokenizer st = new StringTokenizer(name, sep + ""); st.hasMoreTokens();)
                 if ((m = m.slash(st.nextToken(), create)) == null) {
                     if (throwexn) throw new Server.No("no such mailbox " + name);
                     return null;
                 }
-            return m.getMailbox();
+            return m;
         }
 
         // FEATURE: not accurate when a wildcard and subsequent non-wildcards both match a single component
@@ -133,13 +140,13 @@ public class IMAP {
             if (ref.length() == 0) { client.list(sep, start, lsub, false); return; }
             while (start.endsWith(""+sep)) start = start.substring(0, start.length() - 1);
             if (ref.endsWith("%")) ref = ref + sep;
-            String[] children = (start.length() == 0 ? root : mailbox(start, false)).children();
+            String[] children = (start.length() == 0 ? root : mailboxTree(start, false)).children();
             for(int i=0; i<children.length; i++) {
                 String s = children[i], pre = ref, kid = start + (start.length() > 0 ? sep+"" : "") + s;                
                 if (mailbox(kid, false) == null) continue;
-                Mailbox phant = mailbox(kid, false, false);
+                MailboxTree phant = mailboxTree(kid, false, false);
                 if (phant != null) {
-                    boolean phantom = phant.phantom();
+                    boolean phantom = phant.getMailbox()==null;
                     while(true) {
                         if (pre.length() == 0) {
                             if (s.length() == 0)       client.list(sep, kid, lsub, phantom);
@@ -174,8 +181,15 @@ public class IMAP {
             for(Mailbox.Iterator it=selected().iterator(q);it.next();) to.insert(it.cur(), it.getFlags() | Mailbox.Flag.RECENT); }
 
         public void unselect() { selected = null; }
-        public void delete(String m0) { delete(mailbox(m0,false)); }
-        public void delete(Mailbox m) { if (!m.equals(inbox)) m.destroy(false); else throw new Bad("can't delete inbox"); }
+
+        public void delete(String m0) { mailboxTree(dirname(m0),false).rmdir(basename(m0)); }
+        public void rename(String from0, String to) {
+            Mailbox from = mailbox(from0, false);
+            if (from.equals(inbox))                { from.copy(Query.all(), mailbox(to, true)); }
+            else if (to.equalsIgnoreCase("inbox")) { from.copy(Query.all(), mailbox(to, true)); delete(from0); }
+            else mailboxTree(dirname(from0), false).rename(dirname(from0), mailboxTree(dirname(to), false), basename(to));
+        }
+
         public void create(String m) { mailbox(m, true, false); }
         public void append(String m,int f,Date a,String b) { try {
             // FIXME: more efficient streaming here?
@@ -229,12 +243,6 @@ public class IMAP {
             }
         }
 
-        public void rename(String from0, String to) {
-            Mailbox from = mailbox(from0, false);
-            if (from.equals(inbox))                { from.copy(Query.all(), mailbox(to, true)); }
-            else if (to.equalsIgnoreCase("inbox")) { from.copy(Query.all(), mailbox(to, true)); from.destroy(false); }
-            else from.rename(to);
-        }
         public void fetch(Query q, int spec, String[] headers, int start, int end, boolean uid) {
             for(Mailbox.Iterator it = selected().iterator(q); it.next(); ) {
                 Message message = ((spec & (BODYSTRUCTURE | ENVELOPE | INTERNALDATE | FIELDS | FIELDSNOT | RFC822 |
@@ -276,8 +284,9 @@ public class IMAP {
             } else {
                 Account account = (Account)ret;
                 ((MailboxWrapper)api).root = root = account.getMailbox(IMAP.class);
-                ((MailboxWrapper)api).inbox = inbox = root.slash("INBOX", false).getMailbox();
-                if (inbox == null) ((MailboxWrapper)api).inbox = inbox = root.getMailbox();
+                MailboxTree ibt = root.slash("INBOX", false);
+                Mailbox ib = ibt==null ? null : ibt.getMailbox();
+                ((MailboxWrapper)api).inbox = inbox = ib;
             }
         }