remove any leading ::ffff: on ipv4 addresses
[spider.git] / perl / WCY.pm
1 #!/usr/bin/perl
2
3 # The WCY analog of the WWV geomagnetic information and calculation module
4 #
5 # Copyright (c) 2000 - Dirk Koopman G1TLH
6 #
7 #
8 #
9
10 package WCY;
11
12 use DXVars;
13 use DXUtil;
14 use DXLog;
15 use Julian;
16 use IO::File;
17 use DXDebug;
18 use Data::Dumper;
19
20 use strict;
21
22 use vars qw($date $sfi $k $expk $a $r $sa $gmf $au  @allowed @denied $fp $node $from 
23             $dirprefix $param
24             $duplth $dupage $filterdef);
25
26 $fp = 0;                                                # the DXLog fcb
27 $date = 0;                                              # the unix time of the WWV (notional)
28 $sfi = 0;                                               # the current SFI value
29 $k = 0;                                                 # the current K value
30 $a = 0;                                                 # the current A value
31 $r = 0;                                                 # the current R value
32 $sa = "";                                               # solar activity
33 $gmf = "";                                              # Geomag activity
34 $au = 'no';                                             # aurora warning
35 $node = "";                                             # originating node
36 $from = "";                                             # who this came from
37 @allowed = qw(DK0WCY);                                  # if present only these callsigns are regarded as valid WWV updators
38 @denied = ();                                   # if present ignore any wwv from these callsigns
39 $duplth = 20;                                   # the length of text to use in the deduping
40 $dupage = 12*3600;                              # the length of time to hold spot dups
41
42 $dirprefix = "$main::local_data/wcy";
43 $param = "$dirprefix/param";
44
45 $filterdef = bless ([
46                           # tag, sort, field, priv, special parser 
47                           ['by', 'c', 11],
48                           ['origin', 'c', 12],
49                           ['channel', 'c', 13],
50                           ['by_dxcc', 'nc', 14],
51                           ['by_itu', 'ni', 15],
52                           ['by_zone', 'nz', 16],
53                           ['origin_dxcc', 'nc', 17],
54                           ['origin_itu', 'ni', 18],
55                           ['origin_zone', 'nz', 19],
56                          ], 'Filter::Cmd');
57
58 sub init
59 {
60         $fp = DXLog::new('wcy', 'dat', 'm');
61         do "$param" if -e "$param";
62         confess $@ if $@;
63 }
64
65 # write the current data away
66 sub store
67 {
68         my $fh = new IO::File;
69         open $fh, "> $param" or confess "can't open $param $!";
70         print $fh "# WCY data parameter file last mod:", scalar gmtime, "\n";
71         my $dd = new Data::Dumper([ $date, $sfi, $a, $k, $expk, $r, $sa, $gmf, $au, $from, $node, \@denied, \@allowed ], [qw(date sfi a k expk r sa gmf au from node *denied *allowed)]);
72         $dd->Indent(1);
73         $dd->Terse(0);
74         $dd->Quotekeys(0);
75         $fh->print($dd->Dumpxs);
76         $fh->close;
77         
78         # log it
79         $fp->writeunix($date, "$date^$sfi^$a^$k^$expk^$r^$sa^$gmf^$au^$from^$node");
80 }
81
82 # update WWV info in one go (usually from a PC23)
83 sub update
84 {
85         my ($mydate, $mytime, $mysfi, $mya, $myk, $myexpk, $myr, $mysa, $mygmf, $myau, $myfrom, $mynode) = @_;
86         $myfrom =~ s/-\d+$//;
87         if ((@allowed && grep {$_ eq $myfrom} @allowed) || 
88                 (@denied && !grep {$_ eq $myfrom} @denied) ||
89                 (@allowed == 0 && @denied == 0)) {
90                 
91                 #       my $trydate = cltounix($mydate, sprintf("%02d18Z", $mytime));
92                 if ($mydate >= $date) {
93                         if ($myr) {
94                                 $r = 0 + $myr;
95                         } else {
96                                 $r = 0 unless abs ($mysfi - $sfi) > 3;
97                         }
98                         $sfi = $mysfi;
99                         $a = $mya;
100                         $k = $myk;
101                         $expk = $myexpk;
102                         $r = $myr;
103                         $sa = $mysa;
104                         $gmf = $mygmf;
105                         $au = $myau;
106                         $date = $mydate;
107                         $from = $myfrom;
108                         $node = $mynode;
109                         
110                         store();
111                 }
112         }
113 }
114
115 # add or substract an allowed callsign
116 sub allowed
117 {
118         my $flag = shift;
119         if ($flag eq '+') {
120                 push @allowed, map {uc $_} @_;
121         } else {
122                 my $c;
123                 foreach $c (@_) {
124                         @allowed = map {$_ ne uc $c} @allowed; 
125                 } 
126         }
127         store();
128 }
129
130 # add or substract a denied callsign
131 sub denied
132 {
133         my $flag = shift;
134         if ($flag eq '+') {
135                 push @denied, map {uc $_} @_;
136         } else {
137                 my $c;
138                 foreach $c (@_) {
139                         @denied = map {$_ ne uc $c} @denied; 
140                 } 
141         }
142         store();
143 }
144
145 #
146 # print some items from the log backwards in time
147 #
148 # This command outputs a list of n lines starting from line $from to $to
149 #
150 sub search
151 {
152         my $from = shift;
153         my $to = shift;
154         my $date = $fp->unixtoj(shift);
155         my $pattern = shift;
156         my $search;
157         my @out;
158         my $eval;
159         my $count;
160         my $i;
161         
162         $search = 1;
163         $eval = qq(
164                            my \$c;
165                            my \$ref;
166                            for (\$c = \$#in; \$c >= 0; \$c--) {
167                                         \$ref = \$in[\$c];
168                                         if ($search) {
169                                                 \$count++;
170                                                 next if \$count < \$from;
171                                                 push \@out, \$ref;
172                                                 last if \$count >= \$to; # stop after n
173                                         }
174                                 }
175                           );
176         
177         $fp->close;                                     # close any open files
178         my $fh = $fp->open($date); 
179         for ($i = $count = 0; $count < $to; $i++ ) {
180                 my @in = ();
181                 if ($fh) {
182                         while (<$fh>) {
183                                 chomp;
184                                 push @in, [ split '\^' ] if length > 2;
185                         }
186                         eval $eval;                     # do the search on this file
187                         return ("Geomag search error", $@) if $@;
188                         last if $count >= $to; # stop after n
189                 }
190                 $fh = $fp->openprev();  # get the next file
191                 last if !$fh;
192         }
193         
194         return @out;
195 }
196
197 #
198 # the standard log printing interpreting routine.
199 #
200 # every line that is printed should call this routine to be actually visualised
201 #
202 # Don't really know whether this is the correct place to put this stuff, but where
203 # else is correct?
204 #
205 # I get a reference to an array of items
206 #
207 sub print_item
208 {
209         my $r = shift;
210         my $d = cldate($r->[0]);
211         my $t = (gmtime($r->[0]))[2];
212
213         return sprintf("$d   %02d %5d %3d %3d   %3d %3d %-5s %-5s %6s <%s>", 
214                                     $t, @$r[1..9]);
215 }
216
217 #
218 # read in this month's data
219 #
220 sub readfile
221 {
222         my $date = $fp->unixtoj(shift);
223         my $fh = $fp->open($date); 
224         my @spots = ();
225         my @in;
226         
227         if ($fh) {
228                 while (<$fh>) {
229                         chomp;
230                         push @in, [ split '\^' ] if length > 2;
231                 }
232         }
233         return @in;
234 }
235
236 # enter the spot for dup checking and return true if it is already a dup
237 sub dup
238 {
239         my ($d) = @_; 
240
241         # dump if too old
242         return 2 if $d < $main::systime - $dupage;
243  
244         my $dupkey = "C$d";
245         return DXDupe::check($dupkey, $main::systime+$dupage);
246 }
247
248 sub listdups
249 {
250         return DXDupe::listdups('C', $dupage, @_);
251 }
252 1;
253 __END__;
254