Fix showdx, is_ipaddr, create_master_badip_files.pl
[spider.git] / perl / showdx
1 #!/usr/bin/perl
2 #
3 # Implement an external "show/dx" command
4 #
5 # Copyright (c) 1998-2023 Dirk Koopman G1TLH
6 #
7
8 package main;
9
10 # search local then perl directories
11 BEGIN {
12         # root of directory tree for this system
13         $root = "/spider"; 
14         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
15         
16         unshift @INC, "$root/perl";     # this IS the right way round!
17         unshift @INC, "$root/local";
18
19         our $local_data = "$root/local_data";
20         our $data = "$root/data";
21 }
22
23 #no warnings;
24
25 use IO::Handle;
26 use DXUtil;
27 use Bands;
28 use Spot;
29 use VE7CC;
30 use DXCommandmode;
31 use DXUser;
32 use DXM;
33
34 $Spot::spotcachedays = 0;
35 $Spot::readback = 0;
36
37
38 STDOUT->autoflush(1);
39 Spot::init();
40 Bands::load();
41 Prefix::init();
42 DXUser::init(0);
43 DXM::load();
44
45 my $call = 'N0CALL';
46 my $self = bless {_nospawn => 1, width => 80, call=>$call, lang=>'en' }, 'DXCommandmode' ;
47 $self->{user} = DXUser::get($call);
48 my $wantreal = 0;
49
50 while ($ARGV[0] =~ /^-+/) {
51         if ($ARGV[0] =~ /^-+[?h]/i) {
52                 help();
53                 exit(2);
54         }
55         $wantreal = 1 if $ARGV[0] =~ /^-+r/i;
56         $ve7cc = 1 if $ARGV[0] =~ /^-+v/i;
57         if ($ARGV[0] =~ /^-+w$/i && $ARGV[1] && $ARGV[1] =~ /^\d+$/) {
58                 $self->{width} = $ARGV[1];
59                 shift @ARGV;
60         }
61         if ($ARGV[0] =~ /^-+c/i && $ARGV[1] && is_callsign(uc $ARGV[1])) {
62                 $call = uc $ARGV[1];
63                 my $ref = DXUser::get($call);
64                 if ($ref) {
65                         $self->{call} = $call;
66                         $self->{user} = $ref;
67                 }
68                 shift @ARGV;
69         }
70         $self->{user}->wantgrid(1), ++$wantreal if $self->{user} && $ARGV[0] =~ /^-+(wa|wg)/i;
71         $self->{user}->wantusstate(1), ++$wantreal if $self->{user} && $ARGV[0] =~ /^-+(wa|wu)/i;
72         $self->{user}->wantdxitu(1), ++$wantreal if $self->{user} && $ARGV[0] =~ /^-+(wa|wi)/i;
73         $self->{user}->wantdxcq(1), ++$wantreal if $self->{user} && $ARGV[0] =~ /^-+(wa|wc)/i;
74
75         shift @ARGV;
76 }
77
78 $self->{ve7cc} = $ve7cc;
79
80 $dxdir = "/spider/cmd/show";
81 $dxcmd = "dx.pl";
82 $s = readfilestr($dxdir, $dxcmd);
83
84 eval $s;
85 die $@ if $@;
86
87
88 $expr = join ' ', @ARGV if @ARGV;
89
90 for (;;) {
91         if ($expr) {
92                 $myexpr = $expr;
93                 $myexpr = 'real ' . $myexpr if $wantreal && $myexpr !~ /\breal\b/;
94         } else {
95                 print "show/dx: ";
96                 $myexpr = <STDIN>;
97                 last unless defined $myexpr;
98                 chomp $myexpr;
99                 last if $myexpr =~ /^q$/i;
100                 $myexpr = 'real ' . $myexpr if $wantreal && $myexpr !~ /\breal\b/;
101         }
102
103         my @out = map {"$_\n"} handle($self, $myexpr);
104         shift @out;   # remove return code
105         print @out;
106         last if $expr;
107 }
108
109 exit @out > 0 ? 0 : 1;
110
111 sub help
112 {
113         print qq{A static TEST Program that allows standalone sh/dx queries
114 from the command line.
115
116 $0: Usage (examples)
117     showdx on 40m 
118     showdx 50 on 40m 
119     showdx by g1tlh
120     showdx -v -c g1tlh by g1tlh
121     showdx -w 132 -wc -wg -wu 50 on 40m 
122     
123 $0: Arguments:
124     -?:
125     -h: print this text.
126     -c <callsign>: pretend to be this callsign
127     -r: set 'real' mode (a.k.a show/fdx) (saves typing 'real ' in the query)
128     -v: output in VE7CC computer friendly mode.
129     -w <width>: use this width 
130     -wc: set want cq zones
131     -wg: set want grid squares
132     -wi: set want itu zones
133     -wu: set want US states
134     -wa: set all of the above
135
136     NOTE: setting any of -wc, -wg, -wi, -wu flags implies adding 'real ' to 
137           the query, if not already present.
138 };
139 }
140
141 sub dbg {};
142 sub isdbg {};