655630ce798b24baee204c2899230095b2db3b90
[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 ($f = shift @list) {      # next field
46                 dbg "sh/dx arg: $f list: " . join(',', @list) if isdbg('sh/dx');
47                 if (!$from && !$to) {
48                         ($from, $to) = $f =~ m|^(\d+)[-/](\d+)$|; # is it a from -> to count?
49                         dbg("sh/dx from: $from to: $to") if isdbg('sh/dx');
50                         next if $from && $to > $from;
51                 }
52                 if (!$to) {
53                         ($to) = $f =~ /^(\d+)$/o if !$to; # is it a to count?
54                         dbg("sh/dx to: $to") if isdbg('sh/dx');
55                         next if $to;
56                 }
57                 if (lc $f eq 'day' && $list[0]) {
58                         ($fromday, $today) = split m|[-/]|, shift(@list);
59                         dbg "sh/dx got day $fromday/$today" if isdbg('sh/dx');
60                         next;
61                 }
62                 if (lc $f eq 'exact') {
63                         dbg("sh/dx exact") if isdbg('sh/dx');
64                         $exact = 1;
65                         next;
66                 }
67                 if (lc $f eq 'rt' || $f =~ /^real/i) {
68                         dbg("sh/dx real") if isdbg('sh/dx');
69                         $real = 1;
70                         next;
71                 }
72                 if (lc $f =~ /^filt/) {
73                         dbg("sh/dx run spotfilter") if isdbg('sh/dx');
74                         $dofilter = 1 if $self && $self->spotsfilter;
75                         next;
76                 }
77                 if (lc $f eq 'qsl') {
78                         dbg("sh/dx qsl") if isdbg('sh/dx');
79                         push @flist, "info {QSL|VIA}";
80                         next;
81                 }
82                 if (lc $f eq 'iota') {
83                         my $doiota;
84                         if (@list && $list[0] && (($a, $b) = $list[0] =~ /(AF|AN|NA|SA|EU|AS|OC)[-\s]?(\d\d?\d?)/i)) {
85                                 $a = uc $a;
86                                 $doiota = "\\b$a\[\-\ \]\?$b\\b";
87                                 shift @list;
88                         }
89                         $doiota = '\b(IOTA|(AF|AN|NA|SA|EU|AS|OC)[-\s]?\d?\d\d)\b' unless $doiota;
90                         push @flist, 'info', "{$doiota}";
91                         dbg("sh/dx iota info {$doiota}") if isdbg('sh/dx');
92                         next;
93                 }
94                 if (lc $f eq 'qra') {
95                         my $doqra = uc shift @list if @list && $list[0] =~ /[A-Z][A-Z]\d\d/i;
96                         $doqra = '\b([A-Z][A-Z]\d\d|[A-Z][A-Z]\d\d[A-Z][A-Z])\b' unless $doqra;
97                         push @flist, 'info',  "{$doqra}";
98                         dbg("sh/dx qra info {$doqra}") if isdbg('sh/dx');
99                         next;
100                 }
101                 if (grep {lc $f eq $_} qw { ( or and not ) }) {
102                         push @flist, $f;
103                         dbg("sh/dx operator $f") if isdbg('sh/dx');
104                         next;
105                 }
106                 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) ) {
107                         push @flist, $f;
108                         push @flist, shift @list if @list;
109                         dbg("sh/dx function $flist[-2] $flist[-1]") if isdbg('sh/dx');
110                         next;
111                 }
112                 unless ($pre) {
113                         $pre = $f;
114                         next;
115                 }
116                 push @flist, $f;
117         }
118
119         
120         if ($pre) {
121                 # someone (probably me) has forgotten the 'info' keyword
122                 if ($pre =~ /^{.*}$/) {
123                         push @flist, 'info', $pre;
124                 } else {
125                         $pre .= '*' unless $pre =~ /[\*\?\[]$/o;
126                         $pre = shellregex($pre);
127                         if ($usesql) {
128                                 $pre =~ s/\.\*/%/g;
129                         } else {
130                                 $pre =~ s/\.\*\$$//;
131                         }
132                         $pre .= '$' if $exact;
133                         $pre =~ s/\^//;
134                         push @flist, 'call', $pre;
135                 }
136         }
137         
138     my $newline = join(' ', @flist);
139         dbg("sh/dx newline: $newline") if isdbg('sh/dx');
140         my ($r, $filter, $fno, $user, $expr) = $Spot::filterdef->parse($self, 'spots', $newline, 1);
141
142         return (0, "sh/dx parse error '$r' " . $filter) if $r;
143
144         $user ||= '';
145         dbg "sh/dx user: $user expr: $expr from: $from to: $to fromday: $fromday today: $today" if isdbg('sh/dx');
146   
147         # now do the search
148
149         if ($self->{_nospawn}) {
150                 my @res = Spot::search($expr, $fromday, $today, $from, $to, $user, $dofilter ? $self : undef);
151                 my $ref;
152                 my @dx;
153                 foreach $ref (@res) {
154                         if ($self && $self->ve7cc) {
155                                 push @out, VE7CC::dx_spot($self, @$ref);
156                         }
157                         else {
158                                 if ($self && $real) {
159                                         push @out, DXCommandmode::format_dx_spot($self, @$ref);
160                                 }
161                                 else {
162                                         push @out, Spot::formatl(@$ref);
163                                 }
164                         }
165                 }
166         }
167         else {
168                 push @out, $self->spawn_cmd("sh/dx $line", \&Spot::search, 
169                                                                         args => [$expr, $fromday, $today, $from, $to, $filter, $dofilter ? $self : undef],
170                                                                         cb => sub {
171                                                                                 my ($dxchan, @res) = @_; 
172                                                                                 my $ref;
173                                                                                 my @out;
174
175                                                                                 foreach $ref (@res) {
176                                                                                         if ($self->ve7cc) {
177                                                                                                 push @out, VE7CC::dx_spot($self, @$ref);
178                                                                                         }
179                                                                                         else {
180                                                                                                 if ($real) {
181                                                                                                         push @out, DXCommandmode::format_dx_spot($self, @$ref);
182                                                                                                 }
183                                                                                                 else {
184                                                                                                         push @out, Spot::formatl(@$ref);
185                                                                                                 }
186                                                                                         }
187                                                                                 }
188                                                                                 push @out, $self->msg('e3', "sh/dx", "'$line'") unless @out;
189                                                                                 return @out;
190                                                                         });
191         }
192
193
194         return (1, @out);
195 }
196
197