fix per_minute handling
[spider.git] / perl / Aranea.pm
1 #
2 # The new protocol for real at last
3 #
4 # $Id$
5 #
6 # Copyright (c) 2005 Dirk Koopman G1TLH
7 #
8
9 package Aranea;
10
11 use strict;
12
13 use DXUtil;
14 use DXChannel;
15 use DXUser;
16 use DXM;
17 use DXLog;
18 use DXDebug;
19 use Filter;
20 use Time::HiRes qw(gettimeofday tv_interval);
21 use DXHash;
22 use Route;
23 use Route::Node;
24 use Script;
25 use Verify;
26 use DXDupe;
27 use Thingy;
28 use Thingy::Rt;
29 use Thingy::Hello;
30 use Thingy::Bye;
31 use RouteDB;
32 use DXProt;
33 use DXCommandmode;
34
35 use vars qw($VERSION $BRANCH);
36
37 main::mkver($VERSION = q$Revision$);
38
39 use vars qw(@ISA $ntpflag $dupeage $cf_interval $hello_interval);
40
41 @ISA = qw(DXChannel);
42
43 $ntpflag = 0;                                   # should be set in startup if NTP in use
44 $dupeage = 12*60*60;                    # duplicates stored half a day 
45 $cf_interval = 30*60;                   # interval between config broadcasts
46 $hello_interval = 3*60*60;              # interval between hello broadcasts for me and local users
47
48 my $seqno = 0;
49 my $dayno = 0;
50 my $daystart = 0;
51
52 sub init
53 {
54
55 }
56
57 sub new
58 {
59         my $self = DXChannel::alloc(@_);
60
61         # add this node to the table, the values get filled in later
62         my $pkg = shift;
63         my $call = shift;
64         $self->{'sort'} = 'W';
65         return $self;
66 }
67
68 sub start
69 {
70         my ($self, $line, $sort) = @_;
71         my $call = $self->{call};
72         my $user = $self->{user};
73
74         # log it
75         my $host = $self->{conn}->{peerhost} || "unknown";
76         Log('Aranea', "$call connected from $host");
77
78         # remember type of connection
79         $self->{consort} = $line;
80         $self->{outbound} = $sort eq 'O';
81         my $priv = $user->priv;
82         $priv = $user->priv(1) unless $priv;
83         $self->{priv} = $priv;     # other clusters can always be 'normal' users
84         $self->{lang} = $user->lang || 'en';
85         $self->{consort} = $line;       # save the connection type
86         $self->{here} = 1;
87         $self->{width} = 80;
88
89         # sort out registration
90         $self->{registered} = 1;
91
92         # get the output filters
93         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
94         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
95         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
96         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
97         $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
98
99
100         # get the INPUT filters (these only pertain to Clusters)
101         $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
102         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
103         $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
104         $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
105         $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
106         
107         $self->conn->echo(0) if $self->conn->can('echo');
108         
109         # ping neighbour node stuff
110         my $ping = $user->pingint;
111         $ping = $DXProt::pingint unless defined $ping;
112         $self->{pingint} = $ping;
113         $self->{nopings} = $user->nopings || $DXProt::obscount;
114         $self->{pingtime} = [ ];
115         $self->{pingave} = 999;
116         $self->{metric} ||= 100;
117         $self->{lastping} = $main::systime;
118         
119         $self->state('normal');
120         $self->{pc50_t} = $main::systime;
121
122         # send info to all logged in thingies
123         $self->tell_login('loginn');
124
125         # broadcast our configuration to the world
126         unless ($self->{outbound}) {
127                 my $thing = Thingy::Rt->new_cf;
128                 $thing->broadcast;
129                 $self->lastcf($main::systime);
130         }
131         
132         # run a script send the output to the debug file
133         my $script = new Script(lc $call) || new Script('node_default');
134         $script->run($self) if $script;
135 }
136
137 #
138 # This is the normal despatcher
139 #
140 sub normal
141 {
142         my ($self, $line) = @_;
143         my $thing = input($line);
144         $thing->queue($self) if $thing;
145 }
146
147 #
148 # periodic processing (every second)
149 #
150
151 my $lastmin = time;
152
153 sub process
154 {
155
156         # calc day number
157         my $d = (gmtime($main::systime))[3];
158         if ($d != $dayno) {
159                 $dayno = $d;
160                 $daystart = $main::systime - ($main::systime % 86400);
161         }
162         if ($main::systime >= $lastmin + 60) {
163                 per_minute();
164                 $lastmin = $main::systime;
165         }
166 }
167
168 sub per_minute
169 {
170         # send hello and cf packages periodically
171         foreach my $dxchan (DXChannel::get_all()) {
172                 next if $dxchan == $main::me;
173                 next if $dxchan->is_aranea;
174                 if ($main::systime >= $dxchan->lasthello + $hello_interval) {
175                         my $thing = Thingy::Hello->new(user => $dxchan->call, h => $dxchan->here);
176                         $thing->broadcast($dxchan);
177                         $dxchan->lasthello($main::systime);
178                 }
179                 if ($dxchan->is_node) {
180                         if ($main::systime >= $dxchan->lastcf + $cf_interval) {
181                                 my $call = $dxchan->call;
182                                 my $thing = Thingy::Rt->new(user => $call);
183                                 if (my $nref = Route::Node::get($call)) {
184                                         $thing->copy_pc16_data($nref);
185                                         $thing->broadcast($dxchan);
186                                         $dxchan->lastcf($main::systime);
187                                 } else {
188                                         dbg("Aranea::per_minute: Route::Node for $call disappeared");
189                                         $dxchan->disconnect;
190                                 }
191                         }
192                 }
193         }
194 }
195
196 sub disconnect
197 {
198         my $self = shift;
199         my $call = $self->call;
200
201         return if $self->{disconnecting}++;
202
203         my $thing = Thingy::Bye->new(origin=>$main::mycall, user=>$call);
204         $thing->broadcast($self);
205
206         # get rid of any PC16/17/19
207         DXProt::eph_del_regex("^PC1[679]*$call");
208
209         # do routing stuff, remove me from routing table
210         my $node = Route::Node::get($call);
211         my @rout;
212         if ($node) {
213                 @rout = $node->del($main::routeroot);
214                 
215                 # and all my ephemera as well
216                 for (@rout) {
217                         my $c = $_->call;
218                         DXProt::eph_del_regex("^PC1[679].*$c");
219                 }
220         }
221
222         RouteDB::delete_interface($call);
223         
224         # unbusy and stop and outgoing mail
225         my $mref = DXMsg::get_busy($call);
226         $mref->stop_msg($call) if $mref;
227         
228         # broadcast to all other nodes that all the nodes connected to via me are gone
229         DXProt::route_pc21($self, $main::mycall, undef, @rout) if @rout;
230
231         # remove outstanding pings
232 #       delete $pings{$call};
233         
234         # I was the last node visited
235     $self->user->node($main::mycall);
236
237         # send info to all logged in thingies
238         $self->tell_login('logoutn');
239
240         Log('Aranea', $call . " Disconnected");
241
242         $self->SUPER::disconnect;
243 }
244
245
246 # generate new header (this is a general subroutine, not a method
247 # because it has to be used before a channel is fully initialised).
248 #
249
250 sub formathead
251 {
252         my $mycall = shift;
253         my $dts = shift;
254         my $hop = shift;
255         my $user = shift;
256         my $group = shift;
257         
258         my $s = "$mycall,$dts,$hop";
259         $s .= ",$user" if $user;
260         if ($group) {
261                 $s .= "," unless $user;
262                 $s .= ",$group" if $group;
263         } 
264         return $s;
265 }
266
267 sub genheader
268 {
269         my $mycall = shift;
270         my $to = shift;
271         my $from = shift;
272         
273         my $date = ((($dayno << 1) | $ntpflag) << 18) |  ($main::systime % 86400);
274         my $r = formathead($mycall, sprintf('%6X%04X', $date, $seqno), 0, $from, $to);
275         $seqno++;
276         $seqno = 0 if $seqno > 0x0ffff;
277         return $r;
278 }
279
280 #
281 # decode the date time sequence group
282 #
283
284 sub decode_dts
285 {
286         my $dts = shift;
287         my ($dt, $seqno) = map {hex} unpack "A6 A4", $dts;
288         my $secs = $dt & 0x3FFFF;
289         $dt >>= 18;
290         my $day = $dt >> 1;
291         my $ntp = $dt & 1;
292         my $t;
293         if ($dayno == $day) {
294                 $t = $daystart + $secs;
295         } elsif ($dayno < $day) {
296                 $t = $daystart + (($day-$dayno) * 86400) + $secs;
297         } else {
298                 $t = $daystart + (($dayno-$day) * 86400) + $secs;
299         }
300         return ($t, $seqno, $ntp);
301 }
302
303 # subroutines to encode and decode values in lists 
304 sub tencode
305 {
306         my $s = shift;
307         $s =~ s/([\%=|,\'\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
308 #       $s = "'$s'" if $s =~ / /;
309         return $s;
310 }
311
312 sub tdecode
313 {
314         my $s = shift;
315         $s =~ s/^'(.*)'$/$1/;
316         $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
317         return length $s ? $s : '';
318 }
319
320 sub genmsg
321 {
322         my $thing = shift;
323         my $list = ref $_[0] ? shift : \@_;
324         my ($name) = uc ref $thing;
325         $name =~ /::(\w+)$/;
326         $name = $1;
327         my $head = genheader($thing->{origin}, 
328                                                  ($thing->{group} || $thing->{touser} || $thing->{tonode}),
329                                                  ($thing->{user} || $thing->{fromuser} || $thing->{fromnode})
330                                                 );
331          
332         my $data = uc $name . ',';
333         while (@$list) {
334                 my $k = lc shift @$list;
335                 my $v = $thing->{$k};
336                 $data .= "$k=" . tencode($v) . ',' if defined $v;
337         }
338         chop $data;
339         return "$head|$data";
340 }
341
342
343 sub decode_input
344 {
345         my $self = shift;
346         my $line = shift;
347         return ('I', $self->{call}, $line);
348 }
349
350 sub input
351 {
352         my $line = shift;
353         my ($head, $data) = split /\|/, $line, 2;
354         return unless $head && $data;
355
356         my ($origin, $dts, $hop, $user, $group) = split /,/, $head;
357         return if DXDupe::check("Ara,$origin,$dts", $dupeage);
358         my $err;
359         $err .= "incomplete header," unless $origin && $dts && defined $hop;
360         my ($cmd, $rdata) = split /,/, $data, 2;
361
362         # validate it further
363         $err .= "missing cmd or data," unless $cmd && $data;
364         $err .= "invalid command ($cmd)," unless $cmd =~ /^[A-Z][A-Z0-9]*$/;
365         my ($gp, $tus) = split /:/, $group, 2 if $group;
366
367         $err .= "from me," if $origin eq $main::mycall;
368         $err .= "invalid group ($gp)," if $gp && $gp !~ /^[A-Z0-9]{2,}$/;
369         $err .= "invalid tocall ($tus)," if $tus && !is_callsign($tus);
370         $err .= "invalid fromcall ($user)," if $user && !is_callsign($user);
371
372         my $class = 'Thingy::' . ucfirst(lc $cmd);
373         my $thing;
374         my ($t, $seqno, $ntp) = decode_dts($dts) unless $err;
375         dbg("dts: $dts = $ntp $t($main::systime) $seqno") if isdbg('dts');
376         $err .= "invalid date/seq," unless $t;
377         
378         if ($err) {
379                 chop $err;
380                 dbg("Aranea input: $err");
381         } elsif ($class->can('new')) {
382                 # create the appropriate Thingy
383                 $thing = $class->new();
384
385                 # reconstitute the header but wth hop increased by one
386                 $head = formathead($origin, $dts, ++$hop, $user, $group);
387                 $thing->{Aranea} = "$head|$data";
388
389                 # store useful data
390                 $thing->{origin} = $origin;
391                 $thing->{time} = $t;
392                 $thing->{group} = $gp if $gp;
393                 $thing->{touser} = $tus if $tus;
394                 $thing->{user} = $user if $user;
395                 $thing->{hopsaway} = $hop; 
396
397                 if ($rdata) {
398                         for (split(/,/, $rdata)) {
399                                 if (/=/) {
400                                         my ($k,$v) = split /=/, $_, 2;
401                                         $thing->{$k} = tdecode($v);
402                                 } else {
403                                         $thing->{$_} = 1;
404                                 }
405                         }
406                 }
407                 
408                 # post process the thing, this generally adds on semantic meaning
409                 # does parameter checking etc. It also adds / prepares the thingy so
410                 # this is compatible with older protocol and arranges data so
411                 # that the filtering can still work.
412                 if ($thing->can('from_Aranea')) {
413
414                         # if a thing is ok then return that thing, otherwise return
415                         # nothing
416                         $thing = $thing->from_Aranea;
417                 }
418         }
419         return $thing;
420 }
421
422 # this is the DXChannel send
423 # note that this does NOT send out stuff in same way as other DXChannels
424 # it is just as it comes, no extra bits added (here)
425 sub send                                                # this is always later and always data
426 {
427         my $self = shift;
428         my $conn = $self->{conn};
429         return unless $conn;
430         my $call = $self->{call};
431
432         for (@_) {
433 #               chomp;
434         my @lines = split /\n/;
435                 for (@lines) {
436                         $conn->send_later($_);
437                         dbg("-> D $call $_") if isdbg('chan');
438                 }
439         }
440         $self->{t} = $main::systime;
441 }
442
443 #
444 # load of dummies for DXChannel broadcasts
445 # these will go away in time?
446 # These are all from PC protocol
447 #
448
449 sub dx_spot
450 {
451         my $self = shift;
452         my $line = shift;
453         my $isolate = shift;
454         my ($filter, $hops);
455
456         if ($self->{spotsfilter}) {
457                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
458                 return unless $filter;
459         }
460 #       send_prot_line($self, $filter, $hops, $isolate, $line);
461 }
462
463 sub wwv
464 {
465         my $self = shift;
466         my $line = shift;
467         my $isolate = shift;
468         my ($filter, $hops);
469         
470         if ($self->{wwvfilter}) {
471                 ($filter, $hops) = $self->{wwvfilter}->it(@_);
472                 return unless $filter;
473         }
474 #       send_prot_line($self, $filter, $hops, $isolate, $line)
475 }
476
477 sub wcy
478 {
479         my $self = shift;
480         my $line = shift;
481         my $isolate = shift;
482         my ($filter, $hops);
483
484         if ($self->{wcyfilter}) {
485                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
486                 return unless $filter;
487         }
488 #       send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
489 }
490
491 sub announce
492 {
493         my $self = shift;
494         my $line = shift;
495         my $isolate = shift;
496         my $to = shift;
497         my $target = shift;
498         my $text = shift;
499         my ($filter, $hops);
500
501         if ($self->{annfilter}) {
502                 ($filter, $hops) = $self->{annfilter}->it(@_);
503                 return unless $filter;
504         }
505 #       send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
506 }
507
508 sub chat
509 {
510         goto &announce;
511 }
512
513 1;