]> git.donarmstrong.com Git - debbugs.git/commitdiff
add learn subcommand to learn from spam messages
authorDon Armstrong <don@donarmstrong.com>
Fri, 15 Dec 2017 21:49:19 +0000 (13:49 -0800)
committerDon Armstrong <don@donarmstrong.com>
Fri, 15 Dec 2017 21:49:19 +0000 (13:49 -0800)
bin/debbugs-spam

index 51542ce8fc1883c09d8282fae2d1de7d95f4e775..b7ce302e61ed8cb53631753323a17e3fd1f56e29 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,8 @@ my %subcommands =
                     },
      'mark-ham' => {function => \&mark_ham,
                    },
+     'learn' => {function => \&learn,
+                },
      'help' => {function => sub {pod2usage({verbose => 2});}}
     );
 
@@ -226,6 +232,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)) {
+                           spam_learn_spam($rec,$options->{spamc},$options->{spamc_opts});
+                           return;
+                       }
+                       if ($spam->is_ham($mid)) {
+                           spam_learn_ham($rec,$options->{spamc},$options->{spamc_opts});
+                           return;
+                       }
+                   },
+                   $bug_num,
+                  );
+        $spam->save();
+    }
+}
+
 sub auto_spamscan {
     my ($options,$opts,$config,$argv) = @_;
 
@@ -348,6 +382,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 {