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