fixups
[org.ibex.mail.git] / src / org / ibex / mail / Message.java
1 package org.ibex.mail;
2 import org.ibex.crypto.*;
3 import org.ibex.js.*;
4 import org.ibex.util.*;
5 import org.ibex.mail.protocol.*;
6 import java.util.*;
7 import java.net.*;
8 import java.io.*;
9
10 // FIXME MIME: RFC2045, 2046, 2049
11 // NOTE: always use Win32 line endings
12 // hard line limit: 998 chars
13 // soft line limit (suggested): 78 chars
14 // header fields: ascii 33-126 (but no colon)
15 // field body: anything ASCII except CRLF
16 // folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
17 // body needs CRLF; one or the other alone is not acceptable
18 // date/time parsing: see 3.3
19
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
24
25 public class Message extends JSReflection {
26
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
30
31     // parsed header fields
32     public final Date date;
33     public final Address to;
34     public final Address from;        // if multiple From entries, this is sender
35     public final Address replyto;     // if none provided, this is equal to sender
36     public final String subject;
37     public final String messageid;
38     public final Address[] cc;
39     public final Address[] bcc;
40     public final Hashtable[] resent;
41     public final Trace[] traces;
42
43     // envelope fields
44     public final Address envelopeFrom;
45     public final Address[] envelopeTo;
46
47     public void dump(OutputStream os) {
48         Log.error(this, "not implemented");
49     }
50
51     public static class StoredMessage extends Message {
52         public StoredMessage(/*ReadStream rs*/SMTP.LineReader rs, boolean dotTerminatedLikeSMTP) throws IOException {
53             super(rs, dotTerminatedLikeSMTP); uid = -1; }
54         public final int uid;
55         public boolean deleted = false;
56         public boolean read = false;
57         public boolean answered = false;
58         public String dumpStoredForm() { throw new Error("StoredMessage.dumpStoredForm() not implemented"); };
59         public static StoredMessage undump(InputStream os) {
60             Log.error(StoredMessage.class, "not implemented");
61             return null;
62         }
63     }
64
65     public static class Address extends JSReflection {
66         public String coerceToString() {
67             if (description == null || description.equals("")) return user +"@"+ host;
68             return description + " " + "<" + user +"@"+ host + ">";
69         }
70         public final String user;
71         public final String host;
72         public final String description;
73         public Address(String user, String host, String description) {
74             this.user = user; this.host = host; this.description = description;
75         }
76         public Address(String s) {
77             s = s.trim();
78             String descrip = null;
79             if (s.indexOf('<') != -1) {
80                 if (s.indexOf('>') == -1) { /* FIXME */ }
81                 descrip = s.substring(0, s.indexOf('<')) + s.substring(s.indexOf('>') + 1);
82                 s = s.substring(s.indexOf('<') + 1, s.indexOf('>'));
83             }
84             if (s.indexOf('@') == -1) { /* FIXME */ }
85             description = descrip;
86             user = s.substring(0, s.indexOf('@'));
87             host = s.substring(s.indexOf('@')+1);
88         }
89     }
90
91     public class Trace {
92         String returnPath = null;
93         Element[] elements;
94         public class Element {
95             // FIXME final
96             String fromDomain;
97             String fromIP;
98             String toDomain;
99             String forWhom;
100             Date date;
101         }
102     }
103
104     // FIXME: support dotTerminatedLikeSMTP
105     public Message(/*ReadStream rs*/SMTP.LineReader rs, boolean dotTerminatedLikeSMTP) throws IOException {
106         String key = null;
107         StringBuffer all = new StringBuffer();
108         String lastKey = null;
109         replyto = null;
110         subject = null;
111         messageid = null;
112         cc = null;
113         bcc = null;
114         resent = null;
115         traces = null;
116         envelopeFrom = null;
117         envelopeTo = null;
118
119         headers = new Hashtable();
120         date = null; // FIXME
121         to = null;
122         from = null;
123         for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) {
124             all.append(s);
125             all.append("\r\n");
126             if (Character.isSpace(s.charAt(0))) {
127                 if (lastKey == null) { /* FIXME */ }
128                 headers.put(lastKey, headers.get(lastKey) + s);
129                 continue;
130             }
131             if (s.indexOf(':') == -1) { /* FIXME */ }
132
133             key = s.substring(0, s.indexOf(':'));
134             String val = s.substring(0, s.indexOf(':') + 1);
135             while(Character.isSpace(val.charAt(0))) val = val.substring(1);
136
137             if (headers.get(key) != null)
138                 if (key.startsWith("Resent-")) {
139                     // FIXME: multi-resent headers
140                 } else if (key.startsWith("Return-Path:")) {
141                     // FIXME: parse traces, see RFC2821, section 4.4                    
142                 } else if (key.startsWith("Recieved:")) {
143                     // FIXME: parse traces, see RFC2821, section 4.4                    
144                 } else {
145                     // just append it to the previous one; valid for Comments/Keywords
146                     val = headers.get(key) + " " + val;
147                 } 
148             
149             headers.put(key, val);
150         }
151         allHeaders = all.toString();
152         StringBuffer body = new StringBuffer();
153         for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) body.append(s);
154         this.body = body.toString();
155     }
156
157     // http://www.jwz.org/doc/mid.html
158     public static String generateFreshMessageId() {
159         StringBuffer ret = new StringBuffer();
160         ret.append('<');
161         ret.append(Base36.encode(System.currentTimeMillis()));
162         ret.append('.');
163         ret.append(Base36.encode(random.nextLong()));
164         ret.append('.');
165         try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
166         ret.append('>');
167         return ret.toString();
168     }
169
170     private static final Random random = new Random();
171 }