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