a92329c9fde1d1c4ed350ef23fe9619b01493e2b
[spider.git] / cmd / show / badip.pl
1 #
2 # show (or find) list of bad dx nodes
3 #
4 # Copyright (c) 2021-2023 - Dirk Koopman G1TLH
5 #
6 #
7 #
8 my ($self, $line) = @_;
9 return (1, $self->msg('e5')) if $self->remotecmd;
10 # are we permitted?
11 return (1, $self->msg('e5')) if $self->priv < 6;
12 return (1, q{Please install Net::CIDR::Lite or libnet-cidr-lite-perl to use this command}) unless $DXCIDR::active;
13
14 my @out;
15 my @added;
16 my @in = split /\s+/, $line;
17 my $maxlth = 0;
18 my $width = $self->width // 80;
19 my $count = 0;
20
21 #$DB::single = 1;
22
23 # query
24 if (@in) {
25         foreach my $ip (@in) {
26                 if (DXCIDR::find($ip)) {
27                         push @out, "$ip FOUND";
28                         ++$count;
29                 } else {
30                         push @out, "$ip CLEAN";
31                 }
32         }
33         return (1, @out);
34 } else {
35 # list
36         my @list = map {my $s = $_; $s =~ s!/(?:32|128)$!!; $maxlth = length $s if length $s > $maxlth; $s =~ /^1$/?undef:$s} DXCIDR::list();
37         my @l;
38         $maxlth //= 20;
39         my $n = int ($width/($maxlth+1));
40         my $format = "\%-${maxlth}s " x $n;
41         chop $format;
42
43         foreach my $list (@list) {
44                 ++$count;
45                 if (@l > $n) {
46                         push @out, sprintf $format, @l;
47                         @l = ();
48                 }
49                 push @l, $list;
50         }       
51         push @l, "" while @l < $n;
52         push @out, sprintf $format, @l;
53 }
54
55 push @out, "show/badip: $count records found";
56 return (1, @out);