lj + Akismet

After putting Akismet in last night, I woke up this morning to find that 40 comments had been blocked. Yay, Akismet! Still, a few got through, and I wanted to submit those back to Akismet. A few lines of code and all is done. Who said other people's Perl was hard to read?
--- cvs/livejournal/cgi-bin/talklib.pl	2007-07-05 15:44:25.186758848 -0400
+++ cgi-bin/talklib.pl	2007-07-16 11:08:52.073345947 -0400
@@ -1764,6 +1764,25 @@
         return 1 unless $ip;
     }
 
+    if($LJ::DO_AKISMET) {
+      my $akismet = Net::Akismet->new(KEY => $LJ::AKISMET_KEY,
+                                      URL => 'http://www.openweblog.com/',
+                                     ) or die('Key verification failure!');
+
+      my $verdict = $akismet->spam(USER_IP                 => $ip,
+                                   COMMENT_CONTENT         => $body,
+#                                   REFERRER                => $referer,
+                                  ) or die('Is the server here?');
+
+      if ($verdict) {
+        warn "Akismet accepted spam report: $ip, $subject\n";
+        return LJ::server_down_html();
+      }
+      else {
+        warn "Akismet failed on spam
 report: $ip, $subject\n";
+      }
+    }
+
     # step 3: insert into spamreports
     $dbh->do('INSERT INTO spamreports (reporttime, posttime, ip, journalid, posterid, subject, body) ' .
              'VALUES (UNIX_TIMESTAMP(), UNIX_TIMESTAMP(?), ?, ?, ?, ?, ?)',

--- cvs/livejournal/htdocs/talkpost_do.bml	2007-02-10 13:49:08.543614258 -0500
+++ htdocs/talkpost_do.bml	2007-07-16 11:04:11.060038865 -0400
@@ -31,6 +31,31 @@
 
     return LJ::server_down_html() if $LJ::SERVER_DOWN;
 
+    if($LJ::DO_AKISMET && $POST{'usertype'} eq 'anonymous') { # akismet check
+      my $ip = $ENV{'REMOTE_ADDR'};
+      my $body = $POST{'body'};
+      my $referer = $ENV{'HTTP_REFERER'};
+      my $subject = $POST{subject};
+
+      my $akismet = Net::Akismet->new(KEY => $LJ::AKISMET_KEY,
+                                      URL => 'http://www.openweblog.com/',
+                                     ) or die('Key verification failure!');
+
+      my $verdict = $akismet->check(
+                        USER_IP                 => $ip,
+                        COMMENT_CONTENT         => $body,
+                        REFERRER                => $referer,
+                ) or die('Is the server here?');
+
+      if ('true' eq $verdict) {
+        warn "Akismet reports spam: $ip, $referer, $subject\n";
+        return LJ::server_down_html();
+      }
+      else {
+        warn "Akismet reports no spam: $ip, $referer, $subject\n";
+      }
+    }
+
     LJ::need_res('stc/display_none.css');
 
     # Set the title to be for an error, it will be changed later

Comments