Changeset 95

Show
Ignore:
Timestamp:
04/13/05 12:27:08
Author:
brad
Message:

Updated logging to be more consistent and more informative.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plugins/spamlookup/spamlookup.pl

    r94 r95  
    212212    $data->{__blog} = MT::Blog->load($obj->blog_id); 
    213213    return 0 unless $data->{__blog}; 
    214     $data->{__blogname} = qq{"}.$data->{__blog}->name.qq{" (id }.$obj->blog_id.qq{)}; 
    215214 
    216215    my $pdata; 
     
    260259    my ($result, @errors); 
    261260    foreach (@tests) { 
     261        delete $data->{__reason}; 
    262262        $result = $_->[0]->($eh, $app, $obj, $data); 
    263         if ((defined $eh->errstr) && ($eh->errstr =~ m/\w/)) { 
    264             push @errors, $eh->errstr . ' ('.$_->[1].')'; 
    265             $eh->error(undef); 
     263        if (exists $data->{__reason}) { 
     264            push @errors, log_message( Reason => $data->{__reason}, 
     265                Object => $obj, Test => $_->[1], Result => $result, 
     266                BlogID => $data->{__blog}->id, Blog => $data->{__blog}->name ); 
    266267        } 
    267268        if (defined $result && ($result == ACCEPT)) { 
     
    340341} 
    341342 
     343sub log_message { 
     344    # arguments to this function: 
     345    # Reason: textual reason for block/moderate action 
     346    # Object: the object being acted upon 
     347    # Test: the name of the test that caused the action 
     348    # Result: BLOCK or MODERATE constant 
     349    # BlogID: the blog id for this object 
     350    # Blog: the blog name for this object 
     351 
     352    my %args = @_; 
     353 
     354    my $obj = $args{Object}; 
     355 
     356    $args{Result} = $args{Result} == BLOCK ? "Blocking" : "Moderating"; 
     357    $args{Type} = ref $obj eq 'MT::Comment' ? 'Comment' : 'TrackBack Ping'; 
     358    if (ref $obj eq 'MT::Comment') { 
     359        $args{EntryID} = $obj->entry_id; 
     360    } elsif ($obj->tb_id) { 
     361        my $tb = MT::Trackback->load($obj->tb_id); 
     362        if ($tb->entry_id) { 
     363            $args{EntryID} = $tb->entry_id; 
     364        } else { 
     365            $args{CategoryID} = $tb->category_id; 
     366            if (my $cat = MT::Category->load($args{CategoryID})) { 
     367                $args{Category} = $cat->label; 
     368                $args{Title} = $args{Category}; 
     369            } 
     370        } 
     371    } 
     372    if ($args{EntryID}) { 
     373        if (my $entry = MT::Entry->load($args{EntryID})) { 
     374            $args{Entry} = $entry->title; 
     375            $args{Title} = $args{Entry}; 
     376        } 
     377    } 
     378    $args{ObjType} = $args{EntryID} ? "entry" : "category"; 
     379    $args{ObjID} = $args{EntryID} || $args{CategoryID}; 
     380 
     381    # Blocking Comment for blog "My Blog", entry "Once upon a time": URL count test reported "REASON" 
     382    return sprintf(qq{%s %s for blog "%s" (%d), %s "%s" (%d): %s reported %s}, 
     383        $args{Result}, $args{Type}, $args{Blog}, $args{BlogID}, 
     384        $args{ObjType}, $args{Title}, $args{ObjID}, 
     385        $args{Test}, $args{Reason}); 
     386} 
     387 
    342388## "Check" routines... one for each type of test we perform... 
    343389## Each returns one of three states:  0 to reject, 1 to accept, undef 
     
    358404        my $entry = MT::Entry->load($tb->entry_id); 
    359405        if ($entry && !$entry->allow_pings) { 
    360             $eh->error("Blocking ".$data->{__type}." on blog id ".$obj->blog_id." for entry id ".$tb->entry_id." since it no longer allows pings.")
     406            $data->{__reason} = "Entry no longer accepts TrackBack pings."
    361407            return BLOCK; 
    362408        } 
     
    364410        my $cat = MT::Category->load($tb->category_id); 
    365411        if ($cat && !$cat->allow_pings) { 
    366             $eh->error("Blocking ".$data->{__type}." on blog id ".$obj->blog_id." for category id ".$tb->category_id." since it no longer allows pings.")
     412            $data->{__reason} = "Category no longer accepts TrackBack pings."
    367413            return BLOCK; 
    368414        } 
     
    396442        $match ||= wordlist_match($decodedtext, $data->{wordlist_block}); 
    397443        if ($match) { 
    398             $eh->error("Blocking ".$data->{__type}." on blog ".$data->{__blogname}." based on wordlist match: ".$match)
     444            $data->{__reason} = "Wordlist match: $match"
    399445            return BLOCK; 
    400446        } 
     
    405451        $match ||= wordlist_match($decodedtext, $data->{wordlist_moderate}); 
    406452        if ($match) { 
    407             $eh->error("Moderating ".$data->{__type}." on blog ".$data->{__blogname}." based on wordlist match: ".$match)
     453            $data->{__reason} = "Wordlist match: $match"
    408454            return MODERATE; 
    409455        } 
     
    448494 
    449495    if (!defined $inpass) { 
    450         $eh->error("Blocking TrackBack ping for blog " . $obj->blog_id . " because passphrase was not supplied at all.")
     496        $data->{__reason} = "Passphrase was not supplied."
    451497        return BLOCK; 
    452498    } 
     
    460506    return IGNORE if lc($text) eq lc($inpass); 
    461507 
    462     $eh->error(($data->{tbpass_moderate} ? "Moderating" : "Blocking") . 
    463         " TrackBack ping for blog " . $obj->blog_id . " due to passphrase not matching."); 
     508    $data->{__reason} = sprintf("Passphrase did not match ('%s' and '%s')", lc($text), lc($inpass)); 
    464509    return $data->{tbpass_moderate} ? MODERATE : BLOCK; 
    465510} 
     
    475520    my $inpass = $app->{query}->param($formfield); 
    476521    if (!defined $inpass) { 
    477         $eh->error("Blocking comment for blog " . $obj->blog_id . " because passphrase was not supplied at all.")
     522        $data->{__reason} = "Passphrase was not supplied."
    478523        return BLOCK; 
    479524    } 
     
    487532    return IGNORE if lc($text) eq lc($inpass); 
    488533 
    489     $eh->error(($data->{commentpass_moderate} ? "Moderating" : "Blocking") . 
    490         " comment for blog " . $obj->blog_id . " due to passphrase not matching."); 
     534    $data->{__reason} = sprintf("Passphrase did not match ('%s' and '%s')", lc($text), lc($inpass)); 
    491535    return $data->{commentpass_moderate} ? MODERATE : BLOCK; 
    492536} 
     
    499543    my $domainip = checkdns($domain); 
    500544    if (!$domainip) { 
    501         $eh->error("Blocking TrackBack ping for blog " . 
    502             $obj->blog_id." due to failure to resolve IP address " . 
    503             "for source URL ".$obj->source_url); 
     545        $data->{__reason} = "Failed to resolve IP address for source URL " . $obj->source_url; 
    504546        return BLOCK; 
    505547    } 
     
    525567 
    526568    my $action = ($result == MODERATE ? "Moderating" : "Blocking"); 
    527     $eh->error("$action TrackBack ping for blog " . $obj->blog_id . 
    528         " since domain IP does not match ping IP for source URL " . 
    529         $obj->source_url . "; domain IP: $domainip; ping IP: $pingip"); 
     569    $data->{__reason} = "Domain IP does not match ping IP for source URL " . 
     570        $obj->source_url . "; domain IP: $domainip; ping IP: $pingip"; 
    530571 
    531572    return $result; 
     
    611652        } 
    612653        # yep - this is a good proxy 
    613         $eh->error(($data->{proxycheck_moderate} ? "Moderating" : "Blocking") . 
    614             " $type on blog " . $obj->blog_id . 
    615             " based on dynamic proxy check of $ip:$port"); 
     654        $data->{__reason} = "Found proxy at $ip:$port"; 
    616655        $data->{is_proxy} = 1; 
    617656        return $data->{proxycheck_moderate} ? MODERATE : BLOCK; 
     
    650689        # added by the system that sent the request 
    651690        $data->{__proxy} = $obj->ip; 
    652         $eh->error(($data->{headers_moderate} ? "Moderating" : "Blocking") . 
    653             " $type on blog " . $obj->blog_id . 
    654             " based on X-Forwarded-For header $proxy"); 
     691        $data->{__reason} = "X-Forwarded-For header found: $proxy"; 
    655692        return $data->{headers_moderate} ? MODERATE : BLOCK; 
    656693    } 
     
    664701    my $nurls = $data->{__nurls} || 0; 
    665702    if ($nurls >= $data->{urlcount_block_limit}) { 
    666         $eh->error("Blocking $type on blog "  . $obj->blog_id . 
    667            " because of too many ($nurls) links"); 
     703        $data->{__reason} = "Too many links: $nurls"; 
    668704        return BLOCK; 
    669705    } elsif ($nurls >= $data->{urlcount_moderate_limit}) { 
    670706        # what should we do for trackbacks? 
    671707        if($type eq 'comment') { 
    672             $eh->error("Moderating $type on blog "  . $obj->blog_id . 
    673                " because of too many ($nurls) links"); 
     708            $data->{__reason} = "Too many links: $nurls"; 
    674709            return MODERATE; 
    675710        } 
     
    690725        my $result = $cache->{$obj->ip}->{result}; 
    691726        if ($result != ACCEPT) { 
    692             $eh->error(($result == MODERATE ? "Moderating" : "Blocking") . 
    693                 " $type on blog " . $obj->blog_id . 
    694                 " based on cached proxy information for IP " . $obj->ip . 
    695                 "; hit count is " . $cache->{$obj->ip}->{hits}); 
     727            $data->{__reason} = "Cached proxy record found for IP " . $obj->ip . "; cache hitcount is " . $cache->{$obj->ip}->{hits}; 
    696728            return $result; 
    697729        } 
     
    715747            if (checkdns("$url.$service.")) { 
    716748                $data->{__service} = $service; 
    717                 $eh->error(($data->{domainbl_moderate} ? "Moderating" : "Blocking") . 
    718                     " $type on blog ". $obj->blog_id . 
    719                     " based on domain $url matching on service $service"); 
     749                $data->{__reason} = "domain in $url found on service $service"; 
    720750                return $data->{domainbl_moderate} ? MODERATE : BLOCK; 
    721751            } 
     
    736766        if (checkdns("$d.$c.$b.$a.$service.")) { 
    737767            $data->{__service} = $service; 
    738             $eh->error(($data->{ipbl_moderate} ? "Moderating" : "Blocking") . 
    739                 " $type on blog " . $obj->blog_id . 
    740                 " based on IP $remote_ip matching on service $service"); 
     768            $data->{__reason} = "$remote_ip found on service $service"; 
    741769            return $data->{ipbl_moderate} ? MODERATE : BLOCK; 
    742770        }