Milter/Module/HeaderRegex.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 68 CC 4 SCAR 15.5
CC
4
Cov
0%
header_callback
12.0
new
6.0
HeaderRegex
2.0
CRAP 4.7
1 # $Id: HeaderRegex.pm,v 1.5 2004/04/12 14:21:41 tvierling Exp $
2 #
3 # Copyright (c) 2002-2004 Todd Vierling <tv@pobox.com> <tv@duh.org>
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 #
16 # 3. Neither the name of the author nor the names of contributors may be used
17 # to endorse or promote products derived from this software without specific
18 # prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31  
32 package Mail::Milter::Module::HeaderRegex;
33  
34 use 5.006;
35 use base Exporter;
36 use base Mail::Milter::Object;
37  
38 use strict;
39 use warnings;
40  
41 use Carp;
42 use Sendmail::Milter 0.18; # get needed constants
43  
44 our $VERSION = '0.03';
45  
46 =pod
47  
48 =head1 NAME
49  
50 Mail::Milter::Module::HeaderRegex - milter to accept/reject messages with certain headers
51  
52 =head1 SYNOPSIS
53  
54 use Mail::Milter::Module::HeaderRegex;
55  
56 my $milter = new Mail::Milter::Module::HeaderRegex('^Foo: ');
57  
58 my $milter2 = &HeaderRegex('^Foo: Bar'); # convenience
59  
60 =head1 DESCRIPTION
61  
62 This milter module rejects messages at DATA phase if one of the message's
63 headers matches user-supplied regular expressions.
64  
65 =head1 METHODS
66  
67 =over 4
68  
69 =cut
70  
71 our @EXPORT = qw(&HeaderRegex);
72  
73 sub HeaderRegex {
74 new Mail::Milter::Module::HeaderRegex(@_);
75 }
76  
77 =pod
78  
79 =item new(REGEX[, ...])
80  
81 Accepts one or more regular expressions, as strings or qr// precompiled
82 regexes. They are tested in sequence, and the first match terminates
83 checking.
84  
85 =cut
86  
87 sub new ($$;@) {
88 my $this = Mail::Milter::Object::new(shift);
89  
90 $this->{_message} = 'Malformed or invalid header %H: in message';
91  
92 croak 'new HeaderRegex: no regexes supplied' unless scalar @_;
93 $this->{_regexes} = [ map qr/$_/i, @_ ];
94  
95 $this;
96 }
97  
98 =pod
99  
100 =item set_message(MESSAGE)
101  
102 Sets the message used when rejecting messages. This string may contain the
103 substring C<%H>, which will be replaced by the matching header name.
104  
105 This method returns a reference to the object itself, allowing this method
106 call to be chained.
107  
108 =cut
109  
110 sub set_message ($$) {
111 my $this = shift;
112  
113 $this->{_message} = shift;
114  
115 $this;
116 }
117  
118 sub header_callback {
119 my $this = shift;
120 my $ctx = shift;
121 my $hname = shift;
122 my $header = "$hname: ".(shift);
123  
124 foreach my $rx (@{$this->{_regexes}}) {
125 if ($header =~ $rx) {
126 my $msg = $this->{_message};
127  
128 if (defined($msg)) {
129 $msg =~ s/%H/$hname/g;
130 $ctx->setreply('554', '5.7.1', $msg);
131 }
132  
133 return SMFIS_REJECT;
134 }
135 }
136  
137 SMFIS_CONTINUE;
138 }
139  
140 1;
141 __END__