174071f8bc3b8f20edf2262ca912974ba0e4b228
[spider.git] / cmd / show / dx.pl
1 #
2 # show dx (normal)
3 #
4 #
5 #
6
7 require 5.10.1;
8
9 sub handle
10 {
11         my ($self, $line) = @_;
12         my @list = split /\s+/, $line; # split the line up
13
14         my @out;
15         my $f;
16         my $call = $self->call;
17         my $usesql = $main::dbh && $Spot::use_db_for_search;
18         my ($from, $to);
19         my ($fromday, $today);
20         my $exact;
21         my $real;
22         my $user;
23         my $expr;
24         my $dofilter;
25         my $pre;
26         my $dxcc;
27
28         my @flist;
29
30         while ($f = shift @list) {      # next field
31                 dbg "sh/dx arg: $f list: " . join(',', @list) if isdbg('sh/dx');
32                 if (!$from && !$to) {
33                         ($from, $to) = $f =~ m|^(\d+)[-/](\d+)$|; # is it a from -> to count?
34                         dbg("sh/dx from: $from to: $to") if isdbg('sh/dx');
35                         next if $from && $to > $from;
36                 }
37                 if (!$to) {
38                         ($to) = $f =~ /^(\d+)$/o if !$to; # is it a to count?
39                         dbg("sh/dx to: $to") if isdbg('sh/dx');
40                         next if $to;
41                 }
42                 if (lc $f eq 'day' && $list[0]) {
43                         ($fromday, $today) = split m|[-/]|, shift(@list);
44                         dbg "sh/dx got day $fromday/$today" if isdbg('sh/dx');
45                         next;
46                 }
47                 if (lc $f eq 'exact') {
48                         dbg("sh/dx exact") if isdbg('sh/dx');
49                         $exact = 1;
50                         next;
51                 }
52                 if (lc $f eq 'rt' || $f =~ /^real/i) {
53                         dbg("sh/dx real") if isdbg('sh/dx');
54                         $real = 1;
55                         next;
56                 }
57                 if (lc $f =~ /^filt/) {
58                         dbg("sh/dx run spotfilter") if isdbg('sh/dx');
59                         $dofilter = 1 if $self && $self->spotsfilter;
60                         next;
61                 }
62                 if (lc $f eq 'spotter') {
63                         dbg("sh/dx by") if isdbg('sh/dx');
64                         push @flist, 'by';
65                         next;
66                 }
67                 if (lc $f eq 'qsl') {
68                         dbg("sh/dx qsl") if isdbg('sh/dx');
69                         push @flist, "info {QSL|VIA}";
70                         next;
71                 }
72                 if (lc $f eq 'iota') {
73                         my $doiota;
74                         if (@list && $list[0] && (($a, $b) = $list[0] =~ /(AF|AN|NA|SA|EU|AS|OC)-?(\d?\d\d)/oi)) {
75                                 $a = uc $a;
76                                 $doiota = "\\b$a\[\-\ \]\?$b\\b";
77                                 shift @list;
78                         }
79                         $doiota = '\b(IOTA|(AF|AN|NA|SA|EU|AS|OC)[- ]?\d?\d\d)\b' unless $doiota;
80                         push @flist, "info {$doiota}";
81                         dbg("sh/dx iota") if isdbg('sh/dx');
82                         next;
83                 }
84                 if (lc $f eq 'qra') {
85                         my $doqra = uc shift @list if @list && $list[0] =~ /[A-Z][A-Z]\d\d/oi;
86                         $doqra = '\b([A-Z][A-Z]\d\d|[A-Z][A-Z]\d\d[A-Z][A-Z])\b' unless $doqra;
87                         push @flist, "info {$doqra}";
88                         dbg("sh/dx qra") if isdbg('sh/dx');
89                         next;
90                 }
91                 if (grep {lc $f eq $_} qw(zone byzone by_zone itu byitu by_itu state bystate by_state on) ) {
92                         $f =~ s/^by(\w)/by_$1/;
93                         push @flist, $f;
94                         push @flist, shift @list if @list;
95                         next;
96                 }
97                 unless ($pre) {
98                         $pre = $f;
99                         next;
100                 }
101                 push @flist, $f;
102         }
103
104         
105         if ($pre) {
106                 $pre .= '*' unless $pre =~ /[\*\?\[]$/o;
107                 $pre = shellregex($pre);
108                 if ($usesql) {
109                         $pre =~ s/\.\*/%/g;
110                 } else {
111                         $pre =~ s/\.\*\$$//;
112                 }
113                 $pre .= '$' if $exact;
114                 $pre =~ s/\^//;
115                 
116                 push @flist, 'call', $pre;
117         }
118         
119     my $newline = join(' ', @flist);
120         dbg("sh/dx newline: $newline") if isdbg('sh/dx');
121         my ($r, $filter, $fno, $user, $expr) = $Spot::filterdef->parse($self, 'spots', $newline, 1);
122
123         return (0, "sh/dx parse error '$r' " . $filter) if $r;
124         
125         dbg "sh/dx user: $user expr: $expr from: $from to: $to fromday: $fromday today: $today" if isdbg('sh/dx');
126   
127         # now do the search
128
129         if ($self->{_nospawn}) {
130                 my @res = Spot::search($expr, $fromday, $today, $from, $to, $user, $dofilter ? $self : undef);
131                 my $ref;
132                 my @dx;
133                 foreach $ref (@res) {
134                         if ($self && $self->ve7cc) {
135                                 push @out, VE7CC::dx_spot($self, @$ref);
136                         }
137                         else {
138                                 if ($self && $real) {
139                                         push @out, DXCommandmode::format_dx_spot($self, @$ref);
140                                 }
141                                 else {
142                                         push @out, Spot::formatl(@$ref);
143                                 }
144                         }
145                 }
146         }
147         else {
148                 push @out, $self->spawn_cmd("sh/dx $line", \&Spot::search, 
149                                                                         args => [$expr, $fromday, $today, $from, $to, $filter, $dofilter ? $self : undef],
150                                                                         cb => sub {
151                                                                                 my ($dxchan, @res) = @_; 
152                                                                                 my $ref;
153                                                                                 my @out;
154
155                                                                                 foreach $ref (@res) {
156                                                                                         if ($self->ve7cc) {
157                                                                                                 push @out, VE7CC::dx_spot($self, @$ref);
158                                                                                         }
159                                                                                         else {
160                                                                                                 if ($real) {
161                                                                                                         push @out, DXCommandmode::format_dx_spot($self, @$ref);
162                                                                                                 }
163                                                                                                 else {
164                                                                                                         push @out, Spot::formatl(@$ref);
165                                                                                                 }
166                                                                                         }
167                                                                                 }
168                                                                                 push @out, $self->msg('e3', "sh/dx", "'$line'") unless @out;
169                                                                                 return @out;
170                                                                         });
171         }
172
173
174         return (1, @out);
175 }
176
177