1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.transport.mailets;
23
24 import org.apache.mailet.base.MailetUtil;
25 import org.apache.mailet.base.RFC2822Headers;
26 import org.apache.mailet.base.GenericMailet;
27 import org.apache.mailet.Mail;
28 import org.apache.mailet.MailAddress;
29 import org.apache.mailet.MailetException;
30
31 import javax.mail.MessagingException;
32 import javax.mail.internet.MimeMessage;
33 import javax.mail.internet.MimeMultipart;
34 import java.io.IOException;
35 import java.util.Collection;
36
37
38
39
40
41
42
43 public abstract class GenericListserv extends GenericMailet {
44
45
46
47
48 public abstract Collection getMembers() throws MessagingException;
49
50
51
52
53 public abstract boolean isMembersOnly() throws MessagingException;
54
55
56
57
58 public abstract boolean isAttachmentsAllowed() throws MessagingException;
59
60
61
62
63 public abstract boolean isReplyToList() throws MessagingException;
64
65
66
67
68
69
70 public MailAddress getListservAddress() throws MessagingException {
71 return null;
72 }
73
74
75
76
77 public abstract String getSubjectPrefix() throws MessagingException;
78
79
80
81
82
83
84
85
86 public boolean isPrefixAutoBracketed() throws MessagingException {
87 return true;
88 }
89
90
91
92
93
94 public final void service(Mail mail) throws MessagingException {
95 try {
96 Collection members = getMembers();
97
98
99 if (isMembersOnly() && !members.contains(mail.getSender())) {
100
101 getMailetContext().bounce(mail, "Only members of this listserv are allowed to send a message to this address.");
102 mail.setState(Mail.GHOST);
103 return;
104 }
105
106
107 if (!isAttachmentsAllowed() && mail.getMessage().getContent() instanceof MimeMultipart) {
108 getMailetContext().bounce(mail, "You cannot send attachments to this listserv.");
109 mail.setState(Mail.GHOST);
110 return;
111 }
112
113
114 MimeMessage message = new MimeMessage(mail.getMessage());
115
116 message.removeHeader(RFC2822Headers.RETURN_PATH);
117
118
119 MailAddress listservAddr = getListservAddress();
120 if (listservAddr == null) {
121
122 listservAddr = (MailAddress)mail.getRecipients().iterator().next();
123 }
124
125
126
127
128 if (listservAddr.equals(message.getHeader("X-been-there"))) {
129 mail.setState(Mail.GHOST);
130 return;
131 }
132
133
134 String prefix = getSubjectPrefix();
135 if (prefix != null) {
136 if (isPrefixAutoBracketed()) {
137 StringBuffer prefixBuffer =
138 new StringBuffer(64)
139 .append("[")
140 .append(prefix)
141 .append("] ");
142 prefix = prefixBuffer.toString();
143 }
144 String subj = message.getSubject();
145 if (subj == null) {
146 subj = "";
147 }
148 subj = MailetUtil.normalizeSubject(subj, prefix);
149 AbstractRedirect.changeSubject(message, subj);
150 }
151
152
153 if (isReplyToList()) {
154 message.setHeader(RFC2822Headers.REPLY_TO, listservAddr.toString());
155 }
156
157
158 message.setHeader("X-been-there", listservAddr.toString());
159
160
161
162 getMailetContext().sendMail(getMailetContext().getPostmaster(), members, message);
163
164
165 mail.setState(Mail.GHOST);
166 } catch (IOException ioe) {
167 throw new MailetException("Error creating listserv message", ioe);
168 }
169 }
170 }