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