File Coverage

blib/lib/Mail/SpamAssassin/Bayes.pm
Criterion Covered Total %
statement 63 67 94.0
branch 12 26 46.1
condition 2 6 33.3
subroutine 15 16 93.7
pod 0 9 0.0
total 92 124 74.1


line stmt bran cond sub pod time code
1             # <@LICENSE>
2             # Licensed to the Apache Software Foundation (ASF) under one or more
3             # contributor license agreements. See the NOTICE file distributed with
4             # this work for additional information regarding copyright ownership.
5             # The ASF licenses this file to you under the Apache License, Version 2.0
6             # (the "License"); you may not use this file except in compliance with
7             # the License. You may obtain a copy of the License at:
8             #
9             # http://www.apache.org/licenses/LICENSE-2.0
10             #
11             # Unless required by applicable law or agreed to in writing, software
12             # distributed under the License is distributed on an "AS IS" BASIS,
13             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14             # See the License for the specific language governing permissions and
15             # limitations under the License.
16             # </@LICENSE>
17              
18             =head1 NAME
19              
20             Mail::SpamAssassin::Bayes - support for learning classifiers
21              
22             =head1 DESCRIPTION
23              
24             This is the general class used to train a learning classifier with new samples
25             of spam and ham mail, and classify based on prior training.
26              
27             Prior to version 3.3.0, the default Bayes implementation was here; if you're
28             looking for information on that, it has moved to
29             C<Mail::SpamAssassin::Plugin::Bayes>.
30              
31             =cut
32              
33              
34             use strict;
35 22     22   158 use warnings;
  22         47  
  22         708  
36 22     22   111 # use bytes;
  22         42  
  22         774  
37             use re 'taint';
38 22     22   115  
  22         47  
  22         775  
39             use Mail::SpamAssassin;
40 22     22   112 use Mail::SpamAssassin::PerMsgStatus;
  22         57  
  22         444  
41 22     22   101 use Mail::SpamAssassin::Logger;
  22         43  
  22         389  
42 22     22   90 use Mail::SpamAssassin::Util qw(untaint_var);
  22         44  
  22         1562  
43 22     22   114  
  22         45  
  22         11427  
44             our @ISA = qw();
45              
46             ###########################################################################
47              
48             my $class = shift;
49             $class = ref($class) || $class;
50 64     64 0 177  
51 64   33     336 my ($main) = @_;
52             my $self = {
53 64         150 'main' => $main,
54             'conf' => $main->{conf},
55             'use_ignores' => 1,
56             };
57 64         296 bless ($self, $class);
58              
59 64         202 $self->{main}->call_plugins("learner_new");
60             $self;
61 64         346 }
62 64         316  
63             ###########################################################################
64              
65             my $self = shift;
66             # we don't need to do the plugin; Mail::SpamAssassin::finish() does
67             # that for us
68 40     40 0 85 %{$self} = ();
69             }
70              
71 40         73 ###########################################################################
  40         120  
72              
73             # force the Bayes dbs to be closed, if they haven't already been; called
74             # at the end of scan operation, or when switching between user IDs,
75             # or when C<Mail::SpamAssassin::finish_learner()> is called.
76             #
77             my $self = shift;
78             my $quiet = shift;
79             $self->{main}->call_plugins("learner_close", { quiet => $quiet });
80             }
81 6     6 0 14  
82 6         9 ###########################################################################
83 6         21  
84             my ($self,$PMS) = @_;
85              
86             return 0 unless $self->{use_ignores};
87              
88             my $ig_from = $self->{main}->call_plugins ("check_wb_list",
89 14     14 0 60 { permsgstatus => $PMS, type => 'from', list => 'bayes_ignore_from' });
90             my $ig_to = $self->{main}->call_plugins ("check_wb_list",
91 14 50       52 { permsgstatus => $PMS, type => 'to', list => 'bayes_ignore_to' });
92              
93 14         141 my $ignore = $ig_from || $ig_to;
94             dbg("bayes: not using bayes, bayes_ignore_from or _to rule") if $ignore;
95 14         141 return $ignore;
96             }
97              
98 14   33     94 ###########################################################################
99 14 50       54  
100 14         50 my ($self, $isspam, $msg, $id) = @_;
101             return unless $self->{conf}->{use_learner};
102             return unless defined $msg;
103              
104             if( $self->{use_ignores} ) # Remove test when PerMsgStatus available.
105             {
106 10     10 0 2015 # DMK, koppel@ece.lsu.edu: Hoping that the ultimate fix to bug 2263 will
107 10 50       64 # make it unnecessary to construct a PerMsgStatus here.
108 10 50       37 my $PMS = new Mail::SpamAssassin::PerMsgStatus $self->{main}, $msg;
109             my $ignore = $self->ignore_message($PMS);
110 10 50       43 $PMS->finish();
111             return 0 if $ignore;
112             }
113              
114 10         107 return $self->{main}->call_plugins("learn_message", { isspam => $isspam, msg => $msg, id => $id });
115 10         65 }
116 10         72  
117 10 50       83 ###########################################################################
118              
119             my ($self, $msg, $id) = @_;
120 10         86 return unless $self->{conf}->{use_learner};
121             return unless defined $msg;
122             return $self->{main}->call_plugins("forget_message", { msg => $msg, id => $id });
123             }
124              
125             ###########################################################################
126 4     4 0 17  
127 4 50       24 my ($self, $sync, $expire, $opts) = @_;
128 4 50       14 return 0 unless $self->{conf}->{use_learner};
129 4         36  
130             if ($sync) {
131             $self->{main}->call_plugins("learner_sync", $opts );
132             }
133             if ($expire) {
134             $self->{main}->call_plugins("learner_expire_old_training", $opts );
135 2     2 0 12 }
136 2 50       13  
137             return 0;
138 2 50       8 }
139 2         17  
140             ###########################################################################
141 2 50       8  
142 0         0 my $self = shift;
143             return 0 unless $self->{conf}->{use_learner};
144             return $self->{main}->call_plugins("learner_is_scan_available");
145 2         9 }
146              
147             ###########################################################################
148              
149             my($self, $magic, $toks, $regex) = @_;
150             return 0 unless $self->{conf}->{use_learner};
151 144     144 0 297 return $self->{main}->call_plugins("learner_dump_database", {
152 144 50       462 magic => $magic, toks => $toks, regex => $regex });
153 144         442 }
154              
155             1;