3
Mail::SpamAssassin::Plugin::Reuse - For reusing old rule hits during a mass-check
7
loadplugin Mail::SpamAssassin::Plugin::Reuse
9
ifplugin Mail::SpamAssassin::Plugin::Reuse
11
reuse NETWORK_RULE [ NETWORK_RULE_OLD_NAME ]
17
The purpose of this plugin is to work in conjunction with B<mass-check
18
--reuse> to map rules hit in input messages to rule hits in the
23
package Mail::SpamAssassin::Plugin::Reuse;
29
use Mail::SpamAssassin::Conf;
30
use Mail::SpamAssassin::Logger;
33
@ISA = qw(Mail::SpamAssassin::Plugin);
41
my $class = ref($invocant) || $invocant;
42
my $self = $class->SUPER::new($samain);
43
bless ($self, $class);
45
$self->set_config($samain->{conf});
46
# make sure we run last (or close) of the finish_parsing_start since
47
# we need all other rules to be defined
48
$self->register_method_priority("finish_parsing_start", 100);
53
my ($self, $conf) = @_;
56
# reuse CURRENT_NAME ADDITIONAL_NAMES_IN_INPUT ...
58
# reuse NET_TEST_V1 NET_TEST_V0
60
push (@cmds, { setting => 'reuse',
62
my ($conf, $key, $value, $line) = @_;
64
if ($value !~ /\s*(\w+)(?:\s+(\w+(?:\s+\w+)*))?\s*$/) {
65
return $Mail::SpamAssassin::Conf::INVALID_VALUE;
69
my @old_names = ($new_name);
71
push @old_names, split (' ', $2);
74
dbg("reuse: read rule, old: @old_names new: $new_name");
76
foreach my $old (@old_names) {
77
push @{$conf->{reuse_tests}->{$new_name}}, $old;
83
$conf->{parser}->register_commands(\@cmds);
86
sub finish_parsing_start {
87
my ($self, $opts) = @_;
89
my $conf = $opts->{conf};
91
dbg("reuse: finish_parsing_start called");
93
return 0 if (!exists $conf->{reuse_tests});
95
foreach my $rule_name (keys %{$conf->{reuse_tests}}) {
97
# If the rule does not exist, add a new EMPTY test, set default score
98
if (!exists $conf->{tests}->{$rule_name}) {
99
dbg("reuse: $rule_name does not exist, adding empty test");
100
$conf->{parser}->add_test($rule_name, undef, $Mail::SpamAssassin::Conf::TYPE_EMPTY_TESTS);
102
if (!exists $conf->{scores}->{$rule_name}) {
103
my $set_score = ($rule_name =~/^T_/) ? 0.01 : 1.0;
104
$set_score = -$set_score if ( ($conf->{tflags}->{$rule_name}||'') =~ /\bnice\b/ );
105
foreach my $ss (0..3) {
106
$conf->{scoreset}->[$ss]->{$rule_name} = $set_score;
110
# Figure out when to add any hits -- grab priority and "stage"
111
my $priority = $conf->{priority}->{$rule_name} || 0;
112
my $stage = $self->_get_stage_from_rule($opts->{conf}, $rule_name);
113
$conf->{reuse_tests_order}->{$rule_name} = [ $priority, $stage ];
119
my ($self, $opts) = @_;
121
my $pms = $opts->{permsgstatus};
124
my $msg = $pms->get_message();
126
unless (exists $msg->{metadata}->{reuse_tests_hit}) {
127
dbg("reuse: no old test hits passed in");
130
my $old_hash = $msg->{metadata}->{reuse_tests_hit};
132
# now go through the rules and priorities and figure out which ones
133
# need to be disabled
134
foreach my $rule (keys %{$pms->{conf}->{reuse_tests}}) {
136
dbg("reuse: looking at rule $rule");
137
my ($priority, $stage) = @{$pms->{conf}->{reuse_tests_order}->{$rule}};
139
# score set could change after check_start but before we add hits,
140
# so we need to disable the rule in all sets
141
foreach my $ss (0..3) {
142
if (exists $pms->{conf}->{scoreset}->[$ss]->{$rule}) {
143
dbg("reuse: disabling rule $rule in score set $ss");
144
$pms->{reuse_old_scores}->{$rule}->[$ss] =
145
$pms->{conf}->{scoreset}->[$ss]->{$rule};
146
$pms->{conf}->{scoreset}->[$ss]->{$rule} = 0;
150
# now, check for hits
151
OLD: foreach my $old_test (@{$pms->{conf}->{reuse_tests}->{$rule}}) {
152
dbg("reuse: looking for rule $old_test");
153
if ($old_hash->{$old_test}) {
154
push @{$pms->{reuse_hits_to_add}->{"$priority $stage"}}, $rule;
155
dbg("reuse: rule $rule hit, will add at priority $priority, stage " .
164
my ($self, $opts) = @_;
166
my $pms = $opts->{permsgstatus};
168
foreach my $disabled_rule (keys %{$pms->{reuse_old_scores}}) {
169
foreach my $ss (0..3) {
170
next unless exists $pms->{conf}->{scoreset}->[$ss]->{$disabled_rule};
171
$pms->{conf}->{scoreset}->[$ss]->{$disabled_rule} =
172
$pms->{reuse_old_scores}->{$disabled_rule}->[$ss];
176
delete $pms->{reuse_old_scores};
180
my ($self, $opts) = @_;
182
return $self->_add_hits($opts->{permsgstatus}, $opts->{priority},
187
my ($self, $pms, $priority, $stage) = @_;
189
return unless exists $pms->{reuse_hits_to_add}->{"$priority $stage"};
190
foreach my $rule (@{$pms->{reuse_hits_to_add}->{"$priority $stage"}}) {
191
# Add hit even if rule was originally disabled
192
my $ss = $pms->{conf}->get_score_set();
193
$pms->{conf}->{scores}->{$rule} =
194
$pms->{reuse_old_scores}->{$rule}->[$ss] || 0.001;
196
dbg("reuse: registering hit for $rule: score: " .
197
$pms->{conf}->{scores}->{$rule});
198
$pms->got_hit($rule);
200
$pms->{conf}->{scores}->{$rule} = 0;
204
my %type_to_stage = (
205
$Mail::SpamAssassin::Conf::TYPE_HEAD_TESTS => "head",
206
$Mail::SpamAssassin::Conf::TYPE_HEAD_EVALS => "eval",
207
$Mail::SpamAssassin::Conf::TYPE_BODY_TESTS => "body",
208
$Mail::SpamAssassin::Conf::TYPE_BODY_EVALS => "eval",
209
$Mail::SpamAssassin::Conf::TYPE_FULL_TESTS => "full",
210
$Mail::SpamAssassin::Conf::TYPE_FULL_EVALS => "eval",
211
$Mail::SpamAssassin::Conf::TYPE_RAWBODY_TESTS => "rawbody",
212
$Mail::SpamAssassin::Conf::TYPE_RAWBODY_EVALS => "eval",
213
$Mail::SpamAssassin::Conf::TYPE_URI_TESTS => "uri",
214
$Mail::SpamAssassin::Conf::TYPE_URI_EVALS => "eval",
215
$Mail::SpamAssassin::Conf::TYPE_META_TESTS => "meta",
216
$Mail::SpamAssassin::Conf::TYPE_RBL_EVALS => "eval",
219
sub _get_stage_from_rule {
220
my ($self, $conf, $rule) = @_;
222
my $type = $conf->{test_types}->{$rule};
223
if ($type && $type == $Mail::SpamAssassin::Conf::TYPE_EMPTY_TESTS) {
224
# this is a "fake" rule... see if the rule "text"/"definition" is
225
# the name of the "parent" rule"
226
my $parent = $conf->{tests}->{$rule};
228
$type = $conf->{test_types}->{$parent};
231
if ($type && exists $type_to_stage{$type}) {
232
return $type_to_stage{$type};
235
# Run before the meta rules run so that they can use these hits as