1203cb3b2abc99de6659339d2d871faa73c9bca6
[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 if $self->spotsfilter;
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 #$DB::single = 1;
143
144 # first deal with the prefix
145 if ($pre) {
146         my @ans;
147         
148         if ($dxcc) {
149                 @ans = Prefix::extract($pre);   # is it a callsign/prefix?
150                 
151                 if (@ans) {
152
153                         # first deal with the prefix
154                         my $pre = shift @ans;
155                         my $a;
156                         my $str = "Prefix: $pre";
157                         my $l = length $str;
158                         my @expr;
159                         my @hint;
160                         
161                         # build up a search string for this dxcc country/countries
162                         foreach $a (@ans) {
163                                 my $n = $a->dxcc();
164                             push @expr, "\$f5 == $n";
165                                 push @hint, "m{$n}";
166                                 my $name = $a->name();
167                                 $str .= " Dxcc: $n ($name)";
168                                 push @out, $str;
169                                 $str = ' ' x $l;
170                         }
171                         $expr = @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
172                         $hint = @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
173                 }
174         } 
175         unless (@ans) {
176                 $pre .= '*' unless $pre =~ /[\*\?\[]/o;
177                 $pre = shellregex($pre);
178                 $expr = "\$f1 =~ m{$pre}";
179                 $pre =~ s/[\^\$]//g;
180                 $hint = "m{\U$pre}";
181         }
182 }
183   
184 # now deal with any frequencies specified
185 if (@freq) {
186         $expr .= ($expr) ? ' && (' : "(";
187 #       $hint .= ($hint) ? ' && ' : "(";
188 #       $hint .= ' && ' if $hint;
189         my $i;
190         for ($i = 0; $i < @freq; $i += 2) {
191                 $expr .= "(\$f0 >= $freq[$i] && \$f0 <= $freq[$i+1]) ||";
192                 my $r = Spot::ftor($freq[$i], $freq[$i+1]);
193 #               $hint .= "m{$r\\.} ||" if $r;
194 #               $hint .= "m{\d+\.} ||";
195 #               $hint .= "1 ||";
196         }
197         chop $expr;     chop $expr;
198 #       chop $hint;     chop $hint;
199         $expr .= ")";
200 #       $hint .= ")";
201 }
202
203 # any info
204 if ($info) {
205         $expr .= ' && ' if $expr;
206         $info =~ s{(.)}{"\Q$1"}ge;
207         $expr .= "\$f3 =~ m{$info}i";
208         $hint .= ' && ' if $hint;
209         $hint .= "m{$info}i";
210 }
211
212 # any spotter
213 if ($spotter) {
214         
215         if ($fromdxcc) {
216                 @ans = Prefix::extract($spotter);       # is it a callsign/prefix?
217                 
218                 if (@ans) {
219
220                         # first deal with the prefix
221                         my $pre = shift @ans;
222                         my $a;
223                         $expr .= ' && ' if $expr;
224                         $hint .= ' && ' if $hint;
225                         my $str = "Spotter: $pre";
226                         my $l = length $str;
227                         my @expr;
228                         my @hint;
229                         
230                         # build up a search string for this dxcc country/countries
231                         foreach $a (@ans) {
232                                 my $n = $a->dxcc();
233                             push @expr, "\$f6 == $n";
234                                 push @hint, "m{$n}";
235                                 my $name = $a->name();
236                                 $str .= " Dxcc: $n ($name)";
237                                 push @out, $str;
238                                 $str = ' ' x $l;
239                         }
240                         $expr .= @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
241                         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
242                 }
243         } 
244         unless (@ans) {
245                 $expr .= ' && ' if $expr;
246                 $spotter .= '*' unless $spotter =~ /[\*\?\[]/o;
247                 $spotter = shellregex($spotter);
248                 $expr .= "\$f4 =~ m{\U$spotter}";
249                 $hint .= ' && ' if $hint;
250                 $spotter =~ s/[\^\$]//g;
251                 $hint .= "m{\U$spotter}";
252         }
253 }
254
255 # zone requests
256 if ($zone) {
257         my @expr;
258         my @hint;
259         $expr .= ' && ' if $expr;
260         $hint .= ' && ' if $hint;
261         for (split /[:,]/, $zone) {
262                 push @expr, "\$f9==$_";
263                 push @hint, "m{$_}";
264         }
265         $expr .= @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
266         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
267 }
268 if ($byzone) {
269         my @expr;
270         my @hint;
271         $expr .= ' && ' if $expr;
272         $hint .= ' && ' if $hint;
273         for (split /[:,]/, $byzone) {
274                 push @expr, "\$f11==$_";
275                 push @hint, "m{$_}";
276         }
277         $expr .= @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
278         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
279 }
280
281 # itu requests
282 if ($itu) {
283         my @expr;
284         my @hint;
285         $expr .= ' && ' if $expr;
286         $hint .= ' && ' if $hint;
287         for (split /[:,]/, $itu) {
288                 push @expr, "\$f8==$_";
289                 push @hint, "m{$_}";
290         }
291         $expr .= @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
292         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
293 }
294 if ($byitu) {
295         my @expr;
296         my @hint;
297         $expr .= ' && ' if $expr;
298         $hint .= ' && ' if $hint;
299         for (split /[:,]/, $byitu) {
300                 push @expr, "\$f10==$_";
301                 push @hint, "m{$_}";
302         }
303         $expr .= @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
304         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
305 }
306
307 # state requests
308 if ($state) {
309         my @expr;
310         my @hint;
311         $expr .= ' && ' if $expr;
312         $hint .= ' && ' if $hint;
313         for (split /[:,]/, $state) {
314                 push @expr, "\$f12 eq '$_'";
315                 push @hint, "m{$_}";
316         }
317         $expr .= @expr > 1 ? '($f12 && (' . join(' || ', @expr) . '))' : "(\$f12 && $expr[0])";
318         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
319 }
320 if ($bystate) {
321         my @expr;
322         my @hint;
323         $expr .= ' && ' if $expr;
324         $hint .= ' && ' if $hint;
325         for (split /[:,]/, $bystate) {
326                 push @expr, "\$f13 eq '$_'";
327                 push @hint, "m{$_}";
328         }
329         $expr .= @expr > 1 ? '($f13 && (' . join(' || ', @expr) . '))' : "(\$f13 && $expr[0])";
330         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
331 }
332
333 # qsl requests
334 if ($doqsl) {
335         $expr .= ' && ' if $expr;
336         $expr .= "\$f3 =~ m{QSL|VIA}i";
337         $hint .= ' && ' if $hint;
338         $hint .= "m{QSL|VIA}i";
339 }
340
341 # iota requests
342 if ($doiota) {
343         $expr .= ' && ' if $expr;
344         $expr .= "\$f3 =~ m{$doiota}i";
345         $hint .= ' && ' if $hint;
346         $hint .= "m{$doiota}i";
347 }
348
349 # iota requests
350 if ($doqra) {
351         $expr .= ' && ' if $expr;
352         $expr .= "\$f3 =~ m{$doqra}i";
353         $hint .= ' && ' if $hint;
354         $hint .= "m{$doqra}io";
355 }
356
357 #print "expr: $expr from: $from to: $to fromday: $fromday today: $today\n";
358   
359 # now do the search
360 my @res = Spot::search($expr, $fromday, $today, $from, $to, $hint, $dofilter ? $self : undef);
361 my $ref;
362 my @dx;
363 foreach $ref (@res) {
364         if ($self->ve7cc) {
365                 push @out, VE7CC::dx_spot($self, @$ref);
366         } else {
367                 if ($real) {
368                         push @out, $self->format_dx_spot($ref);
369                 } else {
370                         push @out, Spot::formatl(@$ref);
371                 }
372         }
373 }
374
375 return (1, @out);