~ubuntu-branches/ubuntu/utopic/spamassassin/utopic-proposed

« back to all changes in this revision

Viewing changes to lib/Mail/SpamAssassin/Plugin/Reuse.pm

  • Committer: Bazaar Package Importer
  • Author(s): Noah Meyerhans
  • Date: 2010-01-26 22:53:12 UTC
  • mfrom: (1.1.13 upstream) (5.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100126225312-wkftb10idc1kz2aq
Tags: 3.3.0-1
* New upstream version.
* Switch to dpkg-source 3.0 (quilt) format

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
=head1 NAME
 
2
 
 
3
Mail::SpamAssassin::Plugin::Reuse - For reusing old rule hits during a mass-check
 
4
 
 
5
=head1 SYNOPSIS
 
6
 
 
7
  loadplugin    Mail::SpamAssassin::Plugin::Reuse
 
8
 
 
9
  ifplugin      Mail::SpamAssassin::Plugin::Reuse
 
10
 
 
11
  reuse NETWORK_RULE [ NETWORK_RULE_OLD_NAME ]
 
12
 
 
13
  endif
 
14
 
 
15
=head1 DESCRIPTION
 
16
 
 
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
 
19
mass-check output.
 
20
 
 
21
=cut
 
22
 
 
23
package Mail::SpamAssassin::Plugin::Reuse;
 
24
 
 
25
use bytes;
 
26
use strict;
 
27
use warnings;
 
28
 
 
29
use Mail::SpamAssassin::Conf;
 
30
use Mail::SpamAssassin::Logger;
 
31
 
 
32
use vars qw(@ISA);
 
33
@ISA = qw(Mail::SpamAssassin::Plugin);
 
34
 
 
35
# constructor
 
36
sub new {
 
37
  my $invocant = shift;
 
38
  my $samain = shift;
 
39
 
 
40
  # some boilerplate...
 
41
  my $class = ref($invocant) || $invocant;
 
42
  my $self = $class->SUPER::new($samain);
 
43
  bless ($self, $class);
 
44
 
 
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);
 
49
  return $self;
 
50
}
 
51
 
 
52
sub set_config {
 
53
  my ($self, $conf) = @_;
 
54
  my @cmds = ();
 
55
 
 
56
  # reuse CURRENT_NAME ADDITIONAL_NAMES_IN_INPUT ...
 
57
  # e.g.
 
58
  # reuse NET_TEST_V1 NET_TEST_V0
 
59
 
 
60
  push (@cmds, { setting => 'reuse',
 
61
                 code => sub {
 
62
                   my ($conf, $key, $value, $line) = @_;
 
63
 
 
64
                   if ($value !~ /\s*(\w+)(?:\s+(\w+(?:\s+\w+)*))?\s*$/) {
 
65
                     return $Mail::SpamAssassin::Conf::INVALID_VALUE;
 
66
                   }
 
67
 
 
68
                   my $new_name = $1;
 
69
                   my @old_names = ($new_name);
 
70
                   if ($2) {
 
71
                     push @old_names, split (' ', $2);
 
72
                   }
 
73
 
 
74
                   dbg("reuse: read rule, old: @old_names new: $new_name");
 
75
 
 
76
                   foreach my $old (@old_names) {
 
77
                     push @{$conf->{reuse_tests}->{$new_name}}, $old;
 
78
                   }
 
79
 
 
80
               }});
 
81
 
 
82
 
 
83
  $conf->{parser}->register_commands(\@cmds);
 
84
}
 
85
 
 
86
sub finish_parsing_start {
 
87
  my ($self, $opts) = @_;
 
88
 
 
89
  my $conf = $opts->{conf};
 
90
 
 
91
  dbg("reuse: finish_parsing_start called");
 
92
 
 
93
  return 0 if (!exists $conf->{reuse_tests});
 
94
 
 
95
  foreach my $rule_name (keys %{$conf->{reuse_tests}}) {
 
96
 
 
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);
 
101
    }
 
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;
 
107
      }
 
108
    }
 
109
 
 
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 ];
 
114
 
 
115
  }
 
116
}
 
117
 
 
118
sub check_start {
 
119
  my ($self, $opts) = @_;
 
120
 
 
121
  my $pms = $opts->{permsgstatus};
 
122
 
 
123
  # Can we reuse?
 
124
  my $msg = $pms->get_message();
 
125
 
 
126
  unless (exists $msg->{metadata}->{reuse_tests_hit}) {
 
127
    dbg("reuse: no old test hits passed in");
 
128
    return 0;
 
129
  }
 
130
  my $old_hash = $msg->{metadata}->{reuse_tests_hit};
 
131
 
 
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}}) {
 
135
 
 
136
    dbg("reuse: looking at rule $rule");
 
137
    my ($priority, $stage) = @{$pms->{conf}->{reuse_tests_order}->{$rule}};
 
138
 
 
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;
 
147
      }
 
148
    }
 
149
 
 
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 " .
 
156
            "$stage");
 
157
        last OLD;
 
158
      }
 
159
    }
 
160
  }
 
161
}
 
162
 
 
163
sub check_end {
 
164
  my ($self, $opts) = @_;
 
165
 
 
166
  my $pms = $opts->{permsgstatus};
 
167
 
 
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];
 
173
    }
 
174
  }
 
175
 
 
176
  delete $pms->{reuse_old_scores};
 
177
}
 
178
 
 
179
sub start_rules {
 
180
  my ($self, $opts) = @_;
 
181
 
 
182
  return $self->_add_hits($opts->{permsgstatus}, $opts->{priority},
 
183
                          $opts->{ruletype});
 
184
}
 
185
 
 
186
sub _add_hits {
 
187
  my ($self, $pms, $priority, $stage) = @_;
 
188
 
 
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;
 
195
 
 
196
    dbg("reuse: registering hit for $rule: score: " .
 
197
        $pms->{conf}->{scores}->{$rule});
 
198
    $pms->got_hit($rule);
 
199
 
 
200
    $pms->{conf}->{scores}->{$rule} = 0;
 
201
  }
 
202
}
 
203
 
 
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",
 
217
                    );
 
218
 
 
219
sub _get_stage_from_rule {
 
220
  my  ($self, $conf, $rule) = @_;
 
221
 
 
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};
 
227
    if ($parent) {
 
228
      $type = $conf->{test_types}->{$parent};
 
229
    }
 
230
  }
 
231
  if ($type && exists $type_to_stage{$type}) {
 
232
    return $type_to_stage{$type};
 
233
  }
 
234
  else {
 
235
    # Run before the meta rules run so that they can use these hits as
 
236
    # inputs.
 
237
    return "meta";
 
238
  }
 
239
}
 
240
 
 
241