File: ChanOp.pm

package info (click to toggle)
libbot-basicbot-pluggable-perl 1.30%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 516 kB
  • sloc: perl: 3,153; makefile: 17
file content (183 lines) | stat: -rw-r--r-- 4,901 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package Bot::BasicBot::Pluggable::Module::ChanOp;
$Bot::BasicBot::Pluggable::Module::ChanOp::VERSION = '1.30';
use base 'Bot::BasicBot::Pluggable::Module';
use strict;
use warnings;

sub init {
    my $self = shift;
    $self->config(
        {
            user_auto_op        => 0,
            user_flood_control  => 0,
            user_flood_messages => 6,
            user_flood_seconds  => 4
        }
    );
}

sub isop {
    my ( $self, $channel, $who ) = @_;
    return unless $channel;
    $who ||= $self->bot->nick();
    my $channel_data = $self->bot->channel_data($channel)
        or return;
    return $channel_data->{$who}->{op};
}

sub deop_op {
    my ( $self, $op, $who, @channels ) = @_;
    for my $channel (@channels) {
        if ( $self->isop($channel) ) {
            $self->bot->mode("$channel $op $who");
            return "Okay, i $op you in $channel";
        }
        else {
            return "Sorry, i'm not operator in $channel";
        }
    }
}

sub op   { shift->deop_op( '+o', @_ ); }
sub deop { shift->deop_op( '-o', @_ ); }

sub help {
    return
      'ChanOp commands need to be adressed in private and after authentication.'
      . '!op #foo | !deop #foo #bar | !kick #foo user You have been warned ';
}

sub seen {
    my ( $self, $message ) = @_;
    my $who     = $message->{who};
    my $channel = $message->{channel};

    return if !$self->get('user_flood_control');
    return if !$self->isop($channel);

    my $threshold_timestamp  = time - $self->get('user_flood_seconds');
    my $timestamps = $self->{data}->{$channel}->{$who} = [
        grep  { $_ > $threshold_timestamp }
            @{ $self->{data}->{$channel}->{$who} },
        time,
    ];
    if ( @$timestamps > $self->get('user_flood_messages') ) {
        my ( $min, $max ) = ( sort { $a <=> $b } @$timestamps )[ 0, -1 ];
        my $seconds = $max - $min;
        $self->kick( $channel, $who,
                "Stop flooding the channel ("
                . @$timestamps
                . " messages in $seconds seconds)." );
        delete $self->{data}->{$channel}->{$who};
    }
}

sub admin {
    my ( $self, $message ) = @_;
    my $who = $message->{who};
    if ( $self->authed($who) and $self->private($message) ) {
        my $body = $message->{body};
        $body =~ s/(^\s+|\s+$)//g;
        my ( $command, $rest ) = split(/\s+/, $body, 2 );
        if ( $command eq '!op' ) {
            my @channels = split(/\s+/, $rest );
            return $self->op( $who, @channels );
        }
        elsif ( $command eq '!deop' ) {
            my @channels = split(/\s+/, $rest );
            return $self->deop( $who, @channels );
        }
        elsif ( $command eq '!kick' ) {
            my ( $channel, $user, $reason ) = split(/\s+/, $rest, 3 );
            if ( $self->isop($channel) ) {
                $self->bot->kick( $channel, $who, $reason );
                return "Okay, kicked $who from $channel.";
            }
            else {
                return "Sorry, i'm not operator in $channel . ";
            }
        }
    }
}

sub chanjoin {
    my ( $self, $message ) = @_;
    if ( $self->get('user_auto_op') ) {
        my $who = $message->{who};
        if ( $self->authed($who) ) {
            my $channel = $message->{channel};
            $self->op( $who, $channel );
        }
    }
}

####
## Helper Functions
####

sub private {
    my ( $self, $message ) = @_;
    return $message->{address} and $message->{channel} eq ' msg ';
}

sub kick {
    my $self = shift;
    $self->bot->kick(@_);
}

1;

__END__

=head1 NAME

Bot::BasicBot::Pluggable::Module::ChanOp - Channel operator

=head1 VERSION

version 1.30

=head1 SYNOPSIS

  msg> me: !op #botzone
  msg> bot: Okay, i +o you in #botzone
  msg> me: !kick #botzone malice Stay outta here!
  msg> bot: Okay, i kicked malice from #botzone
  msg> me: !deop #botzone
  msg> bot: Okay, i -o you in #botzone

=head1 DESCRIPTION

This module provides commands to perform basic channel management
functions with the help of your bot instance. You can op and deop
yourself any time, ask your bot to kick malicious users. It also
provides a flood control mechanism, that will kick any user who
send more than a specified amount of mesasges in a given time.

=head1 VARIABLES

=head2 user_auto_op

If true, it will op any user who joins a channel and is already
authenticated by your bot. Defaults to false.

=head2 user_flood_control

If true, every user who sends more than C<user_flood_messages> in
C<user_flood_seconds> will be kicked from the channel. Defaults to
false.

=head2 user_flood_messages

Maximum numbers of messages per user in C<user_flood_seconds>. Defaults to 6.

=head2 user_flood_seconds

C<user_flood_seconds>. Defaults to 6.

=head1 AUTHOR

Mario Domgoergen <mdom@cpan.org>

This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.