Filter/Module/Envelope.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 60 CC 3 SCAR 17.5
CC
3
Cov
0%
match
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 5.8
1 #
2 # Courier::Filter::Module::Envelope class
3 #
4 # (C) 2004-2008 Julian Mehnle <julian@mehnle.net>
5 # $Id: Envelope.pm 210 2008-03-21 19:30:31Z julian $
6 #
7 ###############################################################################
8  
9 =head1 NAME
10  
11 Courier::Filter::Module::Envelope - Message envelope filter module for the
12 Courier::Filter framework
13  
14 =cut
15  
16 package Courier::Filter::Module::Envelope;
17  
18 use warnings;
19 use strict;
20  
21 use base 'Courier::Filter::Module';
22  
23 use constant TRUE => (0 == 0);
24 use constant FALSE => not TRUE;
25  
26 =head1 SYNOPSIS
27  
28 use Courier::Filter::Module::Envelope;
29  
30 my $module = Courier::Filter::Module::Envelope->new(
31 fields => {
32 # One or more of the following fields:
33 sender => 'paul.greenfield@unisys.com',
34 recipient => 'julian@mehnle.net',
35 remote_host => '216.250.130.2',
36 remote_host_name => qr/(^|\.)php\.net$/,
37 remote_host_helo => qr/^[^.]*$/
38 },
39  
40 # Optionally the following:
41 response => $response_text,
42  
43 logger => $logger,
44 inverse => 0,
45 trusting => 0,
46 testing => 0,
47 debugging => 0
48 );
49  
50 my $filter = Courier::Filter->new(
51 ...
52 modules => [ $module ],
53 ...
54 );
55  
56 =head1 DESCRIPTION
57  
58 This class is a filter module class for use with Courier::Filter. It matches a
59 message if one of the message's envelope fields matches the configured
60 criteria.
61  
62 =cut
63  
64 # Implementation:
65 ###############################################################################
66  
67 =head2 Constructor
68  
69 The following constructor is provided:
70  
71 =over
72  
73 =item B<new(%options)>: returns I<Courier::Filter::Module::Envelope>
74  
75 Creates a new B<Envelope> filter module.
76  
77 %options is a list of key/value pairs representing any of the following
78 options:
79  
80 =over
81  
82 =item B<fields>
83  
84 I<Required>. A reference to a hash containing the message envelope field names
85 and patterns (as key/value pairs) that messages are to be matched against.
86 Field names are matched case-insensitively. Patterns may either be simple
87 strings (for exact, case-sensitive matches) or regular expression objects
88 created by the C<qr//> operator (for inexact, partial matches).
89  
90 The following envelope fields are supported:
91  
92 =over
93  
94 =item B<sender>
95  
96 The message's envelope sender (from the "MAIL FROM" SMTP command).
97  
98 =item B<recipient>
99  
100 Any of the message's envelope recipients (from the "RCPT TO" SMTP commands).
101  
102 =item B<remote_host>
103  
104 The IP address of the SMTP client that submitted the message.
105  
106 =item B<remote_host_name>
107  
108 The host name (gained by Courier through a DNS reverse lookup) of the SMTP
109 client that submitted the message, if available.
110  
111 =item B<remote_host_helo>
112  
113 The HELO string that the SMTP client specified, if available.
114  
115 =back
116  
117 So for instance, to match any message with a sender of
118 C<paul.greenfield@unisys.com>, directed at C<julian@mehnle.net> (possibly among
119 other recipients), you could set the C<fields> option as follows:
120  
121 fields => {
122 sender => 'paul.greenfield@unisys.com',
123 recipient => 'julian@mehnle.net'
124 }
125  
126 =item B<response>
127  
128 A string that is to be returned literally as the match result in case of a
129 match. Defaults to B<< "Prohibited <field>: <value>" >>.
130  
131 =back
132  
133 All options of the B<Courier::Filter::Module> constructor are also supported.
134 Please see L<Courier::Filter::Module/"new()"> for their descriptions.
135  
136 =back
137  
138 =head2 Instance methods
139  
140 See L<Courier::Filter::Module/"Instance methods"> for a description of the
141 provided instance methods.
142  
143 =cut
144  
145 sub match {
146 my ($self, $message) = @_;
147
148 my $envelope = {
149 sender => [$message->sender],
150 recipient => [$message->recipients],
151 remote_host => [$message->remote_host],
152 remote_host_name => [$message->remote_host_name],
153 remote_host_helo => [$message->remote_host_helo]
154 };
155
156 my $fields = $self->{fields};
157 foreach my $field (keys(%$fields)) {
158 my $pattern = $fields->{$field};
159 my $matcher =
160 UNIVERSAL::isa($pattern, 'Regexp') ?
161 sub { defined($_[0]) and $_[0] =~ $pattern }
162 : sub { defined($_[0]) and $_[0] eq $pattern };
163
164 foreach my $value ( @{$envelope->{$field}} ) {
165 if ($matcher->($value)) {
166 my $field_human_readable = $field;
167 $field_human_readable =~ tr/_/ /;
168 return
169 'Envelope: ' . (
170 $self->{response} ||
171 "Prohibited $field_human_readable detected: $value"
172 );
173 }
174 }
175 }
176
177 return undef;
178 }
179  
180 =head1 SEE ALSO
181  
182 L<Courier::Filter::Module::Header>, L<Courier::Filter::Module>,
183 L<Courier::Filter::Overview>.
184  
185 For AVAILABILITY, SUPPORT, and LICENSE information, see
186 L<Courier::Filter::Overview>.
187  
188 =head1 AUTHOR
189  
190 Julian Mehnle <julian@mehnle.net>
191  
192 =cut
193  
194 TRUE;