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