SpamPhraseModerationPlugin.java [plain text]
package org.blojsom.plugin.moderation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.blojsom.blog.BlogEntry;
import org.blojsom.blog.BlogUser;
import org.blojsom.blog.BlojsomConfiguration;
import org.blojsom.event.BlojsomEvent;
import org.blojsom.event.BlojsomListener;
import org.blojsom.plugin.BlojsomPlugin;
import org.blojsom.plugin.BlojsomPluginException;
import org.blojsom.plugin.comment.CommentModerationPlugin;
import org.blojsom.plugin.comment.CommentPlugin;
import org.blojsom.plugin.comment.event.CommentResponseSubmissionEvent;
import org.blojsom.plugin.response.event.ResponseSubmissionEvent;
import org.blojsom.plugin.trackback.TrackbackModerationPlugin;
import org.blojsom.plugin.trackback.TrackbackPlugin;
import org.blojsom.plugin.trackback.event.TrackbackResponseSubmissionEvent;
import org.blojsom.util.BlojsomConstants;
import org.blojsom.util.BlojsomUtils;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
public class SpamPhraseModerationPlugin implements BlojsomPlugin, BlojsomListener {
private Log _logger = LogFactory.getLog(SpamPhraseModerationPlugin.class);
private static final String SPAM_PHRASE_BLACKLIST_IP = "spam-phrase-blacklist";
private static final String DEFAULT_SPAM_PHRASE_BLACKLIST_FILE = "spam-phrase-blacklist.properties";
private static final String DELETE_PHRASESPAM = "delete-phrasespam";
private static final boolean DEFAULT_DELETE_PHRASESPAM = false;
private BlojsomConfiguration _blojsomConfiguration;
private String _spamPhraseBlacklist = DEFAULT_SPAM_PHRASE_BLACKLIST_FILE;
public SpamPhraseModerationPlugin() {
}
public void init(ServletConfig servletConfig, BlojsomConfiguration blojsomConfiguration) throws BlojsomPluginException {
blojsomConfiguration.getEventBroadcaster().addListener(this);
_blojsomConfiguration = blojsomConfiguration;
String spamPhraseBlacklist = servletConfig.getInitParameter(SPAM_PHRASE_BLACKLIST_IP);
if (!BlojsomUtils.checkNullOrBlank(spamPhraseBlacklist)) {
_spamPhraseBlacklist = spamPhraseBlacklist;
}
_logger.debug("Initialized spam phrase blacklist plugin");
}
public BlogEntry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlogUser user, Map context, BlogEntry[] entries) throws BlojsomPluginException {
return entries;
}
public void cleanup() throws BlojsomPluginException {
}
public void destroy() throws BlojsomPluginException {
}
public void handleEvent(BlojsomEvent event) {
}
public void processEvent(BlojsomEvent event) {
if (event instanceof ResponseSubmissionEvent) {
ResponseSubmissionEvent responseSubmissionEvent = (ResponseSubmissionEvent) event;
String content = responseSubmissionEvent.getContent();
if (!BlojsomUtils.checkNullOrBlank(content)) {
File blacklistFile = new File(_blojsomConfiguration.getInstallationDirectory() + _blojsomConfiguration.getBaseConfigurationDirectory() + "/" +
responseSubmissionEvent.getBlog().getId() + "/" + _spamPhraseBlacklist);
if (blacklistFile.exists()) {
try {
FileInputStream fis = new FileInputStream(blacklistFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fis, BlojsomConstants.UTF8));
ArrayList spamPhrases = new ArrayList(25);
String phrase;
while ((phrase = br.readLine()) != null) {
spamPhrases.add(phrase);
}
br.close();
boolean deletePhraseSpam = DEFAULT_DELETE_PHRASESPAM;
Map metaData = responseSubmissionEvent.getMetaData();
String deletePhraseSpamValue = responseSubmissionEvent.getBlog().getBlog().getBlogProperty(DELETE_PHRASESPAM);
if (!BlojsomUtils.checkNullOrBlank(deletePhraseSpamValue)) {
deletePhraseSpam = Boolean.valueOf(deletePhraseSpamValue).booleanValue();
}
Iterator phraseIterator = spamPhrases.iterator();
boolean phraseSpamFound = false;
while (phraseIterator.hasNext()) {
phrase = (String) phraseIterator.next();
if (content.matches(phrase) || content.indexOf(phrase) != -1) {
phraseSpamFound = true;
break;
}
}
if (phraseSpamFound) {
if (!deletePhraseSpam) {
_logger.debug("Marking response for moderation");
} else{
_logger.debug("Marking response for automatic deletion");
}
if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) {
if (!deletePhraseSpam) {
metaData.put(CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString());
} else {
metaData.put(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY, Boolean.TRUE);
}
} else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) {
if (!deletePhraseSpam) {
metaData.put(TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString());
} else {
metaData.put(TrackbackPlugin.BLOJSOM_PLUGIN_TRACKBACK_METADATA_DESTROY, Boolean.TRUE);
}
}
}
} catch (IOException e) {
_logger.error(e);
}
} else {
_logger.debug("Spam phrase blacklist not found: " + _spamPhraseBlacklist);
}
} else {
_logger.debug("No content to evaluate for response");
}
}
}
}