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  
23  import javax.mail.MessagingException;
24  import java.net.InetAddress;
25  import java.net.UnknownHostException;
26  import java.util.Collection;
27  import java.util.StringTokenizer;
28  
29  /***
30   * Checks the network IP address of the sending server against a
31   * blacklist of spammers.  There are 3 lists that support this...
32   * <ul>
33   * <li><b>blackholes.mail-abuse.org</b>: Rejected - see  http://www.mail-abuse.org/rbl/
34   * <li><b>dialups.mail-abuse.org</b>: Dialup - see http://www.mail-abuse.org/dul/
35   * <li><b>relays.mail-abuse.org</b>: Open spam relay - see http://www.mail-abuse.org/rss/
36   * </ul>
37   *
38   * Example:
39   * &lt;mailet match="InSpammerBlacklist=blackholes.mail-abuse.org" class="ToProcessor"&gt;
40   *   &lt;processor&gt;spam&lt;/processor&gt;
41   * &lt;/mailet&gt;
42   *
43   */
44  public class InSpammerBlacklist extends GenericMatcher {
45      String network = null;
46  
47      public void init() throws MessagingException {
48          network = getCondition();
49      }
50  
51      public Collection match(Mail mail) {
52          String host = mail.getRemoteAddr();
53          try {
54              //Have to reverse the octets first
55              StringBuffer sb = new StringBuffer();
56              StringTokenizer st = new StringTokenizer(host, " .", false);
57  
58              while (st.hasMoreTokens()) {
59                  sb.insert(0, st.nextToken() + ".");
60              }
61  
62              //Add the network prefix for this blacklist
63              sb.append(network);
64  
65              //Try to look it up
66              org.apache.james.dnsserver.DNSServer.getByName(sb.toString());
67  
68              //If we got here, that's bad... it means the host
69              //  was found in the blacklist
70              return mail.getRecipients();
71          } catch (UnknownHostException uhe) {
72              //This is good... it's not on the list
73              return null;
74          }
75      }
76  }