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