Filter/Module.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 99 CC 5 SCAR 14.1
CC
5
Cov
0%
consider
12.0
logger
6.0
debugging
6.0
CRAP 4.1
1 #
2 # Courier::Filter::Module abstract base class
3 #
4 # (C) 2003-2008 Julian Mehnle <julian@mehnle.net>
5 # $Id: Module.pm 210 2008-03-21 19:30:31Z julian $
6 #
7 ###############################################################################
8  
9 =head1 NAME
10  
11 Courier::Filter::Module - Abstract Perl base class for filter modules used by
12 the Courier::Filter framework
13  
14 =cut
15  
16 package Courier::Filter::Module;
17  
18 use warnings;
19 use strict;
20  
21 use Error ':try';
22  
23 use Courier::Error;
24  
25 use constant TRUE => (0 == 0);
26 use constant FALSE => not TRUE;
27  
28 =head1 SYNOPSIS
29  
30 =head2 Courier::Filter message filtering
31  
32 use Courier::Filter::Module::My; # Need to use a non-abstract sub-class.
33
34 my $module = Courier::Filter::Module::My->new(
35 logger => $logger,
36 inverse => 0,
37 trusting => 0,
38 testing => 0,
39 debugging => 0
40 );
41
42 my $filter = Courier::Filter->new(
43 ...
44 modules => [ $module ],
45 ...
46 );
47  
48 =head2 Deriving new filter module classes
49  
50 package Courier::Filter::Module::My;
51 use base qw(Courier::Filter::Module);
52  
53 =head1 DESCRIPTION
54  
55 Sub-classes of B<Courier::Filter::Module> are used by the B<Courier::Filter>
56 mail filtering framework to determine the acceptability of messages. See
57 L<Courier::Filter::Overview> for an overview of how filter modules are used and
58 for how to write them.
59  
60 When overriding a method in a derived class, do not forget calling the
61 inherited method from your overridden method.
62  
63 =cut
64  
65 # Implementation:
66 ###############################################################################
67  
68 =head2 Constructor
69  
70 The following constructor is provided and may be overridden:
71  
72 =over
73  
74 =item B<new(%options)>: returns I<Courier::Filter::Module>
75  
76 Creates a new filter module using the %options. Initializes the filter module,
77 by opening I/O handles, connecting to databases, creating temporary files,
78 initializing parser libraries, etc..
79  
80 %options is a list of key/value pairs representing any of the following
81 options:
82  
83 =over
84  
85 =item B<logger>
86  
87 A B<Courier::Filter::Logger> object that will be used for logging message
88 rejections and error messages caused by this individual filter module. If no
89 logger is specified, Courier::Filter's global logger will be used.
90  
91 =item B<inverse>
92  
93 A boolean value controlling whether the filter module should operate with
94 inverse polarity (B<true>) as opposed to normal polarity (B<false>). Under
95 inverse polarity, the result of the C<match> method is negated by the
96 C<consider> method before returning it. For details on how Courier::Filter
97 translates I<match results> into I<acceptability results>, see
98 L<Courier::Filter::Overview/"How filter modules work">. Defaults to B<false>.
99  
100 =item B<trusting>
101  
102 A boolean value controlling whether the filter module should I<not> be applied
103 to trusted messages. For details on how the trusted status is determined, see
104 the description of the C<trusted> property in L<Courier::Message>. In most
105 configurations, this option can be used to white-list so-called I<outbound>
106 messages. Defaults to B<false>.
107  
108 =item B<testing>
109  
110 A boolean value controlling whether the filter module should run in testing
111 mode. In testing mode, planned message rejections will be logged as usual, but
112 no messages will actually be rejected. Defaults to B<false>.
113  
114 =item B<debugging>
115  
116 A boolean value controlling whether the filter module should log extra
117 debugging information. Defaults to B<false>.
118  
119 =back
120  
121 Derived classes may support additional options.
122  
123 C<Courier::Filter::Module::new()> creates a hashref as an object of the invoked
124 class, and stores the %options in it, but does nothing else.
125  
126 =cut
127  
128 sub new {
129 my ($class, %options) = @_;
130 $class ne __PACKAGE__
131 or throw Courier::Error('Unable to instantiate abstract ' . __PACKAGE__ . ' class');
132 my $self = { %options };
133 return bless($self, $class);
134 }
135  
136 =back
137  
138 =head2 Destructor
139  
140 The following destructor is provided and may be overridden:
141  
142 =over
143  
144 =item B<destroy>
145  
146 Uninitializes the filter module, by closing I/O handles, disconnecting from
147 databases, deleting temporary files, uninitializing parser libraries, etc..
148  
149 C<Courier::Filter::Module::destroy()> does nothing. Sub-classes may override
150 this method and define clean-up behavior.
151  
152 =cut
153  
154 sub destroy {
155 my ($object) = @_;
156 return;
157 }
158  
159 =back
160  
161 =head2 Class methods
162  
163 The following class methods are provided and may be overridden:
164  
165 =over
166  
167 =item B<warn($text)>
168  
169 Writes a warning message to syslog. This method may also be used as an
170 instance method.
171  
172 =cut
173  
174 sub warn {
175 my ($self, $text) = @_;
176 my $class = ref($self) || $self;
177 STDERR->printf("%s: Warning: %s\n", $class, $text);
178 return;
179 }
180  
181 =back
182  
183 =head2 Instance methods
184  
185 The following instance methods are provided and may be overridden:
186  
187 =over
188  
189 =item B<consider($message)>: returns I<string>, [I<string>]; throws Perl exceptions
190  
191 Calls the C<match> method, passing it the $message object. Returns the result
192 of C<match>, negating it beforehand if the filter module has inverse polarity.
193 There is usually no need at all to override this method.
194  
195 =cut
196  
197 sub consider {
198 my ($self, $message) = @_;
199 my ($result, @code) = $self->match($message);
200 ($result, @code) = ($result ? '' : undef)
201 if $self->inverse;
202 return ($result, @code);
203 }
204  
205 =item B<match($message)>: returns I<string>, [I<string>]; throws Perl exceptions
206  
207 Examines the B<Courier::Message> object given as $message. Returns a so-called
208 I<match result> consisting of an SMTP status response I<text>, and an optional
209 numerical SMTP status response I<code>. For details about how I<match results>
210 are used by Courier::Filter, see L<Courier::Filter::Overview/"How filter
211 modules work"> and L<Courier::Filter::Overview/"Writing filter modules">.
212  
213 C<Courier::Filter::Module::match()> does nothing and returns B<undef>.
214 Sub-classes should override this method and define their own matching rule.
215  
216 =cut
217  
218 sub match {
219 my ($self, $message) = @_;
220 return undef;
221 }
222  
223 =item B<logger>: returns I<Courier::Filter::Logger>
224  
225 =item B<logger($logger)>: returns I<Courier::Filter::Logger>
226  
227 If C<$logger> is specified, installs a new logger for this individual filter
228 module. Returns the current (new) logger.
229  
230 =cut
231  
232 sub logger {
233 my ($self, @logger) = @_;
234 $self->{logger} = $logger[0]
235 if @logger;
236 return $self->{logger};
237 }
238  
239 =item B<inverse>: returns I<boolean>
240  
241 Returns a boolean value indicating whether the filter module is operating with
242 inverse polarity, as set through the constructor's C<inverse> option.
243  
244 =cut
245  
246 sub inverse {
247 my ($self) = @_;
248 # Read-only!
249 return ($self->{inverse});
250 }
251  
252 =item B<trusting>: returns I<boolean>
253  
254 Returns a boolean value indicating whether the filter module does I<not> apply
255 to trusted messages, as set through the constructor's C<trusting> option.
256  
257 =cut
258  
259 sub trusting {
260 my ($self) = @_;
261 # Read-only!
262 return $self->{trusting};
263 }
264  
265 =item B<testing>: returns I<boolean>
266  
267 Returns a boolean value indicating whether the filter module is in testing
268 mode, as set through the constructor's C<testing> option.
269  
270 =cut
271  
272 sub testing {
273 my ($self) = @_;
274 # Read-only!
275 return $self->{testing};
276 }
277  
278 =item B<debugging>: returns I<boolean>
279  
280 =item B<debugging($debugging)>: returns I<boolean>
281  
282 If C<$debugging> is specified, sets the debugging mode for this individual
283 filter module. Returns the (newly) configured debugging mode.
284  
285 =cut
286  
287 sub debugging {
288 my ($self, @debugging) = @_;
289 $self->{debugging} = $debugging[0]
290 if @debugging;
291 return $self->{debugging};
292 }
293  
294 =back
295  
296 =cut
297  
298 BEGIN {
299 no warnings 'once';
300 *DESTROY = \&destroy;
301 }
302  
303 =head1 SEE ALSO
304  
305 L<Courier::Filter>, L<Courier::Filter::Module>.
306  
307 For a list of prepared loggers that come with Courier::Filter, see
308 L<Courier::Filter::Overview/"Bundled Courier::Filter loggers">.
309  
310 For AVAILABILITY, SUPPORT, and LICENSE information, see
311 L<Courier::Filter::Overview>.
312  
313 =head1 AUTHOR
314  
315 Julian Mehnle <julian@mehnle.net>
316  
317 =cut
318  
319 TRUE;