2 import org.ibex.crypto.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.protocol.*;
10 // FIXME this is important: folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
12 // soft line limit (suggested): 78 chars / hard line limit: 998 chars
13 // date/time parsing: see spec, 3.3
15 // FIXME: messages must NEVER contain 8-bit binary data; this is a violation of IMAP
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
22 // FIXME RFC822 1,000-char limit per line
25 * [immutable] This class encapsulates a message "floating in the
26 * ether": RFC2822 data but no storage-specific flags or other
29 public class Message extends MIME.Part {
31 // Parsed Headers //////////////////////////////////////////////////////////////////////////////
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;
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 newMessage(stream);
48 StringBuffer sb = new StringBuffer();
49 boolean inheaders = true;
50 if (from != null) sb.append("Return-Path: " + from.toString(true));
52 String s = stream.readln();
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) {
58 if (to != null) sb.append("Envelope-To: " + to.toString(true) + "\r\n");
63 return newMessage(new Stream(sb.toString()));
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")) :
72 headers.gets("To") != null ? Address.parse(headers.gets("To")) : null;
73 this.envelopeFrom = headers.gets("Return-Path") != null ? Address.parse(headers.gets("Return-Path")) :
74 headers.gets("From") != null ? Address.parse(headers.gets("From")) : null;
75 this.replyto = headers.gets("Reply-To") == null ? null : Address.parse(headers.gets("Reply-To"));
76 this.subject = headers.gets("Subject");
77 this.messageid = headers.gets("Message-Id");
78 this.cc = Address.list(headers.gets("Cc"));
79 this.bcc = Address.list(headers.gets("BCc"));
80 this.date = headers.gets("Date") == null ? new Date() : parseDate(headers.gets("Date"));
81 this.arrival = this.date; // FIXME wrong
85 // Helpers /////////////////////////////////////////////////////////////////////////////
87 // http://www.jwz.org/doc/mid.html
88 private static final Random random = new Random();
89 public static String generateFreshMessageId() {
90 StringBuffer ret = new StringBuffer();
92 ret.append(Base36.encode(System.currentTimeMillis()));
94 ret.append(Base36.encode(random.nextLong()));
96 try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
98 return ret.toString();
101 public static Date parseDate(String s) { return null; } // FIXME!!!
103 // use null-sender for error messages (don't send errors to the null addr)
104 public Message bounce(String reason) {
105 Log.warn(Message.class, "bounce not implemented");
109 public String summary() { return "[" + envelopeFrom + " -> " + envelopeTo + "] " + subject; }
111 public void dump(Stream s) {
112 s.setNewline("\r\n");
113 s.println(headers.raw);
119 public int size() { return headers.raw.length() + 2 /* CRLF */ + body.length(); }
120 public String toString() { return headers.raw + "\r\n" + body; }
122 public static class Malformed extends Exception { public Malformed(String s) { super(s); } }