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