// Copyright 2000-2005 the Contributors, as shown in the revision logs. // Licensed under the Apache Public Source License 2.0 ("the License"). // You may not use this file except in compliance with the License. package org.ibex.mail; import org.ibex.mail.*; import org.ibex.util.*; import org.ibex.mail.*; import org.ibex.js.*; import java.io.*; import java.net.*; import java.util.*; import java.text.*; /** abstract superclass for mailboxes, which store messages along with their flags */ public abstract class Mailbox extends JS.Obj implements Target { public static final String STORAGE_ROOT = System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail"); // Required Methods ////////////////////////////////////////////////////////////////////////////// public abstract Mailbox.Iterator iterator(Query q); public abstract void insert(Message message, int flags); public abstract void post(Message message); public abstract void move(Query q, Mailbox dest); public abstract void copy(Query q, Mailbox dest); public abstract int count(Query q); public abstract int uidNext(); // Thunks //////////////////////////////////////////////////////////////////////////// public void accept(Message m) { insert(m, Flag.RECENT); } public Mailbox.Iterator iterator() { return iterator(Query.all()); } // Default Implementation ////////////////////////////////////////////////////////////////////////////// private int randomUidValidity = new Random().nextInt(); public int uidValidity() { return randomUidValidity; } public int maxuid() { int ret = -1; for(Mailbox.Iterator it = iterator(); it.next(); ) ret = Math.max(ret, it.uid()); return ret; } /** default, inefficient implementation of Mailbox; only requires a few methods to be implemented */ public static abstract class Default extends Mailbox /* implements MailTree? */ { public Mailbox.Iterator iterator(Query q) { return new Mailbox.Iterator.QueryIterator(q, this); } public void copy(Query q, Mailbox dest) { for(Mailbox.Iterator it = iterator(q); it.next();) dest.insert(it.cur(), it.getFlags()); } public int count(Query q) { int count = 0; for(Mailbox.Iterator it = iterator(q); it.next();) count++; return count; } public MailTree slash(String name, boolean create) { return null; } public String[] children() { return new String[] { }; } public void post(Message message) { insert(message, Flag.RECENT); } public JS get(JS key) throws JSExn { return (JS)slash(JSU.toString(key), true); } public void move(Query q, Mailbox dest) { for(Mailbox.Iterator it = iterator(q);it.next();) { dest.insert(it.cur(), it.getFlags()); it.delete(); } } public static abstract class Iterator implements Mailbox.Iterator { // FIXME: NNTP spec allows us to use longs (64-bit) here // FIXME: NNTP spec requires that the minimum nntpNumber of a group must never, ever decrease (no, that's not a typo) public int nntpNumber() { throw new MailException("not supported"); } public int getFlags() { return 0; } public void setFlags(int flags) { throw new MailException("not supported"); } } } // Iterator Definition ////////////////////////////////////////////////////////////////////////////// public static interface Iterator { public abstract Message cur(); public abstract Headers head(); // FIXME: change all code to actually use this convention! /** JDBC convention: iterator starts "before the first element" */ public abstract boolean next(); public abstract void delete(); /** a unique identifier for this message */ public abstract int uid(); /** * Message number according to IMAP semantics. * - must range from 1..numMessagesInMailbox * - no two messages in the same mailbox may have the same imapNumber * - sorting by uid must yield the same order as sorting them by imapNumber * - imapNumber changes when messages with lower imapNumbers are deleted */ public abstract int imapNumber(); /** * Message number according to NNTP semantics. * - no two messages in the same mailbox may have the same nntpNumber * - article number may NEVER change or EVER be reused * - uidValidity is irrelevant */ public abstract int nntpNumber(); public abstract int getFlags(); public abstract void setFlags(int flags); public static class Wrapper implements Iterator { private Iterator it; public Wrapper(Iterator it) { this.it = it; } public Message cur() { return it.cur(); } public Headers head() { return it.head(); } public boolean next() { return it.next(); } public int uid() { return it.uid(); } public int nntpNumber() { return it.nntpNumber(); } public int imapNumber() { return it.imapNumber(); } public void delete() { it.delete(); } public int getFlags() { return it.getFlags(); } public void setFlags(int flags) { it.setFlags(flags); } } public static class AclWrapper extends Wrapper { private Acl.Entry acl; public AclWrapper(Iterator it, Acl.Entry acl) { super(it); this.acl = acl; } public Message cur() { if (acl.read && acl.list) return super.cur(); else throw new Acl.PermissionDenied(); } public Headers head() { if (acl.read && acl.list) return super.head(); else throw new Acl.PermissionDenied(); } public boolean next() { if (acl.list) return super.next(); else throw new Acl.PermissionDenied(); } public int uid() { if (acl.list) return super.uid(); else throw new Acl.PermissionDenied(); } public int nntpNumber() { if (acl.list) return super.nntpNumber(); else throw new Acl.PermissionDenied(); } public int imapNumber() { if (acl.list) return super.imapNumber(); else throw new Acl.PermissionDenied(); } public void delete() { if (acl.delete) super.delete(); else throw new Acl.PermissionDenied(); } public int getFlags() { if (acl.list) return super.getFlags(); else throw new Acl.PermissionDenied(); } public void setFlags(int flags) { if (acl.flags) super.setFlags(flags); else throw new Acl.PermissionDenied(); } } class QueryIterator extends Mailbox.Iterator.Wrapper { Query q; public QueryIterator(Query q, Mailbox m) { super(m.iterator()); this.q = q; } public boolean next() { if (q == null) return false; do { if (!super.next()) return false; } while(!q.match(this)); return true; } } public static class NullIterator extends Mailbox.Default.Iterator { public NullIterator() { } public Message cur() { return null; } public Headers head() { return null; } public boolean next() { return false; } public int uid() { return 0; } public void delete() { } public int imapNumber() { return 0; } public int nntpNumber() { throw new RuntimeException("this mailbox does not keep article numbers"); } } } /** constants for the six IMAP flags */ public static class Flag { public static final int DELETED = 0x0001; public static final int SEEN = 0x0002; public static final int FLAGGED = 0x0004; public static final int DRAFT = 0x0008; public static final int ANSWERED = 0x0010; public static final int RECENT = 0x0020; public static final int[] all = new int[] { DELETED, SEEN, FLAGGED, DRAFT, ANSWERED, RECENT }; public static final int defaultFlags = RECENT; } public static class MailboxWrapper extends Mailbox { private Mailbox m; public MailboxWrapper(Mailbox m) { this.m = m; } public Mailbox.Iterator iterator() { return m.iterator(); } public Mailbox.Iterator iterator(Query q) { return m.iterator(q); } public void insert(Message message, int flags) { m.insert(message, flags); } public void post(Message message) { m.insert(message, Flag.RECENT); } public void move(Query q, Mailbox dest) { m.move(q, dest); } public void copy(Query q, Mailbox dest) { m.copy(q, dest); } public int count(Query q) { return m.count(q); } public int uidNext() { return m.uidNext(); } public int uidValidity() { return m.uidValidity(); } } public static class AclWrapper extends MailboxWrapper { private Mailbox m; private Acl.Entry acl; public AclWrapper(Mailbox m, Acl.Entry acl) { super(m); this.acl = acl; } public Mailbox.Iterator iterator(Query q) { if (acl.list) return new Mailbox.Iterator.AclWrapper(m.iterator(q), acl); else throw new Acl.PermissionDenied(); } public Mailbox.Iterator iterator() { if (acl.list) return new Mailbox.Iterator.AclWrapper(m.iterator(), acl); else throw new Acl.PermissionDenied(); } public void insert(Message message, int flags) { if (acl.insert) m.insert(message, flags); else throw new Acl.PermissionDenied(); } public int uidValidity() { if (acl.list) return m.uidValidity(); else throw new Acl.PermissionDenied(); } public void post(Message message) { if (acl.post) m.insert(message, Flag.RECENT); else throw new Acl.PermissionDenied(); } public void move(Query q, Mailbox dest) { if (acl.list && acl.read && acl.delete) m.move(q, dest); else throw new Acl.PermissionDenied(); } public void copy(Query q, Mailbox dest) { if (acl.list && acl.read) m.copy(q, dest); else throw new Acl.PermissionDenied(); } public int count(Query q) { if (acl.list) return m.count(q); else throw new Acl.PermissionDenied(); } public int uidNext() { if (acl.list) return m.uidNext(); else throw new Acl.PermissionDenied(); } } }