import java.text.*;
import java.io.*;
-// FEATURE: hoist all the ok()'s?
+// Relevant RFC's:
+// RFC 2060: IMAPv4
+// RFC 3501: IMAPv4 with clarifications
+// RFC 3691: UNSELECT
+// RFC 2971: ID
+
+// FEATURE: READ-WRITE / READ-ONLY status on SELECT
// FEATURE: pipelining
// FEATURE: support [charset]
+// FEATURE: \Noselect
+// FEATURE: subscriptions
public class IMAP extends MessageProtocol {
+ // Constants //////////////////////////////////////////////////////////////////////////////
+
public static final char imapSeparator = '.';
+ public static final float version = (float)0.1;
- public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(143);
- while(true) {
- System.out.println("listening");
- final Socket s = ss.accept();
- System.out.println("connected");
- new Thread() {
- public void run() {
- try {
- Mailbox root =
- FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + File.separatorChar + "imap", true);
- Mailbox inbox = root.slash("users", true).slash("megacz", true);
- new Listener(s, "megacz.com", root, inbox).handleRequest();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }.start();
- }
- }
- public static class Exn extends MailException {
- public Exn(String s) { super(s); }
- public static class No extends Exn { public No(String s) { super(s); } }
- public static class Bad extends Exn { public Bad(String s) { super(s); } }
- }
+ // Callbacks //////////////////////////////////////////////////////////////////////////////
+
+ public static interface Authenticator { public abstract Mailbox authenticate(String user, String pass); }
+
+
+ // Exceptions //////////////////////////////////////////////////////////////////////////////
- private static class Listener extends Incoming {
+ public static class Exn extends MailException { public Exn(String s) { super(s); } }
+ public static class Bad extends Exn { public Bad(String s) { super(s); } }
+ public static class No extends Exn { public No(String s) { super(s); } }
+
+
+ // Single Session Handler //////////////////////////////////////////////////////////////////////////////
+
+ private static class Session extends Incoming {
Mailbox selected = null;
- Mailbox root;
- Mailbox inbox;
- Socket conn;
- String vhost;
- PrintWriter pw;
- InputStream is;
- PushbackReader r;
+ Mailbox selected() { if (selected == null) throw new Bad("no mailbox selected"); return selected; }
+ String selectedName = null;
+ Mailbox inbox = null;
+ final Mailbox root;
+ final Socket conn;
+ final String vhost;
+ final Authenticator auth;
+ final PrintWriter pw;
+ final PushbackReader r;
+ final InputStream is;
public void init() { }
- public Listener(Socket conn, String vhost, Mailbox root, Mailbox inbox) throws IOException {
- this.vhost = vhost;
- this.conn = conn;
- this.selected = null;
- this.root = root;
- this.inbox = inbox;
+ public Session(Socket conn, Mailbox root, Authenticator auth) throws IOException {
+ this(conn, java.net.InetAddress.getLocalHost().getHostName(), root, auth); }
+ public Session(Socket conn, String vhost, Mailbox root, Authenticator auth) throws IOException {
+ this.vhost = vhost; this.conn = conn; this.root = root; this.auth = auth;
this.pw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
- this.is = conn.getInputStream();
- this.r = new PushbackReader(new InputStreamReader(is));
+ this.r = new PushbackReader(new InputStreamReader(this.is = conn.getInputStream()));
}
- private void star(String s) {
- pw.println("* " + s);
- System.err.println("* " + s);
- pw.flush();
- }
+ private void star(String s) { println("* " + s); }
+ private void println(String s) { pw.println(s); pw.flush(); }
+ private void print(String s) { pw.print(s); }
- // FIXME: user-inbox-relative stuff
- // FIXME should throw a No if mailbox not found and create==false
private Mailbox getMailbox(String name, boolean create) {
Mailbox m = root;
- while(name.length() > 0) {
- int end = name.length();
- if (name.indexOf(imapSeparator) != -1) end = name.indexOf(imapSeparator);
- m = m.slash(name.substring(0, end), create);
- if (end == name.length()) break;
- name = name.substring(end+1);
- }
+ for(StringTokenizer st = new StringTokenizer(name, imapSeparator + ""); st.hasMoreTokens();)
+ if ((m = m.slash(st.nextToken(), create)) == null) throw new No("no such mailbox " + name);
return m;
}
- private boolean auth(String user, String pass) { /* FEATURE */ return user.equals("megacz") && pass.equals("pass"); }
-
- public void lsub(String start, String ref) { list(start, ref); } // FEATURE: manage subscriptions
- public void list(String start, String ref) {
- if (ref.length() == 0) { star("LIST () \".\" \"\""); return; }
- if (start.startsWith(".")) return;
+ // FIXME: not accurate when a wildcard and subsequent non-wildcards both match a single component
+ public HashSet lsub(String start, String ref, HashSet reply) { return list(start, ref, reply); }
+ public HashSet list(String start, String ref, HashSet reply) {
+ if (reply == null) reply = new HashSet();
+ if (ref.length() == 0 && start.length() == 0) { reply.add(""); return reply; }
+ while (start.endsWith(""+imapSeparator)) start = start.substring(0, start.length() - 1);
String[] children = (start.length() == 0 ? root : getMailbox(start, false)).children();
for(int i=0; i<children.length; i++) {
- String s = children[i], pre = ref;
- boolean stop = true;
+ String s = children[i], pre = ref, kid = start + (start.length() > 0 ? imapSeparator+"" : "") + s;
while(true) {
- int percent = pre.indexOf('%'), star = pre.indexOf('*'), dot = pre.indexOf('.');
- if (s.length() == 0 && pre.length() == 0) {
- star("LIST () \".\" \""+(start + (start.length() > 0 ? "." : "") + children[i])+"\"");
- break;
- } else if (pre.length() == 0) {
- break;
- } else if (percent != 0 && star != 0 && dot != 0) {
- if (s.length() == 0 || s.charAt(0) != pre.charAt(0)) break;
- s = s.substring(1); pre = pre.substring(1);
- } else if (pre.charAt(0) == '.') {
- if (s.length() == 0) list(start + (start.length() > 0 ? "." : "") + children[i], pre.substring(1));
- break;
- } else if (pre.charAt(0) == '%') {
- star("LIST () \".\" \""+(start + (start.length() > 0 ? "." : "") + children[i])+"\"");
- list(start + (start.length() > 0 ? "." : "") + children[i], pre.substring(1));
- // FIXME this isn't right
- pre = pre.substring(1);
- } else if (pre.charAt(0) == '*') {
- // FIXME need to iterate
- star("LIST () \".\" \""+(start + (start.length() > 0 ? "." : "") + children[i])+"\"");
- list(start + (start.length() > 0 ? "." : "") + children[i], pre);
- pre = pre.substring(1);
- }
+ if (pre.length() == 0) {
+ if (s.length() == 0) reply.add(kid);
+ } else switch(pre.charAt(0)) {
+ case imapSeparator: if (s.length() == 0) list(kid, pre.substring(1), reply); break;
+ case '%': reply.add(kid); pre=pre.substring(1); list(kid, pre, reply); break;
+ case '*': reply.add(kid); list(kid, pre, reply); pre = pre.substring(1); break;
+ default: if (s.length()==0) break;
+ if (s.charAt(0) != pre.charAt(0)) break;
+ s = s.substring(1); pre = pre.substring(1); continue;
+ }
+ break;
}
}
- // FIXME: \Noselect
+ return reply;
}
- public void copy(final Query q, Mailbox to) { for(Mailbox.Iterator it=selected.iterator(q);it.next();) to.add(it.cur()); }
- public void login(String user, String pass) { if (!auth(user, pass)) throw new Exn.No("Liar, liar, pants on fire."); }
- public void capability() { star("CAPABILITY IMAP4rev1"); }
- public void logout() { star("BYE LOGOUT received"); }
+ public String[] capability() { return new String[] { "IMAP4rev1", "UNSELECT", "ID" }; }
+ public Hashtable id(Hashtable clientId) {
+ Hashtable response = new Hashtable();
+ response.put("name", IMAP.class.getName());
+ response.put("version", version + "");
+ response.put("os", System.getProperty("os.name", null));
+ response.put("os-version", System.getProperty("os.version", null));
+ response.put("vendor", "none");
+ response.put("support-url", "http://mail.ibex.org/");
+ return response;
+ }
+
+ public void copy(Query q, Mailbox to) { for(Mailbox.Iterator it=selected.iterator(q);it.next();) to.add(it.cur()); }
+ public void login(String u, String p) { if ((inbox = auth.authenticate(u,p)) == null) throw new No("Login failed."); }
+ public void logout() { }
+ public void unselect() { selected = null; selectedName = null; }
public void delete(Mailbox m) { if (!m.equals(inbox)) m.destroy(); }
- public void create(String mailbox){if(!mailbox.endsWith(".")&&!mailbox.equalsIgnoreCase("inbox"))getMailbox(mailbox,true);}
- public void close(boolean examineOnly) { expunge(false, true); selected = null; }
+ public void create(String m) { if (!m.endsWith(""+imapSeparator) && !m.equalsIgnoreCase("inbox")) getMailbox(m, true); }
+ public void append(Mailbox m, int flags, Date arrival, String body) { m.add(new Message(null,null,body,arrival), flags); }
public void check() { }
public void noop() { }
-
+ public void close() { for(Mailbox.Iterator it=selected().iterator(Query.deleted()); it.next();) it.delete(); unselect(); }
+ public void expunge() { for(Mailbox.Iterator it = selected().iterator(Query.deleted()); it.next();) expunge(it); }
+ public void expunge(Mailbox.Iterator it) { star(it.uid() + " EXPUNGE"); it.delete(); }
+ public void subscribe(String mailbox) { }
+ public void unsubscribe(String mailbox) { }
public void rename(Mailbox from, String to) {
- if (from.equals(inbox)) from.copy(Query.all(), getMailbox(to, true));
+ if (from.equals(inbox)) from.copy(Query.all(), getMailbox(to, true));
else if (to.equalsIgnoreCase("inbox")) { from.copy(Query.all(), getMailbox(to, true)); from.destroy(); }
else from.rename(to);
}
- public void status(final Mailbox m, Token[] attrs) {
- int count0 = 0, count1 = 0, count2 = 0;
- for(Mailbox.Iterator it = m.iterator(); it.next(); ) { if (!it.seen()) count0++; if (it.recent()) count1++; count2++; }
+ // FIXME lift out unparsing
+ public void status(Mailbox m, Token[] attrs) {
+ int seen = 0, recent = 0, messages = 0;
+ for(Mailbox.Iterator it = m.iterator(); it.next(); ) { if (!it.seen()) seen++; if (it.recent()) recent++; messages++; }
String response = "";
for(int i=0; i<attrs.length; i++) {
String s = attrs[i].atom();
- if (s.equals("MESSAGES")) response += "MESSAGES " + count2;
- if (s.equals("RECENT")) response += "RECENT " + count0;
- if (s.equals("UIDNEXT")) response += "UNSEEN " + count1;
+ if (s.equals("MESSAGES")) response += "MESSAGES " + messages;
+ if (s.equals("RECENT")) response += "RECENT " + seen;
+ if (s.equals("UIDNEXT")) response += "UNSEEN " + recent;
if (s.equals("UIDVALIDITY")) response += "UIDVALIDITY " + m.uidValidity();
if (s.equals("UNSEEN")) response += "UIDNEXT " + m.uidNext();
}
- star("STATUS " + /* FIXME m.getName() +*/ " (" + response + ")");
+ star("STATUS " + selectedName + " (" + response + ")");
}
+ // FIXME: we can omit UIDNEXT!
+ // FIXME use directory date/time as UIDNEXT and file date/time as UID; need to 'correct' file date/time after changes
public void select(String mailbox, boolean examineOnly) {
- selected = getMailbox(mailbox, false);
- if (selected == null) throw new Exn.No("no such mailbox"); // should this be 'Bad'?
- star(selected.count(Query.all()) + " EXISTS");
+ selected = getMailbox(selectedName = mailbox, false);
+ star("FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)");
+ star(selected.count(Query.all()) + " EXISTS");
star(selected.count(Query.recent()) + " RECENT");
- //star("OK [UNSEEN 12] Message 12 is first unseen"); FEATURE
star("OK [UIDVALIDITY " + selected.uidValidity() + "] UIDs valid");
- star("FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)");
- // FEATURE: READ-WRITE / READ-ONLY
- }
-
- public void expunge(final boolean examineOnly, final boolean silent) {
- if (selected == null) throw new Exn.Bad("no mailbox selected");
- for(Mailbox.Iterator it = selected.iterator(); it.next();) {
- if (!it.deleted()) return;
- if (!silent) star(it.uid() + " EXPUNGE");
- if (!examineOnly) it.delete();
- }
- }
-
- public void append(Mailbox m, Token t) {
- Token[] flags = null;
- Date arrival = null;
- if (t.type == t.LIST) { flags = t.l(); t = token(); }
- if (t.type == t.QUOTED) { arrival = t.datetime(); t = token(); }
- String literal = q();
- m.add(new Message(null, new Address[] { null }, new LineReader(new StringReader(literal))));
- if (flags != null) { /* FEATURE */ }
- }
-
- public static String qq(String s) {
- StringBuffer ret = new StringBuffer(s.length() + 20);
- ret.append('{');
- ret.append(s.length());
- ret.append('}');
- ret.append('\r');
- ret.append('\n');
- ret.append(s);
- ret.append('\r');
- ret.append('\n');
- return ret.toString();
}
+ // uber FIXME
public void fetch(Query q, FetchRequest[] fr, boolean uid) {
- System.err.println("query == " + q);
for(Mailbox.Iterator it = selected.iterator(q); it.next(); ) {
Message m = it.cur();
- System.err.println("message == " + m);
StringBuffer reply = new StringBuffer();
boolean peek = false;
for(int i=0; i<fr.length; i++)
if (fr[i] == null) continue;
- else if (fr[i] == BODYSTRUCTURE) throw new Exn.Bad("BODYSTRUCTURE not supported");
+ else if (fr[i] == BODYSTRUCTURE) throw new Bad("BODYSTRUCTURE not supported");
else if (fr[i] == ENVELOPE ) reply.append("ENVELOPE " + envelope(m) + " ");
else if (fr[i] == FLAGS ) reply.append("FLAGS (" + flags(it) + ") ");
else if (fr[i] == INTERNALDATE ) reply.append("INTERNALDATE " + quotify(m.arrival) + " ");
peek = frb.peek;
// FIXME obey path[] here
switch(frb.type) {
- case FetchRequestBody.PATH: case FetchRequestBody.TEXT:
+ case FetchRequestBody.PATH:
+ reply.append("BODY[] "); payload.append(m.body); break;
+ case FetchRequestBody.TEXT:
reply.append("BODY[TEXT] "); payload.append(m.body); break;
- case FetchRequestBody.MIME: throw new Exn.Bad("FETCH MIME not supported");
+ case FetchRequestBody.MIME: throw new Bad("FETCH MIME not supported");
case FetchRequestBody.HEADER: reply.append("BODY[HEADER] "); payload.append(m.allHeaders); break;
case FetchRequestBody.FIELDS:
reply.append("BODY[HEADER.FIELDS (");
}
reply.append(")] ");
break;
- case FetchRequestBody.FIELDS_NOT: throw new Exn.Bad("FETCH HEADERS.NOT not supported");
+ case FetchRequestBody.FIELDS_NOT: throw new Bad("FETCH HEADERS.NOT not supported");
}
if (frb.start == -1) reply.append(qq(payload.toString()));
else if (frb.start == -1) reply.append("<" + frb.end + ">" + qq(payload.substring(0, frb.end + 1)));
}
}
-
- // FEATURE: hoist the parsing here
- public void store(Query q, String what, Token[] flags) {
+ public void store(Query q, int addFlags, int removeFlags, boolean silent, boolean uid) {
for(Mailbox.Iterator it = selected.iterator(q); it.next(); ) {
- Message m = it.cur();
- if (what.charAt(0) == 'F') {
- it.deleted(false); it.seen(false); it.flagged(false); it.draft(false); it.answered(false); it.recent(false); }
- for(int j=0; j<flags.length; j++) {
- String flag = flags[j].flag();
- if (flag.equals("Deleted")) it.deleted(what.charAt(0) != '-');
- if (flag.equals("Seen")) it.seen(what.charAt(0) != '-');
- if (flag.equals("Flagged")) it.flagged(what.charAt(0) != '-');
- if (flag.equals("Draft")) it.draft(what.charAt(0) != '-');
- if (flag.equals("Answered")) it.answered(what.charAt(0) != '-');
- if (flag.equals("Recent")) it.recent( what.charAt(0) != '-');
- }
- selected.add(m); // re-add (?)
+ //it.addFlags(addFlags); FIXME
+ //it.removeFlags(removeFlags);
+ if (!silent) star((uid ? it.uid() : it.num()) + " FETCH (FLAGS (" + flags(it) + "))");
}
}
+ private void emitListResponse(HashSet boxes)
+ { for(Iterator it = boxes.iterator(); it.hasNext(); ) star("LIST \"" + imapSeparator + "\" \"" + it.next() + "\""); }
+
public boolean handleRequest() throws IOException {
- pw.println("* OK " + vhost + " " + IMAP.class.getName() + " IMAP4 v0.1 server ready");
- System.err.println("* OK " + vhost + " " + IMAP.class.getName() + " IMAP4 v0.1 server ready");
- pw.flush();
- while(true) {
- String tag = null;
- try {
- boolean uid = false;
- tag = null;
- // FIXME better error if atom() fails
- tag = atom();
- String command = atom();
- if (command.equalsIgnoreCase("UID")) { uid = true; command = atom(); }
- if (command.equalsIgnoreCase("AUTHENTICATE")) { login(astring(), astring()); }
- else if (command.equalsIgnoreCase("LIST")) list(q(), q());
- else if (command.equalsIgnoreCase("LSUB")) lsub(q(), q());
- else if (command.equalsIgnoreCase("SUBSCRIBE")) { /* FIXME */ }
- else if (command.equalsIgnoreCase("UNSUBSCRIBE")) { /* FIXME */ }
- else if (command.equalsIgnoreCase("CAPABILITY")) { capability(); }
- else if (command.equalsIgnoreCase("LOGIN")) login(astring(), astring());
- else if (command.equalsIgnoreCase("LOGOUT")) { logout(); conn.close(); return false; }
- else if (command.equalsIgnoreCase("RENAME")) rename(mailbox(), atom());
- else if (command.equalsIgnoreCase("APPEND")) append(mailbox(), token());
- else if (command.equalsIgnoreCase("EXAMINE")) select(astring(), true);
- else if (command.equalsIgnoreCase("SELECT")) select(astring(), false);
- else if (command.equalsIgnoreCase("COPY")) copy(Query.num(set()), mailbox());
- else if (command.equalsIgnoreCase("DELETE")) delete(mailbox());
- else if (command.equalsIgnoreCase("CHECK")) check();
- else if (command.equalsIgnoreCase("NOOP")) noop();
- else if (command.equalsIgnoreCase("CREATE")) create(astring());
- else if (command.equalsIgnoreCase("STORE")) store(Query.num(set()), atom(), l());
- else if (command.equalsIgnoreCase("FETCH")) fetch(uid ? Query.uid(set()) : Query.num(set()),
- parseFetchRequest(l()), uid);
- else if (command.equalsIgnoreCase("STATUS")) status(mailbox(), l());
- else throw new Exn.Bad("unrecognized command \"" + command + "\"");
- pw.println(tag + " OK " + command + " Completed.");
- System.err.println(tag + " OK " + command + " Completed.");
- } catch (Exn.Bad b) { pw.println(tag + " Bad " + b.toString()); System.err.println(tag + " Bad " + b.toString()); b.printStackTrace();
- } catch (Exn.No n) { pw.println(tag + " OK " + n.toString()); System.err.println(tag + " OK " + n.toString());
- }
+ println("* OK " + vhost + " " + IMAP.class.getName() + " IMAP4rev1 [RFC3501] v" + version + " server ready");
+ for(String tag = null;; newline()) try {
pw.flush();
- newline();
- }
- }
-
-
- // Unparsing (printing) logic //////////////////////////////////////////////////////////////////////////////
-
- static String quotify(String s){return s==null?"NIL":"\""+s.replaceAll("\\\\","\\\\").replaceAll("\"","\\\\\"")+"\"";}
- static String quotify(Date d) { return new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss +zzzz").format(d); }
- static String address(Address a) {return"("+quotify(a.description)+" NIL "+quotify(a.user)+" "+quotify(a.host)+")"; }
- public static String addressList(Object a) {
- if (a == null) return "NIL";
- if (a instanceof Address) return "("+address((Address)a)+")";
- Address[] aa = (Address[])a;
- StringBuffer ret = new StringBuffer();
- ret.append("(");
- for(int i=0; i<aa.length; i++) { ret.append(aa[i]); if (i < aa.length - 1) ret.append(" "); }
- ret.append(")");
- return ret.toString();
- }
- static String flags(Mailbox.Iterator it) {
- return
- (it.deleted() ? "\\Deleted " : "") +
- (it.seen() ? "\\Seen " : "") +
- (it.flagged() ? "\\Flagged " : "") +
- (it.draft() ? "\\Draft " : "") +
- (it.answered() ? "\\Answered " : "") +
- (it.recent() ? "\\Recent " : "");
- }
- static String envelope(Message m) {
- return
- "(" + quotify(m.arrival.toString()) +
- " " + quotify(m.subject) +
- " " + addressList(m.from) +
- " " + addressList(m.headers.get("sender")) +
- " " + addressList(m.replyto) +
- " " + addressList(m.to) +
- " " + addressList(m.cc) +
- " " + addressList(m.bcc) +
- " " + quotify((String)m.headers.get("in-reply-to")) +
- " " + quotify(m.messageid) +
- ")";
+ boolean uid = false;
+ tag = null; tag = token().atom();
+ String command = token().atom();
+ if (command.equals("UID")) { uid = true; command = token().atom(); }
+ switch(((Integer)commands.get(command.toUpperCase())).intValue()) {
+ case AUTHENTICATE: login(token().astring(), token().astring()); break;
+ case LIST: emitListResponse(list(token().q(), token().q(), null)); break;
+ case LSUB: emitListResponse(lsub(token().q(), token().q(), null)); break;
+ case SUBSCRIBE: subscribe(token().atom()); break;
+ case UNSUBSCRIBE: unsubscribe(token().atom()); break;
+ case CAPABILITY: star("CAPABILITY " + join(" ", capability())); break;
+ //case ID: id(); break;
+ case LOGIN: login(token().astring(), token().astring()); break;
+ case LOGOUT: logout(); star("BYE"); conn.close(); return false;
+ case RENAME: rename(token().mailbox(), token().atom()); break;
+ case EXAMINE: select(token().astring(), true); break;
+ case SELECT: select(token().astring(), false); break;
+ case COPY: copy(uid ? Query.uid(token().set()) : Query.num(token().set()), token().mailbox()); break;
+ case DELETE: delete(token().mailbox()); break;
+ case CHECK: check(); break;
+ case NOOP: noop(); break;
+ case CLOSE: close(); break;
+ case EXPUNGE: expunge(); break;
+ case UNSELECT: unselect(); break;
+ case CREATE: create(token().astring()); break;
+ case STATUS: status(token().mailbox(), token().l()); break;
+ case FETCH: fetch(uid?Query.uid(token().set()):Query.num(token().set()),parseFetch(token().l()),uid);
+ break;
+ case APPEND: {
+ Mailbox m = token().mailbox();
+ int flags = 0;
+ Date arrival = null;
+ Token t = token();
+ if (t.type == t.LIST) { flags = t.flags(); t = token(); }
+ if (t.type == t.QUOTED) { arrival = t.datetime(); t = token(); }
+ append(m, flags, arrival, token().q());
+ break; }
+ case STORE: {
+ Query q = uid ? Query.uid(token().set()) : Query.num(token().set());
+ String s = token().atom();
+ int flags = token().flags();
+ if (s.equals("FLAGS")) store(q, flags, ~flags, false, uid);
+ else if (s.equals("+FLAGS")) store(q, flags, 0, false, uid);
+ else if (s.equals("-FLAGS")) store(q, 0, flags, false, uid);
+ else if (s.equals("FLAGS.SILENT")) store(q, flags, ~flags, true, uid);
+ else if (s.equals("+FLAGS.SILENT")) store(q, flags, 0, true, uid);
+ else if (s.equals("-FLAGS.SILENT")) store(q, 0, flags, true, uid);
+ else throw new Bad("unknown STORE specifier " + s);
+ break; }
+ default: throw new Bad("unrecognized command \"" + command + "\"");
+ }
+ println(tag + " OK " + command + " Completed.");
+ } catch (Bad b) { println(tag==null ? "* BAD Invalid tag" : (tag + " Bad " + b.toString())); b.printStackTrace();
+ } catch (No n) { println(tag==null ? "* BAD Invalid tag" : (tag + " No " + n.toString())); n.printStackTrace(); }
}
-
-
- // Parsing Logic //////////////////////////////////////////////////////////////////////////////
-
Query query() {
String s = null;
boolean not = false;
Query q = null;
while(true) {
Token t = token();
- if (t.type == t.LIST) throw new Exn.No("nested queries not yet supported");
+ if (t.type == t.LIST) throw new No("nested queries not yet supported");
else if (t.type == t.SET) return Query.num(t.set());
s = t.atom();
if (s.equals("NOT")) { not = true; continue; }
else if (s.equals("SEEN")) q = Query.seen();
else if (s.equals("OLD")) { not = true; q = Query.recent(); }
else if (s.equals("NEW")) q = Query.and(Query.recent(), Query.not(Query.seen()));
- else if (s.equals("KEYWORD")) q = Query.header("keyword", flag());
- else if (s.equals("HEADER")) q = Query.header(astring(), astring());
- else if (s.equals("BCC")) q = Query.header("bcc", astring());
- else if (s.equals("CC")) q = Query.header("cc", astring());
- else if (s.equals("FROM")) q = Query.header("from", astring());
- else if (s.equals("TO")) q = Query.header("to", astring());
- else if (s.equals("SUBJECT")) q = Query.header("subject", astring());
- else if (s.equals("LARGER")) q = Query.size(n(), Integer.MAX_VALUE);
- else if (s.equals("SMALLER")) q = Query.size(Integer.MIN_VALUE, n());
- else if (s.equals("BODY")) q = Query.body(astring());
- else if (s.equals("TEXT")) q = Query.full(astring());
- else if (s.equals("BEFORE")) q = Query.arrival(new Date(0), date());
- else if (s.equals("SINCE")) q = Query.arrival(date(), new Date(Long.MAX_VALUE));
- else if (s.equals("ON")) { Date d = date(); q = Query.arrival(d, new Date(d.getTime() + 24 * 60 * 60)); }
- else if (s.equals("SENTBEFORE")) q = Query.sent(new Date(0), date());
- else if (s.equals("SENTSINCE")) q = Query.sent(date(), new Date(Long.MAX_VALUE));
- else if (s.equals("SENTON")) { Date d = date(); q = Query.sent(d, new Date(d.getTime() + 24 * 60 * 60)); }
- else if (s.equals("UID")) q = Query.uid(set());
+ else if (s.equals("KEYWORD")) q = Query.header("keyword", token().flag());
+ else if (s.equals("HEADER")) q = Query.header(token().astring(), token().astring());
+ else if (s.equals("BCC")) q = Query.header("bcc", token().astring());
+ else if (s.equals("CC")) q = Query.header("cc", token().astring());
+ else if (s.equals("FROM")) q = Query.header("from", token().astring());
+ else if (s.equals("TO")) q = Query.header("to", token().astring());
+ else if (s.equals("SUBJECT")) q = Query.header("subject", token().astring());
+ else if (s.equals("LARGER")) q = Query.size(token().n(), Integer.MAX_VALUE);
+ else if (s.equals("SMALLER")) q = Query.size(Integer.MIN_VALUE, token().n());
+ else if (s.equals("BODY")) q = Query.body(token().astring());
+ else if (s.equals("TEXT")) q = Query.full(token().astring());
+ else if (s.equals("BEFORE")) q = Query.arrival(new Date(0), token().date());
+ else if (s.equals("SINCE")) q = Query.arrival(token().date(), new Date(Long.MAX_VALUE));
+ else if (s.equals("ON")) { Date d = token().date(); q = Query.arrival(d, new Date(d.getTime() + 24 * 60 * 60)); }
+ else if (s.equals("SENTBEFORE")) q = Query.sent(new Date(0), token().date());
+ else if (s.equals("SENTSINCE")) q = Query.sent(token().date(), new Date(Long.MAX_VALUE));
+ else if (s.equals("SENTON")) { Date d = token().date(); q = Query.sent(d, new Date(d.getTime() + 24 * 60 * 60)); }
+ else if (s.equals("UID")) q = Query.uid(token().set());
return q;
}
public Token(Token[] list) { l = list; s = null; type = LIST; n = 0; }
public Token(int number) { n = number; l = null; s = null; type = NUMBER; }
- public String mailboxPattern() throws Exn.Bad {
- if (type == ATOM) return s;
- if (type == QUOTED) return s;
- throw new Exn.Bad("exepected a mailbox pattern");
- }
- public String flag() throws Exn.Bad {
- if (type != ATOM) throw new Exn.Bad("expected a flag");
- return s; // if first char != backslash, it is a keyword-flag
- }
- public int n() throws Exn.Bad {
- if (type != NUMBER) throw new Exn.Bad("expected number");
- return n;
- }
- public int nz() throws Exn.Bad {
- if (type != NUMBER) throw new Exn.Bad("expected number");
- if (n == 0) throw new Exn.Bad("expected nonzero number");
- return n;
- }
- public String q() throws Exn.Bad {
- if (type == NIL) return null;
- if (type != QUOTED) throw new Exn.Bad("expected qstring");
- return s;
- }
- public Token[] l() throws Exn.Bad {
- if (type == NIL) return null;
- if (type != LIST) throw new Exn.Bad("expected parenthesized list");
- return l;
+ public String flag() { if (type != ATOM) throw new Bad("expected a flag"); return s; }
+ public int n() { if (type != NUMBER) throw new Bad("expected number"); return n; }
+ public int nz() { int n = n(); if (n == 0) throw new Bad("expected nonzero number"); return n; }
+ public String q() { if (type == NIL) return null; if (type != QUOTED) throw new Bad("expected qstring"); return s; }
+ public Token[] l() { if (type == NIL) return null; if (type != LIST) throw new Bad("expected list"); return l; }
+
+ public int flags() {
+ if (type != LIST) throw new Bad("expected flag list");
+ int ret = 0;
+ for(int i=0; i<l.length; i++) {
+ String flag = l[i].s;
+ if (flag.equals("\\Deleted")) ret |= Mailbox.Flag.DELETED;
+ else if (flag.equals("\\Seen")) ret |= Mailbox.Flag.SEEN;
+ else if (flag.equals("\\Flagged")) ret |= Mailbox.Flag.FLAGGED;
+ else if (flag.equals("\\Draft")) ret |= Mailbox.Flag.DRAFT;
+ else if (flag.equals("\\Answered")) ret |= Mailbox.Flag.ANSWERED;
+ else if (flag.equals("\\Recent")) ret |= Mailbox.Flag.RECENT;
+ }
+ return ret;
}
- public int[] set() throws Exn.Bad {
- if (type != ATOM) throw new Exn.Bad("expected a messageid set");
+ public int[] set() {
+ if (type != ATOM) throw new Bad("expected a messageid set");
Vec ids = new Vec();
StringTokenizer st = new StringTokenizer(s, ",");
while(st.hasMoreTokens()) {
for(int i=0; i<ret.length; i++) ret[i] = ((Integer)ids.elementAt(i)).intValue();
return ret;
}
- public Date date() throws Exn.Bad {
- if (type != QUOTED && type != ATOM) throw new Exn.Bad("Expected quoted or unquoted date");
+ public Date date() {
+ if (type != QUOTED && type != ATOM) throw new Bad("Expected quoted or unquoted date");
try { return new SimpleDateFormat("dd-MMM-yyyy").parse(s);
- } catch (ParseException p) { throw new Exn.Bad("invalid date format; " + p); }
+ } catch (ParseException p) { throw new Bad("invalid date format; " + p); }
}
- public Date datetime() throws Exn.Bad {
- if (type != QUOTED && type != ATOM) throw new Exn.Bad("Expected quoted or unquoted datetime");
+ public Date datetime() {
+ if (type != QUOTED && type != ATOM) throw new Bad("Expected quoted or unquoted datetime");
try { return new SimpleDateFormat("dd-MM-yyyy hh:mm:ss").parse(s.trim());
- } catch (ParseException p) { throw new Exn.Bad("invalid datetime format " + s + " : " + p); }
+ } catch (ParseException p) { throw new Bad("invalid datetime format " + s + " : " + p); }
}
- public String nstring() throws Exn.Bad {
+ public String nstring() {
if (type == NIL) return null;
if (type == QUOTED) return s;
- throw new Exn.Bad("expected NIL or string");
+ throw new Bad("expected NIL or string");
}
- public String astring() throws Exn.Bad {
+ public String astring() {
if (type == ATOM) return s;
if (type == QUOTED) return s;
- throw new Exn.Bad("expected atom or string");
+ throw new Bad("expected atom or string");
}
- public String atom() throws Exn.Bad {
- if (type != ATOM) throw new Exn.Bad("expected atom");
+ public String atom() {
+ if (type != ATOM) throw new Bad("expected atom");
for(int i=0; i<s.length(); i++) {
char c = s.charAt(i);
if (c == '(' || c == ')' || c == '{' || c == ' ' || c == '%' || c == '*')
- throw new Exn.Bad("invalid char in atom: " + c);
+ throw new Bad("invalid char in atom: " + c);
}
return s;
}
- public Mailbox mailbox() throws Exn.Bad {
+ public Mailbox mailbox() {
if (type == BAREWORD && s.toLowerCase().equals("inbox")) return getMailbox("INBOX", false);
return getMailbox(astring(), false);
}
StringBuffer sb = new StringBuffer();
char c = getc(); while (c == ' ') c = getc();
if (c == '\r' || c == '\n') {
- throw new Exn.Bad("unexpected end of line");
+ throw new Bad("unexpected end of line");
} if (c == '{') {
while(peekc() != '}') sb.append(getc());
int octets = Integer.parseInt(sb.toString());
}
return new Token(sb.toString(), true);
- // NOTE: this is technically a violation of the IMAP grammar, since atoms like FOO[BAR should be legal
+ // NOTE: this is technically a violation of the IMAP grammar, since atoms like FOO[BAR should be legal
} else if (c == ']' || c == ')') { return null;
} else if (c == '[' || c == '(') {
Token t;
}
}
- // FEATURE: inline these?
- public String mailboxPattern() throws Exn.Bad { return token().mailboxPattern(); }
- public String flag() throws Exn.Bad { return token().flag(); }
- public int n() throws Exn.Bad { return token().n(); }
- public int nz() throws Exn.Bad { return token().nz(); }
- public String q() throws Exn.Bad { return token().q(); }
- public Token[] l() throws Exn.Bad { return token().l(); }
- public int[] set() throws Exn.Bad { return token().set(); }
- public Date date() throws Exn.Bad { return token().date(); }
- public Date datetime() throws Exn.Bad { return token().datetime(); }
- public String nstring() throws Exn.Bad { return token().nstring(); }
- public String astring() throws Exn.Bad { return token().astring(); }
- public String atom() throws Exn.Bad { return token().atom(); }
- public Mailbox mailbox() throws Exn.Bad, IOException { return token().mailbox(); }
-
public final FetchRequest BODYSTRUCTURE = new FetchRequest();
public final FetchRequest ENVELOPE = new FetchRequest();
public final FetchRequest FLAGS = new FetchRequest();
public final FetchRequest UID = new FetchRequest();
public final FetchRequest BODY = new FetchRequest();
public final FetchRequest BODYPEEK = new FetchRequest();
- public class FetchRequest {
+ public class FetchRequest { }
+ public class FetchRequestBody extends FetchRequest {
+ int type = 0;
+ boolean peek = false;
+ int[] path = null;
+ int start = -1;
+ int end = -1;
+ Token[] headers = null;
+ public static final int PATH = 0;
+ public static final int TEXT = 1;
+ public static final int MIME = 2;
+ public static final int HEADER = 3;
+ public static final int FIELDS = 4;
+ public static final int FIELDS_NOT= 5;
}
- public class FetchRequestBody extends FetchRequest {
- int type = 0;
- boolean peek = false;
- int[] path = null;
- int start = -1;
- int end = -1;
- Token[] headers = null;
- public static final int PATH = 0;
- public static final int TEXT = 1;
- public static final int MIME = 2;
- public static final int HEADER = 3;
- public static final int FIELDS = 4;
- public static final int FIELDS_NOT= 5;
- }
- public FetchRequest[] parseFetchRequest(Token[] t) {
- FetchRequest[] ret = new FetchRequest[t.length];
- for(int i=0; i<t.length; i++) {
- if (t[i] == null) continue;
- if (t[i].s.equalsIgnoreCase("BODYSTRUCTURE")) ret[i] = BODYSTRUCTURE;
- else if (t[i].s.equalsIgnoreCase("ENVELOPE")) ret[i] = ENVELOPE;
- else if (t[i].s.equalsIgnoreCase("FLAGS")) ret[i] = FLAGS;
- else if (t[i].s.equalsIgnoreCase("INTERNALDATE")) ret[i] = INTERNALDATE;
- else if (t[i].s.equalsIgnoreCase("RFC822")) ret[i] = RFC822;
- else if (t[i].s.equalsIgnoreCase("RFC822.HEADER")) ret[i] = RFC822HEADER;
- else if (t[i].s.equalsIgnoreCase("RFC822.SIZE")) ret[i] = RFC822SIZE;
- else if (t[i].s.equalsIgnoreCase("RFC822.TEXT")) ret[i] = RFC822TEXT;
- else if (t[i].s.equalsIgnoreCase("UID")) ret[i] = UID;
- else {
- FetchRequestBody b = new FetchRequestBody();
- String s = t[i].s.toUpperCase();
-
- String startend = null;
- if (s.indexOf('<') != -1 && s.endsWith(">")) {
- startend = s.substring(s.indexOf('<'), s.indexOf('>'));
- s = s.substring(0, s.indexOf('<'));
- }
+ public FetchRequest[] parseFetch(Token[] t) {
+ FetchRequest[] ret = new FetchRequest[t.length];
+ for(int i=0; i<t.length; i++) {
+ if (t[i] == null) continue;
+ if (t[i].s.equalsIgnoreCase("BODYSTRUCTURE")) ret[i] = BODYSTRUCTURE;
+ else if (t[i].s.equalsIgnoreCase("ENVELOPE")) ret[i] = ENVELOPE;
+ else if (t[i].s.equalsIgnoreCase("FLAGS")) ret[i] = FLAGS;
+ else if (t[i].s.equalsIgnoreCase("INTERNALDATE")) ret[i] = INTERNALDATE;
+ else if (t[i].s.equalsIgnoreCase("RFC822")) ret[i] = RFC822;
+ else if (t[i].s.equalsIgnoreCase("RFC822.HEADER")) ret[i] = RFC822HEADER;
+ else if (t[i].s.equalsIgnoreCase("RFC822.SIZE")) ret[i] = RFC822SIZE;
+ else if (t[i].s.equalsIgnoreCase("RFC822.TEXT")) ret[i] = RFC822TEXT;
+ else if (t[i].s.equalsIgnoreCase("UID")) ret[i] = UID;
+ else {
+ FetchRequestBody b = new FetchRequestBody();
+ String s = t[i].s.toUpperCase();
+
+ String startend = null;
+ if (s.indexOf('<') != -1 && s.endsWith(">")) {
+ startend = s.substring(s.indexOf('<'), s.indexOf('>'));
+ s = s.substring(0, s.indexOf('<'));
+ }
- if (s.equalsIgnoreCase("BODY.PEEK")) b.peek = true;
- else if (s.equalsIgnoreCase("BODY")) b.peek = false;
- else throw new Exn.No("unknown fetch argument: " + s);
+ if (s.equalsIgnoreCase("BODY.PEEK")) b.peek = true;
+ else if (s.equalsIgnoreCase("BODY")) b.peek = false;
+ else throw new No("unknown fetch argument: " + s);
- if (i<t.length - 1 && (t[i+1].type == t[i].LIST)) {
- i++;
- Token[] t2 = t[i].l();
- if (t2.length == 0) {
- b.type = b.TEXT;
- } else {
- String s2 = t2[0].s.toUpperCase();
- Vec mimeVec = new Vec();
- while(s2.charAt(0) >= '0' && s2.charAt(0) <= '9') {
- mimeVec.addElement(new Integer(Integer.parseInt(s2.substring(0, s2.indexOf('.')))));
- s2 = s2.substring(s2.indexOf('.') + 1);
- }
- if (mimeVec.size() > 0) {
- b.path = new int[mimeVec.size()];
- for(int j=0; j<b.path.length; j++) b.path[j] = ((Integer)mimeVec.elementAt(j)).intValue();
- }
- if (s2.equals("")) { b.type = b.PATH; }
- else if (s2.equals("HEADER")) { b.type = b.HEADER; }
- else if (s2.equals("HEADER.FIELDS")) { b.type = b.FIELDS; b.headers = t2[1].l(); }
- else if (s2.equals("HEADER.FIELDS.NOT")) { b.type = b.FIELDS_NOT; b.headers = t2[1].l(); }
- else if (s2.equals("MIME")) { b.type = b.MIME; }
- else if (s2.equals("TEXT")) { b.type = b.TEXT; }
+ if (i<t.length - 1 && (t[i+1].type == t[i].LIST)) {
+ i++;
+ Token[] t2 = t[i].l();
+ if (t2.length == 0) {
+ b.type = b.TEXT;
+ } else {
+ String s2 = t2[0].s.toUpperCase();
+ Vec mimeVec = new Vec();
+ while(s2.charAt(0) >= '0' && s2.charAt(0) <= '9') {
+ mimeVec.addElement(new Integer(Integer.parseInt(s2.substring(0, s2.indexOf('.')))));
+ s2 = s2.substring(s2.indexOf('.') + 1);
}
+ if (mimeVec.size() > 0) {
+ b.path = new int[mimeVec.size()];
+ for(int j=0; j<b.path.length; j++) b.path[j] = ((Integer)mimeVec.elementAt(j)).intValue();
+ }
+ if (s2.equals("")) { b.type = b.PATH; }
+ else if (s2.equals("HEADER")) { b.type = b.HEADER; }
+ else if (s2.equals("HEADER.FIELDS")) { b.type = b.FIELDS; b.headers = t2[1].l(); }
+ else if (s2.equals("HEADER.FIELDS.NOT")) { b.type = b.FIELDS_NOT; b.headers = t2[1].l(); }
+ else if (s2.equals("MIME")) { b.type = b.MIME; }
+ else if (s2.equals("TEXT")) { b.type = b.TEXT; }
}
+ }
- if (i<t.length - 1 && (t[i+1].s.startsWith("<"))) {
- i++;
- startend = t[i].s.substring(1, t[i].s.length() - 1);
- }
+ if (i<t.length - 1 && (t[i+1].s.startsWith("<"))) {
+ i++;
+ startend = t[i].s.substring(1, t[i].s.length() - 1);
+ }
- if (startend != null) {
- if (startend.indexOf('.') == -1) {
- b.start = Integer.parseInt(startend);
- b.end = -1;
- } else {
- b.start = Integer.parseInt(startend.substring(0, startend.indexOf('.')));
- b.end = Integer.parseInt(startend.substring(startend.indexOf('.') + 1));
- }
+ if (startend != null) {
+ if (startend.indexOf('.') == -1) {
+ b.start = Integer.parseInt(startend);
+ b.end = -1;
+ } else {
+ b.start = Integer.parseInt(startend.substring(0, startend.indexOf('.')));
+ b.end = Integer.parseInt(startend.substring(startend.indexOf('.') + 1));
}
- ret[i] = b;
}
+ ret[i] = b;
}
- return ret;
+ }
+ return ret;
}
}
+
+
+ // Unparsing (printing) logic //////////////////////////////////////////////////////////////////////////////
+
+ static String quotify(String s){return s==null?"NIL":"\""+s.replaceAll("\\\\","\\\\").replaceAll("\"","\\\\\"")+"\"";}
+ static String quotify(Date d) { return new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss +zzzz").format(d); }
+ static String address(Address a) {return"("+quotify(a.description)+" NIL "+quotify(a.user)+" "+quotify(a.host)+")"; }
+ public static String addressList(Object a) {
+ if (a == null) return "NIL";
+ if (a instanceof Address) return "("+address((Address)a)+")";
+ Address[] aa = (Address[])a;
+ StringBuffer ret = new StringBuffer();
+ ret.append("(");
+ for(int i=0; i<aa.length; i++) { ret.append(aa[i]); if (i < aa.length - 1) ret.append(" "); }
+ ret.append(")");
+ return ret.toString();
+ }
+ static String flags(Mailbox.Iterator it) {
+ return
+ (it.deleted() ? "\\Deleted " : "") +
+ (it.seen() ? "\\Seen " : "") +
+ (it.flagged() ? "\\Flagged " : "") +
+ (it.draft() ? "\\Draft " : "") +
+ (it.answered() ? "\\Answered " : "") +
+ (it.recent() ? "\\Recent " : "");
+ }
+ static String envelope(Message m) {
+ return
+ "(" + quotify(m.arrival.toString()) +
+ " " + quotify(m.subject) +
+ " " + addressList(m.from) +
+ " " + addressList(m.headers.get("sender")) +
+ " " + addressList(m.replyto) +
+ " " + addressList(m.to) +
+ " " + addressList(m.cc) +
+ " " + addressList(m.bcc) +
+ " " + quotify((String)m.headers.get("in-reply-to")) +
+ " " + quotify(m.messageid) +
+ ")";
+ }
+
+ public static String qq(String s) {
+ StringBuffer ret = new StringBuffer(s.length() + 20);
+ ret.append('{');
+ ret.append(s.length());
+ ret.append('}');
+ ret.append('\r');
+ ret.append('\n');
+ ret.append(s);
+ ret.append('\r');
+ ret.append('\n');
+ return ret.toString();
+ }
+
+ private static String join(String delimit, String[] stuff) {
+ StringBuffer ret = new StringBuffer();
+ for(int i=0; i<stuff.length; i++) {
+ ret.append(stuff[i]);
+ if (i<stuff.length - 1) ret.append(delimit);
+ }
+ return ret.toString();
+ }
+
+
+ // Command Switchblock //////////////////////////////////////////////////////////////////////////////
+
+ private static final Hashtable commands = new Hashtable();
+ private static final int UID = 0; static { commands.put("UID", new Integer(UID)); }
+ private static final int AUTHENTICATE = 1; static { commands.put("AUTHENTICATE", new Integer(AUTHENTICATE)); }
+ private static final int LIST = 2; static { commands.put("LIST", new Integer(LIST)); }
+ private static final int LSUB = 3; static { commands.put("LSUB", new Integer(LSUB)); }
+ private static final int SUBSCRIBE = 4; static { commands.put("SUBSCRIBE", new Integer(SUBSCRIBE)); }
+ private static final int UNSUBSCRIBE = 5; static { commands.put("UNSUBSCRIBE", new Integer(UNSUBSCRIBE)); }
+ private static final int CAPABILITY = 6; static { commands.put("CAPABILITY", new Integer(CAPABILITY)); }
+ private static final int ID = 7; static { commands.put("ID", new Integer(ID)); }
+ private static final int LOGIN = 8; static { commands.put("LOGIN", new Integer(LOGIN)); }
+ private static final int LOGOUT = 9; static { commands.put("LOGOUT", new Integer(LOGOUT)); }
+ private static final int RENAME = 10; static { commands.put("RENAME", new Integer(RENAME)); }
+ private static final int EXAMINE = 11; static { commands.put("EXAMINE", new Integer(EXAMINE)); }
+ private static final int SELECT = 12; static { commands.put("SELECT", new Integer(SELECT)); }
+ private static final int COPY = 13; static { commands.put("COPY", new Integer(COPY)); }
+ private static final int DELETE = 14; static { commands.put("DELETE", new Integer(DELETE)); }
+ private static final int CHECK = 15; static { commands.put("CHECK", new Integer(CHECK)); }
+ private static final int NOOP = 16; static { commands.put("NOOP", new Integer(NOOP)); }
+ private static final int CLOSE = 17; static { commands.put("CLOSE", new Integer(CLOSE)); }
+ private static final int EXPUNGE = 18; static { commands.put("EXPUNGE", new Integer(EXPUNGE)); }
+ private static final int UNSELECT = 19; static { commands.put("UNSELECT", new Integer(UNSELECT)); }
+ private static final int CREATE = 20; static { commands.put("CREATE", new Integer(CREATE)); }
+ private static final int STATUS = 21; static { commands.put("STATUS", new Integer(STATUS)); }
+ private static final int FETCH = 22; static { commands.put("FETCH", new Integer(FETCH)); }
+ private static final int APPEND = 23; static { commands.put("APPEND", new Integer(APPEND)); }
+ private static final int STORE = 24; static { commands.put("STORE", new Integer(STORE)); }
+
+
+ // Main //////////////////////////////////////////////////////////////////////////////
+
+ /** simple listener for testing purposes */
+ public static void main(String[] args) throws Exception {
+ ServerSocket ss = new ServerSocket(143);
+ for(final Socket s = ss.accept();;)
+ new Thread() { public void run() { try {
+ final Mailbox root = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT+File.separatorChar+"imap", true);
+ new Session(s, root,
+ new Authenticator() {
+ public Mailbox authenticate(String u, String p) {
+ if (u.equals("megacz")&&p.equals("pass")) return root.slash("users",true).slash("megacz",true);
+ return null;
+ } } ).handleRequest();
+ } catch (Exception e) { e.printStackTrace(); } } }.start();
+ }
}