reorganize Auto.java
[org.ibex.mail.git] / src / org / ibex / mail / Auto.java
1 // Copyright 2000-2006 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.mail;
6 import java.io.*;
7 import java.util.*;
8 import org.ibex.js.*;
9 import org.ibex.io.*;
10 import org.ibex.io.Fountain;
11
12 // FIXME FIXME: rate limiting
13
14 // FIXME: DSN's: RFC 3464 ==> NOTIFY=NEVER
15 // FIXME: MDN's: RFC 3798
16
17 // FIXME
18 // SHOULD NOT issue the same response to the same sender more
19 // than once within a period of several days, even though that
20 // sender may have sent multiple messages.  A 7-day period is
21 // RECOMMENDED as a default.
22
23 // Personal and Group responses whose purpose is to notify the
24 // sender of a message of a temporary absence of the recipient
25 // (e.g., "vacation" and "out of the office" notices) SHOULD
26 // NOT be issued unless a valid address for the recipient is
27 // explicitly included in a recipient (e.g., To, Cc, Bcc,
28 // Resent-To, Resent-Cc, or Resent- Bcc) field of the subject
29 // message.
30
31 // FIXME: be sure to never, ever return large responses (> a few kb)
32
33 /** Implements RFC3834-safe/compliant auto-generated messages+replies */
34 public class Auto {
35
36     /**
37      *  Compose an auto-generated message (not a reply) compliant with RFC3834.
38      *
39      *  @param from
40      *    <b>personal</b>: should be the recipient of the message, might guess from message, MUST be customizable<br>
41      *    <b>service</b>:  should be a human, and description should describe the service
42      *  @param includeAutoInSubject if true; the string "Auto: " is prepended to the subject
43      */
44     public Message autoGenerate(Headers headers, Fountain body, Address autoFrom, Address autoTo, boolean includeAutoInSubject) {
45         return Message.newMessageFromHeadersAndBody(new Headers(headers, new String[] {
46             "Auto-Submitted", "auto-generated",
47             "Precedence",     "bulk",
48             "Subject",        (includeAutoInSubject ? "Auto: " : "")+headers.get("subject"),
49             "From",           autoFrom.toString(true),
50             "Reply-To",       autoFrom.toString(false)
51         }),
52                                                     body, null, autoTo);
53     }
54
55     /**
56      *  Compose an auto-generated reply compliant with RFC3834.
57      *
58      *  @param from
59      *    <b>personal</b>: should be the recipient of the message, might guess from message, MUST be customizable<br>
60      *    <b>service</b>:  should be a human, and description should describe the service
61      *  @param includeAutoInSubject if true; the string "Auto: " is prepended to the subject
62      */
63     public Message autoReply(Fountain fountain, Message m, Address autoFrom, boolean includeAutoInSubject) {
64
65         if (m.envelopeFrom==null) return null;
66
67         String auto = m.headers.get("auto-submittted");
68         if (auto!=null && !auto.equals("no"))                             return null;
69
70         String precedence = m.headers.get("precedence");
71         if (precedence != null) precedence = precedence.trim().toLowerCase();
72         if ("bulk".equals(precedence)) return null;
73         if ("list".equals(precedence)) return null;
74         if ("junk".equals(precedence)) return null;
75
76         // recommended, not required.
77         if (m.envelopeFrom.user.endsWith("-request"))                     return null;
78         if (m.envelopeFrom.user.startsWith("owner-"))                     return null;
79         if (m.envelopeFrom.description.equalsIgnoreCase("mailer-daemon")) return null;
80         for(String key : m.headers.getHeaderNames())
81             if (key.toLowerCase().startsWith("list-")) return null;
82
83         return m.reply(new String[] {
84             "Auto-Submitted", "auto-replied",
85             "Precedence",     "bulk",
86             "Subject",        (includeAutoInSubject ? "Auto: " : "")+m.subject,
87             "From",           autoFrom.toString(true),
88             "Reply-To",       autoFrom.toString(false)
89         },
90                        fountain, null, false);
91     }
92
93 }