// Copyright 2000-2006 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 java.io.*;
import java.util.*;
import org.ibex.js.*;
import org.ibex.io.*;
import org.ibex.io.Fountain;
// FIXME FIXME: rate limiting
// FIXME: DSN's: RFC 3464 ==> NOTIFY=NEVER
// FIXME: MDN's: RFC 3798
// FIXME
// SHOULD NOT issue the same response to the same sender more
// than once within a period of several days, even though that
// sender may have sent multiple messages. A 7-day period is
// RECOMMENDED as a default.
// Personal and Group responses whose purpose is to notify the
// sender of a message of a temporary absence of the recipient
// (e.g., "vacation" and "out of the office" notices) SHOULD
// NOT be issued unless a valid address for the recipient is
// explicitly included in a recipient (e.g., To, Cc, Bcc,
// Resent-To, Resent-Cc, or Resent- Bcc) field of the subject
// message.
// FIXME: be sure to never, ever return large responses (> a few kb)
/** Implements RFC3834-safe/compliant auto-generated messages+replies */
public class Auto {
/**
* Compose an auto-generated message (not a reply) compliant with RFC3834.
*
* @param from
* personal: should be the recipient of the message, might guess from message, MUST be customizable
* service: should be a human, and description should describe the service
* @param includeAutoInSubject if true; the string "Auto: " is prepended to the subject
*/
public Message autoGenerate(Headers headers, Fountain body, Address autoFrom, Address autoTo, boolean includeAutoInSubject) {
return Message.newMessageFromHeadersAndBody(new Headers(headers, new String[] {
"Auto-Submitted", "auto-generated",
"Precedence", "bulk",
"Subject", (includeAutoInSubject ? "Auto: " : "")+headers.get("subject"),
"From", autoFrom.toString(true),
"Reply-To", autoFrom.toString(false)
}),
body, null, autoTo);
}
/**
* Compose an auto-generated reply compliant with RFC3834.
*
* @param from
* personal: should be the recipient of the message, might guess from message, MUST be customizable
* service: should be a human, and description should describe the service
* @param includeAutoInSubject if true; the string "Auto: " is prepended to the subject
*/
public Message autoReply(Fountain fountain, Message m, Address autoFrom, boolean includeAutoInSubject) {
if (m.envelopeFrom==null) return null;
String auto = m.headers.get("auto-submittted");
if (auto!=null && !auto.equals("no")) return null;
String precedence = m.headers.get("precedence");
if (precedence != null) precedence = precedence.trim().toLowerCase();
if ("bulk".equals(precedence)) return null;
if ("list".equals(precedence)) return null;
if ("junk".equals(precedence)) return null;
// recommended, not required.
if (m.envelopeFrom.user.endsWith("-request")) return null;
if (m.envelopeFrom.user.startsWith("owner-")) return null;
if (m.envelopeFrom.description.equalsIgnoreCase("mailer-daemon")) return null;
for(String key : m.headers.getHeaderNames())
if (key.toLowerCase().startsWith("list-")) return null;
return m.reply(new String[] {
"Auto-Submitted", "auto-replied",
"Precedence", "bulk",
"Subject", (includeAutoInSubject ? "Auto: " : "")+m.subject,
"From", autoFrom.toString(true),
"Reply-To", autoFrom.toString(false)
},
fountain, null, false);
}
}