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