MailAddressResolver.java 5.3 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * The MIT License
 * 
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Luca Domenico Milanesio
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24 25
package hudson.tasks;

K
kohsuke 已提交
26 27 28
import hudson.Extension;
import hudson.ExtensionList;
import hudson.ExtensionListView;
29
import hudson.ExtensionPoint;
K
kohsuke 已提交
30 31
import hudson.model.Hudson;
import hudson.model.User;
32 33

import java.util.List;
34
import java.util.logging.Logger;
K
kohsuke 已提交
35
import java.util.regex.Matcher;
K
kohsuke 已提交
36
import java.util.regex.Pattern;
37 38 39 40 41 42

/**
 * Infers e-mail addresses for the user when none is specified.
 *
 * <p>
 * This is an extension point of Hudson. Plugins tha contribute new implementation
K
kohsuke 已提交
43
 * of this class should put {@link Extension} on your implementation class, like this:
44 45
 *
 * <pre>
K
kohsuke 已提交
46 47 48
 * &#64;Extension
 * class MyMailAddressResolver extends {@link MailAddressResolver} {
 *   ...
49 50 51 52
 * }
 * </pre>
 *
 * @author Kohsuke Kawaguchi
53
 * @since 1.192
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
 */
public abstract class MailAddressResolver implements ExtensionPoint {
    /**
     * Infers e-mail address of the given user.
     *
     * <p>
     * This method is called when a {@link User} without explicitly configured e-mail
     * address is used, as an attempt to infer e-mail address.
     *
     * <p>
     * The normal strategy is to look at {@link User#getProjects() the projects that the user
     * is participating}, then use the repository information to infer the e-mail address.
     *
     * <p>
     * When multiple resolvers are installed, they are consulted in order and
     * the search will be over when an address is inferred by someone.
     *
     * <p>
     * Since {@link MailAddressResolver} is singleton, this method can be invoked concurrently
     * from multiple threads.
     *
     * @return
     *      null if the inference failed.
     */
    public abstract String findMailAddressFor(User u);
    
80
    public static String resolve(User u) {
81 82
        LOGGER.fine("Resolving e-mail address for \""+u+"\" ID="+u.getId());

K
kohsuke 已提交
83
        for (MailAddressResolver r : all()) {
84
            String email = r.findMailAddressFor(u);
85 86 87 88
            if(email!=null) {
                LOGGER.fine(r+" resolved "+u.getId()+" to "+email);
                return email;
            }
89
        }
K
kohsuke 已提交
90 91

        // fall back logic
92
        String extractedAddress = extractAddressFromId(u.getFullName());
K
kohsuke 已提交
93
        if (extractedAddress != null)
94
            return extractedAddress;
K
kohsuke 已提交
95

96
        if(u.getFullName().contains("@"))
K
kohsuke 已提交
97
            // this already looks like an e-mail ID
98
            return u.getFullName();
K
kohsuke 已提交
99

100
        String ds = Mailer.descriptor().getDefaultSuffix();
K
kohsuke 已提交
101 102 103 104 105 106 107
        if(ds!=null) {
            // another common pattern is "DOMAIN\person" in Windows. Only
            // do this when this full name is not manually set. see HUDSON-5164
            Matcher m = WINDOWS_DOMAIN_REGEXP.matcher(u.getFullName());
            if (m.matches() && u.getFullName().replace('\\','_').equals(u.getId()))
                return m.group(1)+ds; // user+defaultSuffix

K
kohsuke 已提交
108
            return u.getId()+ds;
K
kohsuke 已提交
109
        } else
K
kohsuke 已提交
110 111 112 113 114 115 116 117 118 119 120
            return null;
    }

    /**
     * Tries to extract an email address from the user id, or returns null
     */
    private static String extractAddressFromId(String id) {
        Matcher m = EMAIL_ADDRESS_REGEXP.matcher(id);
        if(m.matches())
    		return m.group(1);
    	return null;
121 122
    }

K
kohsuke 已提交
123 124 125 126 127 128
    /**
     * Matches strings like "Kohsuke Kawaguchi &lt;kohsuke.kawaguchi@sun.com>"
     * @see #extractAddressFromId(String)
     */
    private static final Pattern EMAIL_ADDRESS_REGEXP = Pattern.compile("^.*<([^>]+)>.*$");

K
kohsuke 已提交
129 130 131 132
    /**
     * Matches something like "DOMAIN\person"
     */
    private static final Pattern WINDOWS_DOMAIN_REGEXP = Pattern.compile("[^\\\\ ]+\\\\([^\\\\ ]+)");
133 134 135

    /**
     * All registered {@link MailAddressResolver} implementations.
K
kohsuke 已提交
136 137 138
     *
     * @deprecated as of 1.286
     *      Use {@link #all()} for read access and {@link Extension} for registration.
139
     */
K
kohsuke 已提交
140
    public static final List<MailAddressResolver> LIST = ExtensionListView.createList(MailAddressResolver.class);
141

K
kohsuke 已提交
142 143 144 145 146
    /**
     * Returns all the registered {@link MailAddressResolver} descriptors.
     */
    public static ExtensionList<MailAddressResolver> all() {
        return Hudson.getInstance().getExtensionList(MailAddressResolver.class);
147
    }
148 149

    private static final Logger LOGGER = Logger.getLogger(MailAddressResolver.class.getName());
150
}