| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||||
| 2 | #ABSTRACT: Test the configuration and FR24 API | ||||||||||||||||||||||||||||||||||||
| 3 | #PODNAME: fr24test | ||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use 5.018; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use open qw( :encoding(UTF-8) :std ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use FindBin qw($RealBin); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | if (-e "$RealBin/../dist.ini") { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use lib "$RealBin/../lib"; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | } | ||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use WWW::Telegram::BotAPI; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Term::ANSIColor qw(:constants); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use FR24::Utils; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 18 | 1 | our $conf; | |||||||||||||||||||||||||||||||||||
| 19 | 1 | my $width = 60; | |||||||||||||||||||||||||||||||||||
| 20 | 1 | my $opt_config = "$ENV{HOME}/.config/fr24-bot.ini"; | |||||||||||||||||||||||||||||||||||
| 21 | 1 | my $opt_verbose; | |||||||||||||||||||||||||||||||||||
| 22 | my $opt_debug; | ||||||||||||||||||||||||||||||||||||
| 23 | 1 | my $opt_token; | |||||||||||||||||||||||||||||||||||
| 24 | 1 | my $opt_ip; | |||||||||||||||||||||||||||||||||||
| 25 | 1 | my $opt_port; | |||||||||||||||||||||||||||||||||||
| 26 | GetOptions( | ||||||||||||||||||||||||||||||||||||
| 27 | 'a|api-key=s' => \$opt_token, | ||||||||||||||||||||||||||||||||||||
| 28 | 'i|ip=s' => \$opt_ip, | ||||||||||||||||||||||||||||||||||||
| 29 | 'p|port=i' => \$opt_port, | ||||||||||||||||||||||||||||||||||||
| 30 | 'c|config=s' => \$opt_config, | ||||||||||||||||||||||||||||||||||||
| 31 | 'verbose' => \$opt_verbose, | ||||||||||||||||||||||||||||||||||||
| 32 | 'debug' => \$opt_debug, | ||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | 'version' => sub { say basename($0). " " . $FR24::Utils::VERSION; exit 0;}, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 34 | 1 | ); | |||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||
| 36 | 1 | ▶ | $opt_verbose = 1 if $opt_debug; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||
| 38 | # Load config | ||||||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | if ( defined $opt_config and -e $opt_config ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
defined $opt_config and -e $opt_config
MC/DC0%0/2
defined $opt_config and -e $opt_config
defined $opt_config
-e $opt_config
Truth table33%1/3
A and B
A= defined $opt_configB= -e $opt_config
| |||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | say STDERR "Reading config from $opt_config" if ($opt_verbose); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 41 | 1 | $conf = FR24::Utils::loadconfig($opt_config); | |||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||
| 43 | } elsif ( -e "$ENV{HOME}/.config/fr24-bot.ini" ) { | ||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | say STDERR "Reading config from ~/.config/fr24-bot.ini" if ($opt_verbose); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 45 | 0 | $conf = FR24::Utils::loadconfig("$ENV{HOME}/.config/fr24-bot.ini"); | |||||||||||||||||||||||||||||||||||
| 46 | } elsif ( -e "$RealBin/.config/fr24-bot.ini" ) { | ||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | say STDERR "Reading config from $RealBin/.config/fr24-bot.ini" if ($opt_verbose); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 48 | 0 | $conf = FR24::Utils::loadconfig("$RealBin/.config/fr24-bot.ini"); | |||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||
| 52 | # If API KEY is provided in the command line, use it | ||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | if ( defined $opt_token and $opt_token ne 'default' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
defined $opt_token and $opt_token ne "default"
MC/DC0%0/2
defined $opt_token and $opt_token ne "default"
defined $opt_token
$opt_token ne "default"
Truth table33%1/3
A and B
A= defined $opt_tokenB= $opt_token ne "default"
| |||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | say STDERR "Using token from command line" if ($opt_verbose); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 55 | 0 | $conf->{telegram}->{apikey} = $opt_token; | |||||||||||||||||||||||||||||||||||
| 56 | } elsif ( defined $conf->{telegram}->{apikey}) { | ||||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | say STDERR "Using token from config file" if ($opt_verbose); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 58 | 1 | $opt_token = $conf->{telegram}->{apikey}; | |||||||||||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | say STDERR Dumper $conf if ($opt_verbose); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 61 | 0 | die "APIKey file not found. Can be in:\n - $ENV{HOME}/.config/fr24-bot.ini\n - $RealBin/.config/fr24-bot.ini\n - --token TOKEN\n"; | |||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||
| 64 | # If API KEY is provided in the command line, use it | ||||||||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | if ( defined $opt_port and $opt_port ne 'default' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
defined $opt_port and $opt_port ne "default"
MC/DC0%0/2
defined $opt_port and $opt_port ne "default"
defined $opt_port
$opt_port ne "default"
Truth table33%1/3
A and B
A= defined $opt_portB= $opt_port ne "default"
| |||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | say STDERR "Using port from command line" if ($opt_verbose); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 67 | 0 | $conf->{server}->{port} = $opt_port; | |||||||||||||||||||||||||||||||||||
| 68 | } elsif ( defined $conf->{server}->{port}) { | ||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | say STDERR "Using port from config file" if ($opt_verbose); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 70 | 1 | $opt_port = $conf->{server}->{port}; | |||||||||||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | say STDERR Dumper $conf if ($opt_verbose); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 73 | 0 | die "Port not found\n"; | |||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||
| 76 | 1 | ▶ | die "ERROR: No API key provided\n" unless defined $opt_token; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||
| 79 | # Check telegram bot API KEY | ||||||||||||||||||||||||||||||||||||
| 80 | 1 | eval { | |||||||||||||||||||||||||||||||||||
| 81 | 1 | my $api = WWW::Telegram::BotAPI->new( token => $opt_token ); | |||||||||||||||||||||||||||||||||||
| 82 | 1 | ▶ | $api->agent->can("inactivity_timeout") and $api->agent->inactivity_timeout(45); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 83 | 1 | ▶ | my $me = $api->getMe or die; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | check($@ ? 0 : 1, "API key is valid $opt_token"); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||
| 88 | sub check { | ||||||||||||||||||||||||||||||||||||
| 89 | 3 | ▶ | my ($condition, $message) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||
| 90 | # Print message in a column large 50 chars: | ||||||||||||||||||||||||||||||||||||
| 91 | 3 | printf "%-${width}s", substr($message, 0, $width); | |||||||||||||||||||||||||||||||||||
| 92 | 3 | ▶ | print length("$message") > $width ? "..." : " "; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 93 | 3 | ▶ | if ($condition) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 94 | 0 | print BOLD GREEN, " OK", RESET; | |||||||||||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||||||||||
| 96 | 3 | print BOLD RED, " ERROR", RESET; | |||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||
| 98 | 3 | print RESET, "\n"; | |||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||
| 101 | # Check IP | ||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||
| 103 | 1 | my %urls = ( | |||||||||||||||||||||||||||||||||||
| 104 | 'test' => 'http://ifconfig.me', | ||||||||||||||||||||||||||||||||||||
| 105 | 'test-https' => 'https://google.com', | ||||||||||||||||||||||||||||||||||||
| 106 | 'fr24-web' => 'https://flightradar24.com/', | ||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||||||||||
| 109 | 1 | ▶ | if (defined $conf->{server}->{ip} and defined $conf->{server}->{port}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $main::conf->{"server"}{"ip"} and defined $main::conf->{"server"}{"port"}
MC/DC0%0/2
defined $main::conf->{"server"}{"ip"} and defined $main::conf->{"server"}{"port"}
defined $main::conf->{"server"}{"ip"}
defined $main::conf->{"server"}{"port"}
Truth table33%1/3
A and B
A= defined $main::conf->{"server"}{"ip"}B= defined $main::conf->{"server"}{"port"}
| |||||||||||||||||||||||||||||||||||||
| 110 | 1 | $urls{'fr24-antenna'} = 'http://' . $conf->{server}->{ip} . ':' . $conf->{server}->{port} . '/settings.html'; | |||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||
| 113 | 1 | for my $key (reverse sort keys %urls) { | |||||||||||||||||||||||||||||||||||
| 114 | 4 | my $url = $urls{$key}; | |||||||||||||||||||||||||||||||||||
| 115 | 4 | my $check = FR24::Utils::url_exists($url); | |||||||||||||||||||||||||||||||||||
| 116 | # Print key (column 10 chars), check (3 chars), url (column 50 chars) | ||||||||||||||||||||||||||||||||||||
| 117 | 4 | printf "%-${width}s", substr("$key ($url)", 0, $width); | |||||||||||||||||||||||||||||||||||
| 118 | 4 | ▶ | print length("$key ($url)") > $width ? "..." : " "; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 119 | 4 | ▶ | if ($check) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 120 | 2 | print BOLD GREEN," OK ", RESET; | |||||||||||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||||||||||
| 122 | 2 | print BOLD RED, " ERROR ", RESET; | |||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||
| 125 | 4 | print RESET, "\n"; | |||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||
| 128 | 1 | my $self = FR24::Utils::systeminfo($conf); | |||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||
| 130 | 1 | ▶ | check(defined $self->{"local_ips"} ? 1 : 0, "IP is valid: " . $self->{"local_ips"} // "undef"); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
'IP is valid: ' . $self->{"local_ips"} // "undef"
MC/DC0%0/1
'IP is valid: ' . $self->{"local_ips"} // "undef"
'IP is valid: ' . $self->{"local_ips"}
Truth table50%1/2
A
A= 'IP is valid: ' . $self->{"local_ips"}
| |||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||
| 132 | 1 | ▶ | check(defined $self->{"feed_alias"} ? 1 : 0, "Antenna alias: " . $self->{"feed_alias"} // "undef"); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
'Antenna alias: ' . $self->{"feed_alias"} // "undef"
MC/DC0%0/1
'Antenna alias: ' . $self->{"feed_alias"} // "undef"
'Antenna alias: ' . $self->{"feed_alias"}
Truth table50%1/2
A
A= 'Antenna alias: ' . $self->{"feed_alias"}
| |||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||
| 134 | __END__ | ||||||||||||||||||||||||||||||||||||