bin/config-fr24-bot

filter: Statementstmt 54.7% 80 / 146 Branchbran 34.8% 30 / 86 Conditioncond 33.3% 4 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 71.4% 10 / 14 Podpod n/a totaltotal 46.9% 125 / 266 CC 5 SCAR 22.2
CC
5
Cov
47%
getInfoFromUser
30.0
__ANON__
2.0
__ANON__
2.0
CRAP 9.2
« blib/lib/FR24/Utils.pm ↑ Summary blib/lib/FR24/Bot.pm »
1 #!/usr/bin/env perl
2 #ABSTRACT: Configuration for FR24-Bot, create update or read your configuration file
3 #PODNAME: config-fr24-bot
4  
5 2 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use FindBin qw($RealBin);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶if (-e "$RealBin/../dist.ini") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$main::RealBin/../dist.ini") 2 0 2
14 2 ▶ use lib "$RealBin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
15 }
16 2 ▶use FR24::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Define version
19 2 our $VERSION = $FR24::Utils::VERSION;
20 2 my $config_file = "$ENV{HOME}/.config/fr24-bot.ini";
21 2 my $verbose = 0;
22 2 my $opt_api_key;
23 my $opt_IP;
24 2 my $fr24default_port = 8754;
25 2 my $opt_port = $fr24default_port;
26 2 my $opt_no_write = 0;
27 2 my @opt_users;
28 my @opt_banned_users;
29 2 my $opt_quiet = 0;
30 2 ▶if (not GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
unless (&GetOptions("a|api-key=s", \$opt_api_key, "i|ip=s", \$opt_IP, "p|port=i", \$opt_port, "c|config=s", \$config_file, "u|users=s", \@opt_users, "b|ban=s", \@opt_banned_users, "no-write", \$opt_no_write, "quiet", \$opt_quiet, "verbose", \$verbose, "version", sub { say basename($0) . ' ' . $VERSION; exit 0; } , "h|help", sub { usage(); exit 0; } )) 0 2 2
31 'a|api-key=s' => \$opt_api_key,
32 'i|ip=s' => \$opt_IP,
33 'p|port=i' => \$opt_port,
34 'c|config=s' => \$config_file,
35 'u|users=s' => \@opt_users,
36 'b|ban=s' => \@opt_banned_users,
37 'no-write' => \$opt_no_write,
38 'quiet' => \$opt_quiet,
39 'verbose' => \$verbose,
40 0 ▶ 'version' => sub { say basename($0). " " . $VERSION; exit 0;},
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 ▶ 'h|help' => sub { usage(); exit 0;},
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 )) {
43 0 usage();
44 0 die "ERROR: Invalid options\n";
45 }
46  
47 2 my $config = FR24::Utils::loadconfig($config_file);
48  
49 # Config to json
50  
51 2 ▶if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 2 2
52 0 my $json = JSON::PP->new->utf8->pretty->canonical;
53 0 my $json_text = $json->encode($config);
54 0 say STDERR "------- Config File -------\n", $json_text;
55 }
56  
57  
58 ## ===== API KEY =====
59 2 my $NEW_API_KEY = undef;
60 2 ▶if (not defined $opt_api_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opt_api_key) { } 1 1 2
61 # Not user supplied
62 1 ▶ say STDERR GREEN, "> API KEY ", RESET, $config->{"telegram"}->{"apikey"} if (defined $config->{"telegram"}->{"apikey"} and not $opt_quiet);
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"telegram"}{"apikey"} and not $opt_quiet 0 1 1
Condition33%1/3
defined $config->{"telegram"}{"apikey"} and not $opt_quiet
!ll&&!rl&&r
010
MC/DC0%0/2
defined $config->{"telegram"}{"apikey"} and not $opt_quiet
defined $config->{"telegram"}{"apikey"} not $opt_quiet
Truth table33%1/3
A and not(B)
A= defined $config->{"telegram"}{"apikey"}B= not $opt_quiet
ABresult
0X0
100
111
63 1 ▶ $NEW_API_KEY = getInfoFromUser("Provide API key for Telegram", $config->{"telegram"}->{"apikey"}) if not defined $config->{"telegram"}->{"apikey"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"telegram"}{"apikey"} 0 1 1
64 } else {
65 # User supplied: overwrite or keep default
66 1 ▶ if ($opt_api_key eq 'default') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_api_key eq 'default') { } 1 0 1
67 #TODO: If missing in config, force prompt
68 1 ▶ say STDERR YELLOW, "> Keeping default API KEY: ", $config->{"telegram"}->{"apikey"}, RESET if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 1 1
69 1 $NEW_API_KEY = $config->{"telegram"}->{"apikey"};
70 } else {
71 0 ▶ say STDERR YELLOW, "> Overwriting API KEY: ", $config->{"telegram"}->{"apikey"}, RESET if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
72 0 $NEW_API_KEY = $opt_api_key;
73 }
74 }
75  
76 ## ===== IP =====
77 2 my $NEW_IP = undef;
78 2 ▶if (not defined $opt_IP) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opt_IP) { } 1 1 2
79 1 ▶ say STDERR GREEN, "> FR24 Server IP ", RESET, $config->{"server"}->{"ip"} if (defined $config->{"server"}->{"ip"} and not $opt_quiet);
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"server"}{"ip"} and not $opt_quiet 0 1 1
Condition33%1/3
defined $config->{"server"}{"ip"} and not $opt_quiet
!ll&&!rl&&r
010
MC/DC0%0/2
defined $config->{"server"}{"ip"} and not $opt_quiet
defined $config->{"server"}{"ip"} not $opt_quiet
Truth table33%1/3
A and not(B)
A= defined $config->{"server"}{"ip"}B= not $opt_quiet
ABresult
0X0
100
111
80 1 ▶ $NEW_IP = getInfoFromUser("Provide IP address for FR24", $config->{"server"}->{"ip"}) if not defined $config->{"server"}->{"ip"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"server"}{"ip"} 0 1 1
81 } else {
82 1 ▶ if ($opt_IP eq 'default') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_IP eq 'default') { } 0 1 1
83 0 ▶ say STDERR YELLOW, "> Keeping default IP: ", $config->{"server"}->{"ip"}, RESET if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
84 0 $NEW_IP = $config->{"server"}->{"ip"};
85 } else {
86 1 ▶ say STDERR YELLOW, "> Overwriting IP: ", $config->{"server"}->{"ip"}, RESET if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 1 1
87 1 $NEW_IP = $opt_IP;
88 }
89 }
90  
91 ## ===== port =====
92
93 2 ▶my $config_port = $config->{"server"}->{"port"} if defined $config->{"server"}->{"port"};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"server"}{"port"} 2 0 2
94 2 ▶if (defined $config_port) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_port) { } 2 0 2
95 # It was defined
96 2 ▶ if (defined $opt_port and $opt_port != $fr24default_port) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt_port and $opt_port != $fr24default_port) { } 1 1 2
Condition66%2/3
defined $opt_port and $opt_port != $fr24default_port
!ll&&!rl&&r
011
MC/DC50%1/2
defined $opt_port and $opt_port != $fr24default_port
defined $opt_port $opt_port != $fr24default_port
Truth table66%2/3
A and B
A= defined $opt_portB= $opt_port != $fr24default_port
ABresult
0X0
100
111
97 1 ▶ say STDERR YELLOW, "> Overwriting port: ", $opt_port, RESET if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 1 1
98 1 $config->{"server"}->{"port"} = $opt_port;
99 } else {
100 1 ▶ say STDERR GREEN, "> Port ", RESET, $config_port if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 1 1
101 }
102 } else {
103 # It was not defined
104 0 ▶ if (defined $opt_port and $opt_port != $fr24default_port) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt_port and $opt_port != $fr24default_port) { } 0 0 0
Condition0%0/3
defined $opt_port and $opt_port != $fr24default_port
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt_port and $opt_port != $fr24default_port
defined $opt_port $opt_port != $fr24default_port
Truth table0%0/3
A and B
A= defined $opt_portB= $opt_port != $fr24default_port
ABresult
0X0
100
111
105 0 ▶ say STDERR YELLOW, "> Setting port: ", $opt_port, RESET if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
106 0 $config->{"server"}->{"port"} = $opt_port;
107 } else {
108 0 $config->{"server"}->{"port"} = getInfoFromUser("Provide port for FR24 server", $fr24default_port);
109  
110 }
111 }
112 ## ===== USERS =====
113 2 ▶say STDERR GREEN , "> Authorized users", RESET if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
114 2 for my $u (keys %{$config->{"users"}}) {
115 2 ▶ say STDERR YELLOW, "> User ", RESET, $u, " is ", $config->{"users"}->{$u} ? "authorized" : "NOT authorized" if $verbose;
Branch: 2 branches
BranchTrueFalseTotal
if $verbose 0 2 2
$config->{'users'}{$u} ? : 0 0 0
116
117 }
118 2 for my $u (@opt_users) {
119 # Check integer
120 0 ▶ if ($u =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u =~ /^\d+$/u) { } 0 0 0
121 0 ▶ say STDERR GREEN, "> Adding user ", RESET, $u if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
122 0 $config->{"users"}->{"$u"} = 1;
123 } else {
124 0 say STDERR RED, "> Invalid user ", RESET, $u;
125 }
126 }
127 2 for my $u (@opt_banned_users) {
128 # Check integer
129 0 ▶ if ($u =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u =~ /^\d+$/u) { } 0 0 0
130 0 ▶ say STDERR GREEN, "> Adding user ", RESET, $u if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
131 0 $config->{"users"}->{"$u"} = 0;
132 } else {
133 0 say STDERR RED, "> Invalid user ", RESET, $u;
134 }
135 }
136 # If there are AUTHORIZED users, set "everyone" to 0
137 2 my $auth_count = 0;
138 2 for my $u (keys %{$config->{"users"}}) {
139 2 ▶ next if $u eq "everyone";
Branch: 1 branch
BranchTrueFalseTotal
if $u eq "everyone" 2 0 2
140 0 ▶ $auth_count++ if $config->{"users"}->{$u};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"users"}{$u} 0 0 0
141 }
142 2 ▶if ($auth_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_count > 0) 0 2 2
143 0 ▶ say STDERR YELLOW, "> Setting everyone to NOT authorized", RESET if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
144 0 $config->{"users"}->{"everyone"} = 0;
145 }
146 # UPDATE CONFIG
147 2 ▶$config->{"telegram"}->{"apikey"} = $NEW_API_KEY if defined $NEW_API_KEY;
Branch: 1 branch
BranchTrueFalseTotal
if defined $NEW_API_KEY 1 1 2
148 2 ▶$config->{"server"}->{"ip"} = $NEW_IP if defined $NEW_IP;
Branch: 1 branch
BranchTrueFalseTotal
if defined $NEW_IP 1 1 2
149  
150  
151  
152  
153 2 ▶if ($opt_no_write) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt_no_write) { } 2 0 2
elsif ($verbose) { } 0 0 0
154 # Print as json
155 2 my $json = JSON::PP->new->utf8->pretty->canonical;
156 2 my $json_text = $json->encode($config);
157 2 say $json_text;
158 2 exit 0;
159 } elsif ($verbose) {
160 0 my $json = JSON::PP->new->utf8->pretty->canonical;
161 0 my $json_text = $json->encode($config);
162 0 say STDERR "------- New Config File -------\n", $json_text;
163 }
164 0 eval {
165 0 FR24::Utils::saveconfig($config_file, $config);
166 };
167 0 ▶if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
168 0 say STDERR "ERROR SAVING to $config_file:\n$@";
169 0 exit 1;
170 } else {
171 0 ▶ say STDERR "Config saved to $config_file" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
172 0 exit 0;
173 }
174  
175  
176  
177 sub usage {
178 0 ▶ say "Usage: $0 [-a|--api-key API_KEY] [-c|--config CONFIG_FILE] [-h|--help] [-v|--version]";
Subroutine usage: not called CC=1 SCAR=6.9
179 0 say "Default config file: $ENV{HOME}/.config/fr24-bot.ini";
180 0 say "\nOptions:";
181 0 say " -c, --config CONFIG_FILE Set config file";
182 0 say " -a, --api-key API_KEY Set API key for Telegram";
183 0 say " -i, --ip IP Set IP address for FR24";
184 0 say " -p, --port PORT Set port for FR24 (default: 8754)";
185 0 say " -u, --users USER1 Authorize user ID (multiple allowed)";
186 0 say " -b, --ban USER1 Ban user ID (multiple allowed)";
187 0 say " --no-write Do not write config file";
188 0 say " --verbose Verbose output";
189 0 say " --help This help message";
190 }
191  
192 sub getInfoFromUser {
193 0 ▶ my ($prompt, $default) = @_;
Subroutine getInfoFromUser: not called CC=5 SCAR=34.0
194 0 ▶ $default = "" if not defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 0 0 0
195 0 print BOLD $prompt, RESET, " [$default]: ";
196 0 my $answer = <STDIN>;
197 0 chomp $answer;
198 # Loop to confirm with "y"
199 0 while (1) {
200 0 my $msg = "Do you confirm your answer";
201 0 ▶ if (length($answer) < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $answer < 1) 0 0 0
202 0 $answer = $default;
203 0 my $msg = "No answer given. Do you want to keep the default?";
204 }
205  
206 0 say UNDERLINE "$msg [$answer]", RESET, BOLD," (y/n): ", RESET;
207 0 my $confirm = <STDIN>;
208 0 chomp $confirm;
209 0 ▶ if ($confirm =~ m/^[yY]$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($confirm =~ /^[yY]$/u) { } 0 0 0
elsif ($confirm eq 'n') { } 0 0 0
210 0 return $answer;
211 } elsif ($confirm eq "n") {
212 0 return getInfoFromUser($prompt, $default);
213 }
214 }
215 }
216  
217 __END__
« blib/lib/FR24/Utils.pm ↑ Summary blib/lib/FR24/Bot.pm »