From 02f93c872109129a15f36965cc38326f8a97f4d5 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sat, 8 Feb 2020 11:04:13 -0800 Subject: [PATCH] Significant refactor and clean-up of PBot class instantiation --- PBot/AntiFlood.pm | 17 ++-------------- PBot/AntiSpam.pm | 19 ++---------------- PBot/BanTracker.pm | 20 ++++--------------- PBot/BlackList.pm | 15 ++------------ PBot/Capabilities.pm | 17 ++-------------- PBot/ChanOpCommands.pm | 16 ++------------- PBot/ChanOps.pm | 14 ++----------- PBot/Channels.pm | 18 ++--------------- PBot/Class.pm | 37 +++++++++++++++++++++++++++++++++++ PBot/Commands.pm | 27 +++++-------------------- PBot/DualIndexHashObject.pm | 15 ++------------ PBot/EventDispatcher.pm | 18 ++++------------- PBot/FactoidCommands.pm | 16 ++------------- PBot/FactoidModuleLauncher.pm | 15 ++------------ PBot/Factoids.pm | 19 ++---------------- PBot/FuncCommand.pm | 15 ++------------ PBot/HashObject.pm | 15 ++------------ PBot/IRCHandlers.pm | 16 ++------------- PBot/IgnoreList.pm | 16 ++------------- PBot/IgnoreListCommands.pm | 15 ++------------ PBot/Interpreter.pm | 19 +++--------------- PBot/LagChecker.pm | 17 ++-------------- PBot/Logger.pm | 14 ++++--------- PBot/MessageHistory.pm | 15 ++------------ PBot/MessageHistory_SQLite.pm | 15 ++------------ PBot/NickList.pm | 16 ++------------- PBot/PBot.pm | 21 ++++++++++---------- PBot/Plugins.pm | 15 ++------------ PBot/Refresher.pm | 16 ++------------- PBot/Registerable.pm | 22 +++------------------ PBot/Registry.pm | 17 ++-------------- PBot/RegistryCommands.pm | 16 ++------------- PBot/SQLiteLogger.pm | 13 +++--------- PBot/SQLiteLoggerLayer.pm | 8 +++----- PBot/SelectHandler.pm | 15 ++------------ PBot/StdinReader.pm | 16 ++------------- PBot/Timer.pm | 27 +++++++------------------ PBot/Users.pm | 18 ++--------------- PBot/VERSION.pm | 20 +++++-------------- PBot/WebPaste.pm | 24 +++-------------------- 40 files changed, 146 insertions(+), 558 deletions(-) create mode 100644 PBot/Class.pm diff --git a/PBot/AntiFlood.pm b/PBot/AntiFlood.pm index c0dc9e81..9c22eab6 100644 --- a/PBot/AntiFlood.pm +++ b/PBot/AntiFlood.pm @@ -12,34 +12,21 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::AntiFlood; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use feature 'switch'; no if $] >= 5.018, warnings => "experimental::smartmatch"; -use PBot::DualIndexHashObject; - use Time::HiRes qw(gettimeofday tv_interval); use Time::Duration; use POSIX qw/strftime/; use Text::CSV; -use Carp (); - -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); # flags for 'validated' field $self->{NICKSERV_VALIDATED} = (1<<0); diff --git a/PBot/AntiSpam.pm b/PBot/AntiSpam.pm index bac871ff..eec4c6c8 100644 --- a/PBot/AntiSpam.pm +++ b/PBot/AntiSpam.pm @@ -8,34 +8,19 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::AntiSpam; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use feature 'switch'; no if $] >= 5.018, warnings => "experimental::smartmatch"; -use PBot::DualIndexHashObject; - -use Carp (); use Time::HiRes qw(gettimeofday); use POSIX qw/strftime/; -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); - my $filename = $conf{spamkeywords_file} // $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/spam_keywords'; $self->{keywords} = PBot::DualIndexHashObject->new(name => 'SpamKeywords', filename => $filename, pbot => $self->{pbot}); $self->{keywords}->load; diff --git a/PBot/BanTracker.pm b/PBot/BanTracker.pm index d5adca56..a406adf1 100644 --- a/PBot/BanTracker.pm +++ b/PBot/BanTracker.pm @@ -11,32 +11,18 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::BanTracker; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use Time::HiRes qw/gettimeofday/; use Time::Duration; use Data::Dumper; $Data::Dumper::Sortkeys = 1; -use Carp (); - -sub new { - Carp::croak("Options to BanTracker should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} sub initialize { my ($self, %conf) = @_; - - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to BanTracker"); - $self->{banlist} = {}; - $self->{pbot}->{registry}->add_default('text', 'bantracker', 'chanserv_ban_timeout', '604800'); $self->{pbot}->{registry}->add_default('text', 'bantracker', 'mute_timeout', '604800'); $self->{pbot}->{registry}->add_default('text', 'bantracker', 'debug', '0'); @@ -46,6 +32,8 @@ sub initialize { $self->{pbot}->{event_dispatcher}->register_handler('irc.endofnames', sub { $self->get_banlist(@_) }); $self->{pbot}->{event_dispatcher}->register_handler('irc.banlist', sub { $self->on_banlist_entry(@_) }); $self->{pbot}->{event_dispatcher}->register_handler('irc.quietlist', sub { $self->on_quietlist_entry(@_) }); + + $self->{banlist} = {}; } sub dumpbans { diff --git a/PBot/BlackList.pm b/PBot/BlackList.pm index a9f39855..6c35a242 100644 --- a/PBot/BlackList.pm +++ b/PBot/BlackList.pm @@ -8,29 +8,18 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::BlackList; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use feature 'switch'; no if $] >= 5.018, warnings => "experimental::smartmatch"; -use Carp (); use Time::HiRes qw(gettimeofday); -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); $self->{filename} = $conf{filename}; $self->{blacklist} = {}; $self->{pbot}->{commands}->register(sub { $self->blacklist(@_) }, "blacklist", 1); diff --git a/PBot/Capabilities.pm b/PBot/Capabilities.pm index 6748b988..a860ea43 100644 --- a/PBot/Capabilities.pm +++ b/PBot/Capabilities.pm @@ -3,34 +3,21 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Capabilities; +use parent 'PBot::Class'; # purpose: provides interface to set/remove/modify/query user capabilities. # # Examples: # -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use feature 'switch'; no if $] >= 5.018, warnings => "experimental::smartmatch"; -use PBot::HashObject; -use Carp (); - -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref $_[1] eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); my $filename = $conf{filename} // $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/capabilities'; $self->{caps} = PBot::HashObject->new(name => 'Capabilities', filename => $filename, pbot => $self->{pbot}); $self->{caps}->load; diff --git a/PBot/ChanOpCommands.pm b/PBot/ChanOpCommands.pm index 301047f4..ed6debc3 100644 --- a/PBot/ChanOpCommands.pm +++ b/PBot/ChanOpCommands.pm @@ -8,28 +8,16 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::ChanOpCommands; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; -use Carp (); use Time::Duration; use Time::HiRes qw/gettimeofday/; -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); - # register commands $self->{pbot}->{commands}->register(sub { $self->ban_user(@_) }, "ban", 1); $self->{pbot}->{commands}->register(sub { $self->unban_user(@_) }, "unban", 1); diff --git a/PBot/ChanOps.pm b/PBot/ChanOps.pm index 03d76d98..b6829e3a 100644 --- a/PBot/ChanOps.pm +++ b/PBot/ChanOps.pm @@ -8,27 +8,17 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::ChanOps; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use PBot::ChanOpCommands; use Time::HiRes qw(gettimeofday); use Time::Duration qw(concise duration); -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to ChanOps"); $self->{unban_timeout} = PBot::DualIndexHashObject->new( pbot => $self->{pbot}, diff --git a/PBot/Channels.pm b/PBot/Channels.pm index b87b1364..52d30b95 100644 --- a/PBot/Channels.pm +++ b/PBot/Channels.pm @@ -8,27 +8,13 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Channels; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; -use Carp (); -use PBot::HashObject; - -sub new { - Carp::croak ("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); - $self->{channels} = PBot::HashObject->new(pbot => $self->{pbot}, name => 'Channels', filename => $conf{filename}); $self->load_channels; diff --git a/PBot/Class.pm b/PBot/Class.pm new file mode 100644 index 00000000..673857db --- /dev/null +++ b/PBot/Class.pm @@ -0,0 +1,37 @@ +# 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/. + +package PBot::Class; + +# purpose: base class for all PBot classes +# +# This prevents each PBot class from needing to define the new() subroutine +# and such boilerplate. + +use warnings; +use strict; + +sub new { + my ($proto, %conf) = @_; + my $class = ref($proto) || $proto; + my $self = bless {}, $class; + + if (not exists $conf{pbot}) { + my ($package, $filename, $line) = caller(0); + my (undef, undef, undef, $subroutine) = caller(1); + Carp::croak("Missing pbot reference to " . $class . ", created by $subroutine at $filename:$line"); + } + + $self->{pbot} = $conf{pbot}; + $self->initialize(%conf); + return $self; +} + +sub initialize { + my ($package, $filename, $line) = caller(0); + my (undef, undef, undef, $subroutine) = caller(1); + Carp::croak("Missing initialize subroutine, created by $subroutine at $filename:$line"); +} + +1; diff --git a/PBot/Commands.pm b/PBot/Commands.pm index ed666cfd..173e62b1 100644 --- a/PBot/Commands.pm +++ b/PBot/Commands.pm @@ -10,30 +10,16 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Commands; +use parent 'PBot::Class', 'PBot::Registerable'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; -use base 'PBot::Registerable'; - -use Carp (); -use PBot::HashObject; use Time::Duration qw/duration/; -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->SUPER::initialize(%conf); - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); + $self->PBot::Registerable::initialize(%conf); $self->{metadata} = PBot::HashObject->new(pbot => $self->{pbot}, name => 'Commands', filename => $conf{filename}); $self->load_metadata; @@ -49,12 +35,9 @@ sub initialize { sub register { my ($self, $subref, $name, $requires_cap) = @_; + Carp::croak("Missing parameters to Commands::register") if not defined $subref or not defined $name; - if (not defined $subref or not defined $name) { - Carp::croak("Missing parameters to Commands::register"); - } - - my $ref = $self->SUPER::register($subref); + my $ref = $self->PBot::Registerable::register($subref); $ref->{name} = lc $name; $ref->{requires_cap} = $requires_cap // 0; diff --git a/PBot/DualIndexHashObject.pm b/PBot/DualIndexHashObject.pm index 5b981a2c..4daf5615 100644 --- a/PBot/DualIndexHashObject.pm +++ b/PBot/DualIndexHashObject.pm @@ -12,29 +12,18 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::DualIndexHashObject; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use Text::Levenshtein qw(fastdistance); use JSON; -use Carp (); - -sub new { - Carp::croak("Options to DualIndexHashObject should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} sub initialize { my ($self, %conf) = @_; $self->{name} = $conf{name} // 'Dual Index hash object'; $self->{filename} = $conf{filename} // Carp::carp("Missing filename to DualIndexHashObject, will not be able to save to or load from file."); - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); $self->{hash} = {}; } diff --git a/PBot/EventDispatcher.pm b/PBot/EventDispatcher.pm index 4cdbed0f..3c87e873 100644 --- a/PBot/EventDispatcher.pm +++ b/PBot/EventDispatcher.pm @@ -3,26 +3,15 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::EventDispatcher; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use IO::Select; -use Carp (); - -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference in " . __FILE__); $self->{handlers} = { any => [] }; } @@ -60,7 +49,8 @@ sub dispatch_event { for (my $i = 0; $i < @{$self->{handlers}->{$event_type}}; $i++) { my $ref = @{$self->{handlers}->{$event_type}}[$i]; my ($handler, $info) = ($ref->[0], $ref->[1]); - $self->{pbot}->{logger}->log("Dispatching $event_type to handler $info\n") if $self->{pbot}->{registry}->get_value('eventdispatcher', 'debug') > 1; + my $debug = $self->{pbot}->{registry}->get_value('eventdispatcher', 'debug') // 0; + $self->{pbot}->{logger}->log("Dispatching $event_type to handler $info\n") if $debug > 1; eval { $ret = $handler->($event_type, $event_data); diff --git a/PBot/FactoidCommands.pm b/PBot/FactoidCommands.pm index 335a4272..aa388cbf 100644 --- a/PBot/FactoidCommands.pm +++ b/PBot/FactoidCommands.pm @@ -8,13 +8,11 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::FactoidCommands; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; -use Carp (); use Time::Duration; use Time::HiRes qw(gettimeofday); use Getopt::Long qw(GetOptionsFromString); @@ -46,18 +44,8 @@ our %factoid_metadata_capabilities = ( # all others are allowed to be factset by anybody ); -sub new { - Carp::croak("Options to FactoidCommands should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to FactoidCommands"); - $self->{pbot}->{registry}->add_default('text', 'general', 'module_repo', $conf{module_repo} // 'https://github.com/pragma-/pbot/blob/master/modules/'); $self->{pbot}->{commands}->register(sub { $self->factadd(@_) }, "learn", 0); diff --git a/PBot/FactoidModuleLauncher.pm b/PBot/FactoidModuleLauncher.pm index 647502e2..1848d49e 100644 --- a/PBot/FactoidModuleLauncher.pm +++ b/PBot/FactoidModuleLauncher.pm @@ -8,14 +8,12 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::FactoidModuleLauncher; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use POSIX qw(WNOHANG); -use Carp (); use Text::Balanced qw(extract_delimited); use JSON; use IPC::Run qw/run timeout/; @@ -24,17 +22,8 @@ use Encode; # automatically reap children processes in background $SIG{CHLD} = sub { while (waitpid(-1, WNOHANG) > 0) {} }; -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); } sub execute_module { diff --git a/PBot/Factoids.pm b/PBot/Factoids.pm index 5e21b236..944efca3 100644 --- a/PBot/Factoids.pm +++ b/PBot/Factoids.pm @@ -8,10 +8,9 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Factoids; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use feature 'switch'; @@ -20,7 +19,6 @@ no if $] >= 5.018, warnings => "experimental::smartmatch"; use HTML::Entities; use Time::HiRes qw(gettimeofday); use Time::Duration qw(duration); -use Carp (); use POSIX qw(strftime); use Text::ParseWords; use JSON; @@ -32,20 +30,9 @@ use PBot::DualIndexHashObject; use PBot::Utils::Indefinite; use PBot::Utils::ValidateString; -sub new { - Carp::croak("Options to Factoids should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - my $filename = $conf{filename}; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); - $self->{factoids} = PBot::DualIndexHashObject->new(name => 'Factoids', filename => $filename, pbot => $self->{pbot}); $self->{pbot} = $self->{pbot}; @@ -58,13 +45,11 @@ sub initialize { $self->{pbot}->{registry}->add_default('text', 'factoids', 'max_channel_length', 20); $self->{pbot}->{atexit}->register(sub { $self->save_factoids; return; }); - $self->load_factoids; } sub load_factoids { my $self = shift; - $self->{factoids}->load; my ($text, $regex, $modules); diff --git a/PBot/FuncCommand.pm b/PBot/FuncCommand.pm index a4be3bde..9f35d79d 100644 --- a/PBot/FuncCommand.pm +++ b/PBot/FuncCommand.pm @@ -20,24 +20,13 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::FuncCommand; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; -use Carp (); - -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); $self->{pbot}->{commands}->register(sub { $self->do_func(@_) }, 'func', 0); $self->init_funcs; } diff --git a/PBot/HashObject.pm b/PBot/HashObject.pm index 7cf99a31..c4494716 100644 --- a/PBot/HashObject.pm +++ b/PBot/HashObject.pm @@ -11,29 +11,18 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::HashObject; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use Text::Levenshtein qw(fastdistance); -use Carp (); use JSON; -sub new { - Carp::croak("Options to HashObject should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; $self->{name} = $conf{name} // 'hash object'; $self->{filename} = $conf{filename} // Carp::carp("Missing filename to HashObject, will not be able to save to or load from file."); - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); $self->{hash} = {}; } diff --git a/PBot/IRCHandlers.pm b/PBot/IRCHandlers.pm index 36f73946..7d1de18d 100644 --- a/PBot/IRCHandlers.pm +++ b/PBot/IRCHandlers.pm @@ -8,29 +8,17 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::IRCHandlers; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; -use Carp(); use Time::HiRes qw(gettimeofday); use Data::Dumper; $Data::Dumper::Sortkeys = 1; -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot parameter to " . __FILE__); - $self->{pbot}->{event_dispatcher}->register_handler('irc.welcome', sub { $self->on_connect(@_) }); $self->{pbot}->{event_dispatcher}->register_handler('irc.disconnect', sub { $self->on_disconnect(@_) }); $self->{pbot}->{event_dispatcher}->register_handler('irc.motd', sub { $self->on_motd(@_) }); diff --git a/PBot/IgnoreList.pm b/PBot/IgnoreList.pm index b806595a..ef5aaa28 100644 --- a/PBot/IgnoreList.pm +++ b/PBot/IgnoreList.pm @@ -8,27 +8,16 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::IgnoreList; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use PBot::IgnoreListCommands; use Time::HiRes qw(gettimeofday); -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); $self->{filename} = $conf{filename}; $self->{ignore_list} = {}; @@ -36,7 +25,6 @@ sub initialize { $self->{last_timestamp} = {}; $self->{commands} = PBot::IgnoreListCommands->new(pbot => $self->{pbot}); - $self->load_ignores(); $self->{pbot}->{timer}->register(sub { $self->check_ignore_timeouts }, 10); diff --git a/PBot/IgnoreListCommands.pm b/PBot/IgnoreListCommands.pm index 718834ee..59697e2c 100644 --- a/PBot/IgnoreListCommands.pm +++ b/PBot/IgnoreListCommands.pm @@ -8,27 +8,16 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::IgnoreListCommands; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use Time::HiRes qw(gettimeofday); use Time::Duration; -use Carp (); - -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); $self->{pbot}->{commands}->register(sub { $self->ignore_user(@_) }, "ignore", 1); $self->{pbot}->{commands}->register(sub { $self->unignore_user(@_) }, "unignore", 1); $self->{pbot}->{capabilities}->add('admin', 'can-ignore', 1); diff --git a/PBot/Interpreter.pm b/PBot/Interpreter.pm index 3754dee6..aad18f44 100644 --- a/PBot/Interpreter.pm +++ b/PBot/Interpreter.pm @@ -8,32 +8,19 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Interpreter; +use parent 'PBot::Class', 'PBot::Registerable'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; -use base 'PBot::Registerable'; - use Time::HiRes qw/gettimeofday/; use Time::Duration; -use Carp (); use PBot::Utils::ValidateString; -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->SUPER::initialize(%conf); - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); + $self->PBot::Registerable::initialize(%conf); $self->{pbot}->{registry}->add_default('text', 'general', 'compile_blocks', $conf{compile_blocks} // 1); $self->{pbot}->{registry}->add_default('array', 'general', 'compile_blocks_channels', $conf{compile_blocks_channels} // '.*'); diff --git a/PBot/LagChecker.pm b/PBot/LagChecker.pm index c5521a0b..1acc04fb 100644 --- a/PBot/LagChecker.pm +++ b/PBot/LagChecker.pm @@ -9,29 +9,16 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::LagChecker; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; -use feature 'switch'; use Time::HiRes qw(gettimeofday tv_interval); use Time::Duration; -use Carp (); - -sub new { - Carp::croak("Options to LagChecker should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to LagChecker"); - $self->{lag_average} = undef; # average of entries in lag history, in seconds $self->{lag_string} = undef; # string representation of lag history and lag average $self->{lag_history} = []; # history of previous PING/PONG timings diff --git a/PBot/Logger.pm b/PBot/Logger.pm index 2eeedab5..602795d5 100644 --- a/PBot/Logger.pm +++ b/PBot/Logger.pm @@ -3,22 +3,16 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Logger; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use Scalar::Util qw/openhandle/; use File::Basename; -use Carp (); -sub new { - Carp::croak("Options to Logger should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - - $self->{pbot} = $conf{pbot} // Carp::croak "Missing pbot reference to " . __FILE__; +sub initialize { + my ($self, %conf) = @_; $self->{logfile} = $conf{filename} // Carp::croak "Missing logfile parameter in " . __FILE__; $self->{start} = time; diff --git a/PBot/MessageHistory.pm b/PBot/MessageHistory.pm index a9c11c73..c6ab329b 100644 --- a/PBot/MessageHistory.pm +++ b/PBot/MessageHistory.pm @@ -12,30 +12,19 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::MessageHistory; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use Getopt::Long qw(GetOptionsFromString); use Time::HiRes qw(gettimeofday tv_interval); use Time::Duration; -use Carp (); use PBot::MessageHistory_SQLite; -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); $self->{filename} = $conf{filename} // $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/message_history.sqlite3'; $self->{database} = PBot::MessageHistory_SQLite->new(pbot => $self->{pbot}, filename => $self->{filename}); diff --git a/PBot/MessageHistory_SQLite.pm b/PBot/MessageHistory_SQLite.pm index b1f267e6..9289e7c4 100644 --- a/PBot/MessageHistory_SQLite.pm +++ b/PBot/MessageHistory_SQLite.pm @@ -8,10 +8,9 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::MessageHistory_SQLite; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use DBI; @@ -21,18 +20,8 @@ use Text::CSV; use Text::Levenshtein qw/fastdistance/; use Time::Duration; -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference in " . __FILE__); $self->{filename} = $conf{filename} // $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/message_history.sqlite3'; $self->{new_entries} = 0; diff --git a/PBot/NickList.pm b/PBot/NickList.pm index 32af664e..06eb9ca3 100644 --- a/PBot/NickList.pm +++ b/PBot/NickList.pm @@ -10,30 +10,18 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::NickList; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use Text::Levenshtein qw/fastdistance/; use Data::Dumper; $Data::Dumper::Sortkeys = 1; -use Carp (); use Time::HiRes qw/gettimeofday/; -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref $_[1] eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); - $self->{nicklist} = {}; $self->{pbot}->{registry}->add_default('text', 'nicklist', 'debug', '0'); diff --git a/PBot/PBot.pm b/PBot/PBot.pm index b77db07f..417c2788 100644 --- a/PBot/PBot.pm +++ b/PBot/PBot.pm @@ -9,9 +9,7 @@ package PBot::PBot; -use strict; -use warnings; - +use strict; use warnings; use feature 'unicode_strings'; # unbuffer stdout @@ -20,6 +18,8 @@ STDOUT->autoflush(1); use Carp (); use PBot::Logger; use PBot::VERSION; +use PBot::HashObject; +use PBot::DualIndexHashObject; use PBot::Registry; use PBot::Capabilities; use PBot::SelectHandler; @@ -49,11 +49,9 @@ use PBot::Utils::ParseDate; use PBot::FuncCommand; sub new { - Carp::croak("Options to PBot should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; + my ($proto, %conf) = @_; + my $class = ref($proto) || $proto; my $self = bless {}, $class; - $self->{atexit} = PBot::Registerable->new(%conf); - $self->register_signal_handlers; $self->initialize(%conf); return $self; } @@ -62,6 +60,9 @@ sub initialize { my ($self, %conf) = @_; $self->{startup_timestamp} = time; + $self->{atexit} = PBot::Registerable->new(%conf, pbot => $self); + $self->register_signal_handlers; + my $data_dir = $conf{data_dir}; my $module_dir = $conf{module_dir}; my $plugin_dir = $conf{plugin_dir}; @@ -114,7 +115,7 @@ sub initialize { $self->{commands}->register(sub { $self->{capabilities}->capcmd(@_) }, "cap"); # prepare the version - $self->{version} = PBot::VERSION->new(pbot => $self, %conf); + $self->{version} = PBot::VERSION->new(pbot => $self, %conf); $self->{logger}->log($self->{version}->version . "\n"); $self->{logger}->log("Args: @ARGV\n") if @ARGV; @@ -123,8 +124,8 @@ sub initialize { $self->{logger}->log("module_dir: $module_dir\n"); $self->{logger}->log("plugin_dir: $plugin_dir\n"); - $self->{timer} = PBot::Timer->new(timeout => 10, %conf); - $self->{func_cmd} = PBot::FuncCommand->new(pbot => $self, %conf); + $self->{timer} = PBot::Timer->new(pbot => $self, timeout => 10, %conf); + $self->{func_cmd} = PBot::FuncCommand->new(pbot => $self, %conf); $self->{refresher} = PBot::Refresher->new(pbot => $self); # create registry and set some defaults diff --git a/PBot/Plugins.pm b/PBot/Plugins.pm index 4fe578ae..c8ec4d31 100644 --- a/PBot/Plugins.pm +++ b/PBot/Plugins.pm @@ -8,26 +8,15 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Plugins; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use File::Basename; -use Carp (); - -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); $self->{plugins} = {}; $self->{pbot}->{commands}->register(sub { $self->load_cmd(@_) }, "plug", 1); $self->{pbot}->{commands}->register(sub { $self->unload_cmd(@_) }, "unplug", 1); diff --git a/PBot/Refresher.pm b/PBot/Refresher.pm index 6775ccce..d73b2abf 100644 --- a/PBot/Refresher.pm +++ b/PBot/Refresher.pm @@ -10,33 +10,21 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Refresher; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use Module::Refresh; -use Carp (); - -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); $self->{refresher} = Module::Refresh->new; $self->{pbot}->{commands}->register(sub { $self->refresh(@_) }, "refresh", 1); } sub refresh { my ($self, $from, $nick, $user, $host, $arguments) = @_; - my $result = eval { if (not $arguments) { $self->{pbot}->{logger}->log("Refreshing all modified modules\n"); diff --git a/PBot/Registerable.pm b/PBot/Registerable.pm index 1f6d586f..79a289eb 100644 --- a/PBot/Registerable.pm +++ b/PBot/Registerable.pm @@ -8,22 +8,11 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Registerable; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; -use Carp (); - -sub new { - Carp::croak("Options to Registerable should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my $self = shift; $self->{handlers} = []; @@ -31,7 +20,6 @@ sub initialize { sub execute_all { my $self = shift; - foreach my $func (@{ $self->{handlers} }) { my $result = &{ $func->{subref} }(@_); return $result if defined $result; @@ -42,11 +30,7 @@ sub execute_all { sub execute { my $self = shift; my $ref = shift; - - if (not defined $ref) { - Carp::croak("Missing reference parameter to Registerable::execute"); - } - + Carp::croak("Missing reference parameter to Registerable::execute") if not defined $ref; foreach my $func (@{ $self->{handlers} }) { if ($ref == $func || $ref == $func->{subref}) { return &{ $func->{subref} }(@_); diff --git a/PBot/Registry.pm b/PBot/Registry.pm index db965d46..ec4ed288 100644 --- a/PBot/Registry.pm +++ b/PBot/Registry.pm @@ -9,29 +9,16 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Registry; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use Time::HiRes qw(gettimeofday); -use Carp (); - -use PBot::DualIndexHashObject; use PBot::RegistryCommands; -sub new { - Carp::croak("Options to " . __FILE__ . " should be item/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); my $filename = $conf{filename} // Carp::croak("Missing filename reference in " . __FILE__); $self->{registry} = PBot::DualIndexHashObject->new(name => 'Registry', filename => $filename, pbot => $self->{pbot}); $self->{triggers} = {}; diff --git a/PBot/RegistryCommands.pm b/PBot/RegistryCommands.pm index 88e424ac..374df0b7 100644 --- a/PBot/RegistryCommands.pm +++ b/PBot/RegistryCommands.pm @@ -8,25 +8,13 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::RegistryCommands; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; -use Carp (); - -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); $self->{pbot}->{commands}->register(sub { $self->regset(@_) }, "regset", 1); $self->{pbot}->{commands}->register(sub { $self->regunset(@_) }, "regunset", 1); $self->{pbot}->{commands}->register(sub { $self->regshow(@_) }, "regshow", 0); diff --git a/PBot/SQLiteLogger.pm b/PBot/SQLiteLogger.pm index 13a05b67..c2d007ae 100644 --- a/PBot/SQLiteLogger.pm +++ b/PBot/SQLiteLogger.pm @@ -10,29 +10,22 @@ package PBot::SQLiteLogger; -use strict; -use warnings; - +use strict; use warnings; use feature 'unicode_strings'; -use Carp; use Time::HiRes qw(gettimeofday); -sub new -{ +sub new { my ($class, %conf) = @_; my $self = {}; $self->{buf} = ''; $self->{timestamp} = gettimeofday; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference in " . __FILE__); return bless $self, $class; } -sub log -{ +sub log { my $self = shift; $self->{buf} .= shift; - # DBI feeds us pieces at a time, so accumulate a complete line # before outputing if ($self->{buf} =~ tr/\n//) { diff --git a/PBot/SQLiteLoggerLayer.pm b/PBot/SQLiteLoggerLayer.pm index 25c63fdd..9705fbca 100644 --- a/PBot/SQLiteLoggerLayer.pm +++ b/PBot/SQLiteLoggerLayer.pm @@ -14,8 +14,7 @@ use warnings; use feature 'unicode_strings'; -sub PUSHED -{ +sub PUSHED { my ($class, $mode, $fh) = @_; my $logger; return bless \$logger, $class; @@ -23,13 +22,12 @@ sub PUSHED sub OPEN { my ($self, $path, $mode, $fh) = @_; - # $path is actually our logger object + # $path is our logger object $$self = $path; return 1; } -sub WRITE -{ +sub WRITE { my ($self, $buf, $fh) = @_; $$self->log($buf); return length($buf); diff --git a/PBot/SelectHandler.pm b/PBot/SelectHandler.pm index faeda923..36957c3b 100644 --- a/PBot/SelectHandler.pm +++ b/PBot/SelectHandler.pm @@ -3,26 +3,15 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::SelectHandler; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use IO::Select; -use Carp (); - -sub new { - Carp::croak("Options to SelectHandler should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference in SelectHandler"); $self->{select} = IO::Select->new(); $self->{readers} = {}; $self->{buffers} = {}; diff --git a/PBot/StdinReader.pm b/PBot/StdinReader.pm index 55ff7c63..4a7b1847 100644 --- a/PBot/StdinReader.pm +++ b/PBot/StdinReader.pm @@ -3,27 +3,15 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::StdinReader; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use POSIX qw(tcgetpgrp getpgrp); # to check whether process is in background or foreground -use Carp (); - -sub new { - Carp::croak("Options to StdinReader should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference in StdinReader"); - # create implicit bot-admin account for bot my $user = $self->{pbot}->{users}->find_user('.*', '*!stdin@pbot'); if (not defined $user or not $self->{pbot}->{capabilities}->userhas($user, 'botowner')) { diff --git a/PBot/Timer.pm b/PBot/Timer.pm index 48046a39..1aee4dbd 100644 --- a/PBot/Timer.pm +++ b/PBot/Timer.pm @@ -10,14 +10,11 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Timer; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; -use Carp (); - our $min_timeout = 1; our $max_seconds = 1000000; our $seconds = 0; @@ -34,23 +31,13 @@ $SIG{ALRM} = sub { $seconds -= $max_seconds if $seconds > $max_seconds; }; -sub new { - Carp::croak("Options to Timer should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - +sub initialize { + my ($self, %conf) = @_; my $timeout = $conf{timeout} // 10; - my $name = $conf{name} // "Unnamed $timeout Second Timer"; - - my $self = { - handlers => [], - name => $name, - timeout => $timeout, - enabled => 0, - }; - - bless $self, $class; $min_timeout = $timeout if $timeout < $min_timeout; - + $self->{name} = $conf{name} // "Unnamed $timeout Second Timer"; + $self->{handlers} = []; + $self->{enabled} = 0; # alarm signal handler (poor-man's timer) $self->{timer_func} = sub { on_tick_handler($self) }; return $self; diff --git a/PBot/Users.pm b/PBot/Users.pm index f7845b51..6a55ff90 100644 --- a/PBot/Users.pm +++ b/PBot/Users.pm @@ -8,27 +8,13 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Users; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; -use PBot::DualIndexHashObject; -use Carp (); - -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); - $self->{users} = PBot::DualIndexHashObject->new(name => 'Users', filename => $conf{filename}, pbot => $conf{pbot}); $self->load; diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index adf88968..f95c680f 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -9,34 +9,24 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::VERSION; +use parent 'PBot::Class'; -use strict; -use warnings; - +use strict; use warnings; use feature 'unicode_strings'; -BEGIN { - use Exporter; - our @ISA = 'Exporter'; - our @EXPORT_OK = qw(version); -} - use LWP::UserAgent; -# These are set automatically by build/update_version.pl +# These are set automatically by the misc/update_version script use constant { BUILD_NAME => "PBot", BUILD_REVISION => 3194, BUILD_DATE => "2020-02-07", }; -sub new { - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); +sub initialize { + my ($self, %conf) = @_; $self->{pbot}->{commands}->register(sub { $self->version_cmd(@_) }, "version", 0); $self->{last_check} = { timestamp => 0, version => BUILD_REVISION, date => BUILD_DATE }; - return $self; } sub version { diff --git a/PBot/WebPaste.pm b/PBot/WebPaste.pm index a2fc1702..70004e7a 100644 --- a/PBot/WebPaste.pm +++ b/PBot/WebPaste.pm @@ -8,29 +8,18 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::WebPaste; +use parent 'PBot::Class'; -use warnings; -use strict; - +use warnings; use strict; use feature 'unicode_strings'; use Time::HiRes qw/gettimeofday/; use Time::Duration; use LWP::UserAgent::Paranoid; -use Carp (); use Encode; -sub new { - Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH'; - my ($class, %conf) = @_; - my $self = bless {}, $class; - $self->initialize(%conf); - return $self; -} - sub initialize { my ($self, %conf) = @_; - $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); $self->{paste_sites} = [ sub { $self->paste_ixio(@_) }, @@ -41,9 +30,7 @@ sub initialize { sub get_paste_site { my ($self) = @_; - my $subref = $self->{paste_sites}->[$self->{current_site}]; - if (++$self->{current_site} >= @{$self->{paste_sites}}) { $self->{current_site} = 0; } @@ -52,11 +39,9 @@ sub get_paste_site { sub paste { my ($self, $text, %opts) = @_; - my %default_opts = ( no_split => 0, ); - %opts = (%default_opts, %opts); $text =~ s/(.{120})\s/$1\n/g unless $opts{no_split}; @@ -66,10 +51,7 @@ sub paste { for (my $tries = 3; $tries > 0; $tries--) { my $paste_site = $self->get_paste_site; $result = $paste_site->($text); - - if ($result !~ m/error pasting/) { - last; - } + last if $result !~ m/error pasting/; } return $result; }