001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.transport.mailets;
019:
020: import org.apache.avalon.framework.service.ServiceException;
021: import org.apache.avalon.framework.service.ServiceManager;
022: import org.apache.james.Constants;
023: import org.apache.james.services.UsersRepository;
024: import org.apache.james.services.UsersStore;
025: import org.apache.mailet.MailAddress;
026:
027: import javax.mail.internet.ParseException;
028: import java.util.Collection;
029: import java.util.Iterator;
030: import java.util.ArrayList;
031:
032: /**
033: * MailingListServer capability.
034: *
035: * <p>Requires a configuration element in the config.xml file of the form:
036: * <br> <mailet match="RecipientIs=LIST-ADDRESS" class="AvalonListserv">
037: * <br> <repositoryName>LIST-NAME</repositoryName>
038: * <br> <membersonly>[true|false]</membersonly>
039: * <br> <attachmentsallowed>[true|false]</attachmentsallowed>
040: * <br> <replytolist>[true|false]</replytolist>
041: * <br> <autobracket>[true|false]</autobracket>
042: * <br> <subjectprefix [xml:space="preserve"]>SUBJECT-PREFIX</subjectprefix>
043: * <br> </mailet>
044: * <p>repositoryName - the name of a user repository configured in the
045: * UsersStore block, e.g.,
046: * <br> <repository name="list-name" class="org.apache.james.userrepository.ListUsersJdbcRepository" destinationURL="db://maildb/lists/list-name">
047: * <br> <sqlFile>file://conf/sqlResources.xml</sqlFile>
048: * <br> </repository>
049: * <p>or
050: * <br> <repository name="list-name" class="org.apache.james.userrepository.UsersFileRepository">
051: * <br> <destination URL="file://var/lists/list-name/"/>
052: * <br> </repository>
053: * <p>membersonly - if true only members can post to the list
054: * <p>attachmentsallowed - if false attachments are not allowed
055: * <p>replytolist - if true, replies go back to the list address; if
056: * false they go to the sender.
057: * <p>subjectprefix - a prefix that will be inserted at the front of
058: * the subject. If autobracketing is disabled (see below), the
059: * xml:space="preserve" attribute can be used to precisely control the
060: * prefix.
061: * <p>autobracket - if true the subject prefix will be rendered as
062: * "[PREFIX] ", if false, the prefix will be used literally.
063: *
064: * @version This is $Revision: 494012 $
065: */
066: public class AvalonListserv extends GenericListserv {
067:
068: /**
069: * Whether only members can post to the list
070: */
071: protected boolean membersOnly = false;
072:
073: /**
074: * Whether attachments can be sent to the list
075: */
076: protected boolean attachmentsAllowed = true;
077:
078: /**
079: * Whether the reply-to header should be set to the list address
080: */
081: protected boolean replyToList = true;
082:
083: /**
084: * A String to prepend to the subject of the message when it
085: * is sent to the list
086: */
087: protected String subjectPrefix = null;
088:
089: /**
090: * Whether the subject prefix should be bracketed with '[' and ']'
091: */
092: protected boolean autoBracket = true;
093:
094: /**
095: * The repository containing the users on this list
096: */
097: private UsersRepository members;
098:
099: /**
100: * Initialize the mailet
101: */
102: public void init() {
103: try {
104: membersOnly = new Boolean(getInitParameter("membersonly"))
105: .booleanValue();
106: } catch (Exception e) {
107: // Ignore any exceptions, default to false
108: }
109: try {
110: attachmentsAllowed = new Boolean(
111: getInitParameter("attachmentsallowed"))
112: .booleanValue();
113: } catch (Exception e) {
114: // Ignore any exceptions, default to true
115: }
116: try {
117: replyToList = new Boolean(getInitParameter("replytolist"))
118: .booleanValue();
119: } catch (Exception e) {
120: // Ignore any exceptions, default to true
121: }
122: subjectPrefix = getInitParameter("subjectprefix");
123:
124: try {
125: autoBracket = new Boolean(getInitParameter("autobracket"))
126: .booleanValue();
127: } catch (Exception e) {
128: // Ignore any exceptions, default to true
129: }
130:
131: ServiceManager compMgr = (ServiceManager) getMailetContext()
132: .getAttribute(Constants.AVALON_COMPONENT_MANAGER);
133: try {
134: UsersStore usersStore = (UsersStore) compMgr
135: .lookup(UsersStore.ROLE);
136: String repName = getInitParameter("repositoryName");
137:
138: members = (UsersRepository) usersStore
139: .getRepository(repName);
140: } catch (ServiceException cnfe) {
141: log("Failed to retrieve Store component:"
142: + cnfe.getMessage());
143: } catch (Exception e) {
144: log("Failed to retrieve Store component:" + e.getMessage());
145: }
146: }
147:
148: public Collection getMembers() throws ParseException {
149: Collection reply = new ArrayList();
150: for (Iterator it = members.list(); it.hasNext();) {
151: String member = it.next().toString();
152: try {
153: reply.add(new MailAddress(member));
154: } catch (Exception e) {
155: // Handle an invalid subscriber address by logging it and
156: // proceeding to the next member.
157: StringBuffer logBuffer = new StringBuffer(1024).append(
158: "Invalid subscriber address: ").append(member)
159: .append(" caused: ").append(e.getMessage());
160: log(logBuffer.toString());
161: }
162: }
163: return reply;
164: }
165:
166: /**
167: * Get whether posting to this list is restricted to list members
168: *
169: * @return whether posting to this list is restricted to list members
170: */
171: public boolean isMembersOnly() {
172: return membersOnly;
173: }
174:
175: /**
176: * Get whether attachments can be sent to this list
177: *
178: * @return whether attachments can be sent to this list
179: */
180: public boolean isAttachmentsAllowed() {
181: return attachmentsAllowed;
182: }
183:
184: /**
185: * Get whether the reply-to header for messages sent to this list
186: * will be replaced with the list address
187: *
188: * @return whether replies to messages posted to this list will go to the entire list
189: */
190: public boolean isReplyToList() {
191: return replyToList;
192: }
193:
194: /**
195: * Get the prefix prepended to the subject line
196: *
197: * @return whether the prefix for subjects on this list will be bracketed.
198: */
199: public String getSubjectPrefix() {
200: return subjectPrefix;
201: }
202:
203: /**
204: * Return whether the prefix for subjects on this list will be bracketed.
205: *
206: * @return whether the prefix for subjects on this list will be bracketed.
207: */
208: public boolean isPrefixAutoBracketed() {
209: return autoBracket;
210: }
211:
212: /**
213: * Return a string describing this mailet.
214: *
215: * @return a string describing this mailet
216: */
217: public String getMailetInfo() {
218: return "AvalonListserv Mailet";
219: }
220: }
|