+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
package org.ibex.mail;
import org.ibex.crypto.*;
import org.ibex.util.*;
*/
public class Message extends MIME.Part {
- // Envelope //////////////////////////////////////////////////////////////////////////////
-
- public final Envelope envelope;
- public static class Envelope extends org.ibex.js.JSReflection {
- public Envelope(Address from, Address to, Date arrival) { this.to = to; this.from = from; this.arrival = arrival; }
- public final Date arrival;
- public final Address to;
- public final Address from;
- public static Envelope augment(Envelope e, MIME.Headers h) {
- if (e.from != null && e.to != null) return e;
- Address to = e.to == null ? Address.parse(h.get("X-org.ibex.mail.headers.envelope.To")) : e.to;
- Address from = e.from == null ? Address.parse(h.get("X-org.ibex.mail.headers.envelope.From")) : e.from;
- return new Envelope(from, to, e.arrival);
- }
- }
-
-
// Parsed Headers //////////////////////////////////////////////////////////////////////////////
public final Address to;
public final Address from; // if multiple From entries, this is sender
+ public final Address envelopeFrom;
+ public final Address envelopeTo;
public final Date date;
+ public final Date arrival;
public final Address replyto; // if none provided, this is equal to sender
public final String subject;
public final String messageid;
public final Address[] cc;
public final Address[] bcc;
- public final Hashtable[] resent;
- public Message(Stream stream, Envelope envelope) throws Malformed {
+ public static Message newMessage(Stream stream) throws Malformed { return newMessage(stream, null, null); }
+ public static Message newMessage(Stream stream, Address from, Address to) throws Malformed {
+ if (from == null && to == null) return new Message(stream);
+ StringBuffer sb = new StringBuffer();
+ boolean inheaders = true;
+ if (from != null) sb.append("Return-Path: " + from.toString(true) + "\r\n");
+ while(true) {
+ String s = stream.readln();
+ if (s == null) break;
+ if (inheaders && to != null && s.toLowerCase().startsWith("envelope-to:")) continue;
+ if (inheaders && from != null && s.toLowerCase().startsWith("return-path:")) continue;
+ if (s.length() == 0 && inheaders) {
+ inheaders = false;
+ if (to != null) sb.append("Envelope-To: " + to.toString(true) + "\r\n");
+ }
+ sb.append(s);
+ sb.append("\r\n");
+ }
+ return newMessage(new Stream(sb.toString()));
+ }
+ private Message(Stream stream) throws Malformed {
super(stream, null, false);
- Vec resent = new Vec(), traces = new Vec();
- this.envelope = Envelope.augment(envelope, headers);
- this.to = headers.get("To") == null ? envelope.to : Address.parse(headers.get("To"));
- this.from = headers.get("From") == null ? envelope.from : Address.parse(headers.get("From"));
- this.replyto = headers.get("Reply-To") == null ? null : Address.parse(headers.get("Reply-To"));
- this.subject = headers.get("Subject");
- this.messageid = headers.get("Message-Id");
- this.cc = Address.list(headers.get("Cc"));
- this.bcc = Address.list(headers.get("BCc"));
- this.date = parseDate(headers.get("Date"));
- resent.copyInto(this.resent = new Hashtable[resent.size()]);
- traces.copyInto(this.traces = new Trace[traces.size()]);
+ this.envelopeTo = headers.gets("Envelope-To") != null ? Address.parse(headers.gets("Envelope-To")) : null;
+ this.envelopeFrom = headers.gets("Return-Path") != null ? Address.parse(headers.gets("Return-Path")) : null;
+ this.to = headers.gets("To") != null ? Address.parse(headers.gets("To")) : this.envelopeTo;
+ this.from = headers.gets("From") != null ? Address.parse(headers.gets("From")) : this.envelopeFrom;
+ this.replyto = headers.gets("Reply-To") == null ? null : Address.parse(headers.gets("Reply-To"));
+ this.subject = headers.gets("Subject");
+ this.messageid = headers.gets("Message-Id");
+ this.cc = Address.list(headers.gets("Cc"));
+ this.bcc = Address.list(headers.gets("BCc"));
+ this.date = parseDate(headers.gets("Date")) == null ? new Date() : parseDate(headers.gets("Date"));
+ this.arrival = this.date; // FIXME wrong
}
public static Date parseDate(String s) { return null; } // FIXME!!!
// use null-sender for error messages (don't send errors to the null addr)
- public Message bounce(String reason) { throw new RuntimeException("bounce not implemented"); } // FIXME!
-
- public String summary() {
- return
- " Subject: " + subject + "\n" +
- " EnvelopeFrom: " + envelope.from + "\n" +
- " EnvelopeTo: " + envelope.to + "\n" +
- " MessageId: " + messageid;
- }
+ public Message bounce(String reason) {
+ Log.warn(Message.class, "bounce not implemented");
+ return null;
+ } // FIXME!
+
+ public String summary() { return "[" + envelopeFrom + " -> " + envelopeTo + "] " + subject; }
public void dump(Stream s) {
s.setNewline("\r\n");
s.println(headers.raw);
- s.println();
+ s.println("");
s.println(body);
s.flush();
}
public int size() { return headers.raw.length() + 2 /* CRLF */ + body.length(); }
public String toString() { return headers.raw + "\r\n" + body; }
-
-
- // SMTP Traces //////////////////////////////////////////////////////////////////////////////
-
- public final Trace[] traces;
- public class Trace {
- final String returnPath;
- final Element[] elements;
- public Trace(Stream stream) throws Trace.Malformed {
- String retPath = stream.readln();
- if (!retPath.startsWith("Return-Path:")) throw new Trace.Malformed("trace did not start with Return-Path header");
- returnPath = retPath.substring(12).trim();
- Vec el = new Vec();
- while(true) {
- String s = stream.readln();
- if (s == null) break;
- if (!s.startsWith("Received:")) { stream.unread(s); break; }
- s = s.substring(9).trim();
- el.addElement(new Element(s));
- }
- elements = new Element[el.size()];
- el.copyInto(elements);
- }
- public class Element {
- String fromDomain;
- String fromIP;
- String toDomain;
- String forWhom;
- Date date;
- public Element(String fromDomain, String fromIP, String toDomain, String forWhom, Date date) {
- this.fromDomain=fromDomain; this.fromIP=fromIP; this.toDomain=toDomain; this.forWhom=forWhom; this.date=date; }
- public Element(String s) throws Trace.Malformed {
- StringTokenizer st = new StringTokenizer(s);
- if (!st.nextToken().equals("FROM")) throw new Trace.Malformed("trace did note have a FROM element: " + s);
- fromDomain = st.nextToken();
- if (!st.nextToken().equals("BY")) throw new Trace.Malformed("trace did note have a BY element: " + s);
- toDomain = st.nextToken();
- // FIXME not done yet
- }
- }
- public class Malformed extends Message.Malformed { public Malformed(String s) { super(s); } }
- }
+ public String coerceToString() { return toString(); }
public static class Malformed extends Exception { public Malformed(String s) { super(s); } }
-
}
- /*
- if (key.startsWith("Resent-")) {
- if (resent.size() == 0 || key.startsWith("Resent-From")) resent.addElement(new Hashtable());
- ((Hashtable)resent.lastElement()).put(key.substring(7), val);
- } else if (key.startsWith("Return-Path")) {
- stream.unread(s);
- traces.addElement(new Trace(stream));
- */