2010-03-22 08:33:44 +01:00
|
|
|
# File: Channels.pm
|
|
|
|
#
|
|
|
|
# Purpose: Manages list of channels and auto-joins.
|
|
|
|
|
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-21 07:44:51 +02:00
|
|
|
package PBot::Core::Channels;
|
|
|
|
use parent 'PBot::Core::Class';
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2021-07-21 06:38:07 +02:00
|
|
|
$self->{storage} = PBot::Storage::HashObject->new(pbot => $self->{pbot}, name => 'Channels', filename => $conf{filename});
|
2021-07-09 23:39:35 +02:00
|
|
|
$self->{storage}->load;
|
2014-03-05 15:28:58 +01:00
|
|
|
}
|
|
|
|
|
2020-04-29 06:33:49 +02:00
|
|
|
sub join {
|
|
|
|
my ($self, $channels) = @_;
|
|
|
|
|
2021-07-13 23:02:14 +02:00
|
|
|
return if not $channels;
|
|
|
|
|
2020-04-29 06:33:49 +02:00
|
|
|
$self->{pbot}->{conn}->join($channels);
|
|
|
|
|
|
|
|
foreach my $channel (split /,/, $channels) {
|
|
|
|
$channel = lc $channel;
|
|
|
|
$self->{pbot}->{event_dispatcher}->dispatch_event('pbot.join', {channel => $channel});
|
|
|
|
|
|
|
|
delete $self->{pbot}->{chanops}->{is_opped}->{$channel};
|
|
|
|
delete $self->{pbot}->{chanops}->{op_requested}->{$channel};
|
|
|
|
|
2021-07-09 23:39:35 +02:00
|
|
|
if ($self->{storage}->exists($channel) and $self->{storage}->get_data($channel, 'permop')) {
|
2020-05-04 04:27:04 +02:00
|
|
|
$self->{pbot}->{chanops}->gain_ops($channel);
|
2020-04-29 06:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$self->{pbot}->{conn}->mode($channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub part {
|
|
|
|
my ($self, $channel) = @_;
|
|
|
|
$channel = lc $channel;
|
|
|
|
$self->{pbot}->{event_dispatcher}->dispatch_event('pbot.part', {channel => $channel});
|
|
|
|
$self->{pbot}->{conn}->part($channel);
|
|
|
|
delete $self->{pbot}->{chanops}->{is_opped}->{$channel};
|
|
|
|
delete $self->{pbot}->{chanops}->{op_requested}->{$channel};
|
|
|
|
}
|
|
|
|
|
2019-12-22 04:43:05 +01:00
|
|
|
sub autojoin {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self) = @_;
|
2021-07-13 23:02:14 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return if $self->{pbot}->{joined_channels};
|
2021-07-13 23:02:14 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $channels;
|
2021-07-09 23:39:35 +02:00
|
|
|
foreach my $channel ($self->{storage}->get_keys) {
|
|
|
|
if ($self->{storage}->get_data($channel, 'enabled')) {
|
|
|
|
$channels .= $self->{storage}->get_key_name($channel) . ',';
|
2020-04-29 06:33:49 +02:00
|
|
|
}
|
2019-12-22 04:43:05 +01:00
|
|
|
}
|
2021-07-13 23:02:14 +02:00
|
|
|
|
|
|
|
return if not $channels;
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log("Joining channels: $channels\n");
|
2020-04-29 06:33:49 +02:00
|
|
|
$self->join($channels);
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{joined_channels} = 1;
|
2019-12-22 04:43:05 +01:00
|
|
|
}
|
|
|
|
|
2016-07-01 21:58:29 +02:00
|
|
|
sub is_active {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel) = @_;
|
|
|
|
# returns undef if channel doesn't exist; otherwise, the value of 'enabled'
|
2021-07-09 23:39:35 +02:00
|
|
|
return $self->{storage}->get_data($channel, 'enabled');
|
2016-07-01 21:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub is_active_op {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel) = @_;
|
2021-07-09 23:39:35 +02:00
|
|
|
return $self->is_active($channel) && $self->{storage}->get_data($channel, 'chanop');
|
2016-07-01 21:58:29 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 07:44:46 +02:00
|
|
|
sub get_meta {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel, $key) = @_;
|
2021-07-09 23:39:35 +02:00
|
|
|
return $self->{storage}->get_data($channel, $key);
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|