2010-06-05 06:34:14 +02:00
|
|
|
# File: VERSION.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
2020-01-20 05:10:46 +01:00
|
|
|
# Purpose: Keeps track of bot version. Can compare current version against
|
|
|
|
# latest version on github or version.check_url site.
|
2010-06-05 06:34:14 +02:00
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# 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/.
|
|
|
|
|
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
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
use strict; use warnings;
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
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",
|
2020-04-20 19:53:43 +02:00
|
|
|
BUILD_REVISION => 3498,
|
|
|
|
BUILD_DATE => "2020-04-20",
|
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) = @_;
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->version_cmd(@_) }, "version", 0);
|
|
|
|
$self->{last_check} = {timestamp => 0, version => BUILD_REVISION, date => BUILD_DATE};
|
2018-01-23 08:48:25 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
sub version { return BUILD_NAME . " version " . BUILD_REVISION . " " . BUILD_DATE; }
|
2018-01-23 08:48:25 +01:00
|
|
|
|
2020-01-20 04:53:45 +01:00
|
|
|
sub version_cmd {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
|
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
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $url = $self->{pbot}->{registry}->get_value('version', 'check_url') // 'https://raw.githubusercontent.com/pragma-/pbot/master/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);
|
2020-01-20 06:13:09 +01:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return "Unable to get version information: " . $response->status_line if not $response->is_success;
|
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
|
|
|
|
2020-02-15 23:38:32 +01: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;
|
|
|
|
$target_nick = $self->{pbot}->{nicklist}->is_present_similar($from, $arguments) if length $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
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($self->{last_check}->{version} > BUILD_REVISION) { $result .= "; new version available: $self->{last_check}->{version} $self->{last_check}->{date}!"; }
|
|
|
|
return $result;
|
2020-01-20 04:53:45 +01:00
|
|
|
}
|
|
|
|
|
2010-06-05 06:34:14 +02:00
|
|
|
1;
|