001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: package org.apache.roller.util;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.StringTokenizer;
023: import java.util.regex.Pattern;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.roller.config.RollerConfig;
027: import org.apache.roller.config.RollerRuntimeConfig;
028: import org.apache.roller.pojos.CommentData;
029: import org.apache.roller.pojos.RefererData;
030: import org.apache.roller.pojos.WebsiteData;
031:
032: /**
033: * Checks comment, trackbacks and referrers for spam.
034: * @author Lance Lavandowska
035: * @author Dave Johnson
036: */
037: public class SpamChecker {
038: private static Log mLogger = LogFactory.getLog(SpamChecker.class);
039:
040: /** Test comment, applying all blacklists, if configured */
041: public static boolean checkComment(CommentData comment) {
042: if (RollerConfig
043: .getBooleanProperty("site.blacklist.enable.comments")) {
044: return testComment(comment);
045: }
046: return false;
047: }
048:
049: /** Test trackback comment, applying all blacklists, if configured */
050: public static boolean checkTrackback(CommentData comment) {
051: if (RollerConfig
052: .getBooleanProperty("site.blacklist.enable.trackbacks")) {
053: return testComment(comment);
054: }
055: return false;
056: }
057:
058: /** Test referrer URL, applying blacklist and website blacklist only if configured */
059: public static boolean checkReferrer(WebsiteData website,
060: String referrerURL) {
061: if (RollerConfig
062: .getBooleanProperty("site.blacklist.enable.referrers")) {
063: List stringRules = new ArrayList();
064: List regexRules = new ArrayList();
065: Blacklist.populateSpamRules(website.getBlacklist(),
066: stringRules, regexRules, null);
067: if (RollerRuntimeConfig.getProperty("spam.blacklist") != null) {
068: Blacklist.populateSpamRules(RollerRuntimeConfig
069: .getProperty("spam.blacklist"), stringRules,
070: regexRules, null);
071: }
072: return Blacklist.matchesRulesOnly(referrerURL, stringRules,
073: regexRules);
074: }
075: return false;
076: }
077:
078: /** Test comment against built in blacklist, site blacklist and website blacklist */
079: private static boolean testComment(CommentData c) {
080: boolean ret = false;
081: List stringRules = new ArrayList();
082: List regexRules = new ArrayList();
083: WebsiteData website = c.getWeblogEntry().getWebsite();
084: Blacklist.populateSpamRules(website.getBlacklist(),
085: stringRules, regexRules, RollerRuntimeConfig
086: .getProperty("spam.blacklist"));
087: Blacklist blacklist = Blacklist.getBlacklist();
088: if (blacklist
089: .isBlacklisted(c.getUrl(), stringRules, regexRules)
090: || blacklist.isBlacklisted(c.getEmail(), stringRules,
091: regexRules)
092: || blacklist.isBlacklisted(c.getName(), stringRules,
093: regexRules)
094: || blacklist.isBlacklisted(c.getContent(), stringRules,
095: regexRules)) {
096: ret = true;
097: }
098: return ret;
099: }
100: }
|