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;
|
|
|
|
|
2010-03-24 07:47:40 +01:00
|
|
|
use vars qw($VERSION);
|
|
|
|
$VERSION = $PBot::PBot::VERSION;
|
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 {
|
|
|
|
if(ref($_[1]) eq 'HASH') {
|
|
|
|
Carp::croak ("Options to Commands should be key/value pairs, not hash reference");
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
|
|
|
my $pbot = delete $conf{pbot};
|
|
|
|
if(not defined $pbot) {
|
|
|
|
Carp::croak ("Missing pbot reference to Channels");
|
|
|
|
}
|
|
|
|
|
2010-06-18 05:21:36 +02:00
|
|
|
my $filename = delete $conf{filename};
|
2010-03-22 08:33:44 +01:00
|
|
|
|
|
|
|
$self->{pbot} = $pbot;
|
2010-06-23 08:51:39 +02:00
|
|
|
$self->{channels} = PBot::HashObject->new(pbot => $pbot, name => 'Channels', filename => $filename);
|
2010-06-18 05:21:36 +02:00
|
|
|
|
|
|
|
$pbot->commands->register(sub { $self->set(@_) }, "chanset", 40);
|
|
|
|
$pbot->commands->register(sub { $self->unset(@_) }, "chanunset", 40);
|
|
|
|
$pbot->commands->register(sub { $self->add(@_) }, "chanadd", 40);
|
|
|
|
$pbot->commands->register(sub { $self->remove(@_) }, "chanrem", 40);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2010-06-23 04:22:08 +02:00
|
|
|
return "Usage: chanset <channel> [key <value>]";
|
2010-06-18 05:21:36 +02:00
|
|
|
}
|
|
|
|
|
2010-06-23 04:22:08 +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
|
|
|
}
|
|
|
|
|
2010-06-23 04:22:08 +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) {
|
2010-06-18 05:21:36 +02:00
|
|
|
return "/msg $nick Usage: chanadd <channel>";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $hash = {};
|
|
|
|
$hash->{enabled} = 1;
|
|
|
|
$hash->{chanop} = 0;
|
|
|
|
|
2010-06-23 08:51:39 +02:00
|
|
|
return "/msg $nick " . $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) {
|
2010-06-18 05:21:36 +02:00
|
|
|
return "/msg $nick Usage: chanrem <channel>";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "/msg $nick " . $self->channels->remove($arguments);
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub load_channels {
|
|
|
|
my $self = shift;
|
|
|
|
|
2010-06-18 05:21:36 +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
|
|
|
|
|
|
|
$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;
|