PageRenderTime 154ms CodeModel.GetById 32ms RepoModel.GetById 7ms app.codeStats 0ms

/core/src/main/java/hudson/tasks/MailAddressResolver.java

https://github.com/fujibee/hudson
Java | 195 lines | 78 code | 23 blank | 94 comment | 12 complexity | 4c0ad61196f5aedd11a4896395a674a1 MD5 | raw file
  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Luca Domenico Milanesio
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.tasks;
  25. import hudson.Extension;
  26. import hudson.ExtensionList;
  27. import hudson.ExtensionListView;
  28. import hudson.ExtensionPoint;
  29. import hudson.model.AbstractProject;
  30. import hudson.model.Hudson;
  31. import hudson.model.User;
  32. import hudson.scm.CVSSCM;
  33. import hudson.scm.SCM;
  34. import java.util.HashMap;
  35. import java.util.List;
  36. import java.util.Map;
  37. import java.util.regex.Matcher;
  38. import java.util.regex.Pattern;
  39. /**
  40. * Infers e-mail addresses for the user when none is specified.
  41. *
  42. * <p>
  43. * This is an extension point of Hudson. Plugins tha contribute new implementation
  44. * of this class should put {@link Extension} on your implementation class, like this:
  45. *
  46. * <pre>
  47. * &#64;Extension
  48. * class MyMailAddressResolver extends {@link MailAddressResolver} {
  49. * ...
  50. * }
  51. * </pre>
  52. *
  53. * @author Kohsuke Kawaguchi
  54. * @since 1.192
  55. */
  56. public abstract class MailAddressResolver implements ExtensionPoint {
  57. /**
  58. * Infers e-mail address of the given user.
  59. *
  60. * <p>
  61. * This method is called when a {@link User} without explicitly configured e-mail
  62. * address is used, as an attempt to infer e-mail address.
  63. *
  64. * <p>
  65. * The normal strategy is to look at {@link User#getProjects() the projects that the user
  66. * is participating}, then use the repository information to infer the e-mail address.
  67. *
  68. * <p>
  69. * When multiple resolvers are installed, they are consulted in order and
  70. * the search will be over when an address is inferred by someone.
  71. *
  72. * <p>
  73. * Since {@link MailAddressResolver} is singleton, this method can be invoked concurrently
  74. * from multiple threads.
  75. *
  76. * @return
  77. * null if the inference failed.
  78. */
  79. public abstract String findMailAddressFor(User u);
  80. public static String resolve(User u) {
  81. for (MailAddressResolver r : all()) {
  82. String email = r.findMailAddressFor(u);
  83. if(email!=null) return email;
  84. }
  85. // fall back logic
  86. String extractedAddress = extractAddressFromId(u.getFullName());
  87. if (extractedAddress != null)
  88. return extractedAddress;
  89. if(u.getFullName().contains("@"))
  90. // this already looks like an e-mail ID
  91. return u.getFullName();
  92. String ds = Mailer.descriptor().getDefaultSuffix();
  93. if(ds!=null)
  94. return u.getId()+ds;
  95. else
  96. return null;
  97. }
  98. /**
  99. * Tries to extract an email address from the user id, or returns null
  100. */
  101. private static String extractAddressFromId(String id) {
  102. Matcher m = EMAIL_ADDRESS_REGEXP.matcher(id);
  103. if(m.matches())
  104. return m.group(1);
  105. return null;
  106. }
  107. /**
  108. * Matches strings like "Kohsuke Kawaguchi &lt;kohsuke.kawaguchi@sun.com>"
  109. * @see #extractAddressFromId(String)
  110. */
  111. private static final Pattern EMAIL_ADDRESS_REGEXP = Pattern.compile("^.*<([^>]+)>.*$");
  112. /**
  113. * All registered {@link MailAddressResolver} implementations.
  114. *
  115. * @deprecated as of 1.286
  116. * Use {@link #all()} for read access and {@link Extension} for registration.
  117. */
  118. public static final List<MailAddressResolver> LIST = ExtensionListView.createList(MailAddressResolver.class);
  119. /**
  120. * Returns all the registered {@link MailAddressResolver} descriptors.
  121. */
  122. public static ExtensionList<MailAddressResolver> all() {
  123. return Hudson.getInstance().getExtensionList(MailAddressResolver.class);
  124. }
  125. /**
  126. * {@link MailAddressResolver} implemenations that cover well-known major public sites.
  127. *
  128. * <p>
  129. * Since this has low UI visibility, we are open to having a large number of rules here.
  130. * If you'd like to add one, please contribute more rules.
  131. */
  132. @Extension
  133. public static class DefaultAddressResolver extends MailAddressResolver {
  134. public String findMailAddressFor(User u) {
  135. for (AbstractProject<?,?> p : u.getProjects()) {
  136. SCM scm = p.getScm();
  137. if (scm instanceof CVSSCM) {
  138. CVSSCM cvsscm = (CVSSCM) scm;
  139. String s = findMailAddressFor(u,cvsscm.getCvsRoot());
  140. if(s!=null) return s;
  141. }
  142. }
  143. // didn't hit any known rules
  144. return null;
  145. }
  146. /**
  147. *
  148. * @param scm
  149. * String that represents SCM connectivity.
  150. */
  151. protected String findMailAddressFor(User u, String scm) {
  152. for (Map.Entry<Pattern, String> e : RULE_TABLE.entrySet())
  153. if(e.getKey().matcher(scm).matches())
  154. return u.getId()+e.getValue();
  155. return null;
  156. }
  157. private static final Map<Pattern,String/*suffix*/> RULE_TABLE = new HashMap<Pattern, String>();
  158. static {
  159. {// java.net
  160. String username = "([A-Za-z0-9_\\-])+";
  161. String host = "(.*.dev.java.net|kohsuke.sfbay.*)";
  162. Pattern cvsUrl = Pattern.compile(":pserver:"+username+"@"+host+":/cvs");
  163. RULE_TABLE.put(cvsUrl,"@dev.java.net");
  164. }
  165. {// source forge
  166. Pattern cvsUrl = Pattern.compile(":(pserver|ext):([^@]+)@([^.]+).cvs.(sourceforge|sf).net:.+");
  167. RULE_TABLE.put(cvsUrl,"@users.sourceforge.net");
  168. }
  169. // TODO: read some file under $HUDSON_HOME?
  170. }
  171. }
  172. }