1 package org.ibex.mail.protocol;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.target.*;
10 // FEATURE: hoist all the ok()'s?
11 // FEATURE: pipelining
12 // FEATURE: support [charset]
13 public class IMAP extends MessageProtocol {
15 public static final char imapSeparator = '.';
17 public static void main(String[] args) throws Exception {
18 ServerSocket ss = new ServerSocket(143);
20 System.out.println("listening");
21 final Socket s = ss.accept();
22 System.out.println("connected");
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) {
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); } }
44 private static class Listener extends Incoming {
45 Mailbox selected = null;
53 public void init() { }
54 public Listener(Socket conn, String vhost, Mailbox root, Mailbox inbox) throws IOException {
60 this.pw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
61 this.is = conn.getInputStream();
62 this.r = new PushbackReader(new InputStreamReader(is));
65 private void star(String s) {
67 System.err.println("* " + s);
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) {
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);
85 private boolean auth(String user, String pass) { /* FEATURE */ return user.equals("megacz") && pass.equals("pass"); }
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;
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])+"\"");
100 } else if (pre.length() == 0) {
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));
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);
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() { }
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);
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();
152 star("STATUS " + /* FIXME m.getName() +*/ " (" + response + ")");
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
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();
175 public void append(Mailbox m, Token t) {
176 Token[] flags = 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 */ }
185 public static String qq(String s) {
186 StringBuffer ret = new StringBuffer(s.length() + 20);
188 ret.append(s.length());
195 return ret.toString();
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();
220 // FIXME obey path[] here
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");
234 case FetchRequestBody.FIELDS_NOT: throw new Exn.Bad("FETCH HEADERS.NOT not supported");
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);
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) != '-');
261 selected.add(m); // re-add (?)
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");
274 // FIXME better error if atom() fails
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());
311 // Unparsing (printing) logic //////////////////////////////////////////////////////////////////////////////
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();
322 for(int i=0; i<aa.length; i++) { ret.append(aa[i]); if (i < aa.length - 1) ret.append(" "); }
324 return ret.toString();
326 static String flags(Mailbox.Iterator it) {
328 (it.deleted() ? "\\Deleted " : "") +
329 (it.seen() ? "\\Seen " : "") +
330 (it.flagged() ? "\\Flagged " : "") +
331 (it.draft() ? "\\Draft " : "") +
332 (it.answered() ? "\\Answered " : "") +
333 (it.recent() ? "\\Recent " : "");
335 static String envelope(Message m) {
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) +
352 // Parsing Logic //////////////////////////////////////////////////////////////////////////////
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());
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());
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());
400 public final String s;
401 public final Token[] l;
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; }
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");
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
424 public int n() throws Exn.Bad {
425 if (type != NUMBER) throw new Exn.Bad("expected number");
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");
433 public String q() throws Exn.Bad {
434 if (type == NIL) return null;
435 if (type != QUOTED) throw new Exn.Bad("expected qstring");
438 public Token[] l() throws Exn.Bad {
439 if (type == NIL) return null;
440 if (type != LIST) throw new Exn.Bad("expected parenthesized list");
443 public int[] set() throws Exn.Bad {
444 if (type != ATOM) throw new Exn.Bad("expected a messageid set");
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));
456 int end = Integer.parseInt(end_s);
457 for(int j=start; j<=end; j++) ids.addElement(new Integer(j));
460 ids.addElement(new Integer(Integer.parseInt(s)));
461 ids.addElement(new Integer(Integer.parseInt(s)));
464 int[] ret = new int[ids.size()];
465 for(int i=0; i<ret.length; i++) ret[i] = ((Integer)ids.elementAt(i)).intValue();
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); }
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); }
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");
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");
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);
497 public Mailbox mailbox() throws Exn.Bad {
498 if (type == BAREWORD && s.toLowerCase().equals("inbox")) return getMailbox("INBOX", false);
499 return getMailbox(astring(), false);
504 public char getc() throws IOException {
506 if (ret == -1) throw new EOFException();
507 System.err.print((char)ret);
511 public char peekc() throws IOException {
513 if (ret == -1) throw new EOFException();
517 public void fill(byte[] b) throws IOException {
519 while (num < b.length) {
520 int numread = is.read(b, num, b.length - num);
521 if (numread == -1) throw new EOFException();
526 public void newline() {
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) {
535 public Token token() {
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");
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];
549 return new Token(new String(bytes), true);
550 } else if (c == '\"') {
553 if (c == '\\') sb.append(getc());
554 else if (c == '\"') break;
557 return new Token(sb.toString(), true);
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 == '(') {
563 do { t = token(); if (t != null) toks.addElement(t); } while (t != null);
564 Token[] ret = new Token[toks.size()];
566 return new Token(ret); // FIXME
571 if (c == ' ' || c == '\"' || c == '(' || c == ')' || c == '[' || c == ']' ||
572 c == '{' || c == '\n' || c == '\r')
573 return new Token(sb.toString(), false);
576 } catch (IOException e) {
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(); }
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 {
610 public class FetchRequestBody extends FetchRequest {
612 boolean peek = false;
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;
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;
639 FetchRequestBody b = new FetchRequestBody();
640 String s = t[i].s.toUpperCase();
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('<'));
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);
652 if (i<t.length - 1 && (t[i+1].type == t[i].LIST)) {
654 Token[] t2 = t[i].l();
655 if (t2.length == 0) {
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);
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();
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; }
677 if (i<t.length - 1 && (t[i+1].s.startsWith("<"))) {
679 startend = t[i].s.substring(1, t[i].s.length() - 1);
682 if (startend != null) {
683 if (startend.indexOf('.') == -1) {
684 b.start = Integer.parseInt(startend);
687 b.start = Integer.parseInt(startend.substring(0, startend.indexOf('.')));
688 b.end = Integer.parseInt(startend.substring(startend.indexOf('.') + 1));