Filter/Module/SendCopy.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 94 CC 10 SCAR 42.4
CC
10
Cov
0%
match
110.0
BEGIN
2.0
BEGIN
2.0
CRAP 69.5
1 #
2 # Courier::Filter::Module::SendCopy class
3 #
4 # (C) 2006-2008 Michael Buschbeck <michael@buschbeck.net>
5 # $Id: SendCopy.pm 211 2008-03-23 01:25:20Z julian $
6 #
7 ###############################################################################
8  
9 =head1 NAME
10  
11 Courier::Filter::Module::SendCopy - Pseudo-filter for the Courier::Filter
12 framework that sends a copy of certain messages to additional recipients
13  
14 =cut
15  
16 package Courier::Filter::Module::SendCopy;
17  
18 use warnings;
19 use strict;
20  
21 use base 'Courier::Filter::Module';
22  
23 use IO::File;
24  
25 use constant TRUE => (0 == 0);
26 use constant FALSE => not TRUE;
27  
28 =head1 SYNOPSIS
29  
30 use Courier::Filter::Module::SendCopy;
31
32 my $module = Courier::Filter::Module::SendCopy->new(
33 # One or more of the following criteria:
34 match_sender => qr/\@office\.example\.net$/,
35 match_recipients => qr/\@customer\.example\.com$/,
36 match_authenticated_user => 'my-smtp-user-name',
37
38 # One or several copy recipients:
39 copy_recipients => [
40 'el-cheffe@office.example.net',
41 'archives@customer.example.com',
42 ],
43
44 # Send a copy to the sender? (always/never/indifferent)
45 copy_to_sender => TRUE, # TRUE/FALSE/undef
46 )
47
48 my $filter = Courier::Filter->new(
49 ...
50 modules => [ $module ],
51 ...
52 );
53  
54 =head1 DESCRIPTION
55  
56 This class is a filter module for use with Courier::Filter. If a message
57 matches a given set of criteria, a blind carbon copy of the message is sent to
58 a configured list of additional recipients by adding them to the message's
59 control file. This module never matches.
60  
61 =cut
62  
63 # Implementation:
64 ###############################################################################
65  
66 =head2 Constructor
67  
68 The following constructor is provided:
69  
70 =over
71  
72 =item B<new(%options)>: returns I<Courier::Filter::Module::SendCopy>
73  
74 Creates a new B<SendCopy> filter module.
75  
76 %options is a list of key/value pairs representing any of the following
77 options:
78  
79 =over
80  
81 =item B<match_sender>
82  
83 A pattern matched against the message's envelope sender. If not specified,
84 any sender qualifies.
85  
86 =item B<match_recipients>
87  
88 A pattern matched against all of the message's envelope recipients. Any of
89 the envelope recipients may match. If no pattern is specified, any recipient
90 qualifies.
91  
92 =item B<match_authenticated_user>
93  
94 A pattern matched against the user name that was used for authentication
95 during submission of the message. If no pattern is specified, any user or
96 none at all qualifies. If a pattern is specified and no authentication took
97 place during submission of the message, the message does not qualify.
98  
99 =item B<copy_recipients>
100  
101 One or several addresses a copy of every matching message is sent to in
102 addition to the original recipients. A single address can be specified as a
103 simple scalar; a list of several addresses must be specified as an array
104 reference. Any address matching I<exactly> one of the original recipient
105 addresses is skipped.
106  
107 =item B<copy_to_sender>
108  
109 Specifies whether a copy of the message should be sent to its envelope sender.
110 If B<false>, no message is ever sent back in copy to its sender, even if the
111 sender is included in the C<copy_recipients> list. If B<true>, the sender is
112 always sent a copy of the message. If B<undef> (the default), the sender must
113 be included in the C<copy_recipients> list to receive a copy of the message.
114  
115 =back
116  
117 Patterns may either be simple strings (for exact, case-sensitive matches) or
118 regular expression objects created by the C<qr//> operator (for partial
119 matches).
120  
121 All options of the B<Courier::Filter::Module> constructor are also supported.
122 Please see L<Courier::Filter::Module/new> for their descriptions.
123  
124 =back
125  
126 =head2 Instance methods
127  
128 See L<Courier::Filter::Module/"Instance methods"> for a description of the
129 provided instance methods.
130  
131 =cut
132  
133 sub match {
134 my ($self, $message) = @_;
135
136 my $envelope = {
137 sender => [ $message->sender ],
138 recipients => [ $message->recipients ],
139 authenticated_user => [ $message->authenticated_user ],
140 };
141
142 foreach my $field (keys(%$envelope)) {
143 my $pattern = $self->{"match_$field"};
144 next
145 unless defined($pattern);
146 my $matched =
147 UNIVERSAL::isa($pattern, 'Regexp') ?
148 grep { defined and $_ =~ $pattern } @{$envelope->{$field}}
149 : grep { defined and $_ eq $pattern } @{$envelope->{$field}};
150 return undef
151 unless $matched;
152 }
153
154 my @copy_recipients;
155 if (defined($self->{copy_recipients})) {
156 @copy_recipients =
157 UNIVERSAL::isa($self->{copy_recipients}, 'ARRAY') ?
158 @{$self->{copy_recipients}}
159 : $self->{copy_recipients};
160 }
161
162 my $copy_recipients = { map { $_ => TRUE } @copy_recipients };
163 my $skip_recipients = { map { $_ => TRUE } $message->recipients };
164
165 if (defined($self->{copy_to_sender})) {
166 $self->{copy_to_sender} ?
167 $copy_recipients->{$message->sender} = TRUE
168 : $skip_recipients->{$message->sender} = TRUE;
169 }
170
171 return undef
172 unless %$copy_recipients;
173
174 # In case of several control files, take the last one.
175 my $control_file_name = ($message->control_file_names)[-1];
176
177 my $control_file_handle = IO::File->new($control_file_name, '>>')
178 or return undef;
179 foreach my $copy_recipient (keys(%$copy_recipients)) {
180 next if $skip_recipients->{$copy_recipient};
181 $control_file_handle->print("r$copy_recipient\n");
182 $control_file_handle->print("R\n");
183 $control_file_handle->print("N\n");
184 }
185 $control_file_handle->close();
186
187 return undef;
188 }
189  
190 =head1 SEE ALSO
191  
192 L<Courier::Filter::Module>, L<Courier::Filter::Overview>.
193  
194 For AVAILABILITY, SUPPORT and LICENSE information, see
195 L<Courier::Filter::Overview>.
196  
197 =head1 AUTHOR
198  
199 Michael Buschbeck <michael@buschbeck.net>
200  
201 =cut
202  
203 TRUE;