4 # Copyright (c) - 1998 Dirk Koopman G1TLH
24 use vars qw($VERSION $BRANCH);
26 main::mkver($VERSION = q$Revision$);
28 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef $totalspots $hfspots $vhfspots );
32 $maxspots = 100; # maximum spots to return
33 $defaultspots = 10; # normal number of spots to return
34 $maxdays = 100; # normal maximum no of days to go back
36 $duplth = 20; # the length of text to use in the deduping
37 $dupage = 3*3600; # the length of time to hold spot dups
39 # tag, sort, field, priv, special parser
40 ['freq', 'r', 0, 0, \&decodefreq],
41 ['on', 'r', 0, 0, \&decodefreq],
45 ['call_dxcc', 'nc', 5],
47 ['origin', 'c', 7, 9],
48 ['call_itu', 'ni', 8],
49 ['call_zone', 'nz', 9],
51 ['by_zone', 'nz', 11],
52 ['call_state', 'ns', 12],
53 ['by_state', 'ns', 13],
57 $totalspots = $hfspots = $vhfspots = 0;
59 # create a Spot Object
64 return bless $self, $class;
71 my @f = split /,/, $l;
79 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
81 my @fr = Bands::get_freq(lc $a, $b);
86 push @out, "$a/$b"; # add them as ranges
89 return ('dfreq', $dxchan->msg('dfreq1', $f));
92 return ('dfreq', $dxchan->msg('e20', $f));
95 return (0, join(',', @out));
100 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
101 $fp = DXLog::new($dirprefix, "dat", 'd');
102 $statp = DXLog::new($dirprefix, "dys", 'd');
107 return $fp->{prefix};
110 # fix up the full spot data from the basic spot data
113 # $freq, $call, $t, $comment, $spotter = @_
114 my @out = @_[0..4]; # just up to the spotter
116 # normalise frequency
117 $_[0] = sprintf "%.1f", $_[0];
119 # remove ssids and /xxx if present on spotter
120 $out[4] =~ s/-\d+$//o;
122 # remove leading and trailing spaces
123 $out[3] = unpad($out[3]);
125 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
126 my @spd = Prefix::cty_data($out[1]);
128 my @spt = Prefix::cty_data($out[4]);
131 return (@out, @spd[1,2], @spt[1,2], $spd[3], $spt[3]);
136 my $buf = join('^', @_);
137 $fp->writeunix($_[2], $buf);
139 if ($_[0] <= 30000) {
144 if ($_[3] =~ /(?:QSL|VIA)/i) {
145 my $q = QSL::get($_[1]) || new QSL $_[1];
146 $q->update($_[3], $_[2], $_[4]);
150 # search the spot database for records based on the field no and an expression
151 # this returns a set of references to the spots
153 # the expression is a legal perl 'if' statement with the possible fields indicated
158 # $f2 = date in unix format
161 # $f5 = spotted dxcc country
162 # $f6 = spotter dxcc country
166 # In addition you can specify a range of days, this means that it will start searching
167 # from <n> days less than today to <m> days less than today
169 # Also you can select a range of entries so normally you would get the 0th (latest) entry
170 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
172 # This routine is designed to be called as Spot::search(..)
177 my ($expr, $dayfrom, $dayto, $from, $to, $hint, $dxchan) = @_;
183 my $today = Julian::Day->new(time());
187 $dayfrom = 0 if !$dayfrom;
188 $dayto = $maxdays unless $dayto;
189 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
190 $fromdate = $today->sub($dayfrom);
191 $todate = $fromdate->sub($dayto);
192 $from = 0 unless $from;
193 $to = $defaultspots unless $to;
194 $hint = $hint ? "next unless $hint" : "";
195 $expr = "1" unless $expr;
197 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
199 $expr =~ s/\$f(\d\d?)/\$ref->[$1]/g; # swap the letter n for the correct field name
200 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
205 my \@a = (Prefix::cty_data(\$s[1]))[1..3];
206 my \@b = (Prefix::cty_data(\$s[4]))[1..3];
207 push \@s, \@a[0,1], \@b[0,1], \$a[2], \$a[2];
209 my (\$filter, \$hops) = \$dxchan->{spotsfilter}->it(\@s);
210 next unless (\$filter);
212 $checkfilter ||= ' ';
214 dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
216 # build up eval to execute
221 my \@s = 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 dbg("Spot eval: $eval") if isdbg('searcheval');
241 $fp->close; # close any open files
243 for ($i = $count = 0; $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
244 my $now = $fromdate->sub($i); # but you can pick which $maxdays worth
245 last if $now->cmp($todate) <= 0;
248 my $fh = $fp->open($now); # get the next file
251 eval $eval; # do the search on this file
252 last if $count >= $to; # stop after to
253 return ("Spot search error", $@) if $@;
260 # change a freq range->regular expression
264 return undef unless $a < $b;
267 my @a = split //, $a;
268 my @b = split //, $b;
276 if (@b < (length $d)) {
278 } elsif ($aa eq $bb) {
280 } elsif ($aa < $bb) {
283 $out .= "[0-$bb$aa-9]";
289 # format a spot for user output in list mode
292 my $spot = ref $_[0] ? shift : \@_;
294 my $t = ztime($spot->[2]);
295 my $d = cldate($spot->[2]);
296 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $spot->[0], $spot->[1], $d, $t, $spot->[3], "<$spot->[4]" ;
299 # format a spot for normal output
303 my $spot = ref $_[0] ? shift : \@_;
305 my $t = ztime($spot->[2]);
307 my $clth = $dxchan->{consort} eq 'local' ? 29 : 30;
308 my $comment = substr $spot->[3], 0, $clth;
309 $comment .= ' ' x ($clth - length($comment));
310 if ($dxchan->{user}->wantgrid) {
311 my $ref = DXUser->get_current($spot->[4]);
313 $loc = $ref->qra || '';
314 $loc = ' ' . substr($loc, 0, 4) if $loc;
318 if ($dxchan->{user}->wantdxitu) {
319 $loc = ' ' . sprintf("%2d", $spot->[10]) if defined $spot->[10];
320 $comment = substr($comment, 0, $dxchan->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $spot->[8]) if defined $spot->[8];
321 } elsif ($dxchan->{user}->wantdxcq) {
322 $loc = ' ' . sprintf("%2d", $spot->[11]) if defined $spot->[11];
323 $comment = substr($comment, 0, $dxchan->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $spot->[9]) if defined $spot->[9];
324 } elsif ($dxchan->{user}->wantusstate) {
325 $loc = ' ' . $spot->[13] if $spot->[13];
326 $comment = substr($comment, 0, $dxchan->{consort} eq 'local' ? 26 : 27) . ' ' . $spot->[12] if $spot->[12];
329 return sprintf "DX de %-7.7s%11.1f %-12.12s %-s $t$loc", "$spot->[4]:", $spot->[0], $spot->[1], $comment;
333 # return all the spots from a day's file as an array of references
334 # the parameter passed is a julian day
339 my $fh = $fp->open(shift);
344 push @spots, [ split '\^' ];
350 # enter the spot for dup checking and return true if it is already a dup
353 my ($freq, $call, $d, $text, $by) = @_;
356 return 2 if $d < $main::systime - $dupage;
358 # turn the time into minutes (should be already but...)
362 $freq = sprintf "%.1f", $freq; # normalise frequency
363 $call = substr($call, 0, 12) if length $call > 12;
366 $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
367 $text = substr($text, 0, $duplth) if length $text > $duplth;
369 $text = pack("C*", map {$_ & 127} unpack("C*", $text));
370 $text =~ s/[^a-zA-Z0-9]//g;
371 my $ldupkey = "X$freq|$call|\L$text";
372 my $sdupkey = "X$freq|$call|$by";
373 my $t = DXDupe::find($ldupkey);
375 my $dt = $main::systime + $dupage - $t;
376 return 1 if $dt < 300;
378 $t = DXDupe::find($sdupkey);
380 my $dt = $main::systime + $dupage - $t;
381 return 1 if $dt < 300;
383 DXDupe::add($ldupkey, $main::systime+$dupage);
384 DXDupe::add($sdupkey, $main::systime+$dupage);
390 return DXDupe::listdups('X', $dupage, @_);
396 my $in = $fp->open($date);
397 my $out = $statp->open($date, 'w');
404 @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);
407 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
408 my $ref = $list{$by} || [0, $dxcc];
410 next unless defined $_;
411 if ($freq >= $_->[1] && $freq <= $_->[2]) {
422 for ($i = 0; $i < @freq+2; $i++) {
425 $statp->write($date, join('^', 'TOTALS', @tot));
427 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
430 for ($i = 0; $i < @freq+2; ++$i) {
433 $statp->write($date, join('^', $call, @$ref));
439 # return true if the stat file is newer than than the spot file
443 my $in = $fp->mtime($date);
444 my $out = $statp->mtime($date);
445 return defined $out && defined $in && $out >= $in;
451 my $date = Julian::Day->new($main::systime)->sub(1);
452 genstats($date) unless checkstats($date);