added all the spots filter commands - you luckey people
[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
20 use strict;
21 use vars qw($fp $maxspots $defaultspots $maxdays $dirprefix $duplth $dupage $filterdef);
22
23 $fp = undef;
24 $maxspots = 50;                                 # maximum spots to return
25 $defaultspots = 10;                             # normal number of spots to return
26 $maxdays = 35;                                  # normal maximum no of days to go back
27 $dirprefix = "spots";
28 $duplth = 20;                                   # the length of text to use in the deduping
29 $dupage = 3*3600;               # the length of time to hold spot dups
30 $filterdef = bless ([
31                           # tag, sort, field, priv, special parser 
32                           ['freq', 'r', 0, 0, \&decodefreq],
33                           ['call', 'c', 1],
34                           ['info', 't', 3],
35                           ['by', 'c', 4],
36                           ['call_dxcc', 'n', 5],
37                           ['by_dxcc', 'n', 6],
38                           ['origin', 'c', 7, 9],
39                           ['call_itu', 'n', 8],
40                           ['call_zone', 'n', 9],
41                           ['by_itu', 'n', 10],
42                           ['by_zone', 'n', 11],
43                           ['channel', 'n', 12, 9],
44                          ], 'Filter::Cmd');
45
46
47 sub decodefreq
48 {
49         my $dxchan = shift;
50         my $l = shift;
51         my @f = split /,/, $l;
52         my @out;
53         my $f;
54         
55         foreach $f (@f) {
56                 my ($a, $b); 
57                 if (m{^\d+/\d+$}) {
58                         push @out, $f;
59                 } elsif (($a, $b) = $f =~ m{^(\w+)(?:/(\w+))?$}) {
60                         $b = lc $b if $b;
61                         my @fr = Bands::get_freq(lc $a, $b);
62                         if (@fr) {
63                                 while (@fr) {
64                                         $a = shift @fr;
65                                         $b = shift @fr;
66                                         push @out, "$a/$b";  # add them as ranges
67                                 }
68                         } else {
69                                 return ('dfreq', $dxchan->msg('dfreq1', $f));
70                         }
71                 } else {
72                         return ('dfreq', $dxchan->msg('e20', $f));
73                 }
74         }
75         return (0, join(',', @out));                     
76 }
77
78 sub init
79 {
80         mkdir "$dirprefix", 0777 if !-e "$dirprefix";
81         $fp = DXLog::new($dirprefix, "dat", 'd');
82 }
83
84 sub prefix
85 {
86         return $fp->{prefix};
87 }
88
89 # add a spot to the data file (call as Spot::add)
90 sub add
91 {
92         my @spot = @_;                          # $freq, $call, $t, $comment, $spotter = @_
93         my @out = @spot[0..4];      # just up to the spotter
94
95         # normalise frequency
96         $spot[0] = sprintf "%.f", $spot[0];
97   
98         # remove ssids if present on spotter
99         $out[4] =~ s/-\d+$//o;
100
101         # remove leading and trailing spaces
102         $spot[3] = unpad($spot[3]);
103         
104         # add the 'dxcc' country on the end for both spotted and spotter, then the cluster call
105         my @dxcc = Prefix::extract($out[1]);
106         my $spotted_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
107         my $spotted_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
108         my $spotted_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
109         push @out, $spotted_dxcc;
110         @dxcc = Prefix::extract($out[4]);
111         my $spotter_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
112         my $spotter_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
113         my $spotter_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
114         push @out, $spotter_dxcc;
115         push @out, $spot[5];
116         
117         my $buf = join("\^", @out);
118
119         # compare dates to see whether need to open another save file (remember, redefining $fp 
120         # automagically closes the output file (if any)). 
121         $fp->writeunix($out[2], $buf);
122   
123         return (@out, $spotted_itu, $spotted_cq, $spotter_itu, $spotter_cq);
124 }
125
126 # search the spot database for records based on the field no and an expression
127 # this returns a set of references to the spots
128 #
129 # the expression is a legal perl 'if' statement with the possible fields indicated
130 # by $f<n> where :-
131 #
132 #   $f0 = frequency
133 #   $f1 = call
134 #   $f2 = date in unix format
135 #   $f3 = comment
136 #   $f4 = spotter
137 #   $f5 = spotted dxcc country
138 #   $f6 = spotter dxcc country
139 #   $f7 = origin
140 #
141 #
142 # In addition you can specify a range of days, this means that it will start searching
143 # from <n> days less than today to <m> days less than today
144 #
145 # Also you can select a range of entries so normally you would get the 0th (latest) entry
146 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
147 #
148 # This routine is designed to be called as Spot::search(..)
149 #
150
151 sub search
152 {
153         my ($expr, $dayfrom, $dayto, $from, $to) = @_;
154         my $eval;
155         my @out;
156         my $ref;
157         my $i;
158         my $count;
159         my @today = Julian::unixtoj(time());
160         my @fromdate;
161         my @todate;
162
163         $dayfrom = 0 if !$dayfrom;
164         $dayto = $maxdays if !$dayto;
165         @fromdate = Julian::sub(@today, $dayfrom);
166         @todate = Julian::sub(@fromdate, $dayto);
167         $from = 0 unless $from;
168         $to = $defaultspots unless $to;
169         
170         $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
171
172         $expr =~ s/\$f(\d)/\$ref->[$1]/g; # swap the letter n for the correct field name
173         #  $expr =~ s/\$f(\d)/\$spots[$1]/g;               # swap the letter n for the correct field name
174   
175         dbg("search", "expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n");
176   
177         # build up eval to execute
178         $eval = qq(
179                            my \$c;
180                            my \$ref;
181                            for (\$c = \$#spots; \$c >= 0; \$c--) {
182                                         \$ref = \$spots[\$c];
183                                         if ($expr) {
184                                                 \$count++;
185                                                 next if \$count < \$from; # wait until from 
186                                                 push(\@out, \$ref);
187                                                 last if \$count >= \$to; # stop after to
188                                         }
189                                 }
190                           );
191
192         $fp->close;                                     # close any open files
193
194         for ($i = $count = 0; $i < $maxdays; ++$i) {    # look thru $maxdays worth of files only
195                 my @now = Julian::sub(@fromdate, $i); # but you can pick which $maxdays worth
196                 last if Julian::cmp(@now, @todate) <= 0;         
197         
198                 my @spots = ();
199                 my $fh = $fp->open(@now); # get the next file
200                 if ($fh) {
201                         my $in;
202                         while (<$fh>) {
203                                 chomp;
204                                 push @spots, [ split '\^' ];
205                         }
206                         eval $eval;                     # do the search on this file
207                         last if $count >= $to; # stop after to
208                         return ("Spot search error", $@) if $@;
209                 }
210         }
211
212         return @out;
213 }
214
215 # format a spot for user output in 'broadcast' mode
216 sub formatb
217 {
218         my $wantgrid = shift;
219         my $t = ztime($_[2]);
220         my $ref = DXUser->get_current($_[4]);
221         my $loc = $ref->qra if $ref && $ref->qra && $wantgrid;
222         $loc = ' ' . substr($ref->qra, 0, 4) if $loc;
223         $loc = "" unless $loc;
224         return sprintf "DX de %-7.7s%11.1f  %-12.12s %-30s %s$loc", "$_[4]:", $_[0], $_[1], $_[3], $t ;
225 }
226
227 # format a spot for user output in list mode
228 sub formatl
229 {
230         my $t = ztime($_[2]);
231         my $d = cldate($_[2]);
232         return sprintf "%8.1f  %-11s %s %s  %-28.28s%7s>", $_[0], $_[1], $d, $t, $_[3], "<$_[4]" ;
233 }
234
235 #
236 # return all the spots from a day's file as an array of references
237 # the parameter passed is a julian day
238 sub readfile
239 {
240         my @spots;
241         
242         my $fh = $fp->open(@_); 
243         if ($fh) {
244                 my $in;
245                 while (<$fh>) {
246                         chomp;
247                         push @spots, [ split '\^' ];
248                 }
249         }
250         return @spots;
251 }
252
253 # enter the spot for dup checking and return true if it is already a dup
254 sub dup
255 {
256         my ($freq, $call, $d, $text) = @_; 
257
258         # dump if too old
259         return 2 if $d < $main::systime - $dupage;
260  
261         $freq = sprintf "%.1f", $freq;       # normalise frequency
262         chomp $text;
263         $text = substr($text, 0, $duplth) if length $text > $duplth; 
264         unpad($text);
265         $text =~ s/[^a-zA-Z0-9]//g;
266         my $dupkey = "X$freq|$call|$d|\L$text";
267         return DXDupe::check($dupkey, $main::systime+$dupage);
268 }
269
270 sub listdups
271 {
272         return DXDupe::listdups('X', $dupage, @_);
273 }
274 1;
275
276
277
278