more bugfixes
[org.ibex.mail.git] / src / org / ibex / mail / protocol / IMAP.java
index 8a2319c..2263bb5 100644 (file)
@@ -128,7 +128,7 @@ public class IMAP {
             }
         }
 
-        public String[] capability() { return new String[] { "IMAP4rev1", "UNSELECT", "ID" }; }
+        public String[] capability() { return new String[] { "IMAP4rev1" /*, "UNSELECT", "ID"*/ }; }
         public Hashtable id(Hashtable clientId) {
             Hashtable response = new Hashtable();
             response.put("name", IMAP.class.getName());
@@ -232,7 +232,7 @@ public class IMAP {
                 boolean uid = false;
                 tag = null; tag = token().atom();
                 String command = token().atom();
-                if (command.equals("UID")) { uid = true; command = token().atom(); }
+                if (command.equalsIgnoreCase("UID")) { uid = true; command = token().atom(); }
                 switch(((Integer)commands.get(command.toUpperCase())).intValue()) {
                     case AUTHENTICATE: api.login(token().astring(), token().astring()); break;
                     case LOGIN:        api.login(token().astring(), token().astring()); break;
@@ -245,7 +245,7 @@ public class IMAP {
                         //case ID:           id(); break;
                     case RENAME:       api.rename(token().atom(), token().atom()); break;
                     case EXAMINE:      api.select(token().astring(), true); break;
-                    case COPY:         api.copy(Query.set(uid, token().set()), token().atom()); break;
+                    case COPY:         api.copy(Query.set(uid, token().set()), token().q()); break;
                     case DELETE:       api.delete(token().atom()); break;
                     case CHECK:        api.check(); break;
                     case NOOP:         api.noop(); break;
@@ -289,12 +289,12 @@ public class IMAP {
                         break; }
                     case STORE: {
                         Query q = uid ? Query.uid(token().set()) : Query.num(token().set());
-                        String s = token().atom();
+                        String s = token().atom().toUpperCase();
                         int flags = token().flags();
                         if (s.equals("FLAGS"))              api.setFlags(q,    flags, uid, null);
                         else if (s.equals("+FLAGS"))        api.addFlags(q,    flags, uid, null);
                         else if (s.equals("-FLAGS"))        api.removeFlags(q, flags, uid, null);
-                        if (s.equals("FLAGS.SILENT"))       api.setFlags(q,    flags, uid, this);
+                        else if (s.equals("FLAGS.SILENT"))  api.setFlags(q,    flags, uid, this);
                         else if (s.equals("+FLAGS.SILENT")) api.addFlags(q,    flags, uid, this);
                         else if (s.equals("-FLAGS.SILENT")) api.removeFlags(q, flags, uid, this);
                         else throw new API.Bad("unknown STORE specifier " + s);
@@ -333,7 +333,7 @@ public class IMAP {
                 } else if (s.equals("INTERNALDATE")) {  spec|=INTERNALDATE; if(e){r.append(" "); r.append(Printer.date(m.arrival));}
                 } else if (s.equals("RFC822")) {        spec|=RFC822; if(e){r.append(" "); r.append(Printer.message(m));}
                 } else if (s.equals("RFC822.TEXT")) {   spec|=RFC822TEXT; if(e){r.append(" ");r.append(Printer.qq(m.body));}
-                } else if (s.equals("RFC822.HEADER")) { spec|=RFC822HEADER; if(e){r.append(" ");r.append(Printer.qq(m.allHeaders));}
+                } else if (s.equals("RFC822.HEADER")) { spec|=RFC822HEADER; if(e){r.append(" ");r.append(Printer.qq(m.allHeaders+"\r\n"));}
                 } else if (s.equals("RFC822.SIZE")) {   spec|=RFC822SIZE; if(e){r.append(" "); r.append(m.rfc822size());}
                 } else if (s.equals("UID")) {
                     spec|=UID;
@@ -342,23 +342,24 @@ public class IMAP {
                 } else if (s.equals("BODY.PEEK") || s.equals("BODY")) {
                     if (s.equalsIgnoreCase("BODY.PEEK")) spec |= PEEK;
                     String payload = "";
-                    if (i<t.length - 1 && (t[i+1].type == t[i].LIST)) {
-                        i++;
+                    if (i<t.length - 1 && (t[i+1].type == Token.LIST)) {
                         r.append("[");
-                        String s2 = t[i].l()[0].s.toUpperCase();
+                        Token[] list = t[++i].l();
+                        String s2 = list.length == 0 ? "" : list[0].s.toUpperCase();
                         r.append(s2);
-                        if (t[i].l().length == 0)                { spec |= RFC822TEXT;   if(e) payload = m.body; }
+                        if (list.length == 0)                    { spec |= RFC822TEXT;   if(e) payload = m.body; }
                         else if (s2.equals(""))                  { spec |= RFC822TEXT;   if(e) payload = m.body; }
                         else if (s2.equals("TEXT"))              { spec |= RFC822TEXT;   if(e) payload = m.body; }
-                        else if (s2.equals("HEADER"))            { spec |= RFC822HEADER; if(e) payload = m.allHeaders; }
+                        else if (s2.equals("HEADER"))            { spec |= RFC822HEADER; if(e) payload = m.allHeaders+"\r\n"; }
                         else if (s2.equals("HEADER.FIELDS"))     {
                             spec |= RFC822HEADER;
                             headers = t[i].l()[1].sl();
                             if (e) r.append(" (");
                             if(e) for(int j=0; j<headers.length; j++) {
                                 r.append(headers[j] + (j<headers.length-1?" ":""));
-                                payload += headers[j]+": "+m.headers.get(headers[j])+"\r\n";
+                                if (m.headers.get(headers[j]) != null) payload += headers[j]+": "+m.headers.get(headers[j])+"\r\n";
                             }
+                            payload += "\r\n";
                             if (e) r.append(")]");
                         } else if (s2.equals("HEADER.FIELDS.NOT")) {
                             spec |= RFC822HEADER|NEGATEHEADERS;
@@ -372,6 +373,7 @@ public class IMAP {
                                 for(int j=0; j<headers.length; j++) if (key.equalsIgnoreCase(headers[j])) continue OUTER;
                                 payload += key + ": " + m.headers.get(key)+"\r\n";
                             } }
+                            payload += "\r\n";
                         } else if (s2.equals("MIME")) {            throw new API.Bad("MIME not supported");
                         } else throw new API.Bad("unknown section type " + s2);
                         if (i<t.length - 1 && (t[i+1].s != null && t[i+1].s.startsWith("<"))) {
@@ -732,8 +734,6 @@ public class IMAP {
             ret.append('\r');
             ret.append('\n');
             ret.append(s);
-            ret.append('\r');
-            ret.append('\n');
             return ret.toString();
         }