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