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