28a8dd7201a9690abb654aba76cec1cb75c2b0b6
[spider.git] / perl / Spot.pm
1 #
2 # the dx spot handler
3 #
4 # Copyright (c) - 1998 Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 package Spot;
10
11 use IO::File;
12 use DXVars;
13 use DXDebug;
14 use DXUtil;
15 use DXLog;
16 use Julian;
17 use Prefix;
18 use DXDupe;
19 use Data::Dumper;
20
21 use strict;
22
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;
28
29 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef);
30
31 $fp = undef;
32 $statp = undef;
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
36 $dirprefix = "spots";
37 $duplth = 20;                                   # the length of text to use in the deduping
38 $dupage = 3*3600;               # the length of time to hold spot dups
39 $filterdef = bless ([
40                           # tag, sort, field, priv, special parser 
41                           ['freq', 'r', 0, 0, \&decodefreq],
42                           ['on', 'r', 0, 0, \&decodefreq],
43                           ['call', 'c', 1],
44                           ['info', 't', 3],
45                           ['by', 'c', 4],
46                           ['call_dxcc', 'n', 5],
47                           ['by_dxcc', 'n', 6],
48                           ['origin', 'c', 7, 9],
49                           ['call_itu', 'n', 8],
50                           ['call_zone', 'n', 9],
51                           ['by_itu', 'n', 10],
52                           ['by_zone', 'n', 11],
53                           ['channel', 'n', 12, 9],
54                          ], 'Filter::Cmd');
55
56
57 # create a Spot Object
58 sub new
59 {
60         my $class = shift;
61         my $self = [ @_ ];
62         return bless $self, $class;
63 }
64
65 sub decodefreq
66 {
67         my $dxchan = shift;
68         my $l = shift;
69         my @f = split /,/, $l;
70         my @out;
71         my $f;
72         
73         foreach $f (@f) {
74                 my ($a, $b); 
75                 if (m{^\d+/\d+$}) {
76                         push @out, $f;
77                 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
78                         $b = lc $b if $b;
79                         my @fr = Bands::get_freq(lc $a, $b);
80                         if (@fr) {
81                                 while (@fr) {
82                                         $a = shift @fr;
83                                         $b = shift @fr;
84                                         push @out, "$a/$b";  # add them as ranges
85                                 }
86                         } else {
87                                 return ('dfreq', $dxchan->msg('dfreq1', $f));
88                         }
89                 } else {
90                         return ('dfreq', $dxchan->msg('e20', $f));
91                 }
92         }
93         return (0, join(',', @out));                     
94 }
95
96 sub init
97 {
98         mkdir "$dirprefix", 0777 if !-e "$dirprefix";
99         $fp = DXLog::new($dirprefix, "dat", 'd');
100         $statp = DXLog::new($dirprefix, "bys", 'd');
101 }
102
103 sub prefix
104 {
105         return $fp->{prefix};
106 }
107
108 # fix up the full spot data from the basic spot data
109 sub prepare
110 {
111         # $freq, $call, $t, $comment, $spotter = @_
112         my @out = @_[0..4];      # just up to the spotter
113
114         # normalise frequency
115         $_[0] = sprintf "%.1f", $_[0];
116   
117         # remove ssids if present on spotter
118         $out[4] =~ s/-\d+$//o;
119
120         # remove leading and trailing spaces
121         $_[3] = unpad($_[3]);
122         
123         # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
124         my @dxcc = Prefix::extract($out[1]);
125         my $spotted_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
126         my $spotted_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
127         my $spotted_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
128         push @out, $spotted_dxcc;
129         @dxcc = Prefix::extract($out[4]);
130         my $spotter_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
131         my $spotter_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
132         my $spotter_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
133         push @out, $spotter_dxcc;
134         push @out, $_[5];
135         return (@out, $spotted_itu, $spotted_cq, $spotter_itu, $spotter_cq);
136 }
137
138 sub add
139 {
140         my $buf = join("\^", @_[0..7]);
141         $fp->writeunix($_[2], $buf);
142 }
143
144 # search the spot database for records based on the field no and an expression
145 # this returns a set of references to the spots
146 #
147 # the expression is a legal perl 'if' statement with the possible fields indicated
148 # by $f<n> where :-
149 #
150 #   $f0 = frequency
151 #   $f1 = call
152 #   $f2 = date in unix format
153 #   $f3 = comment
154 #   $f4 = spotter
155 #   $f5 = spotted dxcc country
156 #   $f6 = spotter dxcc country
157 #   $f7 = origin
158 #
159 #
160 # In addition you can specify a range of days, this means that it will start searching
161 # from <n> days less than today to <m> days less than today
162 #
163 # Also you can select a range of entries so normally you would get the 0th (latest) entry
164 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
165 #
166 # This routine is designed to be called as Spot::search(..)
167 #
168
169 sub search
170 {
171         my ($expr, $dayfrom, $dayto, $from, $to, $hint) = @_;
172         my $eval;
173         my @out;
174         my $ref;
175         my $i;
176         my $count;
177         my $today = Julian::Day->new(time());
178         my $fromdate;
179         my $todate;
180
181         $dayfrom = 0 if !$dayfrom;
182         $dayto = $maxdays unless $dayto;
183         $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
184         $fromdate = $today->sub($dayfrom);
185         $todate = $fromdate->sub($dayto);
186         $from = 0 unless $from;
187         $to = $defaultspots unless $to;
188         $hint = $hint ? "next unless $hint" : "";
189         $expr = "1" unless $expr;
190         
191         $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
192
193         $expr =~ s/\$f(\d)/\$ref->[$1]/g; # swap the letter n for the correct field name
194         #  $expr =~ s/\$f(\d)/\$spots[$1]/g;               # swap the letter n for the correct field name
195   
196         dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
197   
198         # build up eval to execute
199         $eval = qq(
200                            while (<\$fh>) {
201                                    $hint;
202                                    chomp;
203                                    push \@spots, [ split '\\^' ];
204                            }
205                            my \$c;
206                            my \$ref;
207                            for (\$c = \$#spots; \$c >= 0; \$c--) {
208                                         \$ref = \$spots[\$c];
209                                         if ($expr) {
210                                                 \$count++;
211                                                 next if \$count < \$from; # wait until from 
212                                                 push(\@out, \$ref);
213                                                 last if \$count >= \$to; # stop after to
214                                         }
215                                 }
216                           );
217
218         $fp->close;                                     # close any open files
219
220         for ($i = $count = 0; $i < $maxdays; ++$i) {    # look thru $maxdays worth of files only
221                 my $now = $fromdate->sub($i); # but you can pick which $maxdays worth
222                 last if $now->cmp($todate) <= 0;         
223         
224                 my @spots = ();
225                 my $fh = $fp->open($now); # get the next file
226                 if ($fh) {
227                         my $in;
228                         eval $eval;                     # do the search on this file
229                         last if $count >= $to; # stop after to
230                         return ("Spot search error", $@) if $@;
231                 }
232         }
233
234         return @out;
235 }
236
237 # change a freq range->regular expression
238 sub ftor
239 {
240         my ($a, $b) = @_;
241         return undef unless $a < $b;
242         $b--;
243         my $d = $b - $a;
244         my @a = split //, $a;
245         my @b = split //, $b;
246         my $out;
247         while (@b > @a) {
248                 $out .= shift @b;
249         }
250         while (@b) {
251                 my $aa = shift @a;
252                 my $bb = shift @b;
253                 if (@b < (length $d)) {
254                         $out .= '\\d';
255                 } elsif ($aa eq $bb) {
256                         $out .= $aa;
257                 } elsif ($aa < $bb) {
258                         $out .= "[$aa-$bb]";
259                 } else {
260                         $out .= "[0-$bb$aa-9]";
261                 }
262         }
263         return $out;
264 }
265
266 # format a spot for user output in 'broadcast' mode
267 sub formatb
268 {
269         my $wantgrid = shift;
270         my $t = ztime($_[2]);
271         my $ref = DXUser->get_current($_[4]);
272         my $loc = $ref->qra if $ref && $ref->qra && $wantgrid;
273         $loc = ' ' . substr($ref->qra, 0, 4) if $loc;
274         $loc = "" unless $loc;
275         return sprintf "DX de %-7.7s%11.1f  %-12.12s %-30s %s$loc", "$_[4]:", $_[0], $_[1], $_[3], $t ;
276 }
277
278 # format a spot for user output in list mode
279 sub formatl
280 {
281         my $t = ztime($_[2]);
282         my $d = cldate($_[2]);
283         return sprintf "%8.1f  %-11s %s %s  %-28.28s%7s>", $_[0], $_[1], $d, $t, $_[3], "<$_[4]" ;
284 }
285
286 #
287 # return all the spots from a day's file as an array of references
288 # the parameter passed is a julian day
289 sub readfile($)
290 {
291         my @spots;
292         
293         my $fh = $fp->open(shift); 
294         if ($fh) {
295                 my $in;
296                 while (<$fh>) {
297                         chomp;
298                         push @spots, [ split '\^' ];
299                 }
300         }
301         return @spots;
302 }
303
304 # enter the spot for dup checking and return true if it is already a dup
305 sub dup
306 {
307         my ($freq, $call, $d, $text) = @_; 
308
309         # dump if too old
310         return 2 if $d < $main::systime - $dupage;
311  
312         $freq = sprintf "%.1f", $freq;       # normalise frequency
313         chomp $text;
314         $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
315         $text = substr($text, 0, $duplth) if length $text > $duplth; 
316         unpad($text);
317         $text = pack("C*", map {$_ & 127} unpack("C*", $text));
318         $text =~ s/[^a-zA-Z0-9]//g;
319         for (0,60,120,180,240,300) {
320                 my $dt = $d - $_;
321                 my $dupkey = "X$freq|$call|$dt|\L$text";
322                 return 1 if DXDupe::find($dupkey);
323         }
324         my $dupkey = "X$freq|$call|$d|\L$text";
325         DXDupe::add($dupkey, $main::systime+$dupage);
326         return 0;
327 }
328
329 sub listdups
330 {
331         return DXDupe::listdups('X', $dupage, @_);
332 }
333
334 sub genstats($)
335 {
336         my $date = shift;
337         my $in = $fp->open($date);
338         my $out = $statp->open($date, 'w');
339         my @freq = (
340                                 [0, Bands::get_freq('160m')],
341                                 [1, Bands::get_freq('80m')],
342                                 [2, Bands::get_freq('40m')],
343                                 [3, Bands::get_freq('30m')],
344                                 [4, Bands::get_freq('20m')],
345                                 [5, Bands::get_freq('17m')],
346                                 [6, Bands::get_freq('15m')],
347                                 [7, Bands::get_freq('12m')],
348                                 [8, Bands::get_freq('10m')],
349                                 [9, Bands::get_freq('6m')],
350                                 [10, Bands::get_freq('4m')],
351                                 [11, Bands::get_freq('2m')],
352                                 [12, Bands::get_freq('70cm')],
353                                 [13, Bands::get_freq('13cm')],
354                                 [14, Bands::get_freq('9cm')],
355                                 [15, Bands::get_freq('6cm')],
356                                 [16, Bands::get_freq('3cm')],
357                                 [17, Bands::get_freq('12mm')],
358                                 [18, Bands::get_freq('6cm')],
359                            );
360         my %list;
361         my @tot;
362         
363         if ($in && $out) {
364                 while (<$in>) {
365                         chomp;
366                         my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
367                         my $ref = $list{$by} || [0, $dxcc];
368                         for (@freq) {
369                                 if ($freq >= $_->[1] && $freq <= $_->[2]) {
370                                         $$ref[$_->[0]+2]++;
371                                         $tot[$_->[0]+2]++;
372                                         $$ref[0]++;
373                                         $tot[0]++;
374                                         $list{$by} = $ref;
375                                         last;
376                                 }
377                         }
378                 }
379
380                 my $i;
381                 for ($i = 0; $i < @freq+2; $i++) {
382                         $tot[$i] ||= 0;
383                 }
384                 $out->write(join('^', 'TOTALS', @tot) . "\n");
385
386                 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
387                         my $ref = $list{$_};
388                         my $call = $_;
389                         for ($i = 0; $i < @freq+2; ++$i) {
390                                 $ref->[$i] ||= 0;
391                         }
392                         $out->write(join('^', $call, @$ref) . "\n");
393                 }
394                 $out->close;
395         }
396 }
397
398 # return true if the stat file is newer than than the spot file
399 sub checkstats($)
400 {
401         my $date = shift;
402         my $in = $fp->mtime($date);
403         my $out = $statp->mtime($date);
404         return defined $out && defined $in && $out >= $in;
405 }
406
407 # daily processing
408 sub daily
409 {
410         my $date = Julian::Day->new($main::systime)->sub(1);
411         genstats($date) unless checkstats($date);
412 }
413 1;
414
415
416
417