4 # Copyright (c) - 1998 Dirk Koopman G1TLH
26 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef
27 $totalspots $hfspots $vhfspots $maxcalllth $can_encode $use_db_for_search);
31 $maxspots = 100; # maximum spots to return
32 $defaultspots = 10; # normal number of spots to return
33 $maxdays = 100; # normal maximum no of days to go back
35 $duplth = 20; # the length of text to use in the deduping
36 $dupage = 1*3600; # the length of time to hold spot dups
37 $maxcalllth = 12; # the max length of call to take into account for dupes
39 # tag, sort, field, priv, special parser
40 ['freq', 'r', 0, 0, \&decodefreq],
41 ['on', 'r', 0, 0, \&decodefreq],
47 ['call_dxcc', 'nc', 5],
49 ['origin', 'c', 7, 9],
50 ['call_itu', 'ni', 8],
52 ['call_zone', 'nz', 9],
57 ['by_zone', 'nz', 11],
60 ['call_state', 'ns', 12],
62 ['by_state', 'ns', 13],
63 ['bystate', 'ns', 13],
65 # ['channel', 'c', 15],
66 # ['rbn', 'a', 4, 0, \&filterrbnspot],
68 $totalspots = $hfspots = $vhfspots = 0;
69 $use_db_for_search = 0;
71 our %spotcache; # the cache of data within the last $spotcachedays 0 or 2+ days
72 our $spotcachedays = 0;
78 $readback = `which tac`;
82 # create a Spot Object
87 return bless $self, $class;
94 my @f = split /,/, $l;
100 if ($f =~ m{^\d+/\d+$}) {
102 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
104 my @fr = Bands::get_freq(lc $a, $b);
109 push @out, "$a/$b"; # add them as ranges
112 return ('dfreq', $dxchan->msg('dfreq1', $f));
115 return ('dfreq', $dxchan->msg('e20', $f));
118 return (0, join(',', @out));
121 # filter setup for rbn spot so return the regex to detect it
130 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
131 $fp = DXLog::new($dirprefix, "dat", 'd');
132 $statp = DXLog::new($dirprefix, "dys", 'd');
133 my $today = Julian::Day->new(time);
135 # load up any old spots
137 unless (grep $_ eq 'spot', $main::dbh->show_tables) {
138 dbg('initialising spot tables');
141 $main::dbh->spot_create_table;
143 my $now = Julian::Day->alloc(1995, 0);
144 my $sth = $main::dbh->spot_insert_prepare;
145 while ($now->cmp($today) <= 0) {
146 my $fh = $fp->open($now);
148 # $main::dbh->{RaiseError} = 0;
149 $main::dbh->begin_work;
155 my @a = (Prefix::cty_data($s[1]))[1..3];
156 my @b = (Prefix::cty_data($s[4]))[1..3];
157 push @s, $b[1] if @s < 7;
158 push @s, '' if @s < 8;
159 push @s, @a[0,1], @b[0,1] if @s < 12;
160 push @s, $a[2], $b[2] if @s < 14;
162 $main::dbh->spot_insert(\@s, $sth);
166 dbg("inserted $count spots from $now->[0] $now->[1]");
172 $main::dbh->begin_work;
173 $main::dbh->spot_add_indexes;
175 # $main::dbh->{RaiseError} = 1;
177 my $min = int($t / 60);
179 dbg("$total spots converted in $min:$sec");
181 unless ($main::dbh->has_ipaddr) {
182 $main::dbh->add_ipaddr;
183 dbg("added ipaddr field to spot table");
187 # initialise the cache if required
188 if ($spotcachedays) {
189 $spotcachedays = 2 if $spotcachedays < 2;
190 my $now = $today->sub($spotcachedays);
191 while ($now->cmp($today) >= 0) {
192 my $fh = $fp->open($now);
199 my @a = (Prefix::cty_data($s[1]))[1..3];
200 my @b = (Prefix::cty_data($s[4]))[1..3];
201 push @s, $b[1] if @s < 7;
202 push @s, '' if @s < 8;
203 push @s, @a[0,1], @b[0,1] if @s < 12;
204 push @s, $a[2], $b[2] if @s < 14;
209 $spotcache{"$now->[0]|$now->[1]"} = \@in;
218 return $fp->{prefix};
221 # fix up the full spot data from the basic spot data
223 # freq, call, time, comment, spotter, origin[, ip_address]
226 # $freq, $call, $t, $comment, $spotter, node, ip address = @_
227 my @out = @_[0..4]; # just up to the spotter
229 # normalise frequency
230 $out[0] = sprintf "%.1f", $out[0];
232 # remove ssids and /xxx if present on spotter
233 $out[4] =~ s/-\d+$//o;
235 # remove leading and trailing spaces
236 $out[3] = unpad($out[3]);
239 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
240 my @spd = Prefix::cty_data($out[1]);
242 my @spt = Prefix::cty_data($out[4]);
245 push @out, @spd[1,2], @spt[1,2], $spd[3], $spt[3];
246 push @out, $_[6] if $_[6] && is_ipaddr($_[6]);
249 # 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
255 my $buf = join('^', @_);
256 $fp->writeunix($_[2], $buf);
257 if ($spotcachedays) {
258 my $now = Julian::Day->new($_[2]);
259 my $day = "$now->[0]|$now->[1]";
260 my $r = exists $spotcache{$day} ? $spotcache{$day} : $spotcache{$day} = [];
264 while (keys %spotcache > $spotcachedays+1) {
265 while (sort keys %spotcache > $spotcachedays+1) {
266 delete $spotcache{$_};
271 $main::dbh->begin_work;
272 $main::dbh->spot_insert(\@_);
276 if ($_[0] <= 30000) {
281 if ($_[3] =~ /(?:QSL|VIA)/i) {
282 my $q = QSL::get($_[1]) || new QSL $_[1];
283 $q->update($_[3], $_[2], $_[4]);
287 # search the spot database for records based on the field no and an expression
288 # this returns a set of references to the spots
290 # the expression is a legal perl 'if' statement with the possible fields indicated
295 # $f2 = date in unix format
298 # $f5 = spotted dxcc country
299 # $f6 = spotter dxcc country
302 # $f9 = spotted cq zone
304 # $f11 = spotter cq zone
305 # $f12 = spotted us state
306 # $f13 = spotter us state
309 # In addition you can specify a range of days, this means that it will start searching
310 # from <n> days less than today to <m> days less than today
312 # Also you can select a range of entries so normally you would get the 0th (latest) entry
313 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
315 # This routine is designed to be called as Spot::search(..)
320 my ($expr, $dayfrom, $dayto, $from, $to, $hint, $dofilter, $dxchan) = @_;
325 my $today = Julian::Day->new(time());
329 $dayfrom = 0 if !$dayfrom;
330 $dayto = $maxdays unless $dayto;
331 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
332 $fromdate = $today->sub($dayfrom);
333 $todate = $fromdate->sub($dayto);
334 $from = 0 unless $from;
335 $to = $defaultspots unless $to;
336 $hint = $hint ? "next unless $hint" : "";
337 $expr = "1" unless $expr;
339 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
341 if ($main::dbh && $use_db_for_search) {
342 return $main::dbh->spot_search($expr, $dayfrom, $dayto, $from, $to, $hint, $dofilter, $dxchan);
345 # $expr =~ s/\$f(\d\d?)/\$ref->[$1]/g; # swap the letter n for the correct field name
346 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
349 dbg("Spot::search hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
351 # build up eval to execute
352 dbg("Spot::search Spot eval: $expr") if isdbg('searcheval');
353 $expr =~ s/\$r/\$_[0]/g;
354 my $eval = qq{ sub { return $expr; } };
355 dbg("Spot::search Spot eval: $eval") if isdbg('searcheval');
356 my $ecode = eval $eval;
357 return ("Spot search error", $@) if $@;
362 my $today = Julian::Day->new($main::systime);
364 for ($i = $count = 0; $count < $to && $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
365 last if $now->cmp($todate) <= 0;
367 if ($spotcachedays) {
370 my $fn = $fp->fn($now->sub($i));
372 dbg("Spot::search search using tac fn: $fn $i") if isdbg('search');
373 $fh = IO::File->new("$readback $fn |");
376 dbg("Spot::search search fn: $fp->{fn} $i") if isdbg('search');
377 $fh = $fp->open($now->sub($i)); # get the next file
386 if ($dofilter && $dxchan && $dxchan->{spotsfilter}) {
387 my ($gotone, undef) = $dxchan->{spotsfilter}->it(@r);
392 next if $count < $from;
395 last if $count >= $to;
398 shift @out if $count >= $to;
402 dbg("Spot::search recs read: $rec") if isdbg('search');
403 last if $count >= $to; # stop after to
405 return ("Spot search error", $@) if $@;
408 @out = sort {$b->[2] <=> $a->[2]} @out if @out;
412 # change a freq range->regular expression
416 return undef unless $a < $b;
419 my @a = split //, $a;
420 my @b = split //, $b;
428 if (@b < (length $d)) {
430 } elsif ($aa eq $bb) {
432 } elsif ($aa < $bb) {
435 $out .= "[0-$bb$aa-9]";
441 # format a spot for user output in list mode
444 my $t = ztime($_[2]);
445 my $d = cldate($_[2]);
446 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, ($_[3]||''), "<$_[4]" ;
449 # enter the spot for dup checking and return true if it is already a dup
452 my ($freq, $call, $d, $text, $by, $cty) = @_;
455 return 2 if $d < $main::systime - $dupage;
457 # turn the time into minutes (should be already but...)
461 # remove SSID or area
464 # $freq = sprintf "%.1f", $freq; # normalise frequency
465 $freq = int $freq; # normalise frequency
466 $call = substr($call, 0, $maxcalllth) if length $call > $maxcalllth;
469 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
470 $text = uc unpad($text);
471 if ($cty && $text && length $text <= 4) {
472 unless ($text =~ /^C?Q/ || $text =~ /^[\d\W]+$/) {
473 my @try = Prefix::cty_data($text);
474 $text = "" if $cty == $try[0];
478 # $text = Encode::encode("iso-8859-1", $text) if $main::can_encode && Encode::is_utf8($text, 1);
479 $text =~ s/^\+\w+\s*//; # remove leading LoTW callsign
480 $text =~ s/\s{2,}[\dA-Z]?[A-Z]\d?$// if length $text > 24;
481 $text =~ s/[\W\x00-\x2F\x7B-\xFF]//g; # tautology, just to make quite sure!
482 $text = substr($text, 0, $duplth) if length $text > $duplth;
483 my $ldupkey = "X$freq|$call|$by|$text";
484 my $t = DXDupe::find($ldupkey);
485 return 1 if $t && $t - $main::systime > 0;
486 DXDupe::add($ldupkey, $main::systime+$dupage);
487 $otext = substr($otext, 0, $duplth) if length $otext > $duplth;
489 if (length $otext && $otext ne $text) {
490 $ldupkey = "X$freq|$call|$by|$otext";
491 $t = DXDupe::find($ldupkey);
492 return 1 if $t && $t - $main::systime > 0;
493 DXDupe::add($ldupkey, $main::systime+$dupage);
500 return DXDupe::listdups('X', $dupage, @_);
506 my $in = $fp->open($date);
507 my $out = $statp->open($date, 'w');
514 @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);
517 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
518 my $ref = $list{$by} || [0, $dxcc];
520 next unless defined $_;
521 if ($freq >= $_->[1] && $freq <= $_->[2]) {
532 for ($i = 0; $i < @freq+2; $i++) {
535 $statp->write($date, join('^', 'TOTALS', @tot));
537 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
540 for ($i = 0; $i < @freq+2; ++$i) {
543 $statp->write($date, join('^', $call, @$ref));
549 # return true if the stat file is newer than than the spot file
553 my $in = $fp->mtime($date);
554 my $out = $statp->mtime($date);
555 return defined $out && defined $in && $out >= $in;
561 my $date = Julian::Day->new($main::systime)->sub(1);
562 genstats($date) unless checkstats($date);