| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | package Mail::SpamAssassin::SimpleClient; | ||||||||||||||||||||||||||||
| 5 | { | ||||||||||||||||||||||||||||
| 6 | 1 | $Mail::SpamAssassin::SimpleClient::VERSION = '0.102'; | |||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||
| 8 | # ABSTRACT: easy client to SpamAssassin's spamd | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Email::MIME; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Mail::SpamAssassin::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Mail::SpamAssassin::SimpleClient::Result; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 0 | my ($class, $arg) = @_; | |||||||||||||||||||||||||||
| 18 | 0 | ▶ | $arg ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$arg ||= {}
MC/DC0%0/1
$arg ||= {}
$arg
Truth table0%0/2
A
A= $arg
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | $arg->{host} = 'localhost' unless defined $arg->{host}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | $arg->{port} = 783 unless defined $arg->{port}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | $arg->{timeout} = 120 unless defined $arg->{timeout}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | bless $arg => $class; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 0 | my ($self, $message) = @_; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | local $SIG{ALRM} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | 0 | Carp::croak "SpamAssassin failed to respond within $self->{timeout}s"; | |||||||||||||||||||||||||||
| 33 | 0 | }; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | alarm $self->{timeout}; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Maybe we should keep one spamc and ping each check. Whatever, I'll deal | ||||||||||||||||||||||||||||
| 38 | # with that when it turns out to matter. -- rjbs, 2007-03-21 | ||||||||||||||||||||||||||||
| 39 | my $spamc = Mail::SpamAssassin::Client->new({ | ||||||||||||||||||||||||||||
| 40 | host => $self->{host}, | ||||||||||||||||||||||||||||
| 41 | port => $self->{port}, | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | ($self->{username} ? (username => $self->{username}) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | }); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | my $response = $spamc->process($message->as_string); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | alarm 0; | |||||||||||||||||||||||||||
| 48 | # X-Spam-Flag: YES | ||||||||||||||||||||||||||||
| 49 | # X-Spam-Checker-Version: SpamAssassin 3.1.0 (2005-09-13) on emerald.pobox.com | ||||||||||||||||||||||||||||
| 50 | # X-Spam-Status: Yes, score=1000.8 required=5.0 tests=ENGLISH_UCE_SUBJECT,GTUBE, | ||||||||||||||||||||||||||||
| 51 | # NO_REAL_NAME,NO_RECEIVED,NO_RELAYS autolearn=no version=3.1.0 | ||||||||||||||||||||||||||||
| 52 | # X-Spam-Level: ************************************************** | ||||||||||||||||||||||||||||
| 53 | # X-Spam-Report: * 1.4 NO_DNS_FOR_FROM DNS: Envelope sender has no MX or A DNS records * 0.0 MISSING_MID Missing Message-Id: header * -0.0 NO_RELAYS Informational: message was not relayed via SMTP | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # use Data::Dumper; | ||||||||||||||||||||||||||||
| 56 | # warn Dumper($response); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my $response_email = Email::MIME->new($response->{message}); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # We can't just look for tests=\S because when the tests wrap and are | ||||||||||||||||||||||||||||
| 61 | # unfolded, a space is introduced betwen tests. | ||||||||||||||||||||||||||||
| 62 | 0 | my $status = $response_email->header('X-Spam-Status'); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | $status =~ s/,\s([A-Z])/,$1/g; | |||||||||||||||||||||||||||
| 65 | 0 | my ($tests) = $status =~ /tests=(.+?)(?:\s[a-z]|$)/; | |||||||||||||||||||||||||||
| 66 | 0 | my ($version) = $status =~ /version=(\S+)/; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my @tests = split /,/, $tests; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # isspam will be True or False | ||||||||||||||||||||||||||||
| 71 | # score | ||||||||||||||||||||||||||||
| 72 | # threshold | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my (%test_score, %test_desc); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($response->{isspam} eq 'True') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | # prefer the X-Spam-Report header before checking the body | ||||||||||||||||||||||||||||
| 78 | 0 | my $report = $response_email->header('X-Spam-Report'); | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | if( $report ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | foreach my $report_part (split(/\s*\*\s*/, $report)) { | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | next unless $report_part; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | my ($score, $name, $desc) = $report_part =~ /^(-?[\d.]+)\s+(\S+)\s+(.*)$/; | |||||||||||||||||||||||||||
| 83 | 0 | $test_score{ $name } = $score; | |||||||||||||||||||||||||||
| 84 | 0 | $test_desc{ $name } = $desc; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||||||||||
| 87 | 0 | ($report) = ($response_email->parts)[0]; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my $past_header; | |||||||||||||||||||||||||||
| 90 | 0 | LINE: for my $line (split /\n/, $report->body) { | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | next if not($past_header) and not($line =~ /^\s*---- -/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$past_header or $line =~ /^\s*---- -/
MC/DC0%0/2
$past_header or $line =~ /^\s*---- -/
$past_header
$line =~ /^\s*---- -/
Truth table0%0/3
A or B
A= $past_headerB= $line =~ /^\s*---- -/
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | $past_header = 1, next if not $past_header; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | my ($score, $name) = $line =~ /\s*(-?[\d.]+)\s+(\S+)/; | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | next LINE unless defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $test_score{ $name } = $score; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | return Mail::SpamAssassin::SimpleClient::Result->new({ | ||||||||||||||||||||||||||||
| 102 | is_spam => $response->{score} > $response->{threshold}, | ||||||||||||||||||||||||||||
| 103 | score => $response->{score}, | ||||||||||||||||||||||||||||
| 104 | threshold => $response->{threshold}, | ||||||||||||||||||||||||||||
| 105 | version => $version, | ||||||||||||||||||||||||||||
| 106 | email => $response_email, | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | tests => %test_score | ||||||||||||||||||||||||||||
| 109 | ? \%test_score | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | : { map { $_ => undef } @tests }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | test_desc => %test_desc | ||||||||||||||||||||||||||||
| 112 | ? \%test_desc | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | : { map { $_ => undef } @tests }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | }); | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | 1; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||