4 # Copyright (c) - 1998 Dirk Koopman G1TLH
21 use vars qw($fp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef);
24 $maxspots = 50; # maximum spots to return
25 $defaultspots = 10; # normal number of spots to return
26 $maxdays = 3*31; # normal maximum no of days to go back
28 $duplth = 20; # the length of text to use in the deduping
29 $dupage = 3*3600; # the length of time to hold spot dups
31 # tag, sort, field, priv, special parser
32 ['freq', 'r', 0, 0, \&decodefreq],
33 ['on', 'r', 0, 0, \&decodefreq],
37 ['call_dxcc', 'n', 5],
39 ['origin', 'c', 7, 9],
41 ['call_zone', 'n', 9],
44 ['channel', 'n', 12, 9],
48 # create a Spot Object
53 return bless $self, $class;
60 my @f = split /,/, $l;
68 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
70 my @fr = Bands::get_freq(lc $a, $b);
75 push @out, "$a/$b"; # add them as ranges
78 return ('dfreq', $dxchan->msg('dfreq1', $f));
81 return ('dfreq', $dxchan->msg('e20', $f));
84 return (0, join(',', @out));
89 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
90 $fp = DXLog::new($dirprefix, "dat", 'd');
98 # fix up the full spot data from the basic spot data
101 # $freq, $call, $t, $comment, $spotter = @_
102 my @out = @_[0..4]; # just up to the spotter
104 # normalise frequency
105 $_[0] = sprintf "%.1f", $_[0];
107 # remove ssids if present on spotter
108 $out[4] =~ s/-\d+$//o;
110 # remove leading and trailing spaces
111 $_[3] = unpad($_[3]);
113 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
114 my @dxcc = Prefix::extract($out[1]);
115 my $spotted_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
116 my $spotted_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
117 my $spotted_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
118 push @out, $spotted_dxcc;
119 @dxcc = Prefix::extract($out[4]);
120 my $spotter_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
121 my $spotter_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
122 my $spotter_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
123 push @out, $spotter_dxcc;
125 return (@out, $spotted_itu, $spotted_cq, $spotter_itu, $spotter_cq);
130 my $buf = join("\^", @_[0..7]);
131 $fp->writeunix($_[2], $buf);
134 # search the spot database for records based on the field no and an expression
135 # this returns a set of references to the spots
137 # the expression is a legal perl 'if' statement with the possible fields indicated
142 # $f2 = date in unix format
145 # $f5 = spotted dxcc country
146 # $f6 = spotter dxcc country
150 # In addition you can specify a range of days, this means that it will start searching
151 # from <n> days less than today to <m> days less than today
153 # Also you can select a range of entries so normally you would get the 0th (latest) entry
154 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
156 # This routine is designed to be called as Spot::search(..)
161 my ($expr, $dayfrom, $dayto, $from, $to, $hint) = @_;
167 my @today = Julian::unixtoj(time());
171 $dayfrom = 0 if !$dayfrom;
172 $dayto = $maxdays if !$dayto;
173 @fromdate = Julian::sub(@today, $dayfrom);
174 @todate = Julian::sub(@fromdate, $dayto);
175 $from = 0 unless $from;
176 $to = $defaultspots unless $to;
177 $hint = $hint ? "next unless $hint" : "";
178 $expr = "1" unless $expr;
180 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
182 $expr =~ s/\$f(\d)/\$ref->[$1]/g; # swap the letter n for the correct field name
183 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
185 dbg("search", "hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n");
187 # build up eval to execute
192 push \@spots, [ split '\\^' ];
196 for (\$c = \$#spots; \$c >= 0; \$c--) {
197 \$ref = \$spots[\$c];
200 next if \$count < \$from; # wait until from
202 last if \$count >= \$to; # stop after to
207 $fp->close; # close any open files
209 for ($i = $count = 0; $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
210 my @now = Julian::sub(@fromdate, $i); # but you can pick which $maxdays worth
211 last if Julian::cmp(@now, @todate) <= 0;
214 my $fh = $fp->open(@now); # get the next file
217 eval $eval; # do the search on this file
218 last if $count >= $to; # stop after to
219 return ("Spot search error", $@) if $@;
226 # change a freq range->regular expression
230 return undef unless $a < $b;
233 my @a = split //, $a;
234 my @b = split //, $b;
242 if (@b < (length $d) - 1) {
244 } elsif ($aa eq $bb) {
246 } elsif ($aa < $bb) {
249 $out .= "[0-$bb$aa-9]";
255 # format a spot for user output in 'broadcast' mode
258 my $wantgrid = shift;
259 my $t = ztime($_[2]);
260 my $ref = DXUser->get_current($_[4]);
261 my $loc = $ref->qra if $ref && $ref->qra && $wantgrid;
262 $loc = ' ' . substr($ref->qra, 0, 4) if $loc;
263 $loc = "" unless $loc;
264 return sprintf "DX de %-7.7s%11.1f %-12.12s %-30s %s$loc", "$_[4]:", $_[0], $_[1], $_[3], $t ;
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(@_);
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 = substr($text, 0, $duplth) if length $text > $duplth;
305 $text =~ s/[\\\%]\d+//g;
306 $text =~ s/[^a-zA-Z0-9]//g;
307 my $dupkey = "X$freq|$call|$d|\L$text";
308 return DXDupe::check($dupkey, $main::systime+$dupage);
313 return DXDupe::listdups('X', $dupage, @_);