| 1 | package Mail::MtPolicyd::PluginChain; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 6 | our $VERSION = '2.01'; # VERSION | |||||||||||||||||||||||||||
| 7 | # ABSTRACT: class for a VirtualHost instance | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Mail::MtPolicyd::Profiler; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Mail::MtPolicyd::Result; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | has 'plugins' => ( | ||||||||||||||||||||||||||||
| 13 | is => 'ro', | ||||||||||||||||||||||||||||
| 14 | isa => 'ArrayRef[Mail::MtPolicyd::Plugin]', | ||||||||||||||||||||||||||||
| 15 | 2 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 6 | traits => [ 'Array' ], | |||||||||||||||||||||||||||
| 17 | handles => { | ||||||||||||||||||||||||||||
| 18 | 'add_plugin' => 'push', | ||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 6 | has 'plugin_prefix' => ( | |||||||||||||||||||||||||||
| 23 | is => 'ro', isa => 'Str', default => 'Mail::MtPolicyd::Plugin::', | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 6 | has 'vhost_name' => ( is => 'rw', isa => 'Maybe[Str]' ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub run { | ||||||||||||||||||||||||||||
| 29 | 2 | ▶ | my ( $self, $r ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=9 SCAR=25.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 2 | my $result = Mail::MtPolicyd::Result->new; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | foreach my $plugin ( @{$self->plugins} ) { | |||||||||||||||||||||||||||
| 33 | 2 | my $abort = 0; | |||||||||||||||||||||||||||
| 34 | 2 | Mail::MtPolicyd::Profiler->new_timer('plugin '.$plugin->name); | |||||||||||||||||||||||||||
| 35 | 2 | my @plugin_results; | |||||||||||||||||||||||||||
| 36 | 2 | eval { @plugin_results = $plugin->run($r); }; | |||||||||||||||||||||||||||
| 37 | 2 | my $e = $@; | |||||||||||||||||||||||||||
| 38 | 2 | ▶ | if( $e ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | my $msg = 'plugin '.$plugin->name.' failed: '.$e; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | if( ! defined $plugin->on_error || $plugin->on_error ne 'continue' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $plugin->on_error or $plugin->on_error ne "continue"
MC/DC0%0/2
not defined $plugin->on_error or $plugin->on_error ne "continue"
not defined $plugin->on_error
$plugin->on_error ne "continue"
Truth table0%0/3
not(A) or B
A= not defined $plugin->on_errorB= $plugin->on_error ne "continue"
| |||||||||||||||||||||||||||||
| 41 | 0 | die($msg); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 0 | $r->log(0, $msg); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 2 | Mail::MtPolicyd::Profiler->stop_current_timer; | |||||||||||||||||||||||||||
| 46 | 2 | ▶ | if( scalar @plugin_results ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 2 | $result->last_match( $plugin->name ); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 2 | foreach my $plugin_result ( @plugin_results ) { | |||||||||||||||||||||||||||
| 50 | 2 | $result->add_plugin_result($plugin_result); | |||||||||||||||||||||||||||
| 51 | 2 | ▶ | if( $plugin_result->abort ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 2 | $abort = 1; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 2 | ▶ | if( $abort ) { last; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 2 | return $result; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub cron { | ||||||||||||||||||||||||||||
| 62 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
cron: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 2 | my $server = shift; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2 | foreach my $plugin ( @{$self->plugins} ) { | |||||||||||||||||||||||||||
| 66 | 2 | $server->log(3, 'running cron for plugin '.$plugin->name); | |||||||||||||||||||||||||||
| 67 | 2 | eval { $plugin->cron( $server, @_ ); }; | |||||||||||||||||||||||||||
| 68 | 2 | my $e = $@; | |||||||||||||||||||||||||||
| 69 | 2 | ▶ | if( $e ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 1 | $server->log(0, 'plugin '.$plugin->name.' failed in cron: '.$e ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 2 | return; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub load_plugin { | ||||||||||||||||||||||||||||
| 77 | 2 | ▶ | my ( $self, $plugin_name, $params ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
load_plugin: called CC=4 SCAR=14.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 2 | ▶ | if( ! defined $params->{'module'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | die('no module defined for plugin '.$plugin_name.'!'); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 2 | my $module = $params->{'module'}; | |||||||||||||||||||||||||||
| 82 | 2 | my $plugin_class = $self->plugin_prefix.$module; | |||||||||||||||||||||||||||
| 83 | 2 | my $plugin; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | my $code = "require ".$plugin_class.";"; | |||||||||||||||||||||||||||
| 86 | 2 | eval $code; ## no critic (ProhibitStringyEval) | |||||||||||||||||||||||||||
| 87 | 2 | ▶ | if($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | die('could not load module '.$module.' for plugin '.$plugin_name.': '.$@); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 2 | eval { | |||||||||||||||||||||||||||
| 92 | 2 | $plugin = $plugin_class->new( | |||||||||||||||||||||||||||
| 93 | name => $plugin_name, | ||||||||||||||||||||||||||||
| 94 | vhost_name => $self->vhost_name, | ||||||||||||||||||||||||||||
| 95 | %$params, | ||||||||||||||||||||||||||||
| 96 | ); | ||||||||||||||||||||||||||||
| 97 | 2 | $plugin->init(); | |||||||||||||||||||||||||||
| 98 | }; | ||||||||||||||||||||||||||||
| 99 | 2 | ▶ | if($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | die('could not initialize plugin '.$plugin_name.': '.$@); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 2 | $self->add_plugin($plugin); | |||||||||||||||||||||||||||
| 103 | 2 | return; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | sub new_from_config { | ||||||||||||||||||||||||||||
| 107 | 2 | ▶ | my ( $class, $vhost_name, $config ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new_from_config: called CC=4 SCAR=14.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | my $self = $class->new( vhost_name => $vhost_name ); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 2 | ▶ | if( ! defined $config ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | return( $self ); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 2 | ▶ | if( ref($config) ne 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | die('config must be an hashref!'); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 2 | foreach my $plugin_name ( keys %{$config} ) { | |||||||||||||||||||||||||||
| 120 | $self->load_plugin($plugin_name, | ||||||||||||||||||||||||||||
| 121 | 2 | $config->{$plugin_name} ); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 2 | return $self; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 6 | 1; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =pod | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 NAME | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Mail::MtPolicyd::PluginChain - class for a VirtualHost instance | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 VERSION | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | version 2.01 | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Markus Benning <ich@markusbenning.de> | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | The GNU General Public License, Version 2, June 1991 | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||