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 'broadcast' mode
270 my $wantgrid = shift;
271 my $t = ztime($_[2]);
272 my $ref = DXUser->get_current($_[4]);
273 my $loc = $ref->qra if $ref && $ref->qra && $wantgrid;
274 $loc = ' ' . substr($ref->qra, 0, 4) if $loc;
275 $loc = "" unless $loc;
276 return sprintf "DX de %-7.7s%11.1f %-12.12s %-30s %s$loc", "$_[4]:", $_[0], $_[1], $_[3], $t ;
279 # format a spot for user output in list mode
282 my $t = ztime($_[2]);
283 my $d = cldate($_[2]);
284 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, $_[3], "<$_[4]" ;
288 # return all the spots from a day's file as an array of references
289 # the parameter passed is a julian day
294 my $fh = $fp->open(shift);
299 push @spots, [ split '\^' ];
305 # enter the spot for dup checking and return true if it is already a dup
308 my ($freq, $call, $d, $text) = @_;
311 return 2 if $d < $main::systime - $dupage;
313 $freq = sprintf "%.1f", $freq; # normalise frequency
315 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
316 $text = substr($text, 0, $duplth) if length $text > $duplth;
318 $text = pack("C*", map {$_ & 127} unpack("C*", $text));
319 $text =~ s/[^a-zA-Z0-9]//g;
320 for (0,60,120,180,240,300) {
322 my $dupkey = "X$freq|$call|$dt|\L$text";
323 return 1 if DXDupe::find($dupkey);
325 my $dupkey = "X$freq|$call|$d|\L$text";
326 DXDupe::add($dupkey, $main::systime+$dupage);
332 return DXDupe::listdups('X', $dupage, @_);
338 my $in = $fp->open($date);
339 my $out = $statp->open($date, 'w');
341 [0, Bands::get_freq('160m')],
342 [1, Bands::get_freq('80m')],
343 [2, Bands::get_freq('40m')],
344 [3, Bands::get_freq('30m')],
345 [4, Bands::get_freq('20m')],
346 [5, Bands::get_freq('17m')],
347 [6, Bands::get_freq('15m')],
348 [7, Bands::get_freq('12m')],
349 [8, Bands::get_freq('10m')],
350 [9, Bands::get_freq('6m')],
351 [10, Bands::get_freq('4m')],
352 [11, Bands::get_freq('2m')],
353 [12, Bands::get_freq('220')],
354 [13, Bands::get_freq('70cm')],
355 [14, Bands::get_freq('23cm')],
356 [15, Bands::get_freq('13cm')],
357 [16, Bands::get_freq('9cm')],
358 [17, Bands::get_freq('6cm')],
359 [18, Bands::get_freq('3cm')],
360 [19, Bands::get_freq('12mm')],
361 [20, Bands::get_freq('6cm')],
369 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
370 my $ref = $list{$by} || [0, $dxcc];
372 if ($freq >= $_->[1] && $freq <= $_->[2]) {
384 for ($i = 0; $i < @freq+2; $i++) {
387 $statp->write($date, join('^', 'TOTALS', @tot));
389 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
392 for ($i = 0; $i < @freq+2; ++$i) {
395 $statp->write($date, join('^', $call, @$ref));
401 # return true if the stat file is newer than than the spot file
405 my $in = $fp->mtime($date);
406 my $out = $statp->mtime($date);
407 return defined $out && defined $in && $out >= $in;
413 my $date = Julian::Day->new($main::systime)->sub(1);
414 genstats($date) unless checkstats($date);