2 // FIXME MIME: RFC2045, 2046, 2049
3 // NOTE: always use Win32 line endings
4 // hard line limit: 998 chars
5 // soft line limit (suggested): 78 chars
6 // header fields: ascii 33-126 (but no colon)
7 // field body: anything ASCII except CRLF
8 // folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
9 // body needs CRLF; one or the other alone is not acceptable
10 // date/time parsing: see 3.3
12 // FEATURE: PGP-signature-parsing
13 // FEATURE: mailing list header parsing
14 // FEATURE: delivery status notification (and the sneaky variety)
15 // FEATURE: threading as in http://www.jwz.org/doc/threading.html
16 public class Message {
18 public final String allHeaders; // pristine headers
19 public final Hashtable headers; // hash of headers (not including resent's and traces)
21 // parsed header fields
22 public final Date date;
23 public final Address to;
24 public final Address from; // if multiple From entries, this is sender
25 public final Address replyto; // if none provided, this is equal to sender
26 public final String subject;
27 public final String messageid;
28 public final Address[] cc;
29 public final Address[] bcc;
30 public final Hashtable[] resent;
31 public final Trace[] traces;
34 public final Address envelopeFrom;
35 public final Address[] envelopeTo;
37 public static class StoredMessage extends Message {
39 public boolean deleted = false;
40 public boolean read = false;
41 public boolean answered = false;
44 public static class Address {
45 public final String user;
46 public final String host;
47 public final String description;
48 public Address(String user, String host, String description) {
49 this.user = user; this.host = host; this.description = description;
51 public Address(String s) {
53 String descrip = null;
54 if (s.indexOf('<') != -1) {
55 if (s.indexOf('>') == -1) { /* FIXME */ }
56 descrip = s.substring(0, s.indexOf('<')) + s.substring(s.indexOf('>') + 1);
57 s = s.substring(s.indexOf('<') + 1, s.indexOf('>'));
59 if (s.indexOf('@') == -1) { /* FIXME */ }
60 description = descrip;
61 user = s.substring(0, s.indexOf('@'));
62 host = s.substring(s.indexOf('@')+1);
67 final String returnPath = null;
68 final Element[] elements;
69 public class Element {
70 final String fromDomain;
72 final String toDomain;
78 public static class Base36 {
79 public static String encode(long l) {
80 StringBuffer ret = new StringBuffer();
82 if ((l % 36) < 10) ret.append((char)(((int)'0') + (int)(l % 36)));
83 else ret.append((char)(((int)'A') + (int)((l % 36) - 10)));
89 public Message(ReadStream rs) {
91 StringBuffer all = new StringBuffer();
92 for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) {
95 if (Character.isSpace(s.charAt(0))) {
96 if (lastKey == null) { /* FIXME */ }
97 headers.put(lastKey, headers.get(lastKey) + s);
100 if (s.indexOf(':') == -1) { /* FIXME */ }
102 key = s.substring(0, s.indexOf(':'));
103 String val = s.substring(0, s.indexOf(':') + 1);
104 while(Character.isSpace(val.charAt(0))) val = val.substring(1);
106 if (headers.get(key) != null)
107 if (key.startsWith("Resent-")) {
108 // FIXME: multi-resent headers
109 } else if (key.startsWith("Return-Path:")) {
110 // FIXME: parse traces, see RFC2821, section 4.4
111 } else if (key.startsWith("Recieved:")) {
112 // FIXME: parse traces, see RFC2821, section 4.4
114 // just append it to the previous one; valid for Comments/Keywords
115 val = headers.get(key) + " " + val;
118 headers.put(key, val);
120 pristeneHeaders = all.toString();
121 StringBuffer body = new StringBuffer();
122 for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) body.append(s);
123 this.body = body.toString();
126 // http://www.jwz.org/doc/mid.html
127 public static String generateFreshMessageId() {
128 StringBuffer ret = new StringBuffer();
130 ret.append(Base36.encode(System.currentTimeMillis()));
132 ret.append(Base36.encode(random.nextLong()));
134 try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
136 return ret.toString();
139 private static final Random random = new Random();