2 import org.ibex.crypto.*;
4 import org.ibex.util.*;
5 import org.ibex.mail.protocol.*;
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
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
25 public class Message extends JSReflection {
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
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;
44 public final Address envelopeFrom;
45 public final Address[] envelopeTo;
47 public void dump(OutputStream os) {
48 Log.error(this, "not implemented");
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; }
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");
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 + ">";
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;
76 public Address(String s) {
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('>'));
84 if (s.indexOf('@') == -1) { /* FIXME */ }
85 description = descrip;
86 user = s.substring(0, s.indexOf('@'));
87 host = s.substring(s.indexOf('@')+1);
92 String returnPath = null;
94 public class Element {
104 // FIXME: support dotTerminatedLikeSMTP
105 public Message(/*ReadStream rs*/SMTP.LineReader rs, boolean dotTerminatedLikeSMTP) throws IOException {
107 StringBuffer all = new StringBuffer();
108 String lastKey = null;
119 headers = new Hashtable();
120 date = null; // FIXME
123 for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) {
126 if (Character.isSpace(s.charAt(0))) {
127 if (lastKey == null) { /* FIXME */ }
128 headers.put(lastKey, headers.get(lastKey) + s);
131 if (s.indexOf(':') == -1) { /* FIXME */ }
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);
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
145 // just append it to the previous one; valid for Comments/Keywords
146 val = headers.get(key) + " " + val;
149 headers.put(key, val);
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();
157 // http://www.jwz.org/doc/mid.html
158 public static String generateFreshMessageId() {
159 StringBuffer ret = new StringBuffer();
161 ret.append(Base36.encode(System.currentTimeMillis()));
163 ret.append(Base36.encode(random.nextLong()));
165 try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
167 return ret.toString();
170 private static final Random random = new Random();