SpamMonkey.pm

filter: Statementstmt 15.9% 21 / 132 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 29.1% 7 / 24 Podpod 12.5% 2 / 16 totaltotal 12.8% 30 / 234 CC 35 SCAR 40.3
CC
35
Cov
11%
test_headers
182.0
ready
52.3
test_bodylike
30.0
CRAP 56.0
↑ Summary SpamMonkey/Test/check_uridnsbl.pm »
1 package SpamMonkey;
2 1 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use File::Path::Expand qw(expand_filename);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use URI::Find::Schemeless;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our $VERSION = '0.03';
9  
10 =head1 NAME
11  
12 SpamMonkey - Like SpamAssassin, only not.
13  
14 =head1 SYNOPSIS
15  
16 use SpamMonkey;
17 my $monkey = SpamMonkey->new();
18 $monkey->ready;
19  
20 for (@things) {
21 my $result = $monkey->test($_);
22 if ($result->is_spam) { $result->rewrite }
23 }
24  
25 =head1 DESCRIPTION
26  
27 SpamMonkey is a general purpose spam detection suite. It borrows heavily
28 from SpamAssassin, but it is designed to be used for plain text as well
29 as email.
30  
31 =cut
32  
33 0 ▶sub rulesets { qw(body full rawbody uri header); }
Subroutine rulesets: not called CC=1 SCAR=6.9
Pod undocumented
34  
35 0 ▶sub default_rule_dir { "/etc/mail/spamassassin/" }
Subroutine default_rule_dir: not called CC=1 SCAR=6.9
Pod undocumented
36  
37 =head1 CONSTRUCTOR
38  
39 SpamMonkey->new(
40 rule_dir => "/etc/mail/spamassassin/"
41 );
42  
43 SpamMonkey by default loads up rules from F</etc/mail/spamassassin>
44 and then F<~/.spammonkey/user_prefs>. To override the rule directory,
45 specify C<rule_dir> in the constructor.
46  
47 =cut
48  
49 0 ▶sub new { my $class = shift; bless { @_ }, $class; }
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
50  
51 0 ▶sub config_class { "SpamMonkey::Config" }
Subroutine config_class: not called CC=1 SCAR=6.9
Pod undocumented
52 0 ▶sub result_class { "SpamMonkey::Result" } # Subclassers like these
Subroutine result_class: not called CC=1 SCAR=6.9
Pod undocumented
53  
54 =head1 METHODS
55  
56 =head2 ready
57  
58 This loads up the ruleset and then prunes out rules which have no score
59 attached to them. You must call C<ready> before doing a test, else
60 you'll have no rules to test with.
61  
62 =cut
63  
64 0 ▶sub ready {
Subroutine ready: not called CC=8 SCAR=39.6
Pod documented
65 0 my $self = shift;
66 0 $self->config_class->require;
67 0 $self->{conf} = $self->config_class->new();
68  
69 # Read rules
70 0 ▶ for (glob(($self->{rule_dir} || $self->default_rule_dir)."/*.cf")) {
Subroutine BEGIN: called CC=1 SCAR=1.7
Condition0%0/3
$self->{'rule_dir'} || $self->default_rule_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'rule_dir'} || $self->default_rule_dir
$self->{'rule_dir'} $self->default_rule_dir
Truth table0%0/3
A || B
A= $self->{'rule_dir'}B= $self->default_rule_dir
ABresult
1X1
011
000
71 0 $self->{conf}->read($_);
72 }
73  
74 0 my $file = expand_filename("~/.spammonkey/user_prefs");
75 0 ▶ if (-e $file) { $self->{conf}->read($file); }
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
76  
77 # Delete rules with no score(!)
78 0 for ($self->rulesets) {
79 0 my $set = $self->{conf}{rules}{$_};
80 0 for (keys %$set) {
81 0 ▶ if (!exists $self->{conf}{score}{$_}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"conf"}{"score"}{$_} and $self->{"conf"}{"score"}{$_}[0]) 0 0 0
Condition0%0/3
exists $self->{"conf"}{"score"}{$_} and $self->{"conf"}{"score"}{$_}[0]
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"conf"}{"score"}{$_} and $self->{"conf"}{"score"}{$_}[0]
exists $self->{"conf"}{"score"}{$_} $self->{"conf"}{"score"}{$_}[0]
Truth table0%0/3
A and B
A= exists $self->{"conf"}{"score"}{$_}B= $self->{"conf"}{"score"}{$_}[0]
ABresult
0X0
100
111
82 or !$self->{conf}{score}{$_}[0]) {
83 0 delete $set->{$_};# warn "Killing boring rule $_";
84 }
85 }
86 }
87 }
88  
89 =head2 test
90  
91 $self->test(Email::MIME $mime);
92 $self->test($text);
93  
94 This tests an email or a piece of text using the ruleset loaded by
95 C<ready> and returns a C<SpamMonkey::Result> object.
96  
97 =cut
98  
99 0 ▶sub test {
Subroutine test: not called CC=3 SCAR=24.8
Pod documented
100 0 my ($self, $text) = @_;
101 0 $self->{result} = {};
102 0 $self->{per_message} = {};
103 0 ▶ if (not ref $text) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $text) { } 0 0 0
elsif (UNIVERSAL::isa($text, 'Email::MIME')) { } 0 0 0
104 0 $self->{text} = $text;
105 0 $self->test_text()
106 } elsif (UNIVERSAL::isa($text, "Email::MIME")) {
107 0 $self->{email} = $text;
108 0 $self->test_email();
109 }
110 0 $self->result_class->require;
111 0 bless $self->{result}, $self->result_class;
112 0 delete $self->{per_message}; # Ready it to go again
113 # Invert
114 0 $self->{result}{monkey} = $self;
115 0 delete $self->{result};
116 }
117  
118 0 ▶sub test_text {
Subroutine test_text: not called CC=1 SCAR=6.9
Pod undocumented
119 0 my ($self) = @_;
120 0 my $text_r = \do{$self->{text}};
121 0 $self->test_bodylike("rawbody",$text_r,0);
122 # Munge text here
123 0 $self->test_bodylike("body",$text_r,0);
124 0 $self->test_uris($text_r,0);
125 }
126  
127 0 ▶sub test_email {
Subroutine test_email: not called CC=2 SCAR=17.9
Pod undocumented
128 0 my ($self) = @_;
129 0 $self->test_headers($self->{email}, 0);
130 0 $self->{text} = $self->{email}->body_raw;
131 0 $self->test_bodylike("rawbody",\do{$self->{text}},0);
132 0 my $body_r = \do{$self->{email}->body};
133 0 ▶ if ($$body_r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$body_r) { } 0 0 0
134 0 $self->test_bodylike("body",$body_r, 0);
135 } else {
136 0 $self->test_bodylike("body",\do{$self->{text}}, 0);
137 }
138 0 $self->test_uris(\do{$self->{text}}, 0);
139 }
140  
141 0 ▶sub test_headers {
Subroutine test_headers: not called CC=13 SCAR=52.0
Pod undocumented
142 0 my ($self, $email, $scoretype) = @_;
143 0 my $set = $self->{conf}{rules}{header};
144 0 for my $test (keys %$set) {
145 0 my $rule = $set->{$test};
146 0 ▶ if ($rule->{op} eq "eval") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"op"} eq "eval") 0 0 0
147 $self->match($test, $scoretype)
148 0 for $self->do_code_rule($rule); # Maybe more than one
149 next
150 0 }
151 0 my $text = join "\n", $email->header($rule->{header});
152 0 ▶ if ($rule->{header} eq "ALL") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"header"} eq "ALL") 0 0 0
153 0 $text = $email->_headers_as_string; # Urgh
154 }
155  
156 0 ▶ if ($rule->{unset} and not $text) { $text = $rule->{unset} }
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"unset"} and not $text) 0 0 0
Condition0%0/3
$rule->{"unset"} and not $text
!ll&&!rl&&r
000
MC/DC0%0/2
$rule->{"unset"} and not $text
$rule->{"unset"} not $text
Truth table0%0/3
A and not(B)
A= $rule->{"unset"}B= not $text
ABresult
0X0
100
111
157 #warn "$rule->{header}: $text $rule->{op} $rule->{re}";
158 0 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 0 0
159 0 ▶ if ($rule->{op} eq "exists") {
Branch: 2 branches
BranchTrueFalseTotal
if ($rule->{'op'} eq 'exists') { } 0 0 0
elsif ($rule->{'op'} eq '!~') { } 0 0 0
160 0 ▶ $self->match($test, $scoretype) if $text;
Branch: 1 branch
BranchTrueFalseTotal
if $text 0 0 0
161 } elsif ($rule->{op} eq "!~") {
162 0 ▶ if($text !~ $rule->{re}) { $self->match($test, $scoretype); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($text =~ /$rule->{"re"}/) 0 0 0
163 } else {
164 0 ▶ if($text =~ $rule->{re}) { $self->match($test, $scoretype); }
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$rule->{"re"}/) 0 0 0
165 }
166 }
167 }
168  
169 0 ▶sub test_bodylike {
Subroutine test_bodylike: not called CC=5 SCAR=34.0
Pod undocumented
170 0 my ($self, $ruleset, $text, $scoretype) = @_;
171 0 my $set = $self->{conf}{rules}{$ruleset};
172 0 for my $test (keys %$set) {
173 0 my $rule = $set->{$test};
174 0 ▶ if (ref $rule eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule eq 'HASH') { } 0 0 0
175 $self->match($test, $scoretype)
176 0 for $self->do_code_rule($rule, $text);
177 } else {
178 0 ▶ if($$text =~ $rule) { $self->match($test, $scoretype); }
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /$rule/) 0 0 0
179 }
180 }
181 }
182  
183 0 ▶sub get_uris {
Subroutine get_uris: not called CC=2 SCAR=17.9
Pod undocumented
184 0 my ($self, $text_r) = @_;
185 0 ▶ return if $self->{per_message}{uris};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"per_message"}{"uris"} 0 0 0
186 0 $self->{per_message}{uris}= [];
187 0 ▶ my $finder = URI::Find::Schemeless->new( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 0 push @{$self->{per_message}{uris}}, $_[0];
189 0 });
190 0 $finder->find($text_r);
191 }
192  
193 0 ▶sub uris {
Subroutine uris: not called CC=1 SCAR=6.9
Pod undocumented
194 0 my $self = shift;
195 0 $self->get_uris($self->{text});
196 0 return @{$self->{per_message}{uris}};
197 }
198  
199 0 ▶sub test_uris {
Subroutine test_uris: not called CC=4 SCAR=30.0
Pod undocumented
200 0 my ($self, $text_r, $scoretype) = @_;
201 0 my $set = $self->{conf}{rules}{uri};
202 0 $self->get_uris($text_r);
203 0 for my $uri (@{$self->{per_message}{uris}}) {
204 0 for my $test (keys %$set) {
205 0 my $rule = $set->{$test};
206 0 ▶ if ($uri =~ $rule) { $self->match($test, $scoretype) };
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ /$rule/) 0 0 0
207 }
208 }
209 }
210  
211 0 ▶sub match {
Subroutine match: not called CC=1 SCAR=6.9
Pod undocumented
212 0 my ($self, $test, $scoretype) = @_;
213 0 push @{$self->{result}{matched}}, $test;
214 0 $self->{result}{score} += $self->{conf}{score}{$test}[$scoretype];
215 }
216  
217 0 ▶sub do_code_rule {
Subroutine do_code_rule: not called CC=5 SCAR=34.0
Pod undocumented
218 0 my ($self, $rule,$text_r) = @_;
219 0 ▶ my ($pack, $args) = $rule->{code} =~ /(\S+)\((.*)\)$/ or die("Urgh? $rule->{code}");
Branch: 1 branch
BranchTrueFalseTotal
() = $rule->{"code"} =~ /(\S+)\((.*)\)$/ or die qq[Urgh? $rule->{"code"}] 0 0 0
220 0 my @args = ($args =~ m/['"](.*?)['"]\s*(?:,\s*|$)/g);
221 0 $pack = "SpamMonkey::Test::".$pack;
222 0 ▶ $pack->require or return;
Branch: 1 branch
BranchTrueFalseTotal
$pack->require or return 0 0 0
223 0 ▶ if (!$self->{init}{$pack} and $pack->can("init")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"init"}{$pack} and $pack->can("init")) 0 0 0
Condition0%0/3
not $self->{"init"}{$pack} and $pack->can("init")
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"init"}{$pack} and $pack->can("init")
not $self->{"init"}{$pack} $pack->can("init")
Truth table0%0/3
not(A) and B
A= not $self->{"init"}{$pack}B= $pack->can("init")
ABresult
0X0
100
111
224 0 $pack->init($self->{conf});
225 0 $self->{init}{$pack}++;
226 }
227 0 $pack->test($self, $text_r, @args);
228 }
229  
230 =head1 AUTHOR
231  
232 simon, E<lt>simon@E<gt> (please don't contact me about this module,
233 unless you wish to take over its maintainance, in which case upload your
234 own version.)
235  
236 =head1 COPYRIGHT AND LICENSE
237  
238 Copyright (C) 2005 by simon
239  
240 This library is free software; you can redistribute it and/or modify
241 it under the same terms as Perl itself, either Perl version 5.8.7 or,
242 at your option, any later version of Perl 5 you may have available.
243  
244  
245 =cut
↑ Summary SpamMonkey/Test/check_uridnsbl.pm »