removed Message.Envelope class
authoradam <adam@megacz.com>
Sat, 30 Oct 2004 08:44:12 +0000 (08:44 +0000)
committeradam <adam@megacz.com>
Sat, 30 Oct 2004 08:44:12 +0000 (08:44 +0000)
darcs-hash:20041030084412-5007d-dc2094ce855e76ca07f92ddba08cbaa142d80ad7.gz

src/org/ibex/mail/Message.java
src/org/ibex/mail/Query.java
src/org/ibex/mail/protocol/IMAP.java
src/org/ibex/mail/protocol/SMTP.java
src/org/ibex/mail/target/FileBasedMailbox.java

index d1434ad..ea06f0c 100644 (file)
@@ -28,49 +28,57 @@ import java.io.*;
  */
 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 != null && e.from != null && e.to != null) return e;
-            Address to   = (e==null||e.to==null)   ? Address.parse(h.gets("X-org.ibex.mail.headers.envelope.To"))   : e.to;
-            Address from = (e==null||e.from==null) ? Address.parse(h.gets("X-org.ibex.mail.headers.envelope.From")) : e.from;
-            return new Envelope(from, to, e==null ? new Date() : 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 newMessage(stream);
+        StringBuffer sb = new StringBuffer();
+        boolean inheaders = true;
+        if (from != null) sb.append("Return-Path: " + from.toString(true));
+        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 = 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.gets("To") == null       ? this.envelope.to    : Address.parse(headers.gets("To"));
-        this.from         = headers.gets("From") == null     ? this.envelope.from  : Address.parse(headers.gets("From"));
-        this.replyto      = headers.gets("Reply-To") == null ? null                : Address.parse(headers.gets("Reply-To"));
+        this.to           = headers.gets("To") != null ? Address.parse(headers.gets("To")) : 
+                            headers.gets("Envelope-To") != null ? Address.parse(headers.gets("Envelope-To")) : null;
+        this.from         = headers.gets("From") != null ? Address.parse(headers.gets("From")) : 
+                            headers.gets("Return-Path") != null ? Address.parse(headers.gets("Return-Path")) : null;
+        this.envelopeTo   = headers.gets("Envelope-To") != null ? Address.parse(headers.gets("Envelope-To")) :
+                            headers.gets("To") != null ? Address.parse(headers.gets("To")) : null;
+        this.envelopeFrom = headers.gets("Return-Path") != null ? Address.parse(headers.gets("Return-Path")) :
+                            headers.gets("From") != null ? Address.parse(headers.gets("From")) : null;
+        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"));
-        resent.copyInto(this.resent = new Hashtable[resent.size()]);
-        traces.copyInto(this.traces = new Trace[traces.size()]);
+        this.date         = headers.gets("Date") == null ? new Date() : parseDate(headers.gets("Date"));
+        this.arrival      = this.date; // FIXME wrong
     }
 
 
@@ -98,7 +106,7 @@ public class Message extends MIME.Part {
         return null;
     }  // FIXME!
 
-    public String summary() { return "[" + envelope.from + " -> " + envelope.to + "] " + subject; }
+    public String summary() { return "[" + envelopeFrom + " -> " + envelopeTo + "] " + subject; }
 
     public void dump(Stream s) {
         s.setNewline("\r\n");
@@ -111,57 +119,6 @@ public class Message extends MIME.Part {
     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 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));
-        */
index 2865dbd..6b024af 100644 (file)
@@ -91,8 +91,8 @@ public class Query {
                              else return it.num() >= min && it.num() <= max;
             case SENT:       return (latest==null||it.cur().date.before(latest)) && 
                                     (earliest==null||it.cur().date.after(earliest));
-            case ARRIVAL:    return (latest == null || it.cur().envelope.arrival.before(latest)) &&
-                                    (earliest == null || it.cur().envelope.arrival.after(earliest));
+            case ARRIVAL:    return (latest == null || it.cur().arrival.before(latest)) &&
+                                    (earliest == null || it.cur().arrival.after(earliest));
             case SIZE:       return it.cur().size() >= min && it.cur().size() <= max;
             case HEADER:     return it.cur().headers.get(key) != null &&
                                  ((String)it.cur().headers.get(key)).toLowerCase().indexOf(text.toLowerCase()) != -1;
index 5a15e02..49ac90f 100644 (file)
@@ -406,7 +406,7 @@ public class IMAP {
                 if (s.equals("BODYSTRUCTURE")) {       spec|=BODYSTRUCTURE;if(e){r.append(" ");r.append(Printer.bodystructure(m));}
                 } else if (s.equals("ENVELOPE")) {     spec|=ENVELOPE;     if(e){r.append(" ");r.append(Printer.envelope(m));}
                 } else if (s.equals("FLAGS")) {        spec|=FLAGS;        if(e){r.append(" ");r.append(Printer.flags(flags));}
-                } else if (s.equals("INTERNALDATE")) { spec|=INTERNALDATE; if(e){r.append(" ");r.append(Printer.date(m.envelope.arrival));}
+                } else if (s.equals("INTERNALDATE")) { spec|=INTERNALDATE; if(e){r.append(" ");r.append(Printer.date(m.arrival));}
                 } else if (s.equals("RFC822")) {       spec|=RFC822;       if(e){r.append(" ");r.append(Printer.message(m));}
                 } else if (s.equals("RFC822.TEXT")) {  spec|=RFC822TEXT;   if(e){r.append(" ");r.append(Printer.qq(m.body));}
                 } else if (s.equals("RFC822.HEADER")){ spec|=HEADER;if(e){r.append(" ");r.append(Printer.qq(m.headers.raw+"\r\n"));}
@@ -763,7 +763,7 @@ public class IMAP {
         static String date(Date d) { return "\""+d.toString()+"\""; }
         static String envelope(Message m) {
             return
-                "(" + quotify(m.envelope.arrival.toString()) +
+                "(" + quotify(m.arrival.toString()) +
                 " " + quotify(m.subject) +          
                 " " + addressList(m.from) +      
                 " " + addressList(m.headers.gets("sender")) +
index b47585f..46b4c6e 100644 (file)
@@ -71,6 +71,10 @@ public class SMTP {
                     conn.flush();
                     try {
                         StringBuffer buf = new StringBuffer();
+                        buf.append("Received: from " + conn.getRemoteHostname() + " (" + remotehost + ")\r\n");
+                        buf.append("          by "+conn.vhost+" ("+SMTP.class.getName()+") with "+(ehlo?"ESMTP":"SMTP") + "\r\n");
+                        buf.append("          for "); for(int i=0; i<to.size(); i++) buf.append(to.elementAt(i) + " ");
+                        buf.append("; " + dateFormat.format(new Date()) + "\r\n");
                         while(true) {
                             String s = conn.readln();
                             if (s == null) throw new RuntimeException("connection closed");
@@ -120,13 +124,13 @@ public class SMTP {
         }
 
         public static boolean attempt(Message m) throws IOException {
-            InetAddress[] mx = getMailExchangerIPs(m.envelope.to.host);
+            InetAddress[] mx = getMailExchangerIPs(m.envelopeTo.host);
             if (mx.length == 0) {
                 Log.warn(SMTP.Outgoing.class, "could not resolve " + m.envelopeTo.host + "; bouncing it\n" + m.summary());
                 accept(m.bounce("could not resolve " + m.envelopeTo.host));
                 return true;
             }
-            if (new Date().getTime() - m.envelope.arrival.getTime() > 1000 * 60 * 60 * 24 * 5) {
+            if (new Date().getTime() - m.arrival.getTime() > 1000 * 60 * 60 * 24 * 5) {
                 Log.warn(SMTP.Outgoing.class, "could not send message after 5 days; bouncing it\n" + m.summary());
                 accept(m.bounce("could not send for 5 days"));
                 return true;
@@ -156,7 +160,17 @@ public class SMTP {
                 conn.println("MAIL FROM:<" + m.envelope.from.user + "@" + m.envelope.from.host+">");  check(conn.readln(), conn);
                 conn.println("RCPT TO:<"   + m.envelope.to.user + "@" + m.envelope.to.host+">");      check(conn.readln(), conn);
                 conn.println("DATA");                          check(conn.readln(), conn);
-                conn.println(m.toString());
+                Stream stream = new Stream(m.toString());
+                boolean inheaders = true;
+                while(true) {
+                    String s = stream.readln();
+                    if (s == null) break;
+                    if (s.length() == 0) inheaders = false;
+                    // quash Return-Path; required by RFC2822
+                    if (inheaders && s.toLowerCase().startsWith("Return-Path:")) continue;
+                    if (s.startsWith(".")) conn.print(".");
+                    conn.println(s);
+                }
                 conn.println(".");
                 check(conn.readln(), conn);
                 Log.warn(SMTP.Outgoing.class, "success: " + mx + " accepted " + m.summary());
index d8849c7..aaedf52 100644 (file)
@@ -216,10 +216,6 @@ public class FileBasedMailbox extends Mailbox.Default {
             }
             FileOutputStream fo = new FileOutputStream(f);
             Stream stream = new Stream(fo);
-            if (message.envelope != null) {
-                stream.println("X-org.ibex.mail.headers.envelope.From: " + message.envelope.from);
-                stream.println("X-org.ibex.mail.headers.envelope.To: " + message.envelope.to);
-            }
             message.dump(stream);
             fo.close();
             f.renameTo(new File(fullname));