4 # Copyright (c) - 1998 Dirk Koopman G1TLH
23 use vars qw($VERSION $BRANCH);
24 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
25 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
26 $main::build += $VERSION;
27 $main::branch += $BRANCH;
29 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef);
33 $maxspots = 50; # maximum spots to return
34 $defaultspots = 10; # normal number of spots to return
35 $maxdays = 100; # normal maximum no of days to go back
37 $duplth = 20; # the length of text to use in the deduping
38 $dupage = 3*3600; # the length of time to hold spot dups
40 # tag, sort, field, priv, special parser
41 ['freq', 'r', 0, 0, \&decodefreq],
42 ['on', 'r', 0, 0, \&decodefreq],
46 ['call_dxcc', 'n', 5],
48 ['origin', 'c', 7, 9],
50 ['call_zone', 'n', 9],
53 ['channel', 'n', 12, 9],
57 # create a Spot Object
62 return bless $self, $class;
69 my @f = split /,/, $l;
77 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
79 my @fr = Bands::get_freq(lc $a, $b);
84 push @out, "$a/$b"; # add them as ranges
87 return ('dfreq', $dxchan->msg('dfreq1', $f));
90 return ('dfreq', $dxchan->msg('e20', $f));
93 return (0, join(',', @out));
98 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
99 $fp = DXLog::new($dirprefix, "dat", 'd');
100 $statp = DXLog::new($dirprefix, "bys", 'd');
105 return $fp->{prefix};
108 # fix up the full spot data from the basic spot data
111 # $freq, $call, $t, $comment, $spotter = @_
112 my @out = @_[0..4]; # just up to the spotter
114 # normalise frequency
115 $_[0] = sprintf "%.1f", $_[0];
117 # remove ssids if present on spotter
118 $out[4] =~ s/-\d+$//o;
120 # remove leading and trailing spaces
121 $_[3] = unpad($_[3]);
123 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
124 my @dxcc = Prefix::extract($out[1]);
125 my $spotted_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
126 my $spotted_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
127 my $spotted_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
128 push @out, $spotted_dxcc;
129 @dxcc = Prefix::extract($out[4]);
130 my $spotter_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
131 my $spotter_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
132 my $spotter_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
133 push @out, $spotter_dxcc;
135 return (@out, $spotted_itu, $spotted_cq, $spotter_itu, $spotter_cq);
140 my $buf = join("\^", @_[0..7]);
141 $fp->writeunix($_[2], $buf);
144 # search the spot database for records based on the field no and an expression
145 # this returns a set of references to the spots
147 # the expression is a legal perl 'if' statement with the possible fields indicated
152 # $f2 = date in unix format
155 # $f5 = spotted dxcc country
156 # $f6 = spotter dxcc country
160 # In addition you can specify a range of days, this means that it will start searching
161 # from <n> days less than today to <m> days less than today
163 # Also you can select a range of entries so normally you would get the 0th (latest) entry
164 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
166 # This routine is designed to be called as Spot::search(..)
171 my ($expr, $dayfrom, $dayto, $from, $to, $hint) = @_;
177 my $today = Julian::Day->new(time());
181 $dayfrom = 0 if !$dayfrom;
182 $dayto = $maxdays unless $dayto;
183 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
184 $fromdate = $today->sub($dayfrom);
185 $todate = $fromdate->sub($dayto);
186 $from = 0 unless $from;
187 $to = $defaultspots unless $to;
188 $hint = $hint ? "next unless $hint" : "";
189 $expr = "1" unless $expr;
191 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
193 $expr =~ s/\$f(\d)/\$ref->[$1]/g; # swap the letter n for the correct field name
194 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
196 dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
198 # build up eval to execute
203 push \@spots, [ split '\\^' ];
207 for (\$c = \$#spots; \$c >= 0; \$c--) {
208 \$ref = \$spots[\$c];
211 next if \$count < \$from; # wait until from
213 last if \$count >= \$to; # stop after to
218 $fp->close; # close any open files
220 for ($i = $count = 0; $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
221 my $now = $fromdate->sub($i); # but you can pick which $maxdays worth
222 last if $now->cmp($todate) <= 0;
225 my $fh = $fp->open($now); # get the next file
228 eval $eval; # do the search on this file
229 last if $count >= $to; # stop after to
230 return ("Spot search error", $@) if $@;
237 # change a freq range->regular expression
241 return undef unless $a < $b;
244 my @a = split //, $a;
245 my @b = split //, $b;
253 if (@b < (length $d) - 1) {
255 } elsif ($aa eq $bb) {
257 } elsif ($aa < $bb) {
260 $out .= "[0-$bb$aa-9]";
266 # format a spot for user output in 'broadcast' mode
269 my $wantgrid = shift;
270 my $t = ztime($_[2]);
271 my $ref = DXUser->get_current($_[4]);
272 my $loc = $ref->qra if $ref && $ref->qra && $wantgrid;
273 $loc = ' ' . substr($ref->qra, 0, 4) if $loc;
274 $loc = "" unless $loc;
275 return sprintf "DX de %-7.7s%11.1f %-12.12s %-30s %s$loc", "$_[4]:", $_[0], $_[1], $_[3], $t ;
278 # format a spot for user output in list mode
281 my $t = ztime($_[2]);
282 my $d = cldate($_[2]);
283 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, $_[3], "<$_[4]" ;
287 # return all the spots from a day's file as an array of references
288 # the parameter passed is a julian day
293 my $fh = $fp->open(shift);
298 push @spots, [ split '\^' ];
304 # enter the spot for dup checking and return true if it is already a dup
307 my ($freq, $call, $d, $text) = @_;
310 return 2 if $d < $main::systime - $dupage;
312 $freq = sprintf "%.1f", $freq; # normalise frequency
314 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
315 $text = substr($text, 0, $duplth) if length $text > $duplth;
317 $text = pack("C*", map {$_ & 127} unpack("C*", $text));
318 $text =~ s/[^a-zA-Z0-9]//g;
319 for (0,60,120,180,240,300) {
321 my $dupkey = "X$freq|$call|$dt|\L$text";
322 return 1 if DXDupe::find($dupkey);
324 my $dupkey = "X$freq|$call|$d|\L$text";
325 DXDupe::add($dupkey, $main::systime+$dupage);
331 return DXDupe::listdups('X', $dupage, @_);
337 my $in = $fp->open($date);
338 my $out = $statp->open($date, 'w');
340 [0, Bands::get_freq('160m')],
341 [1, Bands::get_freq('80m')],
342 [2, Bands::get_freq('40m')],
343 [3, Bands::get_freq('30m')],
344 [4, Bands::get_freq('20m')],
345 [5, Bands::get_freq('17m')],
346 [6, Bands::get_freq('15m')],
347 [7, Bands::get_freq('12m')],
348 [8, Bands::get_freq('10m')],
349 [9, Bands::get_freq('6m')],
350 [10, Bands::get_freq('4m')],
351 [11, Bands::get_freq('2m')],
352 [12, Bands::get_freq('70cm')],
353 [13, Bands::get_freq('13cm')],
354 [14, Bands::get_freq('9cm')],
355 [15, Bands::get_freq('6cm')],
356 [16, Bands::get_freq('3cm')],
357 [17, Bands::get_freq('12mm')],
358 [18, Bands::get_freq('6cm')],
366 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
367 my $ref = $list{$by} || [0, $dxcc];
369 if ($freq >= $_->[1] && $freq <= $_->[2]) {
381 for ($i = 0; $i < @freq+2; $i++) {
384 $out->write(join('^', 'TOTALS', @tot) . "\n");
386 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
389 for ($i = 0; $i < @freq+2; ++$i) {
392 $out->write(join('^', $call, @$ref) . "\n");
398 # return true if the stat file is newer than than the spot file
402 my $in = $fp->mtime($date);
403 my $out = $statp->mtime($date);
404 return defined $out && defined $in && $out >= $in;
410 my $date = Julian::Day->new($main::systime)->sub(1);
411 genstats($date) unless checkstats($date);