kareila: (Default)
kareila ([personal profile] kareila) wrote in [site community profile] changelog2011-01-24 04:15 pm

[dw-free] refactor sysban

[commit: http://hg.dwscoalition.org/dw-free/rev/0ec6e01c4fd9]

http://bugs.dwscoalition.org/show_bug.cgi?id=2261

Refactor ban/unban code to reduce duplication of logic.

Patch by [personal profile] kareila.

Files modified:
  • bin/ljsysban.pl
  • cgi-bin/ljlib.pl
  • cgi-bin/sysban.pl
--------------------------------------------------------------------------------
diff -r cd67817249a4 -r 0ec6e01c4fd9 bin/ljsysban.pl
--- a/bin/ljsysban.pl	Mon Jan 24 16:16:14 2011 +0800
+++ b/bin/ljsysban.pl	Mon Jan 24 10:14:41 2011 -0600
@@ -32,7 +32,7 @@ exit 1 unless GetOptions('list' => \$lis
                          );
 
 # did they give valid input?
-my $an_opt = ($what || $value || $status || $bandate || $banuntil || $note);
+my $an_opt = ($what || $value || $status || $bandate || $banuntil || $banlength || $note);
 unless (($list   && (($banid && ! $an_opt) || (! $banid && $an_opt)) ||
          ($add    && $what && $value) ||
          ($modify && $banid && $an_opt))) {
@@ -56,6 +56,7 @@ unless (($list   && (($banid && ! $an_op
 
 # now load in the beast
 require "$ENV{'LJHOME'}/cgi-bin/ljlib.pl";
+require 'sysban.pl';
 my $dbh = LJ::get_db_writer();
 
 # list bans
@@ -115,21 +116,7 @@ if ($add) {
     die $dbh->errstr if $dbh->err;
     my $insertid = $dbh->{'mysql_insertid'};
 
-    if ($what eq 'ip') {
-        LJ::procnotify_add("ban_ip", { 'ip' => $value,
-                                       'exptime' => LJ::mysqldate_to_time($banuntil) });
-        LJ::MemCache::delete("sysban:ip");
-    }
-    if ($what eq 'uniq') {
-        LJ::procnotify_add("ban_uniq", { 'uniq' => $value,
-                                         'exptime' => LJ::mysqldate_to_time($banuntil) });
-        LJ::MemCache::delete("sysban:uniq");
-    }
-    if ( $what eq 'spamreport' ) {
-        LJ::procnotify_add( 'ban_spamreport', { spamreport => $value,
-                                                exptime => LJ::mysqldate_to_time( $banuntil ) } );
-        LJ::MemCache::delete( 'sysban:spamreport' );
-    }
+    LJ::sysban_do( $what, $value, LJ::mysqldate_to_time( $banuntil ) );
 
     # log in statushistory
     LJ::statushistory_add(0, 0, 'sysban_add',
@@ -157,22 +144,9 @@ if ($modify) {
         $banuntil && $banuntil ne $ban->{'banuntil'} || 
         ($status && $status ne $ban->{'status'} && $status eq 'expired')) {
 
-        if ($ban->{'what'} eq 'ip') {
-            LJ::procnotify_add("unban_ip", { 'ip' => $value || $ban->{'value'}});
-            LJ::MemCache::delete("sysban:ip");
-        }
-        
-        if ($ban->{'what'} eq 'uniq') {
-            LJ::procnotify_add("unban_uniq", { 'uniq' => $value || $ban->{'value'} });
-            LJ::MemCache::delete("sysban:uniq");
-        }
+        LJ::sysban_undo( $ban->{what}, $value || $ban->{value} );
+    }
 
-        if ( $ban->{what} eq 'spamreport' ) {
-            LJ::procnotify_add( 'unban_spamreport', { spamreport => $value || $ban->{value} } );
-            LJ::MemCache::delete( 'sysban:spamreport' );
-        }
-    }
-        
     # what - must have a value
     if ($what && $what ne $ban->{'what'}) {
         $ban->{'what'} = $what;
@@ -184,25 +158,10 @@ if ($modify) {
         $banuntil && $banuntil ne $ban->{'banuntil'} || 
         ($status && $status ne $ban->{'status'} && $status eq 'active')) {
 
-        my $new_banuntil = $banuntil || $ban->{'banuntil'};
+        my $new_value    = $value || $ban->{value};
+        my $new_banuntil = LJ::mysqldate_to_time( $banuntil || $ban->{banuntil} );
 
-        if ($ban->{'what'} eq 'ip') {
-            LJ::procnotify_add("ban_ip", { 'ip' => $value || $ban->{'value'},
-                                           'exptime' => LJ::mysqldate_to_time($new_banuntil) });
-            LJ::MemCache::delete("sysban:ip");
-        }
-
-        if ($ban->{'what'} eq 'uniq') {
-            LJ::procnotify_add("ban_uniq", { 'uniq' => $value || $ban->{'value'},
-                                             'exptime' => LJ::mysqldate_to_time($new_banuntil) });
-            LJ::MemCache::delete("sysban:uniq");
-       }
-
-        if ( $ban->{what} eq 'spamreport' ) {
-            LJ::procnotify_add( 'ban_spamreport', { spamreport => $value || $ban->{value},
-                                                    exptime => LJ::mysqldate_to_time( $new_banuntil ) } );
-            LJ::MemCache::delete( 'sysban:spamreport' );
-        }
+        LJ::sysban_do( $ban->{what}, $new_value, $new_banuntil );
     }
 
     # value - must have a value
diff -r cd67817249a4 -r 0ec6e01c4fd9 cgi-bin/ljlib.pl
--- a/cgi-bin/ljlib.pl	Mon Jan 24 16:16:14 2011 +0800
+++ b/cgi-bin/ljlib.pl	Mon Jan 24 10:14:41 2011 -0600
@@ -1752,47 +1752,22 @@ sub procnotify_callback
         return;
     }
 
-    # ip bans
-    if ($cmd eq "ban_ip") {
-        $LJ::IP_BANNED{$arg->{'ip'}} = $arg->{'exptime'};
+    # ip/uniq/spamreport bans
+    my %ban_types = (
+                     ip         => \%LJ::IP_BANNED,
+                     uniq       => \%LJ::UNIQ_BANNED,
+                     spamreport => \%LJ::SPAMREPORT_BANNED,
+                    );
+
+    if ( $cmd =~ /^ban_(\w+)$/ && exists $ban_types{$1} ) {
+        my $banarg = $arg->{$1};
+        $ban_types{$1}->{$banarg} = $arg->{exptime};
         return;
     }
 
-    if ($cmd eq "unban_ip") {
-        delete $LJ::IP_BANNED{$arg->{'ip'}};
-        return;
-    }
-
-    # uniq key bans
-    if ($cmd eq "ban_uniq") {
-        $LJ::UNIQ_BANNED{$arg->{'uniq'}} = $arg->{'exptime'};
-        return;
-    }
-
-    if ($cmd eq "unban_uniq") {
-        delete $LJ::UNIQ_BANNED{$arg->{'uniq'}};
-        return;
-    }
-
-    # spamreport bans
-    if ( $cmd eq "ban_spamreport" ) {
-        $LJ::SPAMREPORT_BANNED{$arg->{spamreport}} = $arg->{exptime};
-        return;
-    }
-
-    if ( $cmd eq "unban_spamreport" ) {
-        $LJ::SPAMREPORT_BANNED{$arg->{spamreport}} = $arg->{exptime};
-        return;
-    }
-
-    # spamreport bans
-    if ( $cmd eq "ban_spamreport" ) {
-        $LJ::SPAMREPORT_BANNED{$arg->{spamreport}} = $arg->{exptime};
-        return;
-    }
-
-    if ( $cmd eq "unban_spamreport" ) {
-        delete $LJ::SPAMREPORT_BANNED{$arg->{spamreport}};
+    if ( $cmd =~ /^unban_(\w+)$/ && exists $ban_types{$1} ) {
+        my $banarg = $arg->{$1};
+        delete $ban_types{$1}->{$banarg};
         return;
     }
 
diff -r cd67817249a4 -r 0ec6e01c4fd9 cgi-bin/sysban.pl
--- a/cgi-bin/sysban.pl	Mon Jan 24 16:16:14 2011 +0800
+++ b/cgi-bin/sysban.pl	Mon Jan 24 10:14:41 2011 -0600
@@ -445,20 +445,7 @@ sub sysban_create {
 
     my $exptime = $opts{bandays} ? time() + 86400*$opts{bandays} : 0;
     # special case: creating ip/uniq/spamreport ban
-    if ($opts{'what'} eq 'ip') {
-        LJ::procnotify_add("ban_ip", { 'ip' => $opts{'value'}, exptime => $exptime });
-        LJ::MemCache::delete("sysban:ip");
-    }
-
-    if ($opts{'what'} eq 'uniq') {
-        LJ::procnotify_add("ban_uniq", { 'uniq' => $opts{'value'}, exptime => $exptime});
-        LJ::MemCache::delete("sysban:uniq");
-    }
-
-    if ( $opts{what} eq 'spamreport' ) {
-        LJ::procnotify_add( 'ban_spamreport', { spamreport => $opts{value}, exptime => $exptime } );
-        LJ::MemCache::delete( 'sysban:spamreport' );
-    }
+    LJ::sysban_do( $opts{what}, $opts{value}, $exptime );
 
     # log in statushistory
     my $remote = LJ::get_remote();
@@ -632,6 +619,31 @@ sub sysban_modify {
 
 }
 
+sub sysban_do {
+    my ( $what, $value, $until ) = @_;
+    my %types = ( ip => 1, uniq => 1, spamreport => 1 );
+    return unless $types{$what};
+
+    my $procopts = { $what => $value, exptime => $until };
+
+    LJ::procnotify_add( "ban_$what", $procopts );
+    LJ::MemCache::delete( "sysban:$what" );
+
+    return 1;
+}
+
+sub sysban_undo {
+    my ( $what, $value ) = @_;
+    my %types = ( ip => 1, uniq => 1, spamreport => 1 );
+    return unless $types{$what};
+
+    my $procopts = { $what => $value };
+
+    LJ::procnotify_add( "unban_$what", $procopts );
+    LJ::MemCache::delete( "sysban:$what" );
+
+    return 1;
+}
 
 
 1;
--------------------------------------------------------------------------------

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org