line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bot::Backbone::Service::Role::SendPolicy; |
2
|
|
|
|
|
|
|
$Bot::Backbone::Service::Role::SendPolicy::VERSION = '0.161950'; |
3
|
4
|
|
|
4
|
|
2333
|
use v5.10; |
|
4
|
|
|
|
|
10
|
|
4
|
4
|
|
|
4
|
|
14
|
use Moose::Role; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
26
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
with 'Bot::Backbone::Service::Role::Sender'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# ABSTRACT: Provides send policy framework to a service |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has send_policy_name => ( |
12
|
|
|
|
|
|
|
is => 'ro', |
13
|
|
|
|
|
|
|
isa => 'Str', |
14
|
|
|
|
|
|
|
init_arg => 'send_policy', |
15
|
|
|
|
|
|
|
predicate => 'has_send_policy', |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
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
|
|
|
3
|
|
5
|
my $self = shift; |
31
|
3
|
|
|
|
|
121
|
my $send_policy = $self->bot->meta->send_policies->{ $self->send_policy_name }; |
32
|
|
|
|
|
|
|
|
33
|
3
|
50
|
|
|
|
8
|
die "no such send policy as ", $self->send_policy_name, "\n" |
34
|
|
|
|
|
|
|
unless defined $send_policy; |
35
|
|
|
|
|
|
|
|
36
|
3
|
|
|
|
|
73
|
Bot::Backbone::SendPolicy::Aggregate->new( |
37
|
|
|
|
|
|
|
bot => $self->bot, |
38
|
|
|
|
|
|
|
config => $send_policy, |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
requires qw( send_message ); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
around send_message => sub { |
46
|
|
|
|
|
|
|
my ($next, $self, $params) = @_; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $send_policy_result = $params->{send_policy_result} // { allow => 1 }; |
49
|
|
|
|
|
|
|
my $send_policy = $params->{send_policy}; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
$send_policy_result->{after} //= 0; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
_apply_send_policy($send_policy, $send_policy_result, $params) |
54
|
|
|
|
|
|
|
if defined $send_policy; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
_apply_send_policy($self->send_policy, $send_policy_result, $params) |
57
|
|
|
|
|
|
|
if $self->has_send_policy; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
return unless $send_policy_result->{allow}; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# If this is a bare metal chat... then apply any required delay |
62
|
|
|
|
|
|
|
if (($send_policy_result->{after} // 0) > 0 |
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
|
|
|
|
|
|
|
cb => sub { $self->$next($params) }, |
69
|
|
|
|
|
|
|
); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
$self->_enqueue_message($w); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
return; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Allowed and no delays... so GO! |
77
|
|
|
|
|
|
|
$self->$next($params); |
78
|
|
|
|
|
|
|
}; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub _apply_send_policy { |
81
|
897
|
|
|
897
|
|
1068
|
my ($send_policy, $send_policy_result, $options) = @_; |
82
|
|
|
|
|
|
|
|
83
|
897
|
|
|
|
|
3372
|
my $new_result = $send_policy->allow_send($options); |
84
|
|
|
|
|
|
|
|
85
|
897
|
|
66
|
|
|
3455
|
$send_policy_result->{allow} &&= $new_result->{allow}; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
$send_policy_result->{after} = $new_result->{after} |
88
|
897
|
100
|
100
|
|
|
4054
|
if ($new_result->{after} // 0) > $send_policy_result->{after}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
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.161950 |
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 |