]> git.donarmstrong.com Git - debbugs.git/blobdiff - bin/debbugs-spam
should be Return-path, not Return-Path
[debbugs.git] / bin / debbugs-spam
index 51542ce8fc1883c09d8282fae2d1de7d95f4e775..5dca08edf01de69653388521c21f3c4aa6e6d785 100755 (executable)
@@ -78,6 +78,10 @@ id
 Mark messages as ham if there is a regex match to subject or message
 id
 
+=item B<learn>
+
+Learn from messages which have been marked as spam/ham
+
 =back
 
 
@@ -134,6 +138,12 @@ my %subcommands =
                     },
      'mark-ham' => {function => \&mark_ham,
                    },
+     'learn' => {function => \&learn,
+                 arguments => {'skip_missing|skip-missing!' => 0,
+                              },
+                 defaults => {skip_missing => 0,
+                             },
+                },
      'help' => {function => sub {pod2usage({verbose => 2});}}
     );
 
@@ -226,6 +236,34 @@ sub score_bug {
     }
 }
 
+sub learn {
+    my ($options,$opts,$config,$argv) = @_;
+
+    for my $bug_num (@{$argv}) {
+        if ($opts->{skip_missing} and
+            not defined getbuglocation($bug_num,'log')) {
+            print STDERR "bug $bug_num does not exist\n" if $options->{verbose} > -1;
+            next;
+        }
+        my $spam = Debbugs::Log::Spam->new(bug_num => $bug_num) or
+            die "Unable to open bug log spam for $bug_num";
+        foreachmsg(sub {
+                       my ($bn,$rec,$mid) = @_;
+                       if ($spam->is_spam($mid)) {
+                           spamc_learn_spam($rec,$options->{spamc},$options->{spamc_opts});
+                           return;
+                       }
+                       if ($spam->is_ham($mid)) {
+                           spamc_learn_ham($rec,$options->{spamc},$options->{spamc_opts});
+                           return;
+                       }
+                   },
+                   $bug_num,
+                  );
+        $spam->save();
+    }
+}
+
 sub auto_spamscan {
     my ($options,$opts,$config,$argv) = @_;
 
@@ -302,6 +340,17 @@ sub spam_score_bug {
     return @records;
 }
 
+sub add_return_path {
+    my ($message) = @_;
+    ## debbugs is kind of odd, and puts "Received:" first, them "From
+    ## ", and doesn't have a Return-Path. Fix that up so spamassassin
+    ## is happy.
+    $message =~
+        s{^(Received: \(at \S+\) by \S+;[^\n]+\n)(From (\S+) [^\n]+\n)}
+        {${2}Return-path: $3\n$1};
+    return $message;
+}
+
 sub spamc_bug {
     my ($record,$spamc,$spamc_opts) = @_;
     my $first_line = '';
@@ -320,7 +369,8 @@ sub spamc_bug {
         if (not $childpid) {
             die "Unable to fork spamc";
         }
-        print {$spamc_in} $record->{text};
+        print STDERR add_return_path($record->{text}) if $DEBUG > 1;
+        print {$spamc_in} add_return_path($record->{text});
         close($spamc_in) or die "Unable to close spamc_in: $!";
         waitpid($childpid,0);
         if ($? >> 8) {
@@ -348,6 +398,22 @@ sub spam_score {
     }
     return wantarray?($score,$exit_code,$report):$score;
 }
+
+sub spamc_learn_ham {
+    spamc_learn('ham',@_);
+}
+
+sub spamc_learn_forget {
+    spamc_learn('forget',@_);
+}
+
+sub spamc_learn_spam {
+    spamc_learn('spam',@_);
+}
+
+sub spamc_learn {
+    my ($type,$record,$spamc,$spamc_opts) = @_;
+    spamc_bug($record,$spamc,[@{$spamc_opts},'-L',$type])
 }
 
 sub foreachmsg {
@@ -356,8 +422,7 @@ sub foreachmsg {
         die "Unable to open bug log for $bug_num";
     my %seen_msgids;
     while (my $record = $log->read_record()) {
-        next if $record->{type} eq 'html';
-        next if $record->{type} eq 'autocheck';
+        next unless $record->{type} eq 'incoming-recv';
         my ($msg_id) = record_regex($record,
                                     qr/^Message-Id:\s+<(.+)>/mi);
         next unless defined $msg_id;