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],
43 ['call_dxcc', 'nc', 5],
45 ['origin', 'c', 7, 9],
46 ['call_itu', 'ni', 8],
47 ['call_zone', 'nz', 9],
49 ['by_zone', 'nz', 11],
50 ['call_state', 'ns', 12],
51 ['by_state', 'ns', 13],
53 # ['channel', 'c', 15],
54 # ['rbn', 'a', 4, 0, \&filterrbnspot],
56 $totalspots = $hfspots = $vhfspots = 0;
57 $use_db_for_search = 0;
59 # create a Spot Object
64 return bless $self, $class;
71 my @f = split /,/, $l;
79 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
81 my @fr = Bands::get_freq(lc $a, $b);
86 push @out, "$a/$b"; # add them as ranges
89 return ('dfreq', $dxchan->msg('dfreq1', $f));
92 return ('dfreq', $dxchan->msg('e20', $f));
95 return (0, join(',', @out));
98 # filter setup for rbn spot so return the regex to detect it
107 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
108 $fp = DXLog::new($dirprefix, "dat", 'd');
109 $statp = DXLog::new($dirprefix, "dys", 'd');
111 # load up any old spots
113 unless (grep $_ eq 'spot', $main::dbh->show_tables) {
114 dbg('initialising spot tables');
117 $main::dbh->spot_create_table;
119 my $now = Julian::Day->alloc(1995, 0);
120 my $today = Julian::Day->new(time);
121 my $sth = $main::dbh->spot_insert_prepare;
122 while ($now->cmp($today) <= 0) {
123 my $fh = $fp->open($now);
125 # $main::dbh->{RaiseError} = 0;
126 $main::dbh->begin_work;
132 my @a = (Prefix::cty_data($s[1]))[1..3];
133 my @b = (Prefix::cty_data($s[4]))[1..3];
134 push @s, $b[1] if @s < 7;
135 push @s, '' if @s < 8;
136 push @s, @a[0,1], @b[0,1] if @s < 12;
137 push @s, $a[2], $b[2] if @s < 14;
139 $main::dbh->spot_insert(\@s, $sth);
143 dbg("inserted $count spots from $now->[0] $now->[1]");
149 $main::dbh->begin_work;
150 $main::dbh->spot_add_indexes;
152 # $main::dbh->{RaiseError} = 1;
154 my $min = int($t / 60);
156 dbg("$total spots converted in $min:$sec");
158 unless ($main::dbh->has_ipaddr) {
159 $main::dbh->add_ipaddr;
160 dbg("added ipaddr field to spot table");
167 return $fp->{prefix};
170 # fix up the full spot data from the basic spot data
173 # $freq, $call, $t, $comment, $spotter, node, ip address = @_
174 my @out = @_[0..4]; # just up to the spotter
176 # normalise frequency
177 $out[0] = sprintf "%.1f", $out[0];
179 # remove ssids and /xxx if present on spotter
180 $out[4] =~ s/-\d+$//o;
182 # remove leading and trailing spaces
183 $out[3] = unpad($out[3]);
186 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
187 my @spd = Prefix::cty_data($out[1]);
189 my @spt = Prefix::cty_data($out[4]);
192 push @out, @spd[1,2], @spt[1,2], $spd[3], $spt[3];
193 push @out, $_[6] if $_[6] && is_ipaddr($_[6]);
196 # freq, call, time, comment, spotter, call country code, call itu, call cqzone, spotter country code, spotter itu, spotter cqzone, call state, spotter state, node, spotter ip address
202 my $buf = join('^', @_);
203 $fp->writeunix($_[2], $buf);
205 $main::dbh->begin_work;
206 $main::dbh->spot_insert(\@_);
210 if ($_[0] <= 30000) {
215 if ($_[3] =~ /(?:QSL|VIA)/i) {
216 my $q = QSL::get($_[1]) || new QSL $_[1];
217 $q->update($_[3], $_[2], $_[4]);
221 # search the spot database for records based on the field no and an expression
222 # this returns a set of references to the spots
224 # the expression is a legal perl 'if' statement with the possible fields indicated
229 # $f2 = date in unix format
232 # $f5 = spotted dxcc country
233 # $f6 = spotter dxcc country
237 # In addition you can specify a range of days, this means that it will start searching
238 # from <n> days less than today to <m> days less than today
240 # Also you can select a range of entries so normally you would get the 0th (latest) entry
241 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
243 # This routine is designed to be called as Spot::search(..)
248 my ($expr, $dayfrom, $dayto, $from, $to, $hint, $dxchan) = @_;
254 my $today = Julian::Day->new(time());
258 $dayfrom = 0 if !$dayfrom;
259 $dayto = $maxdays unless $dayto;
260 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
261 $fromdate = $today->sub($dayfrom);
262 $todate = $fromdate->sub($dayto);
263 $from = 0 unless $from;
264 $to = $defaultspots unless $to;
265 $hint = $hint ? "next unless $hint" : "";
266 $expr = "1" unless $expr;
268 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
270 if ($main::dbh && $use_db_for_search) {
271 return $main::dbh->spot_search($expr, $dayfrom, $dayto, $to-$from, $dxchan);
274 $expr =~ s/\$f(\d\d?)/\$ref->[$1]/g; # swap the letter n for the correct field name
275 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
280 my \@a = (Prefix::cty_data(\$s[1]))[1..3];
281 my \@b = (Prefix::cty_data(\$s[4]))[1..3];
282 push \@s, \@a[0,1], \@b[0,1], \$a[2], \$a[2];
287 my (\$filter, \$hops) = \$dxchan->{spotsfilter}->it(\@s);
288 next unless (\$filter);
290 $checkfilter ||= ' ';
292 dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
294 # build up eval to execute
299 my \@s = split /\\^/;
305 for (\$c = \$#spots; \$c >= 0; \$c--) {
306 \$ref = \$spots[\$c];
309 next if \$count < \$from; # wait until from
311 last if \$count >= \$to; # stop after to
317 dbg("Spot eval: $eval") if isdbg('searcheval');
320 $fp->close; # close any open files
322 for ($i = $count = 0; $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
323 my $now = $fromdate->sub($i); # but you can pick which $maxdays worth
324 last if $now->cmp($todate) <= 0;
327 my $fh = $fp->open($now); # get the next file
330 eval $eval; # do the search on this file
331 last if $count >= $to; # stop after to
332 return ("Spot search error", $@) if $@;
339 # change a freq range->regular expression
343 return undef unless $a < $b;
346 my @a = split //, $a;
347 my @b = split //, $b;
355 if (@b < (length $d)) {
357 } elsif ($aa eq $bb) {
359 } elsif ($aa < $bb) {
362 $out .= "[0-$bb$aa-9]";
368 # format a spot for user output in list mode
371 my $t = ztime($_[2]);
372 my $d = cldate($_[2]);
373 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, ($_[3]||''), "<$_[4]" ;
377 # return all the spots from a day's file as an array of references
378 # the parameter passed is a julian day
383 my $fh = $fp->open(shift);
388 push @spots, [ split '\^' ];
394 # enter the spot for dup checking and return true if it is already a dup
397 my ($freq, $call, $d, $text, $by, $cty) = @_;
400 return 2 if $d < $main::systime - $dupage;
402 # turn the time into minutes (should be already but...)
406 # remove SSID or area
409 # $freq = sprintf "%.1f", $freq; # normalise frequency
410 $freq = int $freq; # normalise frequency
411 $call = substr($call, 0, $maxcalllth) if length $call > $maxcalllth;
414 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
415 $text = uc unpad($text);
416 if ($cty && $text && length $text <= 4) {
417 unless ($text =~ /^C?Q/ || $text =~ /^[\d\W]+$/) {
418 my @try = Prefix::cty_data($text);
419 $text = "" if $cty == $try[0];
423 # $text = Encode::encode("iso-8859-1", $text) if $main::can_encode && Encode::is_utf8($text, 1);
424 $text =~ s/^\+\w+\s*//; # remove leading LoTW callsign
425 $text =~ s/\s{2,}[\dA-Z]?[A-Z]\d?$// if length $text > 24;
426 $text =~ s/[\W\x00-\x2F\x7B-\xFF]//g; # tautology, just to make quite sure!
427 $text = substr($text, 0, $duplth) if length $text > $duplth;
428 my $ldupkey = "X$freq|$call|$by|$text";
429 my $t = DXDupe::find($ldupkey);
430 return 1 if $t && $t - $main::systime > 0;
431 DXDupe::add($ldupkey, $main::systime+$dupage);
432 $otext = substr($otext, 0, $duplth) if length $otext > $duplth;
434 if (length $otext && $otext ne $text) {
435 $ldupkey = "X$freq|$call|$by|$otext";
436 $t = DXDupe::find($ldupkey);
437 return 1 if $t && $t - $main::systime > 0;
438 DXDupe::add($ldupkey, $main::systime+$dupage);
445 return DXDupe::listdups('X', $dupage, @_);
451 my $in = $fp->open($date);
452 my $out = $statp->open($date, 'w');
459 @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);
462 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
463 my $ref = $list{$by} || [0, $dxcc];
465 next unless defined $_;
466 if ($freq >= $_->[1] && $freq <= $_->[2]) {
477 for ($i = 0; $i < @freq+2; $i++) {
480 $statp->write($date, join('^', 'TOTALS', @tot));
482 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
485 for ($i = 0; $i < @freq+2; ++$i) {
488 $statp->write($date, join('^', $call, @$ref));
494 # return true if the stat file is newer than than the spot file
498 my $in = $fp->mtime($date);
499 my $out = $statp->mtime($date);
500 return defined $out && defined $in && $out >= $in;
506 my $date = Julian::Day->new($main::systime)->sub(1);
507 genstats($date) unless checkstats($date);