dont guess envelope headers
[org.ibex.mail.git] / src / org / ibex / mail / Message.java
1 package org.ibex.mail;
2 import org.ibex.crypto.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.protocol.*;
5 import org.ibex.io.*;
6 import java.util.*;
7 import java.net.*;
8 import java.io.*;
9
10 // FIXME this is important: folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
11
12 // soft line limit (suggested): 78 chars /  hard line limit: 998 chars
13 // date/time parsing: see spec, 3.3
14
15 // FIXME: messages must NEVER contain 8-bit binary data; this is a violation of IMAP
16
17 // FEATURE: PGP-signature-parsing
18 // FEATURE: mailing list header parsing
19 // FEATURE: delivery status notification (and the sneaky variety)
20 // FEATURE: threading as in http://www.jwz.org/doc/threading.html
21 // FEATURE: lazy body
22 // FIXME RFC822 1,000-char limit per line
23
24 /** 
25  *  [immutable] This class encapsulates a message "floating in the
26  *  ether": RFC2822 data but no storage-specific flags or other
27  *  metadata.
28  */
29 public class Message extends MIME.Part {
30
31     // Parsed Headers //////////////////////////////////////////////////////////////////////////////
32
33     public final Address     to;
34     public final Address     from;                // if multiple From entries, this is sender
35     public final Address     envelopeFrom;
36     public final Address     envelopeTo;
37     public final Date        date;
38     public final Date        arrival;
39     public final Address     replyto;             // if none provided, this is equal to sender
40     public final String      subject;
41     public final String      messageid;
42     public final Address[]   cc;
43     public final Address[]   bcc;
44
45     public static Message newMessage(Stream stream) throws Malformed { return newMessage(stream, null, null); }
46     public static Message newMessage(Stream stream, Address from, Address to) throws Malformed {
47         if (from == null && to == null) return new Message(stream);
48         StringBuffer sb = new StringBuffer();
49         boolean inheaders = true;
50         if (from != null) sb.append("Return-Path: " + from.toString(true) + "\r\n");
51         while(true) {
52             String s = stream.readln();
53             if (s == null) break;
54             if (inheaders && to != null && s.toLowerCase().startsWith("envelope-to:")) continue;
55             if (inheaders && from != null && s.toLowerCase().startsWith("return-path:")) continue;
56             if (s.length() == 0 && inheaders) {
57                 inheaders = false;
58                 if (to != null) sb.append("Envelope-To: " + to.toString(true) + "\r\n");
59             }
60             sb.append(s);
61             sb.append("\r\n");
62         }
63         return newMessage(new Stream(sb.toString()));
64     }
65     private Message(Stream stream) throws Malformed {
66         super(stream, null, false);
67         this.to           = headers.gets("To") != null ? Address.parse(headers.gets("To")) : 
68                             headers.gets("Envelope-To") != null ? Address.parse(headers.gets("Envelope-To")) : null;
69         this.from         = headers.gets("From") != null ? Address.parse(headers.gets("From")) : 
70                             headers.gets("Return-Path") != null ? Address.parse(headers.gets("Return-Path")) : null;
71         this.envelopeTo   = headers.gets("Envelope-To") != null ? Address.parse(headers.gets("Envelope-To")) : null;
72         this.envelopeFrom = headers.gets("Return-Path") != null ? Address.parse(headers.gets("Return-Path")) : null;
73         this.replyto      = headers.gets("Reply-To") == null ? null : Address.parse(headers.gets("Reply-To"));
74         this.subject      = headers.gets("Subject");
75         this.messageid    = headers.gets("Message-Id");
76         this.cc           = Address.list(headers.gets("Cc"));
77         this.bcc          = Address.list(headers.gets("BCc"));
78         this.date         = parseDate(headers.gets("Date")) == null ? new Date() : parseDate(headers.gets("Date"));
79         this.arrival      = this.date; // FIXME wrong
80     }
81
82
83     // Helpers /////////////////////////////////////////////////////////////////////////////
84
85     // http://www.jwz.org/doc/mid.html
86     private static final Random random = new Random();
87     public static String generateFreshMessageId() {
88         StringBuffer ret = new StringBuffer();
89         ret.append('<');
90         ret.append(Base36.encode(System.currentTimeMillis()));
91         ret.append('.');
92         ret.append(Base36.encode(random.nextLong()));
93         ret.append('.');
94         try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
95         ret.append('>');
96         return ret.toString();
97     }
98
99     public static Date parseDate(String s) { return null; } // FIXME!!!
100    
101     //  use null-sender for error messages (don't send errors to the null addr)
102     public Message bounce(String reason) {
103         Log.warn(Message.class, "bounce not implemented");
104         return null;
105     }  // FIXME!
106
107     public String summary() { return "[" + envelopeFrom + " -> " + envelopeTo + "] " + subject; }
108
109     public void dump(Stream s) {
110         s.setNewline("\r\n");
111         s.println(headers.raw);
112         s.println("");
113         s.println(body);
114         s.flush();
115     }
116
117     public int size()        { return headers.raw.length() + 2 /* CRLF */ + body.length(); }
118     public String toString() { return headers.raw + "\r\n" + body; }
119
120     public static class Malformed extends Exception { public Malformed(String s) { super(s); } }
121 }
122