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
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
package PBot::Channels;
|
2020-02-08 20:04:13 +01:00
|
|
|
use parent 'PBot::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-09 23:39:35 +02:00
|
|
|
$self->{storage} = PBot::HashObject->new(pbot => $self->{pbot}, name => 'Channels', filename => $conf{filename});
|
|
|
|
$self->{storage}->load;
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_join(@_) }, "join", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_part(@_) }, "part", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_set(@_) }, "chanset", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_unset(@_) }, "chanunset", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_add(@_) }, "chanadd", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_remove(@_) }, "chanrem", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_list(@_) }, "chanlist", 1);
|
2020-02-15 23:38:32 +01:00
|
|
|
|
|
|
|
$self->{pbot}->{capabilities}->add('admin', 'can-join', 1);
|
|
|
|
$self->{pbot}->{capabilities}->add('admin', 'can-part', 1);
|
|
|
|
$self->{pbot}->{capabilities}->add('admin', 'can-chanlist', 1);
|
2010-06-18 05:21:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_join {
|
|
|
|
my ($self, $context) = @_;
|
|
|
|
foreach my $channel (split /[\s+,]/, $context->{arguments}) {
|
|
|
|
$self->{pbot}->{logger}->log("$context->{hostmask} made me join $channel\n");
|
2020-04-29 06:33:49 +02:00
|
|
|
$self->join($channel);
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2020-05-04 22:21:35 +02:00
|
|
|
return "/msg $context->{nick} Joining $context->{arguments}";
|
2020-01-25 21:28:05 +01:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_part {
|
|
|
|
my ($self, $context) = @_;
|
|
|
|
$context->{arguments} = $context->{from} if not $context->{arguments};
|
|
|
|
foreach my $channel (split /[\s+,]/, $context->{arguments}) {
|
|
|
|
$self->{pbot}->{logger}->log("$context->{hostmask} made me part $channel\n");
|
2020-04-29 06:33:49 +02:00
|
|
|
$self->part($channel);
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2020-05-04 22:21:35 +02:00
|
|
|
return "/msg $context->{nick} Parting $context->{arguments}";
|
2020-01-25 21:28:05 +01:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_set {
|
|
|
|
my ($self, $context) = @_;
|
2020-05-02 05:59:51 +02:00
|
|
|
my ($channel, $key, $value) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 3);
|
2020-02-15 23:38:32 +01:00
|
|
|
return "Usage: chanset <channel> [key [value]]" if not defined $channel;
|
2021-07-09 23:39:35 +02:00
|
|
|
return $self->{storage}->set($channel, $key, $value);
|
2010-06-18 05:21:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_unset {
|
|
|
|
my ($self, $context) = @_;
|
2020-05-02 05:59:51 +02:00
|
|
|
my ($channel, $key) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 2);
|
2020-02-15 23:38:32 +01:00
|
|
|
return "Usage: chanunset <channel> <key>" if not defined $channel or not defined $key;
|
2021-07-09 23:39:35 +02:00
|
|
|
return $self->{storage}->unset($channel, $key);
|
2010-06-18 05:21:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_add {
|
|
|
|
my ($self, $context) = @_;
|
|
|
|
return "Usage: chanadd <channel>" if not length $context->{arguments};
|
2010-06-18 05:21:36 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $data = {
|
|
|
|
enabled => 1,
|
|
|
|
chanop => 0,
|
|
|
|
permop => 0
|
|
|
|
};
|
2010-06-18 05:21:36 +02:00
|
|
|
|
2021-07-09 23:39:35 +02:00
|
|
|
return $self->{storage}->add($context->{arguments}, $data);
|
2010-06-18 05:21:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_remove {
|
|
|
|
my ($self, $context) = @_;
|
|
|
|
return "Usage: chanrem <channel>" if not length $context->{arguments};
|
2020-01-24 06:46:51 +01:00
|
|
|
|
2020-04-29 06:33:49 +02:00
|
|
|
# clear banlists
|
2020-05-30 03:25:55 +02:00
|
|
|
$self->{pbot}->{banlist}->{banlist}->remove($context->{arguments});
|
|
|
|
$self->{pbot}->{banlist}->{quietlist}->remove($context->{arguments});
|
2021-06-22 02:26:24 +02:00
|
|
|
$self->{pbot}->{event_queue}->dequeue_event("unban $context->{arguments} .*");
|
|
|
|
$self->{pbot}->{event_queue}->dequeue_event("unmute $context->{arguments} .*");
|
2020-01-24 06:46:51 +01:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
# TODO: ignores, etc?
|
2021-07-09 23:39:35 +02:00
|
|
|
return $self->{storage}->remove($context->{arguments});
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_list {
|
|
|
|
my ($self, $context) = @_;
|
2020-02-15 23:38:32 +01:00
|
|
|
my $result;
|
2021-07-09 23:39:35 +02:00
|
|
|
foreach my $channel (sort $self->{storage}->get_keys) {
|
|
|
|
$result .= $self->{storage}->get_key_name($channel) . ': {';
|
2020-02-15 23:38:32 +01:00
|
|
|
my $comma = ' ';
|
2021-07-09 23:39:35 +02:00
|
|
|
foreach my $key (sort $self->{storage}->get_keys($channel)) {
|
|
|
|
$result .= "$comma$key => " . $self->{storage}->get_data($channel, $key);
|
2020-02-15 23:38:32 +01:00
|
|
|
$comma = ', ';
|
|
|
|
}
|
|
|
|
$result .= " }\n";
|
2014-03-05 15:28:58 +01:00
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
return $result;
|
2014-03-05 15:28:58 +01:00
|
|
|
}
|
|
|
|
|
2020-04-29 06:33:49 +02:00
|
|
|
sub join {
|
|
|
|
my ($self, $channels) = @_;
|
|
|
|
|
|
|
|
$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) = @_;
|
|
|
|
return if $self->{pbot}->{joined_channels};
|
|
|
|
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
|
|
|
}
|
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;
|