File Coverage

blib/lib/Mail/Qmail/Filter/CheckDeliverability.pm
Criterion Covered Total %
statement 8 47 17.0
branch 0 32 0.0
condition 0 15 0.0
subroutine 3 5 60.0
pod 1 1 100.0
total 12 100 12.0


line stmt bran cond sub pod time code
1 1     1   1179 use 5.014;
  1         5  
2 1     1   5 use warnings;
  1         3  
  1         51  
3              
4             package Mail::Qmail::Filter::CheckDeliverability;
5              
6             our $VERSION = '1.01';
7              
8 1     1   5 use Mo qw(coerce default);
  1         3  
  1         6  
9             extends 'Mail::Qmail::Filter';
10              
11             has 'reject_text' => 'Recipient unknown.';
12             has 'run_commands_matching';
13              
14             sub filter {
15 0     0 1   my $self = shift;
16 0           my $message = $self->message;
17 0           my $run_commands_matching = $self->run_commands_matching;
18              
19 0 0 0       require Qmail::Deliverable and Qmail::Deliverable->import('dot_qmail')
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 0         unless ( defined $dot_qmail ) {
26 0           $self->debug( 'No .qmail file found for rcpt' => $_ );
27 0           $reject_text = $self->reject_text;
28 0           next;
29             }
30 0 0         next if $done{$dot_qmail}++;
31 0 0         open my $fh, '<', $dot_qmail
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> ) ) {
35              
36 0 0         next if $line =~ /^#/;
37 0           ++$valid;
38 0           chomp $line;
39 0 0         return $self->debug( "$_ is at least deliverable to" => $line )
40             if $line =~ m{^[&/\w]};
41             next
42 0 0 0       unless $line =~ /^\|/
      0        
43             && defined $run_commands_matching
44             && $line =~ $run_commands_matching;
45              
46             require Capture::Tiny
47 0 0 0       and Capture::Tiny->import('capture_merged')
48             unless defined &capture_merged;
49 0           local $ENV{SENDER} = $message->from;
50             my ( $output, $exitcode ) = capture_merged(
51             sub {
52 0 0   0     open my $fh, $line
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 0         last if $exitcode == 99;
63 0 0         return $self->debug("Calling $line for $_ resulted in soft failure")
64             if $exitcode == 111;
65 0 0         next unless $exitcode == 100;
66 0 0 0       unless ( defined $reject_text ) {
67 0           $reject_text = $output;
68             }
69             elsif ( $output ne $reject_text ) {
70             return $self->debug(
71             qq(Different reject texts: "$reject_text" vs. "$output"));
72             }
73 0           next Recipient;
74             }
75 0 0         return $self->debug("might be deliverable to <$_>") if $valid;
76             }
77              
78 0 0         $self->reject($reject_text) if defined $reject_text;
79 0 0         $self->reject( $self->reject_text ) unless $valid;
80             }
81              
82             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