line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hubot::Scripts::blacklist; |
2
|
|
|
|
|
|
|
$Hubot::Scripts::blacklist::VERSION = '0.1.9'; |
3
|
1
|
|
|
1
|
|
1509
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
5
|
1
|
|
|
1
|
|
5
|
use Try::Tiny; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
1584
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub load { |
8
|
0
|
|
|
0
|
0
|
|
my ( $class, $robot ) = @_; |
9
|
0
|
|
0
|
|
|
|
$robot->brain->{data}{blacklist}{subscriber} ||= {}; |
10
|
0
|
|
0
|
|
|
|
$robot->brain->{data}{blacklist}{patterns} ||= []; |
11
|
0
|
0
|
|
|
|
|
print STDERR "you have to set env HUBOT_BLACKLIST_MANAGER" |
12
|
|
|
|
|
|
|
unless $ENV{HUBOT_BLACKLIST_MANAGER}; |
13
|
|
|
|
|
|
|
$robot->respond( |
14
|
|
|
|
|
|
|
qr/blacklist add (.*)$/i, |
15
|
|
|
|
|
|
|
sub { |
16
|
0
|
|
|
0
|
|
|
my $msg = shift; |
17
|
|
|
|
|
|
|
|
18
|
0
|
0
|
|
|
|
|
return unless checkPermission( $robot, $msg ); |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
my $pattern = $msg->match->[0]; |
21
|
|
|
|
|
|
|
try { |
22
|
0
|
|
|
|
|
|
qr/$pattern/ |
23
|
0
|
0
|
|
|
|
|
and push @{ $robot->brain->{data}{blacklist}{patterns} }, |
24
|
|
|
|
|
|
|
$pattern; |
25
|
0
|
|
|
|
|
|
$msg->send("OK, added <$pattern> to blacklist"); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
catch { |
28
|
0
|
|
|
|
|
|
$msg->send("Failed to add <$pattern> to blacklist: $_"); |
29
|
0
|
|
|
|
|
|
}; |
30
|
|
|
|
|
|
|
} |
31
|
0
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
$robot->respond( |
34
|
|
|
|
|
|
|
qr/blacklist$/i, |
35
|
|
|
|
|
|
|
sub { |
36
|
0
|
|
|
0
|
|
|
my $msg = shift; |
37
|
0
|
|
|
|
|
|
my $match = $msg->match->[0]; |
38
|
0
|
|
|
|
|
|
my @list = @{ $robot->brain->{data}{blacklist}{patterns} }; |
|
0
|
|
|
|
|
|
|
39
|
0
|
0
|
|
|
|
|
if (@list) { |
40
|
0
|
|
|
|
|
|
my $index = 0; |
41
|
0
|
|
|
|
|
|
map { |
42
|
0
|
|
|
|
|
|
s/^/\# [$index] /; |
43
|
0
|
|
|
|
|
|
$index++; |
44
|
|
|
|
|
|
|
} @list; |
45
|
0
|
|
|
|
|
|
$msg->send(@list); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
else { |
48
|
0
|
|
|
|
|
|
$msg->send('no blacklist'); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
0
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$robot->respond( |
54
|
|
|
|
|
|
|
qr/blacklist del(?:ete)? (\d+)$/i, |
55
|
|
|
|
|
|
|
sub { |
56
|
0
|
|
|
0
|
|
|
my $msg = shift; |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
return unless checkPermission( $robot, $msg ); |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
my $index = $msg->match->[0]; |
61
|
0
|
|
|
|
|
|
my @list = @{ $robot->brain->{data}{blacklist}{patterns} }; |
|
0
|
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
|
if ( $index > @list - 1 ) { |
63
|
0
|
|
|
|
|
|
$msg->send("Can't delete [$index] from blacklist"); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
else { |
66
|
0
|
|
|
|
|
|
my $pattern = splice @list, $index, 1; |
67
|
0
|
|
|
|
|
|
$msg->send("Deleted [$index] - <$pattern> from blacklist"); |
68
|
0
|
|
|
|
|
|
$robot->brain->{data}{blacklist}{patterns} = \@list; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
0
|
|
|
|
|
|
); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$robot->respond( |
74
|
|
|
|
|
|
|
qr/blacklist subscribe$/i, |
75
|
|
|
|
|
|
|
sub { |
76
|
0
|
|
|
0
|
|
|
my $msg = shift; |
77
|
0
|
|
|
|
|
|
my $name = $msg->message->user->{name}; |
78
|
0
|
|
|
|
|
|
$robot->brain->{data}{blacklist}{subscriber}{$name}++; |
79
|
0
|
|
|
|
|
|
$msg->send("OK, $name subscribes blacklist"); |
80
|
|
|
|
|
|
|
} |
81
|
0
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
$robot->respond( |
84
|
|
|
|
|
|
|
qr/blacklist unsubscribe$/i, |
85
|
|
|
|
|
|
|
sub { |
86
|
0
|
|
|
0
|
|
|
my $msg = shift; |
87
|
0
|
|
|
|
|
|
my $name = $msg->message->user->{name}; |
88
|
0
|
|
|
|
|
|
delete $robot->brain->{data}{blacklist}{subscriber}{$name}; |
89
|
0
|
|
|
|
|
|
$msg->send("OK, $name unsubscribes blacklist"); |
90
|
|
|
|
|
|
|
} |
91
|
0
|
|
|
|
|
|
); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
$robot->enter( |
94
|
|
|
|
|
|
|
sub { |
95
|
0
|
|
|
0
|
|
|
my $msg = shift; |
96
|
0
|
|
|
|
|
|
my $user = $msg->message->user->{name}; |
97
|
|
|
|
|
|
|
## support IRC adapter only |
98
|
0
|
0
|
|
|
|
|
if ( 'Hubot::Adapter::Irc' eq ref $robot->adapter ) { |
99
|
0
|
|
|
|
|
|
my $whois = $robot->adapter->whois($user); |
100
|
0
|
|
|
|
|
|
for my $pattern ( |
|
0
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
@{ $robot->brain->{data}{blacklist}{patterns} } ) |
102
|
|
|
|
|
|
|
{ |
103
|
0
|
|
|
|
|
|
my $regex = qr/$pattern/; |
104
|
0
|
0
|
|
|
|
|
if ( $whois =~ m/$regex/ ) { |
105
|
0
|
|
|
|
|
|
my @subscriber = keys |
106
|
0
|
|
|
|
|
|
%{ $robot->brain->{data}{blacklist}{subscriber} }; |
107
|
0
|
|
|
|
|
|
notify( $robot, $msg, $pattern, @subscriber ); |
108
|
0
|
|
|
|
|
|
last; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
0
|
|
|
|
|
|
); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub checkPermission { |
117
|
0
|
|
|
0
|
0
|
|
my ( $robot, $msg ) = @_; |
118
|
0
|
|
0
|
|
|
|
my @manager = split /,/, $ENV{HUBOT_BLACKLIST_MANAGER} || ''; |
119
|
0
|
0
|
|
|
|
|
unless (@manager) { |
120
|
0
|
|
|
|
|
|
$msg->send( "oops! no managers. " |
121
|
|
|
|
|
|
|
. $robot->name |
122
|
|
|
|
|
|
|
. "'s owner has to read the documentation" ); |
123
|
0
|
|
|
|
|
|
return; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
my $name = $msg->message->user->{name}; |
127
|
0
|
0
|
|
|
|
|
unless ( grep {/$name/} @manager ) { |
|
0
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
$msg->send( |
129
|
|
|
|
|
|
|
"you don't have permission. to add blacklist, asking to managers: $ENV{HUBOT_BLACKLIST_MANAGER}" |
130
|
|
|
|
|
|
|
); |
131
|
0
|
|
|
|
|
|
return; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
return 1; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub notify { |
138
|
0
|
|
|
0
|
0
|
|
my ( $robot, $res, $patt, @subs ) = @_; |
139
|
0
|
|
|
|
|
|
for my $sub (@subs) { |
140
|
0
|
|
|
|
|
|
my $to = $robot->userForName($sub); |
141
|
0
|
|
|
|
|
|
$res->whisper( $to, "blacklist[$patt] joined channel" ); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
1; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 NAME |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Hubot::Scripts::blacklist |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 VERSION |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
version 0.1.9 |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 SYNOPSIS |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
hubot blacklist - show blacklist |
158
|
|
|
|
|
|
|
hubot blacklist add <pattern> - add pattern to blacklist |
159
|
|
|
|
|
|
|
hubot blacklist del <index> - delete pattern at blacklist[index] |
160
|
|
|
|
|
|
|
hubot blacklist subscribe - robot will tell you when blacklist entering a room |
161
|
|
|
|
|
|
|
hubot blacklist unsubscribe - robot will not tell you anymore when blacklist entering a room |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 CONFIGURATION |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=over |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item * HUBOT_BLACKLIST_MANAGER |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
manager has permission which can add and delete to blacklist. |
170
|
|
|
|
|
|
|
separate by comma C<,> |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
e.g. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
export HUBOT_BLACKLIST_MANAGER='hshong,aanoaa' |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=back |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 AUTHOR |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Hyungsuk Hong <hshong@perl.kr> |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |