massive refactoring of Headers class
[org.ibex.mail.git] / src / org / ibex / mail / Message.java
index 5fd2e2f..9d91a18 100644 (file)
+// 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.crypto.*;
 import org.ibex.util.*;
 import org.ibex.mail.protocol.*;
+import org.ibex.js.*;
 import org.ibex.io.*;
+import org.ibex.io.Fountain;
 import java.util.*;
 import java.net.*;
 import java.io.*;
 
-// FIXME this is important
-// folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
-
-// soft line limit (suggested): 78 chars /  hard line limit: 998 chars
-// date/time parsing: see spec, 3.3
-
-// FIXME: messages must NEVER contain 8-bit binary data; this is a violation of IMAP
+// FIXME: body constraints (how to enforce?)
+//   - messages must NEVER contain 8-bit binary data; this is a violation of IMAP
+//   - RFC822 1,000-char limit per line [soft line limit (suggested): 78 chars /  hard line limit: 998 chars]
 
 // FEATURE: PGP-signature-parsing
-// FEATURE: mailing list header parsing
-// FEATURE: delivery status notification (and the sneaky variety)
+// FEATURE: mailing list header parsing (?)
 // FEATURE: threading as in http://www.jwz.org/doc/threading.html
-// FEATURE: lazy body
 
 /** 
  *  [immutable] This class encapsulates a message "floating in the
  *  ether": RFC2822 data but no storage-specific flags or other
  *  metadata.
  */
-public class Message extends org.ibex.js.JSReflection {
-
-    public final String allHeaders;           // pristine headers
-    public final CaseInsensitiveHash headers; // hash of headers (not including resent's and traces)
-    public final String body;                 // entire body
-    public final int lines;                   // lines in the body
-
-    public final Date date;
-    public final Address to;
-    public final Address from;                // if multiple From entries, this is sender
-    public final Address replyto;             // if none provided, this is equal to sender
-    public final String subject;
-    public final String messageid;
-    public final Address[] cc;
-    public final Address[] bcc;
-    public final Hashtable[] resent;
-    public final Trace[] traces;
-
-    public final Address envelopeFrom;
-    public final Address envelopeTo;
-
-    public final boolean mime;                // true iff Mime-Version is 1.0
-
-    public final Date arrival;         // when the message first arrived at this machine; IMAP "internal date message attr"
-    
-    public int size() { return allHeaders.length() + 2 /* CRLF */ + body.length(); }
-    public String toString() { return allHeaders + "\r\n" + body; }
-
-    public void dump(Stream s) {
-        s.setNewline("\r\n");
-        s.println("X-org.ibex.mail.headers.envelope.From: " + envelopeFrom);
-        s.println("X-org.ibex.mail.headers.envelope.To: " + envelopeTo);
-        s.println(allHeaders);
-        s.println();
-        s.println(body);
-        s.flush();
+public class Message extends MIME.Part {
+
+    // Parsed Headers //////////////////////////////////////////////////////////////////////////////
+
+    public final Address     to;
+    public final Address     from;                // if multiple From entries, this is sender
+    public final Address     envelopeFrom;
+    public final Address     envelopeTo;
+    public final Date        date;
+    public final Date        arrival;
+    public final Address     replyto;             // if none provided, this is equal to sender
+    public final String      subject;
+    public final String      messageid;
+    public final Address[]   cc;
+    public final Address[]   bcc;
+
+    public static Message newMessage(Fountain in) throws Malformed { return new Message(in); }
+
+    /*
+    public Message reply(Fountain in, Address from, boolean includeReInSubject) throws Malformed {
+       Address to = null;
+       if (to==null) to = Address.parse(headers.get("reply-to"));
+       if (to==null) to = Address.parse(headers.get("from"));
+       if (to==null) to = envelopeFrom;
+        if (to==null) throw new Malformed("cannot reply to a message without a return address");
+       Message ret = newMessage(in, from, to);
+       ret.headers.put("In-Reply-To", messageid);
+       String references = headers.get("references");
+       ret.headers.put("References", messageid + (references==null?"":(" "+references)));
+       if (includeReInSubject && subject!=null && !subject.toLowerCase().trim().startsWith("re:"))
+           headers.put("subject", "Re: "+subject);
+       return ret;
     }
-
-    public class Trace {
-        final String returnPath;
-        final Element[] elements;
-        public Trace(Stream stream) throws Trace.Malformed {
-            String retPath = stream.readln();
-            if (!retPath.startsWith("Return-Path:")) throw new Trace.Malformed("trace did not start with Return-Path header");
-            returnPath = retPath.substring(12).trim();
-            Vec el = new Vec();
-            while(true) {
-                String s = stream.readln();
-                if (s == null) break;
-                if (!s.startsWith("Received:")) { stream.unread(s); break; }
-                s = s.substring(9).trim();
-                el.addElement(new Element(s));
+    */
+
+    // FIXME
+    //public static Message newMessage(Headers headers, Fountain body, Address from, Address to) throws Malformed {
+    //}
+
+    public static Message newMessage(Fountain in, Address from, Address to) throws Malformed {
+        StringBuffer sb = new StringBuffer();
+        if (from != null) sb.append("Return-Path: " + from.toString(true) + "\r\n");
+        Stream stream = in.getStream();
+        while(true) {
+            String s = stream.readln();
+            if (s == null || s.length() == 0) {
+                if (to != null) sb.append("Envelope-To: " + to.toString(true) + "\r\n");
+                sb.append("\r\n");
+                break;
             }
-            elements = new Element[el.size()];
-            el.copyInto(elements);
+            if (to != null && s.toLowerCase().startsWith("envelope-to:")) continue;
+            if (s.toLowerCase().startsWith("return-path:")) continue;
+            sb.append(s);
+            sb.append("\r\n");
         }
-        public class Element {
-             String fromDomain;
-             String fromIP;
-             String toDomain;
-             String forWhom;
-             Date date;
-            public Element(String fromDomain, String fromIP, String toDomain, String forWhom, Date date) {
-                this.fromDomain=fromDomain; this.fromIP=fromIP; this.toDomain=toDomain; this.forWhom=forWhom; this.date=date; }
-            public Element(String s) throws Trace.Malformed {
-                StringTokenizer st = new StringTokenizer(s);
-                if (!st.nextToken().equals("FROM")) throw new Trace.Malformed("trace did note have a FROM element: " + s);
-                fromDomain = st.nextToken();
-                if (!st.nextToken().equals("BY")) throw new Trace.Malformed("trace did note have a BY element: " + s);
-                toDomain = st.nextToken();
-                // FIXME not done yet
-            }
+        for(String s = stream.readln(); s != null; s = stream.readln()) {
+            sb.append(s);
+            sb.append("\r\n");
         }
-        public class Malformed extends Message.Malformed { public Malformed(String s) { super(s); } }
+        return new Message(new Fountain.StringFountain(sb.toString()));
     }
 
-    public static class Malformed extends Exception { public Malformed(String s) { super(s); } }
-
-    public Message(Address from, Address to, String s, Date arrival) throws Malformed {this(from,to,new Stream(s), arrival); }
-    public Message(Address from, Address to, Stream in) throws Malformed { this(from, to, in, new Date()); }
-    public Message(Address envelopeFrom, Address envelopeTo, Stream stream, Date arrival) throws Malformed {
-        this.arrival = arrival;
-        this.headers = new CaseInsensitiveHash();
-        Vec envelopeToHeader = new Vec();
-        String key = null;
-        StringBuffer all = new StringBuffer();
-        Date date = null;
-        Address to = null, from = null, replyto = null;
-        String subject = null, messageid = null;
-        Vec cc = new Vec(), bcc = new Vec(), resent = new Vec(), traces = new Vec();
-        int lines = 0;
-        for(String s = stream.readln(); s != null && !s.equals(""); s = stream.readln()) {
-            all.append(s);
-            lines++;
-            all.append("\r\n");
-            
-            // FIXME RFC822 1,000-char limit per line
-
-
-            // FIXME only iff Mime 1.0 (?)
-            // RFC 2047
-            try { while (s.indexOf("=?") != -1) {
-                String pre = s.substring(0, s.indexOf("=?"));
-                s = s.substring(s.indexOf("=?") + 2);
-
-                // MIME charset; FIXME use this
-                String charset = s.substring(0, s.indexOf('?')).toLowerCase();
-                s = s.substring(s.indexOf('?') + 1);
-
-                String encoding = s.substring(0, s.indexOf('?')).toLowerCase();
-                s = s.substring(s.indexOf('?') + 1);
-
-                String encodedText = s.substring(0, s.indexOf("?="));
-
-                if (encoding.equals("b"))      encodedText = new String(Base64.decode(encodedText));
-
-                // except that ANY char can be endoed (unlike real qp)
-                else if (encoding.equals("q")) encodedText = MIME.QuotedPrintable.decode(encodedText, true);
-                else Log.warn(this, "unknown RFC2047 encoding \""+encoding+"\"");
-
-                String post = s.substring(s.indexOf("?=") + 2);
-                s = pre + encodedText + post;
-
-                // FIXME re-encode when transmitting
+    private Message(Fountain in) throws Malformed {
+        super(in);
+
+        this.envelopeTo   = headers.get("Envelope-To") != null ? Address.parse(headers.get("Envelope-To")) : null;
+        this.envelopeFrom = headers.get("Return-Path") != null ? Address.parse(headers.get("Return-Path")) : null;
+        this.to           = headers.get("To") != null ? Address.parse(headers.get("To")) : this.envelopeTo;
+        this.from         = headers.get("From") != null ? Address.parse(headers.get("From")) : this.envelopeFrom;
+        this.replyto      = headers.get("Reply-To") == null ? null : Address.parse(headers.get("Reply-To"));
+        this.subject      = headers.get("Subject");
+        this.messageid    = headers.get("Message-Id");
+        this.cc           = Address.list(headers.get("Cc"));
+        this.bcc          = Address.list(headers.get("Bcc"));
+        this.date         = parseDate(headers.get("Date")) == null ? new Date() : parseDate(headers.get("Date"));
+
+        // reenable this once whitelisting is moved out of javascript
+        //if (this.messageid==null)
+        //throw new RuntimeException("every RFC2822 message must have a Message-ID: header");
+
+        /*
+        // synthesize a message-id if not provided
+        this.messageid    = headers.get("Message-Id") == null ? generateFreshMessageId(sha1(in.getStream())) : headers.get("Message-Id");
+        if (headers.get("Message-Id") == null) {
+            headers = headers.set("Message-Id", this.messageid);
+            Log.warn(Message.class, "synthesizing message-id for " + summary());
+        }
+        */
 
-            } } catch (Exception e) {
-                Log.warn(this, "error trying to decode RFC2047 encoded-word: \""+s+"\"");
-                Log.warn(this, e);
-            }
+        this.arrival      = this.date; // FIXME wrong; grab this from traces?
+    }
 
-            if (s.length() == 0 || Character.isSpace(s.charAt(0))) {
-                if (key == null) throw new Malformed("Message began with a blank line; no headers");
-                ((CaseInsensitiveHash)headers).add(key, headers.get(key) + s.trim());
-                continue;
-            }
-            if (s.indexOf(':') == -1) throw new Malformed("Header line does not contain colon: " + s);
-            key = s.substring(0, s.indexOf(':'));
-            for(int i=0; i<key.length(); i++)
-                if (key.charAt(i) < 33 || key.charAt(i) > 126)
-                    throw new Malformed("Header key \""+key+"\" contains invalid character \"" + key.charAt(i) + "\"");
-            String val = s.substring(s.indexOf(':') + 1).trim();
-            while(val.length() > 0 && Character.isSpace(val.charAt(0))) val = val.substring(1);
-            if (key.startsWith("Resent-")) {
-                if (resent.size() == 0 || key.startsWith("Resent-From")) resent.addElement(new Hashtable());
-                ((Hashtable)resent.lastElement()).put(key.substring(7), val);
-            } else if (key.startsWith("Return-Path")) {
-                stream.unread(s); traces.addElement(new Trace(stream));
-            } else if (key.equals("X-org.ibex.mail.headers.envelope.From")) {
-                try { if (envelopeFrom == null) envelopeFrom = new Address(val);
-                } catch (Address.Malformed a) { Log.warn(this, a); }
-            } else if (key.equals("X-org.ibex.mail.headers.envelope.To")) {
-                try {if (envelopeTo == null) envelopeTo = new Address(val);
-                } catch (Address.Malformed a) { Log.warn(this, a); }
-            } else {
-                // just append it to the previous one; valid for Comments/Keywords
-                if (headers.get(key) != null) val = headers.get(key) + " " + val;
-                ((CaseInsensitiveHash)headers).add(key, val);
-            }            
-        }
-        
-        // FIXME what if all are null?
-        this.to           = headers.get("To") == null   ? envelopeTo    : Address.parse((String)headers.get("To"));
-        this.from         = headers.get("From") == null ? envelopeFrom  : Address.parse((String)headers.get("From"));
-        this.envelopeFrom = envelopeFrom == null        ? this.from     : envelopeFrom;
-        this.envelopeTo   = envelopeTo == null          ? this.to       : envelopeTo;
-        this.mime         = headers.get("mime-version") != null && headers.get("mime-version").toString().trim().startsWith("1.0");
-        
-        this.date      = new Date(); // FIXME (Date)headers.get("Date");
-        this.replyto   = headers.get("Reply-To") == null ? null : Address.parse((String)headers.get("Reply-To"));
-        this.subject   = (String)headers.get("Subject");
-        this.messageid = (String)headers.get("Message-Id");
-        if (headers.get("Cc") != null) {
-            // FIXME: tokenize better
-            StringTokenizer st = new StringTokenizer((String)headers.get("Cc"), ",");
-            this.cc = new Address[st.countTokens()];
-            for(int i=0; i<this.cc.length; i++) this.cc[i] = Address.parse(st.nextToken());
-        } else {
-            this.cc = new Address[0];
+    /*
+    private static String sha1(Stream stream) {
+        SHA1 sha1 = new SHA1();
+        byte[] b = new byte[1024];
+        while(true) {
+            int numread = stream.read(b, 0, b.length);
+            if (numread == -1) break;
+            sha1.update(b, 0, numread);
         }
-        if (headers.get("Bcc") != null) {
-            StringTokenizer st = new StringTokenizer((String)headers.get("Bcc"));
-            this.bcc = new Address[st.countTokens()];
-            for(int i=0; i<this.bcc.length; i++) this.bcc[i] = Address.parse(st.nextToken());
-        } else {
-            this.bcc = new Address[0];
-        }
-        resent.copyInto(this.resent = new Hashtable[resent.size()]);
-        traces.copyInto(this.traces = new Trace[traces.size()]);
-        allHeaders = all.toString();
-        StringBuffer body = new StringBuffer();
-        for(String s = stream.readln();; s = stream.readln()) {
-            if (s == null) break;
-            lines++;
-            body.append(s);       // FIXME: we're assuming all mail messages fit in memory
-            body.append("\r\n");
-        }
-        this.lines = lines;
-        this.body = body.toString();
+        byte[] results = new byte[sha1.getDigestSize()];
+        sha1.doFinal(results, 0);
+        return new String(Encode.toBase64(results));
     }
+    */
 
-    public static class Part {
-        public final String  type;
-        public final String  subtype;
-        public final Hash    parameters = new Hash();
-        public final Part[]  subparts;
-        public final String  payload;
-        public final String  id;
-        public final String  description;
-
-        public final boolean composite;
-        public final boolean alternative;
 
-        // FIXME charsets  US-ASCII, ISO-8559-X, 
-        public Part(String contentTypeHeader, String transferEncoding,
-                    String id, String description, String payload) throws Malformed {
-            this.id = id;
-            this.description = description;
-
-            // NOTE: it is not valid to send a multipart  message as base64 or quoted-printable; you must encode each section
-            if (transferEncoding == null) transferEncoding = "7bit";
-            transferEncoding = transferEncoding.toLowerCase();
-            if (transferEncoding.equals("quoted-printable")) {
-                this.payload = MIME.QuotedPrintable.decode(payload, false);
-                subparts = null;
-            } else if (transferEncoding.equals("base64")) {
-                this.payload = new String(Base64.decode(payload));
-                subparts = null;
-            } else if (transferEncoding.equals("7bit") || transferEncoding.equals("8bit") ||
-                       transferEncoding.equals("binary")) {
-                this.payload = payload;
-
-                // multipart/mixed -- default
-                // multipart/parallel -- order of components does not matter
-                // multipart/alternative -- same data, different versions
-                // multipart/digest -- default content-type of components is message/rfc822
-
-                // message/rfc822
-                // message/partial -- not supported; see RFC 2046, section 5.2.2
-                // message/external-body -- not supported; see RFC 2046, section 5.2.3
-                
-                /* FIXME
-                if (composite) {
-                    // FIXME: StringTokenizer doesn't really work like this
-                    String boundary = "--" + parameters.get("boundary");  // CRLF, delimiter, optional whitespace, plus CRLF
-                    // preceeding CRLF is part of delimiter
-                    // boundary (including --) cannot be > 70 chars
-                    // delimiter after final part also has a '--' on the end
-                    // first part begins with a boundary delimiter
-                    StringTokenizer st = new StringTokenizer(payload, boundary);
-                    Vec v = new Vec();
-                    while(st.hasMoreTokens()) {
-                        st.nextToken();
-                        Part p = new Part();
-                        v.addElement(p);
-                    }
-                    v.copyInto(subparts = new Part[v.size()]);
-                } else {
-                }
-                */
-                subparts = null;
-            } else {
-                Log.warn(this, "unknown Content-Transfer-Encoding \"" + transferEncoding +
-                         "\"; forcing Content-Type to application/octet-stream (originally \""+contentTypeHeader+"\"");
-                contentTypeHeader = "application/octet-stream";
-                this.payload = payload;
-                subparts = null;
-            }
-
-            if (contentTypeHeader == null) {
-                type = "text";
-                subtype = "plain";
-                parameters.put("charset", "us-ascii");
-            } else {
-                contentTypeHeader = contentTypeHeader.trim();
-                if (contentTypeHeader.endsWith(")")) // remove RFC822 comments
-                    contentTypeHeader = contentTypeHeader.substring(0, contentTypeHeader.lastIndexOf('('));
-                if (contentTypeHeader.indexOf('/') == -1)
-                    throw new Malformed("content-type header lacks a forward slash: \"" + contentTypeHeader + "\"");
-                type = contentTypeHeader.substring(0, contentTypeHeader.indexOf('/')).toLowerCase();
-                contentTypeHeader = contentTypeHeader.substring(contentTypeHeader.indexOf('/') + 1);
-                if (contentTypeHeader.indexOf(';') == -1) {
-                    subtype = contentTypeHeader.toLowerCase();
-                } else {
-                    subtype = contentTypeHeader.substring(0, contentTypeHeader.indexOf(';')).toLowerCase();
-                    StringTokenizer st = new StringTokenizer(contentTypeHeader.substring(contentTypeHeader.indexOf(';') + 1), ";");
-                    while(st.hasMoreTokens()) {
-                        String key = st.nextToken().trim();
-                        if (key.indexOf('=') == -1)
-                            throw new Malformed("content-type parameter lacks an equals sign: \""+key+"\"");
-                        String val = key.substring(key.indexOf('=')+1).trim();
-                        if (val.startsWith("\"") && val.endsWith("\"")) val = val.substring(1, val.length() - 2);
-                        key = key.substring(0, key.indexOf('=')+1).toLowerCase();
-                        parameters.put(key, val);
-                    }
-                }
-            }
+    // Helpers /////////////////////////////////////////////////////////////////////////////
 
-            composite = type.equals("message") || type.equals("multipart");
-            alternative = composite && subtype.equals("alternative");
-
-
-            // fixme: message/rfc822
-
-            // body parts need only contain Content-Foo headers (or none at all); they need not be rfc822 compliant
-
-        }
-    }
-    
     // http://www.jwz.org/doc/mid.html
     private static final Random random = new Random();
     public static String generateFreshMessageId() {
+        return generateFreshMessageId(Base36.encode(System.currentTimeMillis())+'.'+
+                                      Base36.encode(random.nextLong()));
+    }
+    public static String generateFreshMessageId(String seed) {
         StringBuffer ret = new StringBuffer();
         ret.append('<');
-        ret.append(Base36.encode(System.currentTimeMillis()));
-        ret.append('.');
-        ret.append(Base36.encode(random.nextLong()));
-        ret.append('.');
+        ret.append(seed);
+        ret.append('@');
         try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
         ret.append('>');
         return ret.toString();
     }
 
-    //  use null-sender for error messages (don't send errors to the null addr)
-    public Message bounce(String reason) { throw new RuntimeException("bounce not implemented"); }  // FIXME!
-    public static class CaseInsensitiveHash extends org.ibex.js.JSReflection {
-       private Hashtable stuff = new Hashtable();
-        public Object get(Object o) { return stuff.get(((String)o).toLowerCase()); }
-        void add(Object k, Object v) { if (k instanceof String) stuff.put(((String)k).toLowerCase(), v); else stuff.put(k, v); }
+    public static Date parseDate(String s) {
+        // FIXME!!! this must be robust
+        // date/time parsing: see spec, 3.3
+        return null;
     }
 
-    public String summary() {
-        return
-            "          Subject: " + subject + "\n" +
-            "     EnvelopeFrom: " + envelopeFrom + "\n" +
-            "       EnvelopeTo: " + envelopeTo + "\n" +
-            "        MessageId: " + messageid;
+    // this is belived to be compliant with QSBMF (http://cr.yp.to/proto/qsbmf.txt)
+    public Message bounce(String reason) {
+        if (envelopeFrom==null || envelopeFrom.toString().equals("")) return null;
+
+        Log.warn(Message.class, "bouncing message due to: " + reason);
+        Headers h = new Headers(headers, new String[] {
+            "Envelope-To", envelopeFrom.toString(),
+            "Return-Path", "<>",
+            "From",        "MAILER-DAEMON <>",
+            "To",          envelopeFrom.toString(),
+            "Subject",     "failure notice"
+        });
+
+        String error =
+            "\r\n"+
+            "Hi. This is the Ibex Mail Server.  I'm afraid I wasn't able to deliver\r\n"+
+            "your message to the following addresses. This is a permanent error;\r\n"+
+            "I've given up.  Sorry it didn't work out\r\n."+
+            "\r\n"+
+            "<"+envelopeTo.toString()+">:\r\n"+
+            reason+"\r\n"+
+            "\r\n"+
+            "--- Below this line is a copy of the message.\r\n"+
+            "\r\n";
+
+        try {
+            return newMessage(Fountain.Util.concat(h, Fountain.Util.create(error), getBody()));
+        } catch (Message.Malformed e) {
+            Log.error(this, "caught Message.Malformed in Message.bounce(); this should never happen");
+            Log.error(this, e);
+            return null;
+        }
     }
+
+    public       String toString() { throw new RuntimeException("Message.toString() called"); }
+    public final String summary() { return "[" + envelopeFrom + " -> " + envelopeTo + "] " + subject; }
+
+    public static class Malformed extends MailException { public Malformed(String s) { super(s); } }
 }
+