/*
 * $Id: VERPAddress.java 907 2007-02-07 09:06:00Z jon $
 * $URL: http://subetha.tigris.org/svn/subetha/branches/resin/core/src/org/subethamail/core/util/VERPAddress.java $
 */

package org.subethamail.core.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Class which abstracts the handling of VERP bounces.  This can
 * separate the VERP token from the original address.  It can
 * also assemble an address from an email and a token.
 * 
 * A VERP'd address looks like:
 * 
 * announce-verp-THISISTHETOKEN-bounce@domain.com
 * 
 * The extracted email will be announce@domain.com.
 * 
 * Note that the token is a lowercase hex-encoded string.
 * This is because some evil MTAs (Postfix grrr) force
 * the mailbox part of an address to lowercase, screwing
 * up any attempt at base64 encoding.
 * 
 * @author Jeff Schnitzer
 */
public class VERPAddress
{
	/** */
	private final static Logger log = LoggerFactory.getLogger(VERPAddress.class);
	
	/** This is the suffix of all VERP'd addresses, plus the '@' */
	public static final String SUFFIX = "-b@";
	
	/** This preceeds the token in the address */
	public static final String LEAD = "-verp-";
	
	/** */
	String email;
	String tokenEncoded;
	
	/** */
	VERPAddress(String email, String tokenBase62)
	{
		this.email = email;
		this.tokenEncoded = tokenBase62;
	}
	
	/**
	 * @return the "normal" email address, without the verp information.
	 *  This will be the mailing list address that the mail was from. 
	 */
	public String getEmail() { return this.email; }
	
	/**
	 * Lazily decodes the token.
	 * 
	 * @return the token that was extracted from the VERP address. 
	 */
	public byte[] getToken()
	{
		return Base62.decode(this.tokenEncoded);
	}

	/**
	 * @return The Base62 encoded string.
	 */
	public String getRawToken()
	{
		return this.tokenEncoded;
	}

	/**
	 * @return null if the address was not a VERP'ed address.  
	 */
	public static VERPAddress getVERPBounce(String addy)
	{
		// Note the suffix includes the @
		int suffixIndex = addy.indexOf(SUFFIX);
		if (suffixIndex < 0)
			return null;
		
		int leadIndex = addy.lastIndexOf(LEAD, suffixIndex);
		if (leadIndex < 0)
			return null;
		
		// Watch out for blah-verp-bounce@foo.bar
		if (leadIndex + LEAD.length() >= suffixIndex)
			return null;

		String email = addy.substring(0, leadIndex) + '@' + addy.substring(suffixIndex+SUFFIX.length());
		String tokenBase62 = addy.substring(leadIndex+LEAD.length(), suffixIndex);
		
		if (log.isDebugEnabled())
			log.debug(addy + " becomes " + email + "/" + tokenBase62);
		
		return new VERPAddress(email, tokenBase62);
	}
	
	/**
	 * @return an email address encoded with the binary token.
	 */
	public static String encodeVERP(String email, byte[] token)
	{
		int atIndex = email.indexOf('@');
		
		return email.substring(0, atIndex)
			+ LEAD + new String(Base62.encode(token)) + SUFFIX 
			+ email.substring(atIndex+1);
	}
}


	
@@ -227,57 +227,65 @@
 ort 
-org.slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory
+java.util.logging.Level;%0D%0A%0D%0Aimport lombok.extern.java.Log
 ;%0D%0A%0D
@@ -883,16 +883,22 @@
 r%0D%0A */%0D%0A
+@Log%0D%0A
 public c
@@ -922,100 +922,8 @@
 %0A%7B%0D%0A
-%09/** */%0D%0A%09private final static Logger log = LoggerFactory.getLogger(VERPAddress.class);%0D%0A%09%0D%0A
 %09/**
@@ -2497,73 +2497,100 @@
 g.is
-DebugEnabled())%0D%0A%09%09%09log.debug(addy + %22 becomes %22 + email + %22/%22 + 
+Loggable(Level.FINE))%0D%0A%09%09%09log.log(Level.FINE,%22%7B0%7D becomes %7B1%7D/%7B2%7D%22, new Object%5B%5D%7Baddy,email,
 toke
@@ -2592,24 +2592,25 @@
 ,tokenBase62
+%7D
 );%0D%0A%09%09%0D%0A%09%09re

switch logging framework