head	1.4;
access;
symbols;
locks; strict;
comment	@# @;


1.4
date	2001.08.11.09.08.08;	author dlr;	state Exp;
branches;
next	1.3;

1.3
date	2001.07.23.20.33.42;	author dlr;	state Exp;
branches;
next	1.2;

1.2
date	2001.07.19.18.42.23;	author dlr;	state Exp;
branches;
next	1.1;

1.1
date	2001.07.18.23.38.44;	author dlr;	state Exp;
branches;
next	;


desc
@@


1.4
log
@Upgrading to Turbine 3.0-dev, Fulcrum 1.0, and Torque 1.0.
@
text
@/* ================================================================
 * Copyright (c) 2000 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet (http://www.CollabNet/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */

package org.tigris.helm.tool;

import java.util.Date;
import java.util.Properties;
import java.util.List;
import javax.mail.*;
import javax.mail.internet.*;

import org.apache.turbine.util.Log;
import org.apache.commons.util.exception.NestableException;

import org.tigris.helm.tool.HelmProperties;

/**
 * Send email.  This class provides convenience methods as wrappers for Sun's 
 * Java Mail facility.
 *
 * @@author <a href="mailto:davidp@@dataweb-systems.com">David C. Pellegrini</a>
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 * @@version $Id: Emailer.java,v 1.3 2001/07/23 20:33:42 dlr Exp $
 *
 * Copyright (c) 1999 dataweb systems, inc.
 * Copyright (c) 1999-2001 CollabNet, Inc.
 */
public class Emailer
{
    /**
     * The default sender's email address.
     */
    protected static String defaultSender = null;

    /**
     * Whether to log debugging information.
     */
    private static final boolean DEBUG = false;

    /**
     * Sends an email to one person.
     */
    public static void main (String[] args)
        throws NestableException
    {
        switch (args.length)
        {
        case 3:
            new Emailer().sendEmail(args[0], args[1], args[2]);
            break;
        case 4:
            new Emailer().sendEmailFrom(args[0], args[1], args[2], args[3]);
            break;
        default:
            System.err.println
                ("usage: Emailer [sender] <recipient> <subject> <body>");
            System.exit(1);
        }
        System.exit(0);
    }

    /**
     * Sends an email message from the web application to a single recipient.
     *
     * @@param recipient The recipient's email address.
     * @@param subject   The message's subject.
     * @@param body      The message body.
     *
     * @@exception NestableException
     */
    public void sendEmail(String recipient, String subject, String body)
        throws NestableException
    {
        sendEmailFrom(defaultSender, recipient, subject, body);
    }

    /**
     * Sends an email message from the web application to a set of recipients.
     *
     * @@param recipients The recipients' email addresses.
     * @@param subject    The message's subject.
     * @@param body       The message body.
     *
     * @@exception NestableException
     */
    public void sendEmail(List recipients, String subject, String body)
        throws NestableException
    {
        sendEmailcc(recipients, null, subject, body);
    }

    /**
     * Sends an email message from the web application to a set of recipients 
     * and CC's.
     *
     * @@param recipients The recipients' email addresses.
     * @@param ccs        The email addresses to CC.
     * @@param subject    The message's subject.
     * @@param body       The message body.
     *
     * @@exception NestableException
     */
    public void sendEmailcc(List recipients, List ccs, String subject, 
                            String body)
        throws NestableException
    {
        sendEmailccFrom(defaultSender, recipients, ccs, subject, body);
    }

    /**
     * Sends an email message to a single recipient, addressed from the 
     * specified sender.
     *
     * @@param sender     The sender's email address.
     * @@param recipients The recipient's email address.
     * @@param ccs        The email addresses to CC.
     * @@param subject    The message's subject.
     * @@param body       The message body.
     *
     * @@exception NestableException
     */
    public void sendEmailFrom(String sender, String recipient, String subject, 
                              String body)
        throws NestableException
    {
        try
        {
            Message message = newMessage();
            setFields(message, sender, recipient, null, subject, body);
            Transport.send(message);
        }
        catch (IllegalWriteException e)
        {
            throw new NestableException(e);
        }
        catch (MessagingException e)
        {
            throw new NestableException(e);
        }
    }

    /**
     * Sends an email message to a set of recipients, addressed from the 
     * specified sender.
     *
     * @@param sender     The sender's email address.
     * @@param recipients The recipients' email addresses.
     * @@param ccs        The email addresses to CC.
     * @@param subject    The message's subject.
     * @@param body       The message body.
     *
     * @@exception NestableException
     */
    public void sendEmailFrom(String sender, List recipients, String subject, 
                              String body)
        throws NestableException
    {
        sendEmailccFrom(sender, recipients, null, subject, body);
    }

    /**
     *  Sends an email message from a specified sender to a set of
     *  recipients and CC's.
     *
     * @@param sender     The sender's email address.
     * @@param recipients The recipients' email addresses.
     * @@param ccs        The email addresses to CC.
     * @@param subject    The message's subject.
     * @@param body       The message body.
     *
     * @@exception NestableException
     */
    public void sendEmailccFrom(String sender, List recipients, List ccs, 
                                String subject, String body)
        throws NestableException
    {
        try
        {
            Message message = newMessage();
            setFields(message, sender, recipients, ccs, subject, body);
            Transport.send(message);
        }
        catch (IllegalWriteException e)
        {
            throw new NestableException(e);
        }
        catch (MessagingException e)
        {
            throw new NestableException(e);
        }
    }

    /**
     * Creates a new message using the system properties.
     *
     * @@return The newly created message.
     */
    private final Message newMessage ()
    {
        Session session = 
            Session.getDefaultInstance(System.getProperties(), null);
        return new MimeMessage(session);
    }

    /**
     * Turns a <code>List</code> of email addresses into an array of 
     * <code>InternetAddress</code> objects.
     *
     * @@param recipients The email addresses to transform.
     * @@return           The transformed addresses.
     */
    private InternetAddress[] getInternetAddresses (List recipients)
        throws AddressException
    {
        InternetAddress[] addresses = null;
        int nbrAddresses = recipients.size();
        addresses = new InternetAddress[nbrAddresses];
        for (int i = 0; i < nbrAddresses; i++)
        {
            addresses[i] = new InternetAddress((String)recipients.get(i));
        }
        return addresses;
    }

    /**
     * Sets the message fields.
     *
     * @@param message    A reference to the message itself.
     * @@param sender     The sender of the message.
     * @@param recipients The recipients of the message (a <code>List</code> or 
     *                   <code>String</code>).
     * @@param ccs        The people to CC (a <code>List</code>, 
     *                   <code>String</code>, or <code>null</code>).
     * @@param subject    The message subject.
     * @@param body       The message body.
     */
    private void setFields (Message message, String sender, Object recipients, 
                            Object ccs, String subject, String body)
        throws AddressException, MessagingException
    {
        if (sender == null || sender.equals(""))
        {
            // HELP: Not sure if HelmProperties would work properly if this 
            // were placed in a static initializer block (where it belongs).
            if (defaultSender == null)
            {
                defaultSender = createEmailAddress("notifier");
            }
            sender = defaultSender;
        }
        else if (sender.indexOf('@@') == -1)
        {
            // We do not want emails sent as "sender@@localhost".
            sender = createEmailAddress(sender);
        }
        message.setFrom(new InternetAddress(sender));

        message.setSentDate(new Date());

        if (DEBUG) Log.debug("TO: " + recipients);
        if (recipients instanceof List)
        {
            message.setRecipients(Message.RecipientType.TO, 
                                  getInternetAddresses((List)recipients));
        }
        else
        {
            message.setRecipient(Message.RecipientType.TO, 
                                 new InternetAddress(recipients.toString()));
        }

        if (DEBUG) Log.debug("CC: " + ccs);
        if (ccs != null)
        {
            if (ccs instanceof List)
            {
                message.setRecipients(Message.RecipientType.CC, 
                                      getInternetAddresses((List)ccs));
            }
            else
            {
                message.setRecipient(Message.RecipientType.CC, 
                                     new InternetAddress(ccs.toString()));
            }
        }

        message.setSubject(subject);

        message.setText(body);
    }

    /**
     * Appends the mail host to the sender name to create a well-formed email
     * address.
     */
    private String createEmailAddress (String sender)
    {
        StringBuffer buf = new StringBuffer(sender);
        buf.append('@@').append(HelmProperties.getProperty("MAIL_HOST"));
        return buf.toString();
    }

    /**
     * Returns the address for the project mailing list.
     *
     * @@param projectID    The database key of the project whose list to get.
     * @@return    A variable of type String 
     */
    public String getProjectList(String projectID)
    {
        StringBuffer listName = new StringBuffer("project");
        String mailHost = (String) HelmProperties.getProperty("MAIL_HOST");
        listName.append(projectID).append("@@" + mailHost);
        return listName.toString();
    }

    /**
     * Adds the project mailing list to the recipients list.
     *
     * @@param recipients The recipients' email addresses.
     * @@param projectID  the projectID of the mailing list.
     */
    public void ccProject(List recipients, String projectID)
    {
        // FIX: Make this configurable through a property or subclass for 
        // sXc, tigris, etc.
        recipients.add(getProjectList(projectID));
    }
}
@


1.3
log
@Removed Joist reference.
@
text
@d56 1
a56 1
import org.apache.turbine.util.TurbineException;
d66 1
a66 1
 * @@version $Id: Emailer.java,v 1.2 2001/07/19 18:42:23 dlr Exp $
d87 1
a87 1
        throws TurbineException
d112 1
a112 1
     * @@exception TurbineException
d115 1
a115 1
        throws TurbineException
d127 1
a127 1
     * @@exception TurbineException
d130 1
a130 1
        throws TurbineException
d144 1
a144 1
     * @@exception TurbineException
d148 1
a148 1
        throws TurbineException
d163 1
a163 1
     * @@exception TurbineException
d167 1
a167 1
        throws TurbineException
d177 1
a177 1
            throw new TurbineException(e);
d181 1
a181 1
            throw new TurbineException(e);
d195 1
a195 1
     * @@exception TurbineException
d199 1
a199 1
        throws TurbineException
d214 1
a214 1
     * @@exception TurbineException
d218 1
a218 1
        throws TurbineException
d228 1
a228 1
            throw new TurbineException(e);
d232 1
a232 1
            throw new TurbineException(e);
@


1.2
log
@Updated in light of the port of Joist JoistProperties class to Helm (as class HelmProperties).
@
text
@d66 1
a66 1
 * @@version $Id: Emailer.java,v 1.1 2001/07/18 23:38:44 dlr Exp $
a347 1
     * HELP: Does this really belong as part of Joist?
@


1.1
log
@Ported from Joist.
@
text
@d58 1
a58 1
import org.joist.util.JoistProperties;
d66 1
a66 1
 * @@version $Id: Emailer.java,v 1.24 2001/02/21 01:35:11 dlr Exp $
d286 1
a286 1
            // HELP: Not sure if JoistProperties would work properly if this 
d342 1
a342 1
        buf.append('@@').append(JoistProperties.getProperty("MAIL_HOST"));
d356 1
a356 1
        String mailHost = (String) JoistProperties.getProperty("MAIL_HOST");
@

