4 # Copyright (c) - 1998 Dirk Koopman G1TLH
24 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef $totalspots $hfspots $vhfspots $maxcalllth $can_encode);
28 $maxspots = 100; # maximum spots to return
29 $defaultspots = 10; # normal number of spots to return
30 $maxdays = 100; # normal maximum no of days to go back
32 $duplth = 20; # the length of text to use in the deduping
33 $dupage = 1*3600; # the length of time to hold spot dups
34 $maxcalllth = 12; # the max length of call to take into account for dupes
36 # tag, sort, field, priv, special parser
37 ['freq', 'r', 0, 0, \&decodefreq],
38 ['on', 'r', 0, 0, \&decodefreq],
42 ['call_dxcc', 'nc', 5],
44 ['origin', 'c', 7, 9],
45 ['call_itu', 'ni', 8],
46 ['call_zone', 'nz', 9],
48 ['by_zone', 'nz', 11],
49 ['call_state', 'ns', 12],
50 ['by_state', 'ns', 13],
54 $totalspots = $hfspots = $vhfspots = 0;
56 # create a Spot Object
61 return bless $self, $class;
68 my @f = split /,/, $l;
76 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
78 my @fr = Bands::get_freq(lc $a, $b);
83 push @out, "$a/$b"; # add them as ranges
86 return ('dfreq', $dxchan->msg('dfreq1', $f));
89 return ('dfreq', $dxchan->msg('e20', $f));
92 return (0, join(',', @out));
97 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
98 $fp = DXLog::new($dirprefix, "dat", 'd');
99 $statp = DXLog::new($dirprefix, "dys", 'd');
101 # load up any old spots
103 unless (grep $_ eq 'spot', $main::dbh->show_tables) {
104 dbg('initialising spot tables');
107 $main::dbh->spot_create_table;
109 my $now = Julian::Day->alloc(1995, 0);
110 my $today = Julian::Day->new(time);
111 my $sth = $main::dbh->spot_insert_prepare;
112 $main::dbh->{RaiseError} = 0;
113 while ($now->cmp($today) <= 0) {
114 my $fh = $fp->open($now);
121 my @a = (Prefix::cty_data($s[1]))[1..3];
122 my @b = (Prefix::cty_data($s[4]))[1..3];
123 push @s, $b[1] if @s < 7;
124 push @s, '' if @s < 8;
125 push @s, @a[0,1], @b[0,1] if @s < 12;
126 push @s, $a[2], $a[2] if @s < 14;
129 push @s, undef while @s < 14;
130 pop @s while @s > 14;
132 $main::dbh->spot_insert(\@s, $sth);
135 $main::dbh->commit if $count;
136 $main::dbh->{RaiseError} = 0;
137 dbg("inserted $count spots from $now->[0] $now->[1]");
143 $main::dbh->spot_add_indexes;
145 $main::dbh->{RaiseError} = 1;
147 my $min = int($t / 60);
149 dbg("$total spots converted in $min:$sec");
156 return $fp->{prefix};
159 # fix up the full spot data from the basic spot data
162 # $freq, $call, $t, $comment, $spotter = @_
163 my @out = @_[0..4]; # just up to the spotter
165 # normalise frequency
166 $out[0] = sprintf "%.1f", $out[0];
168 # remove ssids and /xxx if present on spotter
169 $out[4] =~ s/-\d+$//o;
171 # remove leading and trailing spaces
172 $out[3] = unpad($out[3]);
175 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
176 my @spd = Prefix::cty_data($out[1]);
178 my @spt = Prefix::cty_data($out[4]);
181 return (@out, @spd[1,2], @spt[1,2], $spd[3], $spt[3]);
186 my $buf = join('^', @_);
187 $fp->writeunix($_[2], $buf);
189 $main::dbh->spot_insert(\@_);
193 if ($_[0] <= 30000) {
198 if ($_[3] =~ /(?:QSL|VIA)/i) {
199 my $q = QSL::get($_[1]) || new QSL $_[1];
200 $q->update($_[3], $_[2], $_[4]);
204 # search the spot database for records based on the field no and an expression
205 # this returns a set of references to the spots
207 # the expression is a legal perl 'if' statement with the possible fields indicated
212 # $f2 = date in unix format
215 # $f5 = spotted dxcc country
216 # $f6 = spotter dxcc country
220 # In addition you can specify a range of days, this means that it will start searching
221 # from <n> days less than today to <m> days less than today
223 # Also you can select a range of entries so normally you would get the 0th (latest) entry
224 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
226 # This routine is designed to be called as Spot::search(..)
231 my ($expr, $dayfrom, $dayto, $from, $to, $hint, $dxchan) = @_;
237 my $today = Julian::Day->new(time());
241 $dayfrom = 0 if !$dayfrom;
242 $dayto = $maxdays unless $dayto;
243 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
244 $fromdate = $today->sub($dayfrom);
245 $todate = $fromdate->sub($dayto);
246 $from = 0 unless $from;
247 $to = $defaultspots unless $to;
248 $hint = $hint ? "next unless $hint" : "";
249 $expr = "1" unless $expr;
251 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
254 return $main::dbh->spot_search($expr, $dayfrom, $dayto, $to-$from, $dxchan);
257 $expr =~ s/\$f(\d\d?)/\$ref->[$1]/g; # swap the letter n for the correct field name
258 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
263 my \@a = (Prefix::cty_data(\$s[1]))[1..3];
264 my \@b = (Prefix::cty_data(\$s[4]))[1..3];
265 push \@s, \@a[0,1], \@b[0,1], \$a[2], \$a[2];
270 my (\$filter, \$hops) = \$dxchan->{spotsfilter}->it(\@s);
271 next unless (\$filter);
273 $checkfilter ||= ' ';
275 dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
277 # build up eval to execute
282 my \@s = split /\\^/;
288 for (\$c = \$#spots; \$c >= 0; \$c--) {
289 \$ref = \$spots[\$c];
292 next if \$count < \$from; # wait until from
294 last if \$count >= \$to; # stop after to
299 dbg("Spot eval: $eval") if isdbg('searcheval');
302 $fp->close; # close any open files
304 for ($i = $count = 0; $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
305 my $now = $fromdate->sub($i); # but you can pick which $maxdays worth
306 last if $now->cmp($todate) <= 0;
309 my $fh = $fp->open($now); # get the next file
312 eval $eval; # do the search on this file
313 last if $count >= $to; # stop after to
314 return ("Spot search error", $@) if $@;
321 # change a freq range->regular expression
325 return undef unless $a < $b;
328 my @a = split //, $a;
329 my @b = split //, $b;
337 if (@b < (length $d)) {
339 } elsif ($aa eq $bb) {
341 } elsif ($aa < $bb) {
344 $out .= "[0-$bb$aa-9]";
350 # format a spot for user output in list mode
353 my $t = ztime($_[2]);
354 my $d = cldate($_[2]);
355 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, ($_[3]||''), "<$_[4]" ;
359 # return all the spots from a day's file as an array of references
360 # the parameter passed is a julian day
365 my $fh = $fp->open(shift);
370 push @spots, [ split '\^' ];
376 # enter the spot for dup checking and return true if it is already a dup
379 my ($freq, $call, $d, $text, $by, $cty) = @_;
382 return 2 if $d < $main::systime - $dupage;
384 # turn the time into minutes (should be already but...)
388 # remove SSID or area
391 # $freq = sprintf "%.1f", $freq; # normalise frequency
392 $freq = int $freq; # normalise frequency
393 $call = substr($call, 0, $maxcalllth) if length $call > $maxcalllth;
396 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
397 $text = uc unpad($text);
398 if ($cty && $text && length $text <= 4) {
399 unless ($text =~ /^C?Q/ || $text =~ /^\d+$/) {
400 my @try = Prefix::cty_data($text);
401 $text = "" if $cty == $try[0];
405 $text = Encode::encode("iso-8859-1", $text) if $main::can_encode && Encode::is_utf8($text, 1);
406 $text =~ s/^\+\w+\s*//; # remove leading LoTW callsign
407 $text = pack("C*", map {$_ & 127} unpack("C*", $text));
408 $text =~ s/\s{2,}[\dA-Z]?[A-Z]\d?$// if length $text > 24;
410 $text = substr($text, 0, $duplth) if length $text > $duplth;
411 my $ldupkey = "X$freq|$call|$by|$text";
412 my $t = DXDupe::find($ldupkey);
413 return 1 if $t && $t - $main::systime > 0;
414 DXDupe::add($ldupkey, $main::systime+$dupage);
415 $otext = substr($otext, 0, $duplth) if length $otext > $duplth;
417 if (length $otext && $otext ne $text) {
418 $ldupkey = "X$freq|$call|$by|$otext";
419 $t = DXDupe::find($ldupkey);
420 return 1 if $t && $t - $main::systime > 0;
421 DXDupe::add($ldupkey, $main::systime+$dupage);
428 return DXDupe::listdups('X', $dupage, @_);
434 my $in = $fp->open($date);
435 my $out = $statp->open($date, 'w');
442 @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);
445 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
446 my $ref = $list{$by} || [0, $dxcc];
448 next unless defined $_;
449 if ($freq >= $_->[1] && $freq <= $_->[2]) {
460 for ($i = 0; $i < @freq+2; $i++) {
463 $statp->write($date, join('^', 'TOTALS', @tot));
465 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
468 for ($i = 0; $i < @freq+2; ++$i) {
471 $statp->write($date, join('^', $call, @$ref));
477 # return true if the stat file is newer than than the spot file
481 my $in = $fp->mtime($date);
482 my $out = $statp->mtime($date);
483 return defined $out && defined $in && $out >= $in;
489 my $date = Julian::Day->new($main::systime)->sub(1);
490 genstats($date) unless checkstats($date);