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 {
|
2019-05-28 18:19:42 +02:00
|
|
|
if (ref($_[1]) eq 'HASH') {
|
2015-09-07 07:04:46 +02:00
|
|
|
Carp::croak ("Options to " . __FILE__ . " should be key/value pairs, not hash reference");
|
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) = @_;
|
|
|
|
|
2015-09-07 07:04:46 +02:00
|
|
|
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
$self->{channels} = PBot::HashObject->new(pbot => $self->{pbot}, name => 'Channels', filename => delete $conf{filename});
|
|
|
|
$self->load_channels;
|
2010-06-18 05:21:36 +02:00
|
|
|
|
2014-05-18 22:09:05 +02: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);
|
2010-06-18 05:21:36 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $channel) {
|
2015-07-13 11:47:30 +02:00
|
|
|
return "Usage: chanset <channel> [key [value]]";
|
2010-06-18 05:21:36 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2010-06-18 05:21:36 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $channel or not defined $key) {
|
2010-06-23 04:22:08 +02:00
|
|
|
return "Usage: chanunset <channel> <key>";
|
2010-06-18 05:21:36 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
my $hash = {};
|
|
|
|
$hash->{enabled} = 1;
|
|
|
|
$hash->{chanop} = 0;
|
2014-12-18 09:46:13 +01:00
|
|
|
$hash->{permop} = 0;
|
2010-06-18 05:21:36 +02:00
|
|
|
|
2014-06-01 23:32:32 +02:00
|
|
|
return $self->{channels}->add($arguments, $hash);
|
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;
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
foreach my $index (sort keys %{ $self->{channels}->hash }) {
|
2014-03-05 15:28:58 +01:00
|
|
|
$result .= "$index: {";
|
|
|
|
my $comma = ' ';
|
2014-05-18 22:09:05 +02: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;
|
|
|
|
foreach my $chan (keys %{ $self->{pbot}->{channels}->{channels}->hash }) {
|
|
|
|
if ($self->{pbot}->{channels}->{channels}->hash->{$chan}{enabled}) {
|
|
|
|
$chans .= "$chan,";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$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) = @_;
|
|
|
|
return exists $self->{channels}->hash->{$channel} && $self->{channels}->hash->{$channel}->{enabled};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub is_active_op {
|
|
|
|
my ($self, $channel) = @_;
|
|
|
|
return $self->is_active($channel) && $self->{channels}->hash->{$channel}->{chanop};
|
|
|
|
}
|
|
|
|
|
2018-08-06 07:44:46 +02:00
|
|
|
sub get_meta {
|
|
|
|
my ($self, $channel, $key) = @_;
|
|
|
|
my $index = $self->{channels}->find_hash($channel);
|
|
|
|
return undef if not defined $index;
|
|
|
|
return $self->{channels}->{hash}->{$index}->{$key};
|
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub load_channels {
|
|
|
|
my $self = shift;
|
2017-08-06 05:14:49 +02:00
|
|
|
$self->{channels}->load();
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub save_channels {
|
|
|
|
my $self = shift;
|
2017-08-06 05:14:49 +02:00
|
|
|
$self->{channels}->save();
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2010-06-18 05:21:36 +02:00
|
|
|
sub channels {
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = shift;
|
|
|
|
return $self->{channels};
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|