fix range over different sized numbers in sh/dx
[spider.git] / cmd / show / dx.pl
1 #
2 # show dx (normal)
3 #
4 # $Id$
5 #
6
7 my ($self, $line) = @_;
8 my @list = split /\s+/, $line;  # split the line up
9
10 my @out;
11 my $f;
12 my $call;
13 my ($from, $to);
14 my ($fromday, $today);
15 my @freq;
16 my @ans;
17 my $pre;
18 my $spotter;
19 my $info;
20 my $expr;
21 my $hint;
22 my $dxcc;
23 my $real;
24 my $fromdxcc;
25 my ($doqsl, $doiota, $doqra);
26
27 while ($f = shift @list) {              # next field
28         #  print "f: $f list: ", join(',', @list), "\n";
29         if (!$from && !$to) {
30                 ($from, $to) = $f =~ m|^(\d+)[-/](\d+)$|; # is it a from -> to count?
31                 next if $from && $to > $from;
32         }
33         if (!$to) {
34                 ($to) = $f =~ /^(\d+)$/o if !$to; # is it a to count?
35                 next if $to;
36         }
37         if (lc $f eq 'dxcc') {
38                 $dxcc = 1;
39                 next;
40         }
41         if (lc $f eq 'rt' || $f =~ /^real/i) {
42                 $real = 1;
43                 next;
44         }
45         if (lc $f eq 'on' && $list[0]) { # is it freq range?
46                 #    print "yup freq\n";
47                 if ($list[0] =~ m|^(\d+)(?:\.\d+)?[-/](\d+)(?:\.\d+)?$|) {
48                         push @freq, $1, $2;
49                         shift @list;
50                         next;
51                 } else {
52                         my @r = split '/', lc $list[0];
53                         # print "r0: $r[0] r1: $r[1]\n";
54                         my @fr = Bands::get_freq($r[0], $r[1]);
55                         if (@fr) {                      # yup, get rid of extranous param
56                                 #         print "freq: ", join(',', @fr), "\n";
57                                 push @freq, @fr;    # add these to the list
58                                 shift @list;
59                                 next;
60                         }
61                 }
62         }
63         if (lc $f eq 'day' && $list[0]) {
64                 #   print "got day\n";
65                 ($fromday, $today) = split m|[-/]|, shift(@list);
66                 next;
67         }
68         if (lc $f eq 'info' && $list[0]) {
69                 #   print "got info\n";
70                 $info = shift @list;
71                 next;
72         }
73         if ((lc $f eq 'spotter' || lc $f eq 'by') && $list[0]) {
74                 #    print "got spotter\n";
75                 $spotter = uc shift @list;
76                 if ($list[0] && lc $list[0] eq 'dxcc') {
77                         $fromdxcc = 1;
78                         shift @list;
79                 }
80                 next;
81         }
82         if (lc $f eq 'qsl') {
83                 $doqsl = 1;
84                 next;
85         }
86         if (lc $f eq 'iota') {
87                 my ($a, $b);
88 #               $DB::single =1;
89                 
90                 if (@list && $list[0] && (($a, $b) = $list[0] =~ /(AF|AN|NA|SA|EU|AS|OC)-?(\d?\d\d)/oi)) {
91                         $a = uc $a;
92                         $doiota = "\\b$a\[\-\ \]\?$b\\b";
93                         shift @list;
94                 }
95                 $doiota = '\b(IOTA|(AF|AN|NA|SA|EU|AS|OC)[- ]?\d?\d\d)\b' unless $doiota;
96                 next;
97         }
98         if (lc $f eq 'qra') {
99                 $doqra = uc shift @list if @list && $list[0] =~ /[A-Z][A-Z]\d\d/oi;
100                 $doqra = '\b([A-Z][A-Z]\d\d|[A-Z][A-Z]\d\d[A-Z][A-Z])\b' unless $doqra;
101                 next;
102         }
103         if (!$pre) {
104                 $pre = uc $f;
105         }
106 }
107
108 # first deal with the prefix
109 if ($pre) {
110         my @ans;
111         
112         if ($dxcc) {
113                 @ans = Prefix::extract($pre);   # is it a callsign/prefix?
114                 
115                 if (@ans) {
116
117                         # first deal with the prefix
118                         my $pre = shift @ans;
119                         my $a;
120                         my $str = "Prefix: $pre";
121                         my $l = length $str;
122                         my @expr;
123                         my @hint;
124                         
125                         # build up a search string for this dxcc country/countries
126                         foreach $a (@ans) {
127                                 my $n = $a->dxcc();
128                             push @expr, "\$f5 == $n";
129                                 push @hint, "m{$n}";
130                                 my $name = $a->name();
131                                 $str .= " Dxcc: $n ($name)";
132                                 push @out, $str;
133                                 $str = ' ' x $l;
134                         }
135                         $expr = @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
136                         $hint = @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
137                 }
138         } 
139         unless (@ans) {
140                 $pre .= '*' unless $pre =~ /[\*\?\[]/o;
141                 $pre = shellregex($pre);
142                 $expr = "\$f1 =~ m{$pre}";
143                 $pre =~ s/[\^\$]//g;
144                 $hint = "m{\U$pre}";
145         }
146 }
147   
148 # now deal with any frequencies specified
149 if (@freq) {
150         $expr .= ($expr) ? " && (" : "(";
151         $hint .= ($hint) ? " && (" : "(";
152         my $i;
153         for ($i = 0; $i < @freq; $i += 2) {
154                 $expr .= "(\$f0 >= $freq[$i] && \$f0 <= $freq[$i+1]) ||";
155                 my $r = Spot::ftor($freq[$i], $freq[$i+1]);
156 #               $hint .= "m{$r\\.} ||" if $r;
157                 $hint .= "m{\d+\.} ||";
158         }
159         chop $expr;     chop $expr;
160         chop $hint;     chop $hint;
161         $expr .= ")";
162         $hint .= ")";
163 }
164
165 # any info
166 if ($info) {
167         $expr .= " && " if $expr;
168         $info =~ s{(.)}{"\Q$1"}ge;
169         $expr .= "\$f3 =~ m{$info}i";
170         $hint .= " && " if $hint;
171         $hint .= "m{$info}i";
172 }
173
174 # any spotter
175 if ($spotter) {
176         
177         if ($fromdxcc) {
178                 @ans = Prefix::extract($spotter);       # is it a callsign/prefix?
179                 
180                 if (@ans) {
181
182                         # first deal with the prefix
183                         my $pre = shift @ans;
184                         my $a;
185                         $expr .= ' && ' if $expr;
186                         $hint .= ' && ' if $hint;
187                         my $str = "Spotter: $pre";
188                         my $l = length $str;
189                         my @expr;
190                         my @hint;
191                         
192                         # build up a search string for this dxcc country/countries
193                         foreach $a (@ans) {
194                                 my $n = $a->dxcc();
195                             push @expr, "\$f6 == $n";
196                                 push @hint, "m{$n}";
197                                 my $name = $a->name();
198                                 $str .= " Dxcc: $n ($name)";
199                                 push @out, $str;
200                                 $str = ' ' x $l;
201                         }
202                         $expr .= @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
203                         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
204                 }
205         } 
206         unless (@ans) {
207                 $expr .= " && " if $expr;
208                 $spotter .= '*' unless $spotter =~ /[\*\?\[]/o;
209                 $spotter = shellregex($spotter);
210                 $expr .= "\$f4 =~ m{\U$spotter}";
211                 $hint .= " && " if $hint;
212                 $spotter =~ s/[\^\$]//g;
213                 $hint .= "m{\U$spotter}";
214         }
215 }
216
217 # qsl requests
218 if ($doqsl) {
219         $expr .= " && " if $expr;
220         $expr .= "\$f3 =~ m{QSL|VIA}i";
221         $hint .= " && " if $hint;
222         $hint .= "m{QSL|VIA}i";
223 }
224
225 # iota requests
226 if ($doiota) {
227         $expr .= " && " if $expr;
228         $expr .= "\$f3 =~ m{$doiota}i";
229         $hint .= " && " if $hint;
230         $hint .= "m{$doiota}i";
231 }
232
233 # iota requests
234 if ($doqra) {
235         $expr .= " && " if $expr;
236         $expr .= "\$f3 =~ m{$doqra}i";
237         $hint .= " && " if $hint;
238         $hint .= "m{$doqra}io";
239 }
240
241 #print "expr: $expr from: $from to: $to fromday: $fromday today: $today\n";
242   
243 # now do the search
244 my @res = Spot::search($expr, $fromday, $today, $from, $to, $hint);
245 my $ref;
246 my @dx;
247 foreach $ref (@res) {
248         if ($real) {
249                 push @out, $self->format_dx_spot(@$ref);
250         } else {
251                 push @out, Spot::formatl(@$ref);
252         }
253 }
254
255 return (1, @out);