4 # Copyright (c) - 1998 Dirk Koopman G1TLH
22 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef);
26 $maxspots = 50; # maximum spots to return
27 $defaultspots = 10; # normal number of spots to return
28 $maxdays = 100; # normal maximum no of days to go back
30 $duplth = 20; # the length of text to use in the deduping
31 $dupage = 3*3600; # the length of time to hold spot dups
33 # tag, sort, field, priv, special parser
34 ['freq', 'r', 0, 0, \&decodefreq],
35 ['on', 'r', 0, 0, \&decodefreq],
39 ['call_dxcc', 'n', 5],
41 ['origin', 'c', 7, 9],
43 ['call_zone', 'n', 9],
46 ['channel', 'n', 12, 9],
50 # create a Spot Object
55 return bless $self, $class;
62 my @f = split /,/, $l;
70 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
72 my @fr = Bands::get_freq(lc $a, $b);
77 push @out, "$a/$b"; # add them as ranges
80 return ('dfreq', $dxchan->msg('dfreq1', $f));
83 return ('dfreq', $dxchan->msg('e20', $f));
86 return (0, join(',', @out));
91 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
92 $fp = DXLog::new($dirprefix, "dat", 'd');
93 $statp = DXLog::new($dirprefix, "bys", 'd');
101 # fix up the full spot data from the basic spot data
104 # $freq, $call, $t, $comment, $spotter = @_
105 my @out = @_[0..4]; # just up to the spotter
107 # normalise frequency
108 $_[0] = sprintf "%.1f", $_[0];
110 # remove ssids if present on spotter
111 $out[4] =~ s/-\d+$//o;
113 # remove leading and trailing spaces
114 $_[3] = unpad($_[3]);
116 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
117 my @dxcc = Prefix::extract($out[1]);
118 my $spotted_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
119 my $spotted_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
120 my $spotted_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
121 push @out, $spotted_dxcc;
122 @dxcc = Prefix::extract($out[4]);
123 my $spotter_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
124 my $spotter_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
125 my $spotter_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
126 push @out, $spotter_dxcc;
128 return (@out, $spotted_itu, $spotted_cq, $spotter_itu, $spotter_cq);
133 my $buf = join("\^", @_[0..7]);
134 $fp->writeunix($_[2], $buf);
137 # search the spot database for records based on the field no and an expression
138 # this returns a set of references to the spots
140 # the expression is a legal perl 'if' statement with the possible fields indicated
145 # $f2 = date in unix format
148 # $f5 = spotted dxcc country
149 # $f6 = spotter dxcc country
153 # In addition you can specify a range of days, this means that it will start searching
154 # from <n> days less than today to <m> days less than today
156 # Also you can select a range of entries so normally you would get the 0th (latest) entry
157 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
159 # This routine is designed to be called as Spot::search(..)
164 my ($expr, $dayfrom, $dayto, $from, $to, $hint) = @_;
170 my @today = Julian::unixtoj(time());
174 $dayfrom = 0 if !$dayfrom;
175 $dayto = $maxdays unless $dayto;
176 $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
177 @fromdate = Julian::sub(@today, $dayfrom);
178 @todate = Julian::sub(@fromdate, $dayto);
179 $from = 0 unless $from;
180 $to = $defaultspots unless $to;
181 $hint = $hint ? "next unless $hint" : "";
182 $expr = "1" unless $expr;
184 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
186 $expr =~ s/\$f(\d)/\$ref->[$1]/g; # swap the letter n for the correct field name
187 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
189 dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
191 # build up eval to execute
196 push \@spots, [ split '\\^' ];
200 for (\$c = \$#spots; \$c >= 0; \$c--) {
201 \$ref = \$spots[\$c];
204 next if \$count < \$from; # wait until from
206 last if \$count >= \$to; # stop after to
211 $fp->close; # close any open files
213 for ($i = $count = 0; $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
214 my @now = Julian::sub(@fromdate, $i); # but you can pick which $maxdays worth
215 last if Julian::cmp(@now, @todate) <= 0;
218 my $fh = $fp->open(@now); # get the next file
221 eval $eval; # do the search on this file
222 last if $count >= $to; # stop after to
223 return ("Spot search error", $@) if $@;
230 # change a freq range->regular expression
234 return undef unless $a < $b;
237 my @a = split //, $a;
238 my @b = split //, $b;
246 if (@b < (length $d) - 1) {
248 } elsif ($aa eq $bb) {
250 } elsif ($aa < $bb) {
253 $out .= "[0-$bb$aa-9]";
259 # format a spot for user output in 'broadcast' mode
262 my $wantgrid = shift;
263 my $t = ztime($_[2]);
264 my $ref = DXUser->get_current($_[4]);
265 my $loc = $ref->qra if $ref && $ref->qra && $wantgrid;
266 $loc = ' ' . substr($ref->qra, 0, 4) if $loc;
267 $loc = "" unless $loc;
268 return sprintf "DX de %-7.7s%11.1f %-12.12s %-30s %s$loc", "$_[4]:", $_[0], $_[1], $_[3], $t ;
271 # format a spot for user output in list mode
274 my $t = ztime($_[2]);
275 my $d = cldate($_[2]);
276 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, $_[3], "<$_[4]" ;
280 # return all the spots from a day's file as an array of references
281 # the parameter passed is a julian day
286 my $fh = $fp->open(@_);
291 push @spots, [ split '\^' ];
297 # enter the spot for dup checking and return true if it is already a dup
300 my ($freq, $call, $d, $text) = @_;
303 return 2 if $d < $main::systime - $dupage;
305 $freq = sprintf "%.1f", $freq; # normalise frequency
307 $text = substr($text, 0, $duplth) if length $text > $duplth;
309 $text =~ s/[\\\%]\d+//g;
310 $text =~ s/[^a-zA-Z0-9]//g;
311 my $dupkey = "X$freq|$call|$d|\L$text";
312 return DXDupe::check($dupkey, $main::systime+$dupage);
317 return DXDupe::listdups('X', $dupage, @_);
323 my $in = $fp->open(@date);
324 my $out = $statp->open(@date, 'w');
326 [0, Bands::get_freq('160m')],
327 [1, Bands::get_freq('80m')],
328 [2, Bands::get_freq('40m')],
329 [3, Bands::get_freq('30m')],
330 [4, Bands::get_freq('20m')],
331 [5, Bands::get_freq('17m')],
332 [6, Bands::get_freq('15m')],
333 [7, Bands::get_freq('12m')],
334 [8, Bands::get_freq('10m')],
335 [9, Bands::get_freq('6m')],
336 [10, Bands::get_freq('4m')],
337 [11, Bands::get_freq('2m')],
338 [12, Bands::get_freq('70cm')],
339 [13, Bands::get_freq('13cm')],
340 [14, Bands::get_freq('9cm')],
341 [15, Bands::get_freq('6cm')],
342 [16, Bands::get_freq('3cm')],
343 [17, Bands::get_freq('12mm')],
344 [18, Bands::get_freq('6cm')],
352 my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
353 my $ref = $list{$by} || [0, $dxcc];
355 if ($freq >= $_->[1] && $freq <= $_->[2]) {
367 for ($i = 0; $i < @freq+2; $i++) {
370 $out->write(join('^', 'TOTALS', @tot) . "\n");
372 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
375 for ($i = 0; $i < @freq+2; ++$i) {
378 $out->write(join('^', $call, @$ref) . "\n");
384 # return true if the stat file is newer than than the spot file
388 my $in = $fp->mtime(@date);
389 my $out = $statp->mtime(@date);
390 return defined $out && defined $in && $out >= $in;
396 my @date = Julian::unixtoj($main::systime);
397 @date = Julian::sub(@date, 1);
398 genstats(@date) unless checkstats(@date);