2 import org.ibex.crypto.*;
4 import org.ibex.util.*;
5 import org.ibex.mail.protocol.*;
10 // NOTE: always use Win32 line endings
11 // hard line limit: 998 chars
12 // soft line limit (suggested): 78 chars
13 // header fields: ascii 33-126 (but no colon)
14 // field body: anything ASCII except CRLF
15 // folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
16 // body needs CRLF; one or the other alone is not acceptable
17 // date/time parsing: see 3.3
19 // FEATURE: MIME RFC2045, 2046, 2049
20 // FEATURE: PGP-signature-parsing
21 // FEATURE: mailing list header parsing
22 // FEATURE: delivery status notification (and the sneaky variety)
23 // FEATURE: threading as in http://www.jwz.org/doc/threading.html
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
31 public final Date date;
32 public final Address to;
33 public final Address from; // if multiple From entries, this is sender
34 public final Address replyto; // if none provided, this is equal to sender
35 public final String subject;
36 public final String messageid;
37 public final Address[] cc;
38 public final Address[] bcc;
39 public final Hashtable[] resent;
40 public Trace[] traces;
42 public final Address envelopeFrom;
43 public final Address[] envelopeTo;
45 public void dump(OutputStream os) throws IOException {
46 Writer w = new OutputStreamWriter(os);
53 public static class StoredMessage extends Message {
55 public boolean deleted = false;
56 public boolean read = false;
57 public boolean answered = false;
58 public StoredMessage(LineReader rs) throws IOException, MailException.Malformed { super(rs); }
62 final String returnPath;
63 final Element[] elements;
64 public Trace(LineReader lr) throws Trace.Malformed, IOException {
65 String retPath = lr.readLine();
66 if (!retPath.startsWith("Return-Path:")) throw new Trace.Malformed("trace did not start with Return-Path header");
67 returnPath = retPath.substring(12).trim();
70 String s = lr.readLine();
72 if (!s.startsWith("Received:")) { lr.pushback(s); break; }
73 s = s.substring(9).trim();
74 el.addElement(new Element(s));
76 elements = new Element[el.size()];
77 el.copyInto(elements);
79 public class Element {
85 public Element(String fromDomain, String fromIP, String toDomain, String forWhom, Date date) {
86 this.fromDomain=fromDomain; this.fromIP=fromIP; this.toDomain=toDomain; this.forWhom=forWhom; this.date=date; }
87 public Element(String s) throws Trace.Malformed {
88 StringTokenizer st = new StringTokenizer(s);
89 if (!st.nextToken().equals("FROM")) throw new Trace.Malformed("trace did note have a FROM element: " + s);
90 fromDomain = st.nextToken();
91 if (!st.nextToken().equals("BY")) throw new Trace.Malformed("trace did note have a BY element: " + s);
92 toDomain = st.nextToken();
96 public class Malformed extends Message.Malformed { public Malformed(String s) { super(s); } }
99 public static class Malformed extends MailException.Malformed { public Malformed(String s) { super(s); } }
100 public Message(LineReader rs) throws IOException, Malformed {
102 StringBuffer all = new StringBuffer();
113 headers = new Hashtable();
114 date = null; // FIXME
117 for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) {
120 if (s.length() == 0 || Character.isSpace(s.charAt(0))) {
121 if (key == null) throw new Malformed("Message began with a blank line; no headers");
122 headers.put(key, headers.get(key) + s);
125 if (s.indexOf(':') == -1) throw new Malformed("Header line does not contain colon: " + s);
126 key = s.substring(0, s.indexOf(':'));
127 String val = s.substring(0, s.indexOf(':') + 1);
128 while(Character.isSpace(val.charAt(0))) val = val.substring(1);
130 if (headers.get(key) != null)
131 if (key.startsWith("Resent-")) {
132 // FIXME: multi-resent headers
134 } else if (key.startsWith("Return-Path:")) {
136 Trace t = new Trace(rs);
137 Trace[] newTraces = new Trace[traces == null ? 1 : traces.length + 1];
138 if (traces != null) System.arraycopy(traces, 0, newTraces, 0, traces.length);
140 traces[traces.length-1] = t;
143 // just append it to the previous one; valid for Comments/Keywords
144 val = headers.get(key) + " " + val;
147 headers.put(key, val);
150 allHeaders = all.toString();
151 StringBuffer body = new StringBuffer();
152 for(String s = rs.readLine();; s = rs.readLine()) { if (s == null) break; else body.append(s + "\r\n"); }
153 this.body = body.toString();
156 // http://www.jwz.org/doc/mid.html
157 public static String generateFreshMessageId() {
158 StringBuffer ret = new StringBuffer();
160 ret.append(Base36.encode(System.currentTimeMillis()));
162 ret.append(Base36.encode(random.nextLong()));
164 try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
166 return ret.toString();
169 private static final Random random = new Random();