/*
 * 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) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.core.impl;

import com.silverwrist.util.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.svc.GlobalSite;
import com.silverwrist.venice.util.MailSend;
import com.silverwrist.venice.util.PropertyProvider;

class PersonalMailSend implements MailSend
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private MailSend inner;    // inner object that we pass through calls to
  private GlobalSite site;   // for getting stock messages

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

  PersonalMailSend(MailSend inner, GlobalSite site, PropertyProvider user_props) throws DataException
  {
    this.inner = inner;
    this.site = site;

    try
    { // set the "From" address in advance
      inner.setFrom(user_props.propGet("fullname"),user_props.propGet("email"));

    } // end try
    catch (EmailException ee)
    { // this shouldn't happen
      throw new InternalStateError("shouldn\'t be an EmailException here!",ee);

    } // end catch

    StringBuffer buf = new StringBuffer("uid ");
    buf.append(user_props.propGet("id")).append("; username \"").append(user_props.propGet("name"));
    buf.append("\"; ip [").append(user_props.propGet("ip.address")).append("]");
    inner.addHeader("X-Venice-Sender-Info",buf.toString());
    String[] headers = StringUtil.splitArray(site.getStockMessage("header-disclaimer"),"\r\n");
    for (int i=0; i<headers.length; i++)
      inner.addHeader("X-Disclaimer-" + String.valueOf(i+1),headers[i]);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private static final void checkHeaderName(String name)
  {
    name = name.toLowerCase();
    if (name.equals("x-venice-sender-info") || name.startsWith("x-disclaimer-"))
      throw new UnsupportedOperationException("cannot modify this header");

  } // end checkHeaderName

  /*--------------------------------------------------------------------------------
   * Implementations from interface MailSend
   *--------------------------------------------------------------------------------
   */

  public void addRecipient(int type, String addr) throws EmailException
  {
    inner.addRecipient(type,addr);

  } // end addRecipient

  public void removeRecipient(int type, String addr) throws EmailException
  {
    inner.removeRecipient(type,addr);

  } // end removeRecipient

  public void clearRecipients(int type)
  {
    inner.clearRecipients(type);

  } // end clearRecipients

  public void setFrom(String from_name, String from_addr)
  {
    throw new UnsupportedOperationException("cannot modify sender address");

  } // end setFrom

  public void setSubject(String subject)
  {
    inner.setSubject(subject);

  } // end setSubject

  public void setStockSubject(String identifier)
  {
    inner.setStockSubject(identifier);

  } // end setStockSubject

  public void setText(String text)
  {
    inner.setText(text + "\n--\n" + site.getStockMessage("signature"));

  } // end setText

  public void setStockText(String identifier)
  {
    this.setText(site.getStockMessage(identifier));

  } // end setStockText

  public void setVariable(String name, String value)
  {
    inner.setVariable(name,value);

  } // end setVariable

  public void addHeader(String name, String value)
  {
    checkHeaderName(name);
    inner.addHeader(name,value);

  } // end addHeader

  public void removeHeader(String name)
  {
    checkHeaderName(name);
    inner.removeHeader(name);

  } // end removeHeader

  public void send() throws EmailException
  {
    inner.send();

  } // end send

} // end class PersonalMailSend
