blib/lib/Bot/BasicBot/Pluggable/Module/ChanOp.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 131 CC 13 SCAR 27.6
CC
13
Cov
0%
admin
42.0
seen
20.0
chanjoin
12.0
CRAP 15.8
1 package Bot::BasicBot::Pluggable::Module::ChanOp;
2 $Bot::BasicBot::Pluggable::Module::ChanOp::VERSION = '1.11';
3 use base 'Bot::BasicBot::Pluggable::Module';
4 use strict;
5 use warnings;
6  
7 sub init {
8 my $self = shift;
9 $self->config(
10 {
11 user_auto_op => 0,
12 user_flood_control => 0,
13 user_flood_messages => 6,
14 user_flood_seconds => 4
15 }
16 );
17 }
18  
19 sub isop {
20 my ( $self, $channel, $who ) = @_;
21 return unless $channel;
22 $who ||= $self->bot->nick();
23 my $channel_data = $self->bot->channel_data($channel)
24 or return;
25 return $channel_data->{$who}->{op};
26 }
27  
28 sub deop_op {
29 my ( $self, $op, $who, @channels ) = @_;
30 for my $channel (@channels) {
31 if ( $self->isop($channel) ) {
32 $self->bot->mode("$channel $op $who");
33 return "Okay, i $op you in $channel";
34 }
35 else {
36 return "Sorry, i'm not operator in $channel";
37 }
38 }
39 }
40  
41 sub op { shift->deop_op( '+o', @_ ); }
42 sub deop { shift->deop_op( '-o', @_ ); }
43  
44 sub help {
45 return
46 'ChanOp commands need to be adressed in private and after authentication.'
47 . '!op #foo | !deop #foo #bar | !kick #foo user You have been warned ';
48 }
49  
50 sub seen {
51 my ( $self, $message ) = @_;
52 my $who = $message->{who};
53 my $channel = $message->{channel};
54  
55 return if !$self->get('user_flood_control');
56 return if !$self->isop($channel);
57  
58 my $threshold_timestamp = time - $self->get('user_flood_seconds');
59 my $timestamps = $self->{data}->{$channel}->{$who} = [
60 grep { $_ > $threshold_timestamp }
61 @{ $self->{data}->{$channel}->{$who} },
62 time,
63 ];
64 if ( @$timestamps > $self->get('user_flood_messages') ) {
65 my ( $min, $max ) = ( sort { $a <=> $b } @$timestamps )[ 0, -1 ];
66 my $seconds = $max - $min;
67 $self->kick( $channel, $who,
68 "Stop flooding the channel ("
69 . @$timestamps
70 . " messages in $seconds seconds)." );
71 delete $self->{data}->{$channel}->{$who};
72 }
73 }
74  
75 sub admin {
76 my ( $self, $message ) = @_;
77 my $who = $message->{who};
78 if ( $self->authed($who) and $self->private($message) ) {
79 my $body = $message->{body};
80 $body =~ s/(^\s+|\s+$)//g;
81 my ( $command, $rest ) = split(/\s+/, $body, 2 );
82 if ( $command eq '!op' ) {
83 my @channels = split(/\s+/, $rest );
84 return $self->op( $who, @channels );
85 }
86 elsif ( $command eq '!deop' ) {
87 my @channels = split(/\s+/, $rest );
88 return $self->deop( $who, @channels );
89 }
90 elsif ( $command eq '!kick' ) {
91 my ( $channel, $user, $reason ) = split(/\s+/, $rest, 3 );
92 if ( $self->isop($channel) ) {
93 $self->bot->kick( $channel, $who, $reason );
94 return "Okay, kicked $who from $channel.";
95 }
96 else {
97 return "Sorry, i'm not operator in $channel . ";
98 }
99 }
100 }
101 }
102  
103 sub chanjoin {
104 my ( $self, $message ) = @_;
105 if ( $self->get('user_auto_op') ) {
106 my $who = $message->{who};
107 if ( $self->authed($who) ) {
108 my $channel = $message->{channel};
109 $self->op( $who, $channel );
110 }
111 }
112 }
113  
114 ####
115 ## Helper Functions
116 ####
117  
118 sub private {
119 my ( $self, $message ) = @_;
120 return $message->{address} and $message->{channel} eq ' msg ';
121 }
122  
123 sub kick {
124 my $self = shift;
125 $self->bot->kick(@_);
126 }
127  
128 1;
129  
130 __END__
131  
132 =head1 NAME
133  
134 Bot::BasicBot::Pluggable::Module::ChanOp - Channel operator
135  
136 =head1 VERSION
137  
138 version 1.11
139  
140 =head1 SYNOPSIS
141  
142 msg> me: !op #botzone
143 msg> bot: Okay, i +o you in #botzone
144 msg> me: !kick #botzone malice Stay outta here!
145 msg> bot: Okay, i kicked malice from #botzone
146 msg> me: !deop #botzone
147 msg> bot: Okay, i -o you in #botzone
148  
149 =head1 DESCRIPTION
150  
151 This module provides commands to perform basic channel management
152 functions with the help of your bot instance. You can op and deop
153 yourself any time, ask your bot to kick malicious users. It also
154 provides a flood control mechanism, that will kick any user who
155 send more than a specified amount of mesasges in a given time.
156  
157 =head1 VARIABLES
158  
159 =head2 user_auto_op
160  
161 If true, it will op any user who joins a channel and is already
162 authenticated by your bot. Defaults to false.
163  
164 =head2 user_flood_control
165  
166 If true, every user who sends more than C<user_flood_messages> in
167 C<user_flood_seconds> will be kicked from the channel. Defaults to
168 false.
169  
170 =head2 user_flood_messages
171  
172 Maximum numbers of messages per user in C<user_flood_seconds>. Defaults to 6.
173  
174 =head2 user_flood_seconds
175  
176 C<user_flood_seconds>. Defaults to 6.
177  
178 =head1 AUTHOR
179  
180 Mario Domgoergen <mdom@cpan.org>
181  
182 This program is free software; you can redistribute it
183 and/or modify it under the same terms as Perl itself.