c9d85c4bef562da88c0de932a8dc5136b56cf712
[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                         $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                 # someone (probably me) has forgotten the 'info' keyword
113                 if ($pre =~ /^{.*}$/) {
114                         push @flist, 'info', $pre;
115                 } else {
116                         $pre .= '*' unless $pre =~ /[\*\?\[]$/o;
117                         $pre = shellregex($pre);
118                         if ($usesql) {
119                                 $pre =~ s/\.\*/%/g;
120                         } else {
121                                 $pre =~ s/\.\*\$$//;
122                         }
123                         $pre .= '$' if $exact;
124                         $pre =~ s/\^//;
125                         push @flist, 'call', $pre;
126                 }
127         }
128         
129     my $newline = join(' ', @flist);
130         dbg("sh/dx newline: $newline") if isdbg('sh/dx');
131         my ($r, $filter, $fno, $user, $expr) = $Spot::filterdef->parse($self, 'spots', $newline, 1);
132
133         return (0, "sh/dx parse error '$r' " . $filter) if $r;
134
135         $user ||= '';
136         dbg "sh/dx user: $user expr: $expr from: $from to: $to fromday: $fromday today: $today" if isdbg('sh/dx');
137   
138         # now do the search
139
140         if ($self->{_nospawn}) {
141                 my @res = Spot::search($expr, $fromday, $today, $from, $to, $user, $dofilter ? $self : undef);
142                 my $ref;
143                 my @dx;
144                 foreach $ref (@res) {
145                         if ($self && $self->ve7cc) {
146                                 push @out, VE7CC::dx_spot($self, @$ref);
147                         }
148                         else {
149                                 if ($self && $real) {
150                                         push @out, DXCommandmode::format_dx_spot($self, @$ref);
151                                 }
152                                 else {
153                                         push @out, Spot::formatl(@$ref);
154                                 }
155                         }
156                 }
157         }
158         else {
159                 push @out, $self->spawn_cmd("sh/dx $line", \&Spot::search, 
160                                                                         args => [$expr, $fromday, $today, $from, $to, $filter, $dofilter ? $self : undef],
161                                                                         cb => sub {
162                                                                                 my ($dxchan, @res) = @_; 
163                                                                                 my $ref;
164                                                                                 my @out;
165
166                                                                                 foreach $ref (@res) {
167                                                                                         if ($self->ve7cc) {
168                                                                                                 push @out, VE7CC::dx_spot($self, @$ref);
169                                                                                         }
170                                                                                         else {
171                                                                                                 if ($real) {
172                                                                                                         push @out, DXCommandmode::format_dx_spot($self, @$ref);
173                                                                                                 }
174                                                                                                 else {
175                                                                                                         push @out, Spot::formatl(@$ref);
176                                                                                                 }
177                                                                                         }
178                                                                                 }
179                                                                                 push @out, $self->msg('e3', "sh/dx", "'$line'") unless @out;
180                                                                                 return @out;
181                                                                         });
182         }
183
184
185         return (1, @out);
186 }
187
188