001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.moderation;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.blog.Blog;
035: import org.blojsom.blog.Entry;
036: import org.blojsom.event.Event;
037: import org.blojsom.event.EventBroadcaster;
038: import org.blojsom.event.Listener;
039: import org.blojsom.plugin.Plugin;
040: import org.blojsom.plugin.PluginException;
041: import org.blojsom.plugin.pingback.event.PingbackResponseSubmissionEvent;
042: import org.blojsom.plugin.pingback.PingbackPlugin;
043: import org.blojsom.plugin.comment.CommentModerationPlugin;
044: import org.blojsom.plugin.comment.CommentPlugin;
045: import org.blojsom.plugin.comment.event.CommentResponseSubmissionEvent;
046: import org.blojsom.plugin.response.event.ResponseSubmissionEvent;
047: import org.blojsom.plugin.trackback.TrackbackModerationPlugin;
048: import org.blojsom.plugin.trackback.TrackbackPlugin;
049: import org.blojsom.plugin.trackback.event.TrackbackResponseSubmissionEvent;
050: import org.blojsom.util.BlojsomUtils;
051:
052: import javax.servlet.http.HttpServletRequest;
053: import javax.servlet.http.HttpServletResponse;
054: import java.util.Map;
055: import java.util.regex.Matcher;
056: import java.util.regex.Pattern;
057:
058: /**
059: * Link spam moderation plugin
060: *
061: * @author David Czarnecki
062: * @version $Id: LinkSpamModerationPlugin.java,v 1.3 2007/01/17 02:35:12 czarneckid Exp $
063: * @since blojsom 3.0
064: */
065: public class LinkSpamModerationPlugin implements Plugin, Listener {
066:
067: private Log _logger = LogFactory
068: .getLog(LinkSpamModerationPlugin.class);
069:
070: private static final Pattern LINK_PATTERN = Pattern.compile(
071: "<a.*?href=.*?>", Pattern.UNICODE_CASE
072: | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
073: | Pattern.DOTALL);
074: private static final String LINKSPAM_COMMENT_THRESHOLD = "linkspam-comment-threshold";
075: private static final String LINKSPAM_TRACKBACK_THRESHOLD = "linkspam-trackback-threshold";
076: private static final String DELETE_LINKSPAM = "delete-linkspam";
077:
078: private static final int DEFAULT_LINK_THRESHOLD = 3;
079:
080: private EventBroadcaster _eventBroadcaster;
081:
082: /**
083: * Create a new instance of the link spam moderation plugin
084: */
085: public LinkSpamModerationPlugin() {
086: }
087:
088: /**
089: * Set the {@link EventBroadcaster} event broadcaster
090: *
091: * @param eventBroadcaster {@link EventBroadcaster}
092: */
093: public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
094: _eventBroadcaster = eventBroadcaster;
095: }
096:
097: /**
098: * Initialize this plugin. This method only called when the plugin is instantiated.
099: *
100: * @throws org.blojsom.plugin.PluginException
101: * If there is an error initializing the plugin
102: */
103: public void init() throws PluginException {
104: _eventBroadcaster.addListener(this );
105: }
106:
107: /**
108: * Process the blog entries
109: *
110: * @param httpServletRequest Request
111: * @param httpServletResponse Response
112: * @param blog {@link Blog} instance
113: * @param context Context
114: * @param entries Blog entries retrieved for the particular request
115: * @return Modified set of blog entries
116: * @throws PluginException If there is an error processing the blog entries
117: */
118: public Entry[] process(HttpServletRequest httpServletRequest,
119: HttpServletResponse httpServletResponse, Blog blog,
120: Map context, Entry[] entries) throws PluginException {
121: return entries;
122: }
123:
124: /**
125: * Perform any cleanup for the plugin. Called after {@link #process}.
126: *
127: * @throws org.blojsom.plugin.PluginException
128: * If there is an error performing cleanup for this plugin
129: */
130: public void cleanup() throws PluginException {
131:
132: }
133:
134: /**
135: * Called when BlojsomServlet is taken out of service
136: *
137: * @throws org.blojsom.plugin.PluginException
138: * If there is an error in finalizing this plugin
139: */
140: public void destroy() throws PluginException {
141:
142: }
143:
144: /**
145: * Handle an event broadcast from another component
146: *
147: * @param event {@link org.blojsom.event.Event} to be handled
148: */
149: public void handleEvent(Event event) {
150:
151: }
152:
153: /**
154: * Process an event from another component
155: *
156: * @param event {@link org.blojsom.event.Event} to be handled
157: */
158: public void processEvent(Event event) {
159: if (event instanceof ResponseSubmissionEvent) {
160: ResponseSubmissionEvent responseSubmissionEvent = (ResponseSubmissionEvent) event;
161: String text = responseSubmissionEvent.getContent();
162: Map metaData = responseSubmissionEvent.getMetaData();
163:
164: if (!BlojsomUtils.checkNullOrBlank(text)) {
165: Matcher linkMatcher = LINK_PATTERN.matcher(text);
166: int linkCount = 0;
167:
168: while (linkMatcher.find()) {
169: linkCount++;
170: }
171:
172: int linkThreshold;
173: String thresholdProperty = "";
174:
175: if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) {
176: thresholdProperty = LINKSPAM_COMMENT_THRESHOLD;
177: } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) {
178: thresholdProperty = LINKSPAM_TRACKBACK_THRESHOLD;
179: }
180:
181: String thresholdPropertyValue = responseSubmissionEvent
182: .getBlog().getProperty(thresholdProperty);
183: if (BlojsomUtils
184: .checkNullOrBlank(thresholdPropertyValue)) {
185: thresholdPropertyValue = Integer
186: .toString(DEFAULT_LINK_THRESHOLD);
187: }
188:
189: String deleteLinkSpamPropertyValue = responseSubmissionEvent
190: .getBlog().getProperty(DELETE_LINKSPAM);
191: boolean deleteLinkSpam;
192:
193: try {
194: linkThreshold = Integer
195: .parseInt(thresholdPropertyValue);
196: } catch (NumberFormatException e) {
197: linkThreshold = DEFAULT_LINK_THRESHOLD;
198: }
199:
200: deleteLinkSpam = Boolean.valueOf(
201: deleteLinkSpamPropertyValue).booleanValue();
202:
203: if (linkCount >= linkThreshold) {
204: _logger
205: .debug("Exceeded threshold for links in response: "
206: + linkCount + " > " + linkThreshold);
207:
208: if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) {
209: if (!deleteLinkSpam) {
210: metaData
211: .put(
212: CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED,
213: Boolean.FALSE.toString());
214: } else {
215: metaData
216: .put(
217: CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY,
218: Boolean.TRUE);
219: }
220: } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) {
221: if (!deleteLinkSpam) {
222: metaData
223: .put(
224: TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED,
225: Boolean.FALSE.toString());
226: } else {
227: metaData
228: .put(
229: TrackbackPlugin.BLOJSOM_PLUGIN_TRACKBACK_METADATA_DESTROY,
230: Boolean.TRUE);
231: }
232: } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) {
233: if (deleteLinkSpam) {
234: metaData
235: .put(
236: PingbackPlugin.BLOJSOM_PLUGIN_PINGBACK_METADATA_DESTROY,
237: Boolean.TRUE);
238: }
239: }
240: }
241: }
242:
243: }
244: }
245: }
|