2016-07-01 22:02:58 +02:00
|
|
|
# File: AutoRejoin.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
|
|
|
# Purpose: Auto-rejoin channels after kick or whatever.
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2019-09-01 20:01:18 +02:00
|
|
|
package Plugins::AutoRejoin;
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2020-02-09 04:48:05 +01:00
|
|
|
use parent 'Plugins::Plugin';
|
2016-07-01 22:02:58 +02:00
|
|
|
|
2020-02-09 04:48:05 +01:00
|
|
|
use warnings; use strict;
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2016-07-01 22:02:58 +02:00
|
|
|
use Time::HiRes qw/gettimeofday/;
|
|
|
|
use Time::Duration;
|
|
|
|
|
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
|
|
|
$self->{pbot}->{registry}->add_default('array', 'autorejoin', 'rejoin_delay', '900,1800,3600');
|
|
|
|
$self->{pbot}->{event_dispatcher}->register_handler('irc.kick', sub { $self->on_kick(@_) });
|
|
|
|
$self->{pbot}->{event_dispatcher}->register_handler('irc.part', sub { $self->on_part(@_) });
|
|
|
|
$self->{rejoins} = {};
|
2016-07-01 22:02:58 +02:00
|
|
|
}
|
|
|
|
|
2020-02-07 21:24:21 +01:00
|
|
|
sub unload {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self) = @_;
|
|
|
|
$self->{pbot}->{event_dispatcher}->remove_handler('irc.kick');
|
|
|
|
$self->{pbot}->{event_dispatcher}->remove_handler('irc.part');
|
2020-02-07 21:24:21 +01:00
|
|
|
}
|
|
|
|
|
2016-07-01 22:02:58 +02:00
|
|
|
sub rejoin_channel {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel) = @_;
|
|
|
|
$self->{rejoins}->{$channel}->{rejoins} = 0 if not exists $self->{rejoins}->{$channel};
|
2016-07-01 22:02:58 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $delay = $self->{pbot}->{registry}->get_array_value($channel, 'rejoin_delay', $self->{rejoins}->{$channel}->{rejoins});
|
|
|
|
$delay = $self->{pbot}->{registry}->get_array_value('autorejoin', 'rejoin_delay', $self->{rejoins}->{$channel}->{rejoins}) if not defined $delay;
|
2017-11-08 20:17:07 +01:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{interpreter}->add_botcmd_to_command_queue($channel, "join $channel", $delay);
|
2016-07-01 22:02:58 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$delay = duration $delay;
|
|
|
|
$self->{pbot}->{logger}->log("Rejoining $channel in $delay.\n");
|
|
|
|
$self->{rejoins}->{$channel}->{last_rejoin} = gettimeofday;
|
2016-07-01 22:02:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub on_kick {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $event_type, $event) = @_;
|
|
|
|
my ($nick, $user, $host, $target, $channel, $reason) =
|
|
|
|
($event->{event}->nick, $event->{event}->user, $event->{event}->host, $event->{event}->to, $event->{event}->{args}[0], $event->{event}->{args}[1]);
|
2016-07-01 22:02:58 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return 0 if not $self->{pbot}->{channels}->is_active($channel);
|
|
|
|
return 0 if $self->{pbot}->{channels}->{channels}->{hash}->{lc $channel}->{noautorejoin};
|
2016-07-01 22:02:58 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($target eq $self->{pbot}->{registry}->get_value('irc', 'botnick')) { $self->rejoin_channel($channel); }
|
|
|
|
return 0;
|
2016-07-01 22:02:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub on_part {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $event_type, $event) = @_;
|
|
|
|
my ($nick, $user, $host, $channel) = ($event->{event}->nick, $event->{event}->user, $event->{event}->host, $event->{event}->to);
|
2016-07-01 22:02:58 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return 0 if not $self->{pbot}->{channels}->is_active($channel);
|
|
|
|
return 0 if $self->{pbot}->{channels}->{channels}->{hash}->{lc $channel}->{noautorejoin};
|
2016-07-01 22:02:58 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($nick eq $self->{pbot}->{registry}->get_value('irc', 'botnick')) { $self->rejoin_channel($channel); }
|
|
|
|
return 0;
|
2016-07-01 22:02:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|