MailAddressResolver.java 6.5 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 32
import hudson.model.AbstractProject;
import hudson.model.Hudson;
import hudson.model.User;
33
import hudson.scm.CVSSCM;
K
kohsuke 已提交
34
import hudson.scm.SCM;
35

K
kohsuke 已提交
36
import java.util.HashMap;
37 38
import java.util.List;
import java.util.Map;
K
kohsuke 已提交
39
import java.util.regex.Matcher;
K
kohsuke 已提交
40
import java.util.regex.Pattern;
41 42 43 44 45 46

/**
 * 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 已提交
47
 * of this class should put {@link Extension} on your implementation class, like this:
48 49
 *
 * <pre>
K
kohsuke 已提交
50 51 52
 * &#64;Extension
 * class MyMailAddressResolver extends {@link MailAddressResolver} {
 *   ...
53 54 55 56
 * }
 * </pre>
 *
 * @author Kohsuke Kawaguchi
57
 * @since 1.192
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
 */
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);
    
84
    public static String resolve(User u) {
K
kohsuke 已提交
85
        for (MailAddressResolver r : all()) {
86
            String email = r.findMailAddressFor(u);
87 88
            if(email!=null) return email;
        }
K
kohsuke 已提交
89 90 91 92 93 94 95 96 97 98

        // fall back logic
        String extractedAddress = extractAddressFromId(u.getId());
        if (extractedAddress != null)
                return extractedAddress;

        if(u.getId().contains("@"))
            // this already looks like an e-mail ID
            return u.getId();

99
        String ds = Mailer.descriptor().getDefaultSuffix();
K
kohsuke 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113
        if(ds!=null)
            return u.getId()+ds;
        else
            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;
114 115
    }

K
kohsuke 已提交
116 117 118 119 120 121
    /**
     * Matches strings like "Kohsuke Kawaguchi &lt;kohsuke.kawaguchi@sun.com>"
     * @see #extractAddressFromId(String)
     */
    private static final Pattern EMAIL_ADDRESS_REGEXP = Pattern.compile("^.*<([^>]+)>.*$");

122 123 124

    /**
     * All registered {@link MailAddressResolver} implementations.
K
kohsuke 已提交
125 126 127
     *
     * @deprecated as of 1.286
     *      Use {@link #all()} for read access and {@link Extension} for registration.
128
     */
K
kohsuke 已提交
129
    public static final List<MailAddressResolver> LIST = ExtensionListView.createList(MailAddressResolver.class);
130

K
kohsuke 已提交
131 132 133 134 135
    /**
     * Returns all the registered {@link MailAddressResolver} descriptors.
     */
    public static ExtensionList<MailAddressResolver> all() {
        return Hudson.getInstance().getExtensionList(MailAddressResolver.class);
136 137 138 139 140 141 142 143 144
    }

    /**
     * {@link MailAddressResolver} implemenations that cover well-known major public sites.
     *
     * <p>
     * Since this has low UI visibility, we are open to having a large number of rules here.
     * If you'd like to add one, please contribute more rules.
     */
K
kohsuke 已提交
145
    @Extension
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    public static class DefaultAddressResolver extends MailAddressResolver {
        public String findMailAddressFor(User u) {
            for (AbstractProject<?,?> p : u.getProjects()) {
                SCM scm = p.getScm();
                if (scm instanceof CVSSCM) {
                    CVSSCM cvsscm = (CVSSCM) scm;
                    
                    String s = findMailAddressFor(u,cvsscm.getCvsRoot());
                    if(s!=null) return s;
                }
            }

            // didn't hit any known rules
            return null;
        }

        /**
         *
         * @param scm
         *      String that represents SCM connectivity.
         */
        protected String findMailAddressFor(User u, String scm) {
            for (Map.Entry<Pattern, String> e : RULE_TABLE.entrySet())
                if(e.getKey().matcher(scm).matches())
                    return u.getId()+e.getValue();
            return null;
        }

        private static final Map<Pattern,String/*suffix*/> RULE_TABLE = new HashMap<Pattern, String>();

        static {
            {// java.net
                String username = "([A-Za-z0-9_\\-])+";
                String host = "(.*.dev.java.net|kohsuke.sfbay.*)";
                Pattern cvsUrl = Pattern.compile(":pserver:"+username+"@"+host+":/cvs");

                RULE_TABLE.put(cvsUrl,"@dev.java.net");
            }

            {// source forge
                Pattern cvsUrl = Pattern.compile(":(pserver|ext):([^@]+)@([^.]+).cvs.(sourceforge|sf).net:.+");
187

188 189 190 191 192 193 194 195
                RULE_TABLE.put(cvsUrl,"@users.sourceforge.net");
            }

            // TODO: read some file under $HUDSON_HOME?
        }

    }
}