/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.core.impl;

import java.util.*;
import org.apache.log4j.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.core.internals.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.htmlcheck.*;

class MailerAgent extends Thread
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Category logger = Category.getInstance(MailerAgent.class);

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private EnvUser env;                      // the environment
  private String from_name;                 // the "from" name
  private String from_addr;                 // the "from" address
  private List recipients;                  // who gets the message
  private String subject;                   // message subject
  private String text;                      // message text

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  MailerAgent(EnvUser env, String from_name, String from_addr, List recipients, String subject, String text)
  {
    super();
    setDaemon(false);

    this.env = env;
    this.from_name = from_name;
    this.from_addr = from_addr;
    this.recipients = recipients;
    this.subject = subject;
    this.text = text;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class Thread
   *--------------------------------------------------------------------------------
   */

  public void run()
  {
    NDC.push("MailerAgent");

    try
    { // attach the signature to the message
      StringBuffer buf = new StringBuffer(text);
      buf.append("\n--\n").append(env.getStockMessage("signature"));
      
      // Create the emailer object and load it up with most of the data.
      Emailer em = env.getEngine().createEmailer();
      em.setSubject(subject);
      em.setText(buf.toString());

      // Deliver the mail to each of the target addresses in turn.
      Iterator it = recipients.iterator();
      while (it.hasNext())
      { // get ready to deliver the mail...
	String addr = (String)(it.next());
	if (logger.isDebugEnabled())
	  logger.debug("Delivering to " + addr);

	try
	{ // set the recipient and send it out
	  em.setFrom(from_name,from_addr);
	  em.setTo(addr);
	  em.send();

	} // end try
	catch (EmailException e)
	{ // log the error and move on
	  logger.error("Caught EmailException when trying to send to " + addr,e);

	} // end catch

      } // end while

    } // end try
    finally
    { // make sure and pop the diagnostic context before we go
      NDC.pop();

    } // end finally

  } // end run

} // end class MailerAgent
