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 * Subscribe handles the subscribe command.
35 * It is configured by:
36 * <pre><command name="subscribe" class="Subscribe"/></pre>
37 *
38 * <br />
39 * <br />
40 *
41 * It uses the formatted text-based resources for its return mail body:
42 * <ul>
43 * <li>subscribe
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 if they
51 * are already subscribed to this list. If not, they will be sent a confirmation mail to
52 * be processed by {@link SubscribeConfirm}
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 SubscribeConfirm
57 */
58 public class Subscribe extends BaseCommand {
59
60
61 protected XMLResources xmlResources;
62
63
64
65 public void init(ICommandListservManager commandListservManager, Configuration configuration) throws ConfigurationException {
66 super.init(commandListservManager, configuration);
67 xmlResources = initXMLResources(new String[]{"subscribe"})[0];
68 }
69
70 /***
71 * After ensuring that the user isn't already subscribed, confirmation mail
72 * will be sent to be processed by {@link SubscribeConfirm}.
73 *
74 * @param mail
75 * @throws MessagingException
76 */
77 public void onCommand(Mail mail) throws MessagingException {
78 if (checkSubscriptionStatus(mail)) {
79
80 Properties props = getStandardProperties();
81 props.put("SENDER_ADDR", mail.getSender().toString());
82
83 String confirmationMail = xmlResources.getString("text", props);
84 String subject = xmlResources.getString("confirm.subscribe.subject", props);
85 String replyAddress = xmlResources.getString("confirm.subscribe.address", props);
86
87 sendStandardReply(mail, subject, confirmationMail, replyAddress);
88 }
89 }
90
91 /***
92 * Checks to see if this user is already subscribed, if so return false and send a message
93 * @param mail
94 * @return false if the user is subscribed, true otherwise
95 * @throws MessagingException
96 */
97 protected boolean checkSubscriptionStatus(Mail mail) throws MessagingException {
98 MailAddress mailAddress = mail.getSender();
99 UsersRepository usersRepository = getUsersRepository();
100 if (usersRepository.contains(mailAddress.toString())) {
101 getCommandListservManager().onError(mail,
102 "Invalid request",
103 xmlResources.getString("already.subscribed", getStandardProperties()));
104 return false;
105 }
106 return true;
107 }
108 }