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