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.listservcommands;
019:
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.james.transport.mailets.ICommandListservManager;
023: import org.apache.james.util.XMLResources;
024: import org.apache.james.services.UsersRepository;
025: import org.apache.mailet.Mail;
026: import org.apache.mailet.MailAddress;
027:
028: import javax.mail.MessagingException;
029: import java.util.Properties;
030:
031: /**
032: * UnSubscribeConfirm handles the unsubscribe-confirm command.
033: * It is configured by:
034: * <pre><command name="unsubscribe-confirm" class="UnSubscribeConfirm"/></pre>
035: *
036: * <br />
037: * <br />
038: *
039: * It uses the formatted text-based resources for its return mail body:
040: * <ul>
041: * <li>unsubscribe-confirm
042: * </ul>
043: *
044: * <br />
045: * <br />
046: * After formatting the text, the message is delivered with {@link #sendStandardReply}
047: *
048: * <br />
049: * <br />
050: * This command basically sends a goodbye message and removes the user from the mailing list.
051: *
052: * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
053: * @since 2.2.0
054: * @see UnSubscribe
055: */
056: public class UnSubscribeConfirm extends BaseCommand {
057:
058: //For resources
059: protected XMLResources xmlResources;
060:
061: public void init(ICommandListservManager commandListservManager,
062: Configuration configuration) throws ConfigurationException {
063: super .init(commandListservManager, configuration);
064: xmlResources = initXMLResources(new String[] { "unsubscribeConfirm" })[0];
065: }
066:
067: /**
068: * After ensuring that the user is currently subscribed, remove the user to the
069: * mailing list, and send a goodbye message.
070: *
071: * @param mail
072: * @throws MessagingException
073: */
074: public void onCommand(Mail mail) throws MessagingException {
075: if (checkSubscriptionStatus(mail)) {
076: getUsersRepository()
077: .removeUser(mail.getSender().toString());
078:
079: //send mail
080: Properties props = getStandardProperties();
081: props.put("SENDER_ADDR", mail.getSender().toString());
082:
083: String confirmationMail = xmlResources.getString("text",
084: props);
085: String subject = xmlResources.getString(
086: "goodbye.subscribe.address", props);
087:
088: sendStandardReply(mail, subject, confirmationMail, null);
089: }
090: }
091:
092: /**
093: * Checks to see that this user is already subscribed, if not return false and send a message
094: * @param mail
095: * @return false if the user isn't subscribed, true otherwise
096: * @throws MessagingException
097: */
098: protected boolean checkSubscriptionStatus(Mail mail)
099: throws MessagingException {
100: MailAddress mailAddress = mail.getSender();
101: UsersRepository usersRepository = getUsersRepository();
102: if (!usersRepository.contains(mailAddress.toString())) {
103: getCommandListservManager().onError(
104: mail,
105: "Invalid request",
106: xmlResources.getString("not.subscribed",
107: getStandardProperties()));
108: return false;
109: }
110: return true;
111: }
112: }
|