don't fork sh/dx on simple query
[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         # disguise regexes
15         $line =~ s/\{(.*?)\}/'{'. unpack('H*', $1) . '}'/eg;
16         dbg("sh/dx disguise any regex: '$line'") if isdbg('sh/dx');
17
18         # now space out brackets and !
19         $line =~ s/([\(\!\)])/ $1 /g;
20         
21         my @list = split /\s+/, $line; # split the line up
22
23         # put back the regexes 
24         @list = map { my $l = $_; $l =~ s/\{([0-9a-fA-F]+)\}/'{' . pack('H*', $1) . '}'/eg; $l } @list;
25
26         dbg("sh/dx after regex return: '" . join(' ', @list) . "'") if isdbg('sh/dx');
27         
28         my @out;
29         my $f;
30         my $call = $self->call;
31         my $usesql = $main::dbh && $Spot::use_db_for_search;
32         my ($from, $to) = (0, 0);
33         my ($fromday, $today) = (0, 0);
34         my $exact;
35         my $real;
36         my $dofilter;
37         my $pre;
38         my $dxcc;
39
40         my @flist;
41
42         
43         dbg("sh/dx list: " . join(" ", @list)) if isdbg('sh/dx');
44         
45         while (@list) { # next field
46                 $f = shift @list;
47                 dbg("sh/dx arg: $f list: '" . join(',', @list) . "'") if isdbg('sh/dx');
48                 if ($f && !$from && !$to) {
49                         ($from, $to) = $f =~ m|^(\d+)[-/](\d+)$| || (0,0); # is it a from -> to count?
50                         dbg("sh/dx from: $from to: $to") if isdbg('sh/dx');
51                         next if $from && $to > $from;
52                 }
53                 if ($f && !$to) {
54                         ($to) = $f =~ /^(\d+)$/o if !$to; # is it a to count?
55                         $to ||= 0;
56                         dbg("sh/dx to: $to") if isdbg('sh/dx');
57                         next if $to;
58                 }
59                 if (lc $f eq 'day' && $list[0]) {
60                         ($fromday, $today) = split m|[-/]|, shift(@list);
61                         dbg("sh/dx got day $fromday/$today") if isdbg('sh/dx');
62                         next;
63                 }
64                 if (lc $f eq 'exact') {
65                         dbg("sh/dx exact") if isdbg('sh/dx');
66                         $exact = 1;
67                         next;
68                 }
69                 if (lc $f eq 'rt' || $f =~ /^real/i) {
70                         dbg("sh/dx real") if isdbg('sh/dx');
71                         $real = 1;
72                         next;
73                 }
74                 if (lc $f =~ /^filt/) {
75                         dbg("sh/dx run spotfilter") if isdbg('sh/dx');
76                         $dofilter = 1 if $self && $self->spotsfilter;
77                         next;
78                 }
79                 if (lc $f eq 'qsl') {
80                         dbg("sh/dx qsl") if isdbg('sh/dx');
81                         push @flist, "info {QSL|VIA}";
82                         next;
83                 }
84                 if (lc $f eq '<es>') {
85                         dbg("sh/dx <es>") if isdbg('sh/dx');
86                         push @flist, "info {<ES>}";
87                         next;
88                 }
89                 if (lc $f eq '<tr>') {
90                         dbg("sh/dx <es>") if isdbg('sh/dx');
91                         push @flist, "info {<TR>}";
92                         next;
93                 }
94                 if (lc $f eq '<ms>') {
95                         dbg("sh/dx <ms>") if isdbg('sh/dx');
96                         push @flist, "info {<ms>}";
97                         next;
98                 }
99
100                 if (lc $f eq 'iota') {
101                         my $doiota;
102                         if (@list && $list[0] && (($a, $b) = $list[0] =~ /(AF|AN|NA|SA|EU|AS|OC)[-\s]?(\d\d?\d?)/i)) {
103                                 $a = uc $a;
104                                 $doiota = "\\b$a\[\-\ \]\?$b\\b";
105                                 shift @list;
106                         }
107                         $doiota = '\b(IOTA|(AF|AN|NA|SA|EU|AS|OC)[-\s]?\d?\d\d)\b' unless $doiota;
108                         push @flist, 'info', "{$doiota}";
109                         dbg("sh/dx iota info {$doiota}") if isdbg('sh/dx');
110                         next;
111                 }
112                 if (lc $f eq 'qra') {
113                         my $doqra = uc shift @list if @list && $list[0] =~ /[A-Z][A-Z]\d\d/i;
114                         $doqra = '\b([A-Z][A-Z]\d\d|[A-Z][A-Z]\d\d[A-Z][A-Z])\b' unless $doqra;
115                         push @flist, 'info',  "{$doqra}";
116                         dbg("sh/dx qra info {$doqra}") if isdbg('sh/dx');
117                         next;
118                 }
119                 if (grep {lc $f eq $_} qw { ( or and not ) }) {
120                         push @flist, $f;
121                         dbg("sh/dx operator $f") if isdbg('sh/dx');
122                         next;
123                 }
124                 if (grep {lc $f eq $_} qw(on freq call info spotter by dxcc call_dxcc by_dxcc bydxcc origin call_itu itu call_zone zone cq bycq  byitu by_itu by_zone byzone call_state state bystate by_state ip) ) {
125                         push @flist, $f;
126                         push @flist, shift @list if @list;
127                         dbg("sh/dx function $flist[-2] $flist[-1]") if isdbg('sh/dx');
128                         next;
129                 }
130                 unless ($pre) {
131                         $pre = $f;
132                         next;
133                 }
134                 push @flist, $f;
135         }
136
137         dbg("sh/dx: flist = '" . join(',', @flist). "'") if isdbg('sh/dx');
138         
139         if ($pre) {
140                 # someone (probably me) has forgotten the 'info' keyword
141                 if ($pre =~ /^{.*}$/) {
142                         push @flist, 'info', $pre;
143                 } else {
144                         $pre .= '*' unless $pre =~ /[\*\?\[]$/o;
145                         $pre = shellregex($pre);
146                         $pre =~ s/\.\*\$$//;
147                         $pre .= '$' if $exact;
148                         $pre =~ s/\^//;
149                         push @flist, 'call', $pre;
150                 }
151         }
152         
153     my $newline = join(' ', @flist);
154         dbg("sh/dx newline: '$newline'") if isdbg('sh/dx');
155         my ($r, $filter, $fno, $user, $expr) = $Spot::filterdef->parse($self, 'spots', $newline, 1);
156
157         return (0, "sh/dx parse error '$r' " . $filter) if $r;
158
159         $user ||= '';
160         $expr ||= '';
161         dbg("sh/dx user: $user expr: $expr from: $from to: $to fromday: $fromday today: $today") if isdbg('sh/dx');
162   
163         # now do the search
164
165         if ($self->{_nospawn} || ($Spot::spotcachedays && !$expr && $from == 0 && $fromday == 0 && $today == 0)) {
166                 my @res = Spot::search($expr, $fromday, $today, $from, $to, $user, $dofilter, $self);
167                 my $ref;
168                 my @dx;
169                 foreach $ref (@res) {
170                         if ($self && $self->ve7cc) {
171                                 push @out, VE7CC::dx_spot($self, @$ref);
172                         }
173                         else {
174                                 if ($self && $real) {
175                                         push @out, DXCommandmode::format_dx_spot($self, @$ref);
176                                 }
177                                 else {
178                                         push @out, Spot::formatl(@$ref);
179                                 }
180                         }
181                 }
182         }
183         else {
184                 push @out, $self->spawn_cmd("sh/dx $line", \&Spot::search, 
185                                                                         args => [$expr, $fromday, $today, $from, $to, $filter, $dofilter, $self],
186                                                                         cb => sub {
187                                                                                 my ($dxchan, @res) = @_; 
188                                                                                 my $ref;
189                                                                                 my @out;
190
191                                                                                 foreach $ref (@res) {
192                                                                                         if ($self->ve7cc) {
193                                                                                                 push @out, VE7CC::dx_spot($self, @$ref);
194                                                                                         }
195                                                                                         else {
196                                                                                                 if ($real) {
197                                                                                                         push @out, DXCommandmode::format_dx_spot($self, @$ref);
198                                                                                                 }
199                                                                                                 else {
200                                                                                                         push @out, Spot::formatl(@$ref);
201                                                                                                 }
202                                                                                         }
203                                                                                 }
204                                                                                 push @out, $self->msg('e3', "sh/dx", "'$line'") unless @out;
205                                                                                 return @out;
206                                                                         });
207         }
208
209
210         return (1, @out);
211 }
212
213