add sh/mydx alias to allow filtered spots
[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, $dofilter);
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 =~ /^filt/) {
83                 $dofilter = 1;
84                 next;
85         }
86         if (lc $f eq 'qsl') {
87                 $doqsl = 1;
88                 next;
89         }
90         if (lc $f eq 'iota') {
91                 my ($a, $b);
92 #               $DB::single =1;
93                 
94                 if (@list && $list[0] && (($a, $b) = $list[0] =~ /(AF|AN|NA|SA|EU|AS|OC)-?(\d?\d\d)/oi)) {
95                         $a = uc $a;
96                         $doiota = "\\b$a\[\-\ \]\?$b\\b";
97                         shift @list;
98                 }
99                 $doiota = '\b(IOTA|(AF|AN|NA|SA|EU|AS|OC)[- ]?\d?\d\d)\b' unless $doiota;
100                 next;
101         }
102         if (lc $f eq 'qra') {
103                 $doqra = uc shift @list if @list && $list[0] =~ /[A-Z][A-Z]\d\d/oi;
104                 $doqra = '\b([A-Z][A-Z]\d\d|[A-Z][A-Z]\d\d[A-Z][A-Z])\b' unless $doqra;
105                 next;
106         }
107         if (!$pre) {
108                 $pre = uc $f;
109         }
110 }
111
112 # first deal with the prefix
113 if ($pre) {
114         my @ans;
115         
116         if ($dxcc) {
117                 @ans = Prefix::extract($pre);   # is it a callsign/prefix?
118                 
119                 if (@ans) {
120
121                         # first deal with the prefix
122                         my $pre = shift @ans;
123                         my $a;
124                         my $str = "Prefix: $pre";
125                         my $l = length $str;
126                         my @expr;
127                         my @hint;
128                         
129                         # build up a search string for this dxcc country/countries
130                         foreach $a (@ans) {
131                                 my $n = $a->dxcc();
132                             push @expr, "\$f5 == $n";
133                                 push @hint, "m{$n}";
134                                 my $name = $a->name();
135                                 $str .= " Dxcc: $n ($name)";
136                                 push @out, $str;
137                                 $str = ' ' x $l;
138                         }
139                         $expr = @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
140                         $hint = @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
141                 }
142         } 
143         unless (@ans) {
144                 $pre .= '*' unless $pre =~ /[\*\?\[]/o;
145                 $pre = shellregex($pre);
146                 $expr = "\$f1 =~ m{$pre}";
147                 $pre =~ s/[\^\$]//g;
148                 $hint = "m{\U$pre}";
149         }
150 }
151   
152 # now deal with any frequencies specified
153 if (@freq) {
154         $expr .= ($expr) ? " && (" : "(";
155         $hint .= ($hint) ? " && (" : "(";
156         my $i;
157         for ($i = 0; $i < @freq; $i += 2) {
158                 $expr .= "(\$f0 >= $freq[$i] && \$f0 <= $freq[$i+1]) ||";
159                 my $r = Spot::ftor($freq[$i], $freq[$i+1]);
160 #               $hint .= "m{$r\\.} ||" if $r;
161 #               $hint .= "m{\d+\.} ||";
162                 $hint .= "1 ||";
163         }
164         chop $expr;     chop $expr;
165         chop $hint;     chop $hint;
166         $expr .= ")";
167         $hint .= ")";
168 }
169
170 # any info
171 if ($info) {
172         $expr .= " && " if $expr;
173         $info =~ s{(.)}{"\Q$1"}ge;
174         $expr .= "\$f3 =~ m{$info}i";
175         $hint .= " && " if $hint;
176         $hint .= "m{$info}i";
177 }
178
179 # any spotter
180 if ($spotter) {
181         
182         if ($fromdxcc) {
183                 @ans = Prefix::extract($spotter);       # is it a callsign/prefix?
184                 
185                 if (@ans) {
186
187                         # first deal with the prefix
188                         my $pre = shift @ans;
189                         my $a;
190                         $expr .= ' && ' if $expr;
191                         $hint .= ' && ' if $hint;
192                         my $str = "Spotter: $pre";
193                         my $l = length $str;
194                         my @expr;
195                         my @hint;
196                         
197                         # build up a search string for this dxcc country/countries
198                         foreach $a (@ans) {
199                                 my $n = $a->dxcc();
200                             push @expr, "\$f6 == $n";
201                                 push @hint, "m{$n}";
202                                 my $name = $a->name();
203                                 $str .= " Dxcc: $n ($name)";
204                                 push @out, $str;
205                                 $str = ' ' x $l;
206                         }
207                         $expr .= @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
208                         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
209                 }
210         } 
211         unless (@ans) {
212                 $expr .= " && " if $expr;
213                 $spotter .= '*' unless $spotter =~ /[\*\?\[]/o;
214                 $spotter = shellregex($spotter);
215                 $expr .= "\$f4 =~ m{\U$spotter}";
216                 $hint .= " && " if $hint;
217                 $spotter =~ s/[\^\$]//g;
218                 $hint .= "m{\U$spotter}";
219         }
220 }
221
222 # qsl requests
223 if ($doqsl) {
224         $expr .= " && " if $expr;
225         $expr .= "\$f3 =~ m{QSL|VIA}i";
226         $hint .= " && " if $hint;
227         $hint .= "m{QSL|VIA}i";
228 }
229
230 # iota requests
231 if ($doiota) {
232         $expr .= " && " if $expr;
233         $expr .= "\$f3 =~ m{$doiota}i";
234         $hint .= " && " if $hint;
235         $hint .= "m{$doiota}i";
236 }
237
238 # iota requests
239 if ($doqra) {
240         $expr .= " && " if $expr;
241         $expr .= "\$f3 =~ m{$doqra}i";
242         $hint .= " && " if $hint;
243         $hint .= "m{$doqra}io";
244 }
245
246 #print "expr: $expr from: $from to: $to fromday: $fromday today: $today\n";
247   
248 # now do the search
249 my @res = Spot::search($expr, $fromday, $today, $from, $to, $hint, $dofilter);
250 my $ref;
251 my @dx;
252 foreach $ref (@res) {
253         if ($real) {
254                 push @out, $self->format_dx_spot(@$ref);
255         } else {
256                 push @out, Spot::formatl(@$ref);
257         }
258 }
259
260 return (1, @out);