2 import org.ibex.crypto.*;
4 import org.ibex.util.*;
5 import org.ibex.mail.protocol.*;
10 // soft line limit (suggested): 78 chars / hard line limit: 998 chars
11 // folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
12 // date/time parsing: see spec, 3.3
14 // FEATURE: PGP-signature-parsing
15 // FEATURE: mailing list header parsing
16 // FEATURE: delivery status notification (and the sneaky variety)
17 // FEATURE: threading as in http://www.jwz.org/doc/threading.html
21 * [immutable] This class encapsulates a message "floating in the
22 * ether": RFC2822 data but no storage-specific flags or other
25 public class Message extends JSReflection {
27 public final String allHeaders; // pristine headers
28 public final Hashtable headers; // hash of headers (not including resent's and traces)
29 public final String body; // entire body
30 public final int lines; // lines in the body
32 public final Date date;
33 public final Address to;
34 public final Address from; // if multiple From entries, this is sender
35 public final Address replyto; // if none provided, this is equal to sender
36 public final String subject;
37 public final String messageid;
38 public final Address[] cc;
39 public final Address[] bcc;
40 public final Hashtable[] resent;
41 public final Trace[] traces;
43 public final Address envelopeFrom;
44 public final Address[] envelopeTo;
46 public final Date arrival; // when the message first arrived at this machine; IMAP "internal date message attr"
48 // FIXME: need to be able to read back in the EnvelopeFrom / EnvelopeTo fields
49 public void dump(OutputStream os) throws IOException {
50 Writer w = new OutputStreamWriter(os);
52 w.write("X-IbexMail-EnvelopeFrom: " + envelopeFrom + "\r\n");
53 w.write("X-IbexMail-EnvelopeTo: "); for(int i=0; i<envelopeTo.length; i++) w.write(envelopeTo[i] + " "); w.write("\r\n");
60 final String returnPath;
61 final Element[] elements;
62 public Trace(LineReader lr) throws Trace.Malformed, IOException {
63 String retPath = lr.readLine();
64 if (!retPath.startsWith("Return-Path:")) throw new Trace.Malformed("trace did not start with Return-Path header");
65 returnPath = retPath.substring(12).trim();
68 String s = lr.readLine();
70 if (!s.startsWith("Received:")) { lr.pushback(s); break; }
71 s = s.substring(9).trim();
72 el.addElement(new Element(s));
74 elements = new Element[el.size()];
75 el.copyInto(elements);
77 public class Element {
83 public Element(String fromDomain, String fromIP, String toDomain, String forWhom, Date date) {
84 this.fromDomain=fromDomain; this.fromIP=fromIP; this.toDomain=toDomain; this.forWhom=forWhom; this.date=date; }
85 public Element(String s) throws Trace.Malformed {
86 StringTokenizer st = new StringTokenizer(s);
87 if (!st.nextToken().equals("FROM")) throw new Trace.Malformed("trace did note have a FROM element: " + s);
88 fromDomain = st.nextToken();
89 if (!st.nextToken().equals("BY")) throw new Trace.Malformed("trace did note have a BY element: " + s);
90 toDomain = st.nextToken();
94 public class Malformed extends Message.Malformed { public Malformed(String s) { super(s); } }
97 public static class Malformed extends MailException.Malformed { public Malformed(String s) { super(s); } }
98 public Message(Address envelopeFrom, Address[] envelopeTo, LineReader rs) {
100 this.envelopeFrom = envelopeFrom;
101 this.envelopeTo = envelopeTo;
102 this.arrival = new Date();
103 this.headers = new CaseInsensitiveHash();
105 StringBuffer all = new StringBuffer();
107 Address to = null, from = null, replyto = null;
108 String subject = null, messageid = null;
109 Vec cc = new Vec(), bcc = new Vec(), resent = new Vec(), traces = new Vec();
110 for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) {
113 if (s.length() == 0 || Character.isSpace(s.charAt(0))) {
114 if (key == null) throw new Malformed("Message began with a blank line; no headers");
115 ((CaseInsensitiveHash)headers).add(key, headers.get(key) + s);
118 if (s.indexOf(':') == -1) throw new Malformed("Header line does not contain colon: " + s);
119 key = s.substring(0, s.indexOf(':'));
120 for(int i=0; i<key.length(); i++)
121 if (key.charAt(i) < 33 || key.charAt(i) > 126)
122 throw new Malformed("Header key \""+key+"\" contains invalid character \"" + key.charAt(i) + "\"");
123 String val = s.substring(s.indexOf(':') + 1).trim();
124 while(Character.isSpace(val.charAt(0))) val = val.substring(1);
125 if (key.startsWith("Resent-")) {
126 if (key.startsWith("Resent-From")) resent.addElement(new Hashtable());
127 ((Hashtable)resent.lastElement()).put(key.substring(7), val);
128 } else if (key.startsWith("Return-Path:")) {
129 rs.pushback(s); traces.addElement(new Trace(rs));
131 // just append it to the previous one; valid for Comments/Keywords
132 if (headers.get(key) != null) val = headers.get(key) + " " + val;
133 ((CaseInsensitiveHash)headers).add(key, val);
137 this.date = (Date)headers.get("Date");
138 this.to = new Address((String)headers.get("To")); // FIXME what if null?
139 this.from = headers.get("From") == null ? envelopeFrom : new Address((String)headers.get("From"));
140 this.replyto = headers.get("Reply-To") == null ? null : new Address((String)headers.get("Reply-To"));
141 this.subject = (String)headers.get("Subject");
142 this.messageid = (String)headers.get("Message-Id");
143 if (headers.get("Cc") != null) {
144 StringTokenizer st = new StringTokenizer((String)headers.get("Cc"));
145 this.cc = new Address[st.countTokens()];
146 for(int i=0; i<this.cc.length; i++) this.cc[i] = new Address(st.nextToken());
148 this.cc = new Address[0];
150 if (headers.get("Bcc") != null) {
151 StringTokenizer st = new StringTokenizer((String)headers.get("Bcc"));
152 this.bcc = new Address[st.countTokens()];
153 for(int i=0; i<this.bcc.length; i++) this.bcc[i] = new Address(st.nextToken());
155 this.bcc = new Address[0];
157 resent.copyInto(this.resent = new Hashtable[resent.size()]);
158 traces.copyInto(this.traces = new Trace[traces.size()]);
159 allHeaders = all.toString();
160 StringBuffer body = new StringBuffer();
162 for(String s = rs.readLine();; s = rs.readLine()) { if (s == null) break; lines++; body.append(s + "\r\n"); }
164 this.body = body.toString();
165 } catch (IOException e) { throw new MailException.IOException(e); }
168 // http://www.jwz.org/doc/mid.html
169 private static final Random random = new Random();
170 public static String generateFreshMessageId() {
171 StringBuffer ret = new StringBuffer();
173 ret.append(Base36.encode(System.currentTimeMillis()));
175 ret.append(Base36.encode(random.nextLong()));
177 try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
179 return ret.toString();
182 public int rfc822size() { return allHeaders.length() + 2 /* CRLF */ + body.length(); } // FIXME: double check this
184 public String summary() {
186 " Subject: " + subject + "\n" +
187 " EnvelopeFrom: " + envelopeFrom + "\n" +
188 " EnvelopeTo: " + envelopeTo + "\n" +
189 " MessageId: " + messageid;
192 // use null-sender for error messages (don't send errors to the null addr)
193 public Message bounce(String reason) { throw new RuntimeException("bounce not implemented"); } // FIXME!
195 private static class CaseInsensitiveHash extends Hashtable {
196 public Object get(Object o) { return (o instanceof String) ? super.get(((String)o).toLowerCase()) : super.get(o); }
197 public Object put(Object k, Object v) { throw new Error("you cannot write to a CaseInsensitiveHash"); }
198 void add(Object k, Object v) { if (k instanceof String) super.put(((String)k).toLowerCase(), v); else super.put(k, v); }