added tons of stuff, including js support
[org.ibex.mail.git] / src / org / ibex / mail / Message.java
1 package org.ibex.mail;
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
12
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
17
18 public class Message extends JSReflection {
19
20     public final String allHeaders;   // pristine headers
21     public final Hashtable headers;   // hash of headers (not including resent's and traces)
22
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;
34
35     // envelope fields
36     public final Address envelopeFrom;
37     public final Address[] envelopeTo;
38
39     public static class StoredMessage extends Message {
40         public final int uid;
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"); };
45     }
46
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 + ">";
51         }
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;
57         }
58         public Address(String s) {
59             s = s.trim();
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('>'));
65             }
66             if (s.indexOf('@') == -1) { /* FIXME */ }
67             description = descrip;
68             user = s.substring(0, s.indexOf('@'));
69             host = s.substring(s.indexOf('@')+1);
70         }
71     }
72
73     public class Trace {
74         final String returnPath = null;
75         final Element[] elements;
76         public class Element {
77             final String fromDomain;
78             final String fromIP;
79             final String toDomain;
80             final String forWhom;
81             final Date date;
82         }
83     }
84
85     // FIXME: support dotTerminatedLikeSMTP
86     public Message(ReadStream rs, boolean dotTermiantedLikeSMTP) {
87         String key = null;
88         StringBuffer all = new StringBuffer();
89         for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) {
90             all.append(s);
91             all.append("\r\n");
92             if (Character.isSpace(s.charAt(0))) {
93                 if (lastKey == null) { /* FIXME */ }
94                 headers.put(lastKey, headers.get(lastKey) + s);
95                 continue;
96             }
97             if (s.indexOf(':') == -1) { /* FIXME */ }
98
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);
102
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                    
110                 } else {
111                     // just append it to the previous one; valid for Comments/Keywords
112                     val = headers.get(key) + " " + val;
113                 } 
114             
115             headers.put(key, val);
116         }
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();
121     }
122
123     // http://www.jwz.org/doc/mid.html
124     public static String generateFreshMessageId() {
125         StringBuffer ret = new StringBuffer();
126         ret.append('<');
127         ret.append(Base36.encode(System.currentTimeMillis()));
128         ret.append('.');
129         ret.append(Base36.encode(random.nextLong()));
130         ret.append('.');
131         try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
132         ret.append('>');
133         return ret.toString();
134     }
135
136     private static final Random random = new Random();
137 }