Mail/Qmail/Filter/CheckDeliverability.pm

filter: Statementstmt 24.0% 13 / 54 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 60.0% 3 / 5 Podpod 100% 1 / 1 totaltotal 13.8% 17 / 123 SCAR 60.3
CC
24
Cov
12%
filter
552.0
__ANON__
6.0
BEGIN
1.0
CRAP 416.0
↑ Summary Mail/Qmail/Filter/SkipQueue.pm »
1 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Mail::Qmail::Filter::CheckDeliverability;
5  
6 1 our $VERSION = '1.01';
7  
8 1 ▶use Mo qw(coerce default);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 extends 'Mail::Qmail::Filter';
10  
11 1 has 'reject_text' => 'Recipient unknown.';
12 1 has 'run_commands_matching';
13  
14 sub filter {
15 0 ▶ my $self = shift;
Subroutine filter: not called CC=23 SCAR=63.1
Pod documented
16 0 my $message = $self->message;
17 0 my $run_commands_matching = $self->run_commands_matching;
18  
19 0 ▶ require Qmail::Deliverable and Qmail::Deliverable->import('dot_qmail')
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Mail::Qmail::Filter::CheckDeliverability::dot_qmail 0 0 0
Condition0%0/6
defined &Mail::Qmail::Filter::CheckDeliverability::dot_qmail || require Qmail::Deliverable && 'Qmail::Deliverable'->import('dot_qmail')
l!l&&r!l&&!r
000
require Qmail::Deliverable and "Qmail::Deliverable"->import("dot_qmail")
!ll&&!rl&&r
000
MC/DC0%0/3
defined &Mail::Qmail::Filter::CheckDeliverability::dot_qmail || require Qmail::Deliverable && 'Qmail::Deliverable'->import('dot_qmail')
defined &Mail::Qmail::Filter::CheckDeliverability::dot_qmail require Qmail::Deliverable "Qmail::Deliverable"->import("dot_qmail")
Truth table0%0/4
A || B and C
A= defined &Mail::Qmail::Filter::CheckDeliverability::dot_qmailB= require Qmail::DeliverableC= "Qmail::Deliverable"->import("dot_qmail")
ABCresult
1XX1
0111
00X0
0100
20 unless defined &dot_qmail;
21  
22 0 my ( %done, $reject_text, $valid );
23 0 Recipient: for ( $message->to ) {
24 0 my $dot_qmail = dot_qmail($_);
25 0 ▶ unless ( defined $dot_qmail ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dot_qmail) 0 0 0
26 0 $self->debug( 'No .qmail file found for rcpt' => $_ );
27 0 $reject_text = $self->reject_text;
28 0 next;
29 }
30 0 ▶ next if $done{$dot_qmail}++;
Branch: 1 branch
BranchTrueFalseTotal
if $done{$dot_qmail}++ 0 0 0
31 0 ▶ open my $fh, '<', $dot_qmail
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $dot_qmail or return $self->debug("Cannot read $dot_qmail", $!) 0 0 0
32 or return $self->debug( "Cannot read $dot_qmail", $! );
33 0 $self->debug( "Checking .qmail for $_" => $dot_qmail );
34 0 ▶ while ( defined( my $line = <$fh> ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
35  
36 0 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/u 0 0 0
37 0 ++$valid;
38 0 chomp $line;
39 0 ▶ return $self->debug( "$_ is at least deliverable to" => $line )
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ m[^[&/\w]]u 0 0 0
40 if $line =~ m{^[&/\w]};
41 next
42 0 ▶ unless $line =~ /^\|/
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^\|/u and defined $run_commands_matching and $line =~ /$run_commands_matching/u 0 0 0
Condition0%0/6
$line =~ /^\|/u and defined $run_commands_matching and $line =~ /$run_commands_matching/u
!ll&&!rl&&r
000
$line =~ /^\|/u and defined $run_commands_matching
!ll&&!rl&&r
000
MC/DC0%0/3
$line =~ /^\|/u and defined $run_commands_matching and $line =~ /$run_commands_matching/u
$line =~ /^\|/u defined $run_commands_matching $line =~ /$run_commands_matching/u
Truth table0%0/4
A and B and C
A= $line =~ /^\|/uB= defined $run_commands_matchingC= $line =~ /$run_commands_matching/u
ABCresult
0XX0
10X0
1100
1111
43 && defined $run_commands_matching
44 && $line =~ $run_commands_matching;
45  
46 require Capture::Tiny
47 0 ▶ and Capture::Tiny->import('capture_merged')
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Mail::Qmail::Filter::CheckDeliverability::capture_merged 0 0 0
Condition0%0/6
defined &Mail::Qmail::Filter::CheckDeliverability::capture_merged || require Capture::Tiny && 'Capture::Tiny'->import('capture_merged')
l!l&&r!l&&!r
000
require Capture::Tiny and "Capture::Tiny"->import("capture_merged")
!ll&&!rl&&r
000
MC/DC0%0/3
defined &Mail::Qmail::Filter::CheckDeliverability::capture_merged || require Capture::Tiny && 'Capture::Tiny'->import('capture_merged')
defined &Mail::Qmail::Filter::CheckDeliverability::capture_merged require Capture::Tiny "Capture::Tiny"->import("capture_merged")
Truth table0%0/4
A || B and C
A= defined &Mail::Qmail::Filter::CheckDeliverability::capture_mergedB= require Capture::TinyC= "Capture::Tiny"->import("capture_merged")
ABCresult
1XX1
0111
00X0
0100
48 unless defined &capture_merged;
49 0 local $ENV{SENDER} = $message->from;
50 my ( $output, $exitcode ) = capture_merged(
51 sub {
52 0 ▶ open my $fh, $line
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $line or return $self->debug("Cannot start $line", $!) 0 0 0
53 or return $self->debug( "Cannot start $line", $! );
54 0 print $fh $message->body;
55 0 close $fh;
56 0 $?;
57 }
58 0 );
59 0 $output = join '/', split /\n/, $output;
60 0 $exitcode >>= 8;
61 0 $self->debug( qq("$line") => $exitcode );
62 0 ▶ last if $exitcode == 99;
Branch: 1 branch
BranchTrueFalseTotal
if $exitcode == 99 0 0 0
63 0 ▶ return $self->debug("Calling $line for $_ resulted in soft failure")
Branch: 1 branch
BranchTrueFalseTotal
if $exitcode == 111 0 0 0
64 if $exitcode == 111;
65 0 ▶ next unless $exitcode == 100;
Branch: 1 branch
BranchTrueFalseTotal
unless $exitcode == 100 0 0 0
66 0 ▶ unless ( defined $reject_text ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $reject_text) { } 0 0 0
(do { $output ne $reject_text }) 0 0 0
67 0 $reject_text = $output;
68 }
69 0 elsif ( $output ne $reject_text ) {
70 0 return $self->debug(
71 qq(Different reject texts: "$reject_text" vs. "$output"));
72 }
73 0 next Recipient;
74 }
75 0 ▶ return $self->debug("might be deliverable to <$_>") if $valid;
Branch: 1 branch
BranchTrueFalseTotal
if $valid 0 0 0
76 }
77  
78 0 ▶ $self->reject($reject_text) if defined $reject_text;
Branch: 1 branch
BranchTrueFalseTotal
if defined $reject_text 0 0 0
79 0 ▶ $self->reject( $self->reject_text ) unless $valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 0 0 0
80 }
81  
82 1 1;
83  
84 __END__
85  
86 =head1 NAME
87  
88 Mail::Qmail::Filter::CheckDeliverabilty -
89 check deliverability according to .qmail files
90  
91 =head1 SYNOPSIS
92  
93 use Mail::Qmail::Filter;
94
95 Mail::Qmail::Filter->new->add_filter(
96 '::CheckDeliverability' => {
97 run_commands_matching => qr{/ezmlm-(?:checksub|reject)\b},
98 skip_if_relayclient => 1,
99 },
100 '::Queue',
101 )->run;
102  
103 =head1 DESCRIPTION
104  
105 This L<Mail::Qmail::Filter> plugin tries to find the appropriate C<.qmail> file
106 for all envelope recipients and optionally
107 L<runs commands|/run_commands_matching> mentioned in these files to check the
108 deliverability of the message.
109  
110 L<Rejects|Mail::Qmail::Filter/reject> the message if it would be I<equally>
111 undeliverable to I<all> recipients.
112  
113 For each recipient address, deliverability is checked as follows:
114  
115 =over 4
116  
117 =item *
118  
119 The appropriate C<.qmail> file is searched using L<Qmail::Deliverable>.
120 If none is found, the address is considered undeliverable,
121 with the L</reject_text> as reason.
122  
123 =item *
124  
125 Otherwise the C<.qmail> file is evaluated line by line in a similar fashion
126 as C<qmail-local> would:
127  
128 =item *
129  
130 Comment lines are skipped.
131  
132 =item *
133  
134 When a forward, maildir or mbox line is found, the recipient is considered
135 deliverable, and the filter stops.
136  
137 =item *
138  
139 When a command line is found, it is executed only if L</run_commands_matching>
140 is set and the line matches the given L<regular expression|perlre>.
141  
142 When it returns with exit code 100, the filter saves its output as possible
143 reject text, and the evaluation of this C<.qmail> file stops.
144  
145 =item *
146  
147 The message is rejected only if the evaluation of each C<.qmail> file resultet
148 with status 100 and the same reject text or if absolutely no valid C<.qmail>
149 files could be found.
150  
151 =back
152  
153 =head1 NOTE
154  
155 Please note that it is usually preferable to check deliverability directly after
156 each C<RCPT TO> command within the SMTP transaction, because only then you can
157 reject single recipients individually.
158 To achieve this, you may want to use tools like
159 L<spamdyke-qrv|https://www.spamdyke.org/documentation/README_spamdyke_qrv.html>.
160  
161 But: These tools do have limitations, e.g. they usually do not call external
162 commands and inherent to the sequence of SMTP commands they cannot take message
163 content into account.
164 And this is where this filters comes in handy.
165  
166 I use it to reject messages to mailing lists which do not come from a subscriber
167 of these lists.
168 Often these are spam messages with forged sender addresses, and by already
169 rejecting them during the SMTP transaction, one can avoid to produce collateral
170 spam.
171  
172 =head1 OPTIONAL PARAMETERS
173  
174 =head2 run_commands_matching
175  
176 expects a L<regular expression|perlre> as argument against which command
177 lines in the C<.qmail> files are to be matched.
178 Only commands matching this regular expression will be called.
179 You should take care to call only commands which do not deliver the message
180 itself, lest you want to shoot yourself in the foot.
181  
182 =head1 SEE ALSO
183  
184 L<Mail::Qmail::Filter/COMMON PARAMETERS FOR ALL FILTERS>
185  
186 =head1 LICENSE AND COPYRIGHT
187  
188 Copyright 2019 Martin Sluka.
189  
190 This module is free software; you can redistribute it and/or modify it
191 under the terms of the the Artistic License (2.0). You may obtain a
192 copy of the full license at:
193  
194 L<http://www.perlfoundation.org/artistic_license_2_0>
195  
196 Any use, modification, and distribution of the Standard or Modified
197 Versions is governed by this Artistic License. By using, modifying or
198 distributing the Package, you accept this license. Do not use, modify,
199 or distribute the Package, if you do not accept this license.
200  
201 If your Modified Version has been derived from a Modified Version made
202 by someone other than you, you are nevertheless required to ensure that
203 your Modified Version complies with the requirements of this license.
204  
205 This license does not grant you the right to use any trademark, service
206 mark, tradename, or logo of the Copyright Holder.
207  
208 This license includes the non-exclusive, worldwide, free-of-charge
209 patent license to make, have made, use, offer to sell, sell, import and
210 otherwise transfer the Package with respect to any patent claims
211 licensable by the Copyright Holder that are necessarily infringed by the
212 Package. If you institute patent litigation (including a cross-claim or
213 counterclaim) against any party alleging that the Package constitutes
214 direct or contributory patent infringement, then this Artistic License
215 to you shall terminate on the date that such litigation is filed.
216  
217 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
218 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
219 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
220 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
221 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
222 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
223 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
224 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
225  
226 =cut
↑ Summary Mail/Qmail/Filter/SkipQueue.pm »