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