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