implemented bounce messages
[org.ibex.mail.git] / src / org / ibex / mail / Message.java
index 4d02e43..7c5635f 100644 (file)
@@ -54,7 +54,7 @@ public class Message extends MIME.Part {
             String s = stream.readln();
             if (s == null) break;
             if (to != null && s.toLowerCase().startsWith("envelope-to:")) continue;
-            if (from != null && s.toLowerCase().startsWith("return-path:")) continue;
+            if (s.toLowerCase().startsWith("return-path:")) continue;
             if (s.length() == 0) {
                 if (to != null) sb.append("Envelope-To: " + to.toString(true) + "\r\n");
                 sb.append("\r\n");
@@ -107,12 +107,37 @@ public class Message extends MIME.Part {
         // date/time parsing: see spec, 3.3
         return null;
     }
-   
-    //  use null-sender for error messages (don't send errors to the null addr)
+
+    // this is belived to be compliant with QSBMF (http://cr.yp.to/proto/qsbmf.txt)
     public Message bounce(String reason) {
-        Log.warn(Message.class, "bounce not implemented");
-        return null;
-    }  // FIXME!
+        if (envelopeFrom==null || envelopeFrom.toString().equals("")) return null;
+
+        Headers h = new Headers(headers.getStream());
+        h.put("Envelope-To", envelopeFrom.toString());
+        h.put("Return-Path", "<>");
+        h.put("From",        "MAILER-DAEMON");
+        h.put("To",          envelopeFrom.toString());
+        h.put("Subject",     "failure notice");
+
+        String error =
+            "Hi. This is the Ibex Mail Server.  I'm afraid I wasn't able to deliver\r\n"+
+            "your message to the following addresses. This is a permanent error;\r\n"+
+            "I've given up.  Sorry it didn't work out\r\n."+
+            "\r\n"+
+            "<"+envelopeTo.toString()+">:\r\n"+
+            reason+"\r\n"+
+            "\r\n"+
+            "--- Below this line is a copy of the message.\r\n"+
+            "\r\n";
+
+        try {
+            return newMessage(new Fountain.Concatenate(new Fountain.StringFountain(h.getString()+"\r\n"+error), getBody()));
+        } catch (Message.Malformed e) {
+            Log.error(this, "caught Message.Malformed in Message.bounce(); this should never happen");
+            Log.error(this, e);
+            return null;
+        }
+    }
 
     public String toString() { throw new RuntimeException("Message.toString() called"); }
     public String summary() { return "[" + envelopeFrom + " -> " + envelopeTo + "] " + subject; }