// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.

package jodd.mail;

import jodd.core.JoddCore;
import jodd.util.CharUtil;
import jodd.util.StringPool;
import jodd.util.StringUtil;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * Email utilities.
 */
public class EmailUtil {

	protected static final String ATTR_CHARSET = "charset=";

	/**
	 * Extracts mime type from parts content type.
	 */
	public static String extractMimeType(String contentType) {
		int ndx = contentType.indexOf(';');
		String mime;
		if (ndx != -1) {
			mime = contentType.substring(0, ndx);
		} else {
			mime = contentType;
		}
		return mime;
	}

	/**
	 * Parses content type for encoding. May return <code>null</code>
	 * if encoding is not specified in content type.
	 */
	public static String extractEncoding(String contentType) {
		int ndx = contentType.indexOf(';');
		String charset = ndx != -1 ? contentType.substring(ndx + 1) : StringPool.EMPTY;
		String encoding = null;

		ndx = charset.indexOf(ATTR_CHARSET);
		if (ndx != -1) {
			ndx += ATTR_CHARSET.length();
			int len = charset.length();

			if (charset.charAt(ndx) == '"') {
				ndx++;
			}
			int start = ndx;

			while (ndx < len) {
				char c = charset.charAt(ndx);
				if ((c == '"') || (CharUtil.isWhitespace(c) == true) || (c == ';')) {
					break;
				}
				ndx++;
			}
			encoding = charset.substring(start, ndx);
		}
		return encoding;
	}

	/**
	 * Converts mail address to strings.
	 */
	public static String[] address2String(Address[] addresses) {
		if (addresses == null) {
			return null;
		}
		if (addresses.length == 0) {
			return null;
		}
		String[] res = new String[addresses.length];
		for (int i = 0; i < addresses.length; i++) {
			Address address = addresses[i];
			res[i] = address.toString();
		}
		return res;
	}

	/**
	 * Converts string to <code>InternetAddress</code> while taking care of encoding.
	 * The email can be given in following form:
	 * <ul>
	 *     <li>"email" - the whole string is an email</li>
	 *     <li>"personal {@literal <email>}" - first part of the string is personal, and
	 *     		the other part is email, surrounded with &lt; and &gt;</li>
	 * </ul>
	 */
	public static InternetAddress string2Address(String address) throws AddressException {
		address = address.trim();
		String charset = JoddCore.encoding;

		if (StringUtil.endsWithChar(address, '>') == false) {
			try {
				return new InternetAddress(address, null, charset);
			} catch (UnsupportedEncodingException ueex) {
				throw new AddressException(ueex.toString());
			}
		}

		int ndx = address.lastIndexOf('<');
		if (ndx == -1) {
			throw new AddressException("Invalid address: " + address);
		}

		try {
			return new InternetAddress(
					address.substring(ndx + 1, address.length() - 1),
					address.substring(0, ndx - 1).trim(), charset);
		} catch (UnsupportedEncodingException ueex) {
			throw new AddressException(ueex.toString());
		}
	}

	/**
	 * Reads EML from a file and parses it into {@link ReceivedEmail}.
	 */
	public static ReceivedEmail parseEML(File emlFile) throws FileNotFoundException, MessagingException {
		Properties props = System.getProperties();
		Session session = Session.getDefaultInstance(props, null);

		Message message = new MimeMessage(session, new FileInputStream(emlFile));

		return new ReceivedEmail(message);
	}

	/**
	 * Parse EML from content into {@link ReceivedEmail}.
	 */
	public static ReceivedEmail parseEML(String emlContent) throws MessagingException {
		Properties props = System.getProperties();
		Session session = Session.getDefaultInstance(props, null);

		Message message = null;
		try {
			message = new MimeMessage(session, new ByteArrayInputStream(emlContent.getBytes(StringPool.US_ASCII)));
		} catch (UnsupportedEncodingException ueex) {
			throw new MessagingException(ueex.toString());
		}

		return new ReceivedEmail(message);
	}

}
@@ -89,35 +89,8 @@
 l;%0A%0A
-import jodd.core.JoddCore;%0A
 impo
@@ -145,64 +145,8 @@
 ol;%0A
-import jodd.util.StringUtil;%0A%0Aimport javax.mail.Address;
 %0Aimp
@@ -238,97 +238,8 @@
 on;%0A
-import javax.mail.internet.AddressException;%0Aimport javax.mail.internet.InternetAddress;%0A
 impo
@@ -1650,1528 +1650,8 @@
 %09%7D%0A%0A
-%09/**%0A%09 * Converts mail address to strings.%0A%09 */%0A%09public static String%5B%5D address2String(Address%5B%5D addresses) %7B%0A%09%09if (addresses == null) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09if (addresses.length == 0) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09String%5B%5D res = new String%5Baddresses.length%5D;%0A%09%09for (int i = 0; i %3C addresses.length; i++) %7B%0A%09%09%09Address address = addresses%5Bi%5D;%0A%09%09%09res%5Bi%5D = address.toString();%0A%09%09%7D%0A%09%09return res;%0A%09%7D%0A%0A%09/**%0A%09 * Converts string to %3Ccode%3EInternetAddress%3C/code%3E while taking care of encoding.%0A%09 * The email can be given in following form:%0A%09 * %3Cul%3E%0A%09 *     %3Cli%3E%22email%22 - the whole string is an email%3C/li%3E%0A%09 *     %3Cli%3E%22personal %7B@literal %3Cemail%3E%7D%22 - first part of the string is personal, and%0A%09 *     %09%09the other part is email, surrounded with &lt; and &gt;%3C/li%3E%0A%09 * %3C/ul%3E%0A%09 */%0A%09public static InternetAddress string2Address(String address) throws AddressException %7B%0A%09%09address = address.trim();%0A%09%09String charset = JoddCore.encoding;%0A%0A%09%09if (StringUtil.endsWithChar(address, '%3E') == false) %7B%0A%09%09%09try %7B%0A%09%09%09%09return new InternetAddress(address, null, charset);%0A%09%09%09%7D catch (UnsupportedEncodingException ueex) %7B%0A%09%09%09%09throw new AddressException(ueex.toString());%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09int ndx = address.lastIndexOf('%3C');%0A%09%09if (ndx == -1) %7B%0A%09%09%09throw new AddressException(%22Invalid address: %22 + address);%0A%09%09%7D%0A%0A%09%09try %7B%0A%09%09%09return new InternetAddress(%0A%09%09%09%09%09address.substring(ndx + 1, address.length() - 1),%0A%09%09%09%09%09address.substring(0, ndx - 1).trim(), charset);%0A%09%09%7D catch (UnsupportedEncodingException ueex) %7B%0A%09%09%09throw new AddressException(ueex.toString());%0A%09%09%7D%0A%09%7D%0A%0A
 %09/**

Remove methods that are moved to MailAddress