Ticket #69: RcptMatcher.java

File RcptMatcher.java, 5.4 kB (added by jza, 4 months ago)

Proposal: doFilter changed

Line 
1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005  Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  */
21 package org.jsmtpd.plugins.filters.builtin;
22
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.jsmtpd.core.common.PluginInitException;
30 import org.jsmtpd.core.common.filter.FilterTreeFailureException;
31 import org.jsmtpd.core.common.filter.FilterTreeSuccesException;
32 import org.jsmtpd.core.common.filter.IFilter;
33 import org.jsmtpd.core.mail.Email;
34 import org.jsmtpd.core.mail.EmailAddress;
35 import org.jsmtpd.core.mail.InvalidAddress;
36 import org.jsmtpd.core.mail.Rcpt;
37
38 /**
39  * This filter returns true if the RCPTS specified are present.
40  * If multiple RCPTS are present, you can configure the logical operator between the matches
41  * by default all rcpt are required to validate the filter. set the properet orOperator to true
42  * will return true at the first rcpt found.
43  *
44  * Additionnally, you can set failOnError to true if you wish to break the tree when rcpt are not found.
45  * You can also validate the whole tree is the filter is passed successfully by setting validateOnSucces to true.
46  *
47  * @author Jean-Francois POUX
48  */
49 public class RcptMatcher implements IFilter {
50
51     private boolean orOperator = false;
52
53     private List<EmailAddress> rcptToMatch = new LinkedList<EmailAddress>();
54     private Log log = LogFactory.getLog(RcptMatcher.class);
55     private boolean failOnError = false;
56     private boolean validateOnSucces = false;
57
58     /* (non-Javadoc)
59      * @see org.jsmtpd.core.common.filter.IFilter#doFilter(org.jsmtpd.core.mail.Email)
60      */
61     public boolean doFilter(Email input) throws FilterTreeFailureException, FilterTreeSuccesException {
62         List<Rcpt> rcptInMail = input.getRcpt();
63         boolean res = false;
64         boolean found = false;
65         if (orOperator) {
66             for (Iterator iterator = rcptInMail.iterator(); iterator.hasNext();) {
67                 Rcpt element = (Rcpt) iterator.next();
68                 EmailAddress oneRcptInMail = element.getEmailAddress();
69                 for (Iterator iter = rcptToMatch.iterator(); iter.hasNext();) {
70                     EmailAddress oneRcptToMatch = (EmailAddress) iter.next();
71                     if (oneRcptToMatch.isEqual(oneRcptInMail)) {
72                         found = true;
73                         break;
74                     }
75                 }
76                 if (found) {
77                     break;
78                 }
79             }
80         } else {
81             log.debug("Plugin: " + getPluginName() + " Start validating without orOperator");
82             for (Iterator iterator = rcptInMail.iterator(); iterator.hasNext();) {
83                 Rcpt element = (Rcpt) iterator.next();
84                 EmailAddress oneRcptInMail = element.getEmailAddress();
85                 found = false;
86                 for (Iterator iter = rcptToMatch.iterator(); iter.hasNext();) {
87                     EmailAddress oneRcptToMatch = (EmailAddress) iter.next();
88                     if (oneRcptToMatch.isEqual(oneRcptInMail)) {
89                         found = true;
90                         break;
91                     }
92                 }
93                 if (!found) {
94                     break;
95                 }
96             }
97         }
98         if (found) {
99             res = true;
100         }
101         if (failOnError && (!res)) {
102             throw new FilterTreeFailureException();
103         }
104
105         if (validateOnSucces && res)
106             throw new FilterTreeSuccesException();
107
108         return res;
109     }
110
111     /* (non-Javadoc)
112      * @see org.jsmtpd.core.common.IGenericPlugin#getPluginName()
113      */
114     public String getPluginName() {
115         return "Recipient filter matcher for Jsmtp";
116     }
117
118     /* (non-Javadoc)
119      * @see org.jsmtpd.core.common.IGenericPlugin#initPlugin()
120      */
121     public void initPlugin() throws PluginInitException {
122
123     }
124
125     /* (non-Javadoc)
126      * @see org.jsmtpd.core.common.IGenericPlugin#shutdownPlugin()
127      */
128     public void shutdownPlugin() {
129
130     }
131
132     public void setRcpt(String rcpt) {
133         try {
134             EmailAddress rcptToAdd = EmailAddress.parseAddress(rcpt);
135             rcptToMatch.add(rcptToAdd);
136         } catch (InvalidAddress e) {
137             log.error("Plugin: " + getPluginName() + " can't add rcpt to matching list, " + rcpt + " is not a valid email adress");
138         }
139     }
140
141     public void setOrOperator(boolean cond) {
142         orOperator = cond;
143     }
144
145     public void setFailOnError(boolean op) {
146         failOnError = op;
147     }
148
149     public void setValidateOnSucces(boolean op) {
150         validateOnSucces = op;
151     }
152 }