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.
|
|
|
|
|
|
|
|
package PBot::Channels;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use Carp ();
|
2010-06-18 05:21:36 +02:00
|
|
|
use PBot::HashObject;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
sub new {
|
|
|
|
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 {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
my ($channel, $key, $value) = split / /, $arguments, 3;
|
|
|
|
|
2010-06-18 06:40:12 +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 {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
my ($channel, $key) = split / /, $arguments;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
return "msg $nick " . $self->{channels}->unset($channel, $key);
|
2010-06-18 05:21:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub add {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments) = @_;
|
|
|
|
|
2012-10-05 03:59:04 +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) = @_;
|
|
|
|
|
2012-10-05 03:59:04 +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
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub load_channels {
|
|
|
|
my $self = shift;
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{channels}->load_hash();
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub save_channels {
|
|
|
|
my $self = shift;
|
2010-06-18 05:21:36 +02:00
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{channels}->save_hash();
|
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;
|