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.comment.CommentModerationPlugin;
042: import org.blojsom.plugin.comment.CommentPlugin;
043: import org.blojsom.plugin.comment.event.CommentResponseSubmissionEvent;
044: import org.blojsom.plugin.pingback.PingbackPlugin;
045: import org.blojsom.plugin.pingback.event.PingbackResponseSubmissionEvent;
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.io.BufferedReader;
055: import java.io.IOException;
056: import java.io.StringReader;
057: import java.util.ArrayList;
058: import java.util.Iterator;
059: import java.util.List;
060: import java.util.Map;
061:
062: /**
063: * Spam phrase moderation plugin
064: *
065: * @author David Czarnecki
066: * @since blojsom 3.0
067: * @version $Id: SpamPhraseModerationPlugin.java,v 1.4 2007/01/17 02:35:12 czarneckid Exp $
068: */
069: public class SpamPhraseModerationPlugin implements Plugin, Listener {
070:
071: private Log _logger = LogFactory
072: .getLog(SpamPhraseModerationPlugin.class);
073:
074: private static final String SPAM_PHRASE_BLACKLIST_IP = "spam-phrase-blacklist";
075: private static final String DELETE_PHRASESPAM = "delete-phrasespam";
076: private static final boolean DEFAULT_DELETE_PHRASESPAM = false;
077:
078: private EventBroadcaster _eventBroadcaster;
079:
080: /**
081: * Create a new instance of the spam phrase moderation plugin
082: */
083: public SpamPhraseModerationPlugin() {
084: }
085:
086: /**
087: * Set the {@link EventBroadcaster}
088: *
089: * @param eventBroadcaster {@link EventBroadcaster}
090: */
091: public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
092: _eventBroadcaster = eventBroadcaster;
093: }
094:
095: /**
096: * Initialize this plugin. This method only called when the plugin is instantiated.
097: *
098: * @throws org.blojsom.plugin.PluginException
099: * If there is an error initializing the plugin
100: */
101: public void init() throws PluginException {
102: _eventBroadcaster.addListener(this );
103: }
104:
105: /**
106: * Process the blog entries
107: *
108: * @param httpServletRequest Request
109: * @param httpServletResponse Response
110: * @param blog {@link Blog} instance
111: * @param context Context
112: * @param entries Blog entries retrieved for the particular request
113: * @return Modified set of blog entries
114: * @throws PluginException If there is an error processing the blog entries
115: */
116: public Entry[] process(HttpServletRequest httpServletRequest,
117: HttpServletResponse httpServletResponse, Blog blog,
118: Map context, Entry[] entries) throws PluginException {
119: return entries;
120: }
121:
122: /**
123: * Perform any cleanup for the plugin. Called after {@link #process}.
124: *
125: * @throws org.blojsom.plugin.PluginException
126: * If there is an error performing cleanup for this plugin
127: */
128: public void cleanup() throws PluginException {
129: }
130:
131: /**
132: * Called when BlojsomServlet is taken out of service
133: *
134: * @throws org.blojsom.plugin.PluginException
135: * If there is an error in finalizing this plugin
136: */
137: public void destroy() throws PluginException {
138: }
139:
140: /**
141: * Handle an event broadcast from another component
142: *
143: * @param event {@link org.blojsom.event.Event} to be handled
144: */
145: public void handleEvent(Event event) {
146: }
147:
148: /**
149: * Load the list of spam phrases from the blog
150: *
151: * @param blog {@link Blog}
152: * @return List of spam phrases
153: */
154: protected List loadSpamPhrases(Blog blog) {
155: ArrayList spamPhrases = new ArrayList(25);
156:
157: String spamPhrasesValues = blog
158: .getProperty(SPAM_PHRASE_BLACKLIST_IP);
159: if (!BlojsomUtils.checkNullOrBlank(spamPhrasesValues)) {
160: try {
161: StringReader stringReader = new StringReader(
162: spamPhrasesValues);
163: BufferedReader br = new BufferedReader(stringReader);
164: String phrase;
165:
166: while ((phrase = br.readLine()) != null) {
167: spamPhrases.add(phrase);
168: }
169:
170: br.close();
171: } catch (IOException e) {
172: if (_logger.isErrorEnabled()) {
173: _logger.error(e);
174: }
175: }
176: }
177:
178: return spamPhrases;
179: }
180:
181: /**
182: * Process an event from another component
183: *
184: * @param event {@link org.blojsom.event.Event} to be handled
185: */
186: public void processEvent(Event event) {
187: if (event instanceof ResponseSubmissionEvent) {
188: ResponseSubmissionEvent responseSubmissionEvent = (ResponseSubmissionEvent) event;
189: String content = responseSubmissionEvent.getContent();
190: String submitter = responseSubmissionEvent.getSubmitter();
191: String submitterItem1 = responseSubmissionEvent
192: .getSubmitterItem1();
193: String submitterItem2 = responseSubmissionEvent
194: .getSubmitterItem2();
195:
196: if (!BlojsomUtils.checkNullOrBlank(content)) {
197: List spamPhrases = loadSpamPhrases(responseSubmissionEvent
198: .getBlog());
199:
200: boolean deletePhraseSpam = DEFAULT_DELETE_PHRASESPAM;
201: Map metaData = responseSubmissionEvent.getMetaData();
202:
203: String deletePhraseSpamValue = responseSubmissionEvent
204: .getBlog().getProperty(DELETE_PHRASESPAM);
205: if (!BlojsomUtils
206: .checkNullOrBlank(deletePhraseSpamValue)) {
207: deletePhraseSpam = Boolean.valueOf(
208: deletePhraseSpamValue).booleanValue();
209: }
210:
211: String phrase;
212: Iterator phraseIterator = spamPhrases.iterator();
213: boolean phraseSpamFound = false;
214: while (phraseIterator.hasNext()) {
215: phrase = (String) phraseIterator.next();
216:
217: if (((content != null) && (content.matches(phrase) || content
218: .indexOf(phrase) != -1))
219: || ((submitter != null) && (submitter
220: .matches(phrase) || submitter
221: .indexOf(phrase) != -1))
222: || ((submitterItem1 != null) && (submitterItem1
223: .matches(phrase) || submitterItem1
224: .indexOf(phrase) != -1))
225: || ((submitterItem2 != null) && (submitterItem2
226: .matches(phrase) || submitterItem2
227: .indexOf(phrase) != -1))) {
228: phraseSpamFound = true;
229: break;
230: }
231: }
232:
233: if (phraseSpamFound) {
234: if (!deletePhraseSpam) {
235: if (_logger.isDebugEnabled()) {
236: _logger
237: .debug("Marking response for moderation");
238: }
239: } else {
240: if (_logger.isDebugEnabled()) {
241: _logger
242: .debug("Marking response for automatic deletion");
243: }
244: }
245:
246: if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) {
247: if (!deletePhraseSpam) {
248: metaData
249: .put(
250: CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED,
251: Boolean.FALSE.toString());
252: } else {
253: metaData
254: .put(
255: CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY,
256: Boolean.TRUE);
257: }
258: } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) {
259: if (!deletePhraseSpam) {
260: metaData
261: .put(
262: TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED,
263: Boolean.FALSE.toString());
264: } else {
265: metaData
266: .put(
267: TrackbackPlugin.BLOJSOM_PLUGIN_TRACKBACK_METADATA_DESTROY,
268: Boolean.TRUE);
269: }
270: } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) {
271: if (deletePhraseSpam) {
272: metaData
273: .put(
274: PingbackPlugin.BLOJSOM_PLUGIN_PINGBACK_METADATA_DESTROY,
275: Boolean.TRUE);
276: }
277: }
278: }
279: } else {
280: if (_logger.isDebugEnabled()) {
281: _logger
282: .debug("No content to evaluate for response");
283: }
284: }
285: }
286: }
287: }
|