tidy up prefix handling
[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 $zone;
25 my $byzone;
26 my $state;
27 my $bystate;
28 my $itu;
29 my $byitu;
30 my $fromdxcc;
31 my ($doqsl, $doiota, $doqra, $dofilter);
32
33 while ($f = shift @list) {              # next field
34         #  print "f: $f list: ", join(',', @list), "\n";
35         if (!$from && !$to) {
36                 ($from, $to) = $f =~ m|^(\d+)[-/](\d+)$|; # is it a from -> to count?
37                 next if $from && $to > $from;
38         }
39         if (!$to) {
40                 ($to) = $f =~ /^(\d+)$/o if !$to; # is it a to count?
41                 next if $to;
42         }
43         if (lc $f eq 'dxcc') {
44                 $dxcc = 1;
45                 next;
46         }
47         if (lc $f eq 'rt' || $f =~ /^real/i) {
48                 $real = 1;
49                 next;
50         }
51         if (lc $f eq 'on' && $list[0]) { # is it freq range?
52                 #    print "yup freq\n";
53                 if ($list[0] =~ m|^(\d+)(?:\.\d+)?[-/](\d+)(?:\.\d+)?$|) {
54                         push @freq, $1, $2;
55                         shift @list;
56                         next;
57                 } else {
58                         my @r = split '/', lc $list[0];
59                         # print "r0: $r[0] r1: $r[1]\n";
60                         my @fr = Bands::get_freq($r[0], $r[1]);
61                         if (@fr) {                      # yup, get rid of extranous param
62                                 #         print "freq: ", join(',', @fr), "\n";
63                                 push @freq, @fr;    # add these to the list
64                                 shift @list;
65                                 next;
66                         }
67                 }
68         }
69         if (lc $f eq 'day' && $list[0]) {
70                 #   print "got day\n";
71                 ($fromday, $today) = split m|[-/]|, shift(@list);
72                 next;
73         }
74         if (lc $f eq 'info' && $list[0]) {
75                 #   print "got info\n";
76                 $info = shift @list;
77                 next;
78         }
79         if ((lc $f eq 'spotter' || lc $f eq 'by') && $list[0]) {
80                 #    print "got spotter\n";
81                 $spotter = uc shift @list;
82                 if ($list[0] && lc $list[0] eq 'dxcc') {
83                         $fromdxcc = 1;
84                         shift @list;
85                 }
86                 next;
87         }
88         if (lc $f =~ /^filt/) {
89                 $dofilter = 1;
90                 next;
91         }
92         if (lc $f eq 'qsl') {
93                 $doqsl = 1;
94                 next;
95         }
96         if (lc $f eq 'iota') {
97                 my ($a, $b);
98 #               $DB::single =1;
99                 
100                 if (@list && $list[0] && (($a, $b) = $list[0] =~ /(AF|AN|NA|SA|EU|AS|OC)-?(\d?\d\d)/oi)) {
101                         $a = uc $a;
102                         $doiota = "\\b$a\[\-\ \]\?$b\\b";
103                         shift @list;
104                 }
105                 $doiota = '\b(IOTA|(AF|AN|NA|SA|EU|AS|OC)[- ]?\d?\d\d)\b' unless $doiota;
106                 next;
107         }
108         if (lc $f eq 'qra') {
109                 $doqra = uc shift @list if @list && $list[0] =~ /[A-Z][A-Z]\d\d/oi;
110                 $doqra = '\b([A-Z][A-Z]\d\d|[A-Z][A-Z]\d\d[A-Z][A-Z])\b' unless $doqra;
111                 next;
112         }
113         if (lc $f eq 'zone') {
114                 $zone = shift @list if @list;
115                 next;
116         }
117         if (lc $f =~ /^by_?zone/) {
118                 $byzone = shift @list if @list;
119                 next;
120         }
121         if (lc $f eq 'itu') {
122                 $itu = shift @list if @list;
123                 next;
124         }
125         if (lc $f =~ /^by_?itu/) {
126                 $byitu = shift @list if @list;
127                 next;
128         }
129         if (lc $f eq 'state') {
130                 $state = uc shift @list if @list;
131                 next;
132         }
133         if (lc $f =~ /^by_?state/) {
134                 $bystate = uc shift @list if @list;
135                 next;
136         }
137         if (!$pre) {
138                 $pre = uc $f;
139         }
140 }
141
142 # first deal with the prefix
143 if ($pre) {
144         my @ans;
145         
146         if ($dxcc) {
147                 @ans = Prefix::extract($pre);   # is it a callsign/prefix?
148                 
149                 if (@ans) {
150
151                         # first deal with the prefix
152                         my $pre = shift @ans;
153                         my $a;
154                         my $str = "Prefix: $pre";
155                         my $l = length $str;
156                         my @expr;
157                         my @hint;
158                         
159                         # build up a search string for this dxcc country/countries
160                         foreach $a (@ans) {
161                                 my $n = $a->dxcc();
162                             push @expr, "\$f5 == $n";
163                                 push @hint, "m{$n}";
164                                 my $name = $a->name();
165                                 $str .= " Dxcc: $n ($name)";
166                                 push @out, $str;
167                                 $str = ' ' x $l;
168                         }
169                         $expr = @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
170                         $hint = @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
171                 }
172         } 
173         unless (@ans) {
174                 $pre .= '*' unless $pre =~ /[\*\?\[]/o;
175                 $pre = shellregex($pre);
176                 $expr = "\$f1 =~ m{$pre}";
177                 $pre =~ s/[\^\$]//g;
178                 $hint = "m{\U$pre}";
179         }
180 }
181   
182 # now deal with any frequencies specified
183 if (@freq) {
184         $expr .= ($expr) ? " && (" : "(";
185 #       $hint .= ($hint) ? " && (" : "(";
186         my $i;
187         for ($i = 0; $i < @freq; $i += 2) {
188                 $expr .= "(\$f0 >= $freq[$i] && \$f0 <= $freq[$i+1]) ||";
189                 my $r = Spot::ftor($freq[$i], $freq[$i+1]);
190 #               $hint .= "m{$r\\.} ||" if $r;
191 #               $hint .= "m{\d+\.} ||";
192 #               $hint .= "1 ||";
193         }
194         chop $expr;     chop $expr;
195 #       chop $hint;     chop $hint;
196         $expr .= ")";
197 #       $hint .= ")";
198 }
199
200 # any info
201 if ($info) {
202         $expr .= " && " if $expr;
203         $info =~ s{(.)}{"\Q$1"}ge;
204         $expr .= "\$f3 =~ m{$info}i";
205         $hint .= " && " if $hint;
206         $hint .= "m{$info}i";
207 }
208
209 # any spotter
210 if ($spotter) {
211         
212         if ($fromdxcc) {
213                 @ans = Prefix::extract($spotter);       # is it a callsign/prefix?
214                 
215                 if (@ans) {
216
217                         # first deal with the prefix
218                         my $pre = shift @ans;
219                         my $a;
220                         $expr .= ' && ' if $expr;
221                         $hint .= ' && ' if $hint;
222                         my $str = "Spotter: $pre";
223                         my $l = length $str;
224                         my @expr;
225                         my @hint;
226                         
227                         # build up a search string for this dxcc country/countries
228                         foreach $a (@ans) {
229                                 my $n = $a->dxcc();
230                             push @expr, "\$f6 == $n";
231                                 push @hint, "m{$n}";
232                                 my $name = $a->name();
233                                 $str .= " Dxcc: $n ($name)";
234                                 push @out, $str;
235                                 $str = ' ' x $l;
236                         }
237                         $expr .= @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
238                         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
239                 }
240         } 
241         unless (@ans) {
242                 $expr .= " && " if $expr;
243                 $spotter .= '*' unless $spotter =~ /[\*\?\[]/o;
244                 $spotter = shellregex($spotter);
245                 $expr .= "\$f4 =~ m{\U$spotter}";
246                 $hint .= " && " if $hint;
247                 $spotter =~ s/[\^\$]//g;
248                 $hint .= "m{\U$spotter}";
249         }
250 }
251
252 # zone requests
253 if ($zone) {
254         my @expr;
255         my @hint;
256         for (split /[:,]/, $zone) {
257                 push @expr, "\$f9==$_";
258                 push @hint, "m{$_}";
259         }
260         $expr .= @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
261         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
262 }
263 if ($byzone) {
264         my @expr;
265         my @hint;
266         for (split /[:,]/, $byzone) {
267                 push @expr, "\$f11==$_";
268                 push @hint, "m{$_}";
269         }
270         $expr .= @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
271         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
272 }
273
274 # itu requests
275 if ($itu) {
276         my @expr;
277         my @hint;
278         for (split /[:,]/, $itu) {
279                 push @expr, "\$f8==$_";
280                 push @hint, "m{$_}";
281         }
282         $expr .= @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
283         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
284 }
285 if ($byitu) {
286         my @expr;
287         my @hint;
288         for (split /[:,]/, $byitu) {
289                 push @expr, "\$f10==$_";
290                 push @hint, "m{$_}";
291         }
292         $expr .= @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
293         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
294 }
295
296 # state requests
297 if ($state) {
298         my @expr;
299         my @hint;
300         for (split /[:,]/, $state) {
301                 push @expr, "\$f12 eq '$_'";
302                 push @hint, "m{$_}";
303         }
304         $expr .= @expr > 1 ? '($f12 && (' . join(' || ', @expr) . '))' : "(\$f12 && $expr[0])";
305         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
306 }
307 if ($bystate) {
308         my @expr;
309         my @hint;
310         for (split /[:,]/, $bystate) {
311                 push @expr, "\$f13 eq '$_'";
312                 push @hint, "m{$_}";
313         }
314         $expr .= @expr > 1 ? '($f13 && (' . join(' || ', @expr) . '))' : "(\$f13 && $expr[0])";
315         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
316 }
317
318 # qsl requests
319 if ($doqsl) {
320         $expr .= " && " if $expr;
321         $expr .= "\$f3 =~ m{QSL|VIA}i";
322         $hint .= " && " if $hint;
323         $hint .= "m{QSL|VIA}i";
324 }
325
326 # iota requests
327 if ($doiota) {
328         $expr .= " && " if $expr;
329         $expr .= "\$f3 =~ m{$doiota}i";
330         $hint .= " && " if $hint;
331         $hint .= "m{$doiota}i";
332 }
333
334 # iota requests
335 if ($doqra) {
336         $expr .= " && " if $expr;
337         $expr .= "\$f3 =~ m{$doqra}i";
338         $hint .= " && " if $hint;
339         $hint .= "m{$doqra}io";
340 }
341
342 #print "expr: $expr from: $from to: $to fromday: $fromday today: $today\n";
343   
344 # now do the search
345 my @res = Spot::search($expr, $fromday, $today, $from, $to, $hint, $dofilter ? $self : undef);
346 my $ref;
347 my @dx;
348 foreach $ref (@res) {
349         if ($real) {
350                 push @out, $self->format_dx_spot(@$ref);
351         } else {
352                 push @out, Spot::formatl(@$ref);
353         }
354 }
355
356 return (1, @out);