1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
6 import org.ibex.crypto.*;
7 import org.ibex.util.*;
8 import org.ibex.mail.protocol.*;
14 // FIXME this is important: folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
16 // soft line limit (suggested): 78 chars / hard line limit: 998 chars
17 // date/time parsing: see spec, 3.3
19 // FIXME: messages must NEVER contain 8-bit binary data; this is a violation of IMAP
21 // FEATURE: PGP-signature-parsing
22 // FEATURE: mailing list header parsing
23 // FEATURE: delivery status notification (and the sneaky variety)
24 // FEATURE: threading as in http://www.jwz.org/doc/threading.html
26 // FIXME RFC822 1,000-char limit per line
29 * [immutable] This class encapsulates a message "floating in the
30 * ether": RFC2822 data but no storage-specific flags or other
33 public class Message extends MIME.Part {
35 // Parsed Headers //////////////////////////////////////////////////////////////////////////////
37 public final Address to;
38 public final Address from; // if multiple From entries, this is sender
39 public final Address envelopeFrom;
40 public final Address envelopeTo;
41 public final Date date;
42 public final Date arrival;
43 public final Address replyto; // if none provided, this is equal to sender
44 public final String subject;
45 public final String messageid;
46 public final Address[] cc;
47 public final Address[] bcc;
49 public static Message newMessage(Stream stream) throws Malformed { return newMessage(stream, null, null); }
50 public static Message newMessage(Stream stream, Address from, Address to) throws Malformed {
51 if (from == null && to == null) return new Message(stream);
52 StringBuffer sb = new StringBuffer();
53 boolean inheaders = true;
54 if (from != null) sb.append("Return-Path: " + from.toString(true) + "\r\n");
56 String s = stream.readln();
58 if (inheaders && to != null && s.toLowerCase().startsWith("envelope-to:")) continue;
59 if (inheaders && from != null && s.toLowerCase().startsWith("return-path:")) continue;
60 if (s.length() == 0 && inheaders) {
62 if (to != null) sb.append("Envelope-To: " + to.toString(true) + "\r\n");
67 return newMessage(new Stream(sb.toString()));
69 private Message(Stream stream) throws Malformed {
70 super(stream, null, false);
71 this.to = headers.gets("To") != null ? Address.parse(headers.gets("To")) :
72 headers.gets("Envelope-To") != null ? Address.parse(headers.gets("Envelope-To")) : null;
73 this.from = headers.gets("From") != null ? Address.parse(headers.gets("From")) :
74 headers.gets("Return-Path") != null ? Address.parse(headers.gets("Return-Path")) : null;
75 this.envelopeTo = headers.gets("Envelope-To") != null ? Address.parse(headers.gets("Envelope-To")) : null;
76 this.envelopeFrom = headers.gets("Return-Path") != null ? Address.parse(headers.gets("Return-Path")) : null;
77 this.replyto = headers.gets("Reply-To") == null ? null : Address.parse(headers.gets("Reply-To"));
78 this.subject = headers.gets("Subject");
79 this.messageid = headers.gets("Message-Id");
80 this.cc = Address.list(headers.gets("Cc"));
81 this.bcc = Address.list(headers.gets("BCc"));
82 this.date = parseDate(headers.gets("Date")) == null ? new Date() : parseDate(headers.gets("Date"));
83 this.arrival = this.date; // FIXME wrong
87 // Helpers /////////////////////////////////////////////////////////////////////////////
89 // http://www.jwz.org/doc/mid.html
90 private static final Random random = new Random();
91 public static String generateFreshMessageId() {
92 StringBuffer ret = new StringBuffer();
94 ret.append(Base36.encode(System.currentTimeMillis()));
96 ret.append(Base36.encode(random.nextLong()));
98 try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
100 return ret.toString();
103 public static Date parseDate(String s) { return null; } // FIXME!!!
105 // use null-sender for error messages (don't send errors to the null addr)
106 public Message bounce(String reason) {
107 Log.warn(Message.class, "bounce not implemented");
111 public String summary() { return "[" + envelopeFrom + " -> " + envelopeTo + "] " + subject; }
113 public void dump(Stream s) {
114 s.setNewline("\r\n");
115 s.println(headers.raw);
121 public int size() { return headers.raw.length() + 2 /* CRLF */ + body.length(); }
122 public String toString() { return headers.raw + "\r\n" + body; }
124 public static class Malformed extends Exception { public Malformed(String s) { super(s); } }