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