1 31 package org.blojsom.plugin.akismet; 32 33 import net.sf.akismet.Akismet; 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.blojsom.blog.Blog; 37 import org.blojsom.blog.Entry; 38 import org.blojsom.event.Event; 39 import org.blojsom.event.EventBroadcaster; 40 import org.blojsom.event.Listener; 41 import org.blojsom.plugin.Plugin; 42 import org.blojsom.plugin.PluginException; 43 import org.blojsom.plugin.comment.CommentModerationPlugin; 44 import org.blojsom.plugin.comment.CommentPlugin; 45 import org.blojsom.plugin.comment.event.CommentResponseSubmissionEvent; 46 import org.blojsom.plugin.comment.event.CommentMarkedSpamEvent; 47 import org.blojsom.plugin.comment.event.CommentUnmarkedSpamEvent; 48 import org.blojsom.plugin.pingback.event.PingbackResponseSubmissionEvent; 49 import org.blojsom.plugin.pingback.event.PingbackMarkedSpamEvent; 50 import org.blojsom.plugin.pingback.event.PingbackUnmarkedSpamEvent; 51 import org.blojsom.plugin.pingback.PingbackPlugin; 52 import org.blojsom.plugin.response.event.ResponseSubmissionEvent; 53 import org.blojsom.plugin.trackback.TrackbackModerationPlugin; 54 import org.blojsom.plugin.trackback.TrackbackPlugin; 55 import org.blojsom.plugin.trackback.event.TrackbackResponseSubmissionEvent; 56 import org.blojsom.plugin.trackback.event.TrackbackUnmarkedSpamEvent; 57 import org.blojsom.plugin.trackback.event.TrackbackMarkedSpamEvent; 58 import org.blojsom.util.BlojsomUtils; 59 60 import javax.servlet.http.HttpServletRequest ; 61 import javax.servlet.http.HttpServletResponse ; 62 import java.util.Map ; 63 64 71 public class AkismetModerationPlugin implements Plugin, Listener { 72 73 private Log _logger = LogFactory.getLog(AkismetModerationPlugin.class); 74 75 private static final String AKISMET_PLUGIN_API_KEY_IP = "akismet-plugin-api-key"; 77 private static final String AKISMET_PLUGIN_DELETE_SPAM_IP = "akismet-plugin-delete-spam"; 78 private static final String AKISMET_PLUGIN_AUTOMATIC_APPROVAL_IP = "akismet-plugin-automatic-approval"; 79 private static final boolean DELETE_SPAM_DEFAULT = false; 80 81 private static final String AKISMET_PLUGIN_RESPONSE = "AKISMET_PLUGIN_RESPONSE"; 83 84 private EventBroadcaster _eventBroadcaster; 85 86 89 public AkismetModerationPlugin() { 90 } 91 92 97 public void setEventBroadcaster(EventBroadcaster eventBroadcaster) { 98 _eventBroadcaster = eventBroadcaster; 99 } 100 101 107 public void init() throws PluginException { 108 _eventBroadcaster.addListener(this); 109 } 110 111 122 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 123 return entries; 124 } 125 126 132 public void cleanup() throws PluginException { 133 } 134 135 141 public void destroy() throws PluginException { 142 } 143 144 149 public void handleEvent(Event event) { 150 } 151 152 157 public void processEvent(Event event) { 158 if (event instanceof ResponseSubmissionEvent) { 159 ResponseSubmissionEvent responseSubmissionEvent = (ResponseSubmissionEvent) event; 160 Blog blog = responseSubmissionEvent.getBlog(); 161 162 String akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP); 163 if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) { 164 if (_logger.isInfoEnabled()) { 165 _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP); 166 } 167 } else { 168 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL()); 169 String responseType = Akismet.COMMENT_TYPE_BLANK; 170 171 if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) { 172 responseType = Akismet.COMMENT_TYPE_COMMENT; 173 } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) { 174 responseType = Akismet.COMMENT_TYPE_TRACKBACK; 175 } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) { 176 responseType = Akismet.COMMENT_TYPE_PINGBACK; 177 } 178 179 HttpServletRequest httpServletRequest = responseSubmissionEvent.getHttpServletRequest(); 181 Map metaData = responseSubmissionEvent.getMetaData(); 182 183 StringBuffer entryLink = new StringBuffer ().append(blog.getBlogURL()) 184 .append(responseSubmissionEvent.getEntry().getCategory()) 185 .append(responseSubmissionEvent.getEntry().getPostSlug()); 186 187 boolean isSpam = akismet.commentCheck(httpServletRequest.getRemoteAddr(), 188 httpServletRequest.getHeader("User-Agent"), 189 httpServletRequest.getHeader("Referer"), 190 entryLink.toString(), 191 responseType, 192 responseSubmissionEvent.getSubmitter(), 193 responseSubmissionEvent.getSubmitterItem1(), 194 responseSubmissionEvent.getSubmitterItem2(), 195 responseSubmissionEvent.getContent(), null); 196 197 metaData.put(AKISMET_PLUGIN_RESPONSE, Boolean.toString(isSpam)); 198 199 if (isSpam) { 201 boolean deleteSpam = DELETE_SPAM_DEFAULT; 202 203 String deleteSpamValue = responseSubmissionEvent.getBlog().getProperty(AKISMET_PLUGIN_DELETE_SPAM_IP); 204 if (!BlojsomUtils.checkNullOrBlank(deleteSpamValue)) { 205 deleteSpam = Boolean.valueOf(deleteSpamValue).booleanValue(); 206 } 207 208 if (!deleteSpam) { 209 if (_logger.isDebugEnabled()) { 210 _logger.debug("Marking response for moderation"); 211 } 212 } else { 213 if (_logger.isDebugEnabled()) { 214 _logger.debug("Marking response for automatic deletion"); 215 } 216 } 217 218 if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) { 219 if (!deleteSpam) { 220 metaData.put(CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString()); 221 } else { 222 metaData.put(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY, Boolean.TRUE); 223 } 224 } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) { 225 if (!deleteSpam) { 226 metaData.put(TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString()); 227 } else { 228 metaData.put(TrackbackPlugin.BLOJSOM_PLUGIN_TRACKBACK_METADATA_DESTROY, Boolean.TRUE); 229 } 230 } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) { 231 if (!deleteSpam) { 232 metaData.put(PingbackPlugin.BLOJSOM_PINGBACK_PLUGIN_APPROVED, Boolean.FALSE.toString()); 233 } else { 234 metaData.put(PingbackPlugin.BLOJSOM_PLUGIN_PINGBACK_METADATA_DESTROY, Boolean.TRUE); 235 } 236 } 237 } else { 238 boolean automaticApproval = Boolean.valueOf(blog.getProperty(AKISMET_PLUGIN_AUTOMATIC_APPROVAL_IP)).booleanValue(); 239 if (automaticApproval) { 240 if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) { 241 if (!metaData.containsKey(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY)) { 242 metaData.put(CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED, Boolean.TRUE.toString()); 243 } 244 } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) { 245 if (!metaData.containsKey(TrackbackPlugin.BLOJSOM_PLUGIN_TRACKBACK_METADATA_DESTROY)) { 246 metaData.put(TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED, Boolean.TRUE.toString()); 247 } 248 } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) { 249 if (!metaData.containsKey(PingbackPlugin.BLOJSOM_PLUGIN_PINGBACK_METADATA_DESTROY)) { 250 metaData.put(PingbackPlugin.BLOJSOM_PINGBACK_PLUGIN_APPROVED, Boolean.TRUE.toString()); 251 } 252 } 253 } 254 } 255 256 responseSubmissionEvent.setMetaData(metaData); 257 } 258 } else if (event instanceof CommentMarkedSpamEvent) { 259 CommentMarkedSpamEvent commentMarkedSpamEvent = (CommentMarkedSpamEvent) event; 260 Blog blog = commentMarkedSpamEvent.getBlog(); 261 262 String akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP); 263 if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) { 264 if (_logger.isInfoEnabled()) { 265 _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP); 266 } 267 } else { 268 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL()); 269 String responseType = Akismet.COMMENT_TYPE_COMMENT; 270 StringBuffer permalink = new StringBuffer (); 271 permalink.append(blog.getBlogURL()).append("/") 272 .append(commentMarkedSpamEvent.getEntry().getBlogCategory().getName()) 273 .append(commentMarkedSpamEvent.getEntry().getPostSlug()); 274 275 akismet.submitSpam(commentMarkedSpamEvent.getComment().getIp(), 276 null, null, permalink.toString(), responseType, commentMarkedSpamEvent.getComment().getAuthor(), 277 commentMarkedSpamEvent.getComment().getAuthorEmail(), 278 commentMarkedSpamEvent.getComment().getAuthorURL(), 279 commentMarkedSpamEvent.getComment().getComment(), null); 280 } 281 } else if (event instanceof CommentUnmarkedSpamEvent) { 282 CommentUnmarkedSpamEvent commentUnmarkedSpamEvent = (CommentUnmarkedSpamEvent) event; 283 Blog blog = commentUnmarkedSpamEvent.getBlog(); 284 285 String akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP); 286 if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) { 287 if (_logger.isInfoEnabled()) { 288 _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP); 289 } 290 } else { 291 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL()); 292 String responseType = Akismet.COMMENT_TYPE_COMMENT; 293 StringBuffer permalink = new StringBuffer (); 294 permalink.append(blog.getBlogURL()).append("/") 295 .append(commentUnmarkedSpamEvent.getEntry().getBlogCategory().getName()) 296 .append(commentUnmarkedSpamEvent.getEntry().getPostSlug()); 297 298 akismet.submitHam(commentUnmarkedSpamEvent.getComment().getIp(), 299 null, null, permalink.toString(), responseType, commentUnmarkedSpamEvent.getComment().getAuthor(), 300 commentUnmarkedSpamEvent.getComment().getAuthorEmail(), 301 commentUnmarkedSpamEvent.getComment().getAuthorURL(), 302 commentUnmarkedSpamEvent.getComment().getComment(), null); 303 } 304 } else if (event instanceof TrackbackMarkedSpamEvent) { 305 TrackbackMarkedSpamEvent trackbackMarkedSpamEvent = (TrackbackMarkedSpamEvent) event; 306 Blog blog = trackbackMarkedSpamEvent.getBlog(); 307 308 String akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP); 309 if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) { 310 if (_logger.isInfoEnabled()) { 311 _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP); 312 } 313 } else { 314 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL()); 315 String responseType = Akismet.COMMENT_TYPE_TRACKBACK; 316 StringBuffer permalink = new StringBuffer (); 317 permalink.append(blog.getBlogURL()).append("/") 318 .append(trackbackMarkedSpamEvent.getEntry().getBlogCategory().getName()) 319 .append(trackbackMarkedSpamEvent.getEntry().getPostSlug()); 320 321 akismet.submitSpam(trackbackMarkedSpamEvent.getTrackback().getIp(), 322 null, null, permalink.toString(), responseType, trackbackMarkedSpamEvent.getTrackback().getTitle(), 323 trackbackMarkedSpamEvent.getTrackback().getBlogName(), 324 trackbackMarkedSpamEvent.getTrackback().getUrl(), 325 trackbackMarkedSpamEvent.getTrackback().getExcerpt(), null); 326 } 327 } else if (event instanceof TrackbackUnmarkedSpamEvent) { 328 TrackbackUnmarkedSpamEvent trackbackUnmarkedSpamEvent = (TrackbackUnmarkedSpamEvent) event; 329 Blog blog = trackbackUnmarkedSpamEvent.getBlog(); 330 331 String akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP); 332 if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) { 333 if (_logger.isInfoEnabled()) { 334 _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP); 335 } 336 } else { 337 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL()); 338 String responseType = Akismet.COMMENT_TYPE_TRACKBACK; 339 StringBuffer permalink = new StringBuffer (); 340 permalink.append(blog.getBlogURL()).append("/") 341 .append(trackbackUnmarkedSpamEvent.getEntry().getBlogCategory().getName()) 342 .append(trackbackUnmarkedSpamEvent.getEntry().getPostSlug()); 343 344 akismet.submitHam(trackbackUnmarkedSpamEvent.getTrackback().getIp(), 345 null, null, permalink.toString(), responseType, trackbackUnmarkedSpamEvent.getTrackback().getTitle(), 346 trackbackUnmarkedSpamEvent.getTrackback().getBlogName(), 347 trackbackUnmarkedSpamEvent.getTrackback().getUrl(), 348 trackbackUnmarkedSpamEvent.getTrackback().getExcerpt(), null); 349 } 350 } else if (event instanceof PingbackMarkedSpamEvent) { 351 PingbackMarkedSpamEvent pingbackMarkedSpamEvent = (PingbackMarkedSpamEvent) event; 352 Blog blog = pingbackMarkedSpamEvent.getBlog(); 353 354 String akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP); 355 if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) { 356 if (_logger.isInfoEnabled()) { 357 _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP); 358 } 359 } else { 360 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL()); 361 String responseType = Akismet.COMMENT_TYPE_PINGBACK; 362 StringBuffer permalink = new StringBuffer (); 363 permalink.append(blog.getBlogURL()).append("/") 364 .append(pingbackMarkedSpamEvent.getEntry().getBlogCategory().getName()) 365 .append(pingbackMarkedSpamEvent.getEntry().getPostSlug()); 366 367 akismet.submitSpam(pingbackMarkedSpamEvent.getPingback().getIp(), 368 null, null, permalink.toString(), responseType, pingbackMarkedSpamEvent.getPingback().getTitle(), 369 pingbackMarkedSpamEvent.getPingback().getBlogName(), 370 pingbackMarkedSpamEvent.getPingback().getUrl(), 371 pingbackMarkedSpamEvent.getPingback().getExcerpt(), null); 372 } 373 } else if (event instanceof PingbackUnmarkedSpamEvent) { 374 PingbackUnmarkedSpamEvent pingbackUnmarkedSpamEvent = (PingbackUnmarkedSpamEvent) event; 375 Blog blog = pingbackUnmarkedSpamEvent.getBlog(); 376 377 String akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP); 378 if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) { 379 if (_logger.isInfoEnabled()) { 380 _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP); 381 } 382 } else { 383 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL()); 384 String responseType = Akismet.COMMENT_TYPE_PINGBACK; 385 StringBuffer permalink = new StringBuffer (); 386 permalink.append(blog.getBlogURL()).append("/") 387 .append(pingbackUnmarkedSpamEvent.getEntry().getBlogCategory().getName()) 388 .append(pingbackUnmarkedSpamEvent.getEntry().getPostSlug()); 389 390 akismet.submitHam(pingbackUnmarkedSpamEvent.getPingback().getIp(), 391 null, null, permalink.toString(), responseType, pingbackUnmarkedSpamEvent.getPingback().getTitle(), 392 pingbackUnmarkedSpamEvent.getPingback().getBlogName(), 393 pingbackUnmarkedSpamEvent.getPingback().getUrl(), 394 pingbackUnmarkedSpamEvent.getPingback().getExcerpt(), null); 395 } 396 } 397 } 398 } 399 | Popular Tags |