fix the small whoopsie in sh/dx
[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)[-\s]?(\d\d?\d?)/i)) {
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)[-\s]?\d?\d\d)\b' unless $doiota;
80                         push @flist, 'info', "{$doiota}";
81                         dbg("sh/dx iota info {$doiota}") 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/i;
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 info {$doqra}") 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(on freq call info spotter by call_dxcc by_dxcc bydxcc origin call_itu itu call_zone zone  byitu by_itu by_zone byzone call_state state bystate by_state ip) ) {
97                         push @flist, $f;
98                         push @flist, shift @list if @list;
99                         dbg("sh/dx function $flist[-2] $flist[-1]") if isdbg('sh/dx');
100                         next;
101                 }
102                 unless ($pre) {
103                         $pre = $f;
104                         next;
105                 }
106                 push @flist, $f;
107         }
108
109         
110         if ($pre) {
111                 # someone (probably me) has forgotten the 'info' keyword
112                 if ($pre =~ /^{.*}$/) {
113                         push @flist, 'info', $pre;
114                 } else {
115                         $pre .= '*' unless $pre =~ /[\*\?\[]$/o;
116                         $pre = shellregex($pre);
117                         if ($usesql) {
118                                 $pre =~ s/\.\*/%/g;
119                         } else {
120                                 $pre =~ s/\.\*\$$//;
121                         }
122                         $pre .= '$' if $exact;
123                         $pre =~ s/\^//;
124                         push @flist, 'call', $pre;
125                 }
126         }
127         
128     my $newline = join(' ', @flist);
129         dbg("sh/dx newline: $newline") if isdbg('sh/dx');
130         my ($r, $filter, $fno, $user, $expr) = $Spot::filterdef->parse($self, 'spots', $newline, 1);
131
132         return (0, "sh/dx parse error '$r' " . $filter) if $r;
133
134         $user ||= '';
135         dbg "sh/dx user: $user expr: $expr from: $from to: $to fromday: $fromday today: $today" if isdbg('sh/dx');
136   
137         # now do the search
138
139         if ($self->{_nospawn}) {
140                 my @res = Spot::search($expr, $fromday, $today, $from, $to, $user, $dofilter ? $self : undef);
141                 my $ref;
142                 my @dx;
143                 foreach $ref (@res) {
144                         if ($self && $self->ve7cc) {
145                                 push @out, VE7CC::dx_spot($self, @$ref);
146                         }
147                         else {
148                                 if ($self && $real) {
149                                         push @out, DXCommandmode::format_dx_spot($self, @$ref);
150                                 }
151                                 else {
152                                         push @out, Spot::formatl(@$ref);
153                                 }
154                         }
155                 }
156         }
157         else {
158                 push @out, $self->spawn_cmd("sh/dx $line", \&Spot::search, 
159                                                                         args => [$expr, $fromday, $today, $from, $to, $filter, $dofilter ? $self : undef],
160                                                                         cb => sub {
161                                                                                 my ($dxchan, @res) = @_; 
162                                                                                 my $ref;
163                                                                                 my @out;
164
165                                                                                 foreach $ref (@res) {
166                                                                                         if ($self->ve7cc) {
167                                                                                                 push @out, VE7CC::dx_spot($self, @$ref);
168                                                                                         }
169                                                                                         else {
170                                                                                                 if ($real) {
171                                                                                                         push @out, DXCommandmode::format_dx_spot($self, @$ref);
172                                                                                                 }
173                                                                                                 else {
174                                                                                                         push @out, Spot::formatl(@$ref);
175                                                                                                 }
176                                                                                         }
177                                                                                 }
178                                                                                 push @out, $self->msg('e3', "sh/dx", "'$line'") unless @out;
179                                                                                 return @out;
180                                                                         });
181         }
182
183
184         return (1, @out);
185 }
186
187