4 # Copyright (c) - 1998 Dirk Koopman G1TLH
24 use vars qw($VERSION $BRANCH);
25 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
26 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
27 $main::build += $VERSION;
28 $main::branch += $BRANCH;
30 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef $totalspots $hfspots $vhfspots $maxcalllth);
34 $maxspots = 100; # maximum spots to return
35 $defaultspots = 10; # normal number of spots to return
36 $maxdays = 100; # normal maximum no of days to go back
38 $duplth = 20; # the length of text to use in the deduping
39 $dupage = 3*3600; # the length of time to hold spot dups
40 $maxcalllth = 12; # the max length of call to take into account for dupes
42 # tag, sort, field, priv, special parser
43 ['freq', 'r', 0, 0, \&decodefreq],
44 ['on', 'r', 0, 0, \&decodefreq],
48 ['call_dxcc', 'nc', 5],
50 ['origin', 'c', 7, 9],
51 ['call_itu', 'ni', 8],
52 ['call_zone', 'nz', 9],
54 ['by_zone', 'nz', 11],
55 ['call_state', 'ns', 12],
56 ['by_state', 'ns', 13],
60 $totalspots = $hfspots = $vhfspots = 0;
62 # create a Spot Object
67 return bless $self, $class;
74 my @f = split /,/, $l;
82 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
84 my @fr = Bands::get_freq(lc $a, $b);
89 push @out, "$a/$b"; # add them as ranges
92 return ('dfreq', $dxchan->msg('dfreq1', $f));
95 return ('dfreq', $dxchan->msg('e20', $f));
98 return (0, join(',', @out));
103 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
104 $fp = DXLog::new($dirprefix, "dat", 'd');
105 $statp = DXLog::new($dirprefix, "dys", 'd');
110 return $fp->{prefix};
113 # fix up the full spot data from the basic spot data
116 # $freq, $call, $t, $comment, $spotter = @_
117 my @out = @_[0..4]; # just up to the spotter
119 # normalise frequency
120 $_[0] = sprintf "%.1f", $_[0];
122 # remove ssids and /xxx if present on spotter
123 $out[4] =~ s/-\d+$//o;
125 # remove leading and trailing spaces
126 $_[3] = unpad($_[3]);
129 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
130 my @spd = Prefix::cty_data($out[1]);
132 my @spt = Prefix::cty_data($out[4]);
135 return (@out, @spd[1,2], @spt[1,2], $spd[3], $spt[3]);
140 my $buf = join('^', @_);
141 $fp->writeunix($_[2], $buf);
143 if ($_[0] <= 30000) {
148 if ($_[3] =~ /(?:QSL|VIA)/i) {
149 my $q = QSL::get($_[1]) || new QSL $_[1];
150 $q->update($_[3], $_[2], $_[4]);
154 # search the spot database for records based on the field no and an expression
155 # this returns a set of references to the spots
157 # the expression is a legal perl 'if' statement with the possible fields indicated
162 # $f2 = date in unix format
165 # $f5 = spotted dxcc country
166 # $f6 = spotter dxcc country
170 # In addition you can specify a range of days, this means that it will start searching
171 # from <n> days less than today to <m> days less than today
173 # Also you can select a range of entries so normally you would get the 0th (latest) entry
174 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
176 # This routine is designed to be called as Spot::search(..)
181 my ($expr, $dayfrom, $dayto, $from, $to, $hint, $dxchan) = @_;
187 my $today = Julian::Day->new(time());
191 $dayfrom = 0 if !$dayfrom;
192 $dayto = $maxdays unless $dayto;
193 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
194 $fromdate = $today->sub($dayfrom);
195 $todate = $fromdate->sub($dayto);
196 $from = 0 unless $from;
197 $to = $defaultspots unless $to;
198 $hint = $hint ? "next unless $hint" : "";
199 $expr = "1" unless $expr;
201 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
203 $expr =~ s/\$f(\d\d?)/\$ref->[$1]/g; # swap the letter n for the correct field name
204 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
209 my \@a = (Prefix::cty_data(\$s[1]))[1..3];
210 my \@b = (Prefix::cty_data(\$s[4]))[1..3];
211 push \@s, \@a[0,1], \@b[0,1], \$a[2], \$a[2];
213 my (\$filter, \$hops) = \$dxchan->{spotsfilter}->it(\@s);
214 next unless (\$filter);
216 $checkfilter ||= ' ';
218 dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
220 # build up eval to execute
225 my \@s = split /\\^/;
231 for (\$c = \$#spots; \$c >= 0; \$c--) {
232 \$ref = \$spots[\$c];
235 next if \$count < \$from; # wait until from
237 last if \$count >= \$to; # stop after to
242 dbg("Spot eval: $eval") if isdbg('searcheval');
245 $fp->close; # close any open files
247 for ($i = $count = 0; $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
248 my $now = $fromdate->sub($i); # but you can pick which $maxdays worth
249 last if $now->cmp($todate) <= 0;
252 my $fh = $fp->open($now); # get the next file
255 eval $eval; # do the search on this file
256 last if $count >= $to; # stop after to
257 return ("Spot search error", $@) if $@;
264 # change a freq range->regular expression
268 return undef unless $a < $b;
271 my @a = split //, $a;
272 my @b = split //, $b;
280 if (@b < (length $d)) {
282 } elsif ($aa eq $bb) {
284 } elsif ($aa < $bb) {
287 $out .= "[0-$bb$aa-9]";
293 # format a spot for user output in list mode
296 my $t = ztime($_[2]);
297 my $d = cldate($_[2]);
298 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, $_[3], "<$_[4]" ;
302 # return all the spots from a day's file as an array of references
303 # the parameter passed is a julian day
308 my $fh = $fp->open(shift);
313 push @spots, [ split '\^' ];
319 # enter the spot for dup checking and return true if it is already a dup
322 my ($freq, $call, $d, $text, $by) = @_;
325 return 2 if $d < $main::systime - $dupage;
327 # turn the time into minutes (should be already but...)
331 $freq = sprintf "%.1f", $freq; # normalise frequency
332 $call = substr($call, 0, $maxcalllth) if length $call > $maxcalllth;
335 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
336 $text = uc unpad($text);
337 my ($prefix) = $text =~ /\b(\w{1,4})$/;
338 $text =~ s/\b\w{1,4}$// if $prefix && Prefix::extract($prefix);
339 $text = substr($text, 0, $duplth) if length $text > $duplth;
340 $text = pack("C*", map {$_ & 127} unpack("C*", $text));
342 my $ldupkey = "X$freq|$call|$by|$text";
343 my $t = DXDupe::find($ldupkey);
344 return 1 if $t && $t - $main::systime > 0;
345 DXDupe::add($ldupkey, $main::systime+$dupage);
346 # my $sdupkey = "X$freq|$call|$by";
347 # $t = DXDupe::find($sdupkey);
348 # return 1 if $t && $t - $main::systime > 0;
349 # DXDupe::add($sdupkey, $main::systime+$dupage);
355 return DXDupe::listdups('X', $dupage, @_);
361 my $in = $fp->open($date);
362 my $out = $statp->open($date, 'w');
369 @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);
372 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
373 my $ref = $list{$by} || [0, $dxcc];
375 next unless defined $_;
376 if ($freq >= $_->[1] && $freq <= $_->[2]) {
387 for ($i = 0; $i < @freq+2; $i++) {
390 $statp->write($date, join('^', 'TOTALS', @tot));
392 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
395 for ($i = 0; $i < @freq+2; ++$i) {
398 $statp->write($date, join('^', $call, @$ref));
404 # return true if the stat file is newer than than the spot file
408 my $in = $fp->mtime($date);
409 my $out = $statp->mtime($date);
410 return defined $out && defined $in && $out >= $in;
416 my $date = Julian::Day->new($main::systime)->sub(1);
417 genstats($date) unless checkstats($date);