1. protect against PC41s with field[3] == field[2]
[spider.git] / perl / Spot.pm
1 #
2 # the dx spot handler
3 #
4 # Copyright (c) - 1998 Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 package Spot;
10
11 use IO::File;
12 use DXVars;
13 use DXDebug;
14 use DXUtil;
15 use DXLog;
16 use Julian;
17 use Prefix;
18 use DXDupe;
19 use Data::Dumper;
20
21 use strict;
22 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef);
23
24 $fp = undef;
25 $statp = undef;
26 $maxspots = 50;                                 # maximum spots to return
27 $defaultspots = 10;                             # normal number of spots to return
28 $maxdays = 100;                         # normal maximum no of days to go back
29 $dirprefix = "spots";
30 $duplth = 20;                                   # the length of text to use in the deduping
31 $dupage = 3*3600;               # the length of time to hold spot dups
32 $filterdef = bless ([
33                           # tag, sort, field, priv, special parser 
34                           ['freq', 'r', 0, 0, \&decodefreq],
35                           ['on', 'r', 0, 0, \&decodefreq],
36                           ['call', 'c', 1],
37                           ['info', 't', 3],
38                           ['by', 'c', 4],
39                           ['call_dxcc', 'n', 5],
40                           ['by_dxcc', 'n', 6],
41                           ['origin', 'c', 7, 9],
42                           ['call_itu', 'n', 8],
43                           ['call_zone', 'n', 9],
44                           ['by_itu', 'n', 10],
45                           ['by_zone', 'n', 11],
46                           ['channel', 'n', 12, 9],
47                          ], 'Filter::Cmd');
48
49
50 # create a Spot Object
51 sub new
52 {
53         my $class = shift;
54         my $self = [ @_ ];
55         return bless $self, $class;
56 }
57
58 sub decodefreq
59 {
60         my $dxchan = shift;
61         my $l = shift;
62         my @f = split /,/, $l;
63         my @out;
64         my $f;
65         
66         foreach $f (@f) {
67                 my ($a, $b); 
68                 if (m{^\d+/\d+$}) {
69                         push @out, $f;
70                 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
71                         $b = lc $b if $b;
72                         my @fr = Bands::get_freq(lc $a, $b);
73                         if (@fr) {
74                                 while (@fr) {
75                                         $a = shift @fr;
76                                         $b = shift @fr;
77                                         push @out, "$a/$b";  # add them as ranges
78                                 }
79                         } else {
80                                 return ('dfreq', $dxchan->msg('dfreq1', $f));
81                         }
82                 } else {
83                         return ('dfreq', $dxchan->msg('e20', $f));
84                 }
85         }
86         return (0, join(',', @out));                     
87 }
88
89 sub init
90 {
91         mkdir "$dirprefix", 0777 if !-e "$dirprefix";
92         $fp = DXLog::new($dirprefix, "dat", 'd');
93         $statp = DXLog::new($dirprefix, "bys", 'd');
94 }
95
96 sub prefix
97 {
98         return $fp->{prefix};
99 }
100
101 # fix up the full spot data from the basic spot data
102 sub prepare
103 {
104         # $freq, $call, $t, $comment, $spotter = @_
105         my @out = @_[0..4];      # just up to the spotter
106
107         # normalise frequency
108         $_[0] = sprintf "%.1f", $_[0];
109   
110         # remove ssids if present on spotter
111         $out[4] =~ s/-\d+$//o;
112
113         # remove leading and trailing spaces
114         $_[3] = unpad($_[3]);
115         
116         # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
117         my @dxcc = Prefix::extract($out[1]);
118         my $spotted_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
119         my $spotted_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
120         my $spotted_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
121         push @out, $spotted_dxcc;
122         @dxcc = Prefix::extract($out[4]);
123         my $spotter_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
124         my $spotter_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
125         my $spotter_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
126         push @out, $spotter_dxcc;
127         push @out, $_[5];
128         return (@out, $spotted_itu, $spotted_cq, $spotter_itu, $spotter_cq);
129 }
130
131 sub add
132 {
133         my $buf = join("\^", @_[0..7]);
134         $fp->writeunix($_[2], $buf);
135 }
136
137 # search the spot database for records based on the field no and an expression
138 # this returns a set of references to the spots
139 #
140 # the expression is a legal perl 'if' statement with the possible fields indicated
141 # by $f<n> where :-
142 #
143 #   $f0 = frequency
144 #   $f1 = call
145 #   $f2 = date in unix format
146 #   $f3 = comment
147 #   $f4 = spotter
148 #   $f5 = spotted dxcc country
149 #   $f6 = spotter dxcc country
150 #   $f7 = origin
151 #
152 #
153 # In addition you can specify a range of days, this means that it will start searching
154 # from <n> days less than today to <m> days less than today
155 #
156 # Also you can select a range of entries so normally you would get the 0th (latest) entry
157 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
158 #
159 # This routine is designed to be called as Spot::search(..)
160 #
161
162 sub search
163 {
164         my ($expr, $dayfrom, $dayto, $from, $to, $hint) = @_;
165         my $eval;
166         my @out;
167         my $ref;
168         my $i;
169         my $count;
170         my $today = Julian::Day->new(time());
171         my $fromdate;
172         my $todate;
173
174         $dayfrom = 0 if !$dayfrom;
175         $dayto = $maxdays unless $dayto;
176         $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
177         $fromdate = $today->sub($dayfrom);
178         $todate = $fromdate->sub($dayto);
179         $from = 0 unless $from;
180         $to = $defaultspots unless $to;
181         $hint = $hint ? "next unless $hint" : "";
182         $expr = "1" unless $expr;
183         
184         $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
185
186         $expr =~ s/\$f(\d)/\$ref->[$1]/g; # swap the letter n for the correct field name
187         #  $expr =~ s/\$f(\d)/\$spots[$1]/g;               # swap the letter n for the correct field name
188   
189         dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
190   
191         # build up eval to execute
192         $eval = qq(
193                            while (<\$fh>) {
194                                    $hint;
195                                    chomp;
196                                    push \@spots, [ split '\\^' ];
197                            }
198                            my \$c;
199                            my \$ref;
200                            for (\$c = \$#spots; \$c >= 0; \$c--) {
201                                         \$ref = \$spots[\$c];
202                                         if ($expr) {
203                                                 \$count++;
204                                                 next if \$count < \$from; # wait until from 
205                                                 push(\@out, \$ref);
206                                                 last if \$count >= \$to; # stop after to
207                                         }
208                                 }
209                           );
210
211         $fp->close;                                     # close any open files
212
213         for ($i = $count = 0; $i < $maxdays; ++$i) {    # look thru $maxdays worth of files only
214                 my $now = $fromdate->sub($i); # but you can pick which $maxdays worth
215                 last if $now->cmp($todate) <= 0;         
216         
217                 my @spots = ();
218                 my $fh = $fp->open($now); # get the next file
219                 if ($fh) {
220                         my $in;
221                         eval $eval;                     # do the search on this file
222                         last if $count >= $to; # stop after to
223                         return ("Spot search error", $@) if $@;
224                 }
225         }
226
227         return @out;
228 }
229
230 # change a freq range->regular expression
231 sub ftor
232 {
233         my ($a, $b) = @_;
234         return undef unless $a < $b;
235         $b--;
236         my $d = $b - $a;
237         my @a = split //, $a;
238         my @b = split //, $b;
239         my $out;
240         while (@b > @a) {
241                 $out .= shift @b;
242         }
243         while (@b) {
244                 my $aa = shift @a;
245                 my $bb = shift @b;
246                 if (@b < (length $d) - 1) {
247                         $out .= '\\d';
248                 } elsif ($aa eq $bb) {
249                         $out .= $aa;
250                 } elsif ($aa < $bb) {
251                         $out .= "[$aa-$bb]";
252                 } else {
253                         $out .= "[0-$bb$aa-9]";
254                 }
255         }
256         return $out;
257 }
258
259 # format a spot for user output in 'broadcast' mode
260 sub formatb
261 {
262         my $wantgrid = shift;
263         my $t = ztime($_[2]);
264         my $ref = DXUser->get_current($_[4]);
265         my $loc = $ref->qra if $ref && $ref->qra && $wantgrid;
266         $loc = ' ' . substr($ref->qra, 0, 4) if $loc;
267         $loc = "" unless $loc;
268         return sprintf "DX de %-7.7s%11.1f  %-12.12s %-30s %s$loc", "$_[4]:", $_[0], $_[1], $_[3], $t ;
269 }
270
271 # format a spot for user output in list mode
272 sub formatl
273 {
274         my $t = ztime($_[2]);
275         my $d = cldate($_[2]);
276         return sprintf "%8.1f  %-11s %s %s  %-28.28s%7s>", $_[0], $_[1], $d, $t, $_[3], "<$_[4]" ;
277 }
278
279 #
280 # return all the spots from a day's file as an array of references
281 # the parameter passed is a julian day
282 sub readfile($)
283 {
284         my @spots;
285         
286         my $fh = $fp->open(shift); 
287         if ($fh) {
288                 my $in;
289                 while (<$fh>) {
290                         chomp;
291                         push @spots, [ split '\^' ];
292                 }
293         }
294         return @spots;
295 }
296
297 # enter the spot for dup checking and return true if it is already a dup
298 sub dup
299 {
300         my ($freq, $call, $d, $text) = @_; 
301
302         # dump if too old
303         return 2 if $d < $main::systime - $dupage;
304  
305         $freq = sprintf "%.1f", $freq;       # normalise frequency
306         chomp $text;
307         $text = substr($text, 0, $duplth) if length $text > $duplth; 
308         unpad($text);
309         $text =~ s/[\\\%]\d+//g;
310         $text =~ s/[^a-zA-Z0-9]//g;
311         my $dupkey = "X$freq|$call|$d|\L$text";
312         return DXDupe::check($dupkey, $main::systime+$dupage);
313 }
314
315 sub listdups
316 {
317         return DXDupe::listdups('X', $dupage, @_);
318 }
319
320 sub genstats($)
321 {
322         my $date = shift;
323         my $in = $fp->open($date);
324         my $out = $statp->open($date, 'w');
325         my @freq = (
326                                 [0, Bands::get_freq('160m')],
327                                 [1, Bands::get_freq('80m')],
328                                 [2, Bands::get_freq('40m')],
329                                 [3, Bands::get_freq('30m')],
330                                 [4, Bands::get_freq('20m')],
331                                 [5, Bands::get_freq('17m')],
332                                 [6, Bands::get_freq('15m')],
333                                 [7, Bands::get_freq('12m')],
334                                 [8, Bands::get_freq('10m')],
335                                 [9, Bands::get_freq('6m')],
336                                 [10, Bands::get_freq('4m')],
337                                 [11, Bands::get_freq('2m')],
338                                 [12, Bands::get_freq('70cm')],
339                                 [13, Bands::get_freq('13cm')],
340                                 [14, Bands::get_freq('9cm')],
341                                 [15, Bands::get_freq('6cm')],
342                                 [16, Bands::get_freq('3cm')],
343                                 [17, Bands::get_freq('12mm')],
344                                 [18, Bands::get_freq('6cm')],
345                            );
346         my %list;
347         my @tot;
348         
349         if ($in && $out) {
350                 while (<$in>) {
351                         chomp;
352                         my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
353                         my $ref = $list{$by} || [0, $dxcc];
354                         for (@freq) {
355                                 if ($freq >= $_->[1] && $freq <= $_->[2]) {
356                                         $$ref[$_->[0]+2]++;
357                                         $tot[$_->[0]+2]++;
358                                         $$ref[0]++;
359                                         $tot[0]++;
360                                         $list{$by} = $ref;
361                                         last;
362                                 }
363                         }
364                 }
365
366                 my $i;
367                 for ($i = 0; $i < @freq+2; $i++) {
368                         $tot[$i] ||= 0;
369                 }
370                 $out->write(join('^', 'TOTALS', @tot) . "\n");
371
372                 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
373                         my $ref = $list{$_};
374                         my $call = $_;
375                         for ($i = 0; $i < @freq+2; ++$i) {
376                                 $ref->[$i] ||= 0;
377                         }
378                         $out->write(join('^', $call, @$ref) . "\n");
379                 }
380                 $out->close;
381         }
382 }
383
384 # return true if the stat file is newer than than the spot file
385 sub checkstats($)
386 {
387         my $date = shift;
388         my $in = $fp->mtime($date);
389         my $out = $statp->mtime($date);
390         return defined $out && defined $in && $out >= $in;
391 }
392
393 # daily processing
394 sub daily
395 {
396         my $date = Julian::Day->new($main::systime)->sub(1);
397         genstats($date) unless checkstats($date);
398 }
399 1;
400
401
402
403