2 import org.ibex.crypto.*;
4 import org.ibex.util.*;
5 import org.ibex.mail.protocol.*;
10 // FIXME this is important
11 // folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
13 // soft line limit (suggested): 78 chars / hard line limit: 998 chars
14 // date/time parsing: see spec, 3.3
16 // FIXME: messages must NEVER contain 8-bit binary data; this is a violation of IMAP
18 // FEATURE: PGP-signature-parsing
19 // FEATURE: mailing list header parsing
20 // FEATURE: delivery status notification (and the sneaky variety)
21 // FEATURE: threading as in http://www.jwz.org/doc/threading.html
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 JSReflection {
31 public final String allHeaders; // pristine headers
32 public final CaseInsensitiveHash headers; // hash of headers (not including resent's and traces)
33 public final String body; // entire body
34 public final int lines; // lines in the body
36 public final Date date;
37 public final Address to;
38 public final Address from; // if multiple From entries, this is sender
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 public final Hashtable[] resent;
45 public final Trace[] traces;
47 public final Address envelopeFrom;
48 public final Address envelopeTo;
50 public final Date arrival; // when the message first arrived at this machine; IMAP "internal date message attr"
52 public void dump(OutputStream os) throws IOException {
53 Writer w = new OutputStreamWriter(os);
54 w.write("X-org.ibex.mail.headers.envelope.From: " + envelopeFrom + "\r\n");
55 w.write("X-org.ibex.mail.headers.envelope.To: " + envelopeTo + "\r\n");
63 final String returnPath;
64 final Element[] elements;
65 public Trace(LineReader lr) throws Trace.Malformed, IOException {
66 String retPath = lr.readLine();
67 if (!retPath.startsWith("Return-Path:")) throw new Trace.Malformed("trace did not start with Return-Path header");
68 returnPath = retPath.substring(12).trim();
71 String s = lr.readLine();
73 if (!s.startsWith("Received:")) { lr.pushback(s); break; }
74 s = s.substring(9).trim();
75 el.addElement(new Element(s));
77 elements = new Element[el.size()];
78 el.copyInto(elements);
80 public class Element {
86 public Element(String fromDomain, String fromIP, String toDomain, String forWhom, Date date) {
87 this.fromDomain=fromDomain; this.fromIP=fromIP; this.toDomain=toDomain; this.forWhom=forWhom; this.date=date; }
88 public Element(String s) throws Trace.Malformed {
89 StringTokenizer st = new StringTokenizer(s);
90 if (!st.nextToken().equals("FROM")) throw new Trace.Malformed("trace did note have a FROM element: " + s);
91 fromDomain = st.nextToken();
92 if (!st.nextToken().equals("BY")) throw new Trace.Malformed("trace did note have a BY element: " + s);
93 toDomain = st.nextToken();
97 public class Malformed extends Message.Malformed { public Malformed(String s) { super(s); } }
100 public static class Malformed extends MailException.Malformed { public Malformed(String s) { super(s); } }
102 public Message(Address envelopeFrom, Address envelopeTo, String s, Date arrival)
103 { this(envelopeFrom, envelopeTo, new LineReader(new StringReader(s)), arrival); }
104 public Message(Address envelopeFrom, Address envelopeTo, LineReader rs) { this(envelopeFrom, envelopeTo, rs, null); }
105 public Message(Address envelopeFrom, Address envelopeTo, LineReader rs, Date arrival) {
107 this.arrival = arrival == null ? new Date() : arrival;
108 this.headers = new CaseInsensitiveHash();
109 Vec envelopeToHeader = new Vec();
111 StringBuffer all = new StringBuffer();
113 Address to = null, from = null, replyto = null;
114 String subject = null, messageid = null;
115 Vec cc = new Vec(), bcc = new Vec(), resent = new Vec(), traces = new Vec();
117 for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) {
121 if (s.length() == 0 || Character.isSpace(s.charAt(0))) {
122 if (key == null) throw new Malformed("Message began with a blank line; no headers");
123 ((CaseInsensitiveHash)headers).add(key, headers.get(key) + s);
126 if (s.indexOf(':') == -1) throw new Malformed("Header line does not contain colon: " + s);
127 key = s.substring(0, s.indexOf(':'));
128 for(int i=0; i<key.length(); i++)
129 if (key.charAt(i) < 33 || key.charAt(i) > 126)
130 throw new Malformed("Header key \""+key+"\" contains invalid character \"" + key.charAt(i) + "\"");
131 String val = s.substring(s.indexOf(':') + 1).trim();
132 while(val.length() > 0 && Character.isSpace(val.charAt(0))) val = val.substring(1);
133 if (key.startsWith("Resent-")) {
134 if (key.startsWith("Resent-From")) resent.addElement(new Hashtable());
135 ((Hashtable)resent.lastElement()).put(key.substring(7), val);
136 } else if (key.startsWith("Return-Path")) {
137 rs.pushback(s); traces.addElement(new Trace(rs));
138 } else if (key.equals("X-org.ibex.mail.headers.envelope.From")) {
139 if (envelopeFrom == null) envelopeFrom = new Address(val);
140 } else if (key.equals("X-org.ibex.mail.headers.envelope.To")) {
141 if (envelopeTo == null) envelopeTo = new Address(val);
143 // just append it to the previous one; valid for Comments/Keywords
144 if (headers.get(key) != null) val = headers.get(key) + " " + val;
145 ((CaseInsensitiveHash)headers).add(key, val);
149 // FIXME what if all are null?
150 this.to = headers.get("To") == null ? envelopeTo : new Address((String)headers.get("To"));
151 this.from = headers.get("From") == null ? envelopeFrom : new Address((String)headers.get("From"));
152 this.envelopeFrom = envelopeFrom == null ? this.from : envelopeFrom;
153 this.envelopeTo = envelopeTo == null ? this.to : envelopeTo;
155 this.date = new Date(); // FIXME (Date)headers.get("Date");
156 this.replyto = headers.get("Reply-To") == null ? null : new Address((String)headers.get("Reply-To"));
157 this.subject = (String)headers.get("Subject");
158 this.messageid = (String)headers.get("Message-Id");
159 if (headers.get("Cc") != null) {
160 // FIXME: tokenize better
161 StringTokenizer st = new StringTokenizer((String)headers.get("Cc"));
162 this.cc = new Address[st.countTokens()];
163 for(int i=0; i<this.cc.length; i++) this.cc[i] = new Address(st.nextToken());
165 this.cc = new Address[0];
167 if (headers.get("Bcc") != null) {
168 StringTokenizer st = new StringTokenizer((String)headers.get("Bcc"));
169 this.bcc = new Address[st.countTokens()];
170 for(int i=0; i<this.bcc.length; i++) this.bcc[i] = new Address(st.nextToken());
172 this.bcc = new Address[0];
174 resent.copyInto(this.resent = new Hashtable[resent.size()]);
175 traces.copyInto(this.traces = new Trace[traces.size()]);
176 allHeaders = all.toString();
177 StringBuffer body = new StringBuffer();
178 for(String s = rs.readLine();; s = rs.readLine()) {
179 if (s == null) break;
181 body.append(s); // FIXME: we're assuming all mail messages fit in memory
185 this.body = body.toString();
186 } catch (IOException e) { throw new MailException.IOException(e); }
189 // http://www.jwz.org/doc/mid.html
190 private static final Random random = new Random();
191 public static String generateFreshMessageId() {
192 StringBuffer ret = new StringBuffer();
194 ret.append(Base36.encode(System.currentTimeMillis()));
196 ret.append(Base36.encode(random.nextLong()));
198 try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
200 return ret.toString();
203 public String rfc822() { return allHeaders + "\r\n" + body; }
204 public int rfc822size() { return allHeaders.length() + 2 /* CRLF */ + body.length(); } // FIXME: double check this
206 public String summary() {
208 " Subject: " + subject + "\n" +
209 " EnvelopeFrom: " + envelopeFrom + "\n" +
210 " EnvelopeTo: " + envelopeTo + "\n" +
211 " MessageId: " + messageid;
214 // use null-sender for error messages (don't send errors to the null addr)
215 public Message bounce(String reason) { throw new RuntimeException("bounce not implemented"); } // FIXME!
217 public static class CaseInsensitiveHash extends org.ibex.js.JSReflection {
218 private Hashtable stuff = new Hashtable();
219 public Object get(Object o) { return stuff.get(((String)o).toLowerCase()); }
220 void add(Object k, Object v) { if (k instanceof String) stuff.put(((String)k).toLowerCase(), v); else stuff.put(k, v); }