package hudson.plugins.jira;

import hudson.Extension;
import hudson.model.User;
import hudson.plugins.jira.soap.RemoteUser;
import hudson.tasks.MailAddressResolver;

import javax.xml.rpc.ServiceException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

/**
 * Resolve user email by searching his userId as username in JIRA.
 *
 * @author Honza Brázdil jbrazdil@redhat.com
 */
@Extension
public class JiraMailAddressResolver extends MailAddressResolver {
    private static final Logger LOGGER = Logger.getLogger(JiraMailAddressResolver.class.getName());

    /**
     * Boolean to disable the Jira mail address resolver.
     *
     * To disable set the System property "-Dhudson.plugins.jira.JiraMailAddressResolver.DISABLE=true"
     */
    public static boolean DISABLE = Boolean.getBoolean(JiraMailAddressResolver.class.getName() + ".DISABLE");

    @Override
    public String findMailAddressFor(User u) {
        if (DISABLE)
            return null;

        String username = u.getId();

        for (JiraSite site : JiraProjectProperty.DESCRIPTOR.getSites()) {
            try {
                JiraSession session = site.createSession();
                if (session != null) {
                    RemoteUser user = session.service.getUser(session.token, username);
                    if (user != null) {
                        String email = user.getEmail();
                        if (email != null) {
                            email = unmaskEmail(email);
                            return email;
                        }
                    }
                }
            } catch (IOException ex) {
                LOGGER.log(Level.WARNING, "Unable to create session with " + site.getName(), ex);
            } catch (ServiceException ex) {
                LOGGER.log(Level.WARNING, "Unable to create session with " + site.getName(), ex);
            }
        }
        return null;
    }

    private static final String PRE = "[( \\[<_{\"=]+";
    private static final String POST = "[) \\]>_}\"=]+";
    private static final Pattern AT = Pattern.compile(PRE + "[aA][tT]" + POST);
    private static final Pattern DOT = Pattern.compile(PRE + "[dD][oO0][tT]" + POST);

    // unmask emails like "john dot doe at example dot com" to john.doe@example.com
    static String unmaskEmail(String email) {
        email = AT.matcher(email).replaceAll("@");
        email = DOT.matcher(email).replaceAll(".");
        return email;
    }
}

@@ -789,23 +789,24 @@
 esolver.
-DISABLE
+disabled
 =true%22%0A 
@@ -838,23 +838,24 @@
 boolean 
-DISABLE
+disabled
  = Boole
@@ -912,23 +912,24 @@
 e() + %22.
-DISABLE
+disabled
 %22);%0A%0A   
@@ -1002,15 +1002,16 @@
 if (
-DISABLE
+disabled
 )%0A  

Rename to disabled.