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
21
22 package org.apache.james.transport.mailets.listservcommands;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.james.api.user.UsersRepository;
27 import org.apache.james.transport.mailets.ICommandListservManager;
28 import org.apache.james.util.XMLResources;
29 import org.apache.mailet.Mail;
30 import org.apache.mailet.MailAddress;
31
32 import javax.mail.MessagingException;
33 import java.util.Properties;
34
35 /**
36 * UnSubscribe handles the unsubscribe command.
37 * It is configured by:
38 * <pre><command name="unsubscribe" class="UnSubscribe"/></pre>
39 *
40 * <br />
41 * <br />
42 *
43 * It uses the formatted text-based resources for its return mail body:
44 * <ul>
45 * <li>unsubscribe
46 * </ul>
47 *
48 * <br />
49 * <br />
50 * After formatting the text, the message is delivered with {@link #sendStandardReply(Mail, String, String, String)}
51 *
52 * Note, prior to formatting and sending any text, the user is checked to see that they
53 * are currently subscribed to this list. If so, they will be sent a confirmation mail to
54 * be processed by {@link UnSubscribeConfirm}
55 *
56 * @version CVS $Revision: 684466 $ $Date: 2008-08-10 12:42:08 +0100 (Sun, 10 Aug 2008) $
57 * @since 2.2.0
58 * @see UnSubscribeConfirm
59 */
60 public class UnSubscribe extends BaseCommand {
61
62 //For resources
63 protected XMLResources xmlResources;
64
65 public void init(ICommandListservManager commandListservManager, Configuration configuration) throws ConfigurationException {
66 super.init(commandListservManager, configuration);
67 xmlResources = initXMLResources(new String[]{"unsubscribe"})[0];
68 }
69
70 /**
71 * After ensuring that the user is currently subscribed, confirmation mail
72 * will be sent to be processed by {@link UnSubscribeConfirm}.
73 * @param mail
74 * @throws MessagingException
75 */
76 public void onCommand(Mail mail) throws MessagingException {
77 if (checkSubscriptionStatus(mail)) {
78 //send confirmation mail
79 Properties props = getStandardProperties();
80 props.put("SENDER_ADDR", mail.getSender().toString());
81
82 String confirmationMail = xmlResources.getString("text", props);
83 String subject = xmlResources.getString("confirm.unsubscribe.subject", props);
84 String replyAddress = xmlResources.getString("confirm.unsubscribe.address", props);
85
86 sendStandardReply(mail, subject, confirmationMail, replyAddress);
87 }
88 }
89
90 /**
91 * Checks to see that this user is already subscribed, if not return false and send a message
92 * @param mail
93 * @return false if the user isn't subscribed, true otherwise
94 * @throws MessagingException
95 */
96 protected boolean checkSubscriptionStatus(Mail mail) throws MessagingException {
97 MailAddress mailAddress = mail.getSender();
98 UsersRepository usersRepository = getUsersRepository();
99 if (!usersRepository.contains(mailAddress.toString())) {
100 getCommandListservManager().onError(mail,
101 "Invalid request",
102 xmlResources.getString("not.subscribed", getStandardProperties()));
103 return false;
104 }
105 return true;
106 }
107 }