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.services.UsersRepository;
023: import org.apache.james.transport.mailets.ICommandListservManager;
024: import org.apache.james.util.XMLResources;
025: import org.apache.mailet.Mail;
026:
027: import javax.mail.MessagingException;
028: import java.util.Iterator;
029: import java.util.Properties;
030:
031: /**
032: * Info handles the info command.
033: * It is configured by:
034: * <pre><command name="info" class="Info"/></pre>
035: *
036: * <br />
037: * <br />
038: *
039: * It uses the formatted text-based resources for its return mail body:
040: * <ul>
041: * <li>header
042: * <li>info
043: * <li>admincommands
044: * </ul>
045: *
046: * <br />
047: * <br />
048: * After formatting the text, the message is delivered with {@link #sendStandardReply}
049: *
050: * Todo: make displaying the current member list optional
051: *
052: * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
053: * @since 2.2.0
054: */
055: public class Info extends BaseCommand {
056:
057: //For resources
058: protected XMLResources[] xmlResources;
059:
060: protected static final int HEADER = 0;
061: protected static final int INFO = 1;
062: protected static final int ADMIN_COMMANDS = 2;
063:
064: public void init(ICommandListservManager commandListservManager,
065: Configuration configuration) throws ConfigurationException {
066: super .init(commandListservManager, configuration);
067: xmlResources = initXMLResources(new String[] { "header",
068: "info", "admincommands" });
069: }
070:
071: /**
072: * Process the info command using the following text resources:
073: * <ul>
074: * <li>{@link #HEADER}
075: * <li>{@link #INFO}
076: * <li>{@link #ADMIN_COMMANDS}
077: * </ul>
078: *
079: * @param mail
080: */
081: public void onCommand(Mail mail) throws MessagingException {
082: //send info mail
083: Properties props = getStandardProperties();
084: props.put("MEMBER_LIST", getMemberList());
085:
086: StringBuffer plainTextMessage = new StringBuffer();
087: String header = xmlResources[HEADER].getString("text", props);
088: plainTextMessage.append(header);
089:
090: String infoMail = xmlResources[INFO].getString("text", props);
091: plainTextMessage.append(infoMail);
092:
093: String adminCommands = xmlResources[ADMIN_COMMANDS].getString(
094: "text", props);
095: plainTextMessage.append(adminCommands);
096:
097: String subject = xmlResources[INFO].getString("info.subject",
098: props);
099:
100: sendStandardReply(mail, subject, plainTextMessage.toString(),
101: null);
102: }
103:
104: /**
105: * Retrieve the current member list
106: * @return the formatted member list
107: *
108: * @see #getUsersRepository
109: */
110: protected String getMemberList() {
111:
112: StringBuffer buffer = new StringBuffer(0x1000);
113: buffer.append("\r\n");
114: UsersRepository usersRepository = getUsersRepository();
115: for (Iterator it = usersRepository.list(); it.hasNext();) {
116: String userName = (String) it.next();
117: buffer.append(" ").append(userName);
118: buffer.append("\r\n");
119: }
120:
121: return buffer.toString();
122: }
123: }
|