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