File Coverage

blib/lib/Mail/Qmail/Filter/RequireFrom.pm
Criterion Covered Total %
statement 14 20 70.0
branch 0 4 0.0
condition n/a
subroutine 5 6 83.3
pod 1 1 100.0
total 20 31 64.5


line stmt bran cond sub pod time code
1 1     1   746 use 5.014;
  1         3  
2 1     1   4 use warnings;
  1         2  
  1         54  
3              
4              
5             our $VERSION = '1.1';
6              
7             use Mail::Qmail::Filter::Util qw(addresses_to_hash match_address);
8 1     1   5  
  1         2  
  1         58  
9             use namespace::clean;
10 1     1   6  
  1         2  
  1         5  
11             use Mo qw(coerce default required);
12 1     1   170 extends 'Mail::Qmail::Filter';
  1         2  
  1         12  
13              
14             has 'allowed_addresses' => coerce => \&addresses_to_hash, required => 1;
15             has 'lowercase_from'; # ignored; only for backwards compatibility
16             has 'reject_text' => sub {
17             sub { "<$_[0]> not allowed as RFC5322.From" }
18             };
19              
20             my $self = shift;
21             my $header_from_address = '';
22 0     0 1   if ( my $header_from = $self->message->header_from ) {
23 0           $header_from_address = $header_from->address;
24 0 0         return
25 0           if match_address( $self->allowed_addresses, $header_from_address );
26             }
27 0 0         $self->reject( $self->reject_text, $header_from_address );
28             }
29 0            
30             1;
31              
32              
33             =head1 NAME
34              
35             Mail::Qmail::Filter::RequireFrom -
36             only allow certain RFC5322.From addresses
37              
38             =head1 SYNOPSIS
39              
40             use Mail::Qmail::Filter;
41              
42             Mail::Qmail::Filter->new->add_filters(
43             '::RequireFrom' => {
44             allowed_addresses => [ 'example.org', 'localpart@example.com', ],
45             lowercase_from => 1,
46             },
47             '::Queue',
48             )->run;
49              
50             =head1 DESCRIPTION
51              
52             This L<Mail::Qmail::Filter> plugin rejects the message if it does not contain
53             one of the explicitely L</allowed_addresses> in its C<From> header line.
54              
55             =head1 REQUIRED PARAMETERS
56              
57             =head2 allowed_addresses
58              
59             List of allowed e-mail addresses which should be allowed in the C<From>
60             header line.
61             If given a domain name instead of a complete address, any localpart @
62             this domain will be allowed.
63              
64             =head1 OPTIONAL PARAMETERS
65              
66             =head2 reject_text
67              
68             Text to use when rejecting the message because it has no allowed C<From>
69             address.
70              
71             May be a string or a subroutine which returns the text.
72             The subroutine may access the problematic address as its first argument.
73              
74             Default:
75              
76             sub { "<$_[0]> not allowed as RFC5322.From" }
77              
78             =head1 SEE ALSO
79              
80             L<Mail::Qmail::Filter/COMMON PARAMETERS FOR ALL FILTERS>
81              
82             =head1 LICENSE AND COPYRIGHT
83              
84             Copyright 2019 Martin Sluka.
85              
86             This module is free software; you can redistribute it and/or modify it
87             under the terms of the the Artistic License (2.0). You may obtain a
88             copy of the full license at:
89              
90             L<http://www.perlfoundation.org/artistic_license_2_0>
91              
92             Any use, modification, and distribution of the Standard or Modified
93             Versions is governed by this Artistic License. By using, modifying or
94             distributing the Package, you accept this license. Do not use, modify,
95             or distribute the Package, if you do not accept this license.
96              
97             If your Modified Version has been derived from a Modified Version made
98             by someone other than you, you are nevertheless required to ensure that
99             your Modified Version complies with the requirements of this license.
100              
101             This license does not grant you the right to use any trademark, service
102             mark, tradename, or logo of the Copyright Holder.
103              
104             This license includes the non-exclusive, worldwide, free-of-charge
105             patent license to make, have made, use, offer to sell, sell, import and
106             otherwise transfer the Package with respect to any patent claims
107             licensable by the Copyright Holder that are necessarily infringed by the
108             Package. If you institute patent litigation (including a cross-claim or
109             counterclaim) against any party alleging that the Package constitutes
110             direct or contributory patent infringement, then this Artistic License
111             to you shall terminate on the date that such litigation is filed.
112              
113             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
114             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
115             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
116             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
117             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
118             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
119             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
120             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
121              
122             =cut