3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-05 03:29:33 +01:00

Progress on refactoring and polishing everything

This commit is contained in:
Pragmatic Software 2021-07-20 23:06:03 -07:00
parent e1d6b1b950
commit abfbc558e8
16 changed files with 112 additions and 65 deletions

View File

@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::Commands::Capabilities;
package PBot::Core::Commands::Capabilities;
use PBot::Imports;
@ -24,7 +24,6 @@ sub new {
sub initialize {
my ($self, %conf) = @_;
$self->{pbot}->{commands}->register(sub { $self->cmd_cap(@_) }, "cap");
}

View File

@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::Commands::ChanOp;
package PBot::Core::Commands::ChanOp;
use PBot::Imports;

View File

@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::Commands::CommandMetadata;
package PBot::Core::Commands::CommandMetadata;
use PBot::Imports;

View File

@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::Commands::Factoids;
package PBot::Core::Commands::Factoids;
use PBot::Imports;

View File

@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::Commands::Help;
package PBot::Core::Commands::Help;
use PBot::Imports;

View File

@ -6,7 +6,7 @@
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::Commands::Misc;
package PBot::Core::Commands::Misc;
use PBot::Imports;

View File

@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::Commands::Registry;
package PBot::Core::Commands::Registry;
use PBot::Imports;

View File

@ -0,0 +1,86 @@
# File: Version.pm
#
# Purpose: The `version` PBot command. It can check against GitHub or a
# user-defined URL for PBot's VERSION.pm file.
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::Core::Commands::Version;
use parent 'PBot::Core::Class';
use PBot::Imports;
use PBot::VERSION qw/BUILD_REVISION BUILD_DATE/;
use LWP::UserAgent;
sub new {
my ($class, %args) = @_;
# ensure class was passed a PBot instance
if (not exists $args{pbot}) {
Carp::croak("Missing pbot reference to $class");
}
my $self = bless { pbot => $args{pbot} }, $class;
$self->initialize(%args);
return $self;
}
sub initialize {
my ($self, %conf) = @_;
# register `version` command
$self->{pbot}->{commands}->register(sub { $self->cmd_version(@_) }, 'version');
# initialize last_check version data
$self->{last_check} = {timestamp => 0, version => BUILD_REVISION, date => BUILD_DATE};
}
sub cmd_version {
my ($self, $context) = @_;
my $ratelimit = $self->{pbot}->{registry}->get_value('version', 'check_limit') // 300;
if (time - $self->{last_check}->{timestamp} >= $ratelimit) {
$self->{last_check}->{timestamp} = time;
my $url = $self->{pbot}->{registry}->get_value('version', 'check_url') // 'https://raw.githubusercontent.com/pragma-/pbot/master/lib/PBot/VERSION.pm';
$self->{pbot}->{logger}->log("Checking $url for new version...\n");
my $ua = LWP::UserAgent->new(timeout => 10);
my $response = $ua->get($url);
if (not $response->is_success) {
return "Unable to get version information: " . $response->status_line;
}
my $text = $response->decoded_content;
my ($version, $date) = $text =~ m/^\s+BUILD_REVISION => (\d+).*^\s+BUILD_DATE\s+=> "([^"]+)"/ms;
if (not defined $version or not defined $date) {
return "Unable to get version information: data did not match expected format";
}
$self->{last_check} = {timestamp => time, version => $version, date => $date};
}
my $target_nick;
if (length $context->{arguments}) {
$target_nick = $self->{pbot}->{nicklist}->is_present_similar($context->{from}, $context->{arguments});
}
my $result = '/say ';
$result .= "$target_nick: " if $target_nick;
$result .= $self->{pbot}->{version}->version;
if ($self->{last_check}->{version} > BUILD_REVISION) {
$result .= "; new version available: $self->{last_check}->{version} $self->{last_check}->{date}!";
}
return $result;
}
1;

View File

@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::IRCHandlers::Cap;
package PBot::Core::IRCHandlers::Cap;
use PBot::Imports;

View File

@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::IRCHandlers::Channel;
package PBot::Core::IRCHandlers::Channel;
use parent 'PBot::Core::Class';
use PBot::Imports;

View File

@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::IRCHandlers::Chat;
package PBot::Core::IRCHandlers::Chat;
use PBot::Imports;

View File

@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::IRCHandlers::NickServ;
package PBot::Core::IRCHandlers::NickServ;
use PBot::Imports;

View File

@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::IRCHandlers::SASL;
package PBot::Core::IRCHandlers::SASL;
use PBot::Imports;

View File

@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::IRCHandlers::Server;
package PBot::Core::IRCHandlers::Server;
use PBot::Imports;

View File

@ -23,14 +23,14 @@ sub load_packages {
my $cwd = getcwd;
chdir "$RealBin/../lib/PBot";
chdir "$RealBin/../lib/PBot/Core";
my @packages = glob "$directory/*.pm";
chdir $cwd;
foreach my $package (sort @packages) {
$package = "PBot/$package";
$package = "PBot/Core/$package";
my $class = $package;
$class =~ s/\//::/g;

View File

@ -11,7 +11,17 @@ use parent 'PBot::Core::Class';
use PBot::Imports;
use LWP::UserAgent;
use Exporter qw/import/;
our @EXPORT = ();
our %EXPORT_TAGS = (
'all' => [qw/BUILD_NAME BUILD_REVISION BUILD_DATE/],
);
our @EXPORT_OK = (
@{ $EXPORT_TAGS{all} },
);
# These are set automatically by the misc/update_version script
use constant {
@ -23,58 +33,10 @@ use constant {
sub initialize {
my ($self, %conf) = @_;
# register `version` command
$self->{pbot}->{commands}->register(sub { $self->cmd_version(@_) }, "version", 0);
# initialize last_check version data
$self->{last_check} = {timestamp => 0, version => BUILD_REVISION, date => BUILD_DATE};
}
sub cmd_version {
my ($self, $context) = @_;
my $ratelimit = $self->{pbot}->{registry}->get_value('version', 'check_limit') // 300;
if (time - $self->{last_check}->{timestamp} >= $ratelimit) {
$self->{last_check}->{timestamp} = time;
my $url = $self->{pbot}->{registry}->get_value('version', 'check_url') // 'https://raw.githubusercontent.com/pragma-/pbot/master/lib/PBot/VERSION.pm';
$self->{pbot}->{logger}->log("Checking $url for new version...\n");
my $ua = LWP::UserAgent->new(timeout => 10);
my $response = $ua->get($url);
if (not $response->is_success) {
return "Unable to get version information: " . $response->status_line;
}
my $text = $response->decoded_content;
my ($version, $date) = $text =~ m/^\s+BUILD_REVISION => (\d+).*^\s+BUILD_DATE\s+=> "([^"]+)"/ms;
if (not defined $version or not defined $date) {
return "Unable to get version information: data did not match expected format";
}
$self->{last_check} = {timestamp => time, version => $version, date => $date};
}
my $target_nick;
if (length $context->{arguments}) {
$target_nick = $self->{pbot}->{nicklist}->is_present_similar($context->{from}, $context->{arguments});
}
my $result = '/say ';
$result .= "$target_nick: " if $target_nick;
$result .= $self->version;
if ($self->{last_check}->{version} > BUILD_REVISION) {
$result .= "; new version available: $self->{last_check}->{version} $self->{last_check}->{date}!";
}
return $result;
}
sub version {
return BUILD_NAME . " version " . BUILD_REVISION . " " . BUILD_DATE;
}