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