4 # Copyright (c) - 1998 Dirk Koopman G1TLH
22 use Time::HiRes qw(gettimeofday tv_interval);
23 use Math::Round qw(nearest nearest_floor);
27 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef
28 $totalspots $hfspots $vhfspots $maxcalllth $can_encode $use_db_for_search);
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
36 $duplth = 15; # the length of text to use in the deduping
37 $dupage = 1*3600; # the length of time to hold spot dups
38 $maxcalllth = 12; # the max length of call to take into account for dupes
40 # tag, sort, field, priv, special parser
41 ['freq', 'r', 0, 0, \&decodefreq],
42 ['on', 'r', 0, 0, \&decodefreq],
48 ['call_dxcc', 'nc', 5],
50 ['origin', 'c', 7, 9],
51 ['call_itu', 'ni', 8],
53 ['call_zone', 'nz', 9],
58 ['by_zone', 'nz', 11],
61 ['call_state', 'ns', 12],
63 ['by_state', 'ns', 13],
64 ['bystate', 'ns', 13],
66 # ['channel', 'c', 15],
67 # ['rbn', 'a', 4, 0, \&filterrbnspot],
69 $totalspots = $hfspots = $vhfspots = 0;
70 $use_db_for_search = 0;
72 our %spotcache; # the cache of data within the last $spotcachedays 0 or 2+ days
73 our $spotcachedays = 2; # default 2 days worth
74 our $minselfspotqrg = 1240000; # minimum freq above which self spotting is allowed
76 our $readback = $main::is_win ? 0 : 1; # don't read spot files backwards if it's windows
77 our $qrggranularity = 25; # normalise the qrg to this number of hz (default: 25khz), so tough luck if you have a fumble fingers moment
78 our $timegranularity = 600; # ditto to the nearest 100 seconds
79 our $oldstyle = 0; # revert to traditional dupe key format
80 our $no_node_in_dupe = 1; # remove the node field from dupe considerations.
83 $readback = `which tac`;
87 # create a Spot Object
92 return bless $self, $class;
99 my @f = split /,/, $l;
105 if ($f =~ m{^\d+/\d+$}) {
107 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
109 my @fr = Bands::get_freq(lc $a, $b);
114 push @out, "$a/$b"; # add them as ranges
117 return ('dfreq', $dxchan->msg('dfreq1', $f));
120 return ('dfreq', $dxchan->msg('e20', $f));
123 return (0, join(',', @out));
126 # filter setup for rbn spot so return the regex to detect it
135 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
136 $fp = DXLog::new($dirprefix, "dat", 'd');
137 $statp = DXLog::new($dirprefix, "dys", 'd');
138 my $today = Julian::Day->new(time);
140 # load up any old spots
142 unless (grep $_ eq 'spot', $main::dbh->show_tables) {
143 dbg('initialising spot tables');
146 $main::dbh->spot_create_table;
148 my $now = Julian::Day->alloc(1995, 0);
149 my $sth = $main::dbh->spot_insert_prepare;
150 while ($now->cmp($today) <= 0) {
151 my $fh = $fp->open($now);
153 # $main::dbh->{RaiseError} = 0;
154 $main::dbh->begin_work;
160 my @a = (Prefix::cty_data($s[1]))[1..3];
161 my @b = (Prefix::cty_data($s[4]))[1..3];
162 push @s, $b[1] if @s < 7;
163 push @s, '' if @s < 8;
164 push @s, @a[0,1], @b[0,1] if @s < 12;
165 push @s, $a[2], $b[2] if @s < 14;
167 $main::dbh->spot_insert(\@s, $sth);
171 dbg("inserted $count spots from $now->[0] $now->[1]");
177 $main::dbh->begin_work;
178 $main::dbh->spot_add_indexes;
180 # $main::dbh->{RaiseError} = 1;
182 my $min = int($t / 60);
184 dbg("$total spots converted in $min:$sec");
186 unless ($main::dbh->has_ipaddr) {
187 $main::dbh->add_ipaddr;
188 dbg("added ipaddr field to spot table");
192 # initialise the cache if required
193 if ($spotcachedays > 0) {
194 my $t0 = [gettimeofday];
195 $spotcachedays = 2 if $spotcachedays < 2;
196 for (my $i = 0; $i < $spotcachedays; ++$i) {
197 my $now = $today->sub($i);
198 my $fh = $fp->open($now);
202 for ($rec = 0; <$fh>; ++$rec) {
206 my @a = (Prefix::cty_data($s[1]))[1..3];
207 my @b = (Prefix::cty_data($s[4]))[1..3];
208 push @s, $b[1] if @s < 7;
209 push @s, '' if @s < 8;
210 push @s, @a[0,1], @b[0,1] if @s < 12;
211 push @s, $a[2], $b[2] if @s < 14;
216 dbg("Spot::init read $rec spots from " . _cachek($now));
217 $spotcache{_cachek($now)} = \@in;
221 dbg("Spot::init $spotcachedays files of spots read into cache in " . _diffms($t0) . "mS")
227 return $fp->{prefix};
230 # fix up the full spot data from the basic spot data
232 # freq, call, time, comment, spotter, origin[, ip_address]
235 # $freq, $call, $t, $comment, $spotter, node, ip address = @_
236 my @out = @_[0..4]; # just up to the spotter
238 # normalise frequency
239 $out[0] = sprintf "%.1f", $out[0];
241 # remove ssids and /xxx if present on spotter
242 $out[4] =~ s/-\d+$//o;
244 # remove leading and trailing spaces from comment field
245 $out[3] = unpad($out[3]);
247 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
248 my @spd = Prefix::cty_data($out[1]);
250 my @spt = Prefix::cty_data($out[4]);
253 push @out, @spd[1,2], @spt[1,2], $spd[3], $spt[3];
254 push @out, $_[6] if $_[6] && is_ipaddr($_[6]);
257 # freq, call, time, comment, spotter, call country code, spotter country code, origin, call itu, call cqzone, spotter itu, spotter cqzone, call state, spotter state, spotter ip address
263 my $buf = join('^', @_);
264 $fp->writeunix($_[2], $buf);
265 if ($spotcachedays > 0) {
266 my $now = Julian::Day->new($_[2]);
267 my $day = _cachek($now);
268 my $r = (exists $spotcache{$day}) ? $spotcache{$day} : ($spotcache{$day} = []);
272 $main::dbh->begin_work;
273 $main::dbh->spot_insert(\@_);
277 if ($_[0] <= 30000) {
282 if ($_[3] =~ /(?:QSL|VIA)/i) {
283 my $q = QSL::get($_[1]) || new QSL $_[1];
284 $q->update($_[3], $_[2], $_[4]);
288 # search the spot database for records based on the field no and an expression
289 # this returns a set of references to the spots
291 # the expression is a legal perl 'if' statement with the possible fields indicated
296 # $f2 = date in unix format
299 # $f5 = spotted dxcc country
300 # $f6 = spotter dxcc country
303 # $f9 = spotted cq zone
305 # $f11 = spotter cq zone
306 # $f12 = spotted us state
307 # $f13 = spotter us state
310 # In addition you can specify a range of days, this means that it will start searching
311 # from <n> days less than today to <m> days less than today
313 # Also you can select a range of entries so normally you would get the 0th (latest) entry
314 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
316 # This routine is designed to be called as Spot::search(..)
321 my ($expr, $dayfrom, $dayto, $from, $to, $hint, $dofilter, $dxchan) = @_;
326 my $today = Julian::Day->new(time());
330 $dayfrom = 0 if !$dayfrom;
331 $dayto = $maxdays unless $dayto;
332 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
333 $fromdate = $today->sub($dayfrom);
334 $todate = $fromdate->sub($dayto);
335 $from = 0 unless $from;
336 $to = $defaultspots unless $to;
337 $hint = $hint ? "next unless $hint" : "";
338 $expr = "1" unless $expr;
340 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
342 if ($main::dbh && $use_db_for_search) {
343 return $main::dbh->spot_search($expr, $dayfrom, $dayto, $from, $to, $hint, $dofilter, $dxchan);
346 # $expr =~ s/\$f(\d\d?)/\$ref->[$1]/g; # swap the letter n for the correct field name
347 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
350 dbg("Spot::search hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
352 # build up eval to execute
354 dbg("Spot::search Spot eval: $expr") if isdbg('searcheval');
355 $expr =~ s/\$r/\$_[0]/g;
356 my $eval = qq{ sub { return $expr; } };
357 dbg("Spot::search Spot eval: $eval") if isdbg('searcheval');
358 my $ecode = eval $eval;
359 return ("Spot search error", $@) if $@;
363 my $today = Julian::Day->new($main::systime);
365 for ($i = $count = 0; $count < $to && $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
366 last if $now->cmp($todate) <= 0;
369 my $this = $now->sub($i);
370 my $fn = $fp->fn($this);
371 my $cachekey = _cachek($this);
374 if ($spotcachedays > 0 && $spotcache{$cachekey}) {
375 foreach my $r (@{$spotcache{$cachekey}}) {
377 if ($dofilter && $dxchan && $dxchan->{spotsfilter}) {
378 my ($gotone, undef) = $dxchan->{spotsfilter}->it(@$r);
383 next if $count < $from;
385 last if $count >= $to;
388 dbg("Spot::search cache recs read: $rec") if isdbg('search');
391 dbg("Spot::search search using tac fn: $fn $i") if isdbg('search');
392 $fh = IO::File->new("$readback $fn |");
395 dbg("Spot::search search fn: $fp->{fn} $i") if isdbg('search');
396 $fh = $fp->open($now->sub($i)); # get the next file
404 if ($dofilter && $dxchan && $dxchan->{spotsfilter}) {
405 my ($gotone, undef) = $dxchan->{spotsfilter}->it(@r);
410 next if $count < $from;
413 last if $count >= $to;
416 shift @out if $count >= $to;
420 dbg("Spot::search file recs read: $rec") if isdbg('search');
421 last if $count >= $to; # stop after to
425 return ("Spot search error", $@) if $@;
427 @out = sort {$b->[2] <=> $a->[2]} @out if @out;
431 # change a freq range->regular expression
435 return undef unless $a < $b;
438 my @a = split //, $a;
439 my @b = split //, $b;
447 if (@b < (length $d)) {
449 } elsif ($aa eq $bb) {
451 } elsif ($aa < $bb) {
454 $out .= "[0-$bb$aa-9]";
460 # format a spot for user output in list mode
463 my $t = ztime($_[3]);
464 my $d = cldate($_[3]);
465 my $spotter = "<$_[5]>";
466 my $comment = $_[4] || '';
467 $comment =~ s/\t+/ /g;
468 my $cl = length $comment;
469 my $s = sprintf "%9.1f %-11s %s %s", $_[1], $_[2], $d, $t;
470 my $width = ($_[0] ? $_[0] : 80) - length($spotter) - length($s) - 4;
472 $comment = substr $comment, 0, $width if $cl > $width;
473 $comment .= ' ' x ($width-$cl) if $cl < $width;
475 # return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, ($_[3]||''), "<$_[4]" ;
476 return "$s $comment$spotter";
479 # enter the spot for dup checking and return true if it is already a dup
482 my ($freq, $call, $d, $text, $by, $node, $just_find) = @_;
484 dbg("Spot::dup: freq=$freq call=$call d=$d text='$text' by=$by node=$node" . ($just_find ? " jf=$just_find" : "")) if isdbg('spotdup');
487 return 2 if $d < $main::systime - $dupage;
489 # turn the time into minutes (should be already but...)
493 my $nd = nearest($timegranularity, $d);
495 # remove SSID or area
498 # $freq = sprintf "%.1f", $freq; # normalise frequency
499 $freq = int $freq; # normalise frequency
501 my $qrg = nearest($qrggranularity, $freq); # to the nearest however many hz
503 $call = substr($call, 0, $maxcalllth) if length $call > $maxcalllth;
507 my $l = length $text;
508 $dtext = qq{original:'$text'($l)} if isdbg('spottext');
512 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
513 $text = uc unpad($text);
516 $dtext .= qq{->afterhex: '$text'($l)} if isdbg('spottext');
518 if (isdbg('spottext')) {
519 (@dubious) = $text =~ /([?\x00-\x08\x0a-\x1F\x7B-\xFF]+)+/;
520 $dtext .= sprintf q{DUBIOUS '%s'}, join '', @dubious if @dubious;
524 # $text = Encode::encode("iso-8859-1", $text) if $main::can_encode && Encode::is_utf8($text, 1);
525 $text =~ s/^\+\w+\s*//; # remove leading LoTW callsign
526 $text =~ s/\s{2,}[\dA-Z]?[A-Z]\d?$//g if length $text > 24;
528 $text =~ s/[\W\x00-\x2F\x7B-\xFF]//g; # tautology, just to make quite sure!
529 $text = substr($text, 0, $duplth) if length $text > $duplth;
532 $dtext .= qq{->final:'$text'($l)} if isdbg('spottext');
537 # new feature: don't include the origin node in Spot dupes
539 $node = '' if $no_node_in_dupe;
540 $ldupkey = $oldstyle ? "X|$call|$by|$freq|$node|$d|$text" : "X|$call|$by|$qrg|$node|$nd|$text";
542 $t = DXDupe::find($ldupkey);
543 dbg("Spot::dup ldupkey $ldupkey t '$t'" . ($t?' DUPE':' NEW')) if isdbg('spotdup');
544 $dtext .= ' DUPE' if $t;
545 dbg("text transforms: $dtext") if length $text && isdbg('spottext');
548 DXDupe::add($ldupkey, $main::systime+$dupage) unless $just_find;
550 $otext = substr($otext, 0, $duplth) if length $otext > $duplth;
552 if (length $otext && $otext ne $text) {
553 $ldupkey = $oldstyle ? "X|$call|$by|$freq|$otext" : "X|$call|$by|$qrg|$otext";
554 $t = DXDupe::find($ldupkey);
555 dbg("Spot::dup (OTEXT) ldupkey $ldupkey t '$t'" . ($t?' DUPE':' NEW')) if isdbg('spotdup');
556 if (isdbg('spottext')) {
557 $dtext .= sprintf q{ DUBIOUS '%s'}, join '', @dubious if @dubious;
558 $dtext .= ' DUPE (OTEXT)' if $t;
559 dbg("text transforms: $dtext") if length $text;
562 DXDupe::add($ldupkey, $main::systime+$dupage) unless $just_find;
574 return DXDupe::listdups('X', $dupage, @_);
580 my $in = $fp->open($date) or dbg("Spot::genstats: Cannot open " . $fp->fn($date) . " $!");
581 my $out = $statp->open($date, 'w') or dbg("Spot::genstats: Cannot open " . $statp->fn($date) . " $!");
588 @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);
591 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
592 my $ref = $list{$by} || [0, $dxcc];
594 next unless defined $_;
595 if ($freq >= $_->[1] && $freq <= $_->[2]) {
606 for ($i = 0; $i < @freq+2; $i++) {
609 $statp->write($date, join('^', 'TOTALS', @tot));
611 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
614 for ($i = 0; $i < @freq+2; ++$i) {
617 $statp->write($date, join('^', $call, @$ref));
623 # return true if the stat file is newer than than the spot file
627 my $in = $fp->mtime($date);
628 my $out = $statp->mtime($date);
629 return defined $out && defined $in && $out >= $in;
635 my $date = Julian::Day->new($main::systime)->sub(1);
636 genstats($date) unless checkstats($date);
642 return "$_[0]->[0]|$_[0]->[1]";
647 if ($spotcachedays > 0) {
648 my $now = Julian::Day->new($main::systime);
649 for (my $i = $spotcachedays; $i < $spotcachedays + 5; ++$i ) {
650 my $k = _cachek($now->sub($i));
651 if (exists $spotcache{$k}) {
652 dbg("Spot::spotcache deleting day $k, more than $spotcachedays days old");
653 delete $spotcache{$k};