Filter/Module/Header.pm untested

filter: Statementstmt 0.0% 0 / 29 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 / 53 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::Header class
3 #
4 # (C) 2004-2008 Julian Mehnle <julian@mehnle.net>
5 # $Id: Header.pm 210 2008-03-21 19:30:31Z julian $
6 #
7 ###############################################################################
8  
9 =head1 NAME
10  
11 Courier::Filter::Module::Header - Message header filter module for the
12 Courier::Filter framework
13  
14 =cut
15  
16 package Courier::Filter::Module::Header;
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::Header;
29  
30 my $module = Courier::Filter::Module::Header->new(
31 fields => \%patterns_by_field_name,
32 response => $response_text,
33  
34 logger => $logger,
35 inverse => 0,
36 trusting => 0,
37 testing => 0,
38 debugging => 0
39 );
40  
41 my $filter = Courier::Filter->new(
42 ...
43 modules => [ $module ],
44 ...
45 );
46  
47 =head1 DESCRIPTION
48  
49 This class is a filter module class for use with Courier::Filter. It matches a
50 message if one of the message's header fields matches the configured criteria.
51  
52 =cut
53  
54 # Implementation:
55 ###############################################################################
56  
57 =head2 Constructor
58  
59 The following constructor is provided:
60  
61 =over
62  
63 =item B<new(%options)>: returns I<Courier::Filter::Module::Header>
64  
65 Creates a new B<Header> filter module.
66  
67 %options is a list of key/value pairs representing any of the following
68 options:
69  
70 =over
71  
72 =item B<fields>
73  
74 I<Required>. A reference to a hash containing the message header field names
75 and patterns (as key/value pairs) that messages are to be matched against.
76 Field names are matched case-insensitively. Patterns may either be simple
77 strings (for exact, case-sensitive matches) or regular expression objects
78 created by the C<qr//> operator (for inexact, partial matches).
79  
80 So for instance, to match any message from the "debian-devel" mailing list with
81 the subject containing something about 'duelling banjoes', you could set the
82 C<fields> option as follows:
83  
84 fields => {
85 'list-id' => '<debian-devel.lists.debian.org>',
86 subject => qr/duell?ing\s+banjoe?s?/i
87 }
88  
89 =item B<response>
90  
91 A string that is to be returned literally as the match result in case of a
92 match. Defaults to B<< "Prohibited header value detected: <field>: <value>" >>.
93  
94 =back
95  
96 All options of the B<Courier::Filter::Module> constructor are also supported.
97 Please see L<Courier::Filter::Module/"new()"> for their descriptions.
98  
99 =back
100  
101 =head2 Instance methods
102  
103 See L<Courier::Filter::Module/"Instance methods"> for a description of the
104 provided instance methods.
105  
106 =cut
107  
108 sub match {
109 my ($self, $message) = @_;
110
111 my $fields = $self->{fields};
112 foreach my $field (keys(%$fields)) {
113 my $pattern = $fields->{$field};
114 my $matcher =
115 UNIVERSAL::isa($pattern, 'Regexp') ?
116 sub { defined($_[0]) and $_[0] =~ $pattern }
117 : sub { defined($_[0]) and $_[0] eq $pattern };
118
119 my @values = $message->header($field);
120
121 foreach my $value (@values) {
122 if ($matcher->($value)) {
123 my $field_human_readable = ucfirst(lc($field));
124 return
125 'Header: ' . (
126 $self->{response} ||
127 "Prohibited header value detected: $field_human_readable: $value"
128 );
129 }
130 }
131 }
132
133 return undef;
134 }
135  
136 =head1 SEE ALSO
137  
138 L<Courier::Filter::Module::Envelope>, L<Courier::Filter::Module>,
139 L<Courier::Filter::Overview>.
140  
141 For AVAILABILITY, SUPPORT, and LICENSE information, see
142 L<Courier::Filter::Overview>.
143  
144 =head1 AUTHOR
145  
146 Julian Mehnle <julian@mehnle.net>
147  
148 =cut
149  
150 TRUE;