2016-07-01 22:02:58 +02:00
|
|
|
# File: AutoRejoin.pm
|
|
|
|
#
|
|
|
|
# Purpose: Auto-rejoin channels after kick or whatever.
|
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
package PBot::Plugin::AutoRejoin;
|
|
|
|
use parent 'PBot::Plugin::Base';
|
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) = @_;
|
2021-07-09 23:39:35 +02:00
|
|
|
|
|
|
|
if (not exists $self->{rejoins}->{$channel}) {
|
|
|
|
$self->{rejoins}->{$channel}->{rejoins} = 0;
|
|
|
|
}
|
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});
|
2021-07-09 23:39:35 +02:00
|
|
|
|
|
|
|
if (not defined $delay) {
|
|
|
|
$delay = $self->{pbot}->{registry}->get_array_value('autorejoin', 'rejoin_delay', $self->{rejoins}->{$channel}->{rejoins});
|
|
|
|
}
|
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) = @_;
|
2021-07-09 23:39:35 +02:00
|
|
|
|
|
|
|
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);
|
2021-07-09 23:39:35 +02:00
|
|
|
return 0 if $self->{pbot}->{channels}->{storage}->get_data($channel, 'noautorejoin');
|
2016-07-01 22:02:58 +02:00
|
|
|
|
2021-07-09 23:39:35 +02:00
|
|
|
if ($target eq $self->{pbot}->{registry}->get_value('irc', 'botnick')) {
|
|
|
|
$self->rejoin_channel($channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2016-07-01 22:02:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub on_part {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $event_type, $event) = @_;
|
2021-07-09 23:39:35 +02:00
|
|
|
|
|
|
|
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);
|
2021-07-09 23:39:35 +02:00
|
|
|
return 0 if $self->{pbot}->{channels}->{storage}->get_data($channel, 'noautorejoin');
|
|
|
|
|
|
|
|
if ($nick eq $self->{pbot}->{registry}->get_value('irc', 'botnick')) {
|
|
|
|
$self->rejoin_channel($channel);
|
|
|
|
}
|
2016-07-01 22:02:58 +02:00
|
|
|
|
2021-07-09 23:39:35 +02:00
|
|
|
return 1;
|
2016-07-01 22:02:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|