]> git.donarmstrong.com Git - debbugs.git/commitdiff
abstract out spamc_bug from spam_score
authorDon Armstrong <don@donarmstrong.com>
Fri, 15 Dec 2017 21:48:13 +0000 (13:48 -0800)
committerDon Armstrong <don@donarmstrong.com>
Fri, 15 Dec 2017 21:48:13 +0000 (13:48 -0800)
bin/debbugs-spam

index 613eec279f295ea5f8cbf7e70af9c44d10e65e95..51542ce8fc1883c09d8282fae2d1de7d95f4e775 100755 (executable)
@@ -302,10 +302,11 @@ sub spam_score_bug {
     return @records;
 }
 
-sub spam_score {
+sub spamc_bug {
     my ($record,$spamc,$spamc_opts) = @_;
-    my ($score,$threshold,$report);
-    my $is_spam = 0;
+    my $first_line = '';
+    my $report = '';
+    my $exit_code = 0;
     eval {
         my ($spamc_in,$spamc_out);
         my $old_sig = $SIG{"PIPE"};
@@ -314,7 +315,7 @@ sub spam_score {
         };
         my $childpid =
             open3($spamc_in,$spamc_out,0,
-                  $spamc,'-E',@{$spamc_opts}) or
+                  $spamc,@{$spamc_opts}) or
                       die "Unable to fork spamc: $!";
         if (not $childpid) {
             die "Unable to fork spamc";
@@ -323,19 +324,10 @@ sub spam_score {
         close($spamc_in) or die "Unable to close spamc_in: $!";
         waitpid($childpid,0);
         if ($? >> 8) {
-            $is_spam = 1;
-        }
-        my ($first_line,@report) = <$spamc_out>;
-        if ($DEBUG) {
-            print STDERR "[$?;".($? >> 8)."] ";
-            print STDERR $first_line,@report;
-            print STDERR " ";
-        }
-        if (defined $first_line) {
-            chomp $first_line;
-            ($score,$threshold) = $first_line =~ m{^(-?[\d\.]+)/(-?[\d\.]+)$};
-            $report = join('',@report);
+            $exit_code = $? >> 8;
         }
+        local $/;
+        $report = <$spamc_out>;
         close($spamc_out);
         $SIG{"PIPE"} = $old_sig;
     };
@@ -343,7 +335,19 @@ sub spam_score {
         carp "processing of message failed [$@]\n";
         return undef;
     }
-    return wantarray?($score,$is_spam,$report):$score;
+    return ($exit_code,$report);
+}
+
+sub spam_score {
+    my ($record,$spamc,$spamc_opts) = @_;
+    my ($score,$threshold,$report,$exit_code);
+    ($exit_code,$report) =
+        spamc_bug($record,$spamc,[@{$spamc_opts},'-c']);
+    if (defined $report) {
+        ($score,$threshold) = $report =~ s{^(-?[\d\.]+)/(-?[\d\.]+)\n?}{};
+    }
+    return wantarray?($score,$exit_code,$report):$score;
+}
 }
 
 sub foreachmsg {