fix DL coords
[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
26 main::mkver($VERSION = q$Revision$);
27
28 use vars qw($fp $statp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef $totalspots $hfspots $vhfspots $maxcalllth );
29
30 $fp = undef;
31 $statp = undef;
32 $maxspots = 100;                                # maximum spots to return
33 $defaultspots = 10;                             # normal number of spots to return
34 $maxdays = 100;                                 # normal maximum no of days to go back
35 $dirprefix = "spots";
36 $duplth = 20;                                   # the length of text to use in the deduping
37 $dupage = 3600;                                 # the length of time to hold spot dups
38 $maxcalllth = 12;                               # the maximum call lth
39
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         $out[3] = unpad($out[3]);
126         
127         # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
128         my @spd = Prefix::cty_data($out[1]);
129         push @out, $spd[0];
130         my @spt = Prefix::cty_data($out[4]);
131         push @out, $spt[0];
132         push @out, $_[5];
133         return (@out, @spd[1,2], @spt[1,2], $spd[3], $spt[3]);
134 }
135
136 sub add
137 {
138         my $buf = join('^', @_);
139         $fp->writeunix($_[2], $buf);
140         $totalspots++;
141         if ($_[0] <= 30000) {
142                 $hfspots++;
143         } else {
144                 $vhfspots++;
145         }
146         if ($_[3] =~ /(?:QSL|VIA)/i) {
147                 my $q = QSL::get($_[1]) || new QSL $_[1];
148                 $q->update($_[3], $_[2], $_[4]);
149         }
150 }
151
152 # search the spot database for records based on the field no and an expression
153 # this returns a set of references to the spots
154 #
155 # the expression is a legal perl 'if' statement with the possible fields indicated
156 # by $f<n> where :-
157 #
158 #   $f0 = frequency
159 #   $f1 = call
160 #   $f2 = date in unix format
161 #   $f3 = comment
162 #   $f4 = spotter
163 #   $f5 = spotted dxcc country
164 #   $f6 = spotter dxcc country
165 #   $f7 = origin
166 #
167 #
168 # In addition you can specify a range of days, this means that it will start searching
169 # from <n> days less than today to <m> days less than today
170 #
171 # Also you can select a range of entries so normally you would get the 0th (latest) entry
172 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
173 #
174 # This routine is designed to be called as Spot::search(..)
175 #
176
177 sub search
178 {
179         my ($expr, $dayfrom, $dayto, $from, $to, $hint, $dxchan) = @_;
180         my $eval;
181         my @out;
182         my $ref;
183         my $i;
184         my $count;
185         my $today = Julian::Day->new(time());
186         my $fromdate;
187         my $todate;
188
189         $dayfrom = 0 if !$dayfrom;
190         $dayto = $maxdays unless $dayto;
191         $dayto = $dayfrom + $maxdays if $dayto < $dayfrom;
192         $fromdate = $today->sub($dayfrom);
193         $todate = $fromdate->sub($dayto);
194         $from = 0 unless $from;
195         $to = $defaultspots unless $to;
196         $hint = $hint ? "next unless $hint" : "";
197         $expr = "1" unless $expr;
198         
199         $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
200
201         $expr =~ s/\$f(\d\d?)/\$ref->[$1]/g; # swap the letter n for the correct field name
202         #  $expr =~ s/\$f(\d)/\$spots[$1]/g;               # swap the letter n for the correct field name
203   
204         my $checkfilter;
205         $checkfilter = qq (
206                       if (\@s < 9) {
207                           my \@a = (Prefix::cty_data(\$s[1]))[1..3];
208                           my \@b = (Prefix::cty_data(\$s[4]))[1..3];
209                           push \@s, \@a[0,1], \@b[0,1], \$a[2], \$a[2];  
210                       }
211                           my (\$filter, \$hops) = \$dxchan->{spotsfilter}->it(\@s);
212                           next unless (\$filter);
213                       ) if $dxchan;
214         $checkfilter ||= ' ';
215         
216         dbg("hint='$hint', expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n") if isdbg('search');
217   
218         # build up eval to execute
219         $eval = qq(
220                            while (<\$fh>) {
221                                    $hint;
222                                    chomp;
223                                    my \@s = split /\\^/;
224                    $checkfilter;
225                    push \@spots, \\\@s;
226                            }
227                            my \$c;
228                            my \$ref;
229                            for (\$c = \$#spots; \$c >= 0; \$c--) {
230                                         \$ref = \$spots[\$c];
231                                         if ($expr) {
232                                                 \$count++;
233                                                 next if \$count < \$from; # wait until from 
234                                                 push(\@out, \$ref);
235                                                 last if \$count >= \$to; # stop after to
236                                         }
237                                 }
238                           );
239     
240         dbg("Spot eval: $eval") if isdbg('searcheval');
241         
242
243         $fp->close;                                     # close any open files
244
245         for ($i = $count = 0; $i < $maxdays; ++$i) {    # look thru $maxdays worth of files only
246                 my $now = $fromdate->sub($i); # but you can pick which $maxdays worth
247                 last if $now->cmp($todate) <= 0;         
248         
249                 my @spots = ();
250                 my $fh = $fp->open($now); # get the next file
251                 if ($fh) {
252                         my $in;
253                         eval $eval;                     # do the search on this file
254                         last if $count >= $to; # stop after to
255                         return ("Spot search error", $@) if $@;
256                 }
257         }
258
259         return @out;
260 }
261
262 # change a freq range->regular expression
263 sub ftor
264 {
265         my ($a, $b) = @_;
266         return undef unless $a < $b;
267         $b--;
268         my $d = $b - $a;
269         my @a = split //, $a;
270         my @b = split //, $b;
271         my $out;
272         while (@b > @a) {
273                 $out .= shift @b;
274         }
275         while (@b) {
276                 my $aa = shift @a;
277                 my $bb = shift @b;
278                 if (@b < (length $d)) {
279                         $out .= '\\d';
280                 } elsif ($aa eq $bb) {
281                         $out .= $aa;
282                 } elsif ($aa < $bb) {
283                         $out .= "[$aa-$bb]";
284                 } else {
285                         $out .= "[0-$bb$aa-9]";
286                 }
287         }
288         return $out;
289 }
290
291 # format a spot for user output in list mode
292 sub formatl
293 {
294         my $spot = ref $_[0] ? shift : \@_;
295
296         my $t = ztime($spot->[2]);
297         my $d = cldate($spot->[2]);
298         return sprintf "%8.1f  %-11s %s %s  %-28.28s%7s>", $spot->[0], $spot->[1], $d, $t, $spot->[3], "<$spot->[4]" ;
299 }
300
301 # format a spot for normal output
302 sub format_dx_spot
303 {
304         my $dxchan = shift;
305         my $spot = ref $_[0] ? shift : \@_;
306         
307         my $t = ztime($spot->[2]);
308         my $loc = '';
309         my $clth = $dxchan->{consort} eq 'local' ? 29 : 30;
310         my $comment = substr $spot->[3], 0, $clth; 
311         $comment .= ' ' x ($clth - length($comment));
312         if ($dxchan->{user}->wantgrid) { 
313                 my $ref = DXUser->get_current($spot->[4]);
314                 if ($ref) {
315                         $loc = $ref->qra || '';
316                         $loc = ' ' . substr($loc, 0, 4) if $loc;
317                 }
318         }
319
320         if ($dxchan->{user}->wantdxitu) {
321                 $loc = ' ' . sprintf("%2d", $spot->[10]) if defined $spot->[10];
322                 $comment = substr($comment, 0,  $dxchan->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $spot->[8]) if defined $spot->[8]; 
323         } elsif ($dxchan->{user}->wantdxcq) {
324                 $loc = ' ' . sprintf("%2d", $spot->[11]) if defined $spot->[11];
325                 $comment = substr($comment, 0,  $dxchan->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $spot->[9]) if defined $spot->[9]; 
326         } elsif ($dxchan->{user}->wantusstate) {
327                 $loc = ' ' . $spot->[13] if $spot->[13];
328                 $comment = substr($comment, 0,  $dxchan->{consort} eq 'local' ? 26 : 27) . ' ' . $spot->[12] if $spot->[12]; 
329         }
330
331         return sprintf "DX de %-7.7s%11.1f  %-12.12s %-s $t$loc", "$spot->[4]:", $spot->[0], $spot->[1], $comment;
332 }
333
334 #
335 # return all the spots from a day's file as an array of references
336 # the parameter passed is a julian day
337 sub readfile($)
338 {
339         my @spots;
340         
341         my $fh = $fp->open(shift); 
342         if ($fh) {
343                 my $in;
344                 while (<$fh>) {
345                         chomp;
346                         push @spots, [ split '\^' ];
347                 }
348         }
349         return @spots;
350 }
351
352 # enter the spot for dup checking and return true if it is already a dup
353 sub dup
354 {
355         my ($freq, $call, $d, $text, $by, $cty) = @_; 
356
357         # dump if too old
358         return 2 if $d < $main::systime - $dupage;
359         
360         # turn the time into minutes (should be already but...)
361         $d = int ($d / 60);
362         $d *= 60;
363
364         $by =~ s/-\d+$//g;
365         
366         $freq = sprintf "%.1f", $freq;       # normalise frequency
367         $call = substr($call, 0, $maxcalllth) if length $call > $maxcalllth;
368
369         chomp $text;
370         $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
371         $text = uc unpad($text);
372         if ($cty && $text && length $text <= 4) {
373                 unless ($text =~ /^CQ/ || $text =~ /^\d+$/) {
374                         my @try = Prefix::cty_data($text);
375                         $text = "" if $cty == $try[0];
376                 }
377         }
378         $text = substr($text, 0, $duplth) if length $text > $duplth;
379         $text = pack("C*", map {$_ & 127} unpack("C*", $text));
380         $text =~ s/[^\w]//g;
381         my $ldupkey = "X$freq|$call|$by|$text";
382         my $t = DXDupe::find($ldupkey);
383         return 1 if $t && $t - $main::systime > 0;
384         DXDupe::add($ldupkey, $main::systime+$dupage);
385 #       my $sdupkey = "X$freq|$call|$by";
386 #       $t = DXDupe::find($sdupkey);
387 #       return 1 if $t && $t - $main::systime > 0;      
388 #       DXDupe::add($sdupkey, $main::systime+$dupage);
389         return 0;
390 }
391
392 sub listdups
393 {
394         return DXDupe::listdups('X', $dupage, @_);
395 }
396
397 sub genstats($)
398 {
399         my $date = shift;
400         my $in = $fp->open($date);
401         my $out = $statp->open($date, 'w');
402         my @freq;
403         my %list;
404         my @tot;
405         
406         if ($in && $out) {
407                 my $i = 0;
408                 @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);
409                 while (<$in>) {
410                         chomp;
411                         my ($freq, $by, $dxcc) = (split /\^/)[0,4,6];
412                         my $ref = $list{$by} || [0, $dxcc];
413                         for (@freq) {
414                                 next unless defined $_;
415                                 if ($freq >= $_->[1] && $freq <= $_->[2]) {
416                                         $$ref[$_->[0]+2]++;
417                                         $tot[$_->[0]+2]++;
418                                         $$ref[0]++;
419                                         $tot[0]++;
420                                         $list{$by} = $ref;
421                                         last;
422                                 }
423                         }
424                 }
425
426                 for ($i = 0; $i < @freq+2; $i++) {
427                         $tot[$i] ||= 0;
428                 }
429                 $statp->write($date, join('^', 'TOTALS', @tot));
430
431                 for (sort {$list{$b}->[0] <=> $list{$a}->[0]} keys %list) {
432                         my $ref = $list{$_};
433                         my $call = $_;
434                         for ($i = 0; $i < @freq+2; ++$i) {
435                                 $ref->[$i] ||= 0;
436                         }
437                         $statp->write($date, join('^', $call, @$ref));
438                 }
439                 $statp->close;
440         }
441 }
442
443 # return true if the stat file is newer than than the spot file
444 sub checkstats($)
445 {
446         my $date = shift;
447         my $in = $fp->mtime($date);
448         my $out = $statp->mtime($date);
449         return defined $out && defined $in && $out >= $in;
450 }
451
452 # daily processing
453 sub daily
454 {
455         my $date = Julian::Day->new($main::systime)->sub(1);
456         genstats($date) unless checkstats($date);
457 }
458 1;
459
460
461
462