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: * Subscribe handles the subscribe-confirm command.
033: * It is configured by:
034: * <pre><command name="subscribe-confirm" class="SubscribeConfirm"/></pre>
035: *
036: * <br />
037: * <br />
038: *
039: * It uses the formatted text-based resources for its return mail body:
040: * <ul>
041: * <li>subscribe-confirm
042: * <li>admincommands
043: * </ul>
044: *
045: * <br />
046: * <br />
047: * After formatting the text, the message is delivered with {@link #sendStandardReply}
048: *
049: * <br />
050: * <br />
051: * This command basically sends the welcome message and adds the user to the mailing list.
052: *
053: * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
054: * @since 2.2.0
055: * @see Subscribe
056: */
057: public class SubscribeConfirm extends BaseCommand {
058:
059: //For resources
060: protected XMLResources[] xmlResources;
061:
062: protected static final int SUBSCRIBE_CONFIRM = 0;
063: protected static final int ADMIN_COMMANDS = 1;
064:
065: public void init(ICommandListservManager commandListservManager,
066: Configuration configuration) throws ConfigurationException {
067: super .init(commandListservManager, configuration);
068: xmlResources = initXMLResources(new String[] {
069: "subscribeConfirm", "admincommands" });
070: }
071:
072: /**
073: * After ensuring that the user isn't already subscribed, add the user to the
074: * mailing list, and send a welcome message.
075: *
076: * <br />
077: * <br />
078: *
079: * It uses the formatted text-based resources for its return mail body:
080: * <ul>
081: * <li>{@link #SUBSCRIBE_CONFIRM}
082: * <li>{@link #ADMIN_COMMANDS}
083: * </ul>
084: *
085: * @param mail
086: * @throws MessagingException
087: */
088: public void onCommand(Mail mail) throws MessagingException {
089: if (checkSubscriptionStatus(mail)) {
090: getUsersRepository().addUser(mail.getSender().toString(),
091: "");
092:
093: //send mail
094: Properties props = getStandardProperties();
095: props.put("SENDER_ADDR", mail.getSender().toString());
096:
097: String confirmationMail = xmlResources[SUBSCRIBE_CONFIRM]
098: .getString("text", props);
099: String adminCommands = xmlResources[ADMIN_COMMANDS]
100: .getString("text", props);
101: String subject = xmlResources[SUBSCRIBE_CONFIRM].getString(
102: "welcome.subscribe.address", props);
103:
104: sendStandardReply(mail, subject, confirmationMail
105: + adminCommands, null);
106: }
107: }
108:
109: /**
110: * Checks to see if this user is already subscribed, if so return false and send a message
111: * @param mail
112: * @return false if the user is subscribed, true otherwise
113: * @throws MessagingException
114: */
115: protected boolean checkSubscriptionStatus(Mail mail)
116: throws MessagingException {
117: MailAddress mailAddress = mail.getSender();
118: UsersRepository usersRepository = getUsersRepository();
119: if (usersRepository.contains(mailAddress.toString())) {
120: getCommandListservManager().onError(
121: mail,
122: "Invalid request",
123: xmlResources[SUBSCRIBE_CONFIRM].getString(
124: "already.subscribed",
125: getStandardProperties()));
126: return false;
127: }
128: return true;
129: }
130: }
|