4 # Copyright (c) - 1998 Dirk Koopman G1TLH
24 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef
25 $totalspots $hfspots $vhfspots $maxcalllth $can_encode $use_db_for_search);
29 $maxspots = 100; # maximum spots to return
30 $defaultspots = 10; # normal number of spots to return
31 $maxdays = 100; # normal maximum no of days to go back
33 $duplth = 20; # the length of text to use in the deduping
34 $dupage = 1*3600; # the length of time to hold spot dups
35 $maxcalllth = 12; # the max length of call to take into account for dupes
37 # tag, sort, field, priv, special parser
38 ['freq', 'r', 0, 0, \&decodefreq],
39 ['on', 'r', 0, 0, \&decodefreq],
44 ['call_dxcc', 'nc', 5],
46 ['origin', 'c', 7, 9],
47 ['call_itu', 'ni', 8],
49 ['call_zone', 'nz', 9],
53 ['by_zone', 'nz', 11],
55 ['call_state', 'ns', 12],
57 ['by_state', 'ns', 13],
58 ['bystate', 'ns', 13],
60 # ['channel', 'c', 15],
61 # ['rbn', 'a', 4, 0, \&filterrbnspot],
63 $totalspots = $hfspots = $vhfspots = 0;
64 $use_db_for_search = 0;
69 $readback = `which tac`;
73 # create a Spot Object
78 return bless $self, $class;
85 my @f = split /,/, $l;
93 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
95 my @fr = Bands::get_freq(lc $a, $b);
100 push @out, "$a/$b"; # add them as ranges
103 return ('dfreq', $dxchan->msg('dfreq1', $f));
106 return ('dfreq', $dxchan->msg('e20', $f));
109 return (0, join(',', @out));
112 # filter setup for rbn spot so return the regex to detect it
121 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
122 $fp = DXLog::new($dirprefix, "dat", 'd');
123 $statp = DXLog::new($dirprefix, "dys", 'd');
125 # load up any old spots
127 unless (grep $_ eq 'spot', $main::dbh->show_tables) {
128 dbg('initialising spot tables');
131 $main::dbh->spot_create_table;
133 my $now = Julian::Day->alloc(1995, 0);
134 my $today = Julian::Day->new(time);
135 my $sth = $main::dbh->spot_insert_prepare;
136 while ($now->cmp($today) <= 0) {
137 my $fh = $fp->open($now);
139 # $main::dbh->{RaiseError} = 0;
140 $main::dbh->begin_work;
146 my @a = (Prefix::cty_data($s[1]))[1..3];
147 my @b = (Prefix::cty_data($s[4]))[1..3];
148 push @s, $b[1] if @s < 7;
149 push @s, '' if @s < 8;
150 push @s, @a[0,1], @b[0,1] if @s < 12;
151 push @s, $a[2], $b[2] if @s < 14;
153 $main::dbh->spot_insert(\@s, $sth);
157 dbg("inserted $count spots from $now->[0] $now->[1]");
163 $main::dbh->begin_work;
164 $main::dbh->spot_add_indexes;
166 # $main::dbh->{RaiseError} = 1;
168 my $min = int($t / 60);
170 dbg("$total spots converted in $min:$sec");
172 unless ($main::dbh->has_ipaddr) {
173 $main::dbh->add_ipaddr;
174 dbg("added ipaddr field to spot table");
181 return $fp->{prefix};
184 # fix up the full spot data from the basic spot data
186 # freq, call, time, comment, spotter, origin[, ip_address]
189 # $freq, $call, $t, $comment, $spotter, node, ip address = @_
190 my @out = @_[0..4]; # just up to the spotter
192 # normalise frequency
193 $out[0] = sprintf "%.1f", $out[0];
195 # remove ssids and /xxx if present on spotter
196 $out[4] =~ s/-\d+$//o;
198 # remove leading and trailing spaces
199 $out[3] = unpad($out[3]);
202 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
203 my @spd = Prefix::cty_data($out[1]);
205 my @spt = Prefix::cty_data($out[4]);
208 push @out, @spd[1,2], @spt[1,2], $spd[3], $spt[3];
209 push @out, $_[6] if $_[6] && is_ipaddr($_[6]);
212 # 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
218 my $buf = join('^', @_);
219 $fp->writeunix($_[2], $buf);
221 $main::dbh->begin_work;
222 $main::dbh->spot_insert(\@_);
226 if ($_[0] <= 30000) {
231 if ($_[3] =~ /(?:QSL|VIA)/i) {
232 my $q = QSL::get($_[1]) || new QSL $_[1];
233 $q->update($_[3], $_[2], $_[4]);
237 # search the spot database for records based on the field no and an expression
238 # this returns a set of references to the spots
240 # the expression is a legal perl 'if' statement with the possible fields indicated
245 # $f2 = date in unix format
248 # $f5 = spotted dxcc country
249 # $f6 = spotter dxcc country
252 # $f9 = spotted cq zone
254 # $f11 = spotter cq zone
255 # $f12 = spotted us state
256 # $f13 = spotter us state
259 # In addition you can specify a range of days, this means that it will start searching
260 # from <n> days less than today to <m> days less than today
262 # Also you can select a range of entries so normally you would get the 0th (latest) entry
263 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
265 # This routine is designed to be called as Spot::search(..)
270 my ($expr, $dayfrom, $dayto, $from, $to, $hint, $dxchan) = @_;
275 my $today = Julian::Day->new(time());
279 $dayfrom = 0 if !$dayfrom;
280 $dayto = $maxdays unless $dayto;
281 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
282 $fromdate = $today->sub($dayfrom);
283 $todate = $fromdate->sub($dayto);
284 $from = 0 unless $from;
285 $to = $defaultspots unless $to;
286 $hint = $hint ? "next unless $hint" : "";
287 $expr = "1" unless $expr;
289 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
291 if ($main::dbh && $use_db_for_search) {
292 return $main::dbh->spot_search($hint, $dayfrom, $dayto, $to-$from, $dxchan);
295 # $expr =~ s/\$f(\d\d?)/\$ref->[$1]/g; # swap the letter n for the correct field name
296 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
299 dbg("Spot::search hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
301 # build up eval to execute
302 dbg("Spot::search Spot eval: $expr") if isdbg('searcheval');
303 $expr =~ s/\$r/\$_[0]/g;
304 my $eval = qq{ sub { return $expr; } };
305 dbg("Spot::search Spot eval: $eval") if isdbg('searcheval');
306 my $ecode = eval $eval;
307 return ("Spot search error", $@) if $@;
315 for ($i = $count = 0; $count < $to && $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
316 last if $now->cmp($todate) <= 0;
318 my $fn = $fp->fn($now->sub($i));
320 dbg("Spot::search search using tac fn: $fn $i") if isdbg('search');
321 $fh = IO::File->new("$readback $fn |");
324 dbg("Spot::search search fn: $fp->{fn} $i") if isdbg('search');
325 $fh = $fp->open($now->sub($i)); # get the next file
334 my ($gotone, undef) = $dxchan->{spotsfilter}->it(@r);
339 next if $count < $from;
342 last if $count >= $to;
345 shift @out if $count >= $to;
349 dbg("Spot::search recs read: $rec") if isdbg('search');
350 last if $count >= $to; # stop after to
352 return ("Spot search error", $@) if $@;
355 @out = sort {$b->[2] <=> $a->[2]} @out if @out;
359 # change a freq range->regular expression
363 return undef unless $a < $b;
366 my @a = split //, $a;
367 my @b = split //, $b;
375 if (@b < (length $d)) {
377 } elsif ($aa eq $bb) {
379 } elsif ($aa < $bb) {
382 $out .= "[0-$bb$aa-9]";
388 # format a spot for user output in list mode
391 my $t = ztime($_[2]);
392 my $d = cldate($_[2]);
393 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, ($_[3]||''), "<$_[4]" ;
396 # enter the spot for dup checking and return true if it is already a dup
399 my ($freq, $call, $d, $text, $by, $cty) = @_;
402 return 2 if $d < $main::systime - $dupage;
404 # turn the time into minutes (should be already but...)
408 # remove SSID or area
411 # $freq = sprintf "%.1f", $freq; # normalise frequency
412 $freq = int $freq; # normalise frequency
413 $call = substr($call, 0, $maxcalllth) if length $call > $maxcalllth;
416 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
417 $text = uc unpad($text);
418 if ($cty && $text && length $text <= 4) {
419 unless ($text =~ /^C?Q/ || $text =~ /^[\d\W]+$/) {
420 my @try = Prefix::cty_data($text);
421 $text = "" if $cty == $try[0];
425 # $text = Encode::encode("iso-8859-1", $text) if $main::can_encode && Encode::is_utf8($text, 1);
426 $text =~ s/^\+\w+\s*//; # remove leading LoTW callsign
427 $text =~ s/\s{2,}[\dA-Z]?[A-Z]\d?$// if length $text > 24;
428 $text =~ s/[\W\x00-\x2F\x7B-\xFF]//g; # tautology, just to make quite sure!
429 $text = substr($text, 0, $duplth) if length $text > $duplth;
430 my $ldupkey = "X$freq|$call|$by|$text";
431 my $t = DXDupe::find($ldupkey);
432 return 1 if $t && $t - $main::systime > 0;
433 DXDupe::add($ldupkey, $main::systime+$dupage);
434 $otext = substr($otext, 0, $duplth) if length $otext > $duplth;
436 if (length $otext && $otext ne $text) {
437 $ldupkey = "X$freq|$call|$by|$otext";
438 $t = DXDupe::find($ldupkey);
439 return 1 if $t && $t - $main::systime > 0;
440 DXDupe::add($ldupkey, $main::systime+$dupage);
447 return DXDupe::listdups('X', $dupage, @_);
453 my $in = $fp->open($date);
454 my $out = $statp->open($date, 'w');
461 @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);
464 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
465 my $ref = $list{$by} || [0, $dxcc];
467 next unless defined $_;
468 if ($freq >= $_->[1] && $freq <= $_->[2]) {
479 for ($i = 0; $i < @freq+2; $i++) {
482 $statp->write($date, join('^', 'TOTALS', @tot));
484 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
487 for ($i = 0; $i < @freq+2; ++$i) {
490 $statp->write($date, join('^', $call, @$ref));
496 # return true if the stat file is newer than than the spot file
500 my $in = $fp->mtime($date);
501 my $out = $statp->mtime($date);
502 return defined $out && defined $in && $out >= $in;
508 my $date = Julian::Day->new($main::systime)->sub(1);
509 genstats($date) unless checkstats($date);