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