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;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
use Carp ();
|
2010-06-18 05:21:36 +02:00
|
|
|
use PBot::HashObject;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
sub new {
|
2020-01-15 03:10:53 +01:00
|
|
|
Carp::croak ("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH';
|
2010-03-22 08:33:44 +01:00
|
|
|
my ($class, %conf) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2020-01-15 03:10:53 +01:00
|
|
|
$self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2020-01-15 03:10:53 +01:00
|
|
|
$self->{channels} = PBot::HashObject->new(pbot => $self->{pbot}, name => 'Channels', filename => $conf{filename});
|
2014-05-17 22:08:19 +02:00
|
|
|
$self->load_channels;
|
2010-06-18 05:21:36 +02:00
|
|
|
|
2020-01-15 03:10:53 +01:00
|
|
|
$self->{pbot}->{commands}->register(sub { $self->set(@_) }, "chanset", 40);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->unset(@_) }, "chanunset", 40);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->add(@_) }, "chanadd", 40);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->remove(@_) }, "chanrem", 40);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->list(@_) }, "chanlist", 10);
|
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) = @_;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $arguments or not length $arguments) {
|
2014-06-01 23:32:32 +02:00
|
|
|
return "Usage: chanadd <channel>";
|
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) = @_;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $arguments or not length $arguments) {
|
2014-06-01 23:32:32 +02:00
|
|
|
return "Usage: chanrem <channel>";
|
2010-06-18 05:21:36 +02:00
|
|
|
}
|
|
|
|
|
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-01-15 03:10:53 +01:00
|
|
|
foreach my $index (sort keys %{ $self->{channels}->{hash} }) {
|
|
|
|
$result .= "$self->{channels}->{hash}->{$index}->{_name}: {";
|
2014-03-05 15:28:58 +01:00
|
|
|
my $comma = ' ';
|
2020-01-15 03:10:53 +01:00
|
|
|
foreach my $key (sort keys %{ $self->{channels}->{hash}->{$index} }) {
|
|
|
|
$result .= "$comma$key => $self->{channels}->{hash}->{$index}->{$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};
|
|
|
|
my $chans;
|
2020-01-15 03:10:53 +01:00
|
|
|
foreach my $chan (keys %{ $self->{channels}->{hash} }) {
|
|
|
|
if ($self->{channels}->{hash}->{$chan}->{enabled}) {
|
|
|
|
$chans .= "$self->{channels}->{hash}->{$chan}->{_name},";
|
2019-12-22 04:43:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$self->{pbot}->{logger}->log("Joining channels: $chans\n");
|
|
|
|
$self->{pbot}->{chanops}->join_channel($chans);
|
|
|
|
$self->{pbot}->{joined_channels} = 1;
|
|
|
|
}
|
|
|
|
|
2016-07-01 21:58:29 +02:00
|
|
|
sub is_active {
|
|
|
|
my ($self, $channel) = @_;
|
2020-01-15 03:10:53 +01:00
|
|
|
my $lc_channel = lc $channel;
|
|
|
|
return exists $self->{channels}->{hash}->{$lc_channel} && $self->{channels}->{hash}->{$lc_channel}->{enabled};
|
2016-07-01 21:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub is_active_op {
|
|
|
|
my ($self, $channel) = @_;
|
2020-01-15 03:10:53 +01:00
|
|
|
return $self->is_active($channel) && $self->{channels}->{hash}->{lc $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-01-15 03:10:53 +01:00
|
|
|
my $lc_channel = lc $channel;
|
|
|
|
return undef if not exists $self->{channels}->{hash}->{$lc_channel};
|
|
|
|
return $self->{channels}->{hash}->{$lc_channel}->{$key};
|
2018-08-06 07:44:46 +02:00
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub load_channels {
|
2020-01-15 03:10:53 +01:00
|
|
|
my ($self) = @_;
|
|
|
|
$self->{channels}->load;
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub save_channels {
|
2020-01-15 03:10:53 +01:00
|
|
|
my ($self) = @_;
|
|
|
|
$self->{channels}->save;
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|