21e937a8f6c150de5a80d816e9015b39d79acd3e
[org.ibex.mail.git] / src / org / ibex / mail / protocol / IMAP.java
1 package org.ibex.mail.protocol;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.target.*;
5 import java.util.*;
6 import java.net.*;
7 import java.text.*;
8 import java.io.*;
9
10 // Relevant RFC's:
11 //   RFC 2060: IMAPv4
12 //   RFC 3501: IMAPv4 with clarifications
13 //   RFC 3691: UNSELECT
14 //   RFC 2971: ID
15
16 // FEATURE: READ-WRITE / READ-ONLY status on SELECT
17 // FEATURE: pipelining
18 // FEATURE: support [charset]
19 // FEATURE: \Noselect
20 // FEATURE: subscriptions
21 public class IMAP extends MessageProtocol {
22
23     // Constants //////////////////////////////////////////////////////////////////////////////
24
25     public static final char imapSeparator = '.';
26     public static final float version = (float)0.1;
27
28
29     // Callbacks //////////////////////////////////////////////////////////////////////////////
30
31     public static interface Authenticator { public abstract Mailbox authenticate(String user, String pass); } 
32
33
34     // Exceptions //////////////////////////////////////////////////////////////////////////////
35
36     public static class Exn extends MailException { public Exn(String s) { super(s); } }
37     public static class Bad extends Exn { public Bad(String s) { super(s); } }
38     public static class No extends Exn { public No(String s) { super(s); } }
39
40
41     // Single Session Handler //////////////////////////////////////////////////////////////////////////////
42
43     private static class Session extends Incoming {
44         Mailbox selected = null;
45         Mailbox selected() { if (selected == null) throw new Bad("no mailbox selected"); return selected; }
46         String selectedName = null;
47         Mailbox inbox = null;
48         final Mailbox root;
49         final Socket conn;
50         final String vhost;
51         final Authenticator auth;
52         final PrintWriter pw;
53         final PushbackReader r;
54         final InputStream is;
55         public void init() { }
56         public Session(Socket conn, Mailbox root, Authenticator auth) throws IOException {
57             this(conn, java.net.InetAddress.getLocalHost().getHostName(), root, auth); }
58         public Session(Socket conn, String vhost, Mailbox root, Authenticator auth) throws IOException {
59             this.vhost = vhost; this.conn = conn; this.root = root; this.auth = auth;
60             this.pw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
61             this.r = new PushbackReader(new InputStreamReader(this.is = conn.getInputStream()));
62         }
63
64         private void star(String s)    { println("* " + s); }
65         private void println(String s) { pw.println(s); pw.flush(); }
66         private void print(String s)   { pw.print(s); }
67
68         private Mailbox getMailbox(String name, boolean create) {
69             Mailbox m = root;
70             for(StringTokenizer st = new StringTokenizer(name, imapSeparator + ""); st.hasMoreTokens();)
71                 if ((m = m.slash(st.nextToken(), create)) == null) throw new No("no such mailbox " + name);
72             return m;
73         }
74
75         // FEATURE: not accurate when a wildcard and subsequent non-wildcards both match a single component
76         public HashSet lsub(String start, String ref, HashSet reply) { return list(start, ref, reply); }
77         public HashSet list(String start, String ref, HashSet reply) {
78             if (reply == null) reply = new HashSet();
79             if (ref.length() == 0 && start.length() == 0) { reply.add(""); return reply; }
80             while (start.endsWith(""+imapSeparator)) start = start.substring(0, start.length() - 1);
81             String[] children = (start.length() == 0 ? root : getMailbox(start, false)).children();
82             for(int i=0; i<children.length; i++) {
83                 String s = children[i], pre = ref, kid = start + (start.length() > 0 ? imapSeparator+"" : "") + s;                
84                 while(true) {
85                     if (pre.length() == 0) {
86                         if (s.length() == 0)       reply.add(kid);
87                     } else switch(pre.charAt(0)) {
88                         case imapSeparator:        if (s.length() == 0) list(kid, pre.substring(1), reply);           break;
89                         case '%':                  reply.add(kid); pre=pre.substring(1); list(kid, pre, reply);       break;
90                         case '*':                  reply.add(kid); list(kid, pre, reply); pre = pre.substring(1);     break;
91                         default:                   if (s.length()==0)                                                 break;
92                                                    if (s.charAt(0) != pre.charAt(0))                                  break;
93                                                    s = s.substring(1); pre = pre.substring(1);                        continue;
94                     }
95                     break;
96                 }
97             }
98             return reply;
99         }
100
101         public String[] capability() { return new String[] { "IMAP4rev1", "UNSELECT", "ID" }; }
102         public Hashtable id(Hashtable clientId) {
103             Hashtable response = new Hashtable();
104             response.put("name", IMAP.class.getName());
105             response.put("version", version + "");
106             response.put("os", System.getProperty("os.name", null));
107             response.put("os-version", System.getProperty("os.version", null));
108             response.put("vendor", "none");
109             response.put("support-url", "http://mail.ibex.org/");
110             return response;
111         }   
112
113         public void copy(Query q, Mailbox to) { for(Mailbox.Iterator it=selected.iterator(q);it.next();) to.add(it.cur()); }
114         public void login(String u, String p) { if ((inbox = auth.authenticate(u,p)) == null) throw new No("Login failed."); }
115         public void logout() { }
116         public void unselect() { selected = null; selectedName = null; }
117         public void delete(Mailbox m) { if (!m.equals(inbox)) m.destroy(); }
118         public void create(String m) { if (!m.endsWith(""+imapSeparator) && !m.equalsIgnoreCase("inbox")) getMailbox(m, true); }
119         public void append(Mailbox m, int flags, Date arrival, String body) { m.add(new Message(null,null,body,arrival), flags); }
120         public void check() { }
121         public void noop() { }
122         public void close() { for(Mailbox.Iterator it=selected().iterator(Query.deleted()); it.next();) it.delete(); unselect(); }
123         public void expunge() { for(Mailbox.Iterator it = selected().iterator(Query.deleted()); it.next();) expunge(it); }
124         public void expunge(Mailbox.Iterator it) { star(it.uid() + " EXPUNGE"); it.delete(); }
125         public void subscribe(String mailbox) { }
126         public void unsubscribe(String mailbox) { }
127         public int seen(String mailbox)        { return getMailbox(mailbox, false).count(Query.seen()); }
128         public int recent(String mailbox)      { return getMailbox(mailbox, false).count(Query.recent()); }
129         public int count(String mailbox)       { return getMailbox(mailbox, false).count(Query.all()); }
130         public int uidNext(String mailbox)     { return getMailbox(mailbox, false).uidNext(); }
131         public int uidValidity(String mailbox) { return getMailbox(mailbox, false).uidValidity(); }
132         public void rename(Mailbox from, String to) {
133             if (from.equals(inbox))                  from.copy(Query.all(), getMailbox(to, true));
134             else if (to.equalsIgnoreCase("inbox")) { from.copy(Query.all(), getMailbox(to, true)); from.destroy(); }
135             else from.rename(to);
136         }
137
138         public void select(String mailbox, boolean examineOnly) {
139             selected = getMailbox(selectedName = mailbox, false);
140             star("FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)");
141             star(selected.count(Query.all())    + " EXISTS");
142             star(selected.count(Query.recent()) + " RECENT");
143             star("OK [UIDVALIDITY " + selected.uidValidity() + "] UIDs valid");
144         }
145
146         // uber FIXME
147         public void fetch(Query q, FetchRequest[] fr, boolean uid) {
148             for(Mailbox.Iterator it = selected.iterator(q); it.next(); ) {
149                 Message m = it.cur();
150                 StringBuffer reply = new StringBuffer();
151                 boolean peek = false;
152                 for(int i=0; i<fr.length; i++)
153                     if (fr[i] == null) continue;
154                     else if (fr[i] == BODYSTRUCTURE)   throw new Bad("BODYSTRUCTURE not supported");
155                     else if (fr[i] == ENVELOPE     )   reply.append("ENVELOPE " + envelope(m) + " ");
156                     else if (fr[i] == FLAGS        )   reply.append("FLAGS (" + flags(it) + ") ");
157                     else if (fr[i] == INTERNALDATE )   reply.append("INTERNALDATE " + quotify(m.arrival) + " ");
158                     else if (fr[i] == UID          )   reply.append("UID " + it.uid() + " ");
159                     else if (fr[i] == RFC822       ) { reply.append("RFC822 "); reply.append(qq(m.body)); }
160                     else if (fr[i] == RFC822HEADER ) { reply.append("RFC822.HEADER ");reply.append(qq(m.allHeaders));}
161                     else if (fr[i] == RFC822SIZE   )   reply.append("RFC822.SIZE " + m.rfc822size() + " ");
162                     else if (fr[i] == RFC822TEXT   ) { }
163                     else if (fr[i] instanceof FetchRequestBody) {
164                         FetchRequestBody frb = (FetchRequestBody)fr[i];
165                         StringBuffer payload = new StringBuffer();
166                         peek = frb.peek;
167                         // obey path[] here
168                         switch(frb.type) {
169                             case FetchRequestBody.PATH:
170                                 reply.append("BODY[] "); payload.append(m.body); break;
171                             case FetchRequestBody.TEXT:
172                                 reply.append("BODY[TEXT] "); payload.append(m.body); break;
173                             case FetchRequestBody.MIME: throw new Bad("FETCH MIME not supported");
174                             case FetchRequestBody.HEADER: reply.append("BODY[HEADER] "); payload.append(m.allHeaders); break;
175                             case FetchRequestBody.FIELDS:
176                                 reply.append("BODY[HEADER.FIELDS (");
177                                 for(int j=0; j<frb.headers.length; j++) {
178                                     reply.append(frb.headers[j].s + " ");
179                                     payload.append(frb.headers[j].s + ": " + m.headers.get(frb.headers[j].s) + "\r\n");
180                                 }
181                                 reply.append(")] ");
182                                 break;
183                             case FetchRequestBody.FIELDS_NOT: throw new Bad("FETCH HEADERS.NOT not supported");
184                         }
185                         if (frb.start == -1) reply.append(qq(payload.toString()));
186                         else if (frb.start == -1) reply.append("<" + frb.end + ">" + qq(payload.substring(0, frb.end + 1)));
187                         else reply.append("<" + frb.start + "." + frb.end + ">" + qq(payload.substring(frb.start, frb.end + 1)));
188                     } else throw new Error("this should never happen");
189                 star((uid ? it.uid() : it.num()) + " FETCH (" + reply.toString() + (uid ? " UID " + it.uid() : "") + ")");
190                 if (!peek) it.seen(true);
191             }
192         }
193
194         public void store(Query q, int addFlags, int removeFlags, boolean silent, boolean uid) {
195             for(Mailbox.Iterator it = selected.iterator(q); it.next(); ) {
196                 it.addFlags(addFlags);
197                 it.removeFlags(removeFlags);
198                 if (!silent) star((uid ? it.uid() : it.num()) + " FETCH (FLAGS (" + flags(it) + "))");
199             }
200         }
201
202         private void emitListResponse(HashSet boxes)
203             { for(Iterator it = boxes.iterator(); it.hasNext(); ) star("LIST \"" + imapSeparator + "\" \"" + it.next() + "\""); }
204
205         public boolean handleRequest() throws IOException {
206             println("* OK " + vhost + " " + IMAP.class.getName() + " IMAP4rev1 [RFC3501] v" + version + " server ready");
207             for(String tag = null;; newline()) try {
208                 pw.flush();
209                 boolean uid = false;
210                 tag = null; tag = token().atom();
211                 String command = token().atom();
212                 if (command.equals("UID")) { uid = true; command = token().atom(); }
213                 switch(((Integer)commands.get(command.toUpperCase())).intValue()) {
214                     case AUTHENTICATE: login(token().astring(), token().astring()); break;
215                     case LIST:         emitListResponse(list(token().q(), token().q(), null)); break;
216                     case LSUB:         emitListResponse(lsub(token().q(), token().q(), null)); break;
217                     case SUBSCRIBE:    subscribe(token().atom()); break;
218                     case UNSUBSCRIBE:  unsubscribe(token().atom()); break;
219                     case CAPABILITY:   star("CAPABILITY " + join(" ", capability())); break;
220                         //case ID:           id(); break;
221                     case LOGIN:        login(token().astring(), token().astring()); break;
222                     case LOGOUT:       logout(); star("BYE"); conn.close(); return false;
223                     case RENAME:       rename(token().mailbox(), token().atom()); break;
224                     case EXAMINE:      select(token().astring(), true); break;
225                     case SELECT:       select(token().astring(), false); break;
226                     case COPY:         copy(Query.set(uid, token().set()), token().mailbox()); break;
227                     case DELETE:       delete(token().mailbox()); break;
228                     case CHECK:        check(); break;
229                     case NOOP:         noop(); break;
230                     case CLOSE:        close(); break;
231                     case EXPUNGE:      expunge(); break;
232                     case UNSELECT:     unselect(); break;
233                     case CREATE:       create(token().astring()); break;
234                     case FETCH:        fetch(Query.set(uid, token().set()), parseFetch(token().l()), uid); break;
235                     case STATUS: {
236                         String mailbox = token().atom();
237                         Token[] list = token().l();
238                         String response = "";
239                         for(int i=0; i<list.length; i++) {
240                             String s = list[i].atom();
241                             if (s.equals("MESSAGES"))    response += "MESSAGES "    + count(mailbox);
242                             if (s.equals("RECENT"))      response += "RECENT "      + seen(mailbox);
243                             if (s.equals("UIDNEXT"))     response += "UNSEEN "      + recent(mailbox);
244                             if (s.equals("UIDVALIDITY")) response += "UIDVALIDITY " + uidValidity(mailbox);
245                             if (s.equals("UNSEEN"))      response += "UIDNEXT "     + uidNext(mailbox);
246                         }
247                         star("STATUS " + selectedName + " (" + response + ")");
248                     }
249                     case APPEND: { 
250                         Mailbox m = token().mailbox();
251                         int flags = 0;
252                         Date arrival = null;
253                         Token t = token();
254                         if (t.type == t.LIST)   { flags = t.flags();      t = token(); }
255                         if (t.type == t.QUOTED) { arrival = t.datetime(); t = token(); }
256                         append(m, flags, arrival, token().q());
257                         break; }
258                     case STORE: {
259                         Query q = uid ? Query.uid(token().set()) : Query.num(token().set());
260                         String s = token().atom();
261                         int flags = token().flags();
262                         if (s.equals("FLAGS"))              store(q, flags, ~flags, false, uid);
263                         else if (s.equals("+FLAGS"))        store(q, flags, 0,      false, uid);
264                         else if (s.equals("-FLAGS"))        store(q, 0,     flags,  false, uid);
265                         else if (s.equals("FLAGS.SILENT"))  store(q, flags, ~flags, true,  uid);
266                         else if (s.equals("+FLAGS.SILENT")) store(q, flags, 0,      true,  uid);
267                         else if (s.equals("-FLAGS.SILENT")) store(q, 0,     flags,  true,  uid);
268                         else throw new Bad("unknown STORE specifier " + s);
269                         break; }
270                     default: throw new Bad("unrecognized command \"" + command + "\"");
271                 }
272                 println(tag + " OK " + command + " Completed.");
273             } catch (Bad b) { println(tag==null ? "* BAD Invalid tag" : (tag + " Bad " + b.toString())); b.printStackTrace();
274             } catch (No n)  { println(tag==null ? "* BAD Invalid tag" : (tag + " No "  + n.toString())); n.printStackTrace(); }
275         }
276
277         Query query() {
278             String s = null;
279             boolean not = false;
280             Query q = null;
281             while(true) {
282                 Token t = token();
283                 if (t.type == t.LIST) throw new No("nested queries not yet supported");
284                 else if (t.type == t.SET) return Query.num(t.set());
285                 s = t.atom();
286                 if (s.equals("NOT")) { not = true; continue; }
287                 if (s.equals("OR"))    return Query.or(query(), query());
288                 if (s.equals("AND"))   return Query.and(query(), query());
289                 break;
290             }
291             if (s.startsWith("UN"))        { not = true; s = s.substring(2); }
292             if (s.equals("ANSWERED"))        q = Query.answered();
293             else if (s.equals("DELETED"))    q = Query.deleted();
294             else if (s.equals("DRAFT"))      q = Query.draft();
295             else if (s.equals("FLAGGED"))    q = Query.flagged();
296             else if (s.equals("RECENT"))     q = Query.recent();
297             else if (s.equals("SEEN"))       q = Query.seen();
298             else if (s.equals("OLD"))      { not = true; q = Query.recent(); }
299             else if (s.equals("NEW"))        q = Query.and(Query.recent(), Query.not(Query.seen()));
300             else if (s.equals("KEYWORD"))    q = Query.header("keyword", token().flag());
301             else if (s.equals("HEADER"))     q = Query.header(token().astring(), token().astring());
302             else if (s.equals("BCC"))        q = Query.header("bcc", token().astring());
303             else if (s.equals("CC"))         q = Query.header("cc", token().astring());
304             else if (s.equals("FROM"))       q = Query.header("from", token().astring());
305             else if (s.equals("TO"))         q = Query.header("to", token().astring());
306             else if (s.equals("SUBJECT"))    q = Query.header("subject", token().astring());
307             else if (s.equals("LARGER"))     q = Query.size(token().n(), Integer.MAX_VALUE);
308             else if (s.equals("SMALLER"))    q = Query.size(Integer.MIN_VALUE, token().n());
309             else if (s.equals("BODY"))       q = Query.body(token().astring());
310             else if (s.equals("TEXT"))       q = Query.full(token().astring());
311             else if (s.equals("BEFORE"))     q = Query.arrival(new Date(0), token().date());
312             else if (s.equals("SINCE"))      q = Query.arrival(token().date(), new Date(Long.MAX_VALUE));
313             else if (s.equals("ON"))       { Date d = token().date(); q = Query.arrival(d, new Date(d.getTime() + 24 * 60 * 60)); }
314             else if (s.equals("SENTBEFORE")) q = Query.sent(new Date(0), token().date());
315             else if (s.equals("SENTSINCE"))  q = Query.sent(token().date(), new Date(Long.MAX_VALUE));
316             else if (s.equals("SENTON"))   { Date d = token().date(); q = Query.sent(d, new Date(d.getTime() + 24 * 60 * 60)); }
317             else if (s.equals("UID"))        q = Query.uid(token().set());
318             return q;
319         }
320
321         class Token {
322             public byte type;
323             public final String s;
324             public final Token[] l;
325             public final int n;
326             private static final byte NIL = 0;
327             private static final byte LIST = 1;
328             private static final byte QUOTED = 2;
329             private static final byte NUMBER = 3;
330             private static final byte ATOM = 4;
331             private static final byte BAREWORD = 5;
332             private static final byte SET = 6;
333             public Token() { n = 0; l = null; s = null; type = NIL; }
334             public Token(String s, boolean quoted) { this.s = s; l = null; type = quoted ? QUOTED : ATOM; n = 0; }
335             public Token(Token[] list) { l = list; s = null; type = LIST; n = 0; }
336             public Token(int number) { n = number; l = null; s = null; type = NUMBER; }
337
338             public String flag() { if (type != ATOM) throw new Bad("expected a flag"); return s; }
339             public int n() { if (type != NUMBER) throw new Bad("expected number"); return n; }
340             public int nz() { int n = n(); if (n == 0) throw new Bad("expected nonzero number"); return n; }
341             public String  q() { if (type == NIL) return null; if (type != QUOTED) throw new Bad("expected qstring"); return s; }
342             public Token[] l() { if (type == NIL) return null; if (type != LIST) throw new Bad("expected list"); return l; }
343
344             public int flags() {
345                 if (type != LIST) throw new Bad("expected flag list");
346                 int ret = 0;
347                 for(int i=0; i<l.length; i++) {
348                     String flag = l[i].s;
349                     if (flag.equals("\\Deleted"))       ret |= Mailbox.Flag.DELETED;
350                     else if (flag.equals("\\Seen"))     ret |= Mailbox.Flag.SEEN;
351                     else if (flag.equals("\\Flagged"))  ret |= Mailbox.Flag.FLAGGED;
352                     else if (flag.equals("\\Draft"))    ret |= Mailbox.Flag.DRAFT;
353                     else if (flag.equals("\\Answered")) ret |= Mailbox.Flag.ANSWERED;
354                     else if (flag.equals("\\Recent"))   ret |= Mailbox.Flag.RECENT;
355                 }
356                 return ret;
357             }
358             public int[] set() {
359                 if (type != ATOM) throw new Bad("expected a messageid set");
360                 Vec ids = new Vec();
361                 StringTokenizer st = new StringTokenizer(s, ",");
362                 while(st.hasMoreTokens()) {
363                     String s = st.nextToken();
364                     if (s.indexOf(':') != -1) {
365                         int start = Integer.parseInt(s.substring(0, s.indexOf(':')));
366                         String end_s = s.substring(s.indexOf(':')+1);
367                         if (end_s.equals("*")) {
368                             ids.addElement(new Integer(start));
369                             ids.addElement(new Integer(Integer.MAX_VALUE));
370                         } else {
371                             int end = Integer.parseInt(end_s);
372                             for(int j=start; j<=end; j++) ids.addElement(new Integer(j));
373                         }
374                     } else {
375                         ids.addElement(new Integer(Integer.parseInt(s)));
376                         ids.addElement(new Integer(Integer.parseInt(s)));
377                     }
378                 }
379                 int[] ret = new int[ids.size()];
380                 for(int i=0; i<ret.length; i++) ret[i] = ((Integer)ids.elementAt(i)).intValue();
381                 return ret;
382             }
383             public Date date() {
384                 if (type != QUOTED && type != ATOM) throw new Bad("Expected quoted or unquoted date");
385                 try { return new SimpleDateFormat("dd-MMM-yyyy").parse(s);
386                 } catch (ParseException p) { throw new Bad("invalid date format; " + p); }
387             }
388             public Date datetime() {
389                 if (type != QUOTED && type != ATOM) throw new Bad("Expected quoted or unquoted datetime");
390                 try { return new SimpleDateFormat("dd-MM-yyyy hh:mm:ss").parse(s.trim());
391                 } catch (ParseException p) { throw new Bad("invalid datetime format " + s + " : " + p); }
392             }
393             public String nstring() {
394                 if (type == NIL) return null;
395                 if (type == QUOTED) return s;
396                 throw new Bad("expected NIL or string");
397             }
398             public String astring() {
399                 if (type == ATOM) return s;
400                 if (type == QUOTED) return s;
401                 throw new Bad("expected atom or string");
402             }
403             public String atom() {
404                 if (type != ATOM) throw new Bad("expected atom");
405                 for(int i=0; i<s.length(); i++) {
406                     char c = s.charAt(i);
407                     if (c == '(' || c == ')' || c == '{' || c == ' ' || c == '%' || c == '*')
408                         throw new Bad("invalid char in atom: " + c);
409                 }
410                 return s;
411             }
412             public Mailbox mailbox() {
413                 if (type == BAREWORD && s.toLowerCase().equals("inbox")) return getMailbox("INBOX", false);
414                 return getMailbox(astring(), false);
415             }
416
417         }
418
419         public char getc() throws IOException {
420             int ret = r.read();
421             if (ret == -1) throw new EOFException();
422             System.err.print((char)ret);
423             System.err.flush();
424             return (char)ret;
425         }
426         public  char peekc() throws IOException {
427             int ret = r.read();
428             if (ret == -1) throw new EOFException();
429             r.unread(ret);
430             return (char)ret;
431         }
432         public  void fill(byte[] b) throws IOException {
433             int num = 0;
434             while (num < b.length) {
435                 int numread = is.read(b, num, b.length - num);
436                 if (numread == -1) throw new EOFException();
437                 num += numread;
438             }
439         }
440
441         public void newline() {
442             try {
443                 for(char c = peekc(); c == ' ';) { getc(); c = peekc(); };
444                 for(char c = peekc(); c == '\r' || c == '\n';) { getc(); c = peekc(); };
445             } catch (IOException e) {
446                 e.printStackTrace();
447             }
448         }
449
450         public Token token() {
451             try {
452                 Vec toks = new Vec();
453                 StringBuffer sb = new StringBuffer();
454                 char c = getc(); while (c == ' ') c = getc();
455                 if (c == '\r' || c == '\n') {
456                     throw new Bad("unexpected end of line");
457                 } if (c == '{') {
458                     while(peekc() != '}') sb.append(getc());
459                     int octets = Integer.parseInt(sb.toString());
460                     while(peekc() == ' ') getc();   // whitespace
461                     while (getc() != '\n' && getc() != '\r') { }
462                     byte[] bytes = new byte[octets];
463                     fill(bytes);
464                     return new Token(new String(bytes), true);
465                 } else if (c == '\"') {
466                     while(true) {
467                         c = getc();
468                         if (c == '\\') sb.append(getc());
469                         else if (c == '\"') break;
470                         else sb.append(c);
471                     }
472                     return new Token(sb.toString(), true);
473
474                     // NOTE: this is technically a violation of the IMAP grammar, since atoms like FOO[BAR should be legal
475                 } else if (c == ']' || c == ')') { return null;
476                 } else if (c == '[' || c == '(') {
477                     Token t;
478                     do { t = token(); if (t != null) toks.addElement(t); } while (t != null);
479                     Token[] ret = new Token[toks.size()];
480                     toks.copyInto(ret);
481                     return new Token(ret);
482
483                 } else while(true) {
484                     sb.append(c);
485                     c = peekc();
486                     if (c == ' ' || c == '\"' || c == '(' || c == ')' || c == '[' || c == ']' ||
487                         c == '{' || c == '\n' || c == '\r')
488                         return new Token(sb.toString(), false);
489                     getc();
490                 }
491             } catch (IOException e) {
492                 e.printStackTrace();
493                 return null;
494             }
495         }
496
497         public final FetchRequest BODYSTRUCTURE = new FetchRequest();
498         public final FetchRequest ENVELOPE      = new FetchRequest();
499         public final FetchRequest FLAGS         = new FetchRequest();
500         public final FetchRequest INTERNALDATE  = new FetchRequest();
501         public final FetchRequest RFC822        = new FetchRequest();
502         public final FetchRequest RFC822HEADER  = new FetchRequest();
503         public final FetchRequest RFC822SIZE    = new FetchRequest();
504         public final FetchRequest RFC822TEXT    = new FetchRequest();
505         public final FetchRequest UID           = new FetchRequest();
506         public final FetchRequest BODY          = new FetchRequest();
507         public final FetchRequest BODYPEEK      = new FetchRequest();
508         public class FetchRequest { }
509         public class FetchRequestBody extends FetchRequest {
510             int type = 0;
511             boolean peek = false;
512             int[] path = null;
513             int start = -1;
514             int end = -1;
515             Token[] headers = null;
516             public static final int PATH = 0;
517             public static final int TEXT = 1;
518             public static final int MIME = 2;
519             public static final int HEADER = 3;
520             public static final int FIELDS = 4;
521             public static final int FIELDS_NOT= 5;
522         }
523
524         public FetchRequest[] parseFetch(Token[] t) {
525             FetchRequest[] ret = new FetchRequest[t.length];
526             for(int i=0; i<t.length; i++) {
527                 if (t[i] == null) continue;
528                 if (t[i].s.equalsIgnoreCase("BODYSTRUCTURE"))      ret[i] = BODYSTRUCTURE;
529                 else if (t[i].s.equalsIgnoreCase("ENVELOPE"))      ret[i] = ENVELOPE;
530                 else if (t[i].s.equalsIgnoreCase("FLAGS"))         ret[i] = FLAGS;
531                 else if (t[i].s.equalsIgnoreCase("INTERNALDATE"))  ret[i] = INTERNALDATE;
532                 else if (t[i].s.equalsIgnoreCase("RFC822"))        ret[i] = RFC822;
533                 else if (t[i].s.equalsIgnoreCase("RFC822.HEADER")) ret[i] = RFC822HEADER;
534                 else if (t[i].s.equalsIgnoreCase("RFC822.SIZE"))   ret[i] = RFC822SIZE;
535                 else if (t[i].s.equalsIgnoreCase("RFC822.TEXT"))   ret[i] = RFC822TEXT;
536                 else if (t[i].s.equalsIgnoreCase("UID"))           ret[i] = UID;
537                 else {
538                     FetchRequestBody b = new FetchRequestBody();
539                     String s = t[i].s.toUpperCase();
540
541                     String startend = null;
542                     if (s.indexOf('<') != -1 && s.endsWith(">")) {
543                         startend = s.substring(s.indexOf('<'), s.indexOf('>'));
544                         s = s.substring(0, s.indexOf('<'));
545                     }
546
547                     if (s.equalsIgnoreCase("BODY.PEEK")) b.peek = true;
548                     else if (s.equalsIgnoreCase("BODY")) b.peek = false;
549                     else throw new No("unknown fetch argument: " + s);
550                     
551                     if (i<t.length - 1 && (t[i+1].type == t[i].LIST)) {
552                         i++;
553                         Token[] t2 = t[i].l();
554                         if (t2.length == 0) {
555                             b.type = b.TEXT;
556                         } else {
557                             String s2 = t2[0].s.toUpperCase();
558                             Vec mimeVec = new Vec();
559                             while(s2.charAt(0) >= '0' && s2.charAt(0) <= '9') {
560                                 mimeVec.addElement(new Integer(Integer.parseInt(s2.substring(0, s2.indexOf('.')))));
561                                 s2 = s2.substring(s2.indexOf('.') + 1);
562                             }
563                             if (mimeVec.size() > 0) {
564                                 b.path = new int[mimeVec.size()];
565                                 for(int j=0; j<b.path.length; j++) b.path[j] = ((Integer)mimeVec.elementAt(j)).intValue();
566                             }
567                             if (s2.equals(""))                       { b.type = b.PATH; }
568                             else if (s2.equals("HEADER"))            { b.type = b.HEADER; }
569                             else if (s2.equals("HEADER.FIELDS"))     { b.type = b.FIELDS; b.headers = t2[1].l(); }
570                             else if (s2.equals("HEADER.FIELDS.NOT")) { b.type = b.FIELDS_NOT; b.headers = t2[1].l(); }
571                             else if (s2.equals("MIME"))              { b.type = b.MIME; }
572                             else if (s2.equals("TEXT"))              { b.type = b.TEXT; }
573                         }
574                     }
575
576                     if (i<t.length - 1 && (t[i+1].s.startsWith("<"))) {
577                         i++;
578                         startend = t[i].s.substring(1, t[i].s.length() - 1);
579                     }
580
581                     if (startend != null) {
582                         if (startend.indexOf('.') == -1) {
583                             b.start = Integer.parseInt(startend);
584                             b.end = -1;
585                         } else {
586                             b.start = Integer.parseInt(startend.substring(0, startend.indexOf('.')));
587                             b.end = Integer.parseInt(startend.substring(startend.indexOf('.') + 1));
588                         }
589                     }
590                     ret[i] = b;
591                 }
592             }
593             return ret;
594         }
595         
596     }
597
598
599     // Unparsing (printing) logic //////////////////////////////////////////////////////////////////////////////
600
601     static String quotify(String s){return s==null?"NIL":"\""+s.replaceAll("\\\\","\\\\").replaceAll("\"","\\\\\"")+"\"";}
602     static String quotify(Date d) { return new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss +zzzz").format(d); }
603     static String address(Address a) {return"("+quotify(a.description)+" NIL "+quotify(a.user)+" "+quotify(a.host)+")"; }
604     public static String addressList(Object a) {
605         if (a == null) return "NIL";
606         if (a instanceof Address) return "("+address((Address)a)+")";
607         Address[] aa = (Address[])a;
608         StringBuffer ret = new StringBuffer();
609         ret.append("(");
610         for(int i=0; i<aa.length; i++) { ret.append(aa[i]); if (i < aa.length - 1) ret.append(" "); }
611         ret.append(")");
612         return ret.toString();
613     }
614     static String flags(Mailbox.Iterator it) {
615         return 
616             (it.deleted()  ? "\\Deleted "  : "") +
617             (it.seen()     ? "\\Seen "     : "") +
618             (it.flagged()  ? "\\Flagged "  : "") +
619             (it.draft()    ? "\\Draft "    : "") +
620             (it.answered() ? "\\Answered " : "") +
621             (it.recent()   ? "\\Recent "   : "");
622     }
623     static String envelope(Message m) {
624         return
625             "(" + quotify(m.arrival.toString()) +
626             " " + quotify(m.subject) +          
627             " " + addressList(m.from) +      
628             " " + addressList(m.headers.get("sender")) +
629             " " + addressList(m.replyto) + 
630             " " + addressList(m.to) + 
631             " " + addressList(m.cc) + 
632             " " + addressList(m.bcc) + 
633             " " + quotify((String)m.headers.get("in-reply-to")) +
634             " " + quotify(m.messageid) +
635             ")";
636     }
637
638     public static String qq(String s) {
639         StringBuffer ret = new StringBuffer(s.length() + 20);
640         ret.append('{');
641         ret.append(s.length());
642         ret.append('}');
643         ret.append('\r');
644         ret.append('\n');
645         ret.append(s);
646         ret.append('\r');
647         ret.append('\n');
648         return ret.toString();
649     }
650     
651     private static String join(String delimit, String[] stuff) {
652         StringBuffer ret = new StringBuffer();
653         for(int i=0; i<stuff.length; i++) {
654             ret.append(stuff[i]);
655             if (i<stuff.length - 1) ret.append(delimit);
656         }
657         return ret.toString();
658     }
659
660
661     // Command Switchblock //////////////////////////////////////////////////////////////////////////////
662
663     private static final Hashtable commands = new Hashtable();
664     private static final int UID = 0;          static { commands.put("UID", new Integer(UID)); }
665     private static final int AUTHENTICATE = 1; static { commands.put("AUTHENTICATE", new Integer(AUTHENTICATE)); }
666     private static final int LIST = 2;         static { commands.put("LIST", new Integer(LIST)); }
667     private static final int LSUB = 3;         static { commands.put("LSUB", new Integer(LSUB)); }
668     private static final int SUBSCRIBE = 4;    static { commands.put("SUBSCRIBE", new Integer(SUBSCRIBE)); }
669     private static final int UNSUBSCRIBE = 5;  static { commands.put("UNSUBSCRIBE", new Integer(UNSUBSCRIBE)); }
670     private static final int CAPABILITY = 6;   static { commands.put("CAPABILITY", new Integer(CAPABILITY)); }
671     private static final int ID = 7;           static { commands.put("ID", new Integer(ID)); }
672     private static final int LOGIN = 8;        static { commands.put("LOGIN", new Integer(LOGIN)); }
673     private static final int LOGOUT = 9;       static { commands.put("LOGOUT", new Integer(LOGOUT)); }
674     private static final int RENAME = 10;      static { commands.put("RENAME", new Integer(RENAME)); }
675     private static final int EXAMINE = 11;     static { commands.put("EXAMINE", new Integer(EXAMINE)); }
676     private static final int SELECT = 12;      static { commands.put("SELECT", new Integer(SELECT)); }
677     private static final int COPY = 13;        static { commands.put("COPY", new Integer(COPY)); }
678     private static final int DELETE = 14;      static { commands.put("DELETE", new Integer(DELETE)); }
679     private static final int CHECK = 15;       static { commands.put("CHECK", new Integer(CHECK)); }
680     private static final int NOOP = 16;        static { commands.put("NOOP", new Integer(NOOP)); }
681     private static final int CLOSE = 17;       static { commands.put("CLOSE", new Integer(CLOSE)); }
682     private static final int EXPUNGE = 18;     static { commands.put("EXPUNGE", new Integer(EXPUNGE)); }
683     private static final int UNSELECT = 19;    static { commands.put("UNSELECT", new Integer(UNSELECT)); }
684     private static final int CREATE = 20;      static { commands.put("CREATE", new Integer(CREATE)); }
685     private static final int STATUS = 21;      static { commands.put("STATUS", new Integer(STATUS)); }
686     private static final int FETCH = 22;       static { commands.put("FETCH", new Integer(FETCH)); }
687     private static final int APPEND = 23;      static { commands.put("APPEND", new Integer(APPEND)); }
688     private static final int STORE = 24;       static { commands.put("STORE", new Integer(STORE)); }
689
690
691     // Main //////////////////////////////////////////////////////////////////////////////
692
693     /** simple listener for testing purposes */
694     public static void main(String[] args) throws Exception {
695         ServerSocket ss = new ServerSocket(143);
696         for(final Socket s = ss.accept();;)
697             new Thread() { public void run() { try {
698                 final Mailbox root = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT+File.separatorChar+"imap", true);
699                 new Session(s, root,
700                             new Authenticator() {
701                                 public Mailbox authenticate(String u, String p) {
702                                     if (u.equals("megacz")&&p.equals("pass")) return root.slash("users",true).slash("megacz",true);
703                                     return null;
704                                 } } ).handleRequest();
705             } catch (Exception e) { e.printStackTrace(); } } }.start();
706     }
707 }