View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.transport.matchers;
19  
20  import org.apache.mailet.GenericMatcher;
21  import org.apache.mailet.Mail;
22  import org.apache.mailet.MailAddress;
23  import org.apache.oro.text.regex.MalformedPatternException;
24  import org.apache.oro.text.regex.Pattern;
25  import org.apache.oro.text.regex.Perl5Compiler;
26  import org.apache.oro.text.regex.Perl5Matcher;
27  
28  import java.util.Collection;
29  
30  import javax.mail.MessagingException;
31  
32  /***
33   * <P>Matches mails that are sent by a sender whose address matches a regular expression.</P>
34   * <P>Is equivalent to the {@link RecipientIsRegex} matcher but matching on the sender.</P>
35   * <P>Configuration string: a regular expression.</P>
36   * <PRE><CODE>
37   * &lt;mailet match=&quot;SenderIsRegex=&lt;regular-expression&gt;&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
38   * </CODE></PRE>
39   * <P>The example below will match any sender in the format user@log.anything</P>
40   * <PRE><CODE>
41   * &lt;mailet match=&quot;SenderIsRegex=(.*)@log\.(.*)&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
42   * &lt;/mailet&gt;
43   * </CODE></PRE>
44   * <P>Another example below will match any sender having some variations of the string
45   * <I>mp3</I> inside the username part.</P>
46   * <PRE><CODE>
47   * &lt;mailet match=&quot;SenderIsRegex=(.*)(mp3|emmepitre)(.*)@&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
48   * &lt;/mailet&gt;
49   * </CODE></PRE>
50   *
51   * @version CVS $Revision: 365582 $ $Date: 2006-01-03 08:51:21 +0000 (mar, 03 gen 2006) $
52   * @since 2.2.0
53   */
54  public class SenderIsRegex extends GenericMatcher {
55      Pattern pattern   = null;
56  
57      public void init() throws MessagingException {
58          String patternString = getCondition();
59          if (patternString == null) {
60              throw new MessagingException("Pattern is missing");
61          }
62          
63          patternString = patternString.trim();
64          Perl5Compiler compiler = new Perl5Compiler();
65          try {
66              pattern = compiler.compile(patternString, Perl5Compiler.READ_ONLY_MASK);
67          } catch(MalformedPatternException mpe) {
68              throw new MessagingException("Malformed pattern: " + patternString, mpe);
69          }
70      }
71  
72      public Collection match(Mail mail) {
73          MailAddress mailAddress = mail.getSender();
74          if (mailAddress == null) {
75              return null;
76          }
77          String senderString = mailAddress.toString();
78          Perl5Matcher matcher  = new Perl5Matcher();
79          if (matcher.matches(senderString, pattern)) {
80              return mail.getRecipients();
81          }
82          return null;
83      }
84  }