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],
45 ['call_dxcc', 'nc', 5],
47 ['origin', 'c', 7, 9],
48 ['call_itu', 'ni', 8],
50 ['call_zone', 'nz', 9],
54 ['by_zone', 'nz', 11],
56 ['call_state', 'ns', 12],
58 ['by_state', 'ns', 13],
59 ['bystate', 'ns', 13],
61 # ['channel', 'c', 15],
62 # ['rbn', 'a', 4, 0, \&filterrbnspot],
64 $totalspots = $hfspots = $vhfspots = 0;
65 $use_db_for_search = 0;
70 $readback = `which tac`;
74 # create a Spot Object
79 return bless $self, $class;
86 my @f = split /,/, $l;
94 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
96 my @fr = Bands::get_freq(lc $a, $b);
101 push @out, "$a/$b"; # add them as ranges
104 return ('dfreq', $dxchan->msg('dfreq1', $f));
107 return ('dfreq', $dxchan->msg('e20', $f));
110 return (0, join(',', @out));
113 # filter setup for rbn spot so return the regex to detect it
122 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
123 $fp = DXLog::new($dirprefix, "dat", 'd');
124 $statp = DXLog::new($dirprefix, "dys", 'd');
126 # load up any old spots
128 unless (grep $_ eq 'spot', $main::dbh->show_tables) {
129 dbg('initialising spot tables');
132 $main::dbh->spot_create_table;
134 my $now = Julian::Day->alloc(1995, 0);
135 my $today = Julian::Day->new(time);
136 my $sth = $main::dbh->spot_insert_prepare;
137 while ($now->cmp($today) <= 0) {
138 my $fh = $fp->open($now);
140 # $main::dbh->{RaiseError} = 0;
141 $main::dbh->begin_work;
147 my @a = (Prefix::cty_data($s[1]))[1..3];
148 my @b = (Prefix::cty_data($s[4]))[1..3];
149 push @s, $b[1] if @s < 7;
150 push @s, '' if @s < 8;
151 push @s, @a[0,1], @b[0,1] if @s < 12;
152 push @s, $a[2], $b[2] if @s < 14;
154 $main::dbh->spot_insert(\@s, $sth);
158 dbg("inserted $count spots from $now->[0] $now->[1]");
164 $main::dbh->begin_work;
165 $main::dbh->spot_add_indexes;
167 # $main::dbh->{RaiseError} = 1;
169 my $min = int($t / 60);
171 dbg("$total spots converted in $min:$sec");
173 unless ($main::dbh->has_ipaddr) {
174 $main::dbh->add_ipaddr;
175 dbg("added ipaddr field to spot table");
182 return $fp->{prefix};
185 # fix up the full spot data from the basic spot data
187 # freq, call, time, comment, spotter, origin[, ip_address]
190 # $freq, $call, $t, $comment, $spotter, node, ip address = @_
191 my @out = @_[0..4]; # just up to the spotter
193 # normalise frequency
194 $out[0] = sprintf "%.1f", $out[0];
196 # remove ssids and /xxx if present on spotter
197 $out[4] =~ s/-\d+$//o;
199 # remove leading and trailing spaces
200 $out[3] = unpad($out[3]);
203 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
204 my @spd = Prefix::cty_data($out[1]);
206 my @spt = Prefix::cty_data($out[4]);
209 push @out, @spd[1,2], @spt[1,2], $spd[3], $spt[3];
210 push @out, $_[6] if $_[6] && is_ipaddr($_[6]);
213 # 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
219 my $buf = join('^', @_);
220 $fp->writeunix($_[2], $buf);
222 $main::dbh->begin_work;
223 $main::dbh->spot_insert(\@_);
227 if ($_[0] <= 30000) {
232 if ($_[3] =~ /(?:QSL|VIA)/i) {
233 my $q = QSL::get($_[1]) || new QSL $_[1];
234 $q->update($_[3], $_[2], $_[4]);
238 # search the spot database for records based on the field no and an expression
239 # this returns a set of references to the spots
241 # the expression is a legal perl 'if' statement with the possible fields indicated
246 # $f2 = date in unix format
249 # $f5 = spotted dxcc country
250 # $f6 = spotter dxcc country
253 # $f9 = spotted cq zone
255 # $f11 = spotter cq zone
256 # $f12 = spotted us state
257 # $f13 = spotter us state
260 # In addition you can specify a range of days, this means that it will start searching
261 # from <n> days less than today to <m> days less than today
263 # Also you can select a range of entries so normally you would get the 0th (latest) entry
264 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
266 # This routine is designed to be called as Spot::search(..)
271 my ($expr, $dayfrom, $dayto, $from, $to, $hint, $dxchan) = @_;
276 my $today = Julian::Day->new(time());
280 $dayfrom = 0 if !$dayfrom;
281 $dayto = $maxdays unless $dayto;
282 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
283 $fromdate = $today->sub($dayfrom);
284 $todate = $fromdate->sub($dayto);
285 $from = 0 unless $from;
286 $to = $defaultspots unless $to;
287 $hint = $hint ? "next unless $hint" : "";
288 $expr = "1" unless $expr;
290 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
292 if ($main::dbh && $use_db_for_search) {
293 return $main::dbh->spot_search($hint, $dayfrom, $dayto, $to-$from, $dxchan);
296 # $expr =~ s/\$f(\d\d?)/\$ref->[$1]/g; # swap the letter n for the correct field name
297 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
300 dbg("Spot::search hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
302 # build up eval to execute
303 dbg("Spot::search Spot eval: $expr") if isdbg('searcheval');
304 $expr =~ s/\$r/\$_[0]/g;
305 my $eval = qq{ sub { return $expr; } };
306 dbg("Spot::search Spot eval: $eval") if isdbg('searcheval');
307 my $ecode = eval $eval;
308 return ("Spot search error", $@) if $@;
316 for ($i = $count = 0; $count < $to && $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
317 last if $now->cmp($todate) <= 0;
319 my $fn = $fp->fn($now->sub($i));
321 dbg("Spot::search search using tac fn: $fn $i") if isdbg('search');
322 $fh = IO::File->new("$readback $fn |");
325 dbg("Spot::search search fn: $fp->{fn} $i") if isdbg('search');
326 $fh = $fp->open($now->sub($i)); # get the next file
335 my ($gotone, undef) = $dxchan->{spotsfilter}->it(@r);
340 next if $count < $from;
343 last if $count >= $to;
346 shift @out if $count >= $to;
350 dbg("Spot::search recs read: $rec") if isdbg('search');
351 last if $count >= $to; # stop after to
353 return ("Spot search error", $@) if $@;
356 @out = sort {$b->[2] <=> $a->[2]} @out if @out;
360 # change a freq range->regular expression
364 return undef unless $a < $b;
367 my @a = split //, $a;
368 my @b = split //, $b;
376 if (@b < (length $d)) {
378 } elsif ($aa eq $bb) {
380 } elsif ($aa < $bb) {
383 $out .= "[0-$bb$aa-9]";
389 # format a spot for user output in list mode
392 my $t = ztime($_[2]);
393 my $d = cldate($_[2]);
394 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, ($_[3]||''), "<$_[4]" ;
397 # enter the spot for dup checking and return true if it is already a dup
400 my ($freq, $call, $d, $text, $by, $cty) = @_;
403 return 2 if $d < $main::systime - $dupage;
405 # turn the time into minutes (should be already but...)
409 # remove SSID or area
412 # $freq = sprintf "%.1f", $freq; # normalise frequency
413 $freq = int $freq; # normalise frequency
414 $call = substr($call, 0, $maxcalllth) if length $call > $maxcalllth;
417 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
418 $text = uc unpad($text);
419 if ($cty && $text && length $text <= 4) {
420 unless ($text =~ /^C?Q/ || $text =~ /^[\d\W]+$/) {
421 my @try = Prefix::cty_data($text);
422 $text = "" if $cty == $try[0];
426 # $text = Encode::encode("iso-8859-1", $text) if $main::can_encode && Encode::is_utf8($text, 1);
427 $text =~ s/^\+\w+\s*//; # remove leading LoTW callsign
428 $text =~ s/\s{2,}[\dA-Z]?[A-Z]\d?$// if length $text > 24;
429 $text =~ s/[\W\x00-\x2F\x7B-\xFF]//g; # tautology, just to make quite sure!
430 $text = substr($text, 0, $duplth) if length $text > $duplth;
431 my $ldupkey = "X$freq|$call|$by|$text";
432 my $t = DXDupe::find($ldupkey);
433 return 1 if $t && $t - $main::systime > 0;
434 DXDupe::add($ldupkey, $main::systime+$dupage);
435 $otext = substr($otext, 0, $duplth) if length $otext > $duplth;
437 if (length $otext && $otext ne $text) {
438 $ldupkey = "X$freq|$call|$by|$otext";
439 $t = DXDupe::find($ldupkey);
440 return 1 if $t && $t - $main::systime > 0;
441 DXDupe::add($ldupkey, $main::systime+$dupage);
448 return DXDupe::listdups('X', $dupage, @_);
454 my $in = $fp->open($date);
455 my $out = $statp->open($date, 'w');
462 @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);
465 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
466 my $ref = $list{$by} || [0, $dxcc];
468 next unless defined $_;
469 if ($freq >= $_->[1] && $freq <= $_->[2]) {
480 for ($i = 0; $i < @freq+2; $i++) {
483 $statp->write($date, join('^', 'TOTALS', @tot));
485 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
488 for ($i = 0; $i < @freq+2; ++$i) {
491 $statp->write($date, join('^', $call, @$ref));
497 # return true if the stat file is newer than than the spot file
501 my $in = $fp->mtime($date);
502 my $out = $statp->mtime($date);
503 return defined $out && defined $in && $out >= $in;
509 my $date = Julian::Day->new($main::systime)->sub(1);
510 genstats($date) unless checkstats($date);