2010-03-22 08:33:44 +01:00
|
|
|
# File: Channels.pm
|
2010-03-24 07:47:40 +01:00
|
|
|
# Author: pragma_
|
2010-03-22 08:33:44 +01:00
|
|
|
#
|
|
|
|
# Purpose: Manages list of channels and auto-joins.
|
|
|
|
|
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/.
|
|
|
|
|
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
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
use warnings; use strict;
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
2020-01-15 03:10:53 +01:00
|
|
|
$self->{channels} = PBot::HashObject->new(pbot => $self->{pbot}, name => 'Channels', filename => $conf{filename});
|
2020-02-12 06:49:43 +01:00
|
|
|
$self->{channels}->load;
|
2010-06-18 05:21:36 +02:00
|
|
|
|
2020-02-04 02:19:04 +01:00
|
|
|
$self->{pbot}->{commands}->register(sub { $self->join(@_) }, "join", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->part(@_) }, "part", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->set(@_) }, "chanset", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->unset(@_) }, "chanunset", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->add(@_) }, "chanadd", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->remove(@_) }, "chanrem", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->list(@_) }, "chanlist", 1);
|
2020-02-06 02:55:31 +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-01-25 21:28:05 +01:00
|
|
|
sub join {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
foreach my $channel (split /[\s+,]/, $arguments) {
|
|
|
|
$self->{pbot}->{logger}->log("$nick!$user\@$host made me join $channel\n");
|
|
|
|
$self->{pbot}->{chanops}->join_channel($channel);
|
|
|
|
}
|
|
|
|
return "/msg $nick Joining $arguments";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub part {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
$arguments = $from if not $arguments;
|
|
|
|
foreach my $channel (split /[\s+,]/, $arguments) {
|
|
|
|
$self->{pbot}->{logger}->log("$nick!$user\@$host made me part $channel\n");
|
|
|
|
$self->{pbot}->{chanops}->part_channel($channel);
|
|
|
|
}
|
|
|
|
return "/msg $nick Parting $arguments";
|
|
|
|
}
|
|
|
|
|
2010-06-18 05:21:36 +02:00
|
|
|
sub set {
|
2018-08-09 02:38:57 +02:00
|
|
|
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
|
|
|
|
my ($channel, $key, $value) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 3);
|
2020-01-15 03:10:53 +01:00
|
|
|
return "Usage: chanset <channel> [key [value]]" if not defined $channel;
|
2014-05-18 22:09:05 +02:00
|
|
|
return $self->{channels}->set($channel, $key, $value);
|
2010-06-18 05:21:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub unset {
|
2018-08-09 02:38:57 +02:00
|
|
|
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
|
|
|
|
my ($channel, $key) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 2);
|
2020-01-15 03:10:53 +01:00
|
|
|
return "Usage: chanunset <channel> <key>" if not defined $channel or not defined $key;
|
2018-05-12 11:52:10 +02:00
|
|
|
return $self->{channels}->unset($channel, $key);
|
2010-06-18 05:21:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub add {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
2020-02-12 06:49:43 +01:00
|
|
|
return "Usage: chanadd <channel>" if not defined $arguments or not length $arguments;
|
2010-06-18 05:21:36 +02:00
|
|
|
|
2020-01-15 03:10:53 +01:00
|
|
|
my $data = {
|
|
|
|
enabled => 1,
|
|
|
|
chanop => 0,
|
|
|
|
permop => 0
|
|
|
|
};
|
2010-06-18 05:21:36 +02:00
|
|
|
|
2020-01-15 03:10:53 +01:00
|
|
|
return $self->{channels}->add($arguments, $data);
|
2010-06-18 05:21:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub remove {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
2020-02-12 06:49:43 +01:00
|
|
|
return "Usage: chanrem <channel>" if not defined $arguments or not length $arguments;
|
2020-01-24 06:46:51 +01:00
|
|
|
|
|
|
|
# clear unban timeouts
|
2020-02-12 06:49:43 +01:00
|
|
|
if ($self->{pbot}->{chanops}->{unban_timeout}->exists($arguments)) {
|
|
|
|
$self->{pbot}->{chanops}->{unban_timeout}->remove($arguments);
|
2020-01-24 06:46:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# clear unmute timeouts
|
2020-02-12 06:49:43 +01:00
|
|
|
if ($self->{pbot}->{chanops}->{unmute_timeout}->exists($arguments)) {
|
|
|
|
$self->{pbot}->{chanops}->{unmute_timeout}->remove($arguments);
|
2020-01-24 06:46:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# TODO: ignores, etc?
|
2014-06-01 23:32:32 +02:00
|
|
|
return $self->{channels}->remove($arguments);
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2014-03-05 15:28:58 +01:00
|
|
|
sub list {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
my $result;
|
2020-02-12 06:49:43 +01:00
|
|
|
foreach my $channel (sort $self->{channels}->get_keys) {
|
|
|
|
$result .= $self->{channels}->get_data($channel, '_name') . ': {';
|
2014-03-05 15:28:58 +01:00
|
|
|
my $comma = ' ';
|
2020-02-12 06:49:43 +01:00
|
|
|
foreach my $key (sort $self->{channels}->get_keys($channel)) {
|
|
|
|
$result .= "$comma$key => " . $self->{channels}->get_data($channel, $key);
|
2014-03-05 15:28:58 +01:00
|
|
|
$comma = ', ';
|
|
|
|
}
|
|
|
|
$result .= " }\n";
|
|
|
|
}
|
2014-06-01 23:32:32 +02:00
|
|
|
return $result;
|
2014-03-05 15:28:58 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 04:43:05 +01:00
|
|
|
sub autojoin {
|
|
|
|
my ($self) = @_;
|
|
|
|
return if $self->{pbot}->{joined_channels};
|
2020-02-12 06:49:43 +01:00
|
|
|
my $channels;
|
|
|
|
foreach my $channel ($self->{channels}->get_keys) {
|
|
|
|
if ($self->{channels}->get_data($channel, 'enabled')) {
|
|
|
|
$channels .= $self->{channels}->get_data($channel, '_name') . ',';
|
2019-12-22 04:43:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-12 06:49:43 +01:00
|
|
|
$self->{pbot}->{logger}->log("Joining channels: $channels\n");
|
|
|
|
$self->{pbot}->{chanops}->join_channel($channels);
|
2019-12-22 04:43:05 +01:00
|
|
|
$self->{pbot}->{joined_channels} = 1;
|
|
|
|
}
|
|
|
|
|
2016-07-01 21:58:29 +02:00
|
|
|
sub is_active {
|
|
|
|
my ($self, $channel) = @_;
|
2020-02-12 06:49:43 +01:00
|
|
|
# returns undef if channel doesn't exist; otherwise, the value of 'enabled'
|
|
|
|
return $self->{channels}->get_data($channel, 'enabled');
|
2016-07-01 21:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub is_active_op {
|
|
|
|
my ($self, $channel) = @_;
|
2020-02-12 06:49:43 +01:00
|
|
|
return $self->is_active($channel) && $self->{channels}->get_data($channel, 'chanop');
|
2016-07-01 21:58:29 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 07:44:46 +02:00
|
|
|
sub get_meta {
|
|
|
|
my ($self, $channel, $key) = @_;
|
2020-02-12 06:49:43 +01:00
|
|
|
return $self->{channels}->get_data($channel, $key);
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|