2 import org.ibex.crypto.*;
4 import org.ibex.util.*;
5 import org.ibex.mail.protocol.*;
10 // soft line limit (suggested): 78 chars / hard line limit: 998 chars
11 // folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
12 // date/time parsing: see spec, 3.3
14 // FEATURE: MIME RFC2045, 2046, 2049
15 // FEATURE: PGP-signature-parsing
16 // FEATURE: mailing list header parsing
17 // FEATURE: delivery status notification (and the sneaky variety)
18 // FEATURE: threading as in http://www.jwz.org/doc/threading.html
20 public class Message extends JSReflection {
22 // FIXME: case-insensitive header hashmap
23 public int rfc822size() { return -1; } // FIXME
24 public int numLines() { return -1; } // FIXME
25 public int messageNum = -1;
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 public final Date date;
32 public final Address to;
33 public final Address from; // if multiple From entries, this is sender
34 public final Address replyto; // if none provided, this is equal to sender
35 public final String subject;
36 public final String messageid;
37 public final Address[] cc;
38 public final Address[] bcc;
39 public final Hashtable[] resent;
40 public final Trace[] traces;
42 public final Address envelopeFrom;
43 public final Address[] envelopeTo;
45 public final Date arrival; // when the message first arrived at this machine; IMAP "internal date message attr"
47 public void dump(OutputStream os) throws IOException {
48 Writer w = new OutputStreamWriter(os);
50 w.write("X-IbexMail-EnvelopeFrom: " + envelopeFrom + "\r\n");
51 w.write("X-IbexMail-EnvelopeTo: "); for(int i=0; i<envelopeTo.length; i++) w.write(envelopeTo[i] + " "); w.write("\r\n");
58 public boolean deleted = false;
59 public boolean seen = false;
60 public boolean flagged = false;
61 public boolean draft = false;
62 public boolean answered = false;
63 public boolean recent = false;
67 public static class StoredMessage extends Message {
70 public final int uidValidity; // must be reset if mailbox is deleted and then recreated
71 public final int sequenceNumber; // starts at 1; numbers reshuffled upon EXPUNGE
72 public Hash keywords = new Hash();
73 public boolean deleted = false;
74 public boolean seen = false;
75 public boolean flagged = false;
76 public boolean draft = false;
77 public boolean answered = false;
78 public boolean recent = false; // "Message is "recently" arrived in this mailbox. This
79 // session is the first session to have been notified
80 // about this message;
81 public StoredMessage(LineReader rs) throws IOException, MailException.Malformed { super(rs); }
86 final String returnPath;
87 final Element[] elements;
88 public Trace(LineReader lr) throws Trace.Malformed, IOException {
89 String retPath = lr.readLine();
90 if (!retPath.startsWith("Return-Path:")) throw new Trace.Malformed("trace did not start with Return-Path header");
91 returnPath = retPath.substring(12).trim();
94 String s = lr.readLine();
96 if (!s.startsWith("Received:")) { lr.pushback(s); break; }
97 s = s.substring(9).trim();
98 el.addElement(new Element(s));
100 elements = new Element[el.size()];
101 el.copyInto(elements);
103 public class Element {
109 public Element(String fromDomain, String fromIP, String toDomain, String forWhom, Date date) {
110 this.fromDomain=fromDomain; this.fromIP=fromIP; this.toDomain=toDomain; this.forWhom=forWhom; this.date=date; }
111 public Element(String s) throws Trace.Malformed {
112 StringTokenizer st = new StringTokenizer(s);
113 if (!st.nextToken().equals("FROM")) throw new Trace.Malformed("trace did note have a FROM element: " + s);
114 fromDomain = st.nextToken();
115 if (!st.nextToken().equals("BY")) throw new Trace.Malformed("trace did note have a BY element: " + s);
116 toDomain = st.nextToken();
117 // FIXME not done yet
120 public class Malformed extends Message.Malformed { public Malformed(String s) { super(s); } }
123 public static class Malformed extends MailException.Malformed { public Malformed(String s) { super(s); } }
124 public Message(Address envelopeFrom, Address[] envelopeTo, LineReader rs) throws IOException, MailException.Malformed {
125 this.envelopeFrom = envelopeFrom;
126 this.envelopeTo = envelopeTo;
127 this.arrival = new Date();
128 this.headers = new Hashtable();
130 StringBuffer all = new StringBuffer();
132 Address to = null, from = null, replyto = null;
133 String subject = null, messageid = null;
134 Vec cc = new Vec(), bcc = new Vec(), resent = new Vec(), traces = new Vec();
135 for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) {
138 if (s.length() == 0 || Character.isSpace(s.charAt(0))) {
139 if (key == null) throw new Malformed("Message began with a blank line; no headers");
140 headers.put(key, headers.get(key) + s);
143 if (s.indexOf(':') == -1) throw new Malformed("Header line does not contain colon: " + s);
144 key = s.substring(0, s.indexOf(':'));
145 for(int i=0; i<key.length(); i++)
146 if (key.charAt(i) < 33 || key.charAt(i) > 126)
147 throw new Malformed("Header key \""+key+"\" contains invalid character \"" + key.charAt(i) + "\"");
148 String val = s.substring(s.indexOf(':') + 1).trim();
149 while(Character.isSpace(val.charAt(0))) val = val.substring(1);
150 if (key.startsWith("Resent-")) {
151 if (key.startsWith("Resent-From")) resent.addElement(new Hashtable());
152 ((Hashtable)resent.lastElement()).put(key.substring(7), val);
153 } else if (key.startsWith("Return-Path:")) {
154 rs.pushback(s); traces.addElement(new Trace(rs));
156 // just append it to the previous one; valid for Comments/Keywords
157 if (headers.get(key) != null) val = headers.get(key) + " " + val;
158 headers.put(key, val);
162 this.date = (Date)headers.get("Date");
163 this.to = new Address((String)headers.get("To")); // FIXME what if null?
164 this.from = headers.get("From") == null ? envelopeFrom : new Address((String)headers.get("From"));
165 this.replyto = headers.get("Reply-To") == null ? null : new Address((String)headers.get("Reply-To"));
166 this.subject = (String)headers.get("Subject");
167 this.messageid = (String)headers.get("Message-Id");
168 if (headers.get("Cc") != null) {
169 StringTokenizer st = new StringTokenizer((String)headers.get("Cc"));
170 this.cc = new Address[st.countTokens()];
171 for(int i=0; i<this.cc.length; i++) this.cc[i] = new Address(st.nextToken());
173 this.cc = new Address[0];
175 if (headers.get("Bcc") != null) {
176 StringTokenizer st = new StringTokenizer((String)headers.get("Bcc"));
177 this.bcc = new Address[st.countTokens()];
178 for(int i=0; i<this.bcc.length; i++) this.bcc[i] = new Address(st.nextToken());
180 this.bcc = new Address[0];
182 resent.copyInto(this.resent = new Hashtable[resent.size()]);
183 traces.copyInto(this.traces = new Trace[traces.size()]);
184 allHeaders = all.toString();
185 StringBuffer body = new StringBuffer();
186 for(String s = rs.readLine();; s = rs.readLine()) { if (s == null) break; else body.append(s + "\r\n"); }
187 this.body = body.toString();
190 // http://www.jwz.org/doc/mid.html
191 private static final Random random = new Random();
192 public static String generateFreshMessageId() {
193 StringBuffer ret = new StringBuffer();
195 ret.append(Base36.encode(System.currentTimeMillis()));
197 ret.append(Base36.encode(random.nextLong()));
199 try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
201 return ret.toString();
204 public String summary() {
206 " Subject: " + subject + "\n" +
207 " EnvelopeFrom: " + envelopeFrom + "\n" +
208 " EnvelopeTo: " + envelopeTo + "\n" +
209 " MessageId: " + messageid;
212 public Message bounce(String reason) {
213 // use null-sender for error messages (don't send errors to the null addr)
215 throw new RuntimeException("bounce not implemented");