3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-10-01 17:16:39 +02:00

Converted Channels.pm (and all modules using channels object) to use HashObject -- can now add/remove channels and set/unset channel meta-data as user commands

This commit is contained in:
Pragmatic Software 2010-06-18 03:21:36 +00:00
parent e0c9c73acc
commit f29202c99c
5 changed files with 71 additions and 49 deletions

View File

@ -103,7 +103,7 @@ sub check_flood {
$length--;
}
return if ($channel =~ /^#/) and (not exists ${ $self->{pbot}->channels->channels }{$channel} or ${ $self->{pbot}->channels->channels }{$channel}{is_op} == 0);
return if ($channel =~ /^#/) and (not exists $self->{pbot}->channels->channels->hash->{$channel} or $self->{pbot}->channels->channels->hash->{$channel}{chanop} == 0);
if($mode == $self->{FLOOD_JOIN}) {
if($text =~ /^JOIN/) {
@ -114,7 +114,7 @@ sub check_flood {
# if QUIT, then assume they existed on any channel the bot exists on
# this makes it possible to deal with ping timeout quits
foreach my $chan (keys %{ $self->{pbot}->channels->channels }) {
foreach my $chan (keys %{ $self->{pbot}->channels->channels->hash }) {
if(not exists ${ $self->message_history }{$account}{$chan}) {
${ $self->message_history }{$account}{$chan}{offenses} = 0;
${ $self->message_history }{$account}{$chan}{join_watch} = 0;

View File

@ -12,6 +12,7 @@ use vars qw($VERSION);
$VERSION = $PBot::PBot::VERSION;
use Carp ();
use PBot::HashObject;
sub new {
if(ref($_[1]) eq 'HASH') {
@ -33,60 +34,77 @@ sub initialize {
Carp::croak ("Missing pbot reference to Channels");
}
my $channels_file = delete $conf{filename};
my $filename = delete $conf{filename};
$self->{pbot} = $pbot;
$self->{filename} = $channels_file;
$self->{channels} = {};
$self->{channels} = PBot::HashObject->new(pbot => $pbot, name => 'Channels', index_key => 'channel', filename => $filename);
$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;
if(not defined $channel or not defined $key) {
return "/msg $nick Usage: chanset <channel> <key> <value>";
}
return "/msg $nick " . $self->channels->set($channel, $key, $value);
}
sub unset {
my ($self, $from, $nick, $user, $host, $arguments) = @_;
my ($channel, $key) = split / /, $arguments;
if(not defined $channel or not defined $key) {
return "/msg $nick Usage: chanunset <channel> <key>";
}
return "/msg $nick " . $self->channels->unset($channel, $key);
}
sub add {
my ($self, $from, $nick, $user, $host, $arguments) = @_;
if(not defined $arguments) {
return "/msg $nick Usage: chanadd <channel>";
}
my $hash = {};
$hash->{channel} = $arguments;
$hash->{enabled} = 1;
$hash->{chanop} = 0;
return "/msg $nick " . $self->channels->add($hash);
}
sub remove {
my ($self, $from, $nick, $user, $host, $arguments) = @_;
if(not defined $arguments) {
return "/msg $nick Usage: chanrem <channel>";
}
return "/msg $nick " . $self->channels->remove($arguments);
}
sub load_channels {
my $self = shift;
open(FILE, "< $self->{filename}") or Carp::croak "Couldn't open $self->{filename}: $!\n";
my @contents = <FILE>;
close(FILE);
$self->{pbot}->logger->log("Loading channels from $self->{filename} ...\n");
my $i = 0;
foreach my $line (@contents) {
$i++;
chomp $line;
my ($channel, $enabled, $is_op, $showall) = split(/\s+/, $line);
if(not defined $channel || not defined $is_op || not defined $enabled) {
Carp::croak "Syntax error around line $i of $self->{filename}\n";
}
$channel = lc $channel;
if(defined ${ $self->channels }{$channel}) {
Carp::croak "Duplicate channel $channel found in $self->{filename} around line $i\n";
}
${ $self->channels }{$channel}{enabled} = $enabled;
${ $self->channels }{$channel}{is_op} = $is_op;
${ $self->channels }{$channel}{showall} = $showall;
$self->{pbot}->logger->log(" Adding channel $channel (enabled: $enabled, op: $is_op, showall: $showall) ...\n");
}
$self->{pbot}->logger->log("Done.\n");
$self->channels->load_hash();
}
sub save_channels {
my $self = shift;
open(FILE, "> $self->{filename}") or Carp::croak "Couldn't open $self->{filename}: $!\n";
foreach my $channel (keys %{ $self->channels }) {
$channel = lc $channel;
print FILE "$channel ${ $self->channels }{$channel}{enabled} ${ $self->channels }{$channel}{is_op} ${ $self->channels }{$channel}{showall}\n";
}
close(FILE);
$self->channels->save_hash();
}
sub PBot::Channels::channels {
# Carp::cluck "PBot::Channels::channels";
sub channels {
my $self = shift;
return $self->{channels};
}

View File

@ -93,8 +93,8 @@ sub on_notice {
$self->{pbot}->logger->log("Received NOTICE from $nick $host '$text'\n");
if($nick eq "NickServ" && $text =~ m/You are now identified/i) {
foreach my $chan (keys %{ $self->{pbot}->channels->channels }) {
if(${ $self->{pbot}->channels->channels }{$chan}{enabled} != 0) {
foreach my $chan (keys %{ $self->{pbot}->channels->channels->hash }) {
if($self->{pbot}->channels->channels->hash->{$chan}{enabled}) {
$self->{pbot}->logger->log("Joining channel: $chan\n");
$conn->join($chan);
}
@ -140,8 +140,8 @@ sub on_mode {
$self->{pbot}->chanops->{unban_timeout}->{$target}{channel} = $channel;
}
} elsif($mode eq "+e" && $channel eq $self->{pbot}->botnick) {
foreach my $chan (keys %{ $self->{pbot}->channels->channels }) {
if($self->channels->{channels}->{$chan}{enabled} != 0) {
foreach my $chan (keys %{ $self->{pbot}->channels->channels->hash }) {
if($self->channels->channels->hash->{$chan}{enabled}) {
$self->{pbot}->logger->log("Joining channel: $chan\n");
$self->{pbot}->conn->join($chan);
}

View File

@ -13,7 +13,7 @@ use warnings;
# These are set automatically by the build/commit script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 169,
BUILD_REVISION => 171,
BUILD_DATE => "2010-06-17",
};

View File

@ -1 +1,5 @@
#pbot2 1 1 0
-
channel: #pbot2
chanop: 1
enabled: 1
-