- public static class Malformed extends MailException.Malformed { public Malformed(String s) { super(s); } }
- public Message(Address envelopeFrom, Address envelopeTo, LineReader rs) throws IOException, Malformed {
- this.envelopeFrom = envelopeFrom;
- this.envelopeTo = envelopeTo;
- this.arrival = new Date();
- this.headers = new Hashtable();
- String key = null;
- StringBuffer all = new StringBuffer();
- Date date = null;
- Address to = null, from = null, replyto = null;
- String subject = null, messageid = null;
- Vec cc = new Vec(), bcc = new Vec(), resent = new Vec(), traces = new Vec();
- for(String s = rs.readLine(); s != null && !s.equals(""); s = rs.readLine()) {
- all.append(s);
- all.append("\r\n");
- if (s.length() == 0 || Character.isSpace(s.charAt(0))) {
- if (key == null) throw new Malformed("Message began with a blank line; no headers");
- headers.put(key, headers.get(key) + s);
- continue;
- }
- if (s.indexOf(':') == -1) throw new Malformed("Header line does not contain colon: " + s);
- key = s.substring(0, s.indexOf(':'));
- for(int i=0; i<s.length(); i++)
- if (s.charAt(i) < 33 || s.charAt(i) > 126)
- throw new Malformed("Header key contains invalid character \"" + s.charAt(i) + "\"");
- String val = s.substring(0, s.indexOf(':'));
- while(Character.isSpace(val.charAt(0))) val = val.substring(1);
- if (key.startsWith("Resent-")) {
- if (key.startsWith("Resent-From")) resent.addElement(new Hashtable());
- ((Hashtable)resent.lastElement()).put(key.substring(7), val);
- } else if (key.startsWith("Return-Path:")) {
- rs.pushback(s); traces.addElement(new Trace(rs));
- } else {
- // just append it to the previous one; valid for Comments/Keywords
- if (headers.get(key) != null) val = headers.get(key) + " " + val;
- headers.put(key, val);
- }
- }
+ this.envelopeTo = headers.get("Envelope-To") != null ? Address.parse(headers.get("Envelope-To")) : null;
+ this.envelopeFrom = headers.get("Return-Path") != null ? Address.parse(headers.get("Return-Path")) : null;
+ this.to = headers.get("To") != null ? Address.parse(headers.get("To")) : this.envelopeTo;
+ this.from = headers.get("From") != null ? Address.parse(headers.get("From")) : this.envelopeFrom;
+ this.replyto = headers.get("Reply-To") == null ? null : Address.parse(headers.get("Reply-To"));
+ this.subject = headers.get("Subject");
+ String messageid = headers.get("Message-Id");
+ this.cc = Address.list(headers.get("Cc"));
+ this.bcc = Address.list(headers.get("Bcc"));
+ Date date = RobustDateParser.parseDate(headers.get("Date"));
+ this.date = date==null ? new Date() : date;
+ this.arrival = this.date; // FIXME wrong: should grab this from traces, I think?