| 1 | package Bot::Backbone::Service::Role::SendPolicy; | ||||||||||||||||||||||||||||||||
| 2 | 4 | $Bot::Backbone::Service::Role::SendPolicy::VERSION = '0.160630'; | |||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use v5.10; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 4 | with 'Bot::Backbone::Service::Role::Sender'; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | # ABSTRACT: Provides send policy framework to a service | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 4 | has send_policy_name => ( | |||||||||||||||||||||||||||||||
| 12 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 13 | isa => 'Str', | ||||||||||||||||||||||||||||||||
| 14 | init_arg => 'send_policy', | ||||||||||||||||||||||||||||||||
| 15 | predicate => 'has_send_policy', | ||||||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 4 | has send_policy => ( | |||||||||||||||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 21 | does => 'Bot::Backbone::SendPolicy', | ||||||||||||||||||||||||||||||||
| 22 | init_arg => undef, | ||||||||||||||||||||||||||||||||
| 23 | lazy_build => 1, | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | # lazy_build implies (predicate => has_send_policy) | ||||||||||||||||||||||||||||||||
| 26 | predicate => 'has_setup_the_send_policy', | ||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | sub _build_send_policy { | ||||||||||||||||||||||||||||||||
| 30 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_build_send_policy: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 31 | 3 | my $send_policy = $self->bot->meta->send_policies->{ $self->send_policy_name }; | |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 3 | ▶ | die "no such send policy as ", $self->send_policy_name, "\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | unless defined $send_policy; | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 3 | Bot::Backbone::SendPolicy::Aggregate->new( | |||||||||||||||||||||||||||||||
| 37 | bot => $self->bot, | ||||||||||||||||||||||||||||||||
| 38 | config => $send_policy, | ||||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 4 | requires qw( send_message ); | |||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | around send_message => sub { | ||||||||||||||||||||||||||||||||
| 46 | 900 | ▶ | my ($next, $self, $params) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.4
| |||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 900 | ▶ | my $send_policy_result = $params->{send_policy_result} // { allow => 1 }; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{"send_policy_result"} // {"allow", 1}
MC/DC0%0/1
$params->{"send_policy_result"} // {"allow", 1}
$params->{"send_policy_result"}
Truth table50%1/2
A
A= $params->{"send_policy_result"}
| |||||||||||||||||||||||||||||||||
| 49 | 900 | my $send_policy = $params->{send_policy}; | |||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 900 | ▶ | $send_policy_result->{after} //= 0; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$send_policy_result->{"after"} //= 0
MC/DC0%0/1
$send_policy_result->{"after"} //= 0
$send_policy_result->{"after"}
Truth table50%1/2
A
A= $send_policy_result->{"after"}
| |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 900 | ▶ | _apply_send_policy($send_policy, $send_policy_result, $params) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | if defined $send_policy; | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 900 | ▶ | _apply_send_policy($self->send_policy, $send_policy_result, $params) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | if $self->has_send_policy; | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 900 | ▶ | return unless $send_policy_result->{allow}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | # If this is a bare metal chat... then apply any required delay | ||||||||||||||||||||||||||||||||
| 62 | 317 | ▶ | if (($send_policy_result->{after} // 0) > 0 | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
($send_policy_result->{"after"} // 0) > 0 and $self->does("Bot::Backbone::Service::Role::BareMetalChat")
$send_policy_result->{"after"} // 0
MC/DC0%0/2
($send_policy_result->{"after"} // 0) > 0 and $self->does("Bot::Backbone::Service::Role::BareMetalChat")
$send_policy_result->{"after"}
$self->does("Bot::Backbone::Service::Role::BareMetalChat")
Truth table33%1/3
A and B
A= $send_policy_result->{"after"}B= $self->does("Bot::Backbone::Service::Role::BareMetalChat")
| |||||||||||||||||||||||||||||||||
| 63 | and $self->does('Bot::Backbone::Service::Role::BareMetalChat')) { | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | # Setting Timer | ||||||||||||||||||||||||||||||||
| 66 | my $w = AnyEvent->timer( | ||||||||||||||||||||||||||||||||
| 67 | after => $send_policy_result->{after}, | ||||||||||||||||||||||||||||||||
| 68 | 7 | ▶ | cb => sub { $self->$next($params) }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 69 | 298 | ); | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 298 | $self->_enqueue_message($w); | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 298 | return; | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | # Allowed and no delays... so GO! | ||||||||||||||||||||||||||||||||
| 77 | 19 | $self->$next($params); | |||||||||||||||||||||||||||||||
| 78 | 4 | }; | |||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | sub _apply_send_policy { | ||||||||||||||||||||||||||||||||
| 81 | 897 | ▶ | my ($send_policy, $send_policy_result, $options) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_apply_send_policy: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 897 | my $new_result = $send_policy->allow_send($options); | |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 897 | ▶ | $send_policy_result->{allow} &&= $new_result->{allow}; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$send_policy_result->{"allow"} &&= $new_result->{"allow"}
MC/DC50%1/2
$send_policy_result->{"allow"} &&= $new_result->{"allow"}
$send_policy_result->{"allow"}
$new_result->{"allow"}
Truth table66%2/3
A &&= B
A= $send_policy_result->{"allow"}B= $new_result->{"allow"}
| |||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | $send_policy_result->{after} = $new_result->{after} | ||||||||||||||||||||||||||||||||
| 88 | 897 | ▶ | if ($new_result->{after} // 0) > $send_policy_result->{after}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$new_result->{"after"} // 0
MC/DC100%1/1
$new_result->{"after"} // 0
$new_result->{"after"}
Truth table100%2/2
A
A= $new_result->{"after"}
| |||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 4 | 1; | |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | =pod | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | Bot::Backbone::Service::Role::SendPolicy - Provides send policy framework to a service | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | version 0.160630 | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | package Bot::Backbone::Service::RandomGibberish; | ||||||||||||||||||||||||||||||||
| 111 | use v5.14; | ||||||||||||||||||||||||||||||||
| 112 | use Bot::Backbone::Service; | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | with qw( | ||||||||||||||||||||||||||||||||
| 115 | Bot::Backbone::Service::Role::Service | ||||||||||||||||||||||||||||||||
| 116 | Bot::Backbone::Service::Role::SendPolicy | ||||||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | use List::Util qw( shuffle ); | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | # Post to a random chat | ||||||||||||||||||||||||||||||||
| 122 | sub send_message { | ||||||||||||||||||||||||||||||||
| 123 | my ($self, $params) = @_; | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | my @chats = grep { $_->does('Bot::Backbone::Service::Role::Chat') } | ||||||||||||||||||||||||||||||||
| 126 | $self->bot->list_services; | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | my ($chat) = shuffle @chats; | ||||||||||||||||||||||||||||||||
| 129 | $chat->send_message($params); | ||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | # ... whatever else this insane service does ... | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | This role is used to apply send policies to | ||||||||||||||||||||||||||||||||
| 137 | L<Bot::Backbone::Service::Role::Chat>, | ||||||||||||||||||||||||||||||||
| 138 | L<Bot::Backbone::Service::Role::ChatConsumer>, and | ||||||||||||||||||||||||||||||||
| 139 | L<Bot::Backbone::Service::Role::Dispatch> services. If you have a service that | ||||||||||||||||||||||||||||||||
| 140 | is none of those, but would like to have a send policy applied to anything it | ||||||||||||||||||||||||||||||||
| 141 | may send to a chat, you may define a C<send_message> method and then apply this | ||||||||||||||||||||||||||||||||
| 142 | role. | ||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | =head2 send_policy_name | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | This is the name of the send policy to apply to this service. It is set using | ||||||||||||||||||||||||||||||||
| 149 | the C<send_policy> setting in the service configuration. It will be used to set | ||||||||||||||||||||||||||||||||
| 150 | L</send_policy>, if any policy is set. | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | =head2 send_policy | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | This is the L<Bot::Backbone::SendPolicy> that has been selected for this | ||||||||||||||||||||||||||||||||
| 155 | service. | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | =head1 REQUIRED METHODS | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | =head2 send_message | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | This role requires a C<send_mesage> method be present that works just the same | ||||||||||||||||||||||||||||||||
| 162 | as the one required in L<Bot::Backbone::Service::Role::Chat>. This role will | ||||||||||||||||||||||||||||||||
| 163 | modify that method to apply the L</send_policy> to calls to that method. | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | Andrew Sterling Hanenkamp <hanenkamp@cpan.org> | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | This software is copyright (c) 2016 by Qubling Software LLC. | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 174 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||||||||||||||