added IMAP setClient() method
[org.ibex.mail.git] / src / org / ibex / mail / protocol / IMAP.java
index e0cc718..2981f30 100644 (file)
@@ -47,10 +47,10 @@ public class IMAP {
     }
 
     public static interface Server {
+        public void      setClient(IMAP.Client client);
         public String[]  capability();
         public Hashtable id(Hashtable clientId);
         public void      copy(Query q, String to);
-        public void      login(String u, String p);
         public void      logout();
         public void      unselect();
         public void      delete(String m);
@@ -97,6 +97,7 @@ public class IMAP {
         final Client client;
 
         public MailboxWrapper(Login auth, Client c) { this.auth=auth; this.client=c;}
+        public void setClient(IMAP.Client client) { }
 
         private Mailbox mailbox(String name, boolean create) { return mailbox(name, create, true); }
         private Mailbox mailbox(String name, boolean create, boolean throwexn) {
@@ -154,13 +155,6 @@ public class IMAP {
         /*  */ void copy(Query q, Mailbox to) {
             for(Mailbox.Iterator it=selected().iterator(q);it.next();) to.add(it.cur(), it.flags() | Mailbox.Flag.RECENT); }
 
-        public void login(String u, String p) {
-            Account account = null;
-            if ((account = auth.login(u,p)) == null) throw new No("Login failed.");
-            root = account.getMailbox(IMAP.class);
-            inbox = root.slash("INBOX", false);
-            if (inbox == null) inbox = root;
-        }
         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"); }
@@ -234,18 +228,32 @@ public class IMAP {
         String selectedName = null;
         Mailbox inbox = null, root = null;
         Server api;
-        Login auth = null;
         Parser parser = null;
         Connection conn = null;
-        public Listener(Login auth) { this.auth = auth; }
+        Login auth;
+        public Listener(Login auth) { api = new IMAP.MailboxWrapper(this.auth = auth, this); }
         Parser.Token token() { return parser.token(); }
         void println(String s) { conn.println(s); }
         void newline() { parser.newline(); }
         Query query() { return parser.query(); }
+
+        public void login(String u, String p) {
+            Object ret;
+            if ((ret = auth.login(u,p,IMAP.class)) == null) throw new Server.No("Login failed.");
+            if (ret instanceof IMAP.Server) {
+                api = (IMAP.Server)ret;
+                api.setClient(this);
+            } else {
+                Account account = (Account)ret;
+                root = account.getMailbox(IMAP.class);
+                inbox = root.slash("INBOX", false);
+                if (inbox == null) inbox = root;
+            }
+        }
+
         public void handleRequest(Connection conn) {
             this.conn = conn;
             parser = new Parser(conn);
-            api = new IMAP.MailboxWrapper(auth, this);
             conn.setTimeout(30 * 60 * 1000);
             println("* OK " + conn.vhost + " " + IMAP.class.getName() + " IMAP4rev1 [RFC3501] v" + version + " server ready");
             for(String tag = null;; newline()) try {
@@ -256,7 +264,7 @@ public class IMAP {
                 if (command.equalsIgnoreCase("UID")) { uid = true; command = token().atom(); }
                 int commandKey = ((Integer)commands.get(command.toUpperCase())).intValue();
                 switch(commandKey) {
-                    case LOGIN:        api.login(token().astring(), token().astring()); break;
+                    case LOGIN:        login(token().astring(), token().astring()); break;
                     case CAPABILITY:   println("* CAPABILITY " + Printer.join(" ", api.capability())); break;
                     case AUTHENTICATE: throw new Server.No("AUTHENTICATE not supported");
                     case LOGOUT:       api.logout(); println("* BYE"); conn.close(); return;
@@ -555,7 +563,7 @@ public class IMAP {
             public Token()                         { this.s = null; n = 0;      l = null; type = NIL; }
             public Token(String s)                 { this(s, false); }
             public Token(String s, boolean quoted) { this.s = s;    n = 0;      l = null; type = quoted ? QUOTED : ATOM;  }
-            public Token(Parser.Token[] list)             { this.s = null; n = 0;      l = list; type = LIST; }
+            public Token(Parser.Token[] list)      { this.s = null; n = 0;      l = list; type = LIST; }
             public Token(int number)               { this.s = null; n = number; l = null; type = NUMBER; }
 
             public String   flag()    { if (type != ATOM) bad("expected a flag"); return s; }