2 import org.ibex.crypto.*;
3 // FIXME MIME: RFC2045, 2046, 2049
4 // NOTE: always use Win32 line endings
5 // hard line limit: 998 chars
6 // soft line limit (suggested): 78 chars
7 // header fields: ascii 33-126 (but no colon)
8 // field body: anything ASCII except CRLF
9 // folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
10 // body needs CRLF; one or the other alone is not acceptable
11 // date/time parsing: see 3.3
13 // FEATURE: PGP-signature-parsing
14 // FEATURE: mailing list header parsing
15 // FEATURE: delivery status notification (and the sneaky variety)
16 // FEATURE: threading as in http://www.jwz.org/doc/threading.html
18 public class Message extends JSReflection {
20 public final String allHeaders; // pristine headers
21 public final Hashtable headers; // hash of headers (not including resent's and traces)
23 // parsed header fields
24 public final Date date;
25 public final Address to;
26 public final Address from; // if multiple From entries, this is sender
27 public final Address replyto; // if none provided, this is equal to sender
28 public final String subject;
29 public final String messageid;
30 public final Address[] cc;
31 public final Address[] bcc;
32 public final Hashtable[] resent;
33 public final Trace[] traces;
36 public final Address envelopeFrom;
37 public final Address[] envelopeTo;
39 public static class StoredMessage extends Message {
41 public boolean deleted = false;
42 public boolean read = false;
43 public boolean answered = false;
44 public String dumpStoredForm() { throw new Error("StoredMessage.dumpStoredForm() not implemented"); };
47 public static class Address extends JSReflection {
48 public String coerceToString() {
49 if (description == null || description.equals("")) return user +"@"+ host;
50 return description + " " + "<" + user +"@"+ host + ">";
52 public final String user;
53 public final String host;
54 public final String description;
55 public Address(String user, String host, String description) {
56 this.user = user; this.host = host; this.description = description;
58 public Address(String s) {
60 String descrip = null;
61 if (s.indexOf('<') != -1) {
62 if (s.indexOf('>') == -1) { /* FIXME */ }
63 descrip = s.substring(0, s.indexOf('<')) + s.substring(s.indexOf('>') + 1);
64 s = s.substring(s.indexOf('<') + 1, s.indexOf('>'));
66 if (s.indexOf('@') == -1) { /* FIXME */ }
67 description = descrip;
68 user = s.substring(0, s.indexOf('@'));
69 host = s.substring(s.indexOf('@')+1);
74 final String returnPath = null;
75 final Element[] elements;
76 public class Element {
77 final String fromDomain;
79 final String toDomain;
85 // FIXME: support dotTerminatedLikeSMTP
86 public Message(ReadStream rs, boolean dotTermiantedLikeSMTP) {
88 StringBuffer all = new StringBuffer();
89 for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) {
92 if (Character.isSpace(s.charAt(0))) {
93 if (lastKey == null) { /* FIXME */ }
94 headers.put(lastKey, headers.get(lastKey) + s);
97 if (s.indexOf(':') == -1) { /* FIXME */ }
99 key = s.substring(0, s.indexOf(':'));
100 String val = s.substring(0, s.indexOf(':') + 1);
101 while(Character.isSpace(val.charAt(0))) val = val.substring(1);
103 if (headers.get(key) != null)
104 if (key.startsWith("Resent-")) {
105 // FIXME: multi-resent headers
106 } else if (key.startsWith("Return-Path:")) {
107 // FIXME: parse traces, see RFC2821, section 4.4
108 } else if (key.startsWith("Recieved:")) {
109 // FIXME: parse traces, see RFC2821, section 4.4
111 // just append it to the previous one; valid for Comments/Keywords
112 val = headers.get(key) + " " + val;
115 headers.put(key, val);
117 pristeneHeaders = all.toString();
118 StringBuffer body = new StringBuffer();
119 for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) body.append(s);
120 this.body = body.toString();
123 // http://www.jwz.org/doc/mid.html
124 public static String generateFreshMessageId() {
125 StringBuffer ret = new StringBuffer();
127 ret.append(Base36.encode(System.currentTimeMillis()));
129 ret.append(Base36.encode(random.nextLong()));
131 try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
133 return ret.toString();
136 private static final Random random = new Random();