/*
 * $Id$
 * $URL$
 */

package org.subethamail.plugin.filter;

import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
import javax.annotation.security.RunAs;
import javax.mail.MessagingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.annotation.ejb.Service;
import org.jboss.annotation.security.SecurityDomain;
import org.subethamail.common.SubEthaMessage;
import org.subethamail.core.lists.i.ListData;
import org.subethamail.core.plugin.i.Filter;
import org.subethamail.core.plugin.i.IgnoreException;
import org.subethamail.core.plugin.i.SendFilterContext;
import org.subethamail.core.plugin.i.helper.GenericFilter;
import org.subethamail.core.plugin.i.helper.Lifecycle;

/**
 * This filter appends the 
 * <a href="http://www.faqs.org/rfcs/rfc2369.html">RFC2369</a> List-* 
 * headers to outgoing mail.
 * 
 * @author Jon Stevens
 */
@Service
@SecurityDomain("subetha")
@RunAs("siteAdmin")
public class ListHeaderFilter extends GenericFilter implements Lifecycle
//TODO:  remove the implements clause when http://jira.jboss.org/jira/browse/EJBTHREE-489 is fixed
{	
	/** */
	private static Log log = LogFactory.getLog(ListHeaderFilter.class);

	public enum ListHeader
	{
		LIST_HELP ("List-Help"),
		LIST_UNSUBSCRIBE ("List-Unsubscribe"),
		LIST_SUBSCRIBE ("List-Subscribe"),
		LIST_POST ("List-Post"),
		LIST_OWNER ("List-Owner"),
		LIST_ARCHIVE ("List-Archive");

		private String header;
		/** A set that contains all listHeaders */
		public static final Set<ListHeader> ALL;

		static
		{
			Set<ListHeader> tmp = new TreeSet<ListHeader>();

			for (ListHeader p: ListHeader.values())
				tmp.add(p);

			ALL = Collections.unmodifiableSet(tmp);
		}

		ListHeader(String header)
		{
			this.header = header;
		}
		
		public String getHeader()
		{
			return this.header;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.subethamail.core.plugin.i.Filter#getName()
	 */
	public String getName()
	{
		return "List Headers";
	}

	/*
	 * (non-Javadoc)
	 * @see org.subethamail.core.plugin.i.Filter#getDescription()
	 */
	public String getDescription()
	{
		return "Appends the List-* headers as defined in RFC2369.";
	}

	/**
	 * @see Filter#onSend(SubEthaMessage, SendFilterContext)
	 */
	@Override
	public void onSend(SubEthaMessage msg, SendFilterContext ctx) throws IgnoreException, MessagingException
	{
		log.debug(this.getName() + " onSend()");

// 		Remove all RFC defined List-* headers then add our own...
//		The RFC defines some logic for this, but for now
//		let's just remove them unconditionally.
		for (ListHeader listHeader : ListHeader.ALL)
		{
			msg.removeHeader(listHeader.getHeader());
		}
		
		ListData listData = ctx.getList();
		msg.setHeader("List-Help", "<" + listData.getUrl() + ">");
		msg.setHeader("List-Unsubscribe", "<" + listData.getUrl() + ">");
		msg.setHeader("List-Subscribe", "<" + listData.getUrl() + ">");
		msg.setHeader("List-Post", "<mailto:" + listData.getEmail() + ">");
		// TODO: this should be much more properly generated
		msg.setHeader("List-Owner", "<mailto:" + listData.getEmail().replace("@", "-owner@") + ">");
		// TODO: Need to have a way to figure out the URL to the archives.
//		msg.setHeader("List-Archive", "<" + listData. + "> (Web Archive)");
	}
}

@@ -65,16 +65,62 @@
 ter;%0D%0A%0D%0A
+import java.io.UnsupportedEncodingException;%0D%0A
 import j
@@ -138,24 +138,24 @@
 llections;%0D%0A
-
 import java.
@@ -264,32 +264,73 @@
 gingException;%0D%0A
+import javax.mail.internet.MimeUtility;%0D%0A
 import org.apach
@@ -1383,16 +1383,40 @@
 er%0D%0A%09%7B%0D%0A
+%09%09LIST_ID (%22List-Id%22),%0D%0A
 %09%09LIST_H
@@ -2905,20 +2905,16 @@
 ;%0D%0A%09%09%7D%0D%0A
-%09%09%0D%0A
 %09%09ListDa
@@ -2942,16 +2942,196 @@
 List();%0D
+%0A%0D%0A%09%09try%0D%0A%09%09%7B%0D%0A%09%09%09msg.setHeader(%22List-Id%22, MimeUtility.encodeText(listData.getName()) + %22 %3C%22 + listData.getEmail() + %22%3E%22);%0D%0A%09%09%7D%0D%0A%09%09catch (UnsupportedEncodingException e)%0D%0A%09%09%7B%0D%0A%09%09%7D%0D
 %0A%09%09msg.s

add a list-id field.