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 = 35; # 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],
36 ['call_dxcc', 'n', 5],
38 ['origin', 'c', 7, 9],
40 ['call_zone', 'n', 9],
43 ['channel', 'n', 12, 9],
51 my @f = split /,/, $l;
59 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
61 my @fr = Bands::get_freq(lc $a, $b);
66 push @out, "$a/$b"; # add them as ranges
69 return ('dfreq', $dxchan->msg('dfreq1', $f));
72 return ('dfreq', $dxchan->msg('e20', $f));
75 return (0, join(',', @out));
80 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
81 $fp = DXLog::new($dirprefix, "dat", 'd');
89 # add a spot to the data file (call as Spot::add)
92 my @spot = @_; # $freq, $call, $t, $comment, $spotter = @_
93 my @out = @spot[0..4]; # just up to the spotter
96 $spot[0] = sprintf "%.f", $spot[0];
98 # remove ssids if present on spotter
99 $out[4] =~ s/-\d+$//o;
101 # remove leading and trailing spaces
102 $spot[3] = unpad($spot[3]);
104 # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
105 my @dxcc = Prefix::extract($out[1]);
106 my $spotted_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
107 my $spotted_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
108 my $spotted_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
109 push @out, $spotted_dxcc;
110 @dxcc = Prefix::extract($out[4]);
111 my $spotter_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
112 my $spotter_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
113 my $spotter_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
114 push @out, $spotter_dxcc;
117 my $buf = join("\^", @out);
119 # compare dates to see whether need to open another save file (remember, redefining $fp
120 # automagically closes the output file (if any)).
121 $fp->writeunix($out[2], $buf);
123 return (@out, $spotted_itu, $spotted_cq, $spotter_itu, $spotter_cq);
126 # search the spot database for records based on the field no and an expression
127 # this returns a set of references to the spots
129 # the expression is a legal perl 'if' statement with the possible fields indicated
134 # $f2 = date in unix format
137 # $f5 = spotted dxcc country
138 # $f6 = spotter dxcc country
142 # In addition you can specify a range of days, this means that it will start searching
143 # from <n> days less than today to <m> days less than today
145 # Also you can select a range of entries so normally you would get the 0th (latest) entry
146 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
148 # This routine is designed to be called as Spot::search(..)
153 my ($expr, $dayfrom, $dayto, $from, $to) = @_;
159 my @today = Julian::unixtoj(time());
163 $dayfrom = 0 if !$dayfrom;
164 $dayto = $maxdays if !$dayto;
165 @fromdate = Julian::sub(@today, $dayfrom);
166 @todate = Julian::sub(@fromdate, $dayto);
167 $from = 0 unless $from;
168 $to = $defaultspots unless $to;
170 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
172 $expr =~ s/\$f(\d)/\$ref->[$1]/g; # swap the letter n for the correct field name
173 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
175 dbg("search", "expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n");
177 # build up eval to execute
181 for (\$c = \$#spots; \$c >= 0; \$c--) {
182 \$ref = \$spots[\$c];
185 next if \$count < \$from; # wait until from
187 last if \$count >= \$to; # stop after to
192 $fp->close; # close any open files
194 for ($i = $count = 0; $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
195 my @now = Julian::sub(@fromdate, $i); # but you can pick which $maxdays worth
196 last if Julian::cmp(@now, @todate) <= 0;
199 my $fh = $fp->open(@now); # get the next file
204 push @spots, [ split '\^' ];
206 eval $eval; # do the search on this file
207 last if $count >= $to; # stop after to
208 return ("Spot search error", $@) if $@;
215 # format a spot for user output in 'broadcast' mode
218 my $wantgrid = shift;
219 my $t = ztime($_[2]);
220 my $ref = DXUser->get_current($_[4]);
221 my $loc = $ref->qra if $ref && $ref->qra && $wantgrid;
222 $loc = ' ' . substr($ref->qra, 0, 4) if $loc;
223 $loc = "" unless $loc;
224 return sprintf "DX de %-7.7s%11.1f %-12.12s %-30s %s$loc", "$_[4]:", $_[0], $_[1], $_[3], $t ;
227 # format a spot for user output in list mode
230 my $t = ztime($_[2]);
231 my $d = cldate($_[2]);
232 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $_[0], $_[1], $d, $t, $_[3], "<$_[4]" ;
236 # return all the spots from a day's file as an array of references
237 # the parameter passed is a julian day
242 my $fh = $fp->open(@_);
247 push @spots, [ split '\^' ];
253 # enter the spot for dup checking and return true if it is already a dup
256 my ($freq, $call, $d, $text) = @_;
259 return 2 if $d < $main::systime - $dupage;
261 $freq = sprintf "%.1f", $freq; # normalise frequency
263 $text = substr($text, 0, $duplth) if length $text > $duplth;
265 $text =~ s/[^a-zA-Z0-9]//g;
266 my $dupkey = "X$freq|$call|$d|\L$text";
267 return DXDupe::check($dupkey, $main::systime+$dupage);
272 return DXDupe::listdups('X', $dupage, @_);