KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > matchers > InSpammerBlacklist


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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 JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
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 JavaDoc network = null;
46
47     public void init() throws MessagingException JavaDoc {
48         network = getCondition();
49     }
50
51     public Collection JavaDoc match(Mail mail) {
52         String JavaDoc host = mail.getRemoteAddr();
53         try {
54             //Have to reverse the octets first
55
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
56             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(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 JavaDoc uhe) {
72             //This is good... it's not on the list
73
return null;
74         }
75     }
76 }
77
Popular Tags