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 from comment field
242 $out[3] = unpad($out[3]);
244 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
245 my @spd = Prefix::cty_data($out[1]);
247 my @spt = Prefix::cty_data($out[4]);
250 push @out, @spd[1,2], @spt[1,2], $spd[3], $spt[3];
251 push @out, $_[6] if $_[6] && is_ipaddr($_[6]);
254 # 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
260 my $buf = join('^', @_);
261 $fp->writeunix($_[2], $buf);
262 if ($spotcachedays > 0) {
263 my $now = Julian::Day->new($_[2]);
264 my $day = _cachek($now);
265 my $r = (exists $spotcache{$day}) ? $spotcache{$day} : ($spotcache{$day} = []);
269 $main::dbh->begin_work;
270 $main::dbh->spot_insert(\@_);
274 if ($_[0] <= 30000) {
279 if ($_[3] =~ /(?:QSL|VIA)/i) {
280 my $q = QSL::get($_[1]) || new QSL $_[1];
281 $q->update($_[3], $_[2], $_[4]);
285 # search the spot database for records based on the field no and an expression
286 # this returns a set of references to the spots
288 # the expression is a legal perl 'if' statement with the possible fields indicated
293 # $f2 = date in unix format
296 # $f5 = spotted dxcc country
297 # $f6 = spotter dxcc country
300 # $f9 = spotted cq zone
302 # $f11 = spotter cq zone
303 # $f12 = spotted us state
304 # $f13 = spotter us state
307 # In addition you can specify a range of days, this means that it will start searching
308 # from <n> days less than today to <m> days less than today
310 # Also you can select a range of entries so normally you would get the 0th (latest) entry
311 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
313 # This routine is designed to be called as Spot::search(..)
318 my ($expr, $dayfrom, $dayto, $from, $to, $hint, $dofilter, $dxchan) = @_;
323 my $today = Julian::Day->new(time());
327 $dayfrom = 0 if !$dayfrom;
328 $dayto = $maxdays unless $dayto;
329 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
330 $fromdate = $today->sub($dayfrom);
331 $todate = $fromdate->sub($dayto);
332 $from = 0 unless $from;
333 $to = $defaultspots unless $to;
334 $hint = $hint ? "next unless $hint" : "";
335 $expr = "1" unless $expr;
337 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
339 if ($main::dbh && $use_db_for_search) {
340 return $main::dbh->spot_search($expr, $dayfrom, $dayto, $from, $to, $hint, $dofilter, $dxchan);
343 # $expr =~ s/\$f(\d\d?)/\$ref->[$1]/g; # swap the letter n for the correct field name
344 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
347 dbg("Spot::search hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
349 # 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 $@;
360 my $today = Julian::Day->new($main::systime);
362 for ($i = $count = 0; $count < $to && $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
363 last if $now->cmp($todate) <= 0;
366 my $this = $now->sub($i);
367 my $fn = $fp->fn($this);
368 my $cachekey = _cachek($this);
371 if ($spotcachedays > 0 && $spotcache{$cachekey}) {
372 foreach my $r (@{$spotcache{$cachekey}}) {
374 if ($dofilter && $dxchan && $dxchan->{spotsfilter}) {
375 my ($gotone, undef) = $dxchan->{spotsfilter}->it(@$r);
380 next if $count < $from;
382 last if $count >= $to;
385 dbg("Spot::search cache recs read: $rec") if isdbg('search');
388 dbg("Spot::search search using tac fn: $fn $i") if isdbg('search');
389 $fh = IO::File->new("$readback $fn |");
392 dbg("Spot::search search fn: $fp->{fn} $i") if isdbg('search');
393 $fh = $fp->open($now->sub($i)); # get the next file
401 if ($dofilter && $dxchan && $dxchan->{spotsfilter}) {
402 my ($gotone, undef) = $dxchan->{spotsfilter}->it(@r);
407 next if $count < $from;
410 last if $count >= $to;
413 shift @out if $count >= $to;
417 dbg("Spot::search file recs read: $rec") if isdbg('search');
418 last if $count >= $to; # stop after to
422 return ("Spot search error", $@) if $@;
424 @out = sort {$b->[2] <=> $a->[2]} @out if @out;
428 # change a freq range->regular expression
432 return undef unless $a < $b;
435 my @a = split //, $a;
436 my @b = split //, $b;
444 if (@b < (length $d)) {
446 } elsif ($aa eq $bb) {
448 } elsif ($aa < $bb) {
451 $out .= "[0-$bb$aa-9]";
457 # format a spot for user output in list mode
460 my $t = ztime($_[3]);
461 my $d = cldate($_[3]);
462 my $spotter = "<$_[5]>";
463 my $comment = $_[4] || '';
464 $comment =~ s/\t+/ /g;
465 my $cl = length $comment;
466 my $s = sprintf "%9.1f %-11s %s %s", $_[1], $_[2], $d, $t;
467 my $width = ($_[0] ? $_[0] : 80) - length($spotter) - length($s) - 4;
469 $comment = substr $comment, 0, $width if $cl > $width;
470 $comment .= ' ' x ($width-$cl) if $cl < $width;
472 # return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, ($_[3]||''), "<$_[4]" ;
473 return "$s $comment$spotter";
476 # enter the spot for dup checking and return true if it is already a dup
479 my ($freq, $call, $d, $text, $by, $node) = @_;
482 return 2 if $d < $main::systime - $dupage;
484 # turn the time into minutes (should be already but...)
488 # remove SSID or area
491 # $freq = sprintf "%.1f", $freq; # normalise frequency
492 $freq = int $freq; # normalise frequency
493 $call = substr($call, 0, $maxcalllth) if length $call > $maxcalllth;
496 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
497 $text = uc unpad($text);
499 # $text = Encode::encode("iso-8859-1", $text) if $main::can_encode && Encode::is_utf8($text, 1);
500 $text =~ s/^\+\w+\s*//; # remove leading LoTW callsign
501 $text =~ s/\s{2,}[\dA-Z]?[A-Z]\d?$// if length $text > 24;
502 $text =~ s/[\W\x00-\x2F\x7B-\xFF]//g; # tautology, just to make quite sure!
503 $text = substr($text, 0, $duplth) if length $text > $duplth;
504 my $ldupkey = "X$|$call|$by|$node|$freq|$d|$text";
505 my $t = DXDupe::find($ldupkey);
506 return 1 if $t && $t - $main::systime > 0;
508 DXDupe::add($ldupkey, $main::systime+$dupage);
509 $otext = substr($otext, 0, $duplth) if length $otext > $duplth;
511 if (length $otext && $otext ne $text) {
512 $ldupkey = "X$freq|$call|$by|$otext";
513 $t = DXDupe::find($ldupkey);
514 return 1 if $t && $t - $main::systime > 0;
515 DXDupe::add($ldupkey, $main::systime+$dupage);
522 return DXDupe::listdups('X', $dupage, @_);
528 my $in = $fp->open($date) or dbg("Spot::genstats: Cannot open " . $fp->fn($date) . " $!");
529 my $out = $statp->open($date, 'w') or dbg("Spot::genstats: Cannot open " . $statp->fn($date) . " $!");
536 @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);
539 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
540 my $ref = $list{$by} || [0, $dxcc];
542 next unless defined $_;
543 if ($freq >= $_->[1] && $freq <= $_->[2]) {
554 for ($i = 0; $i < @freq+2; $i++) {
557 $statp->write($date, join('^', 'TOTALS', @tot));
559 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
562 for ($i = 0; $i < @freq+2; ++$i) {
565 $statp->write($date, join('^', $call, @$ref));
571 # return true if the stat file is newer than than the spot file
575 my $in = $fp->mtime($date);
576 my $out = $statp->mtime($date);
577 return defined $out && defined $in && $out >= $in;
583 my $date = Julian::Day->new($main::systime)->sub(1);
584 genstats($date) unless checkstats($date);
590 return "$_[0]->[0]|$_[0]->[1]";
595 if ($spotcachedays > 0) {
596 my $now = Julian::Day->new($main::systime);
597 for (my $i = $spotcachedays; $i < $spotcachedays + 5; ++$i ) {
598 my $k = _cachek($now->sub($i));
599 if (exists $spotcache{$k}) {
600 dbg("Spot::spotcache deleting day $k, more than $spotcachedays days old");
601 delete $spotcache{$k};