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', 'nc', 5],
48 ['origin', 'c', 7, 9],
49 ['call_itu', 'ni', 8],
50 ['call_zone', 'nz', 9],
52 ['by_zone', 'nz', 11],
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, "cys", 'd');
101 system("rm -f $main::data/$dirprefix/2001/*.bys");
106 return $fp->{prefix};
109 # fix up the full spot data from the basic spot data
112 # $freq, $call, $t, $comment, $spotter = @_
113 my @out = @_[0..4]; # just up to the spotter
115 # normalise frequency
116 $_[0] = sprintf "%.1f", $_[0];
118 # remove ssids if present on spotter
119 $out[4] =~ s/-\d+$//o;
121 # remove leading and trailing spaces
122 $_[3] = unpad($_[3]);
124 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
125 my @dxcc = Prefix::extract($out[1]);
126 my $spotted_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 666;
127 my $spotted_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
128 my $spotted_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
129 push @out, $spotted_dxcc;
130 @dxcc = Prefix::extract($out[4]);
131 my $spotter_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 666;
132 my $spotter_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
133 my $spotter_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
134 push @out, $spotter_dxcc;
136 return (@out, $spotted_itu, $spotted_cq, $spotter_itu, $spotter_cq);
141 my $buf = join("\^", @_[0..7]);
142 $fp->writeunix($_[2], $buf);
145 # search the spot database for records based on the field no and an expression
146 # this returns a set of references to the spots
148 # the expression is a legal perl 'if' statement with the possible fields indicated
153 # $f2 = date in unix format
156 # $f5 = spotted dxcc country
157 # $f6 = spotter dxcc country
161 # In addition you can specify a range of days, this means that it will start searching
162 # from <n> days less than today to <m> days less than today
164 # Also you can select a range of entries so normally you would get the 0th (latest) entry
165 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
167 # This routine is designed to be called as Spot::search(..)
172 my ($expr, $dayfrom, $dayto, $from, $to, $hint) = @_;
178 my $today = Julian::Day->new(time());
182 $dayfrom = 0 if !$dayfrom;
183 $dayto = $maxdays unless $dayto;
184 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
185 $fromdate = $today->sub($dayfrom);
186 $todate = $fromdate->sub($dayto);
187 $from = 0 unless $from;
188 $to = $defaultspots unless $to;
189 $hint = $hint ? "next unless $hint" : "";
190 $expr = "1" unless $expr;
192 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
194 $expr =~ s/\$f(\d)/\$ref->[$1]/g; # swap the letter n for the correct field name
195 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
197 dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
199 # build up eval to execute
204 push \@spots, [ split '\\^' ];
208 for (\$c = \$#spots; \$c >= 0; \$c--) {
209 \$ref = \$spots[\$c];
212 next if \$count < \$from; # wait until from
214 last if \$count >= \$to; # stop after to
219 $fp->close; # close any open files
221 for ($i = $count = 0; $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
222 my $now = $fromdate->sub($i); # but you can pick which $maxdays worth
223 last if $now->cmp($todate) <= 0;
226 my $fh = $fp->open($now); # get the next file
229 eval $eval; # do the search on this file
230 last if $count >= $to; # stop after to
231 return ("Spot search error", $@) if $@;
238 # change a freq range->regular expression
242 return undef unless $a < $b;
245 my @a = split //, $a;
246 my @b = split //, $b;
254 if (@b < (length $d)) {
256 } elsif ($aa eq $bb) {
258 } elsif ($aa < $bb) {
261 $out .= "[0-$bb$aa-9]";
267 # format a spot for user output in list mode
270 my $t = ztime($_[2]);
271 my $d = cldate($_[2]);
272 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, $_[3], "<$_[4]" ;
276 # return all the spots from a day's file as an array of references
277 # the parameter passed is a julian day
282 my $fh = $fp->open(shift);
287 push @spots, [ split '\^' ];
293 # enter the spot for dup checking and return true if it is already a dup
296 my ($freq, $call, $d, $text) = @_;
299 return 2 if $d < $main::systime - $dupage;
301 $freq = sprintf "%.1f", $freq; # normalise frequency
303 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
304 $text = substr($text, 0, $duplth) if length $text > $duplth;
306 $text = pack("C*", map {$_ & 127} unpack("C*", $text));
307 $text =~ s/[^a-zA-Z0-9]//g;
308 for (0,60,120,180,240,300) {
310 my $dupkey = "X$freq|$call|$dt|\L$text";
311 return 1 if DXDupe::find($dupkey);
313 my $dupkey = "X$freq|$call|$d|\L$text";
314 DXDupe::add($dupkey, $main::systime+$dupage);
320 return DXDupe::listdups('X', $dupage, @_);
326 my $in = $fp->open($date);
327 my $out = $statp->open($date, 'w');
329 [0, Bands::get_freq('160m')],
330 [1, Bands::get_freq('80m')],
331 [2, Bands::get_freq('40m')],
332 [3, Bands::get_freq('30m')],
333 [4, Bands::get_freq('20m')],
334 [5, Bands::get_freq('17m')],
335 [6, Bands::get_freq('15m')],
336 [7, Bands::get_freq('12m')],
337 [8, Bands::get_freq('10m')],
338 [9, Bands::get_freq('6m')],
339 [10, Bands::get_freq('4m')],
340 [11, Bands::get_freq('2m')],
341 [12, Bands::get_freq('220')],
342 [13, Bands::get_freq('70cm')],
343 [14, Bands::get_freq('23cm')],
344 [15, Bands::get_freq('13cm')],
345 [16, Bands::get_freq('9cm')],
346 [17, Bands::get_freq('6cm')],
347 [18, Bands::get_freq('3cm')],
348 [19, Bands::get_freq('12mm')],
349 [20, Bands::get_freq('6cm')],
357 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
358 my $ref = $list{$by} || [0, $dxcc];
360 if ($freq >= $_->[1] && $freq <= $_->[2]) {
372 for ($i = 0; $i < @freq+2; $i++) {
375 $statp->write($date, join('^', 'TOTALS', @tot));
377 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
380 for ($i = 0; $i < @freq+2; ++$i) {
383 $statp->write($date, join('^', $call, @$ref));
389 # return true if the stat file is newer than than the spot file
393 my $in = $fp->mtime($date);
394 my $out = $statp->mtime($date);
395 return defined $out && defined $in && $out >= $in;
401 my $date = Julian::Day->new($main::systime)->sub(1);
402 genstats($date) unless checkstats($date);