aac5fdaf27dde51d5f614642f274fef62d4f0e83
[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                 $hint .= "1 ||";
159         }
160         chop $expr;     chop $expr;
161         chop $hint;     chop $hint;
162         $expr .= ")";
163         $hint .= ")";
164 }
165
166 # any info
167 if ($info) {
168         $expr .= " && " if $expr;
169         $info =~ s{(.)}{"\Q$1"}ge;
170         $expr .= "\$f3 =~ m{$info}i";
171         $hint .= " && " if $hint;
172         $hint .= "m{$info}i";
173 }
174
175 # any spotter
176 if ($spotter) {
177         
178         if ($fromdxcc) {
179                 @ans = Prefix::extract($spotter);       # is it a callsign/prefix?
180                 
181                 if (@ans) {
182
183                         # first deal with the prefix
184                         my $pre = shift @ans;
185                         my $a;
186                         $expr .= ' && ' if $expr;
187                         $hint .= ' && ' if $hint;
188                         my $str = "Spotter: $pre";
189                         my $l = length $str;
190                         my @expr;
191                         my @hint;
192                         
193                         # build up a search string for this dxcc country/countries
194                         foreach $a (@ans) {
195                                 my $n = $a->dxcc();
196                             push @expr, "\$f6 == $n";
197                                 push @hint, "m{$n}";
198                                 my $name = $a->name();
199                                 $str .= " Dxcc: $n ($name)";
200                                 push @out, $str;
201                                 $str = ' ' x $l;
202                         }
203                         $expr .= @expr > 1 ? '(' . join(' || ', @expr) . ')' : $expr[0];
204                         $hint .= @hint > 1 ? '(' . join(' || ', @hint) . ')' : $hint[0];
205                 }
206         } 
207         unless (@ans) {
208                 $expr .= " && " if $expr;
209                 $spotter .= '*' unless $spotter =~ /[\*\?\[]/o;
210                 $spotter = shellregex($spotter);
211                 $expr .= "\$f4 =~ m{\U$spotter}";
212                 $hint .= " && " if $hint;
213                 $spotter =~ s/[\^\$]//g;
214                 $hint .= "m{\U$spotter}";
215         }
216 }
217
218 # qsl requests
219 if ($doqsl) {
220         $expr .= " && " if $expr;
221         $expr .= "\$f3 =~ m{QSL|VIA}i";
222         $hint .= " && " if $hint;
223         $hint .= "m{QSL|VIA}i";
224 }
225
226 # iota requests
227 if ($doiota) {
228         $expr .= " && " if $expr;
229         $expr .= "\$f3 =~ m{$doiota}i";
230         $hint .= " && " if $hint;
231         $hint .= "m{$doiota}i";
232 }
233
234 # iota requests
235 if ($doqra) {
236         $expr .= " && " if $expr;
237         $expr .= "\$f3 =~ m{$doqra}i";
238         $hint .= " && " if $hint;
239         $hint .= "m{$doqra}io";
240 }
241
242 #print "expr: $expr from: $from to: $to fromday: $fromday today: $today\n";
243   
244 # now do the search
245 my @res = Spot::search($expr, $fromday, $today, $from, $to, $hint);
246 my $ref;
247 my @dx;
248 foreach $ref (@res) {
249         if ($real) {
250                 push @out, $self->format_dx_spot(@$ref);
251         } else {
252                 push @out, Spot::formatl(@$ref);
253         }
254 }
255
256 return (1, @out);