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 // FEATURE: manage subscriptions
88 public void lsub(Mailbox m, String s) {
89 star("LSUB () \".\" INBOX");
93 public void list(Mailbox m, String s) {
94 //star("LIST () \".\" INBOX");
95 star("LIST () \".\" users.megacz.imap");
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() { }
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);
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();
126 star("STATUS " + /* FIXME m.getName() +*/ " (" + response + ")");
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
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();
149 public void append(Mailbox m, Token t) {
150 Token[] flags = 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 */ }
159 public static String qq(String s) {
160 StringBuffer ret = new StringBuffer(s.length() + 20);
162 ret.append(s.length());
169 return ret.toString();
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();
194 // FIXME obey path[] here
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");
208 case FetchRequestBody.FIELDS_NOT: throw new Exn.Bad("FETCH HEADERS.NOT not supported");
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);
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) != '-');
235 selected.add(m); // re-add (?)
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");
248 // FIXME better error if atom() fails
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());
285 // Unparsing (printing) logic //////////////////////////////////////////////////////////////////////////////
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();
296 for(int i=0; i<aa.length; i++) { ret.append(aa[i]); if (i < aa.length - 1) ret.append(" "); }
298 return ret.toString();
300 static String flags(Mailbox.Iterator it) {
302 (it.deleted() ? "\\Deleted " : "") +
303 (it.seen() ? "\\Seen " : "") +
304 (it.flagged() ? "\\Flagged " : "") +
305 (it.draft() ? "\\Draft " : "") +
306 (it.answered() ? "\\Answered " : "") +
307 (it.recent() ? "\\Recent " : "");
309 static String envelope(Message m) {
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) +
326 // Parsing Logic //////////////////////////////////////////////////////////////////////////////
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());
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());
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());
374 public final String s;
375 public final Token[] l;
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; }
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");
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
398 public int n() throws Exn.Bad {
399 if (type != NUMBER) throw new Exn.Bad("expected number");
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");
407 public String q() throws Exn.Bad {
408 if (type == NIL) return null;
409 if (type != QUOTED) throw new Exn.Bad("expected qstring");
412 public Token[] l() throws Exn.Bad {
413 if (type == NIL) return null;
414 if (type != LIST) throw new Exn.Bad("expected parenthesized list");
417 public int[] set() throws Exn.Bad {
418 if (type != ATOM) throw new Exn.Bad("expected a messageid set");
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));
430 int end = Integer.parseInt(end_s);
431 for(int j=start; j<=end; j++) ids.addElement(new Integer(j));
434 ids.addElement(new Integer(Integer.parseInt(s)));
435 ids.addElement(new Integer(Integer.parseInt(s)));
438 int[] ret = new int[ids.size()];
439 for(int i=0; i<ret.length; i++) ret[i] = ((Integer)ids.elementAt(i)).intValue();
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); }
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); }
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");
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");
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);
471 public Mailbox mailbox() throws Exn.Bad {
472 if (type == BAREWORD && s.toLowerCase().equals("inbox")) return getMailbox("INBOX", false);
473 return getMailbox(astring(), false);
478 public char getc() throws IOException {
480 if (ret == -1) throw new EOFException();
481 System.err.print((char)ret);
485 public char peekc() throws IOException {
487 if (ret == -1) throw new EOFException();
491 public void fill(byte[] b) throws IOException {
493 while (num < b.length) {
494 int numread = is.read(b, num, b.length - num);
495 if (numread == -1) throw new EOFException();
500 public void newline() {
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) {
509 public Token token() {
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");
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];
523 return new Token(new String(bytes), true);
524 } else if (c == '\"') {
527 if (c == '\\') sb.append(getc());
528 else if (c == '\"') break;
531 return new Token(sb.toString(), true);
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 == '(') {
537 do { t = token(); if (t != null) toks.addElement(t); } while (t != null);
538 Token[] ret = new Token[toks.size()];
540 return new Token(ret); // FIXME
545 if (c == ' ' || c == '\"' || c == '(' || c == ')' || c == '[' || c == ']' ||
546 c == '{' || c == '\n' || c == '\r')
547 return new Token(sb.toString(), false);
550 } catch (IOException e) {
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(); }
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 {
584 public class FetchRequestBody extends FetchRequest {
586 boolean peek = false;
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;
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;
613 FetchRequestBody b = new FetchRequestBody();
614 String s = t[i].s.toUpperCase();
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('<'));
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);
626 if (i<t.length - 1 && (t[i+1].type == t[i].LIST)) {
628 Token[] t2 = t[i].l();
629 if (t2.length == 0) {
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);
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();
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; }
651 if (i<t.length - 1 && (t[i+1].s.startsWith("<"))) {
653 startend = t[i].s.substring(1, t[i].s.length() - 1);
656 if (startend != null) {
657 if (startend.indexOf('.') == -1) {
658 b.start = Integer.parseInt(startend);
661 b.start = Integer.parseInt(startend.substring(0, startend.indexOf('.')));
662 b.end = Integer.parseInt(startend.substring(startend.indexOf('.') + 1));