KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > SpamChecker


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 package org.apache.roller.util;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23 import java.util.regex.Pattern JavaDoc;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.roller.config.RollerConfig;
27 import org.apache.roller.config.RollerRuntimeConfig;
28 import org.apache.roller.pojos.CommentData;
29 import org.apache.roller.pojos.RefererData;
30 import org.apache.roller.pojos.WebsiteData;
31
32 /**
33  * Checks comment, trackbacks and referrers for spam.
34  * @author Lance Lavandowska
35  * @author Dave Johnson
36  */

37 public class SpamChecker {
38     private static Log mLogger = LogFactory.getLog(SpamChecker.class);
39     
40     /** Test comment, applying all blacklists, if configured */
41     public static boolean checkComment(CommentData comment) {
42         if (RollerConfig.getBooleanProperty("site.blacklist.enable.comments")) {
43             return testComment(comment);
44         }
45         return false;
46     }
47     
48     /** Test trackback comment, applying all blacklists, if configured */
49     public static boolean checkTrackback(CommentData comment) {
50         if (RollerConfig.getBooleanProperty("site.blacklist.enable.trackbacks")) {
51             return testComment(comment);
52         }
53         return false;
54     }
55
56     /** Test referrer URL, applying blacklist and website blacklist only if configured */
57     public static boolean checkReferrer(WebsiteData website, String JavaDoc referrerURL) {
58         if (RollerConfig.getBooleanProperty("site.blacklist.enable.referrers")) {
59             List JavaDoc stringRules = new ArrayList JavaDoc();
60             List JavaDoc regexRules = new ArrayList JavaDoc();
61             Blacklist.populateSpamRules(
62                 website.getBlacklist(), stringRules, regexRules, null);
63             if (RollerRuntimeConfig.getProperty("spam.blacklist") != null) {
64                 Blacklist.populateSpamRules(
65                     RollerRuntimeConfig.getProperty("spam.blacklist"), stringRules, regexRules, null);
66             }
67             return Blacklist.matchesRulesOnly(referrerURL, stringRules, regexRules);
68         }
69         return false;
70     }
71
72     /** Test comment against built in blacklist, site blacklist and website blacklist */
73     private static boolean testComment(CommentData c) {
74         boolean ret = false;
75         List JavaDoc stringRules = new ArrayList JavaDoc();
76         List JavaDoc regexRules = new ArrayList JavaDoc();
77         WebsiteData website = c.getWeblogEntry().getWebsite();
78         Blacklist.populateSpamRules(
79             website.getBlacklist(), stringRules, regexRules,
80             RollerRuntimeConfig.getProperty("spam.blacklist"));
81         Blacklist blacklist = Blacklist.getBlacklist();
82         if ( blacklist.isBlacklisted(c.getUrl(), stringRules, regexRules)
83             || blacklist.isBlacklisted(c.getEmail(), stringRules, regexRules)
84             || blacklist.isBlacklisted(c.getName(), stringRules, regexRules)
85             || blacklist.isBlacklisted(c.getContent(), stringRules, regexRules)) {
86             ret = true;
87         }
88         return ret;
89     }
90 }
91
92
Popular Tags