1 31 package org.blojsom.plugin.moderation; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.blojsom.blog.Blog; 36 import org.blojsom.blog.Entry; 37 import org.blojsom.event.Event; 38 import org.blojsom.event.EventBroadcaster; 39 import org.blojsom.event.Listener; 40 import org.blojsom.plugin.Plugin; 41 import org.blojsom.plugin.PluginException; 42 import org.blojsom.plugin.comment.CommentModerationPlugin; 43 import org.blojsom.plugin.comment.CommentPlugin; 44 import org.blojsom.plugin.comment.event.CommentResponseSubmissionEvent; 45 import org.blojsom.plugin.pingback.PingbackPlugin; 46 import org.blojsom.plugin.pingback.event.PingbackResponseSubmissionEvent; 47 import org.blojsom.plugin.response.event.ResponseSubmissionEvent; 48 import org.blojsom.plugin.trackback.TrackbackModerationPlugin; 49 import org.blojsom.plugin.trackback.TrackbackPlugin; 50 import org.blojsom.plugin.trackback.event.TrackbackResponseSubmissionEvent; 51 import org.blojsom.util.BlojsomUtils; 52 53 import javax.servlet.http.HttpServletRequest ; 54 import javax.servlet.http.HttpServletResponse ; 55 import java.io.BufferedReader ; 56 import java.io.IOException ; 57 import java.io.StringReader ; 58 import java.util.ArrayList ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 import java.util.Map ; 62 63 70 public class SpamPhraseModerationPlugin implements Plugin, Listener { 71 72 private Log _logger = LogFactory.getLog(SpamPhraseModerationPlugin.class); 73 74 private static final String SPAM_PHRASE_BLACKLIST_IP = "spam-phrase-blacklist"; 75 private static final String DELETE_PHRASESPAM = "delete-phrasespam"; 76 private static final boolean DEFAULT_DELETE_PHRASESPAM = false; 77 78 private EventBroadcaster _eventBroadcaster; 79 80 83 public SpamPhraseModerationPlugin() { 84 } 85 86 91 public void setEventBroadcaster(EventBroadcaster eventBroadcaster) { 92 _eventBroadcaster = eventBroadcaster; 93 } 94 95 101 public void init() throws PluginException { 102 _eventBroadcaster.addListener(this); 103 } 104 105 116 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 117 return entries; 118 } 119 120 126 public void cleanup() throws PluginException { 127 } 128 129 135 public void destroy() throws PluginException { 136 } 137 138 143 public void handleEvent(Event event) { 144 } 145 146 152 protected List loadSpamPhrases(Blog blog) { 153 ArrayList spamPhrases = new ArrayList (25); 154 155 String spamPhrasesValues = blog.getProperty(SPAM_PHRASE_BLACKLIST_IP); 156 if (!BlojsomUtils.checkNullOrBlank(spamPhrasesValues)) { 157 try { 158 StringReader stringReader = new StringReader (spamPhrasesValues); 159 BufferedReader br = new BufferedReader (stringReader); 160 String phrase; 161 162 while ((phrase = br.readLine()) != null) { 163 spamPhrases.add(phrase); 164 } 165 166 br.close(); 167 } catch (IOException e) { 168 if (_logger.isErrorEnabled()) { 169 _logger.error(e); 170 } 171 } 172 } 173 174 return spamPhrases; 175 } 176 177 182 public void processEvent(Event event) { 183 if (event instanceof ResponseSubmissionEvent) { 184 ResponseSubmissionEvent responseSubmissionEvent = (ResponseSubmissionEvent) event; 185 String content = responseSubmissionEvent.getContent(); 186 187 if (!BlojsomUtils.checkNullOrBlank(content)) { 188 List spamPhrases = loadSpamPhrases(responseSubmissionEvent.getBlog()); 189 190 boolean deletePhraseSpam = DEFAULT_DELETE_PHRASESPAM; 191 Map metaData = responseSubmissionEvent.getMetaData(); 192 193 String deletePhraseSpamValue = responseSubmissionEvent.getBlog().getProperty(DELETE_PHRASESPAM); 194 if (!BlojsomUtils.checkNullOrBlank(deletePhraseSpamValue)) { 195 deletePhraseSpam = Boolean.valueOf(deletePhraseSpamValue).booleanValue(); 196 } 197 198 String phrase; 199 Iterator phraseIterator = spamPhrases.iterator(); 200 boolean phraseSpamFound = false; 201 while (phraseIterator.hasNext()) { 202 phrase = (String ) phraseIterator.next(); 203 204 if (content.matches(phrase) || content.indexOf(phrase) != -1) { 205 phraseSpamFound = true; 206 break; 207 } 208 } 209 210 if (phraseSpamFound) { 211 if (!deletePhraseSpam) { 212 if (_logger.isDebugEnabled()) { 213 _logger.debug("Marking response for moderation"); 214 } 215 } else{ 216 if (_logger.isDebugEnabled()) { 217 _logger.debug("Marking response for automatic deletion"); 218 } 219 } 220 221 if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) { 222 if (!deletePhraseSpam) { 223 metaData.put(CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString()); 224 } else { 225 metaData.put(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY, Boolean.TRUE); 226 } 227 } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) { 228 if (!deletePhraseSpam) { 229 metaData.put(TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString()); 230 } else { 231 metaData.put(TrackbackPlugin.BLOJSOM_PLUGIN_TRACKBACK_METADATA_DESTROY, Boolean.TRUE); 232 } 233 } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) { 234 if (deletePhraseSpam) { 235 metaData.put(PingbackPlugin.BLOJSOM_PLUGIN_PINGBACK_METADATA_DESTROY, Boolean.TRUE); 236 } 237 } 238 } 239 } else { 240 if (_logger.isDebugEnabled()) { 241 _logger.debug("No content to evaluate for response"); 242 } 243 } 244 } 245 } 246 } | Popular Tags |