line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bot::Backbone::SendPolicy; |
2
|
|
|
|
|
|
|
$Bot::Backbone::SendPolicy::VERSION = '0.161950'; |
3
|
4
|
|
|
4
|
|
1994
|
use v5.10; |
|
4
|
|
|
|
|
12
|
|
4
|
4
|
|
|
4
|
|
16
|
use Moose::Role; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
26
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Define policies to prevent flooding and other bot no-nos |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has bot => ( |
10
|
|
|
|
|
|
|
is => 'ro', |
11
|
|
|
|
|
|
|
isa => 'Bot::Backbone::Bot', |
12
|
|
|
|
|
|
|
required => 1, |
13
|
|
|
|
|
|
|
weak_ref => 1, |
14
|
|
|
|
|
|
|
); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
requires 'allow_send'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
1; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
__END__ |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=pod |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=encoding UTF-8 |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 NAME |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Bot::Backbone::SendPolicy - Define policies to prevent flooding and other bot no-nos |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 VERSION |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
version 0.161950 |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 SYNOPSIS |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
package MyBot; |
38
|
|
|
|
|
|
|
use v5.14; |
39
|
|
|
|
|
|
|
use Bot::Backbone; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# This policy prevents the bot from sending more than once every 1.5 seconds |
42
|
|
|
|
|
|
|
send_policy no_flooding => ( |
43
|
|
|
|
|
|
|
MinimumInterval => { interval => 1.5 }, |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# This policy does the same, but discards messages coming too fast |
47
|
|
|
|
|
|
|
send_policy no_flooding_with_prejedice => ( |
48
|
|
|
|
|
|
|
MinimumInterval => { interval => 1.5, discard => 1 }; |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# This policy discards messages repeated within 5 minutes |
52
|
|
|
|
|
|
|
send_policy dont_repeat_yourself => ( |
53
|
|
|
|
|
|
|
MinimumRepeatInterval => { interval => 5*60, discard => 1 }; |
54
|
|
|
|
|
|
|
); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
service jabber_chat => ( |
57
|
|
|
|
|
|
|
service => 'JabberChat', |
58
|
|
|
|
|
|
|
# your other settings... |
59
|
|
|
|
|
|
|
send_policy => 'no_flooding', |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
service group_foo => ( |
63
|
|
|
|
|
|
|
service => 'GroupChat', |
64
|
|
|
|
|
|
|
chat => 'jabber_chat', |
65
|
|
|
|
|
|
|
group => 'foo', |
66
|
|
|
|
|
|
|
# your other settings... |
67
|
|
|
|
|
|
|
send_policy => 'no_flooding_with_prejudice', |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Policy that is shared |
71
|
|
|
|
|
|
|
my $do_not_repeat = [ |
72
|
|
|
|
|
|
|
Bot::Backbone::SendPolicy::MinimumRepeatInterval->new( |
73
|
|
|
|
|
|
|
interval => 300, # 5 * 60 |
74
|
|
|
|
|
|
|
discar => 1, |
75
|
|
|
|
|
|
|
), |
76
|
|
|
|
|
|
|
]; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
service wikipedia => ( |
79
|
|
|
|
|
|
|
service => '.Wikipedia', |
80
|
|
|
|
|
|
|
chat => 'group_foo', |
81
|
|
|
|
|
|
|
# your other settings... |
82
|
|
|
|
|
|
|
send_policies => $do_not_repeat, |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
service google_search => ( |
86
|
|
|
|
|
|
|
service => 'GoogleSearch', |
87
|
|
|
|
|
|
|
chat => 'group_foo', |
88
|
|
|
|
|
|
|
# your other settings... |
89
|
|
|
|
|
|
|
send_policies => $do_not_repeat, |
90
|
|
|
|
|
|
|
); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 DESCRIPTION |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Bots are fun and all, but they can easily become annoying. These controls for |
95
|
|
|
|
|
|
|
preventing a bot from sending too often or repeating itself too frequently can |
96
|
|
|
|
|
|
|
help to minimize that annoyance. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
The purpose of the send policy framework is to allow the bot maintainer to set |
99
|
|
|
|
|
|
|
policies against any service that may call C<send_message>. The policy set |
100
|
|
|
|
|
|
|
against that service may delay any message being sent, cause a message to be |
101
|
|
|
|
|
|
|
discarded, or alter the message. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
The framework is designed to be extensible with a couple very useful policies |
104
|
|
|
|
|
|
|
being provided with the backbone framework. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
See L<Bot::Backbone::SendPolicy::MinimumInterval> and |
107
|
|
|
|
|
|
|
L<Bot::Backbone::SendPolicy::MinimumRepeatInterval>. See L<Bot::Backbone> and |
108
|
|
|
|
|
|
|
L<Bot::Backbone::Service> for more information on how send policies are defined |
109
|
|
|
|
|
|
|
and applied. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
The rest of this docuemntation describes how to build a send policy |
112
|
|
|
|
|
|
|
implementation. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 bot |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
This is a back reference to the bot. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 REQUIRED METHODS |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 allow_send |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my $send_policy = $policy->allow_send({ |
125
|
|
|
|
|
|
|
text => 'some message', |
126
|
|
|
|
|
|
|
... |
127
|
|
|
|
|
|
|
}); |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Given a set of options passed to the C<send_message> method of |
130
|
|
|
|
|
|
|
L<Bot::Backbone::Service::Role::Chat>, return a hash reference containing the |
131
|
|
|
|
|
|
|
instrucitons on what to do with that message. The C<allow_send> method may also |
132
|
|
|
|
|
|
|
modify the passed in options to alter the message being posted. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
The result may contain the following keys: |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=over |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item allow |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
This is a boolean value. If true, the message will be delivered to the chat. If |
141
|
|
|
|
|
|
|
it is false, the message is immediately discarded. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
This must be set. If not set, an exception will be thrown. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=item after |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
This is a numeric value that contains a number of fractional sections to wait |
148
|
|
|
|
|
|
|
utnil the message should be delivered. The message will be put on hold and then |
149
|
|
|
|
|
|
|
delivered after that amount of wait time has passed. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=back |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 AUTHOR |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Andrew Sterling Hanenkamp <hanenkamp@cpan.org> |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
This software is copyright (c) 2016 by Qubling Software LLC. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
162
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |