01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/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: * <mailet match="SenderIsRegex=<regular-expression>" class="<any-class>">
38: * </CODE></PRE>
39: * <P>The example below will match any sender in the format user@log.anything</P>
40: * <PRE><CODE>
41: * <mailet match="SenderIsRegex=(.*)@log\.(.*)" class="<any-class>">
42: * </mailet>
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: * <mailet match="SenderIsRegex=(.*)(mp3|emmepitre)(.*)@" class="<any-class>">
48: * </mailet>
49: * </CODE></PRE>
50: *
51: * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
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,
67: Perl5Compiler.READ_ONLY_MASK);
68: } catch (MalformedPatternException mpe) {
69: throw new MessagingException("Malformed pattern: "
70: + 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: }
|