change num() to imapNumber() and nntpNumber(), add comments about semantics
[org.ibex.mail.git] / src / org / ibex / mail / protocol / IMAP.java
index 9730436..5b98105 100644 (file)
@@ -44,7 +44,7 @@ public class IMAP {
     // FIXME this is evil
     public static String getBodyString(Message m) {
         StringBuffer sb = new StringBuffer();
-        m.getStream().transcribe(sb);
+        m.getBody().getStream().transcribe(sb);
         return sb.toString();
     }
 
@@ -138,17 +138,21 @@ public class IMAP {
             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;
-                boolean phantom = mailbox(kid, false).phantom();
-                while(true) {
-                    if (pre.length() == 0) {
-                        if (s.length() == 0)       client.list(sep, kid, lsub, phantom);
-                    } else switch(pre.charAt(0)) {
-                        case sep:        if (s.length() == 0) list(kid, pre.substring(1), lsub);                    break;
-                        case '%':        client.list(sep,kid,lsub,phantom);pre=pre.substring(1); s = "";            continue;
-                        case '*':        client.list(sep,kid,lsub,phantom);list(kid,pre,lsub);pre=pre.substring(1); break;
-                        default:         if (s.length()==0)                                                         break;
-                                         if (s.charAt(0) != pre.charAt(0))                                          break;
-                                         s = s.substring(1); pre = pre.substring(1);                                continue;
+                Mailbox phant = mailbox(kid, false, false);
+                if (phant != null) {
+                    boolean phantom = phant.phantom();
+                    while(true) {
+                        if (pre.length() == 0) {
+                            if (s.length() == 0)       client.list(sep, kid, lsub, phantom);
+                        } else switch(pre.charAt(0)) {
+                            case sep:        if (s.length() == 0) list(kid, pre.substring(1), lsub);                    break;
+                            case '%':        client.list(sep,kid,lsub,phantom);pre=pre.substring(1); s = "";            continue;
+                            case '*':        client.list(sep,kid,lsub,phantom);list(kid,pre,lsub);pre=pre.substring(1); break;
+                            default:         if (s.length()==0)                                                         break;
+                                if (s.charAt(0) != pre.charAt(0))                                          break;
+                                s = s.substring(1); pre = pre.substring(1);                                continue;
+                        }
+                        break;
                     }
                 }
             }
@@ -203,7 +207,7 @@ public class IMAP {
         public int[] search(Query q, boolean uid) {
             Vec.Int vec = new Vec.Int();
             for(Mailbox.Iterator it = selected().iterator(q); it.next();) {
-                vec.addElement(uid ? it.uid() : it.num());
+                vec.addElement(uid ? it.uid() : it.imapNumber());
                 it.recent(false);
             }
             return vec.dump();
@@ -221,7 +225,7 @@ public class IMAP {
                 else if (style == 0) it.setFlags(flags);
                 else if (style == 1) it.addFlags(flags);
                 it.recent(recent);
-                if (!silent) client.fetch(it.num(), it.flags(), -1, null, it.uid());
+                if (!silent) client.fetch(it.imapNumber(), it.flags(), -1, null, it.uid());
             }
         }            
         public void rename(String from0, String to) {
@@ -235,7 +239,7 @@ public class IMAP {
                 Message message = ((spec & (BODYSTRUCTURE | ENVELOPE | INTERNALDATE | FIELDS | FIELDSNOT | RFC822 |
                                             RFC822TEXT | RFC822SIZE | HEADERNOT | HEADER)) != 0) ? it.cur() : null;
                 int size = message == null ? 0 : message.getLength();
-                client.fetch(it.num(), it.flags(), size, message, it.uid());
+                client.fetch(it.imapNumber(), it.flags(), size, message, it.uid());
                 it.recent(false);
             }
         }
@@ -297,7 +301,6 @@ public class IMAP {
                     case SUBSCRIBE:    api.subscribe(token().astring()); break;
                     case UNSUBSCRIBE:  api.unsubscribe(token().astring()); break;
                     case RENAME:       api.rename(token().astring(), token().astring()); break;
-                    case COPY:         selected(); api.copy(Query.set(uid, token().set(maxn(uid))), token().astring()); break;
                     case DELETE:       api.delete(token().atom()); break;
                     case CHECK:        selected(); api.check(); break;
                     case NOOP:         api.noop(); break;
@@ -305,8 +308,13 @@ public class IMAP {
                     case EXPUNGE:      selected(); api.expunge(); break;
                     case UNSELECT:     selected(); api.unselect(); selected = false; break;
                     case CREATE:       api.create(token().astring()); break;
-                    case FETCH:        selected(); fetch(Query.set(lastuid=uid, token().set(maxn(uid))),
+                    case FETCH:        selected(); fetch(((lastuid=uid)
+                                                          ? Query.uid(token().set(maxn(uid)))
+                                                          : Query.imapNumber(token().set(maxn(uid)))),
                                                         lastfetch=token().lx(), 0, 0, 0, uid, 0); break;
+                    case COPY:         selected(); api.copy(uid
+                                                            ? Query.uid(token().set(maxn(uid)))
+                                                            : Query.imapNumber(token().set(maxn(uid))), token().astring()); break;
                     case SEARCH: {
                         selected();
                         int[] result = api.search(query(maxn(uid)), uid);
@@ -353,7 +361,7 @@ public class IMAP {
                         break; }
                     case STORE: {
                         selected();
-                        Query q = uid ? Query.uid(token().set(maxn(uid))) : Query.num(token().set(maxn(uid)));
+                        Query q = uid ? Query.uid(token().set(maxn(uid))) : Query.imapNumber(token().set(maxn(uid)));
                         String s = token().atom().toUpperCase();
                         int flags = token().flags();
                         if (s.equals("FLAGS"))              api.setFlags(q,    flags, uid, false);
@@ -456,7 +464,7 @@ public class IMAP {
                 } else if (!(s.equals("BODY.PEEK") || s.equals("BODY"))) { throw new Server.No("unknown fetch argument: " + s);
                 } else {
                     if (s.equalsIgnoreCase("BODY.PEEK"))   spec |= PEEK;
-                    //else if (e) api.addFlags(Query.num(new int[] { num, num }), Mailbox.Flag.SEEN, false, false);
+                    //else if (e) api.addFlags(Query.imapNumber(new int[] { num, num }), Mailbox.Flag.SEEN, false, false);
                     if (i >= t.length - 1 || t[i+1].type != Parser.Token.LIST) {
                        spec |= BODYSTRUCTURE;
                        if (e) { r.append(" "); r.append(Printer.bodystructure(m)); } continue;
@@ -559,7 +567,7 @@ public class IMAP {
                 Parser.Token t = token(false);
                 if (t == null) break;
                 if (t.type == t.LIST) throw new Server.No("nested queries not yet supported FIXME");
-                else if (t.type == t.SET) return Query.num(t.set(max));
+                else if (t.type == t.SET) return Query.imapNumber(t.set(max));
                 s = t.atom().toUpperCase();
                 if (s.equals("NOT"))   return Query.not(query(max, maxuid));
                 if (s.equals("OR"))    return Query.or(query(max, maxuid), query(max, maxuid));    // FIXME parse rest of list