lib/Mail/Decency/Core/Server.pm

filter: Statementstmt 27.5% 30 / 109 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 50.0% 9 / 18 Podpod 100% 9 / 9 totaltotal 21.1% 48 / 227 CC 37 SCAR 52.7
CC
37
Cov
16%
gen_child
462.0
init_postfix_server
72.0
init_database
30.0
CRAP 194.9
« lib/Mail/Decency/Core/ExportImport.pm ↑ Summary lib/Mail/Decency/Core/SessionItem/ContentFilter.pm »
1 package Mail::Decency::Core::Server;
2  
3  
4 24 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 extends 'Mail::Decency::Core::Meta';
6  
7 24 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 24 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use Scalar::Util qw/ weaken blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 24 ▶use Mail::Decency::Helper::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 24 ▶use Mail::Decency::Helper::Database;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 24 ▶use Mail::Decency::Helper::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 #use POE::Component::Server::Postfix;
16  
17 24 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 =head1 NAME
21  
22 Mail::Decency::Core::Server
23  
24 =head1 DESCRIPTION
25  
26 Base module for all decency servers (policy, content filter, syslog parser).
27  
28  
29 =head1 CLASS ATTRIBUTES
30  
31 =head2 inited Bool
32  
33 Wheter the server is inited or not
34  
35 =cut
36  
37 24 has inited => ( is => 'ro', isa => 'Bool' );
38  
39  
40 =head2 inited ArrayRef[Mail::Decency::Core::Child]
41  
42 List of all (enabled) modules for this server.. Will be required when called handle
43  
44 =cut
45  
46 has childs => (
47 is => 'rw',
48 isa => 'ArrayRef[Mail::Decency::Core::Child]',
49 8 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
50 24 );
51  
52  
53 =head1 METHODS
54  
55  
56 =head2 init
57  
58 Init class for the server
59  
60 =cut
61  
62 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
63 0 die "Init method has to be overwritten by server methosd\n";
64 }
65  
66  
67 =head2 init_postfix_server
68  
69 Setup POE::Component::Server::Postfix
70  
71 =cut
72  
73 0 ▶sub init_postfix_server {
Subroutine init_postfix_server: not called CC=8 SCAR=42.8
Pod documented
74 0 my ( $self ) = @_;
75
76 # check server config
77 die "server config missing!\n"
78 0 ▶ unless defined $self->config->{ server } && ref( $self->config->{ server } ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->config->{"server"} and ref $self->config->{"server"} eq "HASH" 0 0 0
Condition0%0/3
defined $self->config->{"server"} and ref $self->config->{"server"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->config->{"server"} and ref $self->config->{"server"} eq "HASH"
defined $self->config->{"server"} ref $self->config->{"server"} eq "HASH"
Truth table0%0/3
A and B
A= defined $self->config->{"server"}B= ref $self->config->{"server"} eq "HASH"
ABresult
0X0
100
111
79 die "set either host and port OR socket for server\n"
80 if (
81 ! defined $self->config->{ server }->{ host }
82 && ! defined $self->config->{ server }->{ port }
83 && ! defined $self->config->{ server }->{ socket }
84 ) || (
85 defined $self->config->{ server }->{ host }
86 && defined $self->config->{ server }->{ socket }
87 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->config->{"server"}{"host"} || defined $self->config->{"server"}{"port"} || defined $self->config->{"server"}{"socket"} or defined $self->config->{"server"}{"host"} and defined $self->config->{"server"}{"socket"} 0 0 0
Condition0%0/12
not defined $self->config->{"server"}{"host"} || defined $self->config->{"server"}{"port"} || defined $self->config->{"server"}{"socket"} or defined $self->config->{"server"}{"host"} and defined $self->config->{"server"}{"socket"}
l!l&&r!l&&!r
000
defined $self->config->{'server'}{'host'} || defined $self->config->{'server'}{'port'} || defined $self->config->{'server'}{'socket'}
l!l&&r!l&&!r
000
defined $self->config->{"server"}{"host"} or defined $self->config->{"server"}{"port"}
l!l&&r!l&&!r
000
defined $self->config->{"server"}{"host"} and defined $self->config->{"server"}{"socket"}
!ll&&!rl&&r
000
MC/DC0%0/5
not defined $self->config->{"server"}{"host"} || defined $self->config->{"server"}{"port"} || defined $self->config->{"server"}{"socket"} or defined $self->config->{"server"}{"host"} and defined $self->config->{"server"}{"socket"}
defined $self->config->{"server"}{"host"} defined $self->config->{"server"}{"port"} defined $self->config->{'server'}{'socket'} defined $self->config->{"server"}{"host"} defined $self->config->{"server"}{"socket"}
Truth table0%0/10
not(A or B || C) or D and E
A= defined $self->config->{"server"}{"host"}B= defined $self->config->{"server"}{"port"}C= defined $self->config->{'server'}{'socket'}D= defined $self->config->{"server"}{"host"}E= defined $self->config->{"server"}{"socket"}
ABCDEresult
000XX1
1XX111
01X111
001111
1XX0X0
1XX100
01X0X0
01X100
0010X0
001100
88
89 0 return 1;
90 }
91  
92  
93 =head2 init_logger
94  
95 Setup logger facility
96  
97 =cut
98  
99 0 ▶sub init_logger {
Subroutine init_logger: not called CC=1 SCAR=6.9
Pod documented
100 0 my ( $self ) = @_;
101
102 # setup logger
103 0 ( my $prefix = ref( $self ) ) =~ s/^.*:://;
104 my $logger = Mail::Decency::Helper::Logger->new(
105 0 %{ $self->config->{ logging } },
106 prefix => $prefix
107 );
108 0 $self->{ logger } = $logger;
109 # $self->{ logger } = sub {
110 # $logger->log( @_ );
111 # };
112
113 0 return 1;
114 }
115  
116  
117 =head2 init_cache
118  
119 Setup cache facility ( $self->cache )
120  
121 =cut
122  
123 0 ▶sub init_cache {
Subroutine init_cache: not called CC=3 SCAR=24.8
Pod documented
124 0 my ( $self ) = @_;
125
126 # setup cache
127 die "cache config missing!\n"
128 0 ▶ unless defined $self->config->{ cache };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->config->{"cache"} 0 0 0
129 $self->{ cache } = blessed( $self->config->{ cache } )
130 ? $self->config->{ cache }
131 0 ▶ : Mail::Decency::Helper::Cache->new( %{ $self->config->{ cache } } )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self->config->{'cache'}) ? : 0 0 0
132 ;
133
134 0 return 1;
135 }
136  
137  
138 =head2 init_database
139  
140 Initi's database
141  
142 =cut
143  
144 0 ▶sub init_database {
Subroutine init_database: not called CC=5 SCAR=34.0
Pod documented
145 0 my ( $self ) = @_;
146
147 # setup cache
148 die "database config missing!\n"
149 0 ▶ unless defined $self->config->{ database };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->config->{"database"} 0 0 0
150
151 0 ▶ if ( blessed( $self->config->{ database } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self->config->{'database'})) { } 0 0 0
152 0 $self->{ database } = $self->config->{ database };
153 }
154 else {
155 my $type = $self->config->{ database }->{ type }
156 0 ▶ or die "Missing type for database (main)!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->config->{"database"}{"type"} or die "Missing type for database (main)!\n" 0 0 0
157
158 0 eval {
159 $self->{ database } = Mail::Decency::Helper::Database
160 0 ->create( $type => $self->config->{ database } );
161 };
162 0 ▶ die "Cannot create main database: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
163
164 }
165
166 0 weaken( my $self_weak = $self );
167 0 $self->database->logger( $self->logger->clone( "$self/db" ) );
168 }
169  
170  
171  
172 =head2 run
173  
174 Run the server
175  
176 =cut
177  
178 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
179 0 die "Run method has to be overwritten my server\n";
180 }
181  
182  
183 =head2 gen_child
184  
185 =cut
186  
187 0 ▶sub gen_child {
Subroutine gen_child: not called CC=21 SCAR=61.4
Pod documented
188 0 my ( $self, $base, $name, $config_ref, $init_args_ref ) = @_;
189
190 # if not hashref as config .. check wheter file
191 0 ▶ if ( ! ref( $config_ref ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $config_ref) 0 0 0
192
193 # having config dir ?
194 0 ▶ if ( ! $self->has_config_dir && defined $self->config->{ config_dir } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->has_config_dir and defined $self->config->{"config_dir"}) 0 0 0
Condition0%0/3
not $self->has_config_dir and defined $self->config->{"config_dir"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->has_config_dir and defined $self->config->{"config_dir"}
not $self->has_config_dir defined $self->config->{"config_dir"}
Truth table0%0/3
not(A) and B
A= not $self->has_config_dirB= defined $self->config->{"config_dir"}
ABresult
0X0
100
111
195 0 ▶ if ( -d $self->config->{ config_dir } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->config->{'config_dir'}) { } 0 0 0
196 0 $self->config_dir( $self->config->{ config_dir } );
197 }
198 else {
199 0 die "Provided config_dir '". $self->config->{ config_dir }. "' is not a directory or not readable\n";
200 }
201 }
202
203 # having dir-name ?
204 0 ▶ if ( ! -f $config_ref && $self->has_config_dir && -f $self->config_dir . "/$config_ref" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $config_ref and $self->has_config_dir and -f $self->config_dir . "/$config_ref") 0 0 0
Condition0%0/6
not -f $config_ref and $self->has_config_dir and -f $self->config_dir . "/$config_ref"
!ll&&!rl&&r
000
not -f $config_ref and $self->has_config_dir
!ll&&!rl&&r
000
MC/DC0%0/3
not -f $config_ref and $self->has_config_dir and -f $self->config_dir . "/$config_ref"
not -f $config_ref $self->has_config_dir -f $self->config_dir . "/$config_ref"
Truth table0%0/4
not(A) and B and C
A= not -f $config_refB= $self->has_config_dirC= -f $self->config_dir . "/$config_ref"
ABCresult
0XX0
10X0
1100
1111
205 0 $config_ref = $self->config_dir . "/$config_ref";
206 }
207
208 # having file ..
209 0 ▶ if ( -f $config_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $config_ref) { } 0 0 0
210 0 eval {
211 0 $config_ref = YAML::LoadFile( $config_ref );
212 };
213 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
214 0 die "Error loading config file '$config_ref' for $name: $@\n";
215 }
216 }
217 else {
218 0 ▶ die "Sorry, cannot find config file '$config_ref' for $name. (config_dir: ". ( $self->has_config_dir ? $self->config_dir : "not set" ). ")\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->has_config_dir ? : 0 0 0
219 }
220 }
221
222 # being disabled ?
223 0 ▶ return if $config_ref->{ disable };
Branch: 1 branch
BranchTrueFalseTotal
if $config_ref->{"disable"} 0 0 0
224
225 # weak reference to self
226 0 weaken( my $self_weak = $self );
227
228 # havin extra databas for this fellow ?
229 0 my $database;
230 0 ▶ if ( defined $config_ref->{ database } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_ref->{'database'}) { } 0 0 0
231 my $type = $config_ref->{ database }->{ type }
232 0 ▶ or die "Missing required 'type' for database ($name)!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $type = $config_ref->{"database"}{"type"} or die "Missing required 'type' for database ($name)!\n" 0 0 0
233 0 eval {
234 $database = Mail::Decency::Helper::Database
235 0 ->create( $type => $config_ref->{ database } );
236 };
237 0 ▶ die "Cannot create database for $name: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
238 }
239 else {
240 0 weaken( $database = $self->database );
241 }
242
243 # determine module base
244 0 my $module = "$base\::$name";
245 0 eval "use $module";
246 0 ▶ die "Missing policy module '$name' ($module)\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
247
248 # create instance of sub module
249 0 my $obj;
250 0 eval {
251 0 my $logger = $self->logger->clone( lc( $self->logger->prefix. "/$name" ) );
252
253 # delegate logger if new database
254 $database->logger( $logger->clone->prefix. "/db" )
255 0 ▶ if ( defined $config_ref->{ database } );
Branch: 1 branch
BranchTrueFalseTotal
if defined $config_ref->{"database"} 0 0 0
256
257 # create the object itself
258 0 $obj = $module->new(
259 name => $name,
260 config => $config_ref,
261 cache => $self->cache,
262 database => $database,
263 server => $self,
264 logger => $logger,
265 %$init_args_ref
266 );
267
268 # check database, if can .. don't start with corrupted !
269 0 ▶ if ( $obj->can( 'check_database' ) && ! $ENV{ NO_CHECK_DATABASE } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->can("check_database") and not $ENV{"NO_CHECK_DATABASE"}) 0 0 0
Condition0%0/3
$obj->can("check_database") and not $ENV{"NO_CHECK_DATABASE"}
!ll&&!rl&&r
000
MC/DC0%0/2
$obj->can("check_database") and not $ENV{"NO_CHECK_DATABASE"}
$obj->can("check_database") not $ENV{"NO_CHECK_DATABASE"}
Truth table0%0/3
A and not(B)
A= $obj->can("check_database")B= not $ENV{"NO_CHECK_DATABASE"}
ABresult
0X0
100
111
270 0 ( my $db_class = ref( $self->database ) ) =~ s/^.+:://;
271 0 ▶ $obj->check_database( $obj->schema_definition )
Branch: 1 branch
BranchTrueFalseTotal
$obj->check_database($obj->schema_definition) or die "Please create the database yourself (class: $db_class)\n" 0 0 0
272 or die "Please create the database yourself (class: $db_class)\n";
273 }
274
275 };
276
277 0 ▶ die "Error creating $name: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
278
279 # add to meta list of childs
280 0 push @{ $self->childs }, $obj;
281
282 0 return $obj;
283 }
284  
285  
286 =head2 maintenance
287  
288 Call maintenance, cleanup databases.
289  
290 =cut
291  
292 0 ▶sub maintenance {
Subroutine maintenance: not called CC=3 SCAR=24.8
Pod documented
293 0 my ( $self ) = @_;
294
295 0 $self->logger->info( "Running in maintenance mode" );
296
297 0 foreach my $policy( @{ $self->childs } ) {
298 0 ▶ $policy->maintenance() if $policy->can( 'maintenance' );
Branch: 1 branch
BranchTrueFalseTotal
if $policy->can("maintenance") 0 0 0
299 }
300
301 0 $self->logger->info( "Maintenance performed" );
302
303 0 exit 0;
304 }
305  
306  
307 =head2 disable_logging
308  
309 Disables all loggers in all modules
310  
311 =cut
312  
313 0 ▶sub disable_logging {
Subroutine disable_logging: not called CC=2 SCAR=17.9
Pod documented
314 0 my ( $self ) = @_;
315 0 $self->logger->disabled( 1 );
316 0 $self->database->logger->disabled( 1 );
317 0 foreach my $child( @{ $self->childs } ) {
318 0 $child->logger->disabled( 1 );
319 0 $child->database->logger->disabled( 1 );
320 }
321 }
322  
323  
324 =head1 AUTHOR
325  
326 Ulrich Kautz <uk@fortrabbit.de>
327  
328 =head1 COPYRIGHT
329  
330 Copyright (c) 2010 the L</AUTHOR> as listed above
331  
332 =head1 LICENCSE
333  
334 This library is free software and may be distributed under the same terms as perl itself.
335  
336 =cut
337  
338  
339  
340 24 1;
« lib/Mail/Decency/Core/ExportImport.pm ↑ Summary lib/Mail/Decency/Core/SessionItem/ContentFilter.pm »