2010-06-05 04:34:14 +00:00
|
|
|
# File: VERSION.pm
|
|
|
|
#
|
2020-01-19 20:10:46 -08:00
|
|
|
# Purpose: Keeps track of bot version. Can compare current version against
|
2021-06-18 21:23:34 -07:00
|
|
|
# latest version on github or URL in `version.check_url` registry entry.
|
2010-06-05 04:34:14 +00:00
|
|
|
|
2021-07-10 15:00:22 -07:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 21:33:31 +00:00
|
|
|
|
2010-06-05 04:34:14 +00:00
|
|
|
package PBot::VERSION;
|
2020-02-08 11:04:13 -08:00
|
|
|
use parent 'PBot::Class';
|
2010-06-05 04:34:14 +00:00
|
|
|
|
2021-06-18 21:23:34 -07:00
|
|
|
use PBot::Imports;
|
2019-07-10 18:40:53 -07:00
|
|
|
|
2020-01-19 19:53:45 -08:00
|
|
|
use LWP::UserAgent;
|
|
|
|
|
2020-02-08 11:04:13 -08:00
|
|
|
# These are set automatically by the misc/update_version script
|
2010-06-05 04:34:14 +00:00
|
|
|
use constant {
|
2020-02-15 14:38:32 -08:00
|
|
|
BUILD_NAME => "PBot",
|
2021-07-13 20:14:11 -07:00
|
|
|
BUILD_REVISION => 4192,
|
2021-07-13 15:17:13 -07:00
|
|
|
BUILD_DATE => "2021-07-13",
|
2010-06-05 04:34:14 +00:00
|
|
|
};
|
|
|
|
|
2020-02-08 11:04:13 -08:00
|
|
|
sub initialize {
|
2020-02-15 14:38:32 -08:00
|
|
|
my ($self, %conf) = @_;
|
2021-06-05 13:20:03 -07:00
|
|
|
|
|
|
|
# register `version` command
|
2020-05-04 13:21:35 -07:00
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_version(@_) }, "version", 0);
|
2021-06-05 13:20:03 -07:00
|
|
|
|
|
|
|
# initialize last_check version data
|
2020-02-15 14:38:32 -08:00
|
|
|
$self->{last_check} = {timestamp => 0, version => BUILD_REVISION, date => BUILD_DATE};
|
2018-01-22 23:48:25 -08:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:21:35 -07:00
|
|
|
sub cmd_version {
|
|
|
|
my ($self, $context) = @_;
|
2020-01-19 19:53:45 -08:00
|
|
|
|
2020-02-15 14:38:32 -08:00
|
|
|
my $ratelimit = $self->{pbot}->{registry}->get_value('version', 'check_limit') // 300;
|
2020-01-19 21:13:09 -08:00
|
|
|
|
2020-02-15 14:38:32 -08:00
|
|
|
if (time - $self->{last_check}->{timestamp} >= $ratelimit) {
|
|
|
|
$self->{last_check}->{timestamp} = time;
|
2020-01-19 21:13:09 -08:00
|
|
|
|
2021-07-13 20:13:48 -07:00
|
|
|
my $url = $self->{pbot}->{registry}->get_value('version', 'check_url') // 'https://raw.githubusercontent.com/pragma-/pbot/master/lib/PBot/VERSION.pm';
|
2021-06-05 13:20:03 -07:00
|
|
|
|
2020-02-15 14:38:32 -08:00
|
|
|
$self->{pbot}->{logger}->log("Checking $url for new version...\n");
|
2021-06-05 13:20:03 -07:00
|
|
|
|
2020-02-15 14:38:32 -08:00
|
|
|
my $ua = LWP::UserAgent->new(timeout => 10);
|
|
|
|
my $response = $ua->get($url);
|
2020-01-19 21:13:09 -08:00
|
|
|
|
2021-06-05 13:20:03 -07:00
|
|
|
if (not $response->is_success) {
|
|
|
|
return "Unable to get version information: " . $response->status_line;
|
|
|
|
}
|
2020-01-19 21:13:09 -08:00
|
|
|
|
2020-02-15 14:38:32 -08:00
|
|
|
my $text = $response->decoded_content;
|
|
|
|
my ($version, $date) = $text =~ m/^\s+BUILD_REVISION => (\d+).*^\s+BUILD_DATE\s+=> "([^"]+)"/ms;
|
2020-01-19 21:13:09 -08:00
|
|
|
|
2021-06-05 13:20:03 -07:00
|
|
|
if (not defined $version or not defined $date) {
|
|
|
|
return "Unable to get version information: data did not match expected format";
|
|
|
|
}
|
2020-01-19 21:13:09 -08:00
|
|
|
|
2020-02-15 14:38:32 -08:00
|
|
|
$self->{last_check} = {timestamp => time, version => $version, date => $date};
|
|
|
|
}
|
2020-01-19 21:13:09 -08:00
|
|
|
|
2020-02-15 14:38:32 -08:00
|
|
|
my $target_nick;
|
2021-06-05 13:20:03 -07:00
|
|
|
if (length $context->{arguments}) {
|
|
|
|
$target_nick = $self->{pbot}->{nicklist}->is_present_similar($context->{from}, $context->{arguments});
|
|
|
|
}
|
2020-01-19 21:13:09 -08:00
|
|
|
|
2020-02-15 14:38:32 -08:00
|
|
|
my $result = '/say ';
|
|
|
|
$result .= "$target_nick: " if $target_nick;
|
|
|
|
$result .= $self->version;
|
2020-01-19 21:13:09 -08:00
|
|
|
|
2021-06-05 13:20:03 -07:00
|
|
|
if ($self->{last_check}->{version} > BUILD_REVISION) {
|
|
|
|
$result .= "; new version available: $self->{last_check}->{version} $self->{last_check}->{date}!";
|
|
|
|
}
|
|
|
|
|
2020-02-15 14:38:32 -08:00
|
|
|
return $result;
|
2020-01-19 19:53:45 -08:00
|
|
|
}
|
|
|
|
|
2021-06-05 13:20:03 -07:00
|
|
|
sub version {
|
|
|
|
return BUILD_NAME . " version " . BUILD_REVISION . " " . BUILD_DATE;
|
|
|
|
}
|
2020-05-04 13:21:35 -07:00
|
|
|
|
2010-06-05 04:34:14 +00:00
|
|
|
1;
|