4 # Copyright (c) - 1998 Dirk Koopman G1TLH
22 use Time::HiRes qw(gettimeofday tv_interval);
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 = 20; # 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
79 $readback = `which tac`;
83 # create a Spot Object
88 return bless $self, $class;
95 my @f = split /,/, $l;
101 if ($f =~ m{^\d+/\d+$}) {
103 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
105 my @fr = Bands::get_freq(lc $a, $b);
110 push @out, "$a/$b"; # add them as ranges
113 return ('dfreq', $dxchan->msg('dfreq1', $f));
116 return ('dfreq', $dxchan->msg('e20', $f));
119 return (0, join(',', @out));
122 # filter setup for rbn spot so return the regex to detect it
131 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
132 $fp = DXLog::new($dirprefix, "dat", 'd');
133 $statp = DXLog::new($dirprefix, "dys", 'd');
134 my $today = Julian::Day->new(time);
136 # load up any old spots
138 unless (grep $_ eq 'spot', $main::dbh->show_tables) {
139 dbg('initialising spot tables');
142 $main::dbh->spot_create_table;
144 my $now = Julian::Day->alloc(1995, 0);
145 my $sth = $main::dbh->spot_insert_prepare;
146 while ($now->cmp($today) <= 0) {
147 my $fh = $fp->open($now);
149 # $main::dbh->{RaiseError} = 0;
150 $main::dbh->begin_work;
156 my @a = (Prefix::cty_data($s[1]))[1..3];
157 my @b = (Prefix::cty_data($s[4]))[1..3];
158 push @s, $b[1] if @s < 7;
159 push @s, '' if @s < 8;
160 push @s, @a[0,1], @b[0,1] if @s < 12;
161 push @s, $a[2], $b[2] if @s < 14;
163 $main::dbh->spot_insert(\@s, $sth);
167 dbg("inserted $count spots from $now->[0] $now->[1]");
173 $main::dbh->begin_work;
174 $main::dbh->spot_add_indexes;
176 # $main::dbh->{RaiseError} = 1;
178 my $min = int($t / 60);
180 dbg("$total spots converted in $min:$sec");
182 unless ($main::dbh->has_ipaddr) {
183 $main::dbh->add_ipaddr;
184 dbg("added ipaddr field to spot table");
188 # initialise the cache if required
189 if ($spotcachedays > 0) {
190 my $t0 = [gettimeofday];
191 $spotcachedays = 2 if $spotcachedays < 2;
192 dbg "Spot::init - reading in $spotcachedays days of spots into cache";
193 for (my $i = 0; $i < $spotcachedays; ++$i) {
194 my $now = $today->sub($i);
195 my $fh = $fp->open($now);
199 for ($rec = 0; <$fh>; ++$rec) {
203 my @a = (Prefix::cty_data($s[1]))[1..3];
204 my @b = (Prefix::cty_data($s[4]))[1..3];
205 push @s, $b[1] if @s < 7;
206 push @s, '' if @s < 8;
207 push @s, @a[0,1], @b[0,1] if @s < 12;
208 push @s, $a[2], $b[2] if @s < 14;
213 dbg("Spot::init read $rec spots from " . _cachek($now));
214 $spotcache{_cachek($now)} = \@in;
218 dbg("Spot::init $spotcachedays files of spots read into cache in " . _diffms($t0) . "mS")
224 return $fp->{prefix};
227 # fix up the full spot data from the basic spot data
229 # freq, call, time, comment, spotter, origin[, ip_address]
232 # $freq, $call, $t, $comment, $spotter, node, ip address = @_
233 my @out = @_[0..4]; # just up to the spotter
235 # normalise frequency
236 $out[0] = sprintf "%.1f", $out[0];
238 # remove ssids and /xxx if present on spotter
239 $out[4] =~ s/-\d+$//o;
241 # remove leading and trailing spaces
242 $out[3] = unpad($out[3]);
245 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
246 my @spd = Prefix::cty_data($out[1]);
248 my @spt = Prefix::cty_data($out[4]);
251 push @out, @spd[1,2], @spt[1,2], $spd[3], $spt[3];
252 push @out, $_[6] if $_[6] && is_ipaddr($_[6]);
255 # 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
261 my $buf = join('^', @_);
262 $fp->writeunix($_[2], $buf);
263 if ($spotcachedays > 0) {
264 my $now = Julian::Day->new($_[2]);
265 my $day = _cachek($now);
266 my $r = (exists $spotcache{$day}) ? $spotcache{$day} : ($spotcache{$day} = []);
270 $main::dbh->begin_work;
271 $main::dbh->spot_insert(\@_);
275 if ($_[0] <= 30000) {
280 if ($_[3] =~ /(?:QSL|VIA)/i) {
281 my $q = QSL::get($_[1]) || new QSL $_[1];
282 $q->update($_[3], $_[2], $_[4]);
286 # search the spot database for records based on the field no and an expression
287 # this returns a set of references to the spots
289 # the expression is a legal perl 'if' statement with the possible fields indicated
294 # $f2 = date in unix format
297 # $f5 = spotted dxcc country
298 # $f6 = spotter dxcc country
301 # $f9 = spotted cq zone
303 # $f11 = spotter cq zone
304 # $f12 = spotted us state
305 # $f13 = spotter us state
308 # In addition you can specify a range of days, this means that it will start searching
309 # from <n> days less than today to <m> days less than today
311 # Also you can select a range of entries so normally you would get the 0th (latest) entry
312 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
314 # This routine is designed to be called as Spot::search(..)
319 my ($expr, $dayfrom, $dayto, $from, $to, $hint, $dofilter, $dxchan) = @_;
324 my $today = Julian::Day->new(time());
328 $dayfrom = 0 if !$dayfrom;
329 $dayto = $maxdays unless $dayto;
330 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
331 $fromdate = $today->sub($dayfrom);
332 $todate = $fromdate->sub($dayto);
333 $from = 0 unless $from;
334 $to = $defaultspots unless $to;
335 $hint = $hint ? "next unless $hint" : "";
336 $expr = "1" unless $expr;
338 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
340 if ($main::dbh && $use_db_for_search) {
341 return $main::dbh->spot_search($expr, $dayfrom, $dayto, $from, $to, $hint, $dofilter, $dxchan);
344 # $expr =~ s/\$f(\d\d?)/\$ref->[$1]/g; # swap the letter n for the correct field name
345 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
348 dbg("Spot::search hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
350 # build up eval to execute
351 dbg("Spot::search Spot eval: $expr") if isdbg('searcheval');
352 $expr =~ s/\$r/\$_[0]/g;
353 my $eval = qq{ sub { return $expr; } };
354 dbg("Spot::search Spot eval: $eval") if isdbg('searcheval');
355 my $ecode = eval $eval;
356 return ("Spot search error", $@) if $@;
361 my $today = Julian::Day->new($main::systime);
363 for ($i = $count = 0; $count < $to && $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
364 last if $now->cmp($todate) <= 0;
367 my $this = $now->sub($i);
368 my $fn = $fp->fn($this);
369 my $cachekey = _cachek($this);
372 if ($spotcachedays > 0 && $spotcache{$cachekey}) {
373 foreach my $r (@{$spotcache{$cachekey}}) {
375 if ($dofilter && $dxchan && $dxchan->{spotsfilter}) {
376 my ($gotone, undef) = $dxchan->{spotsfilter}->it(@$r);
381 next if $count < $from;
383 last if $count >= $to;
386 dbg("Spot::search cache recs read: $rec") if isdbg('search');
389 dbg("Spot::search search using tac fn: $fn $i") if isdbg('search');
390 $fh = IO::File->new("$readback $fn |");
393 dbg("Spot::search search fn: $fp->{fn} $i") if isdbg('search');
394 $fh = $fp->open($now->sub($i)); # get the next file
402 if ($dofilter && $dxchan && $dxchan->{spotsfilter}) {
403 my ($gotone, undef) = $dxchan->{spotsfilter}->it(@r);
408 next if $count < $from;
411 last if $count >= $to;
414 shift @out if $count >= $to;
418 dbg("Spot::search file recs read: $rec") if isdbg('search');
419 last if $count >= $to; # stop after to
423 return ("Spot search error", $@) if $@;
425 @out = sort {$b->[2] <=> $a->[2]} @out if @out;
429 # change a freq range->regular expression
433 return undef unless $a < $b;
436 my @a = split //, $a;
437 my @b = split //, $b;
445 if (@b < (length $d)) {
447 } elsif ($aa eq $bb) {
449 } elsif ($aa < $bb) {
452 $out .= "[0-$bb$aa-9]";
458 # format a spot for user output in list mode
461 my $t = ztime($_[3]);
462 my $d = cldate($_[3]);
463 my $width = ($_[0] ? $_[0] : 80) - 80 + 28;
464 my $comment = $_[4] || '';
465 $comment = substr $comment, 0, $width if length($comment) > $width;
466 $comment .= ' ' x ($width - length($comment)) if length($comment) < $width;
468 # return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, ($_[3]||''), "<$_[4]" ;
469 return sprintf "%8.1f %-11s %s %s ${comment}%7s>", $_[1], $_[2], $d, $t, "<$_[5]" ;
472 # enter the spot for dup checking and return true if it is already a dup
475 my ($freq, $call, $d, $text, $by, $cty) = @_;
478 return 2 if $d < $main::systime - $dupage;
480 # turn the time into minutes (should be already but...)
484 # remove SSID or area
487 # $freq = sprintf "%.1f", $freq; # normalise frequency
488 $freq = int $freq; # normalise frequency
489 $call = substr($call, 0, $maxcalllth) if length $call > $maxcalllth;
492 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
493 $text = uc unpad($text);
494 if ($cty && $text && length $text <= 4) {
495 unless ($text =~ /^C?Q/ || $text =~ /^[\d\W]+$/) {
496 my @try = Prefix::cty_data($text);
497 $text = "" if $cty == $try[0];
501 # $text = Encode::encode("iso-8859-1", $text) if $main::can_encode && Encode::is_utf8($text, 1);
502 $text =~ s/^\+\w+\s*//; # remove leading LoTW callsign
503 $text =~ s/\s{2,}[\dA-Z]?[A-Z]\d?$// if length $text > 24;
504 $text =~ s/[\W\x00-\x2F\x7B-\xFF]//g; # tautology, just to make quite sure!
505 $text = substr($text, 0, $duplth) if length $text > $duplth;
506 my $ldupkey = "X$freq|$call|$by|$text";
507 my $t = DXDupe::find($ldupkey);
508 return 1 if $t && $t - $main::systime > 0;
509 DXDupe::add($ldupkey, $main::systime+$dupage);
510 $otext = substr($otext, 0, $duplth) if length $otext > $duplth;
512 if (length $otext && $otext ne $text) {
513 $ldupkey = "X$freq|$call|$by|$otext";
514 $t = DXDupe::find($ldupkey);
515 return 1 if $t && $t - $main::systime > 0;
516 DXDupe::add($ldupkey, $main::systime+$dupage);
523 return DXDupe::listdups('X', $dupage, @_);
529 my $in = $fp->open($date);
530 my $out = $statp->open($date, 'w');
537 @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);
540 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
541 my $ref = $list{$by} || [0, $dxcc];
543 next unless defined $_;
544 if ($freq >= $_->[1] && $freq <= $_->[2]) {
555 for ($i = 0; $i < @freq+2; $i++) {
558 $statp->write($date, join('^', 'TOTALS', @tot));
560 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
563 for ($i = 0; $i < @freq+2; ++$i) {
566 $statp->write($date, join('^', $call, @$ref));
572 # return true if the stat file is newer than than the spot file
576 my $in = $fp->mtime($date);
577 my $out = $statp->mtime($date);
578 return defined $out && defined $in && $out >= $in;
584 my $date = Julian::Day->new($main::systime)->sub(1);
585 genstats($date) unless checkstats($date);
591 return "$_[0]->[0]|$_[0]->[1]";
596 if ($spotcachedays > 0) {
597 my $now = Julian::Day->new($main::systime);
598 for (my $i = $spotcachedays; $i < $spotcachedays + 5; ++$i ) {
599 my $k = _cachek($now->sub($i));
600 if (exists $spotcache{$k}) {
601 dbg("Spot::spotcache deleting day $k, more than $spotcachedays days old");
602 delete $spotcache{$k};