1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.transport.mailets.listservcommands;
21
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.james.services.UsersRepository;
25 import org.apache.james.transport.mailets.ICommandListservManager;
26 import org.apache.james.util.XMLResources;
27 import org.apache.mailet.Mail;
28 import org.apache.mailet.MailAddress;
29
30 import javax.mail.MessagingException;
31 import java.util.Properties;
32
33 /***
34 * UnSubscribe handles the unsubscribe command.
35 * It is configured by:
36 * <pre><command name="unsubscribe" class="UnSubscribe"/></pre>
37 *
38 * <br />
39 * <br />
40 *
41 * It uses the formatted text-based resources for its return mail body:
42 * <ul>
43 * <li>unsubscribe
44 * </ul>
45 *
46 * <br />
47 * <br />
48 * After formatting the text, the message is delivered with {@link #sendStandardReply}
49 *
50 * Note, prior to formatting and sending any text, the user is checked to see that they
51 * are currently subscribed to this list. If so, they will be sent a confirmation mail to
52 * be processed by {@link UnSubscribeConfirm}
53 *
54 * @version CVS $Revision: 494012 $ $Date: 2007-01-08 10:23:58 +0000 (Mon, 08 Jan 2007) $
55 * @since 2.2.0
56 * @see UnSubscribeConfirm
57 */
58 public class UnSubscribe extends BaseCommand {
59
60
61 protected XMLResources xmlResources;
62
63 public void init(ICommandListservManager commandListservManager, Configuration configuration) throws ConfigurationException {
64 super.init(commandListservManager, configuration);
65 xmlResources = initXMLResources(new String[]{"unsubscribe"})[0];
66 }
67
68 /***
69 * After ensuring that the user is currently subscribed, confirmation mail
70 * will be sent to be processed by {@link UnSubscribeConfirm}.
71 * @param mail
72 * @throws MessagingException
73 */
74 public void onCommand(Mail mail) throws MessagingException {
75 if (checkSubscriptionStatus(mail)) {
76
77 Properties props = getStandardProperties();
78 props.put("SENDER_ADDR", mail.getSender().toString());
79
80 String confirmationMail = xmlResources.getString("text", props);
81 String subject = xmlResources.getString("confirm.unsubscribe.subject", props);
82 String replyAddress = xmlResources.getString("confirm.unsubscribe.address", props);
83
84 sendStandardReply(mail, subject, confirmationMail, replyAddress);
85 }
86 }
87
88 /***
89 * Checks to see that this user is already subscribed, if not return false and send a message
90 * @param mail
91 * @return false if the user isn't subscribed, true otherwise
92 * @throws MessagingException
93 */
94 protected boolean checkSubscriptionStatus(Mail mail) throws MessagingException {
95 MailAddress mailAddress = mail.getSender();
96 UsersRepository usersRepository = getUsersRepository();
97 if (!usersRepository.contains(mailAddress.toString())) {
98 getCommandListservManager().onError(mail,
99 "Invalid request",
100 xmlResources.getString("not.subscribed", getStandardProperties()));
101 return false;
102 }
103 return true;
104 }
105 }