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,0));
26 $main::build += $VERSION;
27 $main::branch += $BRANCH;
29 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef $totalspots $hfspots $vhfspots);
33 $maxspots = 100; # 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', 'nc', 5],
48 ['origin', 'c', 7, 9],
49 ['call_itu', 'ni', 8],
50 ['call_zone', 'nz', 9],
52 ['by_zone', 'nz', 11],
53 ['call_state', 'ns', 12],
54 ['by_state', 'ns', 13],
58 $totalspots = $hfspots = $vhfspots = 0;
60 # create a Spot Object
65 return bless $self, $class;
72 my @f = split /,/, $l;
80 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
82 my @fr = Bands::get_freq(lc $a, $b);
87 push @out, "$a/$b"; # add them as ranges
90 return ('dfreq', $dxchan->msg('dfreq1', $f));
93 return ('dfreq', $dxchan->msg('e20', $f));
96 return (0, join(',', @out));
101 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
102 $fp = DXLog::new($dirprefix, "dat", 'd');
103 $statp = DXLog::new($dirprefix, "dys", 'd');
104 system("rm -f $main::data/$dirprefix/200?/*.bys");
105 system("rm -f $main::data/$dirprefix/200?/*.cys");
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 if present on spotter
123 $out[4] =~ s/-\d+$//o;
125 # remove leading and trailing spaces
126 $_[3] = unpad($_[3]);
128 my ($spotted_dxcc, $spotted_itu, $spotted_cq, $spotted_state) = (666, 0, 0, "");
129 my ($spotter_dxcc, $spotter_itu, $spotter_cq, $spotter_state) = (666, 0, 0, "");
131 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
132 my @dxcc = Prefix::extract($out[1]);
134 $spotted_dxcc = $dxcc[1]->dxcc();
135 $spotted_itu = $dxcc[1]->itu();
136 $spotted_cq = $dxcc[1]->cq();
137 $spotted_state = $dxcc[1]->state();
139 push @out, $spotted_dxcc;
140 @dxcc = Prefix::extract($out[4]);
142 $spotter_dxcc = $dxcc[1]->dxcc();
143 $spotter_itu = $dxcc[1]->itu();
144 $spotter_cq = $dxcc[1]->cq();
145 $spotter_state = $dxcc[1]->state();
147 push @out, $spotter_dxcc;
149 return (@out, $spotted_itu, $spotted_cq, $spotter_itu, $spotter_cq, $spotted_state, $spotter_state);
154 my $buf = join("\^", @_[0..7]);
155 $fp->writeunix($_[2], $buf);
157 if ($_[0] <= 30000) {
164 # search the spot database for records based on the field no and an expression
165 # this returns a set of references to the spots
167 # the expression is a legal perl 'if' statement with the possible fields indicated
172 # $f2 = date in unix format
175 # $f5 = spotted dxcc country
176 # $f6 = spotter dxcc country
180 # In addition you can specify a range of days, this means that it will start searching
181 # from <n> days less than today to <m> days less than today
183 # Also you can select a range of entries so normally you would get the 0th (latest) entry
184 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
186 # This routine is designed to be called as Spot::search(..)
191 my ($expr, $dayfrom, $dayto, $from, $to, $hint) = @_;
197 my $today = Julian::Day->new(time());
201 $dayfrom = 0 if !$dayfrom;
202 $dayto = $maxdays unless $dayto;
203 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
204 $fromdate = $today->sub($dayfrom);
205 $todate = $fromdate->sub($dayto);
206 $from = 0 unless $from;
207 $to = $defaultspots unless $to;
208 $hint = $hint ? "next unless $hint" : "";
209 $expr = "1" unless $expr;
211 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
213 $expr =~ s/\$f(\d)/\$ref->[$1]/g; # swap the letter n for the correct field name
214 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
216 dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
218 # build up eval to execute
223 push \@spots, [ split '\\^' ];
227 for (\$c = \$#spots; \$c >= 0; \$c--) {
228 \$ref = \$spots[\$c];
231 next if \$count < \$from; # wait until from
233 last if \$count >= \$to; # stop after to
238 $fp->close; # close any open files
240 for ($i = $count = 0; $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
241 my $now = $fromdate->sub($i); # but you can pick which $maxdays worth
242 last if $now->cmp($todate) <= 0;
245 my $fh = $fp->open($now); # get the next file
248 eval $eval; # do the search on this file
249 last if $count >= $to; # stop after to
250 return ("Spot search error", $@) if $@;
257 # change a freq range->regular expression
261 return undef unless $a < $b;
264 my @a = split //, $a;
265 my @b = split //, $b;
273 if (@b < (length $d)) {
275 } elsif ($aa eq $bb) {
277 } elsif ($aa < $bb) {
280 $out .= "[0-$bb$aa-9]";
286 # format a spot for user output in list mode
289 my $t = ztime($_[2]);
290 my $d = cldate($_[2]);
291 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, $_[3], "<$_[4]" ;
295 # return all the spots from a day's file as an array of references
296 # the parameter passed is a julian day
301 my $fh = $fp->open(shift);
306 push @spots, [ split '\^' ];
312 # enter the spot for dup checking and return true if it is already a dup
315 my ($freq, $call, $d, $text, $by) = @_;
318 return 2 if $d < $main::systime - $dupage;
320 # turn the time into minutes (should be already but...)
324 $freq = sprintf "%.1f", $freq; # normalise frequency
325 $call = substr($call, 0, 12) if length $call > 12;
327 # quick test now for simple case
328 my $sdupkey = "X$freq|$call|$d|$by";
329 return 1 if DXDupe::find($sdupkey);
332 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
333 $text = substr($text, 0, $duplth) if length $text > $duplth;
335 $text = pack("C*", map {$_ & 127} unpack("C*", $text));
336 $text =~ s/[^a-zA-Z0-9]//g;
337 for (-60, -120, -180, -240, 0, 60, 120, 180, 240, 300) {
339 my $ldupkey = "X$freq|$call|$dt|\L$text";
340 my $sdupkey = "X$freq|$call|$dt|$by";
341 return 1 if DXDupe::find($ldupkey) || DXDupe::find($sdupkey);
343 my $ldupkey = "X$freq|$call|$d|\L$text";
344 $sdupkey = "X$freq|$call|$d|$by";
345 DXDupe::add($ldupkey, $main::systime+$dupage);
346 DXDupe::add($sdupkey, $main::systime+$dupage);
352 return DXDupe::listdups('X', $dupage, @_);
358 my $in = $fp->open($date);
359 my $out = $statp->open($date, 'w');
366 @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);
369 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
370 my $ref = $list{$by} || [0, $dxcc];
372 if ($freq >= $_->[1] && $freq <= $_->[2]) {
383 for ($i = 0; $i < @freq+2; $i++) {
386 $statp->write($date, join('^', 'TOTALS', @tot));
388 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
391 for ($i = 0; $i < @freq+2; ++$i) {
394 $statp->write($date, join('^', $call, @$ref));
400 # return true if the stat file is newer than than the spot file
404 my $in = $fp->mtime($date);
405 my $out = $statp->mtime($date);
406 return defined $out && defined $in && $out >= $in;
412 my $date = Julian::Day->new($main::systime)->sub(1);
413 genstats($date) unless checkstats($date);