22d94ddd658601ebe4d500f4bd1bae97be803928
[org.ibex.mail.git] / src / org / ibex / mail / Message.java
1 package org.ibex.mail;
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
11
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 {
17
18     public final String allHeaders;   // pristine headers
19     public final Hashtable headers;   // hash of headers (not including resent's and traces)
20
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;
32
33     // envelope fields
34     public final Address envelopeFrom;
35     public final Address[] envelopeTo;
36
37     public static class StoredMessage extends Message {
38         public final int uid;
39         public boolean deleted = false;
40         public boolean read = false;
41         public boolean answered = false;
42     }
43
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;
50         }
51         public Address(String s) {
52             s = s.trim();
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('>'));
58             }
59             if (s.indexOf('@') == -1) { /* FIXME */ }
60             description = descrip;
61             user = s.substring(0, s.indexOf('@'));
62             host = s.substring(s.indexOf('@')+1);
63         }
64     }
65
66     public class Trace {
67         final String returnPath = null;
68         final Element[] elements;
69         public class Element {
70             final String fromDomain;
71             final String fromIP;
72             final String toDomain;
73             final String forWhom;
74             final Date date;
75         }
76     }
77
78     public static class Base36 {
79         public static String encode(long l) {
80             StringBuffer ret = new StringBuffer();
81             while (l > 0) {
82                 if ((l % 36) < 10) ret.append((char)(((int)'0') + (int)(l % 36)));
83                 else ret.append((char)(((int)'A') + (int)((l % 36) - 10)));
84                 l /= 36;
85             }
86         }
87     }
88
89     public Message(ReadStream rs) {
90         String key = null;
91         StringBuffer all = new StringBuffer();
92         for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) {
93             all.append(s);
94             all.append("\r\n");
95             if (Character.isSpace(s.charAt(0))) {
96                 if (lastKey == null) { /* FIXME */ }
97                 headers.put(lastKey, headers.get(lastKey) + s);
98                 continue;
99             }
100             if (s.indexOf(':') == -1) { /* FIXME */ }
101
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);
105
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                    
113                 } else {
114                     // just append it to the previous one; valid for Comments/Keywords
115                     val = headers.get(key) + " " + val;
116                 } 
117             
118             headers.put(key, val);
119         }
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();
124     }
125
126     // http://www.jwz.org/doc/mid.html
127     public static String generateFreshMessageId() {
128         StringBuffer ret = new StringBuffer();
129         ret.append('<');
130         ret.append(Base36.encode(System.currentTimeMillis()));
131         ret.append('.');
132         ret.append(Base36.encode(random.nextLong()));
133         ret.append('.');
134         try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
135         ret.append('>');
136         return ret.toString();
137     }
138
139     private static final Random random = new Random();
140 }